chiark / gitweb /
dbus: split out object management code into dbus-common, and simplify it
[elogind.git] / src / dbus-common.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   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.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <assert.h>
23 #include <sys/socket.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <dbus/dbus.h>
29 #include <string.h>
30
31 #include "log.h"
32 #include "dbus-common.h"
33 #include "util.h"
34 #include "def.h"
35 #include "strv.h"
36
37 int bus_check_peercred(DBusConnection *c) {
38         int fd;
39         struct ucred ucred;
40         socklen_t l;
41
42         assert(c);
43
44         assert_se(dbus_connection_get_unix_fd(c, &fd));
45
46         l = sizeof(struct ucred);
47         if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) {
48                 log_error("SO_PEERCRED failed: %m");
49                 return -errno;
50         }
51
52         if (l != sizeof(struct ucred)) {
53                 log_error("SO_PEERCRED returned wrong size.");
54                 return -E2BIG;
55         }
56
57         if (ucred.uid != 0)
58                 return -EPERM;
59
60         return 1;
61 }
62
63 static int sync_auth(DBusConnection *bus, DBusError *error) {
64         usec_t begin, tstamp;
65
66         assert(bus);
67
68         /* This complexity should probably move into D-Bus itself:
69          *
70          * https://bugs.freedesktop.org/show_bug.cgi?id=35189 */
71
72         begin = tstamp = now(CLOCK_MONOTONIC);
73         for (;;) {
74
75                 if (tstamp > begin + DEFAULT_TIMEOUT_USEC)
76                         break;
77
78                 if (dbus_connection_get_is_authenticated(bus))
79                         break;
80
81                 if (!dbus_connection_read_write_dispatch(bus, ((begin + DEFAULT_TIMEOUT_USEC - tstamp) + USEC_PER_MSEC - 1) / USEC_PER_MSEC))
82                         break;
83
84                 tstamp = now(CLOCK_MONOTONIC);
85         }
86
87         if (!dbus_connection_get_is_connected(bus)) {
88                 dbus_set_error_const(error, DBUS_ERROR_NO_SERVER, "Connection terminated during authentication.");
89                 return -ECONNREFUSED;
90         }
91
92         if (!dbus_connection_get_is_authenticated(bus)) {
93                 dbus_set_error_const(error, DBUS_ERROR_TIMEOUT, "Failed to authenticate in time.");
94                 return -EACCES;
95         }
96
97         return 0;
98 }
99
100 int bus_connect(DBusBusType t, DBusConnection **_bus, bool *private, DBusError *error) {
101         DBusConnection *bus;
102         int r;
103
104         assert(_bus);
105
106         /* If we are root, then let's not go via the bus */
107         if (geteuid() == 0 && t == DBUS_BUS_SYSTEM) {
108
109                 if (!(bus = dbus_connection_open_private("unix:path=/run/systemd/private", error))) {
110 #ifndef LEGACY
111                         dbus_error_free(error);
112
113                         /* Retry with the pre v21 socket name, to ease upgrades */
114                         if (!(bus = dbus_connection_open_private("unix:abstract=/org/freedesktop/systemd1/private", error)))
115 #endif
116                                 return -EIO;
117                 }
118
119                 dbus_connection_set_exit_on_disconnect(bus, FALSE);
120
121                 if (bus_check_peercred(bus) < 0) {
122                         dbus_connection_close(bus);
123                         dbus_connection_unref(bus);
124
125                         dbus_set_error_const(error, DBUS_ERROR_ACCESS_DENIED, "Failed to verify owner of bus.");
126                         return -EACCES;
127                 }
128
129                 if (private)
130                         *private = true;
131
132         } else {
133                 if (!(bus = dbus_bus_get_private(t, error)))
134                         return -EIO;
135
136                 dbus_connection_set_exit_on_disconnect(bus, FALSE);
137
138                 if (private)
139                         *private = false;
140         }
141
142         if ((r = sync_auth(bus, error)) < 0) {
143                 dbus_connection_close(bus);
144                 dbus_connection_unref(bus);
145                 return r;
146         }
147
148         *_bus = bus;
149         return 0;
150 }
151
152 int bus_connect_system_ssh(const char *user, const char *host, DBusConnection **_bus, DBusError *error) {
153         DBusConnection *bus;
154         char *p = NULL;
155         int r;
156
157         assert(_bus);
158         assert(user || host);
159
160         if (user && host)
161                 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@%s,argv3=systemd-stdio-bridge", user, host);
162         else if (user)
163                 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@localhost,argv3=systemd-stdio-bridge", user);
164         else if (host)
165                 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s,argv3=systemd-stdio-bridge", host);
166
167         if (!p) {
168                 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
169                 return -ENOMEM;
170         }
171
172         bus = dbus_connection_open_private(p, error);
173         free(p);
174
175         if (!bus)
176                 return -EIO;
177
178         dbus_connection_set_exit_on_disconnect(bus, FALSE);
179
180         if ((r = sync_auth(bus, error)) < 0) {
181                 dbus_connection_close(bus);
182                 dbus_connection_unref(bus);
183                 return r;
184         }
185
186         if (!dbus_bus_register(bus, error)) {
187                 dbus_connection_close(bus);
188                 dbus_connection_unref(bus);
189                 return r;
190         }
191
192         *_bus = bus;
193         return 0;
194 }
195
196 int bus_connect_system_polkit(DBusConnection **_bus, DBusError *error) {
197         DBusConnection *bus;
198         int r;
199
200         assert(_bus);
201
202         /* Don't bother with PolicyKit if we are root */
203         if (geteuid() == 0)
204                 return bus_connect(DBUS_BUS_SYSTEM, _bus, NULL, error);
205
206         if (!(bus = dbus_connection_open_private("exec:path=pkexec,argv1=" SYSTEMD_STDIO_BRIDGE_BINARY_PATH, error)))
207                 return -EIO;
208
209         dbus_connection_set_exit_on_disconnect(bus, FALSE);
210
211         if ((r = sync_auth(bus, error)) < 0) {
212                 dbus_connection_close(bus);
213                 dbus_connection_unref(bus);
214                 return r;
215         }
216
217         if (!dbus_bus_register(bus, error)) {
218                 dbus_connection_close(bus);
219                 dbus_connection_unref(bus);
220                 return r;
221         }
222
223         *_bus = bus;
224         return 0;
225 }
226
227 const char *bus_error_message(const DBusError *error) {
228         assert(error);
229
230         /* Sometimes the D-Bus server is a little bit too verbose with
231          * its error messages, so let's override them here */
232         if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED))
233                 return "Access denied";
234
235         return error->message;
236 }
237
238 DBusHandlerResult bus_default_message_handler(
239                 DBusConnection *c,
240                 DBusMessage *message,
241                 const char *introspection,
242                 const char *interfaces,
243                 const BusProperty *properties) {
244
245         DBusError error;
246         DBusMessage *reply = NULL;
247         int r;
248
249         assert(c);
250         assert(message);
251
252         dbus_error_init(&error);
253
254         if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
255
256                 if (!(reply = dbus_message_new_method_return(message)))
257                         goto oom;
258
259                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
260                         goto oom;
261
262         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
263                 const char *interface, *property;
264                 const BusProperty *p;
265
266                 if (!dbus_message_get_args(
267                             message,
268                             &error,
269                             DBUS_TYPE_STRING, &interface,
270                             DBUS_TYPE_STRING, &property,
271                             DBUS_TYPE_INVALID))
272                         return bus_send_error_reply(c, message, &error, -EINVAL);
273
274                 for (p = properties; p->property; p++)
275                         if (streq(p->interface, interface) && streq(p->property, property))
276                                 break;
277
278                 if (p->property) {
279                         DBusMessageIter iter, sub;
280
281                         if (!(reply = dbus_message_new_method_return(message)))
282                                 goto oom;
283
284                         dbus_message_iter_init_append(reply, &iter);
285
286                         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
287                                 goto oom;
288
289                         if ((r = p->append(&sub, property, (void*) p->data)) < 0) {
290
291                                 if (r == -ENOMEM)
292                                         goto oom;
293
294                                 dbus_message_unref(reply);
295                                 return bus_send_error_reply(c, message, NULL, r);
296                         }
297
298                         if (!dbus_message_iter_close_container(&iter, &sub))
299                                 goto oom;
300                 } else {
301                         if (!nulstr_contains(interfaces, interface))
302                                 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
303                         else
304                                 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
305
306                         return bus_send_error_reply(c, message, &error, -EINVAL);
307                 }
308
309         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
310                 const char *interface;
311                 const BusProperty *p;
312                 DBusMessageIter iter, sub, sub2, sub3;
313
314                 if (!dbus_message_get_args(
315                             message,
316                             &error,
317                             DBUS_TYPE_STRING, &interface,
318                             DBUS_TYPE_INVALID))
319                         return bus_send_error_reply(c, message, &error, -EINVAL);
320
321                 if (interface[0] && !nulstr_contains(interfaces, interface)) {
322                         dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
323                         return bus_send_error_reply(c, message, &error, -EINVAL);
324                 }
325
326                 if (!(reply = dbus_message_new_method_return(message)))
327                         goto oom;
328
329                 dbus_message_iter_init_append(reply, &iter);
330
331                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
332                         goto oom;
333
334                 for (p = properties; p->property; p++) {
335                         if (interface[0] && !streq(p->interface, interface))
336                                 continue;
337
338                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
339                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
340                             !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
341                                 goto oom;
342
343                         if ((r = p->append(&sub3, p->property, (void*) p->data)) < 0) {
344
345                                 if (r == -ENOMEM)
346                                         goto oom;
347
348                                 dbus_message_unref(reply);
349                                 return bus_send_error_reply(c, message, NULL, r);
350                         }
351
352                         if (!dbus_message_iter_close_container(&sub2, &sub3) ||
353                             !dbus_message_iter_close_container(&sub, &sub2))
354                                 goto oom;
355                 }
356
357                 if (!dbus_message_iter_close_container(&iter, &sub))
358                         goto oom;
359
360         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Set") && properties) {
361                 const char *interface, *property;
362                 DBusMessageIter iter;
363                 const BusProperty *p;
364
365                 if (!dbus_message_iter_init(message, &iter) ||
366                     dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
367                         return bus_send_error_reply(c, message, NULL, -EINVAL);
368
369                 dbus_message_iter_get_basic(&iter, &interface);
370
371                 if (!dbus_message_iter_next(&iter) ||
372                     dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
373                         return bus_send_error_reply(c, message, NULL, -EINVAL);
374
375                 dbus_message_iter_get_basic(&iter, &property);
376
377                 if (!dbus_message_iter_next(&iter) ||
378                     dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT ||
379                     dbus_message_iter_has_next(&iter))
380                         return bus_send_error_reply(c, message, NULL, -EINVAL);
381
382                 for (p = properties; p->property; p++)
383                         if (streq(p->interface, interface) && streq(p->property, property))
384                                 break;
385
386                 if (p->set) {
387                         DBusMessageIter sub;
388                         char *sig;
389
390                         dbus_message_iter_recurse(&iter, &sub);
391
392                         if (!(sig = dbus_message_iter_get_signature(&sub)))
393                                 goto oom;
394
395                         if (!streq(sig, p->signature)) {
396                                 dbus_free(sig);
397                                 return bus_send_error_reply(c, message, NULL, -EINVAL);
398                         }
399
400                         dbus_free(sig);
401
402                         if ((r = p->set(&sub, property)) < 0) {
403                                 if (r == -ENOMEM)
404                                         goto oom;
405                                 return bus_send_error_reply(c, message, NULL, r);
406                         }
407
408                         if (!(reply = dbus_message_new_method_return(message)))
409                                 goto oom;
410                 } else {
411                         if (p->property)
412                                 dbus_set_error_const(&error, DBUS_ERROR_PROPERTY_READ_ONLY, "Property read-only");
413                         else if (!nulstr_contains(interfaces, interface))
414                                 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
415                         else
416                                 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
417
418                         return bus_send_error_reply(c, message, &error, -EINVAL);
419                 }
420
421         } else if (!nulstr_contains(interfaces, dbus_message_get_interface(message))) {
422                 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
423                 return bus_send_error_reply(c, message, &error, -EINVAL);
424         }
425
426         if (reply) {
427                 if (!dbus_connection_send(c, reply, NULL))
428                         goto oom;
429
430                 dbus_message_unref(reply);
431                 return DBUS_HANDLER_RESULT_HANDLED;
432         }
433
434         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
435
436 oom:
437         if (reply)
438                 dbus_message_unref(reply);
439
440         dbus_error_free(&error);
441
442         return DBUS_HANDLER_RESULT_NEED_MEMORY;
443 }
444
445 int bus_property_append_string(DBusMessageIter *i, const char *property, void *data) {
446         const char *t = data;
447
448         assert(i);
449         assert(property);
450
451         if (!t)
452                 t = "";
453
454         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
455                 return -ENOMEM;
456
457         return 0;
458 }
459
460 int bus_property_append_strv(DBusMessageIter *i, const char *property, void *data) {
461         DBusMessageIter sub;
462         char **t = data;
463
464         assert(i);
465         assert(property);
466
467         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
468                 return -ENOMEM;
469
470         STRV_FOREACH(t, t)
471                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
472                         return -ENOMEM;
473
474         if (!dbus_message_iter_close_container(i, &sub))
475                 return -ENOMEM;
476
477         return 0;
478 }
479
480 int bus_property_append_bool(DBusMessageIter *i, const char *property, void *data) {
481         bool *b = data;
482         dbus_bool_t db;
483
484         assert(i);
485         assert(property);
486         assert(b);
487
488         db = *b;
489
490         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
491                 return -ENOMEM;
492
493         return 0;
494 }
495
496 int bus_property_append_uint64(DBusMessageIter *i, const char *property, void *data) {
497         assert(i);
498         assert(property);
499         assert(data);
500
501         /* Let's ensure that pid_t is actually 64bit, and hence this
502          * function can be used for usec_t */
503         assert_cc(sizeof(uint64_t) == sizeof(usec_t));
504
505         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
506                 return -ENOMEM;
507
508         return 0;
509 }
510
511 int bus_property_append_uint32(DBusMessageIter *i, const char *property, void *data) {
512         assert(i);
513         assert(property);
514         assert(data);
515
516         /* Let's ensure that pid_t and mode_t is actually 32bit, and
517          * hence this function can be used for pid_t/mode_t */
518         assert_cc(sizeof(uint32_t) == sizeof(pid_t));
519         assert_cc(sizeof(uint32_t) == sizeof(mode_t));
520         assert_cc(sizeof(uint32_t) == sizeof(unsigned));
521
522         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
523                 return -ENOMEM;
524
525         return 0;
526 }
527
528 int bus_property_append_int32(DBusMessageIter *i, const char *property, void *data) {
529         assert(i);
530         assert(property);
531         assert(data);
532
533         assert_cc(sizeof(int32_t) == sizeof(int));
534
535         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
536                 return -ENOMEM;
537
538         return 0;
539 }
540
541 int bus_property_append_size(DBusMessageIter *i, const char *property, void *data) {
542         uint64_t u;
543
544         assert(i);
545         assert(property);
546         assert(data);
547
548         u = (uint64_t) *(size_t*) data;
549
550         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
551                 return -ENOMEM;
552
553         return 0;
554 }
555
556 int bus_property_append_ul(DBusMessageIter *i, const char *property, void *data) {
557         uint64_t u;
558
559         assert(i);
560         assert(property);
561         assert(data);
562
563         u = (uint64_t) *(unsigned long*) data;
564
565         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
566                 return -ENOMEM;
567
568         return 0;
569 }
570
571 const char *bus_errno_to_dbus(int error) {
572
573         switch(error) {
574
575         case -EINVAL:
576                 return DBUS_ERROR_INVALID_ARGS;
577
578         case -ENOMEM:
579                 return DBUS_ERROR_NO_MEMORY;
580
581         case -EPERM:
582         case -EACCES:
583                 return DBUS_ERROR_ACCESS_DENIED;
584
585         case -ESRCH:
586                 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
587
588         case -ENOENT:
589                 return DBUS_ERROR_FILE_NOT_FOUND;
590
591         case -EEXIST:
592                 return DBUS_ERROR_FILE_EXISTS;
593
594         case -ETIMEDOUT:
595         case -ETIME:
596                 return DBUS_ERROR_TIMEOUT;
597
598         case -EIO:
599                 return DBUS_ERROR_IO_ERROR;
600
601         case -ENETRESET:
602         case -ECONNABORTED:
603         case -ECONNRESET:
604                 return DBUS_ERROR_DISCONNECTED;
605         }
606
607         return DBUS_ERROR_FAILED;
608 }
609
610 DBusHandlerResult bus_send_error_reply(DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
611         DBusMessage *reply = NULL;
612         const char *name, *text;
613
614         if (berror && dbus_error_is_set(berror)) {
615                 name = berror->name;
616                 text = berror->message;
617         } else {
618                 name = bus_errno_to_dbus(error);
619                 text = strerror(-error);
620         }
621
622         if (!(reply = dbus_message_new_error(message, name, text)))
623                 goto oom;
624
625         if (!dbus_connection_send(c, reply, NULL))
626                 goto oom;
627
628         dbus_message_unref(reply);
629
630         if (berror)
631                 dbus_error_free(berror);
632
633         return DBUS_HANDLER_RESULT_HANDLED;
634
635 oom:
636         if (reply)
637                 dbus_message_unref(reply);
638
639         if (berror)
640                 dbus_error_free(berror);
641
642         return DBUS_HANDLER_RESULT_NEED_MEMORY;
643 }
644
645 DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties) {
646         DBusMessage *m;
647         DBusMessageIter iter, sub;
648         const char *i;
649
650         assert(interface);
651         assert(properties);
652
653         if (!(m = dbus_message_new_signal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged")))
654                 goto oom;
655
656         dbus_message_iter_init_append(m, &iter);
657
658         /* We won't send any property values, since they might be
659          * large and sometimes not cheap to generated */
660
661         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface) ||
662             !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub) ||
663             !dbus_message_iter_close_container(&iter, &sub) ||
664             !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub))
665                 goto oom;
666
667         NULSTR_FOREACH(i, properties)
668                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &i))
669                         goto oom;
670
671         if (!dbus_message_iter_close_container(&iter, &sub))
672                 goto oom;
673
674         return m;
675
676 oom:
677         if (m)
678                 dbus_message_unref(m);
679
680         return NULL;
681 }