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.");
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) {
107 /* If we are root, then let's not go via the bus */
108 if (geteuid() == 0 && t == DBUS_BUS_SYSTEM) {
110 if (!(bus = dbus_connection_open_private("unix:path=/run/systemd/private", error))) {
112 dbus_error_free(error);
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)))
120 dbus_connection_set_exit_on_disconnect(bus, FALSE);
122 if (bus_check_peercred(bus) < 0) {
123 dbus_connection_close(bus);
124 dbus_connection_unref(bus);
126 dbus_set_error_const(error, DBUS_ERROR_ACCESS_DENIED, "Failed to verify owner of bus.");
134 if (!(bus = dbus_bus_get_private(t, error)))
137 dbus_connection_set_exit_on_disconnect(bus, FALSE);
143 if ((r = sync_auth(bus, error)) < 0) {
144 dbus_connection_close(bus);
145 dbus_connection_unref(bus);
153 int bus_connect_system_ssh(const char *user, const char *host, DBusConnection **_bus, DBusError *error) {
159 assert(user || host);
162 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@%s,argv3=systemd-stdio-bridge", user, host);
164 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@localhost,argv3=systemd-stdio-bridge", user);
166 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s,argv3=systemd-stdio-bridge", host);
169 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
173 bus = dbus_connection_open_private(p, error);
179 dbus_connection_set_exit_on_disconnect(bus, FALSE);
181 if ((r = sync_auth(bus, error)) < 0) {
182 dbus_connection_close(bus);
183 dbus_connection_unref(bus);
187 if (!dbus_bus_register(bus, error)) {
188 dbus_connection_close(bus);
189 dbus_connection_unref(bus);
197 int bus_connect_system_polkit(DBusConnection **_bus, DBusError *error) {
203 /* Don't bother with PolicyKit if we are root */
205 return bus_connect(DBUS_BUS_SYSTEM, _bus, NULL, error);
207 if (!(bus = dbus_connection_open_private("exec:path=pkexec,argv1=" SYSTEMD_STDIO_BRIDGE_BINARY_PATH, error)))
210 dbus_connection_set_exit_on_disconnect(bus, FALSE);
212 if ((r = sync_auth(bus, error)) < 0) {
213 dbus_connection_close(bus);
214 dbus_connection_unref(bus);
218 if (!dbus_bus_register(bus, error)) {
219 dbus_connection_close(bus);
220 dbus_connection_unref(bus);
228 const char *bus_error_message(const DBusError *error) {
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";
236 return error->message;
239 DBusHandlerResult bus_default_message_handler(
241 DBusMessage *message,
242 const char *introspection,
243 const char *interfaces,
244 const BusProperty *properties) {
247 DBusMessage *reply = NULL;
253 dbus_error_init(&error);
255 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
257 if (!(reply = dbus_message_new_method_return(message)))
260 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
263 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
264 const char *interface, *property;
265 const BusProperty *p;
267 if (!dbus_message_get_args(
270 DBUS_TYPE_STRING, &interface,
271 DBUS_TYPE_STRING, &property,
273 return bus_send_error_reply(c, message, &error, -EINVAL);
275 for (p = properties; p->property; p++)
276 if (streq(p->interface, interface) && streq(p->property, property))
280 DBusMessageIter iter, sub;
282 if (!(reply = dbus_message_new_method_return(message)))
285 dbus_message_iter_init_append(reply, &iter);
287 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
290 if ((r = p->append(&sub, property, (void*) p->data)) < 0) {
295 dbus_message_unref(reply);
296 return bus_send_error_reply(c, message, NULL, r);
299 if (!dbus_message_iter_close_container(&iter, &sub))
302 if (!nulstr_contains(interfaces, interface))
303 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
305 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
307 return bus_send_error_reply(c, message, &error, -EINVAL);
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;
315 if (!dbus_message_get_args(
318 DBUS_TYPE_STRING, &interface,
320 return bus_send_error_reply(c, message, &error, -EINVAL);
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);
327 if (!(reply = dbus_message_new_method_return(message)))
330 dbus_message_iter_init_append(reply, &iter);
332 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
335 for (p = properties; p->property; p++) {
336 if (interface[0] && !streq(p->interface, interface))
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))
344 if ((r = p->append(&sub3, p->property, (void*) p->data)) < 0) {
349 dbus_message_unref(reply);
350 return bus_send_error_reply(c, message, NULL, r);
353 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
354 !dbus_message_iter_close_container(&sub, &sub2))
358 if (!dbus_message_iter_close_container(&iter, &sub))
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;
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);
370 dbus_message_iter_get_basic(&iter, &interface);
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);
376 dbus_message_iter_get_basic(&iter, &property);
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);
383 for (p = properties; p->property; p++)
384 if (streq(p->interface, interface) && streq(p->property, property))
391 dbus_message_iter_recurse(&iter, &sub);
393 if (!(sig = dbus_message_iter_get_signature(&sub)))
396 if (!streq(sig, p->signature)) {
398 return bus_send_error_reply(c, message, NULL, -EINVAL);
403 if ((r = p->set(&sub, property)) < 0) {
406 return bus_send_error_reply(c, message, NULL, r);
409 if (!(reply = dbus_message_new_method_return(message)))
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");
417 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
419 return bus_send_error_reply(c, message, &error, -EINVAL);
423 const char *interface = dbus_message_get_interface(message);
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);
432 if (!dbus_connection_send(c, reply, NULL))
435 dbus_message_unref(reply);
436 return DBUS_HANDLER_RESULT_HANDLED;
439 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
443 dbus_message_unref(reply);
445 dbus_error_free(&error);
447 return DBUS_HANDLER_RESULT_NEED_MEMORY;
450 int bus_property_append_string(DBusMessageIter *i, const char *property, void *data) {
451 const char *t = data;
459 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
465 int bus_property_append_strv(DBusMessageIter *i, const char *property, void *data) {
471 return bus_append_strv_iter(i, t);
474 int bus_property_append_bool(DBusMessageIter *i, const char *property, void *data) {
484 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
490 int bus_property_append_uint64(DBusMessageIter *i, const char *property, void *data) {
495 /* Let's ensure that usec_t is actually 64bit, and hence this
496 * function can be used for usec_t */
497 assert_cc(sizeof(uint64_t) == sizeof(usec_t));
499 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
505 int bus_property_append_uint32(DBusMessageIter *i, const char *property, void *data) {
510 /* Let's ensure that pid_t, mode_t, uid_t, gid_t are actually
511 * 32bit, and hence this function can be used for
512 * pid_t/mode_t/uid_t/gid_t */
513 assert_cc(sizeof(uint32_t) == sizeof(pid_t));
514 assert_cc(sizeof(uint32_t) == sizeof(mode_t));
515 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
516 assert_cc(sizeof(uint32_t) == sizeof(uid_t));
517 assert_cc(sizeof(uint32_t) == sizeof(gid_t));
519 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
525 int bus_property_append_int32(DBusMessageIter *i, const char *property, void *data) {
530 assert_cc(sizeof(int32_t) == sizeof(int));
532 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
538 int bus_property_append_size(DBusMessageIter *i, const char *property, void *data) {
545 u = (uint64_t) *(size_t*) data;
547 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
553 int bus_property_append_ul(DBusMessageIter *i, const char *property, void *data) {
560 u = (uint64_t) *(unsigned long*) data;
562 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
568 int bus_property_append_long(DBusMessageIter *i, const char *property, void *data) {
575 l = (int64_t) *(long*) data;
577 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT64, &l))
583 const char *bus_errno_to_dbus(int error) {
588 return DBUS_ERROR_INVALID_ARGS;
591 return DBUS_ERROR_NO_MEMORY;
595 return DBUS_ERROR_ACCESS_DENIED;
598 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
601 return DBUS_ERROR_FILE_NOT_FOUND;
604 return DBUS_ERROR_FILE_EXISTS;
608 return DBUS_ERROR_TIMEOUT;
611 return DBUS_ERROR_IO_ERROR;
616 return DBUS_ERROR_DISCONNECTED;
619 return DBUS_ERROR_FAILED;
622 DBusHandlerResult bus_send_error_reply(DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
623 DBusMessage *reply = NULL;
624 const char *name, *text;
626 if (berror && dbus_error_is_set(berror)) {
628 text = berror->message;
630 name = bus_errno_to_dbus(error);
631 text = strerror(-error);
634 if (!(reply = dbus_message_new_error(message, name, text)))
637 if (!dbus_connection_send(c, reply, NULL))
640 dbus_message_unref(reply);
643 dbus_error_free(berror);
645 return DBUS_HANDLER_RESULT_HANDLED;
649 dbus_message_unref(reply);
652 dbus_error_free(berror);
654 return DBUS_HANDLER_RESULT_NEED_MEMORY;
657 DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties) {
659 DBusMessageIter iter, sub;
665 if (!(m = dbus_message_new_signal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged")))
668 dbus_message_iter_init_append(m, &iter);
670 /* We won't send any property values, since they might be
671 * large and sometimes not cheap to generated */
673 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface) ||
674 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub) ||
675 !dbus_message_iter_close_container(&iter, &sub) ||
676 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub))
679 NULSTR_FOREACH(i, properties)
680 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &i))
683 if (!dbus_message_iter_close_container(&iter, &sub))
690 dbus_message_unref(m);
695 uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
701 /* no watch flags for disabled watches */
702 if (!dbus_watch_get_enabled(bus_watch))
705 flags = dbus_watch_get_flags(bus_watch);
707 if (flags & DBUS_WATCH_READABLE)
709 if (flags & DBUS_WATCH_WRITABLE)
712 return events | EPOLLHUP | EPOLLERR;
715 unsigned bus_events_to_flags(uint32_t events) {
718 if (events & EPOLLIN)
719 flags |= DBUS_WATCH_READABLE;
720 if (events & EPOLLOUT)
721 flags |= DBUS_WATCH_WRITABLE;
722 if (events & EPOLLHUP)
723 flags |= DBUS_WATCH_HANGUP;
724 if (events & EPOLLERR)
725 flags |= DBUS_WATCH_ERROR;
730 int bus_parse_strv(DBusMessage *m, char ***_l) {
731 DBusMessageIter iter;
736 if (!dbus_message_iter_init(m, &iter))
739 return bus_parse_strv_iter(&iter, _l);
742 int bus_parse_strv_iter(DBusMessageIter *iter, char ***_l) {
744 unsigned n = 0, i = 0;
750 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY ||
751 dbus_message_iter_get_element_type(iter) != DBUS_TYPE_STRING)
754 dbus_message_iter_recurse(iter, &sub);
756 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
758 dbus_message_iter_next(&sub);
761 if (!(l = new(char*, n+1)))
764 dbus_message_iter_recurse(iter, &sub);
766 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
769 assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
770 dbus_message_iter_get_basic(&sub, &s);
772 if (!(l[i++] = strdup(s))) {
777 dbus_message_iter_next(&sub);
789 int bus_append_strv_iter(DBusMessageIter *iter, char **l) {
794 if (!dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "s", &sub))
798 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, l))
801 if (!dbus_message_iter_close_container(iter, &sub))