1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
23 #include <sys/socket.h>
28 #include <dbus/dbus.h>
30 #include <sys/epoll.h>
33 #include "dbus-common.h"
38 int bus_check_peercred(DBusConnection *c) {
45 assert_se(dbus_connection_get_unix_fd(c, &fd));
47 l = sizeof(struct ucred);
48 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) {
49 log_error("SO_PEERCRED failed: %m");
53 if (l != sizeof(struct ucred)) {
54 log_error("SO_PEERCRED returned wrong size.");
58 if (ucred.uid != 0 && ucred.uid != geteuid())
64 static int sync_auth(DBusConnection *bus, DBusError *error) {
69 /* This complexity should probably move into D-Bus itself:
71 * https://bugs.freedesktop.org/show_bug.cgi?id=35189 */
73 begin = tstamp = now(CLOCK_MONOTONIC);
76 if (tstamp > begin + DEFAULT_TIMEOUT_USEC)
79 if (dbus_connection_get_is_authenticated(bus))
82 if (!dbus_connection_read_write_dispatch(bus, ((begin + DEFAULT_TIMEOUT_USEC - tstamp) + USEC_PER_MSEC - 1) / USEC_PER_MSEC))
85 tstamp = now(CLOCK_MONOTONIC);
88 if (!dbus_connection_get_is_connected(bus)) {
89 dbus_set_error_const(error, DBUS_ERROR_NO_SERVER, "Connection terminated during authentication.");
93 if (!dbus_connection_get_is_authenticated(bus)) {
94 dbus_set_error_const(error, DBUS_ERROR_TIMEOUT, "Failed to authenticate in time.");
101 int bus_connect(DBusBusType t, DBusConnection **_bus, bool *_private, DBusError *error) {
102 DBusConnection *bus = NULL;
108 if (geteuid() == 0 && t == DBUS_BUS_SYSTEM) {
109 /* If we are root, then let's talk directly to the
110 * system instance, instead of going via the bus */
112 bus = dbus_connection_open_private("unix:path=/run/systemd/private", error);
117 if (t == DBUS_BUS_SESSION) {
120 /* If we are supposed to talk to the instance,
121 * try via XDG_RUNTIME_DIR first, then
122 * fallback to normal bus access */
124 e = getenv("XDG_RUNTIME_DIR");
128 if (asprintf(&p, "unix:path=%s/systemd/private", e) < 0)
131 bus = dbus_connection_open_private(p, NULL);
137 bus = dbus_bus_get_private(t, error);
145 dbus_connection_set_exit_on_disconnect(bus, FALSE);
148 if (bus_check_peercred(bus) < 0) {
149 dbus_connection_close(bus);
150 dbus_connection_unref(bus);
152 dbus_set_error_const(error, DBUS_ERROR_ACCESS_DENIED, "Failed to verify owner of bus.");
157 r = sync_auth(bus, error);
159 dbus_connection_close(bus);
160 dbus_connection_unref(bus);
171 int bus_connect_system_ssh(const char *user, const char *host, DBusConnection **_bus, DBusError *error) {
177 assert(user || host);
180 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@%s,argv3=systemd-stdio-bridge", user, host);
182 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@localhost,argv3=systemd-stdio-bridge", user);
184 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s,argv3=systemd-stdio-bridge", host);
187 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
191 bus = dbus_connection_open_private(p, error);
197 dbus_connection_set_exit_on_disconnect(bus, FALSE);
199 if ((r = sync_auth(bus, error)) < 0) {
200 dbus_connection_close(bus);
201 dbus_connection_unref(bus);
205 if (!dbus_bus_register(bus, error)) {
206 dbus_connection_close(bus);
207 dbus_connection_unref(bus);
215 int bus_connect_system_polkit(DBusConnection **_bus, DBusError *error) {
221 /* Don't bother with PolicyKit if we are root */
223 return bus_connect(DBUS_BUS_SYSTEM, _bus, NULL, error);
225 if (!(bus = dbus_connection_open_private("exec:path=pkexec,argv1=" SYSTEMD_STDIO_BRIDGE_BINARY_PATH, error)))
228 dbus_connection_set_exit_on_disconnect(bus, FALSE);
230 if ((r = sync_auth(bus, error)) < 0) {
231 dbus_connection_close(bus);
232 dbus_connection_unref(bus);
236 if (!dbus_bus_register(bus, error)) {
237 dbus_connection_close(bus);
238 dbus_connection_unref(bus);
246 const char *bus_error_message(const DBusError *error) {
249 /* Sometimes the D-Bus server is a little bit too verbose with
250 * its error messages, so let's override them here */
251 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED))
252 return "Access denied";
254 return error->message;
257 DBusHandlerResult bus_default_message_handler(
259 DBusMessage *message,
260 const char *introspection,
261 const char *interfaces,
262 const BusProperty *properties) {
265 DBusMessage *reply = NULL;
271 dbus_error_init(&error);
273 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
275 if (!(reply = dbus_message_new_method_return(message)))
278 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
281 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
282 const char *interface, *property;
283 const BusProperty *p;
285 if (!dbus_message_get_args(
288 DBUS_TYPE_STRING, &interface,
289 DBUS_TYPE_STRING, &property,
291 return bus_send_error_reply(c, message, &error, -EINVAL);
293 for (p = properties; p->property; p++)
294 if (streq(p->interface, interface) && streq(p->property, property))
298 DBusMessageIter iter, sub;
300 if (!(reply = dbus_message_new_method_return(message)))
303 dbus_message_iter_init_append(reply, &iter);
305 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
308 if ((r = p->append(&sub, property, (void*) p->data)) < 0) {
313 dbus_message_unref(reply);
314 return bus_send_error_reply(c, message, NULL, r);
317 if (!dbus_message_iter_close_container(&iter, &sub))
320 if (!nulstr_contains(interfaces, interface))
321 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
323 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
325 return bus_send_error_reply(c, message, &error, -EINVAL);
328 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
329 const char *interface;
330 const BusProperty *p;
331 DBusMessageIter iter, sub, sub2, sub3;
333 if (!dbus_message_get_args(
336 DBUS_TYPE_STRING, &interface,
338 return bus_send_error_reply(c, message, &error, -EINVAL);
340 if (interface[0] && !nulstr_contains(interfaces, interface)) {
341 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
342 return bus_send_error_reply(c, message, &error, -EINVAL);
345 if (!(reply = dbus_message_new_method_return(message)))
348 dbus_message_iter_init_append(reply, &iter);
350 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
353 for (p = properties; p->property; p++) {
354 if (interface[0] && !streq(p->interface, interface))
357 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
358 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
359 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
362 if ((r = p->append(&sub3, p->property, (void*) p->data)) < 0) {
367 dbus_message_unref(reply);
368 return bus_send_error_reply(c, message, NULL, r);
371 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
372 !dbus_message_iter_close_container(&sub, &sub2))
376 if (!dbus_message_iter_close_container(&iter, &sub))
379 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Set") && properties) {
380 const char *interface, *property;
381 DBusMessageIter iter;
382 const BusProperty *p;
384 if (!dbus_message_iter_init(message, &iter) ||
385 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
386 return bus_send_error_reply(c, message, NULL, -EINVAL);
388 dbus_message_iter_get_basic(&iter, &interface);
390 if (!dbus_message_iter_next(&iter) ||
391 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
392 return bus_send_error_reply(c, message, NULL, -EINVAL);
394 dbus_message_iter_get_basic(&iter, &property);
396 if (!dbus_message_iter_next(&iter) ||
397 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT ||
398 dbus_message_iter_has_next(&iter))
399 return bus_send_error_reply(c, message, NULL, -EINVAL);
401 for (p = properties; p->property; p++)
402 if (streq(p->interface, interface) && streq(p->property, property))
409 dbus_message_iter_recurse(&iter, &sub);
411 if (!(sig = dbus_message_iter_get_signature(&sub)))
414 if (!streq(sig, p->signature)) {
416 return bus_send_error_reply(c, message, NULL, -EINVAL);
421 if ((r = p->set(&sub, property)) < 0) {
424 return bus_send_error_reply(c, message, NULL, r);
427 if (!(reply = dbus_message_new_method_return(message)))
431 dbus_set_error_const(&error, DBUS_ERROR_PROPERTY_READ_ONLY, "Property read-only");
432 else if (!nulstr_contains(interfaces, interface))
433 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
435 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
437 return bus_send_error_reply(c, message, &error, -EINVAL);
441 const char *interface = dbus_message_get_interface(message);
443 if (!interface || !nulstr_contains(interfaces, interface)) {
444 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
445 return bus_send_error_reply(c, message, &error, -EINVAL);
450 if (!dbus_connection_send(c, reply, NULL))
453 dbus_message_unref(reply);
454 return DBUS_HANDLER_RESULT_HANDLED;
457 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
461 dbus_message_unref(reply);
463 dbus_error_free(&error);
465 return DBUS_HANDLER_RESULT_NEED_MEMORY;
468 int bus_property_append_string(DBusMessageIter *i, const char *property, void *data) {
469 const char *t = data;
477 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
483 int bus_property_append_strv(DBusMessageIter *i, const char *property, void *data) {
489 return bus_append_strv_iter(i, t);
492 int bus_property_append_bool(DBusMessageIter *i, const char *property, void *data) {
502 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
508 int bus_property_append_uint64(DBusMessageIter *i, const char *property, void *data) {
513 /* Let's ensure that usec_t is actually 64bit, and hence this
514 * function can be used for usec_t */
515 assert_cc(sizeof(uint64_t) == sizeof(usec_t));
517 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
523 int bus_property_append_uint32(DBusMessageIter *i, const char *property, void *data) {
528 /* Let's ensure that pid_t, mode_t, uid_t, gid_t are actually
529 * 32bit, and hence this function can be used for
530 * pid_t/mode_t/uid_t/gid_t */
531 assert_cc(sizeof(uint32_t) == sizeof(pid_t));
532 assert_cc(sizeof(uint32_t) == sizeof(mode_t));
533 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
534 assert_cc(sizeof(uint32_t) == sizeof(uid_t));
535 assert_cc(sizeof(uint32_t) == sizeof(gid_t));
537 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
543 int bus_property_append_int32(DBusMessageIter *i, const char *property, void *data) {
548 assert_cc(sizeof(int32_t) == sizeof(int));
550 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
556 int bus_property_append_size(DBusMessageIter *i, const char *property, void *data) {
563 u = (uint64_t) *(size_t*) data;
565 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
571 int bus_property_append_ul(DBusMessageIter *i, const char *property, void *data) {
578 u = (uint64_t) *(unsigned long*) data;
580 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
586 int bus_property_append_long(DBusMessageIter *i, const char *property, void *data) {
593 l = (int64_t) *(long*) data;
595 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT64, &l))
601 const char *bus_errno_to_dbus(int error) {
606 return DBUS_ERROR_INVALID_ARGS;
609 return DBUS_ERROR_NO_MEMORY;
613 return DBUS_ERROR_ACCESS_DENIED;
616 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
619 return DBUS_ERROR_FILE_NOT_FOUND;
622 return DBUS_ERROR_FILE_EXISTS;
626 return DBUS_ERROR_TIMEOUT;
629 return DBUS_ERROR_IO_ERROR;
634 return DBUS_ERROR_DISCONNECTED;
637 return DBUS_ERROR_FAILED;
640 DBusHandlerResult bus_send_error_reply(DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
641 DBusMessage *reply = NULL;
642 const char *name, *text;
644 if (berror && dbus_error_is_set(berror)) {
646 text = berror->message;
648 name = bus_errno_to_dbus(error);
649 text = strerror(-error);
652 if (!(reply = dbus_message_new_error(message, name, text)))
655 if (!dbus_connection_send(c, reply, NULL))
658 dbus_message_unref(reply);
661 dbus_error_free(berror);
663 return DBUS_HANDLER_RESULT_HANDLED;
667 dbus_message_unref(reply);
670 dbus_error_free(berror);
672 return DBUS_HANDLER_RESULT_NEED_MEMORY;
675 DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties) {
677 DBusMessageIter iter, sub;
683 if (!(m = dbus_message_new_signal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged")))
686 dbus_message_iter_init_append(m, &iter);
688 /* We won't send any property values, since they might be
689 * large and sometimes not cheap to generated */
691 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface) ||
692 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub) ||
693 !dbus_message_iter_close_container(&iter, &sub) ||
694 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub))
697 NULSTR_FOREACH(i, properties)
698 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &i))
701 if (!dbus_message_iter_close_container(&iter, &sub))
708 dbus_message_unref(m);
713 uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
719 /* no watch flags for disabled watches */
720 if (!dbus_watch_get_enabled(bus_watch))
723 flags = dbus_watch_get_flags(bus_watch);
725 if (flags & DBUS_WATCH_READABLE)
727 if (flags & DBUS_WATCH_WRITABLE)
730 return events | EPOLLHUP | EPOLLERR;
733 unsigned bus_events_to_flags(uint32_t events) {
736 if (events & EPOLLIN)
737 flags |= DBUS_WATCH_READABLE;
738 if (events & EPOLLOUT)
739 flags |= DBUS_WATCH_WRITABLE;
740 if (events & EPOLLHUP)
741 flags |= DBUS_WATCH_HANGUP;
742 if (events & EPOLLERR)
743 flags |= DBUS_WATCH_ERROR;
748 int bus_parse_strv(DBusMessage *m, char ***_l) {
749 DBusMessageIter iter;
754 if (!dbus_message_iter_init(m, &iter))
757 return bus_parse_strv_iter(&iter, _l);
760 int bus_parse_strv_iter(DBusMessageIter *iter, char ***_l) {
762 unsigned n = 0, i = 0;
768 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY ||
769 dbus_message_iter_get_element_type(iter) != DBUS_TYPE_STRING)
772 dbus_message_iter_recurse(iter, &sub);
774 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
776 dbus_message_iter_next(&sub);
779 if (!(l = new(char*, n+1)))
782 dbus_message_iter_recurse(iter, &sub);
784 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
787 assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
788 dbus_message_iter_get_basic(&sub, &s);
790 if (!(l[i++] = strdup(s))) {
795 dbus_message_iter_next(&sub);
807 int bus_append_strv_iter(DBusMessageIter *iter, char **l) {
812 if (!dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "s", &sub))
816 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, l))
819 if (!dbus_message_iter_close_container(iter, &sub))
825 int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
830 if (dbus_message_iter_get_arg_type(iter) != type)
833 dbus_message_iter_get_basic(iter, data);
835 if (!dbus_message_iter_next(iter) != !next)
841 int generic_print_property(const char *name, DBusMessageIter *iter, bool all) {
845 switch (dbus_message_iter_get_arg_type(iter)) {
847 case DBUS_TYPE_STRING: {
849 dbus_message_iter_get_basic(iter, &s);
851 if (all || !isempty(s))
852 printf("%s=%s\n", name, s);
857 case DBUS_TYPE_BOOLEAN: {
860 dbus_message_iter_get_basic(iter, &b);
861 printf("%s=%s\n", name, yes_no(b));
866 case DBUS_TYPE_UINT64: {
868 dbus_message_iter_get_basic(iter, &u);
870 /* Yes, heuristics! But we can change this check
871 * should it turn out to not be sufficient */
873 if (endswith(name, "Timestamp")) {
874 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
876 t = format_timestamp(timestamp, sizeof(timestamp), u);
878 printf("%s=%s\n", name, strempty(t));
880 } else if (strstr(name, "USec")) {
881 char timespan[FORMAT_TIMESPAN_MAX];
883 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
885 printf("%s=%llu\n", name, (unsigned long long) u);
890 case DBUS_TYPE_UINT32: {
892 dbus_message_iter_get_basic(iter, &u);
894 if (strstr(name, "UMask") || strstr(name, "Mode"))
895 printf("%s=%04o\n", name, u);
897 printf("%s=%u\n", name, (unsigned) u);
902 case DBUS_TYPE_INT32: {
904 dbus_message_iter_get_basic(iter, &i);
906 printf("%s=%i\n", name, (int) i);
910 case DBUS_TYPE_DOUBLE: {
912 dbus_message_iter_get_basic(iter, &d);
914 printf("%s=%g\n", name, d);
918 case DBUS_TYPE_ARRAY:
920 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
924 dbus_message_iter_recurse(iter, &sub);
926 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
929 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
932 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
933 dbus_message_iter_get_basic(&sub, &s);
934 printf("%s%s", space ? " " : "", s);
937 dbus_message_iter_next(&sub);
945 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
948 dbus_message_iter_recurse(iter, &sub);
950 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
953 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
956 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
957 dbus_message_iter_get_basic(&sub, &u);
960 dbus_message_iter_next(&sub);