Debian Codegraph @

src/libsystemd/sd-bus/bus-control.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus.h"
4
5 #include "alloc-util.h"
6 #include "bus-control.h"
7 #include "bus-error.h"
8 #include "bus-internal.h"
9 #include "bus-message.h"
10 #include "fd-util.h"
11 #include "log.h"
12 #include "pidref.h"
13 #include "process-util.h"
14 #include "string-util.h"
15 #include "strv.h"
16 #include "user-util.h"
17
18 _public_ int sd_bus_get_unique_name(sd_bus *bus, const char **unique) {
19 int r;
20
21 assert_return(bus, -EINVAL);
22 assert_return(bus = bus_resolve(bus), -ENOPKG);
23 assert_return(unique, -EINVAL);
24 assert_return(!bus_origin_changed(bus), -ECHILD);
25
26 if (!bus->bus_client)
27 return -EINVAL;
28
29 r = bus_ensure_running(bus);
30 if (r < 0)
31 return r;
32
33 *unique = bus->unique_name;
34 return 0;
35 }
36
37 static int validate_request_name_parameters(
38 sd_bus *bus,
39 const char *name,
40 uint64_t flags,
41 uint32_t *ret_param) {
42
43 uint32_t param = 0;
44
45 assert(bus);
46 assert(name);
47 assert(ret_param);
48
49 assert_return(!(flags & ~(SD_BUS_NAME_ALLOW_REPLACEMENT|SD_BUS_NAME_REPLACE_EXISTING|SD_BUS_NAME_QUEUE)), -EINVAL);
50 assert_return(service_name_is_valid(name), -EINVAL);
51 assert_return(name[0] != ':', -EINVAL);
52
53 if (!bus->bus_client)
54 return -EINVAL;
55
56 /* Don't allow requesting the special driver and local names */
57 if (STR_IN_SET(name, "org.freedesktop.DBus", "org.freedesktop.DBus.Local"))
58 return -EINVAL;
59
60 if (!BUS_IS_OPEN(bus->state))
61 return -ENOTCONN;
62
63 if (flags & SD_BUS_NAME_ALLOW_REPLACEMENT)
64 param |= BUS_NAME_ALLOW_REPLACEMENT;
65 if (flags & SD_BUS_NAME_REPLACE_EXISTING)
66 param |= BUS_NAME_REPLACE_EXISTING;
67 if (!(flags & SD_BUS_NAME_QUEUE))
68 param |= BUS_NAME_DO_NOT_QUEUE;
69
70 *ret_param = param;
71
72 return 0;
73 }
74
75 _public_ int sd_bus_request_name(
76 sd_bus *bus,
77 const char *name,
78 uint64_t flags) {
79
80 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
81 uint32_t ret, param = 0;
82 int r;
83
84 assert_return(bus, -EINVAL);
85 assert_return(bus = bus_resolve(bus), -ENOPKG);
86 assert_return(name, -EINVAL);
87 assert_return(!bus_origin_changed(bus), -ECHILD);
88
89 r = validate_request_name_parameters(bus, name, flags, &param);
90 if (r < 0)
91 return r;
92
93 r = sd_bus_call_method(
94 bus,
95 "org.freedesktop.DBus",
96 "/org/freedesktop/DBus",
97 "org.freedesktop.DBus",
98 "RequestName",
99 NULL,
100 &reply,
101 "su",
102 name,
103 param);
104 if (r < 0)
105 return r;
106
107 r = sd_bus_message_read(reply, "u", &ret);
108 if (r < 0)
109 return r;
110
111 switch (ret) {
112
113 case BUS_NAME_ALREADY_OWNER:
114 return -EALREADY;
115
116 case BUS_NAME_EXISTS:
117 return -EEXIST;
118
119 case BUS_NAME_IN_QUEUE:
120 return 0;
121
122 case BUS_NAME_PRIMARY_OWNER:
123 return 1;
124 }
125
126 return -EIO;
127 }
128
129 static int default_request_name_handler(
130 sd_bus_message *m,
131 void *userdata,
132 sd_bus_error *ret_error) {
133
134 uint32_t ret;
135 int r;
136
137 assert(m);
138
139 if (sd_bus_message_is_method_error(m, NULL)) {
140 const sd_bus_error *e = ASSERT_PTR(sd_bus_message_get_error(m));
141 r = sd_bus_error_get_errno(e);
142
143 log_debug_errno(r,
144 "Unable to request name, failing connection: %s",
145 bus_error_message(e, r));
146
147 bus_enter_closing(sd_bus_message_get_bus(m), -r);
148 return 1;
149 }
150
151 r = sd_bus_message_read(m, "u", &ret);
152 if (r < 0)
153 return r;
154
155 switch (ret) {
156
157 case BUS_NAME_ALREADY_OWNER:
158 log_debug("Already owner of requested service name, ignoring.");
159 return 1;
160
161 case BUS_NAME_IN_QUEUE:
162 log_debug("In queue for requested service name.");
163 return 1;
164
165 case BUS_NAME_PRIMARY_OWNER:
166 log_debug("Successfully acquired requested service name.");
167 return 1;
168
169 case BUS_NAME_EXISTS:
170 log_debug("Requested service name already owned, failing connection.");
171 bus_enter_closing(sd_bus_message_get_bus(m), -EEXIST);
172 return 1;
173 }
174
175 log_debug("Unexpected response from RequestName(), failing connection.");
176 bus_enter_closing(sd_bus_message_get_bus(m), -EPROTO);
177 return 1;
178 }
179
180 _public_ int sd_bus_request_name_async(
181 sd_bus *bus,
182 sd_bus_slot **ret_slot,
183 const char *name,
184 uint64_t flags,
185 sd_bus_message_handler_t callback,
186 void *userdata) {
187
188 uint32_t param = 0;
189 int r;
190
191 assert_return(bus, -EINVAL);
192 assert_return(bus = bus_resolve(bus), -ENOPKG);
193 assert_return(name, -EINVAL);
194 assert_return(!bus_origin_changed(bus), -ECHILD);
195
196 r = validate_request_name_parameters(bus, name, flags, &param);
197 if (r < 0)
198 return r;
199
200 return sd_bus_call_method_async(
201 bus,
202 ret_slot,
203 "org.freedesktop.DBus",
204 "/org/freedesktop/DBus",
205 "org.freedesktop.DBus",
206 "RequestName",
207 callback ?: default_request_name_handler,
208 userdata,
209 "su",
210 name,
211 param);
212 }
213
214 static int validate_release_name_parameters(
215 sd_bus *bus,
216 const char *name) {
217
218 assert(bus);
219 assert(name);
220
221 assert_return(service_name_is_valid(name), -EINVAL);
222 assert_return(name[0] != ':', -EINVAL);
223
224 if (!bus->bus_client)
225 return -EINVAL;
226
227 /* Don't allow releasing the special driver and local names */
228 if (STR_IN_SET(name, "org.freedesktop.DBus", "org.freedesktop.DBus.Local"))
229 return -EINVAL;
230
231 if (!BUS_IS_OPEN(bus->state))
232 return -ENOTCONN;
233
234 return 0;
235 }
236
237 _public_ int sd_bus_release_name(
238 sd_bus *bus,
239 const char *name) {
240
241 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
242 uint32_t ret;
243 int r;
244
245 assert_return(bus, -EINVAL);
246 assert_return(bus = bus_resolve(bus), -ENOPKG);
247 assert_return(name, -EINVAL);
248 assert_return(!bus_origin_changed(bus), -ECHILD);
249
250 r = validate_release_name_parameters(bus, name);
251 if (r < 0)
252 return r;
253
254 r = sd_bus_call_method(
255 bus,
256 "org.freedesktop.DBus",
257 "/org/freedesktop/DBus",
258 "org.freedesktop.DBus",
259 "ReleaseName",
260 NULL,
261 &reply,
262 "s",
263 name);
264 if (r < 0)
265 return r;
266
267 r = sd_bus_message_read(reply, "u", &ret);
268 if (r < 0)
269 return r;
270
271 switch (ret) {
272
273 case BUS_NAME_NON_EXISTENT:
274 return -ESRCH;
275
276 case BUS_NAME_NOT_OWNER:
277 return -EADDRINUSE;
278
279 case BUS_NAME_RELEASED:
280 return 0;
281 }
282
283 return -EIO;
284 }
285
286 static int default_release_name_handler(
287 sd_bus_message *m,
288 void *userdata,
289 sd_bus_error *ret_error) {
290
291 uint32_t ret;
292 int r;
293
294 assert(m);
295
296 if (sd_bus_message_is_method_error(m, NULL)) {
297 const sd_bus_error *e = ASSERT_PTR(sd_bus_message_get_error(m));
298 r = sd_bus_error_get_errno(e);
299
300 log_debug_errno(r, "Unable to release name, failing connection: %s", bus_error_message(e, r));
301
302 bus_enter_closing(sd_bus_message_get_bus(m), -r);
303 return 1;
304 }
305
306 r = sd_bus_message_read(m, "u", &ret);
307 if (r < 0)
308 return r;
309
310 switch (ret) {
311
312 case BUS_NAME_NON_EXISTENT:
313 log_debug("Name asked to release is not taken currently, ignoring.");
314 return 1;
315
316 case BUS_NAME_NOT_OWNER:
317 log_debug("Name asked to release is owned by somebody else, ignoring.");
318 return 1;
319
320 case BUS_NAME_RELEASED:
321 log_debug("Name successfully released.");
322 return 1;
323 }
324
325 log_debug("Unexpected response from ReleaseName(), failing connection.");
326 bus_enter_closing(sd_bus_message_get_bus(m), -EPROTO);
327 return 1;
328 }
329
330 _public_ int sd_bus_release_name_async(
331 sd_bus *bus,
332 sd_bus_slot **ret_slot,
333 const char *name,
334 sd_bus_message_handler_t callback,
335 void *userdata) {
336
337 int r;
338
339 assert_return(bus, -EINVAL);
340 assert_return(bus = bus_resolve(bus), -ENOPKG);
341 assert_return(name, -EINVAL);
342 assert_return(!bus_origin_changed(bus), -ECHILD);
343
344 r = validate_release_name_parameters(bus, name);
345 if (r < 0)
346 return r;
347
348 return sd_bus_call_method_async(
349 bus,
350 ret_slot,
351 "org.freedesktop.DBus",
352 "/org/freedesktop/DBus",
353 "org.freedesktop.DBus",
354 "ReleaseName",
355 callback ?: default_release_name_handler,
356 userdata,
357 "s",
358 name);
359 }
360
361 _public_ int sd_bus_list_names(sd_bus *bus, char ***ret_acquired, char ***ret_activatable) {
362 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
363 _cleanup_strv_free_ char **x = NULL, **y = NULL;
364 int r;
365
366 assert_return(bus, -EINVAL);
367 assert_return(bus = bus_resolve(bus), -ENOPKG);
368 assert_return(ret_acquired || ret_activatable, -EINVAL);
369 assert_return(!bus_origin_changed(bus), -ECHILD);
370
371 if (!bus->bus_client)
372 return -EINVAL;
373
374 if (!BUS_IS_OPEN(bus->state))
375 return -ENOTCONN;
376
377 if (ret_acquired) {
378 r = sd_bus_call_method(
379 bus,
380 "org.freedesktop.DBus",
381 "/org/freedesktop/DBus",
382 "org.freedesktop.DBus",
383 "ListNames",
384 NULL,
385 &reply,
386 NULL);
387 if (r < 0)
388 return r;
389
390 r = sd_bus_message_read_strv(reply, &x);
391 if (r < 0)
392 return r;
393
394 reply = sd_bus_message_unref(reply);
395 }
396
397 if (ret_activatable) {
398 r = sd_bus_call_method(
399 bus,
400 "org.freedesktop.DBus",
401 "/org/freedesktop/DBus",
402 "org.freedesktop.DBus",
403 "ListActivatableNames",
404 NULL,
405 &reply,
406 NULL);
407 if (r < 0)
408 return r;
409
410 r = sd_bus_message_read_strv(reply, &y);
411 if (r < 0)
412 return r;
413
414 *ret_activatable = TAKE_PTR(y);
415 }
416
417 if (ret_acquired)
418 *ret_acquired = TAKE_PTR(x);
419
420 return 0;
421 }
422
423 _public_ int sd_bus_get_name_creds(
424 sd_bus *bus,
425 const char *name,
426 uint64_t mask,
427 sd_bus_creds **ret) {
428
429 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply_unique = NULL, *reply = NULL;
430 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *c = NULL;
431 const char *unique;
432 int r;
433
434 assert_return(bus, -EINVAL);
435 assert_return(bus = bus_resolve(bus), -ENOPKG);
436 assert_return(name, -EINVAL);
437 assert_return((mask & ~SD_BUS_CREDS_AUGMENT) <= _SD_BUS_CREDS_ALL, -EOPNOTSUPP);
438 assert_return(mask == 0 || ret, -EINVAL);
439 assert_return(!bus_origin_changed(bus), -ECHILD);
440 assert_return(service_name_is_valid(name), -EINVAL);
441
442 if (!bus->bus_client)
443 return -EINVAL;
444
445 /* Turn off augmenting if this isn't a local connection. If the connection is not local, then /proc is not
446 * going to match. */
447 if (!bus->is_local)
448 mask &= ~SD_BUS_CREDS_AUGMENT;
449
450 if (streq(name, "org.freedesktop.DBus.Local"))
451 return -EINVAL;
452
453 if (streq(name, "org.freedesktop.DBus"))
454 return sd_bus_get_owner_creds(bus, mask, ret);
455
456 if (!BUS_IS_OPEN(bus->state))
457 return -ENOTCONN;
458
459 /* If the name is unique anyway, we can use it directly */
460 unique = name[0] == ':' ? name : NULL;
461
462 /* Only query the owner if the caller wants to know it and the name is not unique anyway, or if the caller just
463 * wants to check whether a name exists */
464 if ((FLAGS_SET(mask, SD_BUS_CREDS_UNIQUE_NAME) && !unique) || mask == 0) {
465 r = sd_bus_call_method(
466 bus,
467 "org.freedesktop.DBus",
468 "/org/freedesktop/DBus",
469 "org.freedesktop.DBus",
470 "GetNameOwner",
471 NULL,
472 &reply_unique,
473 "s",
474 name);
475 if (r < 0)
476 return r;
477
478 r = sd_bus_message_read(reply_unique, "s", &unique);
479 if (r < 0)
480 return r;
481 }
482
483 if (mask != 0) {
484 bool need_pid, need_uid, need_gids, need_selinux, need_separate_calls, need_pidfd, need_augment;
485 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
486 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
487
488 c = bus_creds_new();
489 if (!c)
490 return -ENOMEM;
491
492 if ((mask & SD_BUS_CREDS_UNIQUE_NAME) && unique) {
493 c->unique_name = strdup(unique);
494 if (!c->unique_name)
495 return -ENOMEM;
496
497 c->mask |= SD_BUS_CREDS_UNIQUE_NAME;
498 }
499
500 need_augment =
501 (mask & SD_BUS_CREDS_AUGMENT) &&
502 (mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
503 SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID|
504 SD_BUS_CREDS_SUPPLEMENTARY_GIDS|
505 SD_BUS_CREDS_COMM|SD_BUS_CREDS_EXE|SD_BUS_CREDS_CMDLINE|
506 SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID|
507 SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS|
508 SD_BUS_CREDS_SELINUX_CONTEXT|
509 SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID|
510 SD_BUS_CREDS_PIDFD));
511
512 need_pid = (mask & SD_BUS_CREDS_PID) || need_augment;
513 need_uid = mask & SD_BUS_CREDS_EUID;
514 need_gids = mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS;
515 need_selinux = mask & SD_BUS_CREDS_SELINUX_CONTEXT;
516 need_pidfd = (mask & SD_BUS_CREDS_PIDFD) || need_augment;
517
518 if (need_pid + need_uid + need_selinux + need_pidfd + need_gids > 1) {
519
520 /* If we need more than one of the credentials, then use GetConnectionCredentials() */
521
522 r = sd_bus_call_method(
523 bus,
524 "org.freedesktop.DBus",
525 "/org/freedesktop/DBus",
526 "org.freedesktop.DBus",
527 "GetConnectionCredentials",
528 &error,
529 &reply,
530 "s",
531 unique ?: name);
532
533 if (r < 0) {
534
535 if (!sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD))
536 return r;
537
538 /* If we got an unknown method error, fall back to the individual calls... */
539 need_separate_calls = true;
540 sd_bus_error_free(&error);
541
542 } else {
543 need_separate_calls = false;
544
545 r = sd_bus_message_enter_container(reply, 'a', "{sv}");
546 if (r < 0)
547 return r;
548
549 for (;;) {
550 const char *m;
551
552 r = sd_bus_message_enter_container(reply, 'e', "sv");
553 if (r < 0)
554 return r;
555 if (r == 0)
556 break;
557
558 r = sd_bus_message_read(reply, "s", &m);
559 if (r < 0)
560 return r;
561
562 if (need_uid && streq(m, "UnixUserID")) {
563 uint32_t u;
564
565 r = sd_bus_message_read(reply, "v", "u", &u);
566 if (r < 0)
567 return r;
568
569 c->euid = u;
570 c->mask |= SD_BUS_CREDS_EUID;
571
572 } else if (need_pid && streq(m, "ProcessID")) {
573 uint32_t p;
574
575 r = sd_bus_message_read(reply, "v", "u", &p);
576 if (r < 0)
577 return r;
578
579 if (!pidref_is_set(&pidref))
580 pidref = PIDREF_MAKE_FROM_PID(p);
581
582 if (mask & SD_BUS_CREDS_PID) {
583 c->pid = p;
584 c->mask |= SD_BUS_CREDS_PID;
585 }
586
587 } else if (need_selinux && streq(m, "LinuxSecurityLabel")) {
588 const void *p = NULL;
589 size_t sz = 0;
590
591 r = sd_bus_message_enter_container(reply, 'v', "ay");
592 if (r < 0)
593 return r;
594
595 r = sd_bus_message_read_array(reply, 'y', &p, &sz);
596 if (r < 0)
597 return r;
598
599 r = free_and_strndup(&c->label, p, sz);
600 if (r < 0)
601 return r;
602
603 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
604
605 r = sd_bus_message_exit_container(reply);
606 if (r < 0)
607 return r;
608 } else if (need_pidfd && streq(m, "ProcessFD")) {
609 int fd;
610
611 r = sd_bus_message_read(reply, "v", "h", &fd);
612 if (r < 0)
613 return r;
614
615 pidref_done(&pidref);
616 r = pidref_set_pidfd(&pidref, fd);
617 if (r < 0)
618 return r;
619
620 if (mask & SD_BUS_CREDS_PIDFD) {
621 fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
622 if (fd < 0)
623 return -errno;
624
625 close_and_replace(c->pidfd, fd);
626 c->mask |= SD_BUS_CREDS_PIDFD;
627 }
628 } else if (need_gids && streq(m, "UnixGroupIDs")) {
629
630 /* Note that D-Bus actually only gives us a combined list of
631 * primary gid and supplementary gids. And we don't know
632 * which one the primary one is. We'll take the whole shebang
633 * hence and use it as the supplementary group list, and not
634 * initialize the primary gid field. This is slightly
635 * incorrect of course, but only slightly, as in effect if
636 * the primary gid is also listed in the supplementary gid
637 * it has zero effect. */
638
639 r = sd_bus_message_enter_container(reply, 'v', "au");
640 if (r < 0)
641 return r;
642
643 r = sd_bus_message_enter_container(reply, 'a', "u");
644 if (r < 0)
645 return r;
646
647 for (;;) {
648 uint32_t u;
649
650 r = sd_bus_message_read(reply, "u", &u);
651 if (r < 0)
652 return r;
653 if (r == 0)
654 break;
655
656 if (!GREEDY_REALLOC(c->supplementary_gids, c->n_supplementary_gids+1))
657 return -ENOMEM;
658
659 c->supplementary_gids[c->n_supplementary_gids++] = (gid_t) u;
660 }
661
662 r = sd_bus_message_exit_container(reply);
663 if (r < 0)
664 return r;
665
666 r = sd_bus_message_exit_container(reply);
667 if (r < 0)
668 return r;
669
670 c->mask |= SD_BUS_CREDS_SUPPLEMENTARY_GIDS;
671 } else {
672 r = sd_bus_message_skip(reply, "v");
673 if (r < 0)
674 return r;
675 }
676
677 r = sd_bus_message_exit_container(reply);
678 if (r < 0)
679 return r;
680 }
681
682 r = sd_bus_message_exit_container(reply);
683 if (r < 0)
684 return r;
685
686 if (need_pid && !pidref_is_set(&pidref))
687 return -EPROTO;
688 }
689
690 } else /* When we only need a single field, then let's use separate calls */
691 need_separate_calls = true;
692
693 if (need_separate_calls) {
694 if (need_pid) {
695 uint32_t u;
696
697 r = sd_bus_call_method(
698 bus,
699 "org.freedesktop.DBus",
700 "/org/freedesktop/DBus",
701 "org.freedesktop.DBus",
702 "GetConnectionUnixProcessID",
703 NULL,
704 &reply,
705 "s",
706 unique ?: name);
707 if (r < 0)
708 return r;
709
710 r = sd_bus_message_read(reply, "u", &u);
711 if (r < 0)
712 return r;
713
714 if (!pidref_is_set(&pidref))
715 pidref = PIDREF_MAKE_FROM_PID(u);
716
717 if (mask & SD_BUS_CREDS_PID) {
718 c->pid = u;
719 c->mask |= SD_BUS_CREDS_PID;
720 }
721
722 reply = sd_bus_message_unref(reply);
723 }
724
725 if (need_uid) {
726 uint32_t u;
727
728 r = sd_bus_call_method(
729 bus,
730 "org.freedesktop.DBus",
731 "/org/freedesktop/DBus",
732 "org.freedesktop.DBus",
733 "GetConnectionUnixUser",
734 NULL,
735 &reply,
736 "s",
737 unique ?: name);
738 if (r < 0)
739 return r;
740
741 r = sd_bus_message_read(reply, "u", &u);
742 if (r < 0)
743 return r;
744
745 c->euid = u;
746 c->mask |= SD_BUS_CREDS_EUID;
747
748 reply = sd_bus_message_unref(reply);
749 }
750
751 if (need_selinux) {
752 const void *p = NULL;
753 size_t sz = 0;
754
755 r = sd_bus_call_method(
756 bus,
757 "org.freedesktop.DBus",
758 "/org/freedesktop/DBus",
759 "org.freedesktop.DBus",
760 "GetConnectionSELinuxSecurityContext",
761 &error,
762 &reply,
763 "s",
764 unique ?: name);
765 if (r < 0) {
766 if (!sd_bus_error_has_name(&error, SD_BUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN))
767 return r;
768
769 /* no data is fine */
770 } else {
771 r = sd_bus_message_read_array(reply, 'y', &p, &sz);
772 if (r < 0)
773 return r;
774
775 c->label = memdup_suffix0(p, sz);
776 if (!c->label)
777 return -ENOMEM;
778
779 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
780 }
781 }
782 }
783
784 if (pidref_is_set(&pidref)) {
785 r = bus_creds_add_more(c, mask, &pidref, 0);
786 if (r < 0 && r != -ESRCH) /* Return the error, but ignore ESRCH which just means the process is already gone */
787 return r;
788 }
789 }
790
791 if (ret)
792 *ret = TAKE_PTR(c);
793
794 return 0;
795 }
796
797 static int parse_sockaddr_string(const char *t, char **ret_comm, char **ret_description) {
798 _cleanup_free_ char *comm = NULL, *description = NULL;
799 const char *e, *sl;
800
801 assert(t);
802 assert(ret_comm);
803 assert(ret_description);
804
805 e = strstrafter(t, "/bus/");
806 if (!e) {
807 log_debug("Didn't find /bus/ substring in peer socket address, ignoring.");
808 goto not_found;
809 }
810
811 sl = strchr(e, '/');
812 if (!sl) {
813 log_debug("Didn't find / substring after /bus/ in peer socket address, ignoring.");
814 goto not_found;
815 }
816
817 if (sl - e > 0) {
818 comm = strndup(e, sl - e);
819 if (!comm)
820 return -ENOMEM;
821 }
822
823 sl++;
824 if (!isempty(sl)) {
825 description = strdup(sl);
826 if (!description)
827 return -ENOMEM;
828 }
829
830 *ret_comm = TAKE_PTR(comm);
831 *ret_description = TAKE_PTR(description);
832 return 0;
833
834 not_found:
835 *ret_comm = *ret_description = NULL;
836 return 0;
837 }
838
839 _public_ int sd_bus_get_owner_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
840 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *c = NULL;
841 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
842 bool do_label, do_groups, do_sockaddr_peer, do_pidfd;
843 int r;
844
845 assert_return(bus, -EINVAL);
846 assert_return(bus = bus_resolve(bus), -ENOPKG);
847 assert_return((mask & ~SD_BUS_CREDS_AUGMENT) <= _SD_BUS_CREDS_ALL, -EOPNOTSUPP);
848 assert_return(ret, -EINVAL);
849 assert_return(!bus_origin_changed(bus), -ECHILD);
850
851 if (!BUS_IS_OPEN(bus->state))
852 return -ENOTCONN;
853
854 if (!bus->is_local)
855 mask &= ~SD_BUS_CREDS_AUGMENT;
856
857 do_label = bus->label && (mask & SD_BUS_CREDS_SELINUX_CONTEXT);
858 do_groups = bus->n_groups != SIZE_MAX && (mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS);
859 do_sockaddr_peer = bus->sockaddr_size_peer >= offsetof(struct sockaddr_un, sun_path) + 1 &&
860 bus->sockaddr_peer.sa.sa_family == AF_UNIX &&
861 bus->sockaddr_peer.un.sun_path[0] == 0;
862 do_pidfd = bus->pidfd >= 0 && (mask & SD_BUS_CREDS_PIDFD);
863
864 /* Avoid allocating anything if we have no chance of returning useful data */
865 if (!bus->ucred_valid && !do_label && !do_groups && !do_sockaddr_peer && !do_pidfd)
866 return -ENODATA;
867
868 c = bus_creds_new();
869 if (!c)
870 return -ENOMEM;
871
872 if (bus->ucred_valid) {
873 if (pid_is_valid(bus->ucred.pid)) {
874 c->pid = bus->ucred.pid;
875 c->mask |= SD_BUS_CREDS_PID & mask;
876
877 pidref = PIDREF_MAKE_FROM_PID(c->pid);
878 }
879
880 if (uid_is_valid(bus->ucred.uid)) {
881 c->euid = bus->ucred.uid;
882 c->mask |= SD_BUS_CREDS_EUID & mask;
883 }
884
885 if (gid_is_valid(bus->ucred.gid)) {
886 c->egid = bus->ucred.gid;
887 c->mask |= SD_BUS_CREDS_EGID & mask;
888 }
889 }
890
891 if (do_label) {
892 c->label = strdup(bus->label);
893 if (!c->label)
894 return -ENOMEM;
895
896 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
897 }
898
899 if (do_groups) {
900 c->supplementary_gids = newdup(gid_t, bus->groups, bus->n_groups);
901 if (!c->supplementary_gids)
902 return -ENOMEM;
903
904 c->n_supplementary_gids = bus->n_groups;
905
906 c->mask |= SD_BUS_CREDS_SUPPLEMENTARY_GIDS;
907 }
908
909 if (do_sockaddr_peer) {
910 _cleanup_free_ char *t = NULL;
911
912 assert(bus->sockaddr_size_peer >= offsetof(struct sockaddr_un, sun_path) + 1);
913 assert(bus->sockaddr_peer.sa.sa_family == AF_UNIX);
914 assert(bus->sockaddr_peer.un.sun_path[0] == 0);
915
916 /* So this is an abstract namespace socket, good. Now let's find the data we are interested in */
917 r = make_cstring(bus->sockaddr_peer.un.sun_path + 1,
918 bus->sockaddr_size_peer - offsetof(struct sockaddr_un, sun_path) - 1,
919 MAKE_CSTRING_ALLOW_TRAILING_NUL,
920 &t);
921 if (r == -ENOMEM)
922 return r;
923 if (r < 0)
924 log_debug_errno(r, "Can't extract string from peer socket address, ignoring: %m");
925 else {
926 r = parse_sockaddr_string(t, &c->comm, &c->description);
927 if (r < 0)
928 return r;
929
930 if (c->comm)
931 c->mask |= SD_BUS_CREDS_COMM & mask;
932
933 if (c->description)
934 c->mask |= SD_BUS_CREDS_DESCRIPTION & mask;
935 }
936 }
937
938 if (do_pidfd) {
939 c->pidfd = fcntl(bus->pidfd, F_DUPFD_CLOEXEC, 3);
940 if (c->pidfd < 0)
941 return -errno;
942
943 pidref_done(&pidref);
944 r = pidref_set_pidfd(&pidref, bus->pidfd);
945 if (r < 0)
946 return r;
947
948 c->mask |= SD_BUS_CREDS_PIDFD;
949 }
950
951 r = bus_creds_add_more(c, mask, &pidref, 0);
952 if (r < 0 && r != -ESRCH) /* If the process vanished, then don't complain, just return what we got */
953 return r;
954
955 *ret = TAKE_PTR(c);
956
957 return 0;
958 }
959
960 #define append_eavesdrop(bus, m) \
961 ((bus)->is_monitor \
962 ? (isempty(m) ? "eavesdrop='true'" : strjoina((m), ",eavesdrop='true'")) \
963 : (m))
964
965 int bus_add_match_internal(
966 sd_bus *bus,
967 const char *match,
968 uint64_t timeout_usec,
969 uint64_t *ret_counter) {
970
971 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
972 const char *e;
973 int r;
974
975 assert(bus);
976
977 if (!bus->bus_client)
978 return -EINVAL;
979
980 e = append_eavesdrop(bus, match);
981
982 r = sd_bus_message_new_method_call(
983 bus,
984 &m,
985 "org.freedesktop.DBus",
986 "/org/freedesktop/DBus",
987 "org.freedesktop.DBus",
988 "AddMatch");
989 if (r < 0)
990 return r;
991
992 r = sd_bus_message_append(m, "s", e);
993 if (r < 0)
994 return r;
995
996 r = sd_bus_call(
997 bus,
998 m,
999 timeout_usec,
1000 NULL,
1001 &reply);
1002 if (r < 0)
1003 return r;
1004
1005 /* If the caller asked for it, return the read counter of the reply */
1006 if (ret_counter)
1007 *ret_counter = reply->read_counter;
1008
1009 return r;
1010 }
1011
1012 int bus_add_match_internal_async(
1013 sd_bus *bus,
1014 sd_bus_slot **ret_slot,
1015 const char *match,
1016 sd_bus_message_handler_t callback,
1017 void *userdata,
1018 uint64_t timeout_usec) {
1019
1020 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1021 const char *e;
1022 int r;
1023
1024 assert(bus);
1025
1026 if (!bus->bus_client)
1027 return -EINVAL;
1028
1029 e = append_eavesdrop(bus, match);
1030
1031 r = sd_bus_message_new_method_call(
1032 bus,
1033 &m,
1034 "org.freedesktop.DBus",
1035 "/org/freedesktop/DBus",
1036 "org.freedesktop.DBus",
1037 "AddMatch");
1038 if (r < 0)
1039 return r;
1040
1041 r = sd_bus_message_append(m, "s", e);
1042 if (r < 0)
1043 return r;
1044
1045 return sd_bus_call_async(
1046 bus,
1047 ret_slot,
1048 m,
1049 callback,
1050 userdata,
1051 timeout_usec);
1052 }
1053
1054 int bus_remove_match_internal(
1055 sd_bus *bus,
1056 const char *match) {
1057
1058 const char *e;
1059
1060 assert(bus);
1061 assert(match);
1062
1063 if (!bus->bus_client)
1064 return -EINVAL;
1065
1066 e = append_eavesdrop(bus, match);
1067
1068 /* Fire and forget */
1069
1070 return sd_bus_call_method_async(
1071 bus,
1072 NULL,
1073 "org.freedesktop.DBus",
1074 "/org/freedesktop/DBus",
1075 "org.freedesktop.DBus",
1076 "RemoveMatch",
1077 NULL,
1078 NULL,
1079 "s",
1080 e);
1081 }
1082
1083 _public_ int sd_bus_get_name_machine_id(sd_bus *bus, const char *name, sd_id128_t *ret) {
1084 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL;
1085 const char *mid;
1086 int r;
1087
1088 assert_return(bus, -EINVAL);
1089 assert_return(bus = bus_resolve(bus), -ENOPKG);
1090 assert_return(name, -EINVAL);
1091 assert_return(ret, -EINVAL);
1092 assert_return(!bus_origin_changed(bus), -ECHILD);
1093 assert_return(service_name_is_valid(name), -EINVAL);
1094
1095 if (!bus->bus_client)
1096 return -EINVAL;
1097
1098 if (!BUS_IS_OPEN(bus->state))
1099 return -ENOTCONN;
1100
1101 if (streq_ptr(name, bus->unique_name))
1102 return sd_id128_get_machine(ret);
1103
1104 r = sd_bus_message_new_method_call(
1105 bus,
1106 &m,
1107 name,
1108 "/",
1109 "org.freedesktop.DBus.Peer",
1110 "GetMachineId");
1111 if (r < 0)
1112 return r;
1113
1114 r = sd_bus_message_set_auto_start(m, false);
1115 if (r < 0)
1116 return r;
1117
1118 r = sd_bus_call(bus, m, 0, NULL, &reply);
1119 if (r < 0)
1120 return r;
1121
1122 r = sd_bus_message_read(reply, "s", &mid);
1123 if (r < 0)
1124 return r;
1125
1126 return sd_id128_from_string(mid, ret);
1127 }