e3202e527af20e0eca7731b6bf6930defc9f6f11
[platform/upstream/dbus.git] / bus / kdbus-d.c
1 /*
2  * kdbus-d.c
3  *
4  *  Created on: Sep 4, 2013
5  *      Author: r.pajak
6  *
7  *  kdbus add-on to dbus daemon
8  *
9  */
10
11 #include <dbus/dbus-connection-internal.h>
12 #include "kdbus-d.h"
13 #include <dbus/kdbus.h>
14 #include <dbus/dbus-bus.h>
15 #include "dispatch.h"
16 #include <dbus/kdbus-common.h>
17 #include <dbus/dbus-transport.h>
18 #include <dbus/dbus-transport-kdbus.h>
19 #include "connection.h"
20 #include "activation.h"
21
22 #include <utils.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <errno.h>
28
29 //todo there should be no include below - needed functions should be moved to kdbus-common
30 #include <dbus/dbus-transport-kdbus.h>
31
32 __u64 sender_name_to_id(const char* name, DBusError* error)
33 {
34         __u64 sender_id = 0;
35
36         if(!strncmp(name, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
37                 sender_id = strtoull(&name[3], NULL, 10);
38         else
39                 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Could not convert sender of the message into kdbus unique id");
40
41         return sender_id;
42 }
43
44 char* make_kdbus_bus(DBusBusType type, DBusError *error)
45 {
46     struct {
47         struct kdbus_cmd_bus_make head;
48         uint64_t n_size;
49         uint64_t n_type;
50         char name[64];
51     } __attribute__ ((__aligned__(8))) bus_make;
52
53     int fdc, ret;
54     char *bus;
55
56     /*TODO Distinguish session and system bus make*/
57     /*TODO Add dbus_set_error(error, DBUS_ERROR_FAILED,  "...") (?)*/
58
59     _dbus_verbose("Opening /dev/kdbus/control\n");
60     fdc = open("/dev/kdbus/control", O_RDWR|O_CLOEXEC);
61     if (fdc < 0)
62     {
63         _dbus_verbose("--- error %d (%m)\n", fdc);
64         return NULL;
65     }
66
67     memset(&bus_make, 0, sizeof(bus_make));
68     bus_make.head.bloom_size = 64;
69     bus_make.head.flags = KDBUS_MAKE_ACCESS_WORLD;
70
71     snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus", getuid());
72     bus_make.n_type = KDBUS_MAKE_NAME;
73     bus_make.n_size = KDBUS_PART_HEADER_SIZE + strlen(bus_make.name) + 1;
74     bus_make.head.size = sizeof(struct kdbus_cmd_bus_make) + bus_make.n_size;
75
76     _dbus_verbose("Creating bus '%s'\n", bus_make.name);
77     ret = ioctl(fdc, KDBUS_CMD_BUS_MAKE, &bus_make);
78     if (ret)
79     {
80         _dbus_verbose("--- error %d (%m)\n", ret);
81         return NULL;
82     }
83
84     if (asprintf(&bus, "kdbus:path=/dev/kdbus/%s/bus", bus_make.name) < 0)
85     {
86         BUS_SET_OOM (error);
87         return NULL;
88     }
89
90     _dbus_verbose("Return value '%s'\n", bus);
91         return bus;
92 }
93
94 DBusServer* empty_server_init(char* address)
95 {
96         return dbus_server_init_mini(address);
97 }
98
99 DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error)
100 {
101         DBusConnection* connection;
102         DBusString daemon_name;
103
104         dbus_bus_set_bus_connection_address(type, address);
105
106         connection = dbus_bus_get_private(type, error);  /*todo possibly could be optimised by using lower functions*/
107         if(connection == NULL)
108                 return NULL;
109
110         _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
111         if(!kdbus_register_policy (&daemon_name, connection))
112                 goto failed;
113
114         if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
115                 goto failed;
116
117         if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "type='signal', member='NameLost'"))  //todo handle tis in dispatch
118         {
119               dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
120               return FALSE;
121         }
122
123         if(dbus_error_is_set(error))
124         {
125 failed:
126                 _dbus_connection_close_possibly_shared (connection);
127                 dbus_connection_unref (connection);
128                 connection = NULL;
129         }
130         else
131                 _dbus_verbose ("Daemon connected as kdbus client.\n");
132
133         return connection;
134 }
135
136 dbus_bool_t kdbus_register_policy (const DBusString *service_name, DBusConnection* connection)
137 {
138         int fd;
139
140         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
141
142         return register_kdbus_policy(_dbus_string_get_const_data(service_name), fd);
143 }
144
145 dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
146 {
147         int fd;
148
149         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
150
151         return request_kdbus_name(fd, _dbus_string_get_const_data(service_name), flags, sender_id);
152 }
153
154 dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *service_name, __u64 sender_id)
155 {
156         int fd;
157
158         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
159
160         return release_kdbus_name(fd, _dbus_string_get_const_data(service_name), sender_id);
161 }
162
163 dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len)
164 {
165         int fd;
166
167         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
168
169         return list_kdbus_names(fd, listp, array_len);
170 }
171
172 dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* message, const char* text, DBusError* error)
173 {
174         __u64 sender_id;
175
176         sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
177         if(dbus_error_is_set(error))
178                 return FALSE;
179
180         if(!add_match_kdbus (dbus_connection_get_transport(connection), sender_id, text))
181         {
182               dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:%d, %s",
183                               sender_id, _dbus_strerror_from_errno ());
184               return FALSE;
185         }
186
187         return TRUE;
188 }
189
190 dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message, DBusError* error)
191 {
192         __u64 sender_id;
193
194         sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
195         if(dbus_error_is_set(error))
196                 return FALSE;
197
198         if(!remove_match_kdbus (dbus_connection_get_transport(connection), sender_id))
199         {
200               dbus_set_error (error, _dbus_error_from_errno (errno), "Could not remove match rules for id:%d", sender_id);
201               return FALSE;
202         }
203
204         return TRUE;
205 }
206
207 dbus_bool_t kdbus_get_connection_unix_user(DBusConnection* connection, DBusMessage* message, unsigned long* uid, DBusError* error)
208 {
209         char* name = NULL;
210         struct nameInfo info;
211         int inter_ret;
212         dbus_bool_t ret = FALSE;
213
214         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
215         inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
216         if(inter_ret == 0) //name found
217         {
218                 _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
219                 *uid = info.userId;
220                 return TRUE;
221         }
222         else if(inter_ret == -ENOENT)  //name has no owner
223                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get UID of name '%s': no such name", name);
224         else
225         {
226                 _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
227                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name);
228         }
229
230         return ret;
231 }
232
233 dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, DBusMessage* message, unsigned long* pid, DBusError* error)
234 {
235         char* name = NULL;
236         struct nameInfo info;
237         int inter_ret;
238         dbus_bool_t ret = FALSE;
239
240         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
241         inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
242         if(inter_ret == 0) //name found
243         {
244                 _dbus_verbose("Process id:%llu\n", (unsigned long long) info.processId);
245                 *pid = info.processId;
246                 return TRUE;
247         }
248         else if(inter_ret == -ENOENT)  //name has no owner
249                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get PID of name '%s': no such name", name);
250         else
251         {
252                 _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
253                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine PID for '%s'", name);
254         }
255
256         return ret;
257 }
258
259 dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error)
260 {
261         char* name = NULL;
262         struct nameInfo info;
263         int inter_ret;
264         dbus_bool_t ret = FALSE;
265
266         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
267         inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
268         if(inter_ret == -ENOENT)  //name has no owner
269                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get security context of name '%s': no such name", name);
270         else if(inter_ret < 0)
271         {
272                 _dbus_verbose("kdbus error determining security context: err %d (%m)\n", errno);
273                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine security context for '%s'", name);
274         }
275         else
276         {
277                 if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
278                 {
279                       _DBUS_SET_OOM (error);
280                       return FALSE;
281                 }
282                 ret = TRUE;
283         }
284
285         return ret;
286 }
287
288 DBusConnection* create_phantom_connection(DBusConnection* connection, const char* unique_name)
289 {
290     DBusConnection *phantom_connection;
291     DBusString name;
292     DBusError error;
293
294     _dbus_string_init_const(&name, unique_name);
295
296     phantom_connection = _dbus_connection_new_for_used_transport (dbus_connection_get_transport(connection));
297     if(phantom_connection == NULL)
298         return FALSE;
299     if(!bus_connections_setup_connection(bus_connection_get_connections(connection), phantom_connection))
300     {
301         /*todo FIXME something should be done to clean up the phantom connection
302          * but we can't use standard disconnect, unref or last_unref because the transport is taken from connection
303          * so we probably should write own function on the basis of _dbus_connection_last_unref
304          */
305         phantom_connection = NULL;
306     }
307     if(!bus_connection_complete(phantom_connection, &name, &error))
308     {
309         /* todo FIXME exactly the same issue as above */
310         phantom_connection = NULL;
311     }
312
313     _dbus_verbose ("Created phantom connection for %s\n", bus_connection_get_name(phantom_connection));
314
315     return phantom_connection;
316 }
317
318 dbus_bool_t register_kdbus_starters(DBusConnection* connection)
319 {
320     int i, len;
321     char **services;
322     dbus_bool_t retval = FALSE;
323     int fd;
324
325     if (!bus_activation_list_services (bus_connection_get_activation (connection), &services, &len))
326         return FALSE;
327
328     _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
329
330    /* for(i=0; i<len; i++)
331     {
332         if (request_kdbus_name(fd, services[i], DBUS_NAME_FLAG_ALLOW_REPLACEMENT, 1) < 0)
333             goto out;
334     }*/
335     retval = TRUE;
336
337 out:
338     dbus_free_string_array (services);
339     return retval;
340 }