]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/bus-util.c
Merge pull request #6462 from keszybz/man-tweaks
[thirdparty/systemd.git] / src / shared / bus-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2013 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <inttypes.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/ioctl.h>
27 #include <sys/resource.h>
28 #include <sys/socket.h>
29 #include <unistd.h>
30
31 #include "sd-bus-protocol.h"
32 #include "sd-bus.h"
33 #include "sd-daemon.h"
34 #include "sd-event.h"
35 #include "sd-id128.h"
36
37 #include "alloc-util.h"
38 #include "bus-internal.h"
39 #include "bus-label.h"
40 #include "bus-message.h"
41 #include "bus-util.h"
42 #include "def.h"
43 #include "escape.h"
44 #include "fd-util.h"
45 #include "missing.h"
46 #include "nsflags.h"
47 #include "parse-util.h"
48 #include "proc-cmdline.h"
49 #include "rlimit-util.h"
50 #include "stdio-util.h"
51 #include "strv.h"
52 #include "user-util.h"
53
54 static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
55 sd_event *e = userdata;
56
57 assert(m);
58 assert(e);
59
60 sd_bus_close(sd_bus_message_get_bus(m));
61 sd_event_exit(e, 0);
62
63 return 1;
64 }
65
66 int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name) {
67 _cleanup_free_ char *match = NULL;
68 const char *unique;
69 int r;
70
71 assert(e);
72 assert(bus);
73 assert(name);
74
75 /* We unregister the name here and then wait for the
76 * NameOwnerChanged signal for this event to arrive before we
77 * quit. We do this in order to make sure that any queued
78 * requests are still processed before we really exit. */
79
80 r = sd_bus_get_unique_name(bus, &unique);
81 if (r < 0)
82 return r;
83
84 r = asprintf(&match,
85 "sender='org.freedesktop.DBus',"
86 "type='signal',"
87 "interface='org.freedesktop.DBus',"
88 "member='NameOwnerChanged',"
89 "path='/org/freedesktop/DBus',"
90 "arg0='%s',"
91 "arg1='%s',"
92 "arg2=''", name, unique);
93 if (r < 0)
94 return -ENOMEM;
95
96 r = sd_bus_add_match(bus, NULL, match, name_owner_change_callback, e);
97 if (r < 0)
98 return r;
99
100 r = sd_bus_release_name(bus, name);
101 if (r < 0)
102 return r;
103
104 return 0;
105 }
106
107 int bus_event_loop_with_idle(
108 sd_event *e,
109 sd_bus *bus,
110 const char *name,
111 usec_t timeout,
112 check_idle_t check_idle,
113 void *userdata) {
114 bool exiting = false;
115 int r, code;
116
117 assert(e);
118 assert(bus);
119 assert(name);
120
121 for (;;) {
122 bool idle;
123
124 r = sd_event_get_state(e);
125 if (r < 0)
126 return r;
127 if (r == SD_EVENT_FINISHED)
128 break;
129
130 if (check_idle)
131 idle = check_idle(userdata);
132 else
133 idle = true;
134
135 r = sd_event_run(e, exiting || !idle ? (uint64_t) -1 : timeout);
136 if (r < 0)
137 return r;
138
139 if (r == 0 && !exiting && idle) {
140
141 r = sd_bus_try_close(bus);
142 if (r == -EBUSY)
143 continue;
144
145 /* Fallback for dbus1 connections: we
146 * unregister the name and wait for the
147 * response to come through for it */
148 if (r == -EOPNOTSUPP) {
149
150 /* Inform the service manager that we
151 * are going down, so that it will
152 * queue all further start requests,
153 * instead of assuming we are already
154 * running. */
155 sd_notify(false, "STOPPING=1");
156
157 r = bus_async_unregister_and_exit(e, bus, name);
158 if (r < 0)
159 return r;
160
161 exiting = true;
162 continue;
163 }
164
165 if (r < 0)
166 return r;
167
168 sd_event_exit(e, 0);
169 break;
170 }
171 }
172
173 r = sd_event_get_exit_code(e, &code);
174 if (r < 0)
175 return r;
176
177 return code;
178 }
179
180 int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error) {
181 _cleanup_(sd_bus_message_unrefp) sd_bus_message *rep = NULL;
182 int r, has_owner = 0;
183
184 assert(c);
185 assert(name);
186
187 r = sd_bus_call_method(c,
188 "org.freedesktop.DBus",
189 "/org/freedesktop/dbus",
190 "org.freedesktop.DBus",
191 "NameHasOwner",
192 error,
193 &rep,
194 "s",
195 name);
196 if (r < 0)
197 return r;
198
199 r = sd_bus_message_read_basic(rep, 'b', &has_owner);
200 if (r < 0)
201 return sd_bus_error_set_errno(error, r);
202
203 return has_owner;
204 }
205
206 static int check_good_user(sd_bus_message *m, uid_t good_user) {
207 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
208 uid_t sender_uid;
209 int r;
210
211 assert(m);
212
213 if (good_user == UID_INVALID)
214 return 0;
215
216 r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_EUID, &creds);
217 if (r < 0)
218 return r;
219
220 /* Don't trust augmented credentials for authorization */
221 assert_return((sd_bus_creds_get_augmented_mask(creds) & SD_BUS_CREDS_EUID) == 0, -EPERM);
222
223 r = sd_bus_creds_get_euid(creds, &sender_uid);
224 if (r < 0)
225 return r;
226
227 return sender_uid == good_user;
228 }
229
230 int bus_test_polkit(
231 sd_bus_message *call,
232 int capability,
233 const char *action,
234 const char **details,
235 uid_t good_user,
236 bool *_challenge,
237 sd_bus_error *e) {
238
239 int r;
240
241 assert(call);
242 assert(action);
243
244 /* Tests non-interactively! */
245
246 r = check_good_user(call, good_user);
247 if (r != 0)
248 return r;
249
250 r = sd_bus_query_sender_privilege(call, capability);
251 if (r < 0)
252 return r;
253 else if (r > 0)
254 return 1;
255 #ifdef ENABLE_POLKIT
256 else {
257 _cleanup_(sd_bus_message_unrefp) sd_bus_message *request = NULL;
258 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
259 int authorized = false, challenge = false;
260 const char *sender, **k, **v;
261
262 sender = sd_bus_message_get_sender(call);
263 if (!sender)
264 return -EBADMSG;
265
266 r = sd_bus_message_new_method_call(
267 call->bus,
268 &request,
269 "org.freedesktop.PolicyKit1",
270 "/org/freedesktop/PolicyKit1/Authority",
271 "org.freedesktop.PolicyKit1.Authority",
272 "CheckAuthorization");
273 if (r < 0)
274 return r;
275
276 r = sd_bus_message_append(
277 request,
278 "(sa{sv})s",
279 "system-bus-name", 1, "name", "s", sender,
280 action);
281 if (r < 0)
282 return r;
283
284 r = sd_bus_message_open_container(request, 'a', "{ss}");
285 if (r < 0)
286 return r;
287
288 STRV_FOREACH_PAIR(k, v, details) {
289 r = sd_bus_message_append(request, "{ss}", *k, *v);
290 if (r < 0)
291 return r;
292 }
293
294 r = sd_bus_message_close_container(request);
295 if (r < 0)
296 return r;
297
298 r = sd_bus_message_append(request, "us", 0, NULL);
299 if (r < 0)
300 return r;
301
302 r = sd_bus_call(call->bus, request, 0, e, &reply);
303 if (r < 0) {
304 /* Treat no PK available as access denied */
305 if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN)) {
306 sd_bus_error_free(e);
307 return -EACCES;
308 }
309
310 return r;
311 }
312
313 r = sd_bus_message_enter_container(reply, 'r', "bba{ss}");
314 if (r < 0)
315 return r;
316
317 r = sd_bus_message_read(reply, "bb", &authorized, &challenge);
318 if (r < 0)
319 return r;
320
321 if (authorized)
322 return 1;
323
324 if (_challenge) {
325 *_challenge = challenge;
326 return 0;
327 }
328 }
329 #endif
330
331 return -EACCES;
332 }
333
334 #ifdef ENABLE_POLKIT
335
336 typedef struct AsyncPolkitQuery {
337 sd_bus_message *request, *reply;
338 sd_bus_message_handler_t callback;
339 void *userdata;
340 sd_bus_slot *slot;
341 Hashmap *registry;
342 } AsyncPolkitQuery;
343
344 static void async_polkit_query_free(AsyncPolkitQuery *q) {
345
346 if (!q)
347 return;
348
349 sd_bus_slot_unref(q->slot);
350
351 if (q->registry && q->request)
352 hashmap_remove(q->registry, q->request);
353
354 sd_bus_message_unref(q->request);
355 sd_bus_message_unref(q->reply);
356
357 free(q);
358 }
359
360 static int async_polkit_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
361 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
362 AsyncPolkitQuery *q = userdata;
363 int r;
364
365 assert(reply);
366 assert(q);
367
368 q->slot = sd_bus_slot_unref(q->slot);
369 q->reply = sd_bus_message_ref(reply);
370
371 r = sd_bus_message_rewind(q->request, true);
372 if (r < 0) {
373 r = sd_bus_reply_method_errno(q->request, r, NULL);
374 goto finish;
375 }
376
377 r = q->callback(q->request, q->userdata, &error_buffer);
378 r = bus_maybe_reply_error(q->request, r, &error_buffer);
379
380 finish:
381 async_polkit_query_free(q);
382
383 return r;
384 }
385
386 #endif
387
388 int bus_verify_polkit_async(
389 sd_bus_message *call,
390 int capability,
391 const char *action,
392 const char **details,
393 bool interactive,
394 uid_t good_user,
395 Hashmap **registry,
396 sd_bus_error *error) {
397
398 #ifdef ENABLE_POLKIT
399 _cleanup_(sd_bus_message_unrefp) sd_bus_message *pk = NULL;
400 AsyncPolkitQuery *q;
401 const char *sender, **k, **v;
402 sd_bus_message_handler_t callback;
403 void *userdata;
404 int c;
405 #endif
406 int r;
407
408 assert(call);
409 assert(action);
410 assert(registry);
411
412 r = check_good_user(call, good_user);
413 if (r != 0)
414 return r;
415
416 #ifdef ENABLE_POLKIT
417 q = hashmap_get(*registry, call);
418 if (q) {
419 int authorized, challenge;
420
421 /* This is the second invocation of this function, and
422 * there's already a response from polkit, let's
423 * process it */
424 assert(q->reply);
425
426 if (sd_bus_message_is_method_error(q->reply, NULL)) {
427 const sd_bus_error *e;
428
429 /* Copy error from polkit reply */
430 e = sd_bus_message_get_error(q->reply);
431 sd_bus_error_copy(error, e);
432
433 /* Treat no PK available as access denied */
434 if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN))
435 return -EACCES;
436
437 return -sd_bus_error_get_errno(e);
438 }
439
440 r = sd_bus_message_enter_container(q->reply, 'r', "bba{ss}");
441 if (r >= 0)
442 r = sd_bus_message_read(q->reply, "bb", &authorized, &challenge);
443
444 if (r < 0)
445 return r;
446
447 if (authorized)
448 return 1;
449
450 if (challenge)
451 return sd_bus_error_set(error, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED, "Interactive authentication required.");
452
453 return -EACCES;
454 }
455 #endif
456
457 r = sd_bus_query_sender_privilege(call, capability);
458 if (r < 0)
459 return r;
460 else if (r > 0)
461 return 1;
462
463 #ifdef ENABLE_POLKIT
464 if (sd_bus_get_current_message(call->bus) != call)
465 return -EINVAL;
466
467 callback = sd_bus_get_current_handler(call->bus);
468 if (!callback)
469 return -EINVAL;
470
471 userdata = sd_bus_get_current_userdata(call->bus);
472
473 sender = sd_bus_message_get_sender(call);
474 if (!sender)
475 return -EBADMSG;
476
477 c = sd_bus_message_get_allow_interactive_authorization(call);
478 if (c < 0)
479 return c;
480 if (c > 0)
481 interactive = true;
482
483 r = hashmap_ensure_allocated(registry, NULL);
484 if (r < 0)
485 return r;
486
487 r = sd_bus_message_new_method_call(
488 call->bus,
489 &pk,
490 "org.freedesktop.PolicyKit1",
491 "/org/freedesktop/PolicyKit1/Authority",
492 "org.freedesktop.PolicyKit1.Authority",
493 "CheckAuthorization");
494 if (r < 0)
495 return r;
496
497 r = sd_bus_message_append(
498 pk,
499 "(sa{sv})s",
500 "system-bus-name", 1, "name", "s", sender,
501 action);
502 if (r < 0)
503 return r;
504
505 r = sd_bus_message_open_container(pk, 'a', "{ss}");
506 if (r < 0)
507 return r;
508
509 STRV_FOREACH_PAIR(k, v, details) {
510 r = sd_bus_message_append(pk, "{ss}", *k, *v);
511 if (r < 0)
512 return r;
513 }
514
515 r = sd_bus_message_close_container(pk);
516 if (r < 0)
517 return r;
518
519 r = sd_bus_message_append(pk, "us", !!interactive, NULL);
520 if (r < 0)
521 return r;
522
523 q = new0(AsyncPolkitQuery, 1);
524 if (!q)
525 return -ENOMEM;
526
527 q->request = sd_bus_message_ref(call);
528 q->callback = callback;
529 q->userdata = userdata;
530
531 r = hashmap_put(*registry, call, q);
532 if (r < 0) {
533 async_polkit_query_free(q);
534 return r;
535 }
536
537 q->registry = *registry;
538
539 r = sd_bus_call_async(call->bus, &q->slot, pk, async_polkit_callback, q, 0);
540 if (r < 0) {
541 async_polkit_query_free(q);
542 return r;
543 }
544
545 return 0;
546 #endif
547
548 return -EACCES;
549 }
550
551 void bus_verify_polkit_async_registry_free(Hashmap *registry) {
552 #ifdef ENABLE_POLKIT
553 AsyncPolkitQuery *q;
554
555 while ((q = hashmap_steal_first(registry)))
556 async_polkit_query_free(q);
557
558 hashmap_free(registry);
559 #endif
560 }
561
562 int bus_check_peercred(sd_bus *c) {
563 struct ucred ucred;
564 socklen_t l;
565 int fd;
566
567 assert(c);
568
569 fd = sd_bus_get_fd(c);
570 if (fd < 0)
571 return fd;
572
573 l = sizeof(struct ucred);
574 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0)
575 return -errno;
576
577 if (l != sizeof(struct ucred))
578 return -E2BIG;
579
580 if (ucred.uid != 0 && ucred.uid != geteuid())
581 return -EPERM;
582
583 return 1;
584 }
585
586 int bus_connect_system_systemd(sd_bus **_bus) {
587 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
588 int r;
589
590 assert(_bus);
591
592 if (geteuid() != 0)
593 return sd_bus_default_system(_bus);
594
595 /* If we are root then let's talk directly to the system
596 * instance, instead of going via the bus */
597
598 r = sd_bus_new(&bus);
599 if (r < 0)
600 return r;
601
602 r = sd_bus_set_address(bus, "unix:path=/run/systemd/private");
603 if (r < 0)
604 return r;
605
606 r = sd_bus_start(bus);
607 if (r < 0)
608 return sd_bus_default_system(_bus);
609
610 r = bus_check_peercred(bus);
611 if (r < 0)
612 return r;
613
614 *_bus = bus;
615 bus = NULL;
616
617 return 0;
618 }
619
620 int bus_connect_user_systemd(sd_bus **_bus) {
621 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
622 _cleanup_free_ char *ee = NULL;
623 const char *e;
624 int r;
625
626 assert(_bus);
627
628 e = secure_getenv("XDG_RUNTIME_DIR");
629 if (!e)
630 return sd_bus_default_user(_bus);
631
632 ee = bus_address_escape(e);
633 if (!ee)
634 return -ENOMEM;
635
636 r = sd_bus_new(&bus);
637 if (r < 0)
638 return r;
639
640 bus->address = strjoin("unix:path=", ee, "/systemd/private");
641 if (!bus->address)
642 return -ENOMEM;
643
644 r = sd_bus_start(bus);
645 if (r < 0)
646 return sd_bus_default_user(_bus);
647
648 r = bus_check_peercred(bus);
649 if (r < 0)
650 return r;
651
652 *_bus = bus;
653 bus = NULL;
654
655 return 0;
656 }
657
658 #define print_property(name, fmt, ...) \
659 do { \
660 if (value) \
661 printf(fmt "\n", __VA_ARGS__); \
662 else \
663 printf("%s=" fmt "\n", name, __VA_ARGS__); \
664 } while(0)
665
666 int bus_print_property(const char *name, sd_bus_message *property, bool value, bool all) {
667 char type;
668 const char *contents;
669 int r;
670
671 assert(name);
672 assert(property);
673
674 r = sd_bus_message_peek_type(property, &type, &contents);
675 if (r < 0)
676 return r;
677
678 switch (type) {
679
680 case SD_BUS_TYPE_STRING: {
681 const char *s;
682
683 r = sd_bus_message_read_basic(property, type, &s);
684 if (r < 0)
685 return r;
686
687 if (all || !isempty(s)) {
688 bool good;
689
690 /* This property has a single value, so we need to take
691 * care not to print a new line, everything else is OK. */
692 good = !strchr(s, '\n');
693 print_property(name, "%s", good ? s : "[unprintable]");
694 }
695
696 return 1;
697 }
698
699 case SD_BUS_TYPE_BOOLEAN: {
700 int b;
701
702 r = sd_bus_message_read_basic(property, type, &b);
703 if (r < 0)
704 return r;
705
706 print_property(name, "%s", yes_no(b));
707
708 return 1;
709 }
710
711 case SD_BUS_TYPE_UINT64: {
712 uint64_t u;
713
714 r = sd_bus_message_read_basic(property, type, &u);
715 if (r < 0)
716 return r;
717
718 /* Yes, heuristics! But we can change this check
719 * should it turn out to not be sufficient */
720
721 if (endswith(name, "Timestamp")) {
722 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
723
724 t = format_timestamp(timestamp, sizeof(timestamp), u);
725 if (t || all)
726 print_property(name, "%s", strempty(t));
727
728 } else if (strstr(name, "USec")) {
729 char timespan[FORMAT_TIMESPAN_MAX];
730
731 print_property(name, "%s", format_timespan(timespan, sizeof(timespan), u, 0));
732 } else if (streq(name, "RestrictNamespaces")) {
733 _cleanup_free_ char *s = NULL;
734 const char *result = NULL;
735
736 if ((u & NAMESPACE_FLAGS_ALL) == 0)
737 result = "yes";
738 else if ((u & NAMESPACE_FLAGS_ALL) == NAMESPACE_FLAGS_ALL)
739 result = "no";
740 else {
741 r = namespace_flag_to_string_many(u, &s);
742 if (r < 0)
743 return r;
744
745 result = s;
746 }
747
748 print_property(name, "%s", result);
749 } else
750 print_property(name, "%"PRIu64, u);
751
752 return 1;
753 }
754
755 case SD_BUS_TYPE_INT64: {
756 int64_t i;
757
758 r = sd_bus_message_read_basic(property, type, &i);
759 if (r < 0)
760 return r;
761
762 print_property(name, "%"PRIi64, i);
763
764 return 1;
765 }
766
767 case SD_BUS_TYPE_UINT32: {
768 uint32_t u;
769
770 r = sd_bus_message_read_basic(property, type, &u);
771 if (r < 0)
772 return r;
773
774 if (strstr(name, "UMask") || strstr(name, "Mode"))
775 print_property(name, "%04o", u);
776 else
777 print_property(name, "%"PRIu32, u);
778
779 return 1;
780 }
781
782 case SD_BUS_TYPE_INT32: {
783 int32_t i;
784
785 r = sd_bus_message_read_basic(property, type, &i);
786 if (r < 0)
787 return r;
788
789 print_property(name, "%"PRIi32, i);
790 return 1;
791 }
792
793 case SD_BUS_TYPE_DOUBLE: {
794 double d;
795
796 r = sd_bus_message_read_basic(property, type, &d);
797 if (r < 0)
798 return r;
799
800 print_property(name, "%g", d);
801 return 1;
802 }
803
804 case SD_BUS_TYPE_ARRAY:
805 if (streq(contents, "s")) {
806 bool first = true;
807 const char *str;
808
809 r = sd_bus_message_enter_container(property, SD_BUS_TYPE_ARRAY, contents);
810 if (r < 0)
811 return r;
812
813 while ((r = sd_bus_message_read_basic(property, SD_BUS_TYPE_STRING, &str)) > 0) {
814 bool good;
815
816 if (first && !value)
817 printf("%s=", name);
818
819 /* This property has multiple space-seperated values, so
820 * neither spaces not newlines can be allowed in a value. */
821 good = str[strcspn(str, " \n")] == '\0';
822
823 printf("%s%s", first ? "" : " ", good ? str : "[unprintable]");
824
825 first = false;
826 }
827 if (r < 0)
828 return r;
829
830 if (first && all && !value)
831 printf("%s=", name);
832 if (!first || all)
833 puts("");
834
835 r = sd_bus_message_exit_container(property);
836 if (r < 0)
837 return r;
838
839 return 1;
840
841 } else if (streq(contents, "y")) {
842 const uint8_t *u;
843 size_t n;
844
845 r = sd_bus_message_read_array(property, SD_BUS_TYPE_BYTE, (const void**) &u, &n);
846 if (r < 0)
847 return r;
848
849 if (all || n > 0) {
850 unsigned int i;
851
852 if (!value)
853 printf("%s=", name);
854
855 for (i = 0; i < n; i++)
856 printf("%02x", u[i]);
857
858 puts("");
859 }
860
861 return 1;
862
863 } else if (streq(contents, "u")) {
864 uint32_t *u;
865 size_t n;
866
867 r = sd_bus_message_read_array(property, SD_BUS_TYPE_UINT32, (const void**) &u, &n);
868 if (r < 0)
869 return r;
870
871 if (all || n > 0) {
872 unsigned int i;
873
874 if (!value)
875 printf("%s=", name);
876
877 for (i = 0; i < n; i++)
878 printf("%08x", u[i]);
879
880 puts("");
881 }
882
883 return 1;
884 }
885
886 break;
887 }
888
889 return 0;
890 }
891
892 int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool value, bool all) {
893 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
894 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
895 int r;
896
897 assert(bus);
898 assert(path);
899
900 r = sd_bus_call_method(bus,
901 dest,
902 path,
903 "org.freedesktop.DBus.Properties",
904 "GetAll",
905 &error,
906 &reply,
907 "s", "");
908 if (r < 0)
909 return r;
910
911 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
912 if (r < 0)
913 return r;
914
915 while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
916 const char *name;
917 const char *contents;
918
919 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &name);
920 if (r < 0)
921 return r;
922
923 if (!filter || strv_find(filter, name)) {
924 r = sd_bus_message_peek_type(reply, NULL, &contents);
925 if (r < 0)
926 return r;
927
928 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
929 if (r < 0)
930 return r;
931
932 r = bus_print_property(name, reply, value, all);
933 if (r < 0)
934 return r;
935 if (r == 0) {
936 if (all)
937 printf("%s=[unprintable]\n", name);
938 /* skip what we didn't read */
939 r = sd_bus_message_skip(reply, contents);
940 if (r < 0)
941 return r;
942 }
943
944 r = sd_bus_message_exit_container(reply);
945 if (r < 0)
946 return r;
947 } else {
948 r = sd_bus_message_skip(reply, "v");
949 if (r < 0)
950 return r;
951 }
952
953 r = sd_bus_message_exit_container(reply);
954 if (r < 0)
955 return r;
956 }
957 if (r < 0)
958 return r;
959
960 r = sd_bus_message_exit_container(reply);
961 if (r < 0)
962 return r;
963
964 return 0;
965 }
966
967 int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
968 sd_id128_t *p = userdata;
969 const void *v;
970 size_t n;
971 int r;
972
973 r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, &v, &n);
974 if (r < 0)
975 return r;
976
977 if (n == 0)
978 *p = SD_ID128_NULL;
979 else if (n == 16)
980 memcpy((*p).bytes, v, n);
981 else
982 return -EINVAL;
983
984 return 0;
985 }
986
987 static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
988 char type;
989 int r;
990
991 r = sd_bus_message_peek_type(m, &type, NULL);
992 if (r < 0)
993 return r;
994
995 switch (type) {
996
997 case SD_BUS_TYPE_STRING: {
998 char **p = userdata;
999 const char *s;
1000
1001 r = sd_bus_message_read_basic(m, type, &s);
1002 if (r < 0)
1003 return r;
1004
1005 if (isempty(s))
1006 s = NULL;
1007
1008 return free_and_strdup(p, s);
1009 }
1010
1011 case SD_BUS_TYPE_ARRAY: {
1012 _cleanup_strv_free_ char **l = NULL;
1013 char ***p = userdata;
1014
1015 r = bus_message_read_strv_extend(m, &l);
1016 if (r < 0)
1017 return r;
1018
1019 strv_free(*p);
1020 *p = l;
1021 l = NULL;
1022 return 0;
1023 }
1024
1025 case SD_BUS_TYPE_BOOLEAN: {
1026 unsigned b;
1027 int *p = userdata;
1028
1029 r = sd_bus_message_read_basic(m, type, &b);
1030 if (r < 0)
1031 return r;
1032
1033 *p = b;
1034 return 0;
1035 }
1036
1037 case SD_BUS_TYPE_INT32:
1038 case SD_BUS_TYPE_UINT32: {
1039 uint32_t u, *p = userdata;
1040
1041 r = sd_bus_message_read_basic(m, type, &u);
1042 if (r < 0)
1043 return r;
1044
1045 *p = u;
1046 return 0;
1047 }
1048
1049 case SD_BUS_TYPE_INT64:
1050 case SD_BUS_TYPE_UINT64: {
1051 uint64_t t, *p = userdata;
1052
1053 r = sd_bus_message_read_basic(m, type, &t);
1054 if (r < 0)
1055 return r;
1056
1057 *p = t;
1058 return 0;
1059 }
1060
1061 case SD_BUS_TYPE_DOUBLE: {
1062 double d, *p = userdata;
1063
1064 r = sd_bus_message_read_basic(m, type, &d);
1065 if (r < 0)
1066 return r;
1067
1068 *p = d;
1069 return 0;
1070 }}
1071
1072 return -EOPNOTSUPP;
1073 }
1074
1075 int bus_message_map_all_properties(
1076 sd_bus_message *m,
1077 const struct bus_properties_map *map,
1078 sd_bus_error *error,
1079 void *userdata) {
1080
1081 int r;
1082
1083 assert(m);
1084 assert(map);
1085
1086 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
1087 if (r < 0)
1088 return r;
1089
1090 while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
1091 const struct bus_properties_map *prop;
1092 const char *member;
1093 const char *contents;
1094 void *v;
1095 unsigned i;
1096
1097 r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member);
1098 if (r < 0)
1099 return r;
1100
1101 for (i = 0, prop = NULL; map[i].member; i++)
1102 if (streq(map[i].member, member)) {
1103 prop = &map[i];
1104 break;
1105 }
1106
1107 if (prop) {
1108 r = sd_bus_message_peek_type(m, NULL, &contents);
1109 if (r < 0)
1110 return r;
1111
1112 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
1113 if (r < 0)
1114 return r;
1115
1116 v = (uint8_t *)userdata + prop->offset;
1117 if (map[i].set)
1118 r = prop->set(sd_bus_message_get_bus(m), member, m, error, v);
1119 else
1120 r = map_basic(sd_bus_message_get_bus(m), member, m, error, v);
1121 if (r < 0)
1122 return r;
1123
1124 r = sd_bus_message_exit_container(m);
1125 if (r < 0)
1126 return r;
1127 } else {
1128 r = sd_bus_message_skip(m, "v");
1129 if (r < 0)
1130 return r;
1131 }
1132
1133 r = sd_bus_message_exit_container(m);
1134 if (r < 0)
1135 return r;
1136 }
1137 if (r < 0)
1138 return r;
1139
1140 return sd_bus_message_exit_container(m);
1141 }
1142
1143 int bus_message_map_properties_changed(
1144 sd_bus_message *m,
1145 const struct bus_properties_map *map,
1146 sd_bus_error *error,
1147 void *userdata) {
1148
1149 const char *member;
1150 int r, invalidated, i;
1151
1152 assert(m);
1153 assert(map);
1154
1155 r = bus_message_map_all_properties(m, map, error, userdata);
1156 if (r < 0)
1157 return r;
1158
1159 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "s");
1160 if (r < 0)
1161 return r;
1162
1163 invalidated = 0;
1164 while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member)) > 0)
1165 for (i = 0; map[i].member; i++)
1166 if (streq(map[i].member, member)) {
1167 ++invalidated;
1168 break;
1169 }
1170 if (r < 0)
1171 return r;
1172
1173 r = sd_bus_message_exit_container(m);
1174 if (r < 0)
1175 return r;
1176
1177 return invalidated;
1178 }
1179
1180 int bus_map_all_properties(
1181 sd_bus *bus,
1182 const char *destination,
1183 const char *path,
1184 const struct bus_properties_map *map,
1185 sd_bus_error *error,
1186 void *userdata) {
1187
1188 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1189 int r;
1190
1191 assert(bus);
1192 assert(destination);
1193 assert(path);
1194 assert(map);
1195
1196 r = sd_bus_call_method(
1197 bus,
1198 destination,
1199 path,
1200 "org.freedesktop.DBus.Properties",
1201 "GetAll",
1202 error,
1203 &m,
1204 "s", "");
1205 if (r < 0)
1206 return r;
1207
1208 return bus_message_map_all_properties(m, map, error, userdata);
1209 }
1210
1211 int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **ret) {
1212 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
1213 int r;
1214
1215 assert(transport >= 0);
1216 assert(transport < _BUS_TRANSPORT_MAX);
1217 assert(ret);
1218
1219 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1220 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1221
1222 switch (transport) {
1223
1224 case BUS_TRANSPORT_LOCAL:
1225 if (user)
1226 r = sd_bus_default_user(&bus);
1227 else
1228 r = sd_bus_default_system(&bus);
1229
1230 break;
1231
1232 case BUS_TRANSPORT_REMOTE:
1233 r = sd_bus_open_system_remote(&bus, host);
1234 break;
1235
1236 case BUS_TRANSPORT_MACHINE:
1237 r = sd_bus_open_system_machine(&bus, host);
1238 break;
1239
1240 default:
1241 assert_not_reached("Hmm, unknown transport type.");
1242 }
1243 if (r < 0)
1244 return r;
1245
1246 r = sd_bus_set_exit_on_disconnect(bus, true);
1247 if (r < 0)
1248 return r;
1249
1250 *ret = bus;
1251 bus = NULL;
1252
1253 return 0;
1254 }
1255
1256 int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
1257 int r;
1258
1259 assert(transport >= 0);
1260 assert(transport < _BUS_TRANSPORT_MAX);
1261 assert(bus);
1262
1263 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1264 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1265
1266 switch (transport) {
1267
1268 case BUS_TRANSPORT_LOCAL:
1269 if (user)
1270 r = bus_connect_user_systemd(bus);
1271 else
1272 r = bus_connect_system_systemd(bus);
1273
1274 break;
1275
1276 case BUS_TRANSPORT_REMOTE:
1277 r = sd_bus_open_system_remote(bus, host);
1278 break;
1279
1280 case BUS_TRANSPORT_MACHINE:
1281 r = sd_bus_open_system_machine(bus, host);
1282 break;
1283
1284 default:
1285 assert_not_reached("Hmm, unknown transport type.");
1286 }
1287
1288 return r;
1289 }
1290
1291 int bus_property_get_bool(
1292 sd_bus *bus,
1293 const char *path,
1294 const char *interface,
1295 const char *property,
1296 sd_bus_message *reply,
1297 void *userdata,
1298 sd_bus_error *error) {
1299
1300 int b = *(bool*) userdata;
1301
1302 return sd_bus_message_append_basic(reply, 'b', &b);
1303 }
1304
1305 int bus_property_get_id128(
1306 sd_bus *bus,
1307 const char *path,
1308 const char *interface,
1309 const char *property,
1310 sd_bus_message *reply,
1311 void *userdata,
1312 sd_bus_error *error) {
1313
1314 sd_id128_t *id = userdata;
1315
1316 if (sd_id128_is_null(*id)) /* Add an empty array if the ID is zero */
1317 return sd_bus_message_append(reply, "ay", 0);
1318 else
1319 return sd_bus_message_append_array(reply, 'y', id->bytes, 16);
1320 }
1321
1322 #if __SIZEOF_SIZE_T__ != 8
1323 int bus_property_get_size(
1324 sd_bus *bus,
1325 const char *path,
1326 const char *interface,
1327 const char *property,
1328 sd_bus_message *reply,
1329 void *userdata,
1330 sd_bus_error *error) {
1331
1332 uint64_t sz = *(size_t*) userdata;
1333
1334 return sd_bus_message_append_basic(reply, 't', &sz);
1335 }
1336 #endif
1337
1338 #if __SIZEOF_LONG__ != 8
1339 int bus_property_get_long(
1340 sd_bus *bus,
1341 const char *path,
1342 const char *interface,
1343 const char *property,
1344 sd_bus_message *reply,
1345 void *userdata,
1346 sd_bus_error *error) {
1347
1348 int64_t l = *(long*) userdata;
1349
1350 return sd_bus_message_append_basic(reply, 'x', &l);
1351 }
1352
1353 int bus_property_get_ulong(
1354 sd_bus *bus,
1355 const char *path,
1356 const char *interface,
1357 const char *property,
1358 sd_bus_message *reply,
1359 void *userdata,
1360 sd_bus_error *error) {
1361
1362 uint64_t ul = *(unsigned long*) userdata;
1363
1364 return sd_bus_message_append_basic(reply, 't', &ul);
1365 }
1366 #endif
1367
1368 int bus_log_parse_error(int r) {
1369 return log_error_errno(r, "Failed to parse bus message: %m");
1370 }
1371
1372 int bus_log_create_error(int r) {
1373 return log_error_errno(r, "Failed to create bus message: %m");
1374 }
1375
1376 /**
1377 * bus_path_encode_unique() - encode unique object path
1378 * @b: bus connection or NULL
1379 * @prefix: object path prefix
1380 * @sender_id: unique-name of client, or NULL
1381 * @external_id: external ID to be chosen by client, or NULL
1382 * @ret_path: storage for encoded object path pointer
1383 *
1384 * Whenever we provide a bus API that allows clients to create and manage
1385 * server-side objects, we need to provide a unique name for these objects. If
1386 * we let the server choose the name, we suffer from a race condition: If a
1387 * client creates an object asynchronously, it cannot destroy that object until
1388 * it received the method reply. It cannot know the name of the new object,
1389 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1390 *
1391 * Therefore, many APIs allow the client to choose the unique name for newly
1392 * created objects. There're two problems to solve, though:
1393 * 1) Object names are usually defined via dbus object paths, which are
1394 * usually globally namespaced. Therefore, multiple clients must be able
1395 * to choose unique object names without interference.
1396 * 2) If multiple libraries share the same bus connection, they must be
1397 * able to choose unique object names without interference.
1398 * The first problem is solved easily by prefixing a name with the
1399 * unique-bus-name of a connection. The server side must enforce this and
1400 * reject any other name. The second problem is solved by providing unique
1401 * suffixes from within sd-bus.
1402 *
1403 * This helper allows clients to create unique object-paths. It uses the
1404 * template '/prefix/sender_id/external_id' and returns the new path in
1405 * @ret_path (must be freed by the caller).
1406 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1407 * NULL, this function allocates a unique suffix via @b (by requesting a new
1408 * cookie). If both @sender_id and @external_id are given, @b can be passed as
1409 * NULL.
1410 *
1411 * Returns: 0 on success, negative error code on failure.
1412 */
1413 int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) {
1414 _cleanup_free_ char *sender_label = NULL, *external_label = NULL;
1415 char external_buf[DECIMAL_STR_MAX(uint64_t)], *p;
1416 int r;
1417
1418 assert_return(b || (sender_id && external_id), -EINVAL);
1419 assert_return(object_path_is_valid(prefix), -EINVAL);
1420 assert_return(ret_path, -EINVAL);
1421
1422 if (!sender_id) {
1423 r = sd_bus_get_unique_name(b, &sender_id);
1424 if (r < 0)
1425 return r;
1426 }
1427
1428 if (!external_id) {
1429 xsprintf(external_buf, "%"PRIu64, ++b->cookie);
1430 external_id = external_buf;
1431 }
1432
1433 sender_label = bus_label_escape(sender_id);
1434 if (!sender_label)
1435 return -ENOMEM;
1436
1437 external_label = bus_label_escape(external_id);
1438 if (!external_label)
1439 return -ENOMEM;
1440
1441 p = strjoin(prefix, "/", sender_label, "/", external_label);
1442 if (!p)
1443 return -ENOMEM;
1444
1445 *ret_path = p;
1446 return 0;
1447 }
1448
1449 /**
1450 * bus_path_decode_unique() - decode unique object path
1451 * @path: object path to decode
1452 * @prefix: object path prefix
1453 * @ret_sender: output parameter for sender-id label
1454 * @ret_external: output parameter for external-id label
1455 *
1456 * This does the reverse of bus_path_encode_unique() (see its description for
1457 * details). Both trailing labels, sender-id and external-id, are unescaped and
1458 * returned in the given output parameters (the caller must free them).
1459 *
1460 * Note that this function returns 0 if the path does not match the template
1461 * (see bus_path_encode_unique()), 1 if it matched.
1462 *
1463 * Returns: Negative error code on failure, 0 if the given object path does not
1464 * match the template (return parameters are set to NULL), 1 if it was
1465 * parsed successfully (return parameters contain allocated labels).
1466 */
1467 int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) {
1468 const char *p, *q;
1469 char *sender, *external;
1470
1471 assert(object_path_is_valid(path));
1472 assert(object_path_is_valid(prefix));
1473 assert(ret_sender);
1474 assert(ret_external);
1475
1476 p = object_path_startswith(path, prefix);
1477 if (!p) {
1478 *ret_sender = NULL;
1479 *ret_external = NULL;
1480 return 0;
1481 }
1482
1483 q = strchr(p, '/');
1484 if (!q) {
1485 *ret_sender = NULL;
1486 *ret_external = NULL;
1487 return 0;
1488 }
1489
1490 sender = bus_label_unescape_n(p, q - p);
1491 external = bus_label_unescape(q + 1);
1492 if (!sender || !external) {
1493 free(sender);
1494 free(external);
1495 return -ENOMEM;
1496 }
1497
1498 *ret_sender = sender;
1499 *ret_external = external;
1500 return 1;
1501 }
1502
1503 int bus_property_get_rlimit(
1504 sd_bus *bus,
1505 const char *path,
1506 const char *interface,
1507 const char *property,
1508 sd_bus_message *reply,
1509 void *userdata,
1510 sd_bus_error *error) {
1511
1512 struct rlimit *rl;
1513 uint64_t u;
1514 rlim_t x;
1515 const char *is_soft;
1516
1517 assert(bus);
1518 assert(reply);
1519 assert(userdata);
1520
1521 is_soft = endswith(property, "Soft");
1522 rl = *(struct rlimit**) userdata;
1523 if (rl)
1524 x = is_soft ? rl->rlim_cur : rl->rlim_max;
1525 else {
1526 struct rlimit buf = {};
1527 int z;
1528 const char *s;
1529
1530 s = is_soft ? strndupa(property, is_soft - property) : property;
1531
1532 z = rlimit_from_string(strstr(s, "Limit"));
1533 assert(z >= 0);
1534
1535 getrlimit(z, &buf);
1536 x = is_soft ? buf.rlim_cur : buf.rlim_max;
1537 }
1538
1539 /* rlim_t might have different sizes, let's map
1540 * RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on
1541 * all archs */
1542 u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x;
1543
1544 return sd_bus_message_append(reply, "t", u);
1545 }
1546
1547 int bus_track_add_name_many(sd_bus_track *t, char **l) {
1548 int r = 0;
1549 char **i;
1550
1551 assert(t);
1552
1553 /* Continues adding after failure, and returns the first failure. */
1554
1555 STRV_FOREACH(i, l) {
1556 int k;
1557
1558 k = sd_bus_track_add_name(t, *i);
1559 if (k < 0 && r >= 0)
1560 r = k;
1561 }
1562
1563 return r;
1564 }