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