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>
31 #include "dbus-common.h"
35 int bus_check_peercred(DBusConnection
*c
) {
42 assert_se(dbus_connection_get_unix_fd(c
, &fd
));
44 l
= sizeof(struct ucred
);
45 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &l
) < 0) {
46 log_error("SO_PEERCRED failed: %m");
50 if (l
!= sizeof(struct ucred
)) {
51 log_error("SO_PEERCRED returned wrong size.");
61 static int sync_auth(DBusConnection
*bus
, DBusError
*error
) {
66 /* This complexity should probably move into D-Bus itself:
68 * https://bugs.freedesktop.org/show_bug.cgi?id=35189 */
70 begin
= tstamp
= now(CLOCK_MONOTONIC
);
73 if (tstamp
> begin
+ DEFAULT_TIMEOUT_USEC
)
76 if (dbus_connection_get_is_authenticated(bus
))
79 if (!dbus_connection_read_write_dispatch(bus
, ((begin
+ DEFAULT_TIMEOUT_USEC
- tstamp
) + USEC_PER_MSEC
- 1) / USEC_PER_MSEC
))
82 tstamp
= now(CLOCK_MONOTONIC
);
85 if (!dbus_connection_get_is_connected(bus
)) {
86 dbus_set_error_const(error
, DBUS_ERROR_NO_SERVER
, "Connection terminated during authentication.");
90 if (!dbus_connection_get_is_authenticated(bus
)) {
91 dbus_set_error_const(error
, DBUS_ERROR_TIMEOUT
, "Failed to authenticate in time.");
98 int bus_connect(DBusBusType t
, DBusConnection
**_bus
, bool *private, DBusError
*error
) {
104 /* If we are root, then let's not go via the bus */
105 if (geteuid() == 0 && t
== DBUS_BUS_SYSTEM
) {
107 if (!(bus
= dbus_connection_open_private("unix:path=/dev/.run/systemd/private", error
))) {
109 dbus_error_free(error
);
111 /* Retry with the pre v21 socket name, to ease upgrades */
112 if (!(bus
= dbus_connection_open_private("unix:abstract=/org/freedesktop/systemd1/private", error
)))
117 dbus_connection_set_exit_on_disconnect(bus
, FALSE
);
119 if (bus_check_peercred(bus
) < 0) {
120 dbus_connection_close(bus
);
121 dbus_connection_unref(bus
);
123 dbus_set_error_const(error
, DBUS_ERROR_ACCESS_DENIED
, "Failed to verify owner of bus.");
131 if (!(bus
= dbus_bus_get_private(t
, error
)))
134 dbus_connection_set_exit_on_disconnect(bus
, FALSE
);
140 if ((r
= sync_auth(bus
, error
)) < 0) {
141 dbus_connection_close(bus
);
142 dbus_connection_unref(bus
);
150 int bus_connect_system_ssh(const char *user
, const char *host
, DBusConnection
**_bus
, DBusError
*error
) {
156 assert(user
|| host
);
159 asprintf(&p
, "exec:path=ssh,argv1=-xT,argv2=%s@%s,argv3=systemd-stdio-bridge", user
, host
);
161 asprintf(&p
, "exec:path=ssh,argv1=-xT,argv2=%s@localhost,argv3=systemd-stdio-bridge", user
);
163 asprintf(&p
, "exec:path=ssh,argv1=-xT,argv2=%s,argv3=systemd-stdio-bridge", host
);
166 dbus_set_error_const(error
, DBUS_ERROR_NO_MEMORY
, NULL
);
170 bus
= dbus_connection_open_private(p
, error
);
176 dbus_connection_set_exit_on_disconnect(bus
, FALSE
);
178 if ((r
= sync_auth(bus
, error
)) < 0) {
179 dbus_connection_close(bus
);
180 dbus_connection_unref(bus
);
184 if (!dbus_bus_register(bus
, error
)) {
185 dbus_connection_close(bus
);
186 dbus_connection_unref(bus
);
194 int bus_connect_system_polkit(DBusConnection
**_bus
, DBusError
*error
) {
200 /* Don't bother with PolicyKit if we are root */
202 return bus_connect(DBUS_BUS_SYSTEM
, _bus
, NULL
, error
);
204 if (!(bus
= dbus_connection_open_private("exec:path=pkexec,argv1=" SYSTEMD_STDIO_BRIDGE_BINARY_PATH
, error
)))
207 dbus_connection_set_exit_on_disconnect(bus
, FALSE
);
209 if ((r
= sync_auth(bus
, error
)) < 0) {
210 dbus_connection_close(bus
);
211 dbus_connection_unref(bus
);
215 if (!dbus_bus_register(bus
, error
)) {
216 dbus_connection_close(bus
);
217 dbus_connection_unref(bus
);
225 const char *bus_error_message(const DBusError
*error
) {
228 /* Sometimes the D-Bus server is a little bit too verbose with
229 * its error messages, so let's override them here */
230 if (dbus_error_has_name(error
, DBUS_ERROR_ACCESS_DENIED
))
231 return "Access denied";
233 return error
->message
;