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