2d7676aa6d183ed6a8944e58a250d5438071d2ba
[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     snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus", getuid());
69     bus_make.n_type = KDBUS_MAKE_NAME;
70     bus_make.n_size = KDBUS_PART_HEADER_SIZE + strlen(bus_make.name) + 1;
71     bus_make.head.size = sizeof(struct kdbus_cmd_bus_make) + bus_make.n_size;
72
73     _dbus_verbose("Creating bus '%s'\n", bus_make.name);
74     ret = ioctl(fdc, KDBUS_CMD_BUS_MAKE, &bus_make);
75     if (ret)
76     {
77         _dbus_verbose("--- error %d (%m)\n", ret);
78         return NULL;
79     }
80
81     if (asprintf(&bus, "kdbus:path=/dev/kdbus/%s/bus", bus_make.name) < 0)
82     {
83         BUS_SET_OOM (error);
84         return NULL;
85     }
86
87     _dbus_verbose("Return value '%s'\n", bus);
88         return bus;
89 }
90
91 DBusServer* empty_server_init(char* address)
92 {
93         return dbus_server_init_mini(address);
94 }
95
96 DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error)
97 {
98         DBusConnection* connection;
99         DBusString daemon_name;
100
101         dbus_bus_set_bus_connection_address(type, address);
102
103         connection = dbus_bus_get_private(type, error);  /*todo possibly could be optimised by using lower functions*/
104         if(connection == NULL)
105                 return NULL;
106
107         _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
108         if(!kdbus_register_policy (&daemon_name, connection))
109                 goto failed;
110
111         if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
112                 goto failed;
113
114     if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='IdRemoved'"))
115     {
116           dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
117           return FALSE;
118     }
119     if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameChanged'"))
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='NameLost'"))
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='NameAcquired'"))
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
135         if(dbus_error_is_set(error))
136         {
137 failed:
138                 _dbus_connection_close_possibly_shared (connection);
139                 dbus_connection_unref (connection);
140                 connection = NULL;
141         }
142         else
143                 _dbus_verbose ("Daemon connected as kdbus client.\n");
144
145         return connection;
146 }
147
148 dbus_bool_t kdbus_register_policy (const DBusString *service_name, DBusConnection* connection)
149 {
150         int fd;
151
152         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
153
154         return register_kdbus_policy(_dbus_string_get_const_data(service_name), fd);
155 }
156
157 dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
158 {
159         int fd;
160
161         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
162
163         return request_kdbus_name(fd, _dbus_string_get_const_data(service_name), flags, sender_id);
164 }
165
166 dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *service_name, __u64 sender_id)
167 {
168         int fd;
169
170         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
171
172         return release_kdbus_name(fd, _dbus_string_get_const_data(service_name), sender_id);
173 }
174
175 dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len)
176 {
177         int fd;
178
179         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
180
181         return list_kdbus_names(fd, listp, array_len);
182 }
183
184 dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* message, const char* text, DBusError* error)
185 {
186         __u64 sender_id;
187
188         sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
189         if(dbus_error_is_set(error))
190                 return FALSE;
191
192         if(!add_match_kdbus (dbus_connection_get_transport(connection), sender_id, text))
193         {
194               dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:%d, %s",
195                               sender_id, _dbus_strerror_from_errno ());
196               return FALSE;
197         }
198
199         return TRUE;
200 }
201
202 dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message, DBusError* error)
203 {
204         __u64 sender_id;
205
206         sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
207         if(dbus_error_is_set(error))
208                 return FALSE;
209
210         if(!remove_match_kdbus (dbus_connection_get_transport(connection), sender_id))
211         {
212               dbus_set_error (error, _dbus_error_from_errno (errno), "Could not remove match rules for id:%d", sender_id);
213               return FALSE;
214         }
215
216         return TRUE;
217 }
218
219 dbus_bool_t kdbus_get_connection_unix_user(DBusConnection* connection, DBusMessage* message, unsigned long* uid, DBusError* error)
220 {
221         char* name = NULL;
222         struct nameInfo info;
223         int inter_ret;
224         dbus_bool_t ret = FALSE;
225
226         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
227         inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
228         if(inter_ret == 0) //name found
229         {
230                 _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
231                 *uid = info.userId;
232                 return TRUE;
233         }
234         else if(inter_ret == -ENOENT)  //name has no owner
235                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get UID of name '%s': no such name", name);
236         else
237         {
238                 _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
239                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name);
240         }
241
242         return ret;
243 }
244
245 dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, DBusMessage* message, unsigned long* pid, DBusError* error)
246 {
247         char* name = NULL;
248         struct nameInfo info;
249         int inter_ret;
250         dbus_bool_t ret = FALSE;
251
252         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
253         inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
254         if(inter_ret == 0) //name found
255         {
256                 _dbus_verbose("Process id:%llu\n", (unsigned long long) info.processId);
257                 *pid = info.processId;
258                 return TRUE;
259         }
260         else if(inter_ret == -ENOENT)  //name has no owner
261                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get PID of name '%s': no such name", name);
262         else
263         {
264                 _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
265                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine PID for '%s'", name);
266         }
267
268         return ret;
269 }
270
271 dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error)
272 {
273         char* name = NULL;
274         struct nameInfo info;
275         int inter_ret;
276         dbus_bool_t ret = FALSE;
277
278         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
279         inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
280         if(inter_ret == -ENOENT)  //name has no owner
281                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get security context of name '%s': no such name", name);
282         else if(inter_ret < 0)
283         {
284                 _dbus_verbose("kdbus error determining security context: err %d (%m)\n", errno);
285                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine security context for '%s'", name);
286         }
287         else
288         {
289                 if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
290                 {
291                       _DBUS_SET_OOM (error);
292                       return FALSE;
293                 }
294                 ret = TRUE;
295         }
296
297         return ret;
298 }
299
300 DBusConnection* create_phantom_connection(DBusConnection* connection, const char* unique_name, DBusError* error)
301 {
302     DBusConnection *phantom_connection;
303     DBusString name;
304
305     _dbus_string_init_const(&name, unique_name);
306
307     phantom_connection = _dbus_connection_new_for_used_transport (dbus_connection_get_transport(connection));
308     if(phantom_connection == NULL)
309         return FALSE;
310     if(!bus_connections_setup_connection(bus_connection_get_connections(connection), phantom_connection))
311     {
312         dbus_connection_unref_phantom(phantom_connection);
313         phantom_connection = NULL;
314         dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", unique_name);
315         goto out;
316     }
317     if(!bus_connection_complete(phantom_connection, &name, error))
318     {
319         bus_connection_disconnected(phantom_connection);
320         phantom_connection = NULL;
321         goto out;
322     }
323
324     _dbus_verbose ("Created phantom connection for %s\n", bus_connection_get_name(phantom_connection));
325
326 out:
327     return phantom_connection;
328 }
329
330 dbus_bool_t register_kdbus_starters(DBusConnection* connection)
331 {
332     int i, len;
333     char **services;
334     dbus_bool_t retval = FALSE;
335     int fd;
336
337     if (!bus_activation_list_services (bus_connection_get_activation (connection), &services, &len))
338         return FALSE;
339
340     _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
341
342     for(i=0; i<len; i++)
343     {
344         if(!register_kdbus_policy(services[i], fd))
345             goto out;
346
347         if (request_kdbus_name(fd, services[i], (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER) , 0) < 0)
348             goto out;
349     }
350     retval = TRUE;
351
352 out:
353     dbus_free_string_array (services);
354     return retval;
355 }
356
357 static dbus_bool_t remove_conn_if_name_match (DBusConnection *connection, void *data)
358 {
359     if(!strcmp(bus_connection_get_name(connection), (char*)data))
360     {
361         bus_connection_disconnected(connection);
362 //        return FALSE; //needed to break foreach function
363         /* todo should we brake? Now we create phantom for each name, so if someone acquire more than
364          * one name he will have more than one phantom. I think that there should be one phantom for one name
365          * but if so, name acquiring and releasing must be changed
366          */
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     if(!strncmp(name, ":1.", 3))/*if it starts from :1. it is unique name - this might be IdRemoved info*/
382     {
383         if(!strcmp(name, old))  //yes it is - someone has disconnected
384         {
385             _dbus_verbose ("Connection %s has disconnected. Removing.\n", name);  //todo to remove at the end of development
386             bus_connections_foreach_active(bus_connection_get_connections(connection), remove_conn_if_name_match, (void*)name);
387         }
388     }
389     else //it is well-known name
390     {
391         if((*old != 0) && (strcmp(old, ":1.1")))
392         {
393             DBusMessage *message;
394
395             _dbus_verbose ("Owner '%s' lost name '%s'. Sending NameLost.\n", old, name);
396
397             message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameLost");
398             if (message == NULL)
399                 goto next;
400
401             if (!dbus_message_set_destination (message, old) || !dbus_message_append_args (message,
402                                                                  DBUS_TYPE_STRING, &name,
403                                                                  DBUS_TYPE_INVALID))
404             {
405                 dbus_message_unref (message);
406                 goto next;
407             }
408
409             bus_transaction_send_from_driver (transaction, connection, message);
410             dbus_message_unref (message);
411         }
412     next:
413         if((*new != 0) && (strcmp(new, ":1.1")))
414         {
415             DBusMessage *message;
416
417             _dbus_verbose ("Owner '%s' acquired name '%s'. Sending NameAcquired.\n", new, name);
418
419             message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameAcquired");
420             if (message == NULL)
421                 return;
422
423             if (!dbus_message_set_destination (message, new) || !dbus_message_append_args (message,
424                                                                  DBUS_TYPE_STRING, &name,
425                                                                  DBUS_TYPE_INVALID))
426             {
427                 dbus_message_unref (message);
428                 return;
429             }
430
431             bus_transaction_send_from_driver (transaction, connection, message);
432             dbus_message_unref (message);
433         }
434     }
435 }