789730ebecd90ad069589cb3c56b11c92584f24a
[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 #include "services.h"
22
23 #include <utils.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <errno.h>
29
30 __u64 sender_name_to_id(const char* name, DBusError* error)
31 {
32         __u64 sender_id = 0;
33
34         if(!strncmp(name, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
35                 sender_id = strtoull(&name[3], NULL, 10);
36         else
37                 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Could not convert sender of the message into kdbus unique id");
38
39         return sender_id;
40 }
41
42 char* make_kdbus_bus(DBusBusType type, DBusError *error)
43 {
44     struct {
45         struct kdbus_cmd_bus_make head;
46         uint64_t n_size;
47         uint64_t n_type;
48         char name[64];
49     } __attribute__ ((__aligned__(8))) bus_make;
50
51     int fdc, ret;
52     char *bus;
53
54     /*TODO Distinguish session and system bus make*/
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         dbus_set_error(error, DBUS_ERROR_FAILED, "Opening /dev/kdbus/control failed: %d (%m)", fdc);
62         return NULL;
63     }
64
65     memset(&bus_make, 0, sizeof(bus_make));
66     bus_make.head.bloom_size = 64;
67     bus_make.head.flags = KDBUS_MAKE_ACCESS_WORLD;
68
69     if(type == DBUS_BUS_SYSTEM)
70         snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus-%s", getuid(), "system");
71     else
72         snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus-%u", getuid(), getpid());
73     bus_make.n_type = KDBUS_MAKE_NAME;
74     bus_make.n_size = KDBUS_PART_HEADER_SIZE + strlen(bus_make.name) + 1;
75     bus_make.head.size = sizeof(struct kdbus_cmd_bus_make) + bus_make.n_size;
76
77     _dbus_verbose("Creating bus '%s'\n", bus_make.name);
78     ret = ioctl(fdc, KDBUS_CMD_BUS_MAKE, &bus_make);
79     if (ret)
80     {
81         _dbus_verbose("--- error %d (%m)\n", ret);
82         dbus_set_error(error, DBUS_ERROR_FAILED, "Creating bus '%s' failed: %d (%m)", bus_make.name, fdc);
83         return NULL;
84     }
85
86     if (asprintf(&bus, "kdbus:path=/dev/kdbus/%s/bus", bus_make.name) < 0)
87     {
88         BUS_SET_OOM (error);
89         return NULL;
90     }
91
92     _dbus_verbose("Return value '%s'\n", bus);
93         return bus;
94 }
95
96 DBusServer* empty_server_init(char* address)
97 {
98         return dbus_server_init_mini(address);
99 }
100
101 DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error)
102 {
103         DBusConnection* connection;
104         DBusString daemon_name;
105
106         dbus_bus_set_bus_connection_address(type, address);
107
108         connection = dbus_bus_get_private(type, error);  /*todo possibly could be optimised by using lower functions*/
109         if(connection == NULL)
110                 return NULL;
111
112         _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
113         if(!kdbus_register_policy (&daemon_name, connection))
114                 goto failed;
115
116         if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
117                 goto failed;
118
119     if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='IdRemoved'"))
120     {
121           dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
122           return FALSE;
123     }
124     if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameChanged'"))
125     {
126           dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
127           return FALSE;
128     }
129     if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameLost'"))
130     {
131           dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
132           return FALSE;
133     }
134     if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameAcquired'"))
135     {
136           dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
137           return FALSE;
138     }
139
140         if(dbus_error_is_set(error))
141         {
142 failed:
143                 _dbus_connection_close_possibly_shared (connection);
144                 dbus_connection_unref (connection);
145                 connection = NULL;
146         }
147         else
148                 _dbus_verbose ("Daemon connected as kdbus client.\n");
149
150         return connection;
151 }
152
153 dbus_bool_t kdbus_register_policy (const DBusString *service_name, DBusConnection* connection)
154 {
155         int fd;
156
157         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
158
159         return register_kdbus_policy(_dbus_string_get_const_data(service_name), fd);
160 }
161
162 dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
163 {
164         int fd;
165
166         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
167
168         return request_kdbus_name(fd, _dbus_string_get_const_data(service_name), flags, sender_id);
169 }
170
171 dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *service_name, __u64 sender_id)
172 {
173         int fd;
174
175         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
176
177         return release_kdbus_name(fd, _dbus_string_get_const_data(service_name), sender_id);
178 }
179
180 dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len)
181 {
182         int fd;
183
184         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
185
186         return list_kdbus_names(fd, listp, array_len);
187 }
188
189 dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* message, const char* text, DBusError* error)
190 {
191         __u64 sender_id;
192
193         sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
194         if(dbus_error_is_set(error))
195                 return FALSE;
196
197         if(!add_match_kdbus (dbus_connection_get_transport(connection), sender_id, text))
198         {
199               dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:%d, %s",
200                               sender_id, _dbus_strerror_from_errno ());
201               return FALSE;
202         }
203
204         return TRUE;
205 }
206
207 dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message, DBusError* error)
208 {
209         __u64 sender_id;
210
211         sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
212         if(dbus_error_is_set(error))
213                 return FALSE;
214
215         if(!remove_match_kdbus (dbus_connection_get_transport(connection), sender_id))
216         {
217               dbus_set_error (error, _dbus_error_from_errno (errno), "Could not remove match rules for id:%d", sender_id);
218               return FALSE;
219         }
220
221         return TRUE;
222 }
223
224 dbus_bool_t kdbus_get_connection_unix_user(DBusConnection* connection, DBusMessage* message, unsigned long* uid, DBusError* error)
225 {
226         char* name = NULL;
227         struct nameInfo info;
228         int inter_ret;
229         dbus_bool_t ret = FALSE;
230
231         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
232         inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
233         if(inter_ret == 0) //name found
234         {
235                 _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
236                 *uid = info.userId;
237                 return TRUE;
238         }
239         else if(inter_ret == -ENOENT)  //name has no owner
240                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get UID of name '%s': no such name", name);
241         else
242         {
243                 _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
244                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name);
245         }
246
247         return ret;
248 }
249
250 dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, DBusMessage* message, unsigned long* pid, DBusError* error)
251 {
252         char* name = NULL;
253         struct nameInfo info;
254         int inter_ret;
255         dbus_bool_t ret = FALSE;
256
257         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
258         inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
259         if(inter_ret == 0) //name found
260         {
261                 _dbus_verbose("Process id:%llu\n", (unsigned long long) info.processId);
262                 *pid = info.processId;
263                 return TRUE;
264         }
265         else if(inter_ret == -ENOENT)  //name has no owner
266                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get PID of name '%s': no such name", name);
267         else
268         {
269                 _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
270                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine PID for '%s'", name);
271         }
272
273         return ret;
274 }
275
276 dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error)
277 {
278         char* name = NULL;
279         struct nameInfo info;
280         int inter_ret;
281         dbus_bool_t ret = FALSE;
282
283         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
284         inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
285         if(inter_ret == -ENOENT)  //name has no owner
286                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get security context of name '%s': no such name", name);
287         else if(inter_ret < 0)
288         {
289                 _dbus_verbose("kdbus error determining security context: err %d (%m)\n", errno);
290                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine security context for '%s'", name);
291         }
292         else
293         {
294                 if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
295                 {
296                       _DBUS_SET_OOM (error);
297                       return FALSE;
298                 }
299                 ret = TRUE;
300         }
301
302         return ret;
303 }
304
305 DBusConnection* create_phantom_connection(DBusConnection* connection, const char* unique_name, DBusError* error)
306 {
307     DBusConnection *phantom_connection;
308     DBusString name;
309
310     _dbus_string_init_const(&name, unique_name);
311
312     phantom_connection = _dbus_connection_new_for_used_transport (dbus_connection_get_transport(connection));
313     if(phantom_connection == NULL)
314         return FALSE;
315     if(!bus_connections_setup_connection(bus_connection_get_connections(connection), phantom_connection))
316     {
317         dbus_connection_unref_phantom(phantom_connection);
318         phantom_connection = NULL;
319         dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", unique_name);
320         goto out;
321     }
322     if(!bus_connection_complete(phantom_connection, &name, error))
323     {
324         bus_connection_disconnected(phantom_connection);
325         phantom_connection = NULL;
326         goto out;
327     }
328
329     _dbus_verbose ("Created phantom connection for %s\n", bus_connection_get_name(phantom_connection));
330
331 out:
332     return phantom_connection;
333 }
334
335 dbus_bool_t register_kdbus_starters(DBusConnection* connection)
336 {
337     int i,j, len;
338     char **services;
339     dbus_bool_t retval = FALSE;
340     int fd;
341     BusTransaction *transaction;
342     DBusString name;
343
344     transaction = bus_transaction_new (bus_connection_get_context(connection));
345     if (transaction == NULL)
346         return FALSE;
347
348     if (!bus_activation_list_services (bus_connection_get_activation (connection), &services, &len))
349         return FALSE;
350
351     _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
352     _dbus_string_init(&name);
353
354     for(i=0; i<len; i++)
355     {
356         if(!register_kdbus_policy(services[i], fd))
357             goto out;
358
359         if (request_kdbus_name(fd, services[i], (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER) , 0) < 0)
360             goto out;
361
362         if(!_dbus_string_append(&name, services[i]))
363                 goto out;
364         if(!bus_registry_ensure (bus_connection_get_registry (connection), &name, connection,
365                         (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER), transaction, NULL))
366                 goto out;
367         if(!_dbus_string_set_length(&name, 0))
368                 goto out;
369     }
370     retval = TRUE;
371
372 out:
373     if(retval == FALSE)
374     {
375         for(j=0; j<i; j++)
376             release_kdbus_name(fd, services[j], 0);
377     }
378     dbus_free_string_array (services);
379     _dbus_string_free(&name);
380     bus_transaction_free(transaction);
381     return retval;
382 }
383
384 dbus_bool_t update_kdbus_starters(DBusConnection* connection)
385 {
386     dbus_bool_t retval = FALSE;
387     DBusList **services_old;
388     DBusList *link;
389     BusService *service = NULL;
390     BusTransaction *transaction;
391     int fd;
392
393     transaction = bus_transaction_new (bus_connection_get_context(connection));
394     if (transaction == NULL)
395         return FALSE;
396
397     if(!_dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd))
398         goto out;
399
400     services_old = bus_connection_get_services_owned(connection);
401     link = _dbus_list_get_first_link(services_old);
402
403     while (link != NULL)
404     {
405         int ret;
406
407         service = (BusService*) link->data;
408         if(service == NULL)
409             goto out;
410
411         ret = release_kdbus_name(fd, bus_service_get_name(service), 0);
412
413         if (ret == DBUS_RELEASE_NAME_REPLY_RELEASED)
414         {
415             if(!bus_service_remove_owner(service, connection, transaction, NULL))
416                 _dbus_verbose ("Unable to remove\n");
417         }
418         else if(ret < 0)
419             goto out;
420
421         link = _dbus_list_get_next_link (services_old, link);
422     }
423
424     if(!register_kdbus_starters(connection))
425     {
426         _dbus_verbose ("Registering kdbus starters for dbus activatable names failed!\n");
427         goto out;
428     }
429     retval = TRUE;
430
431 out:
432     bus_transaction_free(transaction);
433     return retval;
434 }
435
436 /*
437 static dbus_bool_t remove_conn_if_name_match (DBusConnection *connection, void *data)
438 {
439     if(!strcmp(bus_connection_get_name(connection), (char*)data))
440     {
441         bus_connection_disconnected(connection);
442         return FALSE; //this is to break foreach function
443     }
444     return TRUE;
445 }*/
446
447 void handleNameOwnerChanged(DBusMessage *msg, BusTransaction *transaction, DBusConnection *connection)
448 {
449     const char *name, *old, *new;
450
451     if(!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID))
452     {
453         _dbus_verbose ("Couldn't get args of NameOwnerChanged signal: .\n");//, error.message);
454         return;
455     }
456
457     _dbus_verbose ("Got NameOwnerChanged signal:\nName: %s\nOld: %s\nNew: %s\n", name, old, new);
458
459     if(!strncmp(name, ":1.", 3))/*if it starts from :1. it is unique name - this might be IdRemoved info*/
460     {
461         if(!strcmp(name, old))  //yes it is - someone has disconnected
462         {
463             DBusConnection* conn;
464
465             conn = bus_connections_find_conn_by_name(bus_connection_get_connections(connection), name);
466             if(conn)
467                 bus_connection_disconnected(conn);
468         }
469     }
470     else //it is well-known name
471     {
472         if((*old != 0) && (strcmp(old, ":1.1")))
473         {
474             DBusMessage *message;
475
476             if(bus_connections_find_conn_by_name(bus_connection_get_connections(connection), old) == NULL)
477                 goto next;
478
479             _dbus_verbose ("Owner '%s' lost name '%s'. Sending NameLost.\n", old, name);
480
481             message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameLost");
482             if (message == NULL)
483                 goto next;
484
485             if (!dbus_message_set_destination (message, old) || !dbus_message_append_args (message,
486                                                                  DBUS_TYPE_STRING, &name,
487                                                                  DBUS_TYPE_INVALID))
488             {
489                 dbus_message_unref (message);
490                 goto next;
491             }
492
493             bus_transaction_send_from_driver (transaction, connection, message);
494             dbus_message_unref (message);
495         }
496     next:
497         if((*new != 0) && (strcmp(new, ":1.1")))
498         {
499             DBusMessage *message;
500
501             _dbus_verbose ("Owner '%s' acquired name '%s'. Sending NameAcquired.\n", new, name);
502
503             message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameAcquired");
504             if (message == NULL)
505                 return;
506
507             if (!dbus_message_set_destination (message, new) || !dbus_message_append_args (message,
508                                                                  DBUS_TYPE_STRING, &name,
509                                                                  DBUS_TYPE_INVALID))
510             {
511                 dbus_message_unref (message);
512                 return;
513             }
514
515             bus_transaction_send_from_driver (transaction, connection, message);
516             dbus_message_unref (message);
517         }
518     }
519 }