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>
26 #include <dbus/dbus.h>
29 #include "dbus-common.h"
31 int bus_check_peercred(DBusConnection *c) {
38 assert_se(dbus_connection_get_unix_fd(c, &fd));
40 l = sizeof(struct ucred);
41 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) {
42 log_error("SO_PEERCRED failed: %m");
46 if (l != sizeof(struct ucred)) {
47 log_error("SO_PEERCRED returned wrong size.");
57 int bus_connect(DBusBusType t, DBusConnection **_bus, bool *private, DBusError *error) {
62 /* If we are root, then let's not go via the bus */
63 if (geteuid() == 0 && t == DBUS_BUS_SYSTEM) {
65 if (!(bus = dbus_connection_open_private("unix:abstract=/org/freedesktop/systemd1/private", error)))
68 if (bus_check_peercred(bus) < 0) {
69 dbus_connection_unref(bus);
71 dbus_set_error_const(error, DBUS_ERROR_ACCESS_DENIED, "Failed to verify owner of bus.");
79 if (!(bus = dbus_bus_get_private(t, error)))
86 dbus_connection_set_exit_on_disconnect(bus, FALSE);
92 const char *bus_error_message(const DBusError *error) {
95 /* Sometimes the D-Bus server is a little bit too verbose with
96 * its error messages, so let's override them here */
97 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED))
98 return "Access denied";
100 return error->message;