[daemon-dev][daemon-fix] starting services by direct message (autostart) and some...
[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, "type='signal', member='NameLost'"))  //todo handle this in dispatch
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
120         if(dbus_error_is_set(error))
121         {
122 failed:
123                 _dbus_connection_close_possibly_shared (connection);
124                 dbus_connection_unref (connection);
125                 connection = NULL;
126         }
127         else
128                 _dbus_verbose ("Daemon connected as kdbus client.\n");
129
130         return connection;
131 }
132
133 dbus_bool_t kdbus_register_policy (const DBusString *service_name, DBusConnection* connection)
134 {
135         int fd;
136
137         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
138
139         return register_kdbus_policy(_dbus_string_get_const_data(service_name), fd);
140 }
141
142 dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
143 {
144         int fd;
145
146         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
147
148         return request_kdbus_name(fd, _dbus_string_get_const_data(service_name), flags, sender_id);
149 }
150
151 dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *service_name, __u64 sender_id)
152 {
153         int fd;
154
155         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
156
157         return release_kdbus_name(fd, _dbus_string_get_const_data(service_name), sender_id);
158 }
159
160 dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len)
161 {
162         int fd;
163
164         _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
165
166         return list_kdbus_names(fd, listp, array_len);
167 }
168
169 dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* message, const char* text, DBusError* error)
170 {
171         __u64 sender_id;
172
173         sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
174         if(dbus_error_is_set(error))
175                 return FALSE;
176
177         if(!add_match_kdbus (dbus_connection_get_transport(connection), sender_id, text))
178         {
179               dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:%d, %s",
180                               sender_id, _dbus_strerror_from_errno ());
181               return FALSE;
182         }
183
184         return TRUE;
185 }
186
187 dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message, DBusError* error)
188 {
189         __u64 sender_id;
190
191         sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
192         if(dbus_error_is_set(error))
193                 return FALSE;
194
195         if(!remove_match_kdbus (dbus_connection_get_transport(connection), sender_id))
196         {
197               dbus_set_error (error, _dbus_error_from_errno (errno), "Could not remove match rules for id:%d", sender_id);
198               return FALSE;
199         }
200
201         return TRUE;
202 }
203
204 dbus_bool_t kdbus_get_connection_unix_user(DBusConnection* connection, DBusMessage* message, unsigned long* uid, DBusError* error)
205 {
206         char* name = NULL;
207         struct nameInfo info;
208         int inter_ret;
209         dbus_bool_t ret = FALSE;
210
211         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
212         inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
213         if(inter_ret == 0) //name found
214         {
215                 _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
216                 *uid = info.userId;
217                 return TRUE;
218         }
219         else if(inter_ret == -ENOENT)  //name has no owner
220                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get UID of name '%s': no such name", name);
221         else
222         {
223                 _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
224                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name);
225         }
226
227         return ret;
228 }
229
230 dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, DBusMessage* message, unsigned long* pid, DBusError* error)
231 {
232         char* name = NULL;
233         struct nameInfo info;
234         int inter_ret;
235         dbus_bool_t ret = FALSE;
236
237         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
238         inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
239         if(inter_ret == 0) //name found
240         {
241                 _dbus_verbose("Process id:%llu\n", (unsigned long long) info.processId);
242                 *pid = info.processId;
243                 return TRUE;
244         }
245         else if(inter_ret == -ENOENT)  //name has no owner
246                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get PID of name '%s': no such name", name);
247         else
248         {
249                 _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
250                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine PID for '%s'", name);
251         }
252
253         return ret;
254 }
255
256 dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error)
257 {
258         char* name = NULL;
259         struct nameInfo info;
260         int inter_ret;
261         dbus_bool_t ret = FALSE;
262
263         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
264         inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
265         if(inter_ret == -ENOENT)  //name has no owner
266                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get security context of name '%s': no such name", name);
267         else if(inter_ret < 0)
268         {
269                 _dbus_verbose("kdbus error determining security context: err %d (%m)\n", errno);
270                 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine security context for '%s'", name);
271         }
272         else
273         {
274                 if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
275                 {
276                       _DBUS_SET_OOM (error);
277                       return FALSE;
278                 }
279                 ret = TRUE;
280         }
281
282         return ret;
283 }
284
285 DBusConnection* create_phantom_connection(DBusConnection* connection, const char* unique_name, DBusError* error)
286 {
287     DBusConnection *phantom_connection;
288     DBusString name;
289
290     _dbus_string_init_const(&name, unique_name);
291
292     phantom_connection = _dbus_connection_new_for_used_transport (dbus_connection_get_transport(connection));
293     if(phantom_connection == NULL)
294         return FALSE;
295     if(!bus_connections_setup_connection(bus_connection_get_connections(connection), phantom_connection))
296     {
297         dbus_connection_unref_phantom(phantom_connection);
298         phantom_connection = NULL;
299         dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", unique_name);
300         goto out;
301     }
302     if(!bus_connection_complete(phantom_connection, &name, error))
303     {
304         dbus_connection_unref_phantom(phantom_connection);
305         phantom_connection = NULL;
306         goto out;
307     }
308
309     _dbus_verbose ("Created phantom connection for %s\n", bus_connection_get_name(phantom_connection));
310
311 out:
312     return phantom_connection;
313 }
314
315 dbus_bool_t register_kdbus_starters(DBusConnection* connection)
316 {
317     int i, len;
318     char **services;
319     dbus_bool_t retval = FALSE;
320     int fd;
321
322     if (!bus_activation_list_services (bus_connection_get_activation (connection), &services, &len))
323         return FALSE;
324
325     _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
326
327     for(i=0; i<len; i++)
328     {
329         if(!register_kdbus_policy(services[i], fd))
330             goto out;
331
332         if (request_kdbus_name(fd, services[i], (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER) , 0) < 0)
333             goto out;
334     }
335     retval = TRUE;
336
337 out:
338     dbus_free_string_array (services);
339     return retval;
340 }