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
);
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
);
427 if (!dbus_connection_send(c
, reply
, NULL
))
430 dbus_message_unref(reply
);
431 return DBUS_HANDLER_RESULT_HANDLED
;
434 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
438 dbus_message_unref(reply
);
440 dbus_error_free(&error
);
442 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
445 int bus_property_append_string(DBusMessageIter
*i
, const char *property
, void *data
) {
446 const char *t
= data
;
454 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
460 int bus_property_append_strv(DBusMessageIter
*i
, const char *property
, void *data
) {
467 if (!dbus_message_iter_open_container(i
, DBUS_TYPE_ARRAY
, "s", &sub
))
471 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, t
))
474 if (!dbus_message_iter_close_container(i
, &sub
))
480 int bus_property_append_bool(DBusMessageIter
*i
, const char *property
, void *data
) {
490 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_BOOLEAN
, &db
))
496 int bus_property_append_uint64(DBusMessageIter
*i
, const char *property
, void *data
) {
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
));
505 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT64
, data
))
511 int bus_property_append_uint32(DBusMessageIter
*i
, const char *property
, void *data
) {
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));
522 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT32
, data
))
528 int bus_property_append_int32(DBusMessageIter
*i
, const char *property
, void *data
) {
533 assert_cc(sizeof(int32_t) == sizeof(int));
535 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_INT32
, data
))
541 int bus_property_append_size(DBusMessageIter
*i
, const char *property
, void *data
) {
548 u
= (uint64_t) *(size_t*) data
;
550 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT64
, &u
))
556 int bus_property_append_ul(DBusMessageIter
*i
, const char *property
, void *data
) {
563 u
= (uint64_t) *(unsigned long*) data
;
565 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT64
, &u
))
571 const char *bus_errno_to_dbus(int error
) {
576 return DBUS_ERROR_INVALID_ARGS
;
579 return DBUS_ERROR_NO_MEMORY
;
583 return DBUS_ERROR_ACCESS_DENIED
;
586 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN
;
589 return DBUS_ERROR_FILE_NOT_FOUND
;
592 return DBUS_ERROR_FILE_EXISTS
;
596 return DBUS_ERROR_TIMEOUT
;
599 return DBUS_ERROR_IO_ERROR
;
604 return DBUS_ERROR_DISCONNECTED
;
607 return DBUS_ERROR_FAILED
;
610 DBusHandlerResult
bus_send_error_reply(DBusConnection
*c
, DBusMessage
*message
, DBusError
*berror
, int error
) {
611 DBusMessage
*reply
= NULL
;
612 const char *name
, *text
;
614 if (berror
&& dbus_error_is_set(berror
)) {
616 text
= berror
->message
;
618 name
= bus_errno_to_dbus(error
);
619 text
= strerror(-error
);
622 if (!(reply
= dbus_message_new_error(message
, name
, text
)))
625 if (!dbus_connection_send(c
, reply
, NULL
))
628 dbus_message_unref(reply
);
631 dbus_error_free(berror
);
633 return DBUS_HANDLER_RESULT_HANDLED
;
637 dbus_message_unref(reply
);
640 dbus_error_free(berror
);
642 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
645 DBusMessage
* bus_properties_changed_new(const char *path
, const char *interface
, const char *properties
) {
647 DBusMessageIter iter
, sub
;
653 if (!(m
= dbus_message_new_signal(path
, "org.freedesktop.DBus.Properties", "PropertiesChanged")))
656 dbus_message_iter_init_append(m
, &iter
);
658 /* We won't send any property values, since they might be
659 * large and sometimes not cheap to generated */
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
))
667 NULSTR_FOREACH(i
, properties
)
668 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &i
))
671 if (!dbus_message_iter_close_container(&iter
, &sub
))
678 dbus_message_unref(m
);