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>
32 #include "dbus-common.h"
37 int bus_check_peercred(DBusConnection *c) {
44 assert_se(dbus_connection_get_unix_fd(c, &fd));
46 l = sizeof(struct ucred);
47 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) {
48 log_error("SO_PEERCRED failed: %m");
52 if (l != sizeof(struct ucred)) {
53 log_error("SO_PEERCRED returned wrong size.");
63 static int sync_auth(DBusConnection *bus, DBusError *error) {
68 /* This complexity should probably move into D-Bus itself:
70 * https://bugs.freedesktop.org/show_bug.cgi?id=35189 */
72 begin = tstamp = now(CLOCK_MONOTONIC);
75 if (tstamp > begin + DEFAULT_TIMEOUT_USEC)
78 if (dbus_connection_get_is_authenticated(bus))
81 if (!dbus_connection_read_write_dispatch(bus, ((begin + DEFAULT_TIMEOUT_USEC - tstamp) + USEC_PER_MSEC - 1) / USEC_PER_MSEC))
84 tstamp = now(CLOCK_MONOTONIC);
87 if (!dbus_connection_get_is_connected(bus)) {
88 dbus_set_error_const(error, DBUS_ERROR_NO_SERVER, "Connection terminated during authentication.");
92 if (!dbus_connection_get_is_authenticated(bus)) {
93 dbus_set_error_const(error, DBUS_ERROR_TIMEOUT, "Failed to authenticate in time.");
100 int bus_connect(DBusBusType t, DBusConnection **_bus, bool *private, DBusError *error) {
106 /* If we are root, then let's not go via the bus */
107 if (geteuid() == 0 && t == DBUS_BUS_SYSTEM) {
109 if (!(bus = dbus_connection_open_private("unix:path=/run/systemd/private", error))) {
111 dbus_error_free(error);
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)))
119 dbus_connection_set_exit_on_disconnect(bus, FALSE);
121 if (bus_check_peercred(bus) < 0) {
122 dbus_connection_close(bus);
123 dbus_connection_unref(bus);
125 dbus_set_error_const(error, DBUS_ERROR_ACCESS_DENIED, "Failed to verify owner of bus.");
133 if (!(bus = dbus_bus_get_private(t, error)))
136 dbus_connection_set_exit_on_disconnect(bus, FALSE);
142 if ((r = sync_auth(bus, error)) < 0) {
143 dbus_connection_close(bus);
144 dbus_connection_unref(bus);
152 int bus_connect_system_ssh(const char *user, const char *host, DBusConnection **_bus, DBusError *error) {
158 assert(user || host);
161 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@%s,argv3=systemd-stdio-bridge", user, host);
163 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@localhost,argv3=systemd-stdio-bridge", user);
165 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s,argv3=systemd-stdio-bridge", host);
168 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
172 bus = dbus_connection_open_private(p, error);
178 dbus_connection_set_exit_on_disconnect(bus, FALSE);
180 if ((r = sync_auth(bus, error)) < 0) {
181 dbus_connection_close(bus);
182 dbus_connection_unref(bus);
186 if (!dbus_bus_register(bus, error)) {
187 dbus_connection_close(bus);
188 dbus_connection_unref(bus);
196 int bus_connect_system_polkit(DBusConnection **_bus, DBusError *error) {
202 /* Don't bother with PolicyKit if we are root */
204 return bus_connect(DBUS_BUS_SYSTEM, _bus, NULL, error);
206 if (!(bus = dbus_connection_open_private("exec:path=pkexec,argv1=" SYSTEMD_STDIO_BRIDGE_BINARY_PATH, error)))
209 dbus_connection_set_exit_on_disconnect(bus, FALSE);
211 if ((r = sync_auth(bus, error)) < 0) {
212 dbus_connection_close(bus);
213 dbus_connection_unref(bus);
217 if (!dbus_bus_register(bus, error)) {
218 dbus_connection_close(bus);
219 dbus_connection_unref(bus);
227 const char *bus_error_message(const DBusError *error) {
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";
235 return error->message;
238 DBusHandlerResult bus_default_message_handler(
240 DBusMessage *message,
241 const char *introspection,
242 const char *interfaces,
243 const BusProperty *properties) {
246 DBusMessage *reply = NULL;
252 dbus_error_init(&error);
254 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
256 if (!(reply = dbus_message_new_method_return(message)))
259 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
262 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
263 const char *interface, *property;
264 const BusProperty *p;
266 if (!dbus_message_get_args(
269 DBUS_TYPE_STRING, &interface,
270 DBUS_TYPE_STRING, &property,
272 return bus_send_error_reply(c, message, &error, -EINVAL);
274 for (p = properties; p->property; p++)
275 if (streq(p->interface, interface) && streq(p->property, property))
279 DBusMessageIter iter, sub;
281 if (!(reply = dbus_message_new_method_return(message)))
284 dbus_message_iter_init_append(reply, &iter);
286 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
289 if ((r = p->append(&sub, property, (void*) p->data)) < 0) {
294 dbus_message_unref(reply);
295 return bus_send_error_reply(c, message, NULL, r);
298 if (!dbus_message_iter_close_container(&iter, &sub))
301 if (!nulstr_contains(interfaces, interface))
302 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
304 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
306 return bus_send_error_reply(c, message, &error, -EINVAL);
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;
314 if (!dbus_message_get_args(
317 DBUS_TYPE_STRING, &interface,
319 return bus_send_error_reply(c, message, &error, -EINVAL);
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);
326 if (!(reply = dbus_message_new_method_return(message)))
329 dbus_message_iter_init_append(reply, &iter);
331 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
334 for (p = properties; p->property; p++) {
335 if (interface[0] && !streq(p->interface, interface))
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))
343 if ((r = p->append(&sub3, p->property, (void*) p->data)) < 0) {
348 dbus_message_unref(reply);
349 return bus_send_error_reply(c, message, NULL, r);
352 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
353 !dbus_message_iter_close_container(&sub, &sub2))
357 if (!dbus_message_iter_close_container(&iter, &sub))
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;
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);
369 dbus_message_iter_get_basic(&iter, &interface);
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);
375 dbus_message_iter_get_basic(&iter, &property);
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);
382 for (p = properties; p->property; p++)
383 if (streq(p->interface, interface) && streq(p->property, property))
390 dbus_message_iter_recurse(&iter, &sub);
392 if (!(sig = dbus_message_iter_get_signature(&sub)))
395 if (!streq(sig, p->signature)) {
397 return bus_send_error_reply(c, message, NULL, -EINVAL);
402 if ((r = p->set(&sub, property)) < 0) {
405 return bus_send_error_reply(c, message, NULL, r);
408 if (!(reply = dbus_message_new_method_return(message)))
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");
416 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
418 return bus_send_error_reply(c, message, &error, -EINVAL);
422 const char *interface = dbus_message_get_interface(message);
424 if (!interface || !nulstr_contains(interfaces, interface)) {
425 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
426 return bus_send_error_reply(c, message, &error, -EINVAL);
431 if (!dbus_connection_send(c, reply, NULL))
434 dbus_message_unref(reply);
435 return DBUS_HANDLER_RESULT_HANDLED;
438 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
442 dbus_message_unref(reply);
444 dbus_error_free(&error);
446 return DBUS_HANDLER_RESULT_NEED_MEMORY;
449 int bus_property_append_string(DBusMessageIter *i, const char *property, void *data) {
450 const char *t = data;
458 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
464 int bus_property_append_strv(DBusMessageIter *i, const char *property, void *data) {
471 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
475 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
478 if (!dbus_message_iter_close_container(i, &sub))
484 int bus_property_append_bool(DBusMessageIter *i, const char *property, void *data) {
494 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
500 int bus_property_append_uint64(DBusMessageIter *i, const char *property, void *data) {
505 /* Let's ensure that pid_t is actually 64bit, and hence this
506 * function can be used for usec_t */
507 assert_cc(sizeof(uint64_t) == sizeof(usec_t));
509 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
515 int bus_property_append_uint32(DBusMessageIter *i, const char *property, void *data) {
520 /* Let's ensure that pid_t and mode_t is actually 32bit, and
521 * hence this function can be used for pid_t/mode_t */
522 assert_cc(sizeof(uint32_t) == sizeof(pid_t));
523 assert_cc(sizeof(uint32_t) == sizeof(mode_t));
524 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
526 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
532 int bus_property_append_int32(DBusMessageIter *i, const char *property, void *data) {
537 assert_cc(sizeof(int32_t) == sizeof(int));
539 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
545 int bus_property_append_size(DBusMessageIter *i, const char *property, void *data) {
552 u = (uint64_t) *(size_t*) data;
554 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
560 int bus_property_append_ul(DBusMessageIter *i, const char *property, void *data) {
567 u = (uint64_t) *(unsigned long*) data;
569 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
575 int bus_property_append_long(DBusMessageIter *i, const char *property, void *data) {
582 l = (int64_t) *(long*) data;
584 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT64, &l))
590 const char *bus_errno_to_dbus(int error) {
595 return DBUS_ERROR_INVALID_ARGS;
598 return DBUS_ERROR_NO_MEMORY;
602 return DBUS_ERROR_ACCESS_DENIED;
605 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
608 return DBUS_ERROR_FILE_NOT_FOUND;
611 return DBUS_ERROR_FILE_EXISTS;
615 return DBUS_ERROR_TIMEOUT;
618 return DBUS_ERROR_IO_ERROR;
623 return DBUS_ERROR_DISCONNECTED;
626 return DBUS_ERROR_FAILED;
629 DBusHandlerResult bus_send_error_reply(DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
630 DBusMessage *reply = NULL;
631 const char *name, *text;
633 if (berror && dbus_error_is_set(berror)) {
635 text = berror->message;
637 name = bus_errno_to_dbus(error);
638 text = strerror(-error);
641 if (!(reply = dbus_message_new_error(message, name, text)))
644 if (!dbus_connection_send(c, reply, NULL))
647 dbus_message_unref(reply);
650 dbus_error_free(berror);
652 return DBUS_HANDLER_RESULT_HANDLED;
656 dbus_message_unref(reply);
659 dbus_error_free(berror);
661 return DBUS_HANDLER_RESULT_NEED_MEMORY;
664 DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties) {
666 DBusMessageIter iter, sub;
672 if (!(m = dbus_message_new_signal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged")))
675 dbus_message_iter_init_append(m, &iter);
677 /* We won't send any property values, since they might be
678 * large and sometimes not cheap to generated */
680 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface) ||
681 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub) ||
682 !dbus_message_iter_close_container(&iter, &sub) ||
683 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub))
686 NULSTR_FOREACH(i, properties)
687 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &i))
690 if (!dbus_message_iter_close_container(&iter, &sub))
697 dbus_message_unref(m);