chiark / gitweb /
dbus: timeout connection setup
[elogind.git] / src / dbus-common.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <assert.h>
23 #include <sys/socket.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <dbus/dbus.h>
27
28 #include "log.h"
29 #include "dbus-common.h"
30 #include "util.h"
31
32 int bus_check_peercred(DBusConnection *c) {
33         int fd;
34         struct ucred ucred;
35         socklen_t l;
36
37         assert(c);
38
39         assert_se(dbus_connection_get_unix_fd(c, &fd));
40
41         l = sizeof(struct ucred);
42         if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) {
43                 log_error("SO_PEERCRED failed: %m");
44                 return -errno;
45         }
46
47         if (l != sizeof(struct ucred)) {
48                 log_error("SO_PEERCRED returned wrong size.");
49                 return -E2BIG;
50         }
51
52         if (ucred.uid != 0)
53                 return -EPERM;
54
55         return 1;
56 }
57
58 int bus_connect(DBusBusType t, DBusConnection **_bus, bool *private, DBusError *error) {
59         DBusConnection *bus;
60
61         assert(_bus);
62
63 #define TIMEOUT_USEC (60*USEC_PER_SEC)
64
65         /* If we are root, then let's not go via the bus */
66         if (geteuid() == 0 && t == DBUS_BUS_SYSTEM) {
67                 usec_t begin, tstamp;
68
69                 if (!(bus = dbus_connection_open_private("unix:abstract=/org/freedesktop/systemd1/private", error)))
70                         return -EIO;
71
72                 if (bus_check_peercred(bus) < 0) {
73                         dbus_connection_close(bus);
74                         dbus_connection_unref(bus);
75
76                         dbus_set_error_const(error, DBUS_ERROR_ACCESS_DENIED, "Failed to verify owner of bus.");
77                         return -EACCES;
78                 }
79
80                 begin = tstamp = now(CLOCK_MONOTONIC);
81                 for (;;) {
82
83                         if (tstamp > begin + TIMEOUT_USEC)
84                                 break;
85
86                         if (dbus_connection_get_is_authenticated(bus))
87                                 break;
88
89                         if (!dbus_connection_read_write_dispatch(bus, ((begin + TIMEOUT_USEC - tstamp) + USEC_PER_MSEC - 1) / USEC_PER_MSEC))
90                                 break;
91
92                         tstamp = now(CLOCK_MONOTONIC);
93                 }
94
95                 if (!dbus_connection_get_is_connected(bus)) {
96                         dbus_connection_close(bus);
97                         dbus_connection_unref(bus);
98
99                         dbus_set_error_const(error, DBUS_ERROR_NO_SERVER, "Connection terminated during authentication.");
100                         return -ECONNREFUSED;
101                 }
102
103                 if (!dbus_connection_get_is_authenticated(bus)) {
104                         dbus_connection_close(bus);
105                         dbus_connection_unref(bus);
106
107                         dbus_set_error_const(error, DBUS_ERROR_TIMEOUT, "Failed to authenticate in time.");
108                         return -EACCES;
109                 }
110
111                 if (private)
112                         *private = true;
113
114         } else {
115                 if (!(bus = dbus_bus_get_private(t, error)))
116                         return -EIO;
117
118                 if (private)
119                         *private = false;
120         }
121
122         dbus_connection_set_exit_on_disconnect(bus, FALSE);
123
124         *_bus = bus;
125         return 0;
126 }
127
128 const char *bus_error_message(const DBusError *error) {
129         assert(error);
130
131         /* Sometimes the D-Bus server is a little bit too verbose with
132          * its error messages, so let's override them here */
133         if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED))
134                 return "Access denied";
135
136         return error->message;
137 }