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