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