chiark / gitweb /
sd-daemon: Add sd_is_special for special file descriptors
[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 #include <string.h>
30
31 #include "log.h"
32 #include "dbus-common.h"
33 #include "util.h"
34 #include "def.h"
35 #include "strv.h"
36
37 int bus_check_peercred(DBusConnection *c) {
38         int fd;
39         struct ucred ucred;
40         socklen_t l;
41
42         assert(c);
43
44         assert_se(dbus_connection_get_unix_fd(c, &fd));
45
46         l = sizeof(struct ucred);
47         if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) {
48                 log_error("SO_PEERCRED failed: %m");
49                 return -errno;
50         }
51
52         if (l != sizeof(struct ucred)) {
53                 log_error("SO_PEERCRED returned wrong size.");
54                 return -E2BIG;
55         }
56
57         if (ucred.uid != 0)
58                 return -EPERM;
59
60         return 1;
61 }
62
63 static int sync_auth(DBusConnection *bus, DBusError *error) {
64         usec_t begin, tstamp;
65
66         assert(bus);
67
68         /* This complexity should probably move into D-Bus itself:
69          *
70          * https://bugs.freedesktop.org/show_bug.cgi?id=35189 */
71
72         begin = tstamp = now(CLOCK_MONOTONIC);
73         for (;;) {
74
75                 if (tstamp > begin + DEFAULT_TIMEOUT_USEC)
76                         break;
77
78                 if (dbus_connection_get_is_authenticated(bus))
79                         break;
80
81                 if (!dbus_connection_read_write_dispatch(bus, ((begin + DEFAULT_TIMEOUT_USEC - tstamp) + USEC_PER_MSEC - 1) / USEC_PER_MSEC))
82                         break;
83
84                 tstamp = now(CLOCK_MONOTONIC);
85         }
86
87         if (!dbus_connection_get_is_connected(bus)) {
88                 dbus_set_error_const(error, DBUS_ERROR_NO_SERVER, "Connection terminated during authentication.");
89                 return -ECONNREFUSED;
90         }
91
92         if (!dbus_connection_get_is_authenticated(bus)) {
93                 dbus_set_error_const(error, DBUS_ERROR_TIMEOUT, "Failed to authenticate in time.");
94                 return -EACCES;
95         }
96
97         return 0;
98 }
99
100 int bus_connect(DBusBusType t, DBusConnection **_bus, bool *private, DBusError *error) {
101         DBusConnection *bus;
102         int r;
103
104         assert(_bus);
105
106         /* If we are root, then let's not go via the bus */
107         if (geteuid() == 0 && t == DBUS_BUS_SYSTEM) {
108
109                 if (!(bus = dbus_connection_open_private("unix:path=/run/systemd/private", error))) {
110 #ifndef LEGACY
111                         dbus_error_free(error);
112
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)))
115 #endif
116                                 return -EIO;
117                 }
118
119                 dbus_connection_set_exit_on_disconnect(bus, FALSE);
120
121                 if (bus_check_peercred(bus) < 0) {
122                         dbus_connection_close(bus);
123                         dbus_connection_unref(bus);
124
125                         dbus_set_error_const(error, DBUS_ERROR_ACCESS_DENIED, "Failed to verify owner of bus.");
126                         return -EACCES;
127                 }
128
129                 if (private)
130                         *private = true;
131
132         } else {
133                 if (!(bus = dbus_bus_get_private(t, error)))
134                         return -EIO;
135
136                 dbus_connection_set_exit_on_disconnect(bus, FALSE);
137
138                 if (private)
139                         *private = false;
140         }
141
142         if ((r = sync_auth(bus, error)) < 0) {
143                 dbus_connection_close(bus);
144                 dbus_connection_unref(bus);
145                 return r;
146         }
147
148         *_bus = bus;
149         return 0;
150 }
151
152 int bus_connect_system_ssh(const char *user, const char *host, DBusConnection **_bus, DBusError *error) {
153         DBusConnection *bus;
154         char *p = NULL;
155         int r;
156
157         assert(_bus);
158         assert(user || host);
159
160         if (user && host)
161                 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@%s,argv3=systemd-stdio-bridge", user, host);
162         else if (user)
163                 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@localhost,argv3=systemd-stdio-bridge", user);
164         else if (host)
165                 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s,argv3=systemd-stdio-bridge", host);
166
167         if (!p) {
168                 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
169                 return -ENOMEM;
170         }
171
172         bus = dbus_connection_open_private(p, error);
173         free(p);
174
175         if (!bus)
176                 return -EIO;
177
178         dbus_connection_set_exit_on_disconnect(bus, FALSE);
179
180         if ((r = sync_auth(bus, error)) < 0) {
181                 dbus_connection_close(bus);
182                 dbus_connection_unref(bus);
183                 return r;
184         }
185
186         if (!dbus_bus_register(bus, error)) {
187                 dbus_connection_close(bus);
188                 dbus_connection_unref(bus);
189                 return r;
190         }
191
192         *_bus = bus;
193         return 0;
194 }
195
196 int bus_connect_system_polkit(DBusConnection **_bus, DBusError *error) {
197         DBusConnection *bus;
198         int r;
199
200         assert(_bus);
201
202         /* Don't bother with PolicyKit if we are root */
203         if (geteuid() == 0)
204                 return bus_connect(DBUS_BUS_SYSTEM, _bus, NULL, error);
205
206         if (!(bus = dbus_connection_open_private("exec:path=pkexec,argv1=" SYSTEMD_STDIO_BRIDGE_BINARY_PATH, error)))
207                 return -EIO;
208
209         dbus_connection_set_exit_on_disconnect(bus, FALSE);
210
211         if ((r = sync_auth(bus, error)) < 0) {
212                 dbus_connection_close(bus);
213                 dbus_connection_unref(bus);
214                 return r;
215         }
216
217         if (!dbus_bus_register(bus, error)) {
218                 dbus_connection_close(bus);
219                 dbus_connection_unref(bus);
220                 return r;
221         }
222
223         *_bus = bus;
224         return 0;
225 }
226
227 const char *bus_error_message(const DBusError *error) {
228         assert(error);
229
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";
234
235         return error->message;
236 }
237
238 DBusHandlerResult bus_default_message_handler(
239                 DBusConnection *c,
240                 DBusMessage *message,
241                 const char *introspection,
242                 const char *interfaces,
243                 const BusProperty *properties) {
244
245         DBusError error;
246         DBusMessage *reply = NULL;
247         int r;
248
249         assert(c);
250         assert(message);
251
252         dbus_error_init(&error);
253
254         if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
255
256                 if (!(reply = dbus_message_new_method_return(message)))
257                         goto oom;
258
259                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
260                         goto oom;
261
262         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
263                 const char *interface, *property;
264                 const BusProperty *p;
265
266                 if (!dbus_message_get_args(
267                             message,
268                             &error,
269                             DBUS_TYPE_STRING, &interface,
270                             DBUS_TYPE_STRING, &property,
271                             DBUS_TYPE_INVALID))
272                         return bus_send_error_reply(c, message, &error, -EINVAL);
273
274                 for (p = properties; p->property; p++)
275                         if (streq(p->interface, interface) && streq(p->property, property))
276                                 break;
277
278                 if (p->property) {
279                         DBusMessageIter iter, sub;
280
281                         if (!(reply = dbus_message_new_method_return(message)))
282                                 goto oom;
283
284                         dbus_message_iter_init_append(reply, &iter);
285
286                         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
287                                 goto oom;
288
289                         if ((r = p->append(&sub, property, (void*) p->data)) < 0) {
290
291                                 if (r == -ENOMEM)
292                                         goto oom;
293
294                                 dbus_message_unref(reply);
295                                 return bus_send_error_reply(c, message, NULL, r);
296                         }
297
298                         if (!dbus_message_iter_close_container(&iter, &sub))
299                                 goto oom;
300                 } else {
301                         if (!nulstr_contains(interfaces, interface))
302                                 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
303                         else
304                                 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
305
306                         return bus_send_error_reply(c, message, &error, -EINVAL);
307                 }
308
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;
313
314                 if (!dbus_message_get_args(
315                             message,
316                             &error,
317                             DBUS_TYPE_STRING, &interface,
318                             DBUS_TYPE_INVALID))
319                         return bus_send_error_reply(c, message, &error, -EINVAL);
320
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);
324                 }
325
326                 if (!(reply = dbus_message_new_method_return(message)))
327                         goto oom;
328
329                 dbus_message_iter_init_append(reply, &iter);
330
331                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
332                         goto oom;
333
334                 for (p = properties; p->property; p++) {
335                         if (interface[0] && !streq(p->interface, interface))
336                                 continue;
337
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))
341                                 goto oom;
342
343                         if ((r = p->append(&sub3, p->property, (void*) p->data)) < 0) {
344
345                                 if (r == -ENOMEM)
346                                         goto oom;
347
348                                 dbus_message_unref(reply);
349                                 return bus_send_error_reply(c, message, NULL, r);
350                         }
351
352                         if (!dbus_message_iter_close_container(&sub2, &sub3) ||
353                             !dbus_message_iter_close_container(&sub, &sub2))
354                                 goto oom;
355                 }
356
357                 if (!dbus_message_iter_close_container(&iter, &sub))
358                         goto oom;
359
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;
364
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);
368
369                 dbus_message_iter_get_basic(&iter, &interface);
370
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);
374
375                 dbus_message_iter_get_basic(&iter, &property);
376
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);
381
382                 for (p = properties; p->property; p++)
383                         if (streq(p->interface, interface) && streq(p->property, property))
384                                 break;
385
386                 if (p->set) {
387                         DBusMessageIter sub;
388                         char *sig;
389
390                         dbus_message_iter_recurse(&iter, &sub);
391
392                         if (!(sig = dbus_message_iter_get_signature(&sub)))
393                                 goto oom;
394
395                         if (!streq(sig, p->signature)) {
396                                 dbus_free(sig);
397                                 return bus_send_error_reply(c, message, NULL, -EINVAL);
398                         }
399
400                         dbus_free(sig);
401
402                         if ((r = p->set(&sub, property)) < 0) {
403                                 if (r == -ENOMEM)
404                                         goto oom;
405                                 return bus_send_error_reply(c, message, NULL, r);
406                         }
407
408                         if (!(reply = dbus_message_new_method_return(message)))
409                                 goto oom;
410                 } else {
411                         if (p->property)
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");
415                         else
416                                 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
417
418                         return bus_send_error_reply(c, message, &error, -EINVAL);
419                 }
420
421         } else {
422                 const char *interface = dbus_message_get_interface(message);
423
424                 if (!interface || !nulstr_contains(interfaces, interface)) {
425                         dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
426                         return bus_send_error_reply(c, message, &error, -EINVAL);
427                 }
428         }
429
430         if (reply) {
431                 if (!dbus_connection_send(c, reply, NULL))
432                         goto oom;
433
434                 dbus_message_unref(reply);
435                 return DBUS_HANDLER_RESULT_HANDLED;
436         }
437
438         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
439
440 oom:
441         if (reply)
442                 dbus_message_unref(reply);
443
444         dbus_error_free(&error);
445
446         return DBUS_HANDLER_RESULT_NEED_MEMORY;
447 }
448
449 int bus_property_append_string(DBusMessageIter *i, const char *property, void *data) {
450         const char *t = data;
451
452         assert(i);
453         assert(property);
454
455         if (!t)
456                 t = "";
457
458         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
459                 return -ENOMEM;
460
461         return 0;
462 }
463
464 int bus_property_append_strv(DBusMessageIter *i, const char *property, void *data) {
465         DBusMessageIter sub;
466         char **t = data;
467
468         assert(i);
469         assert(property);
470
471         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
472                 return -ENOMEM;
473
474         STRV_FOREACH(t, t)
475                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
476                         return -ENOMEM;
477
478         if (!dbus_message_iter_close_container(i, &sub))
479                 return -ENOMEM;
480
481         return 0;
482 }
483
484 int bus_property_append_bool(DBusMessageIter *i, const char *property, void *data) {
485         bool *b = data;
486         dbus_bool_t db;
487
488         assert(i);
489         assert(property);
490         assert(b);
491
492         db = *b;
493
494         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
495                 return -ENOMEM;
496
497         return 0;
498 }
499
500 int bus_property_append_uint64(DBusMessageIter *i, const char *property, void *data) {
501         assert(i);
502         assert(property);
503         assert(data);
504
505         /* Let's ensure that pid_t is actually 64bit, and hence this
506          * function can be used for usec_t */
507         assert_cc(sizeof(uint64_t) == sizeof(usec_t));
508
509         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
510                 return -ENOMEM;
511
512         return 0;
513 }
514
515 int bus_property_append_uint32(DBusMessageIter *i, const char *property, void *data) {
516         assert(i);
517         assert(property);
518         assert(data);
519
520         /* Let's ensure that pid_t and mode_t is actually 32bit, and
521          * hence this function can be used for pid_t/mode_t */
522         assert_cc(sizeof(uint32_t) == sizeof(pid_t));
523         assert_cc(sizeof(uint32_t) == sizeof(mode_t));
524         assert_cc(sizeof(uint32_t) == sizeof(unsigned));
525
526         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
527                 return -ENOMEM;
528
529         return 0;
530 }
531
532 int bus_property_append_int32(DBusMessageIter *i, const char *property, void *data) {
533         assert(i);
534         assert(property);
535         assert(data);
536
537         assert_cc(sizeof(int32_t) == sizeof(int));
538
539         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
540                 return -ENOMEM;
541
542         return 0;
543 }
544
545 int bus_property_append_size(DBusMessageIter *i, const char *property, void *data) {
546         uint64_t u;
547
548         assert(i);
549         assert(property);
550         assert(data);
551
552         u = (uint64_t) *(size_t*) data;
553
554         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
555                 return -ENOMEM;
556
557         return 0;
558 }
559
560 int bus_property_append_ul(DBusMessageIter *i, const char *property, void *data) {
561         uint64_t u;
562
563         assert(i);
564         assert(property);
565         assert(data);
566
567         u = (uint64_t) *(unsigned long*) data;
568
569         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
570                 return -ENOMEM;
571
572         return 0;
573 }
574
575 int bus_property_append_long(DBusMessageIter *i, const char *property, void *data) {
576         int64_t l;
577
578         assert(i);
579         assert(property);
580         assert(data);
581
582         l = (int64_t) *(long*) data;
583
584         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT64, &l))
585                 return -ENOMEM;
586
587         return 0;
588 }
589
590 const char *bus_errno_to_dbus(int error) {
591
592         switch(error) {
593
594         case -EINVAL:
595                 return DBUS_ERROR_INVALID_ARGS;
596
597         case -ENOMEM:
598                 return DBUS_ERROR_NO_MEMORY;
599
600         case -EPERM:
601         case -EACCES:
602                 return DBUS_ERROR_ACCESS_DENIED;
603
604         case -ESRCH:
605                 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
606
607         case -ENOENT:
608                 return DBUS_ERROR_FILE_NOT_FOUND;
609
610         case -EEXIST:
611                 return DBUS_ERROR_FILE_EXISTS;
612
613         case -ETIMEDOUT:
614         case -ETIME:
615                 return DBUS_ERROR_TIMEOUT;
616
617         case -EIO:
618                 return DBUS_ERROR_IO_ERROR;
619
620         case -ENETRESET:
621         case -ECONNABORTED:
622         case -ECONNRESET:
623                 return DBUS_ERROR_DISCONNECTED;
624         }
625
626         return DBUS_ERROR_FAILED;
627 }
628
629 DBusHandlerResult bus_send_error_reply(DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
630         DBusMessage *reply = NULL;
631         const char *name, *text;
632
633         if (berror && dbus_error_is_set(berror)) {
634                 name = berror->name;
635                 text = berror->message;
636         } else {
637                 name = bus_errno_to_dbus(error);
638                 text = strerror(-error);
639         }
640
641         if (!(reply = dbus_message_new_error(message, name, text)))
642                 goto oom;
643
644         if (!dbus_connection_send(c, reply, NULL))
645                 goto oom;
646
647         dbus_message_unref(reply);
648
649         if (berror)
650                 dbus_error_free(berror);
651
652         return DBUS_HANDLER_RESULT_HANDLED;
653
654 oom:
655         if (reply)
656                 dbus_message_unref(reply);
657
658         if (berror)
659                 dbus_error_free(berror);
660
661         return DBUS_HANDLER_RESULT_NEED_MEMORY;
662 }
663
664 DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties) {
665         DBusMessage *m;
666         DBusMessageIter iter, sub;
667         const char *i;
668
669         assert(interface);
670         assert(properties);
671
672         if (!(m = dbus_message_new_signal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged")))
673                 goto oom;
674
675         dbus_message_iter_init_append(m, &iter);
676
677         /* We won't send any property values, since they might be
678          * large and sometimes not cheap to generated */
679
680         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface) ||
681             !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub) ||
682             !dbus_message_iter_close_container(&iter, &sub) ||
683             !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub))
684                 goto oom;
685
686         NULSTR_FOREACH(i, properties)
687                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &i))
688                         goto oom;
689
690         if (!dbus_message_iter_close_container(&iter, &sub))
691                 goto oom;
692
693         return m;
694
695 oom:
696         if (m)
697                 dbus_message_unref(m);
698
699         return NULL;
700 }