[daemon-fix] Session and system bus differentiate
[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 __u64 sender_name_to_id(const char* name, DBusError* error)
30 {
31         __u64 sender_id = 0;
32
33         if(!strncmp(name, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
34                 sender_id = strtoull(&name[3], NULL, 10);
35         else
36                 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Could not convert sender of the message into kdbus unique id");
37
38         return sender_id;
39 }
40
41 char* make_kdbus_bus(DBusBusType type, DBusError *error)
42 {
43     struct {
44         struct kdbus_cmd_bus_make head;
45         uint64_t n_size;
46         uint64_t n_type;
47         char name[64];
48     } __attribute__ ((__aligned__(8))) bus_make;
49
50     int fdc, ret;
51     char *bus;
52
53     /*TODO Distinguish session and system bus make*/
54     /*TODO Add dbus_set_error(error, DBUS_ERROR_FAILED,  "...") (?)*/
55
56     _dbus_verbose("Opening /dev/kdbus/control\n");
57     fdc = open("/dev/kdbus/control", O_RDWR|O_CLOEXEC);
58     if (fdc < 0)
59     {
60         _dbus_verbose("--- error %d (%m)\n", fdc);
61         return NULL;
62     }
63
64     memset(&bus_make, 0, sizeof(bus_make));
65     bus_make.head.bloom_size = 64;
66     bus_make.head.flags = KDBUS_MAKE_ACCESS_WORLD;
67
68     if(type == DBUS_BUS_SYSTEM)
69         snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus-%s", getuid(), "system");
70     else
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, "member='IdRemoved'"))
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     if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameChanged'"))
123     {
124           dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
125           return FALSE;
126     }
127     if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameLost'"))
128     {
129           dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
130           return FALSE;
131     }
132     if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameAcquired'"))
133     {
134           dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
135           return FALSE;
136     }
137
138         if(dbus_error_is_set(error))
139         {
140 failed:
141                 _dbus_connection_close_possibly_shared (connection);
142                 dbus_connection_unref (connection);
143                 connection = NULL;
144         }
145         else
146                 _dbus_verbose ("Daemon connected as kdbus client.\n");
147
148         return connection;
149 }
150
151 dbus_bool_t kdbus_register_policy (const DBusString *service_name, DBusConnection* connection)
152 {
153         int fd;
154
155         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
156
157         return register_kdbus_policy(_dbus_string_get_const_data(service_name), fd);
158 }
159
160 dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
161 {
162         int fd;
163
164         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
165
166         return request_kdbus_name(fd, _dbus_string_get_const_data(service_name), flags, sender_id);
167 }
168
169 dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *service_name, __u64 sender_id)
170 {
171         int fd;
172
173         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
174
175         return release_kdbus_name(fd, _dbus_string_get_const_data(service_name), sender_id);
176 }
177
178 dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len)
179 {
180         int fd;
181
182         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
183
184         return list_kdbus_names(fd, listp, array_len);
185 }
186
187 dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* message, const char* text, DBusError* error)
188 {
189         __u64 sender_id;
190
191         sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
192         if(dbus_error_is_set(error))
193                 return FALSE;
194
195         if(!add_match_kdbus (dbus_connection_get_transport(connection), sender_id, text))
196         {
197               dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:%d, %s",
198                               sender_id, _dbus_strerror_from_errno ());
199               return FALSE;
200         }
201
202         return TRUE;
203 }
204
205 dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message, DBusError* error)
206 {
207         __u64 sender_id;
208
209         sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
210         if(dbus_error_is_set(error))
211                 return FALSE;
212
213         if(!remove_match_kdbus (dbus_connection_get_transport(connection), sender_id))
214         {
215               dbus_set_error (error, _dbus_error_from_errno (errno), "Could not remove match rules for id:%d", sender_id);
216               return FALSE;
217         }
218
219         return TRUE;
220 }
221
222 dbus_bool_t kdbus_get_connection_unix_user(DBusConnection* connection, DBusMessage* message, unsigned long* uid, DBusError* error)
223 {
224         char* name = NULL;
225         struct nameInfo info;
226         int inter_ret;
227         dbus_bool_t ret = FALSE;
228
229         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
230         inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
231         if(inter_ret == 0) //name found
232         {
233                 _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
234                 *uid = info.userId;
235                 return TRUE;
236         }
237         else if(inter_ret == -ENOENT)  //name has no owner
238                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get UID of name '%s': no such name", name);
239         else
240         {
241                 _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
242                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name);
243         }
244
245         return ret;
246 }
247
248 dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, DBusMessage* message, unsigned long* pid, DBusError* error)
249 {
250         char* name = NULL;
251         struct nameInfo info;
252         int inter_ret;
253         dbus_bool_t ret = FALSE;
254
255         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
256         inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
257         if(inter_ret == 0) //name found
258         {
259                 _dbus_verbose("Process id:%llu\n", (unsigned long long) info.processId);
260                 *pid = info.processId;
261                 return TRUE;
262         }
263         else if(inter_ret == -ENOENT)  //name has no owner
264                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get PID of name '%s': no such name", name);
265         else
266         {
267                 _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
268                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine PID for '%s'", name);
269         }
270
271         return ret;
272 }
273
274 dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error)
275 {
276         char* name = NULL;
277         struct nameInfo info;
278         int inter_ret;
279         dbus_bool_t ret = FALSE;
280
281         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
282         inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
283         if(inter_ret == -ENOENT)  //name has no owner
284                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get security context of name '%s': no such name", name);
285         else if(inter_ret < 0)
286         {
287                 _dbus_verbose("kdbus error determining security context: err %d (%m)\n", errno);
288                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine security context for '%s'", name);
289         }
290         else
291         {
292                 if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
293                 {
294                       _DBUS_SET_OOM (error);
295                       return FALSE;
296                 }
297                 ret = TRUE;
298         }
299
300         return ret;
301 }
302
303 DBusConnection* create_phantom_connection(DBusConnection* connection, const char* unique_name, DBusError* error)
304 {
305     DBusConnection *phantom_connection;
306     DBusString name;
307
308     _dbus_string_init_const(&name, unique_name);
309
310     phantom_connection = _dbus_connection_new_for_used_transport (dbus_connection_get_transport(connection));
311     if(phantom_connection == NULL)
312         return FALSE;
313     if(!bus_connections_setup_connection(bus_connection_get_connections(connection), phantom_connection))
314     {
315         dbus_connection_unref_phantom(phantom_connection);
316         phantom_connection = NULL;
317         dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", unique_name);
318         goto out;
319     }
320     if(!bus_connection_complete(phantom_connection, &name, error))
321     {
322         bus_connection_disconnected(phantom_connection);
323         phantom_connection = NULL;
324         goto out;
325     }
326
327     _dbus_verbose ("Created phantom connection for %s\n", bus_connection_get_name(phantom_connection));
328
329 out:
330     return phantom_connection;
331 }
332
333 dbus_bool_t register_kdbus_starters(DBusConnection* connection)
334 {
335     int i, len;
336     char **services;
337     dbus_bool_t retval = FALSE;
338     int fd;
339
340     if (!bus_activation_list_services (bus_connection_get_activation (connection), &services, &len))
341         return FALSE;
342
343     _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
344
345     for(i=0; i<len; i++)
346     {
347         if(!register_kdbus_policy(services[i], fd))
348             goto out;
349
350         if (request_kdbus_name(fd, services[i], (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER) , 0) < 0)
351             goto out;
352     }
353     retval = TRUE;
354
355 out:
356     dbus_free_string_array (services);
357     return retval;
358 }
359
360 /*
361 static dbus_bool_t remove_conn_if_name_match (DBusConnection *connection, void *data)
362 {
363     if(!strcmp(bus_connection_get_name(connection), (char*)data))
364     {
365         bus_connection_disconnected(connection);
366         return FALSE; //this is to break foreach function
367     }
368     return TRUE;
369 }*/
370
371 void handleNameOwnerChanged(DBusMessage *msg, BusTransaction *transaction, DBusConnection *connection)
372 {
373     const char *name, *old, *new;
374
375     if(!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID))
376     {
377         _dbus_verbose ("Couldn't get args of NameOwnerChanged signal: .\n");//, error.message);
378         return;
379     }
380
381     _dbus_verbose ("Got NameOwnerChanged signal:\nName: %s\nOld: %s\nNew: %s\n", name, old, new);
382
383     if(!strncmp(name, ":1.", 3))/*if it starts from :1. it is unique name - this might be IdRemoved info*/
384     {
385         if(!strcmp(name, old))  //yes it is - someone has disconnected
386         {
387             DBusConnection* conn;
388
389             conn = bus_connections_find_conn_by_name(bus_connection_get_connections(connection), name);
390             if(conn)
391                 bus_connection_disconnected(conn);
392         }
393     }
394     else //it is well-known name
395     {
396         if((*old != 0) && (strcmp(old, ":1.1")))
397         {
398             DBusMessage *message;
399
400             if(bus_connections_find_conn_by_name(bus_connection_get_connections(connection), old) == NULL)
401                 goto next;
402
403             _dbus_verbose ("Owner '%s' lost name '%s'. Sending NameLost.\n", old, name);
404
405             message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameLost");
406             if (message == NULL)
407                 goto next;
408
409             if (!dbus_message_set_destination (message, old) || !dbus_message_append_args (message,
410                                                                  DBUS_TYPE_STRING, &name,
411                                                                  DBUS_TYPE_INVALID))
412             {
413                 dbus_message_unref (message);
414                 goto next;
415             }
416
417             bus_transaction_send_from_driver (transaction, connection, message);
418             dbus_message_unref (message);
419         }
420     next:
421         if((*new != 0) && (strcmp(new, ":1.1")))
422         {
423             DBusMessage *message;
424
425             _dbus_verbose ("Owner '%s' acquired name '%s'. Sending NameAcquired.\n", new, name);
426
427             message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameAcquired");
428             if (message == NULL)
429                 return;
430
431             if (!dbus_message_set_destination (message, new) || !dbus_message_append_args (message,
432                                                                  DBUS_TYPE_STRING, &name,
433                                                                  DBUS_TYPE_INVALID))
434             {
435                 dbus_message_unref (message);
436                 return;
437             }
438
439             bus_transaction_send_from_driver (transaction, connection, message);
440             dbus_message_unref (message);
441         }
442     }
443 }