]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/bus-util.c
bus-util: replace non-printable values with [unprintable]
[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 bool good;
729
730 /* This property has a single value, so we need to take
731 * care not to print a new line, everything else is OK. */
732 good = !strchr(s, '\n');
733 print_property(name, "%s", good ? s : "[unprintable]");
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 if (streq(name, "RestrictNamespaces")) {
773 _cleanup_free_ char *s = NULL;
774 const char *result = NULL;
775
776 if ((u & NAMESPACE_FLAGS_ALL) == 0)
777 result = "yes";
778 else if ((u & NAMESPACE_FLAGS_ALL) == NAMESPACE_FLAGS_ALL)
779 result = "no";
780 else {
781 r = namespace_flag_to_string_many(u, &s);
782 if (r < 0)
783 return r;
784
785 result = s;
786 }
787
788 print_property(name, "%s", result);
789 } else
790 print_property(name, "%"PRIu64, u);
791
792 return 1;
793 }
794
795 case SD_BUS_TYPE_INT64: {
796 int64_t i;
797
798 r = sd_bus_message_read_basic(property, type, &i);
799 if (r < 0)
800 return r;
801
802 print_property(name, "%"PRIi64, i);
803
804 return 1;
805 }
806
807 case SD_BUS_TYPE_UINT32: {
808 uint32_t u;
809
810 r = sd_bus_message_read_basic(property, type, &u);
811 if (r < 0)
812 return r;
813
814 if (strstr(name, "UMask") || strstr(name, "Mode"))
815 print_property(name, "%04o", u);
816 else
817 print_property(name, "%"PRIu32, u);
818
819 return 1;
820 }
821
822 case SD_BUS_TYPE_INT32: {
823 int32_t i;
824
825 r = sd_bus_message_read_basic(property, type, &i);
826 if (r < 0)
827 return r;
828
829 print_property(name, "%"PRIi32, i);
830 return 1;
831 }
832
833 case SD_BUS_TYPE_DOUBLE: {
834 double d;
835
836 r = sd_bus_message_read_basic(property, type, &d);
837 if (r < 0)
838 return r;
839
840 print_property(name, "%g", d);
841 return 1;
842 }
843
844 case SD_BUS_TYPE_ARRAY:
845 if (streq(contents, "s")) {
846 bool first = true;
847 const char *str;
848
849 r = sd_bus_message_enter_container(property, SD_BUS_TYPE_ARRAY, contents);
850 if (r < 0)
851 return r;
852
853 while ((r = sd_bus_message_read_basic(property, SD_BUS_TYPE_STRING, &str)) > 0) {
854 bool good;
855
856 if (first && !value)
857 printf("%s=", name);
858
859 /* This property has multiple space-seperated values, so
860 * neither spaces not newlines can be allowed in a value. */
861 good = str[strcspn(str, " \n")] == '\0';
862
863 printf("%s%s", first ? "" : " ", good ? str : "[unprintable]");
864
865 first = false;
866 }
867 if (r < 0)
868 return r;
869
870 if (first && all && !value)
871 printf("%s=", name);
872 if (!first || all)
873 puts("");
874
875 r = sd_bus_message_exit_container(property);
876 if (r < 0)
877 return r;
878
879 return 1;
880
881 } else if (streq(contents, "y")) {
882 const uint8_t *u;
883 size_t n;
884
885 r = sd_bus_message_read_array(property, SD_BUS_TYPE_BYTE, (const void**) &u, &n);
886 if (r < 0)
887 return r;
888
889 if (all || n > 0) {
890 unsigned int i;
891
892 if (!value)
893 printf("%s=", name);
894
895 for (i = 0; i < n; i++)
896 printf("%02x", u[i]);
897
898 puts("");
899 }
900
901 return 1;
902
903 } else if (streq(contents, "u")) {
904 uint32_t *u;
905 size_t n;
906
907 r = sd_bus_message_read_array(property, SD_BUS_TYPE_UINT32, (const void**) &u, &n);
908 if (r < 0)
909 return r;
910
911 if (all || n > 0) {
912 unsigned int i;
913
914 if (!value)
915 printf("%s=", name);
916
917 for (i = 0; i < n; i++)
918 printf("%08x", u[i]);
919
920 puts("");
921 }
922
923 return 1;
924 }
925
926 break;
927 }
928
929 return 0;
930 }
931
932 int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool value, bool all) {
933 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
934 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
935 int r;
936
937 assert(bus);
938 assert(path);
939
940 r = sd_bus_call_method(bus,
941 dest,
942 path,
943 "org.freedesktop.DBus.Properties",
944 "GetAll",
945 &error,
946 &reply,
947 "s", "");
948 if (r < 0)
949 return r;
950
951 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
952 if (r < 0)
953 return r;
954
955 while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
956 const char *name;
957 const char *contents;
958
959 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &name);
960 if (r < 0)
961 return r;
962
963 if (!filter || strv_find(filter, name)) {
964 r = sd_bus_message_peek_type(reply, NULL, &contents);
965 if (r < 0)
966 return r;
967
968 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
969 if (r < 0)
970 return r;
971
972 r = bus_print_property(name, reply, value, all);
973 if (r < 0)
974 return r;
975 if (r == 0) {
976 if (all)
977 printf("%s=[unprintable]\n", name);
978 /* skip what we didn't read */
979 r = sd_bus_message_skip(reply, contents);
980 if (r < 0)
981 return r;
982 }
983
984 r = sd_bus_message_exit_container(reply);
985 if (r < 0)
986 return r;
987 } else {
988 r = sd_bus_message_skip(reply, "v");
989 if (r < 0)
990 return r;
991 }
992
993 r = sd_bus_message_exit_container(reply);
994 if (r < 0)
995 return r;
996 }
997 if (r < 0)
998 return r;
999
1000 r = sd_bus_message_exit_container(reply);
1001 if (r < 0)
1002 return r;
1003
1004 return 0;
1005 }
1006
1007 int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1008 sd_id128_t *p = userdata;
1009 const void *v;
1010 size_t n;
1011 int r;
1012
1013 r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, &v, &n);
1014 if (r < 0)
1015 return r;
1016
1017 if (n == 0)
1018 *p = SD_ID128_NULL;
1019 else if (n == 16)
1020 memcpy((*p).bytes, v, n);
1021 else
1022 return -EINVAL;
1023
1024 return 0;
1025 }
1026
1027 static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1028 char type;
1029 int r;
1030
1031 r = sd_bus_message_peek_type(m, &type, NULL);
1032 if (r < 0)
1033 return r;
1034
1035 switch (type) {
1036
1037 case SD_BUS_TYPE_STRING: {
1038 char **p = userdata;
1039 const char *s;
1040
1041 r = sd_bus_message_read_basic(m, type, &s);
1042 if (r < 0)
1043 return r;
1044
1045 if (isempty(s))
1046 s = NULL;
1047
1048 return free_and_strdup(p, s);
1049 }
1050
1051 case SD_BUS_TYPE_ARRAY: {
1052 _cleanup_strv_free_ char **l = NULL;
1053 char ***p = userdata;
1054
1055 r = bus_message_read_strv_extend(m, &l);
1056 if (r < 0)
1057 return r;
1058
1059 strv_free(*p);
1060 *p = l;
1061 l = NULL;
1062 return 0;
1063 }
1064
1065 case SD_BUS_TYPE_BOOLEAN: {
1066 unsigned b;
1067 int *p = userdata;
1068
1069 r = sd_bus_message_read_basic(m, type, &b);
1070 if (r < 0)
1071 return r;
1072
1073 *p = b;
1074 return 0;
1075 }
1076
1077 case SD_BUS_TYPE_INT32:
1078 case SD_BUS_TYPE_UINT32: {
1079 uint32_t u, *p = userdata;
1080
1081 r = sd_bus_message_read_basic(m, type, &u);
1082 if (r < 0)
1083 return r;
1084
1085 *p = u;
1086 return 0;
1087 }
1088
1089 case SD_BUS_TYPE_INT64:
1090 case SD_BUS_TYPE_UINT64: {
1091 uint64_t t, *p = userdata;
1092
1093 r = sd_bus_message_read_basic(m, type, &t);
1094 if (r < 0)
1095 return r;
1096
1097 *p = t;
1098 return 0;
1099 }
1100
1101 case SD_BUS_TYPE_DOUBLE: {
1102 double d, *p = userdata;
1103
1104 r = sd_bus_message_read_basic(m, type, &d);
1105 if (r < 0)
1106 return r;
1107
1108 *p = d;
1109 return 0;
1110 }}
1111
1112 return -EOPNOTSUPP;
1113 }
1114
1115 int bus_message_map_all_properties(
1116 sd_bus_message *m,
1117 const struct bus_properties_map *map,
1118 sd_bus_error *error,
1119 void *userdata) {
1120
1121 int r;
1122
1123 assert(m);
1124 assert(map);
1125
1126 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
1127 if (r < 0)
1128 return r;
1129
1130 while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
1131 const struct bus_properties_map *prop;
1132 const char *member;
1133 const char *contents;
1134 void *v;
1135 unsigned i;
1136
1137 r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member);
1138 if (r < 0)
1139 return r;
1140
1141 for (i = 0, prop = NULL; map[i].member; i++)
1142 if (streq(map[i].member, member)) {
1143 prop = &map[i];
1144 break;
1145 }
1146
1147 if (prop) {
1148 r = sd_bus_message_peek_type(m, NULL, &contents);
1149 if (r < 0)
1150 return r;
1151
1152 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
1153 if (r < 0)
1154 return r;
1155
1156 v = (uint8_t *)userdata + prop->offset;
1157 if (map[i].set)
1158 r = prop->set(sd_bus_message_get_bus(m), member, m, error, v);
1159 else
1160 r = map_basic(sd_bus_message_get_bus(m), member, m, error, v);
1161 if (r < 0)
1162 return r;
1163
1164 r = sd_bus_message_exit_container(m);
1165 if (r < 0)
1166 return r;
1167 } else {
1168 r = sd_bus_message_skip(m, "v");
1169 if (r < 0)
1170 return r;
1171 }
1172
1173 r = sd_bus_message_exit_container(m);
1174 if (r < 0)
1175 return r;
1176 }
1177 if (r < 0)
1178 return r;
1179
1180 return sd_bus_message_exit_container(m);
1181 }
1182
1183 int bus_message_map_properties_changed(
1184 sd_bus_message *m,
1185 const struct bus_properties_map *map,
1186 sd_bus_error *error,
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, error, 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 sd_bus_error *error,
1226 void *userdata) {
1227
1228 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = 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, error, 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 }