2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
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.
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.
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/>.
20 #if HAVE_VALGRIND_MEMCHECK_H
21 #include <valgrind/memcheck.h>
29 #include "alloc-util.h"
30 #include "bus-bloom.h"
31 #include "bus-control.h"
32 #include "bus-internal.h"
33 #include "bus-message.h"
35 #include "capability-util.h"
36 #include "stdio-util.h"
37 #include "string-util.h"
39 #include "user-util.h"
41 _public_ int sd_bus_get_unique_name(sd_bus *bus, const char **unique) {
44 assert_return(bus, -EINVAL);
45 assert_return(unique, -EINVAL);
46 assert_return(!bus_pid_changed(bus), -ECHILD);
51 r = bus_ensure_running(bus);
55 *unique = bus->unique_name;
59 static int bus_request_name_dbus1(sd_bus *bus, const char *name, uint64_t flags) {
60 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
61 uint32_t ret, param = 0;
67 if (flags & SD_BUS_NAME_ALLOW_REPLACEMENT)
68 param |= BUS_NAME_ALLOW_REPLACEMENT;
69 if (flags & SD_BUS_NAME_REPLACE_EXISTING)
70 param |= BUS_NAME_REPLACE_EXISTING;
71 if (!(flags & SD_BUS_NAME_QUEUE))
72 param |= BUS_NAME_DO_NOT_QUEUE;
74 r = sd_bus_call_method(
76 "org.freedesktop.DBus",
77 "/org/freedesktop/DBus",
78 "org.freedesktop.DBus",
88 r = sd_bus_message_read(reply, "u", &ret);
92 if (ret == BUS_NAME_ALREADY_OWNER)
94 else if (ret == BUS_NAME_EXISTS)
96 else if (ret == BUS_NAME_IN_QUEUE)
98 else if (ret == BUS_NAME_PRIMARY_OWNER)
104 _public_ int sd_bus_request_name(sd_bus *bus, const char *name, uint64_t flags) {
105 assert_return(bus, -EINVAL);
106 assert_return(name, -EINVAL);
107 assert_return(!bus_pid_changed(bus), -ECHILD);
108 assert_return(!(flags & ~(SD_BUS_NAME_ALLOW_REPLACEMENT|SD_BUS_NAME_REPLACE_EXISTING|SD_BUS_NAME_QUEUE)), -EINVAL);
109 assert_return(service_name_is_valid(name), -EINVAL);
110 assert_return(name[0] != ':', -EINVAL);
112 if (!bus->bus_client)
115 /* Don't allow requesting the special driver and local names */
116 if (STR_IN_SET(name, "org.freedesktop.DBus", "org.freedesktop.DBus.Local"))
119 if (!BUS_IS_OPEN(bus->state))
122 return bus_request_name_dbus1(bus, name, flags);
125 static int bus_release_name_dbus1(sd_bus *bus, const char *name) {
126 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
133 r = sd_bus_call_method(
135 "org.freedesktop.DBus",
136 "/org/freedesktop/DBus",
137 "org.freedesktop.DBus",
146 r = sd_bus_message_read(reply, "u", &ret);
149 if (ret == BUS_NAME_NON_EXISTENT)
151 if (ret == BUS_NAME_NOT_OWNER)
153 if (ret == BUS_NAME_RELEASED)
159 _public_ int sd_bus_release_name(sd_bus *bus, const char *name) {
160 assert_return(bus, -EINVAL);
161 assert_return(name, -EINVAL);
162 assert_return(!bus_pid_changed(bus), -ECHILD);
163 assert_return(service_name_is_valid(name), -EINVAL);
164 assert_return(name[0] != ':', -EINVAL);
166 if (!bus->bus_client)
169 /* Don't allow releasing the special driver and local names */
170 if (STR_IN_SET(name, "org.freedesktop.DBus", "org.freedesktop.DBus.Local"))
173 if (!BUS_IS_OPEN(bus->state))
176 return bus_release_name_dbus1(bus, name);
179 static int bus_list_names_dbus1(sd_bus *bus, char ***acquired, char ***activatable) {
180 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
181 _cleanup_strv_free_ char **x = NULL, **y = NULL;
185 r = sd_bus_call_method(
187 "org.freedesktop.DBus",
188 "/org/freedesktop/DBus",
189 "org.freedesktop.DBus",
197 r = sd_bus_message_read_strv(reply, &x);
201 reply = sd_bus_message_unref(reply);
205 r = sd_bus_call_method(
207 "org.freedesktop.DBus",
208 "/org/freedesktop/DBus",
209 "org.freedesktop.DBus",
210 "ListActivatableNames",
217 r = sd_bus_message_read_strv(reply, &y);
233 _public_ int sd_bus_list_names(sd_bus *bus, char ***acquired, char ***activatable) {
234 assert_return(bus, -EINVAL);
235 assert_return(acquired || activatable, -EINVAL);
236 assert_return(!bus_pid_changed(bus), -ECHILD);
238 if (!bus->bus_client)
241 if (!BUS_IS_OPEN(bus->state))
244 return bus_list_names_dbus1(bus, acquired, activatable);
247 static int bus_get_name_creds_dbus1(
251 sd_bus_creds **creds) {
253 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply_unique = NULL, *reply = NULL;
254 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *c = NULL;
255 const char *unique = NULL;
259 /* Only query the owner if the caller wants to know it or if
260 * the caller just wants to check whether a name exists */
261 if ((mask & SD_BUS_CREDS_UNIQUE_NAME) || mask == 0) {
262 r = sd_bus_call_method(
264 "org.freedesktop.DBus",
265 "/org/freedesktop/DBus",
266 "org.freedesktop.DBus",
275 r = sd_bus_message_read(reply_unique, "s", &unique);
281 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
282 bool need_pid, need_uid, need_selinux, need_separate_calls;
287 if ((mask & SD_BUS_CREDS_UNIQUE_NAME) && unique) {
288 c->unique_name = strdup(unique);
292 c->mask |= SD_BUS_CREDS_UNIQUE_NAME;
295 need_pid = (mask & SD_BUS_CREDS_PID) ||
296 ((mask & SD_BUS_CREDS_AUGMENT) &&
297 (mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
298 SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID|
299 SD_BUS_CREDS_SUPPLEMENTARY_GIDS|
300 SD_BUS_CREDS_COMM|SD_BUS_CREDS_EXE|SD_BUS_CREDS_CMDLINE|
301 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|
302 SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS|
303 SD_BUS_CREDS_SELINUX_CONTEXT|
304 SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID)));
305 need_uid = mask & SD_BUS_CREDS_EUID;
306 need_selinux = mask & SD_BUS_CREDS_SELINUX_CONTEXT;
308 if (need_pid + need_uid + need_selinux > 1) {
310 /* If we need more than one of the credentials, then use GetConnectionCredentials() */
312 r = sd_bus_call_method(
314 "org.freedesktop.DBus",
315 "/org/freedesktop/DBus",
316 "org.freedesktop.DBus",
317 "GetConnectionCredentials",
325 if (!sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD))
328 /* If we got an unknown method error, fall back to the invidual calls... */
329 need_separate_calls = true;
330 sd_bus_error_free(&error);
333 need_separate_calls = false;
335 r = sd_bus_message_enter_container(reply, 'a', "{sv}");
342 r = sd_bus_message_enter_container(reply, 'e', "sv");
348 r = sd_bus_message_read(reply, "s", &m);
352 if (need_uid && streq(m, "UnixUserID")) {
355 r = sd_bus_message_read(reply, "v", "u", &u);
360 c->mask |= SD_BUS_CREDS_EUID;
362 } else if (need_pid && streq(m, "ProcessID")) {
365 r = sd_bus_message_read(reply, "v", "u", &p);
370 if (mask & SD_BUS_CREDS_PID) {
372 c->mask |= SD_BUS_CREDS_PID;
375 } else if (need_selinux && streq(m, "LinuxSecurityLabel")) {
376 const void *p = NULL;
379 r = sd_bus_message_enter_container(reply, 'v', "ay");
383 r = sd_bus_message_read_array(reply, 'y', &p, &sz);
388 c->label = strndup(p, sz);
392 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
394 r = sd_bus_message_exit_container(reply);
398 r = sd_bus_message_skip(reply, "v");
403 r = sd_bus_message_exit_container(reply);
408 r = sd_bus_message_exit_container(reply);
412 if (need_pid && pid == 0)
416 } else /* When we only need a single field, then let's use separate calls */
417 need_separate_calls = true;
419 if (need_separate_calls) {
423 r = sd_bus_call_method(
425 "org.freedesktop.DBus",
426 "/org/freedesktop/DBus",
427 "org.freedesktop.DBus",
428 "GetConnectionUnixProcessID",
436 r = sd_bus_message_read(reply, "u", &u);
441 if (mask & SD_BUS_CREDS_PID) {
443 c->mask |= SD_BUS_CREDS_PID;
446 reply = sd_bus_message_unref(reply);
452 r = sd_bus_call_method(
454 "org.freedesktop.DBus",
455 "/org/freedesktop/DBus",
456 "org.freedesktop.DBus",
457 "GetConnectionUnixUser",
461 unique ? unique : name);
465 r = sd_bus_message_read(reply, "u", &u);
470 c->mask |= SD_BUS_CREDS_EUID;
472 reply = sd_bus_message_unref(reply);
476 const void *p = NULL;
479 r = sd_bus_call_method(
481 "org.freedesktop.DBus",
482 "/org/freedesktop/DBus",
483 "org.freedesktop.DBus",
484 "GetConnectionSELinuxSecurityContext",
488 unique ? unique : name);
490 if (!sd_bus_error_has_name(&error, "org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown"))
493 /* no data is fine */
495 r = sd_bus_message_read_array(reply, 'y', &p, &sz);
499 c->label = strndup(p, sz);
503 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
508 r = bus_creds_add_more(c, mask, pid, 0);
521 _public_ int sd_bus_get_name_creds(
525 sd_bus_creds **creds) {
527 assert_return(bus, -EINVAL);
528 assert_return(name, -EINVAL);
529 assert_return((mask & ~SD_BUS_CREDS_AUGMENT) <= _SD_BUS_CREDS_ALL, -EOPNOTSUPP);
530 assert_return(mask == 0 || creds, -EINVAL);
531 assert_return(!bus_pid_changed(bus), -ECHILD);
532 assert_return(service_name_is_valid(name), -EINVAL);
534 if (!bus->bus_client)
537 /* Turn off augmenting if this isn't a local connection. If the connection is not local, then /proc is not
540 mask &= ~SD_BUS_CREDS_AUGMENT;
542 if (streq(name, "org.freedesktop.DBus.Local"))
545 if (streq(name, "org.freedesktop.DBus"))
546 return sd_bus_get_owner_creds(bus, mask, creds);
548 if (!BUS_IS_OPEN(bus->state))
551 return bus_get_name_creds_dbus1(bus, name, mask, creds);
554 static int bus_get_owner_creds_dbus1(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
555 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *c = NULL;
562 do_label = bus->label && (mask & SD_BUS_CREDS_SELINUX_CONTEXT);
564 /* Avoid allocating anything if we have no chance of returning useful data */
565 if (!bus->ucred_valid && !do_label)
572 if (bus->ucred_valid) {
573 if (bus->ucred.pid > 0) {
574 pid = c->pid = bus->ucred.pid;
575 c->mask |= SD_BUS_CREDS_PID & mask;
578 if (bus->ucred.uid != UID_INVALID) {
579 c->euid = bus->ucred.uid;
580 c->mask |= SD_BUS_CREDS_EUID & mask;
583 if (bus->ucred.gid != GID_INVALID) {
584 c->egid = bus->ucred.gid;
585 c->mask |= SD_BUS_CREDS_EGID & mask;
590 c->label = strdup(bus->label);
594 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
597 r = bus_creds_add_more(c, mask, pid, 0);
606 _public_ int sd_bus_get_owner_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
607 assert_return(bus, -EINVAL);
608 assert_return((mask & ~SD_BUS_CREDS_AUGMENT) <= _SD_BUS_CREDS_ALL, -EOPNOTSUPP);
609 assert_return(ret, -EINVAL);
610 assert_return(!bus_pid_changed(bus), -ECHILD);
612 if (!BUS_IS_OPEN(bus->state))
616 mask &= ~SD_BUS_CREDS_AUGMENT;
618 return bus_get_owner_creds_dbus1(bus, mask, ret);
621 #define internal_match(bus, m) \
622 ((bus)->hello_flags & KDBUS_HELLO_MONITOR \
623 ? (isempty(m) ? "eavesdrop='true'" : strjoina((m), ",eavesdrop='true'")) \
626 static int bus_add_match_internal_dbus1(
635 e = internal_match(bus, match);
637 return sd_bus_call_method(
639 "org.freedesktop.DBus",
640 "/org/freedesktop/DBus",
641 "org.freedesktop.DBus",
649 int bus_add_match_internal(
652 struct bus_match_component *components,
653 unsigned n_components) {
657 if (!bus->bus_client)
660 return bus_add_match_internal_dbus1(bus, match);
663 static int bus_remove_match_internal_dbus1(
672 e = internal_match(bus, match);
674 return sd_bus_call_method(
676 "org.freedesktop.DBus",
677 "/org/freedesktop/DBus",
678 "org.freedesktop.DBus",
686 int bus_remove_match_internal(
692 if (!bus->bus_client)
695 return bus_remove_match_internal_dbus1(bus, match);
698 _public_ int sd_bus_get_name_machine_id(sd_bus *bus, const char *name, sd_id128_t *machine) {
699 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL;
703 assert_return(bus, -EINVAL);
704 assert_return(name, -EINVAL);
705 assert_return(machine, -EINVAL);
706 assert_return(!bus_pid_changed(bus), -ECHILD);
707 assert_return(service_name_is_valid(name), -EINVAL);
709 if (!bus->bus_client)
712 if (!BUS_IS_OPEN(bus->state))
715 if (streq_ptr(name, bus->unique_name))
716 return sd_id128_get_machine(machine);
718 r = sd_bus_message_new_method_call(
723 "org.freedesktop.DBus.Peer",
728 r = sd_bus_message_set_auto_start(m, false);
732 r = sd_bus_call(bus, m, 0, NULL, &reply);
736 r = sd_bus_message_read(reply, "s", &mid);
740 return sd_id128_from_string(mid, machine);