]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/bus-util.c
Merge pull request #4538 from fbuihuu/confirm-spawn-fixes
[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 and kdbus is not available, then let's talk
596 * directly to the system instance, instead of going via the
597 * bus */
598
599 r = sd_bus_new(&bus);
600 if (r < 0)
601 return r;
602
603 r = sd_bus_set_address(bus, KERNEL_SYSTEM_BUS_ADDRESS);
604 if (r < 0)
605 return r;
606
607 bus->bus_client = true;
608
609 r = sd_bus_start(bus);
610 if (r >= 0) {
611 *_bus = bus;
612 bus = NULL;
613 return 0;
614 }
615
616 bus = sd_bus_unref(bus);
617
618 r = sd_bus_new(&bus);
619 if (r < 0)
620 return r;
621
622 r = sd_bus_set_address(bus, "unix:path=/run/systemd/private");
623 if (r < 0)
624 return r;
625
626 r = sd_bus_start(bus);
627 if (r < 0)
628 return sd_bus_default_system(_bus);
629
630 r = bus_check_peercred(bus);
631 if (r < 0)
632 return r;
633
634 *_bus = bus;
635 bus = NULL;
636
637 return 0;
638 }
639
640 int bus_connect_user_systemd(sd_bus **_bus) {
641 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
642 _cleanup_free_ char *ee = NULL;
643 const char *e;
644 int r;
645
646 /* Try via kdbus first, and then directly */
647
648 assert(_bus);
649
650 r = sd_bus_new(&bus);
651 if (r < 0)
652 return r;
653
654 if (asprintf(&bus->address, KERNEL_USER_BUS_ADDRESS_FMT, getuid()) < 0)
655 return -ENOMEM;
656
657 bus->bus_client = true;
658
659 r = sd_bus_start(bus);
660 if (r >= 0) {
661 *_bus = bus;
662 bus = NULL;
663 return 0;
664 }
665
666 bus = sd_bus_unref(bus);
667
668 e = secure_getenv("XDG_RUNTIME_DIR");
669 if (!e)
670 return sd_bus_default_user(_bus);
671
672 ee = bus_address_escape(e);
673 if (!ee)
674 return -ENOMEM;
675
676 r = sd_bus_new(&bus);
677 if (r < 0)
678 return r;
679
680 bus->address = strjoin("unix:path=", ee, "/systemd/private");
681 if (!bus->address)
682 return -ENOMEM;
683
684 r = sd_bus_start(bus);
685 if (r < 0)
686 return sd_bus_default_user(_bus);
687
688 r = bus_check_peercred(bus);
689 if (r < 0)
690 return r;
691
692 *_bus = bus;
693 bus = NULL;
694
695 return 0;
696 }
697
698 #define print_property(name, fmt, ...) \
699 do { \
700 if (value) \
701 printf(fmt "\n", __VA_ARGS__); \
702 else \
703 printf("%s=" fmt "\n", name, __VA_ARGS__); \
704 } while(0)
705
706 int bus_print_property(const char *name, sd_bus_message *property, bool value, bool all) {
707 char type;
708 const char *contents;
709 int r;
710
711 assert(name);
712 assert(property);
713
714 r = sd_bus_message_peek_type(property, &type, &contents);
715 if (r < 0)
716 return r;
717
718 switch (type) {
719
720 case SD_BUS_TYPE_STRING: {
721 const char *s;
722
723 r = sd_bus_message_read_basic(property, type, &s);
724 if (r < 0)
725 return r;
726
727 if (all || !isempty(s)) {
728 _cleanup_free_ char *escaped = NULL;
729
730 escaped = xescape(s, "\n");
731 if (!escaped)
732 return -ENOMEM;
733
734 print_property(name, "%s", escaped);
735 }
736
737 return 1;
738 }
739
740 case SD_BUS_TYPE_BOOLEAN: {
741 int b;
742
743 r = sd_bus_message_read_basic(property, type, &b);
744 if (r < 0)
745 return r;
746
747 print_property(name, "%s", yes_no(b));
748
749 return 1;
750 }
751
752 case SD_BUS_TYPE_UINT64: {
753 uint64_t u;
754
755 r = sd_bus_message_read_basic(property, type, &u);
756 if (r < 0)
757 return r;
758
759 /* Yes, heuristics! But we can change this check
760 * should it turn out to not be sufficient */
761
762 if (endswith(name, "Timestamp")) {
763 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
764
765 t = format_timestamp(timestamp, sizeof(timestamp), u);
766 if (t || all)
767 print_property(name, "%s", strempty(t));
768
769 } else if (strstr(name, "USec")) {
770 char timespan[FORMAT_TIMESPAN_MAX];
771
772 print_property(name, "%s", format_timespan(timespan, sizeof(timespan), u, 0));
773 } else if (streq(name, "RestrictNamespaces")) {
774 _cleanup_free_ char *s = NULL;
775 const char *result = NULL;
776
777 if ((u & NAMESPACE_FLAGS_ALL) == 0)
778 result = "yes";
779 else if ((u & NAMESPACE_FLAGS_ALL) == NAMESPACE_FLAGS_ALL)
780 result = "no";
781 else {
782 r = namespace_flag_to_string_many(u, &s);
783 if (r < 0)
784 return r;
785
786 result = s;
787 }
788
789 print_property(name, "%s", result);
790 } else
791 print_property(name, "%"PRIu64, u);
792
793 return 1;
794 }
795
796 case SD_BUS_TYPE_INT64: {
797 int64_t i;
798
799 r = sd_bus_message_read_basic(property, type, &i);
800 if (r < 0)
801 return r;
802
803 print_property(name, "%"PRIi64, i);
804
805 return 1;
806 }
807
808 case SD_BUS_TYPE_UINT32: {
809 uint32_t u;
810
811 r = sd_bus_message_read_basic(property, type, &u);
812 if (r < 0)
813 return r;
814
815 if (strstr(name, "UMask") || strstr(name, "Mode"))
816 print_property(name, "%04o", u);
817 else
818 print_property(name, "%"PRIu32, u);
819
820 return 1;
821 }
822
823 case SD_BUS_TYPE_INT32: {
824 int32_t i;
825
826 r = sd_bus_message_read_basic(property, type, &i);
827 if (r < 0)
828 return r;
829
830 print_property(name, "%"PRIi32, i);
831 return 1;
832 }
833
834 case SD_BUS_TYPE_DOUBLE: {
835 double d;
836
837 r = sd_bus_message_read_basic(property, type, &d);
838 if (r < 0)
839 return r;
840
841 print_property(name, "%g", d);
842 return 1;
843 }
844
845 case SD_BUS_TYPE_ARRAY:
846 if (streq(contents, "s")) {
847 bool first = true;
848 const char *str;
849
850 r = sd_bus_message_enter_container(property, SD_BUS_TYPE_ARRAY, contents);
851 if (r < 0)
852 return r;
853
854 while ((r = sd_bus_message_read_basic(property, SD_BUS_TYPE_STRING, &str)) > 0) {
855 _cleanup_free_ char *escaped = NULL;
856
857 if (first && !value)
858 printf("%s=", name);
859
860 escaped = xescape(str, "\n ");
861 if (!escaped)
862 return -ENOMEM;
863
864 printf("%s%s", first ? "" : " ", escaped);
865
866 first = false;
867 }
868 if (r < 0)
869 return r;
870
871 if (first && all && !value)
872 printf("%s=", name);
873 if (!first || all)
874 puts("");
875
876 r = sd_bus_message_exit_container(property);
877 if (r < 0)
878 return r;
879
880 return 1;
881
882 } else if (streq(contents, "y")) {
883 const uint8_t *u;
884 size_t n;
885
886 r = sd_bus_message_read_array(property, SD_BUS_TYPE_BYTE, (const void**) &u, &n);
887 if (r < 0)
888 return r;
889
890 if (all || n > 0) {
891 unsigned int i;
892
893 if (!value)
894 printf("%s=", name);
895
896 for (i = 0; i < n; i++)
897 printf("%02x", u[i]);
898
899 puts("");
900 }
901
902 return 1;
903
904 } else if (streq(contents, "u")) {
905 uint32_t *u;
906 size_t n;
907
908 r = sd_bus_message_read_array(property, SD_BUS_TYPE_UINT32, (const void**) &u, &n);
909 if (r < 0)
910 return r;
911
912 if (all || n > 0) {
913 unsigned int i;
914
915 if (!value)
916 printf("%s=", name);
917
918 for (i = 0; i < n; i++)
919 printf("%08x", u[i]);
920
921 puts("");
922 }
923
924 return 1;
925 }
926
927 break;
928 }
929
930 return 0;
931 }
932
933 int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool value, bool all) {
934 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
935 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
936 int r;
937
938 assert(bus);
939 assert(path);
940
941 r = sd_bus_call_method(bus,
942 dest,
943 path,
944 "org.freedesktop.DBus.Properties",
945 "GetAll",
946 &error,
947 &reply,
948 "s", "");
949 if (r < 0)
950 return r;
951
952 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
953 if (r < 0)
954 return r;
955
956 while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
957 const char *name;
958 const char *contents;
959
960 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &name);
961 if (r < 0)
962 return r;
963
964 if (!filter || strv_find(filter, name)) {
965 r = sd_bus_message_peek_type(reply, NULL, &contents);
966 if (r < 0)
967 return r;
968
969 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
970 if (r < 0)
971 return r;
972
973 r = bus_print_property(name, reply, value, all);
974 if (r < 0)
975 return r;
976 if (r == 0) {
977 if (all)
978 printf("%s=[unprintable]\n", name);
979 /* skip what we didn't read */
980 r = sd_bus_message_skip(reply, contents);
981 if (r < 0)
982 return r;
983 }
984
985 r = sd_bus_message_exit_container(reply);
986 if (r < 0)
987 return r;
988 } else {
989 r = sd_bus_message_skip(reply, "v");
990 if (r < 0)
991 return r;
992 }
993
994 r = sd_bus_message_exit_container(reply);
995 if (r < 0)
996 return r;
997 }
998 if (r < 0)
999 return r;
1000
1001 r = sd_bus_message_exit_container(reply);
1002 if (r < 0)
1003 return r;
1004
1005 return 0;
1006 }
1007
1008 int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1009 sd_id128_t *p = userdata;
1010 const void *v;
1011 size_t n;
1012 int r;
1013
1014 r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, &v, &n);
1015 if (r < 0)
1016 return r;
1017
1018 if (n == 0)
1019 *p = SD_ID128_NULL;
1020 else if (n == 16)
1021 memcpy((*p).bytes, v, n);
1022 else
1023 return -EINVAL;
1024
1025 return 0;
1026 }
1027
1028 static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1029 char type;
1030 int r;
1031
1032 r = sd_bus_message_peek_type(m, &type, NULL);
1033 if (r < 0)
1034 return r;
1035
1036 switch (type) {
1037
1038 case SD_BUS_TYPE_STRING: {
1039 char **p = userdata;
1040 const char *s;
1041
1042 r = sd_bus_message_read_basic(m, type, &s);
1043 if (r < 0)
1044 return r;
1045
1046 if (isempty(s))
1047 s = NULL;
1048
1049 return free_and_strdup(p, s);
1050 }
1051
1052 case SD_BUS_TYPE_ARRAY: {
1053 _cleanup_strv_free_ char **l = NULL;
1054 char ***p = userdata;
1055
1056 r = bus_message_read_strv_extend(m, &l);
1057 if (r < 0)
1058 return r;
1059
1060 strv_free(*p);
1061 *p = l;
1062 l = NULL;
1063 return 0;
1064 }
1065
1066 case SD_BUS_TYPE_BOOLEAN: {
1067 unsigned b;
1068 int *p = userdata;
1069
1070 r = sd_bus_message_read_basic(m, type, &b);
1071 if (r < 0)
1072 return r;
1073
1074 *p = b;
1075 return 0;
1076 }
1077
1078 case SD_BUS_TYPE_INT32:
1079 case SD_BUS_TYPE_UINT32: {
1080 uint32_t u, *p = userdata;
1081
1082 r = sd_bus_message_read_basic(m, type, &u);
1083 if (r < 0)
1084 return r;
1085
1086 *p = u;
1087 return 0;
1088 }
1089
1090 case SD_BUS_TYPE_INT64:
1091 case SD_BUS_TYPE_UINT64: {
1092 uint64_t t, *p = userdata;
1093
1094 r = sd_bus_message_read_basic(m, type, &t);
1095 if (r < 0)
1096 return r;
1097
1098 *p = t;
1099 return 0;
1100 }
1101
1102 case SD_BUS_TYPE_DOUBLE: {
1103 double d, *p = userdata;
1104
1105 r = sd_bus_message_read_basic(m, type, &d);
1106 if (r < 0)
1107 return r;
1108
1109 *p = d;
1110 return 0;
1111 }}
1112
1113 return -EOPNOTSUPP;
1114 }
1115
1116 int bus_message_map_all_properties(
1117 sd_bus_message *m,
1118 const struct bus_properties_map *map,
1119 void *userdata) {
1120
1121 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1122 int r;
1123
1124 assert(m);
1125 assert(map);
1126
1127 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
1128 if (r < 0)
1129 return r;
1130
1131 while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
1132 const struct bus_properties_map *prop;
1133 const char *member;
1134 const char *contents;
1135 void *v;
1136 unsigned i;
1137
1138 r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member);
1139 if (r < 0)
1140 return r;
1141
1142 for (i = 0, prop = NULL; map[i].member; i++)
1143 if (streq(map[i].member, member)) {
1144 prop = &map[i];
1145 break;
1146 }
1147
1148 if (prop) {
1149 r = sd_bus_message_peek_type(m, NULL, &contents);
1150 if (r < 0)
1151 return r;
1152
1153 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
1154 if (r < 0)
1155 return r;
1156
1157 v = (uint8_t *)userdata + prop->offset;
1158 if (map[i].set)
1159 r = prop->set(sd_bus_message_get_bus(m), member, m, &error, v);
1160 else
1161 r = map_basic(sd_bus_message_get_bus(m), member, m, &error, v);
1162 if (r < 0)
1163 return r;
1164
1165 r = sd_bus_message_exit_container(m);
1166 if (r < 0)
1167 return r;
1168 } else {
1169 r = sd_bus_message_skip(m, "v");
1170 if (r < 0)
1171 return r;
1172 }
1173
1174 r = sd_bus_message_exit_container(m);
1175 if (r < 0)
1176 return r;
1177 }
1178 if (r < 0)
1179 return r;
1180
1181 return sd_bus_message_exit_container(m);
1182 }
1183
1184 int bus_message_map_properties_changed(
1185 sd_bus_message *m,
1186 const struct bus_properties_map *map,
1187 void *userdata) {
1188
1189 const char *member;
1190 int r, invalidated, i;
1191
1192 assert(m);
1193 assert(map);
1194
1195 r = bus_message_map_all_properties(m, map, userdata);
1196 if (r < 0)
1197 return r;
1198
1199 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "s");
1200 if (r < 0)
1201 return r;
1202
1203 invalidated = 0;
1204 while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member)) > 0)
1205 for (i = 0; map[i].member; i++)
1206 if (streq(map[i].member, member)) {
1207 ++invalidated;
1208 break;
1209 }
1210 if (r < 0)
1211 return r;
1212
1213 r = sd_bus_message_exit_container(m);
1214 if (r < 0)
1215 return r;
1216
1217 return invalidated;
1218 }
1219
1220 int bus_map_all_properties(
1221 sd_bus *bus,
1222 const char *destination,
1223 const char *path,
1224 const struct bus_properties_map *map,
1225 void *userdata) {
1226
1227 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1228 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1229 int r;
1230
1231 assert(bus);
1232 assert(destination);
1233 assert(path);
1234 assert(map);
1235
1236 r = sd_bus_call_method(
1237 bus,
1238 destination,
1239 path,
1240 "org.freedesktop.DBus.Properties",
1241 "GetAll",
1242 &error,
1243 &m,
1244 "s", "");
1245 if (r < 0)
1246 return r;
1247
1248 return bus_message_map_all_properties(m, map, userdata);
1249 }
1250
1251 int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **ret) {
1252 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
1253 int r;
1254
1255 assert(transport >= 0);
1256 assert(transport < _BUS_TRANSPORT_MAX);
1257 assert(ret);
1258
1259 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1260 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1261
1262 switch (transport) {
1263
1264 case BUS_TRANSPORT_LOCAL:
1265 if (user)
1266 r = sd_bus_default_user(&bus);
1267 else
1268 r = sd_bus_default_system(&bus);
1269
1270 break;
1271
1272 case BUS_TRANSPORT_REMOTE:
1273 r = sd_bus_open_system_remote(&bus, host);
1274 break;
1275
1276 case BUS_TRANSPORT_MACHINE:
1277 r = sd_bus_open_system_machine(&bus, host);
1278 break;
1279
1280 default:
1281 assert_not_reached("Hmm, unknown transport type.");
1282 }
1283 if (r < 0)
1284 return r;
1285
1286 r = sd_bus_set_exit_on_disconnect(bus, true);
1287 if (r < 0)
1288 return r;
1289
1290 *ret = bus;
1291 bus = NULL;
1292
1293 return 0;
1294 }
1295
1296 int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
1297 int r;
1298
1299 assert(transport >= 0);
1300 assert(transport < _BUS_TRANSPORT_MAX);
1301 assert(bus);
1302
1303 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1304 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1305
1306 switch (transport) {
1307
1308 case BUS_TRANSPORT_LOCAL:
1309 if (user)
1310 r = bus_connect_user_systemd(bus);
1311 else
1312 r = bus_connect_system_systemd(bus);
1313
1314 break;
1315
1316 case BUS_TRANSPORT_REMOTE:
1317 r = sd_bus_open_system_remote(bus, host);
1318 break;
1319
1320 case BUS_TRANSPORT_MACHINE:
1321 r = sd_bus_open_system_machine(bus, host);
1322 break;
1323
1324 default:
1325 assert_not_reached("Hmm, unknown transport type.");
1326 }
1327
1328 return r;
1329 }
1330
1331 int bus_property_get_bool(
1332 sd_bus *bus,
1333 const char *path,
1334 const char *interface,
1335 const char *property,
1336 sd_bus_message *reply,
1337 void *userdata,
1338 sd_bus_error *error) {
1339
1340 int b = *(bool*) userdata;
1341
1342 return sd_bus_message_append_basic(reply, 'b', &b);
1343 }
1344
1345 int bus_property_get_id128(
1346 sd_bus *bus,
1347 const char *path,
1348 const char *interface,
1349 const char *property,
1350 sd_bus_message *reply,
1351 void *userdata,
1352 sd_bus_error *error) {
1353
1354 sd_id128_t *id = userdata;
1355
1356 if (sd_id128_is_null(*id)) /* Add an empty array if the ID is zero */
1357 return sd_bus_message_append(reply, "ay", 0);
1358 else
1359 return sd_bus_message_append_array(reply, 'y', id->bytes, 16);
1360 }
1361
1362 #if __SIZEOF_SIZE_T__ != 8
1363 int bus_property_get_size(
1364 sd_bus *bus,
1365 const char *path,
1366 const char *interface,
1367 const char *property,
1368 sd_bus_message *reply,
1369 void *userdata,
1370 sd_bus_error *error) {
1371
1372 uint64_t sz = *(size_t*) userdata;
1373
1374 return sd_bus_message_append_basic(reply, 't', &sz);
1375 }
1376 #endif
1377
1378 #if __SIZEOF_LONG__ != 8
1379 int bus_property_get_long(
1380 sd_bus *bus,
1381 const char *path,
1382 const char *interface,
1383 const char *property,
1384 sd_bus_message *reply,
1385 void *userdata,
1386 sd_bus_error *error) {
1387
1388 int64_t l = *(long*) userdata;
1389
1390 return sd_bus_message_append_basic(reply, 'x', &l);
1391 }
1392
1393 int bus_property_get_ulong(
1394 sd_bus *bus,
1395 const char *path,
1396 const char *interface,
1397 const char *property,
1398 sd_bus_message *reply,
1399 void *userdata,
1400 sd_bus_error *error) {
1401
1402 uint64_t ul = *(unsigned long*) userdata;
1403
1404 return sd_bus_message_append_basic(reply, 't', &ul);
1405 }
1406 #endif
1407
1408 int bus_log_parse_error(int r) {
1409 return log_error_errno(r, "Failed to parse bus message: %m");
1410 }
1411
1412 int bus_log_create_error(int r) {
1413 return log_error_errno(r, "Failed to create bus message: %m");
1414 }
1415
1416 /**
1417 * bus_path_encode_unique() - encode unique object path
1418 * @b: bus connection or NULL
1419 * @prefix: object path prefix
1420 * @sender_id: unique-name of client, or NULL
1421 * @external_id: external ID to be chosen by client, or NULL
1422 * @ret_path: storage for encoded object path pointer
1423 *
1424 * Whenever we provide a bus API that allows clients to create and manage
1425 * server-side objects, we need to provide a unique name for these objects. If
1426 * we let the server choose the name, we suffer from a race condition: If a
1427 * client creates an object asynchronously, it cannot destroy that object until
1428 * it received the method reply. It cannot know the name of the new object,
1429 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1430 *
1431 * Therefore, many APIs allow the client to choose the unique name for newly
1432 * created objects. There're two problems to solve, though:
1433 * 1) Object names are usually defined via dbus object paths, which are
1434 * usually globally namespaced. Therefore, multiple clients must be able
1435 * to choose unique object names without interference.
1436 * 2) If multiple libraries share the same bus connection, they must be
1437 * able to choose unique object names without interference.
1438 * The first problem is solved easily by prefixing a name with the
1439 * unique-bus-name of a connection. The server side must enforce this and
1440 * reject any other name. The second problem is solved by providing unique
1441 * suffixes from within sd-bus.
1442 *
1443 * This helper allows clients to create unique object-paths. It uses the
1444 * template '/prefix/sender_id/external_id' and returns the new path in
1445 * @ret_path (must be freed by the caller).
1446 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1447 * NULL, this function allocates a unique suffix via @b (by requesting a new
1448 * cookie). If both @sender_id and @external_id are given, @b can be passed as
1449 * NULL.
1450 *
1451 * Returns: 0 on success, negative error code on failure.
1452 */
1453 int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) {
1454 _cleanup_free_ char *sender_label = NULL, *external_label = NULL;
1455 char external_buf[DECIMAL_STR_MAX(uint64_t)], *p;
1456 int r;
1457
1458 assert_return(b || (sender_id && external_id), -EINVAL);
1459 assert_return(object_path_is_valid(prefix), -EINVAL);
1460 assert_return(ret_path, -EINVAL);
1461
1462 if (!sender_id) {
1463 r = sd_bus_get_unique_name(b, &sender_id);
1464 if (r < 0)
1465 return r;
1466 }
1467
1468 if (!external_id) {
1469 xsprintf(external_buf, "%"PRIu64, ++b->cookie);
1470 external_id = external_buf;
1471 }
1472
1473 sender_label = bus_label_escape(sender_id);
1474 if (!sender_label)
1475 return -ENOMEM;
1476
1477 external_label = bus_label_escape(external_id);
1478 if (!external_label)
1479 return -ENOMEM;
1480
1481 p = strjoin(prefix, "/", sender_label, "/", external_label);
1482 if (!p)
1483 return -ENOMEM;
1484
1485 *ret_path = p;
1486 return 0;
1487 }
1488
1489 /**
1490 * bus_path_decode_unique() - decode unique object path
1491 * @path: object path to decode
1492 * @prefix: object path prefix
1493 * @ret_sender: output parameter for sender-id label
1494 * @ret_external: output parameter for external-id label
1495 *
1496 * This does the reverse of bus_path_encode_unique() (see its description for
1497 * details). Both trailing labels, sender-id and external-id, are unescaped and
1498 * returned in the given output parameters (the caller must free them).
1499 *
1500 * Note that this function returns 0 if the path does not match the template
1501 * (see bus_path_encode_unique()), 1 if it matched.
1502 *
1503 * Returns: Negative error code on failure, 0 if the given object path does not
1504 * match the template (return parameters are set to NULL), 1 if it was
1505 * parsed successfully (return parameters contain allocated labels).
1506 */
1507 int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) {
1508 const char *p, *q;
1509 char *sender, *external;
1510
1511 assert(object_path_is_valid(path));
1512 assert(object_path_is_valid(prefix));
1513 assert(ret_sender);
1514 assert(ret_external);
1515
1516 p = object_path_startswith(path, prefix);
1517 if (!p) {
1518 *ret_sender = NULL;
1519 *ret_external = NULL;
1520 return 0;
1521 }
1522
1523 q = strchr(p, '/');
1524 if (!q) {
1525 *ret_sender = NULL;
1526 *ret_external = NULL;
1527 return 0;
1528 }
1529
1530 sender = bus_label_unescape_n(p, q - p);
1531 external = bus_label_unescape(q + 1);
1532 if (!sender || !external) {
1533 free(sender);
1534 free(external);
1535 return -ENOMEM;
1536 }
1537
1538 *ret_sender = sender;
1539 *ret_external = external;
1540 return 1;
1541 }
1542
1543 int bus_property_get_rlimit(
1544 sd_bus *bus,
1545 const char *path,
1546 const char *interface,
1547 const char *property,
1548 sd_bus_message *reply,
1549 void *userdata,
1550 sd_bus_error *error) {
1551
1552 struct rlimit *rl;
1553 uint64_t u;
1554 rlim_t x;
1555 const char *is_soft;
1556
1557 assert(bus);
1558 assert(reply);
1559 assert(userdata);
1560
1561 is_soft = endswith(property, "Soft");
1562 rl = *(struct rlimit**) userdata;
1563 if (rl)
1564 x = is_soft ? rl->rlim_cur : rl->rlim_max;
1565 else {
1566 struct rlimit buf = {};
1567 int z;
1568 const char *s;
1569
1570 s = is_soft ? strndupa(property, is_soft - property) : property;
1571
1572 z = rlimit_from_string(strstr(s, "Limit"));
1573 assert(z >= 0);
1574
1575 getrlimit(z, &buf);
1576 x = is_soft ? buf.rlim_cur : buf.rlim_max;
1577 }
1578
1579 /* rlim_t might have different sizes, let's map
1580 * RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on
1581 * all archs */
1582 u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x;
1583
1584 return sd_bus_message_append(reply, "t", u);
1585 }
1586
1587 int bus_track_add_name_many(sd_bus_track *t, char **l) {
1588 int r = 0;
1589 char **i;
1590
1591 assert(t);
1592
1593 /* Continues adding after failure, and returns the first failure. */
1594
1595 STRV_FOREACH(i, l) {
1596 int k;
1597
1598 k = sd_bus_track_add_name(t, *i);
1599 if (k < 0 && r >= 0)
1600 r = k;
1601 }
1602
1603 return r;
1604 }