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