chiark / gitweb /
main: check if we have a valid PID before getting the name of it
[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 <stdio.h>
27 #include <stdlib.h>
28 #include <dbus/dbus.h>
29
30 #include "log.h"
31 #include "dbus-common.h"
32 #include "util.h"
33
34 int bus_check_peercred(DBusConnection *c) {
35         int fd;
36         struct ucred ucred;
37         socklen_t l;
38
39         assert(c);
40
41         assert_se(dbus_connection_get_unix_fd(c, &fd));
42
43         l = sizeof(struct ucred);
44         if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) {
45                 log_error("SO_PEERCRED failed: %m");
46                 return -errno;
47         }
48
49         if (l != sizeof(struct ucred)) {
50                 log_error("SO_PEERCRED returned wrong size.");
51                 return -E2BIG;
52         }
53
54         if (ucred.uid != 0)
55                 return -EPERM;
56
57         return 1;
58 }
59
60 #define TIMEOUT_USEC (60*USEC_PER_SEC)
61
62 static int sync_auth(DBusConnection *bus, DBusError *error) {
63         usec_t begin, tstamp;
64
65         assert(bus);
66
67         /* This complexity should probably move into D-Bus itself:
68          *
69          * https://bugs.freedesktop.org/show_bug.cgi?id=35189 */
70
71         begin = tstamp = now(CLOCK_MONOTONIC);
72         for (;;) {
73
74                 if (tstamp > begin + TIMEOUT_USEC)
75                         break;
76
77                 if (dbus_connection_get_is_authenticated(bus))
78                         break;
79
80                 if (!dbus_connection_read_write_dispatch(bus, ((begin + TIMEOUT_USEC - tstamp) + USEC_PER_MSEC - 1) / USEC_PER_MSEC))
81                         break;
82
83                 tstamp = now(CLOCK_MONOTONIC);
84         }
85
86         if (!dbus_connection_get_is_connected(bus)) {
87                 dbus_set_error_const(error, DBUS_ERROR_NO_SERVER, "Connection terminated during authentication.");
88                 return -ECONNREFUSED;
89         }
90
91         if (!dbus_connection_get_is_authenticated(bus)) {
92                 dbus_set_error_const(error, DBUS_ERROR_TIMEOUT, "Failed to authenticate in time.");
93                 return -EACCES;
94         }
95
96         return 0;
97 }
98
99 int bus_connect(DBusBusType t, DBusConnection **_bus, bool *private, DBusError *error) {
100         DBusConnection *bus;
101         int r;
102
103         assert(_bus);
104
105         /* If we are root, then let's not go via the bus */
106         if (geteuid() == 0 && t == DBUS_BUS_SYSTEM) {
107
108                 if (!(bus = dbus_connection_open_private("unix:path=/dev/.run/systemd/private", error))) {
109 #ifndef LEGACY
110                         dbus_error_free(error);
111
112                         /* Retry with the pre v21 socket name, to ease upgrades */
113                         if (!(bus = dbus_connection_open_private("unix:abstract=/org/freedesktop/systemd1/private", error)))
114 #endif
115                                 return -EIO;
116                 }
117
118                 dbus_connection_set_exit_on_disconnect(bus, FALSE);
119
120                 if (bus_check_peercred(bus) < 0) {
121                         dbus_connection_close(bus);
122                         dbus_connection_unref(bus);
123
124                         dbus_set_error_const(error, DBUS_ERROR_ACCESS_DENIED, "Failed to verify owner of bus.");
125                         return -EACCES;
126                 }
127
128                 if (private)
129                         *private = true;
130
131         } else {
132                 if (!(bus = dbus_bus_get_private(t, error)))
133                         return -EIO;
134
135                 dbus_connection_set_exit_on_disconnect(bus, FALSE);
136
137                 if (private)
138                         *private = false;
139         }
140
141         if ((r = sync_auth(bus, error)) < 0) {
142                 dbus_connection_close(bus);
143                 dbus_connection_unref(bus);
144                 return r;
145         }
146
147         *_bus = bus;
148         return 0;
149 }
150
151 int bus_connect_system_ssh(const char *user, const char *host, DBusConnection **_bus, DBusError *error) {
152         DBusConnection *bus;
153         char *p = NULL;
154         int r;
155
156         assert(_bus);
157         assert(user || host);
158
159         if (user && host)
160                 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@%s,argv3=systemd-stdio-bridge", user, host);
161         else if (user)
162                 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@localhost,argv3=systemd-stdio-bridge", user);
163         else if (host)
164                 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s,argv3=systemd-stdio-bridge", host);
165
166         if (!p) {
167                 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
168                 return -ENOMEM;
169         }
170
171         bus = dbus_connection_open_private(p, error);
172         free(p);
173
174         if (!bus)
175                 return -EIO;
176
177         dbus_connection_set_exit_on_disconnect(bus, FALSE);
178
179         if ((r = sync_auth(bus, error)) < 0) {
180                 dbus_connection_close(bus);
181                 dbus_connection_unref(bus);
182                 return r;
183         }
184
185         if (!dbus_bus_register(bus, error)) {
186                 dbus_connection_close(bus);
187                 dbus_connection_unref(bus);
188                 return r;
189         }
190
191         *_bus = bus;
192         return 0;
193 }
194
195 int bus_connect_system_polkit(DBusConnection **_bus, DBusError *error) {
196         DBusConnection *bus;
197         int r;
198
199         assert(_bus);
200
201         /* Don't bother with PolicyKit if we are root */
202         if (geteuid() == 0)
203                 return bus_connect(DBUS_BUS_SYSTEM, _bus, NULL, error);
204
205         if (!(bus = dbus_connection_open_private("exec:path=pkexec,argv1=" SYSTEMD_STDIO_BRIDGE_BINARY_PATH, error)))
206                 return -EIO;
207
208         dbus_connection_set_exit_on_disconnect(bus, FALSE);
209
210         if ((r = sync_auth(bus, error)) < 0) {
211                 dbus_connection_close(bus);
212                 dbus_connection_unref(bus);
213                 return r;
214         }
215
216         if (!dbus_bus_register(bus, error)) {
217                 dbus_connection_close(bus);
218                 dbus_connection_unref(bus);
219                 return r;
220         }
221
222         *_bus = bus;
223         return 0;
224 }
225
226 const char *bus_error_message(const DBusError *error) {
227         assert(error);
228
229         /* Sometimes the D-Bus server is a little bit too verbose with
230          * its error messages, so let's override them here */
231         if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED))
232                 return "Access denied";
233
234         return error->message;
235 }