]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/bus-socket.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include <sys/inotify.h>
11 #include "sd-daemon.h"
13 #include "alloc-util.h"
14 #include "bus-internal.h"
15 #include "bus-message.h"
16 #include "bus-socket.h"
17 #include "errno-util.h"
21 #include "hexdecoct.h"
23 #include "iovec-util.h"
25 #include "memory-util.h"
26 #include "path-util.h"
27 #include "process-util.h"
28 #include "random-util.h"
29 #include "stdio-util.h"
30 #include "string-util.h"
31 #include "time-util.h"
32 #include "user-util.h"
35 #define SNDBUF_SIZE (8*1024*1024)
37 static void iovec_advance(struct iovec iov
[], unsigned *idx
, size_t size
) {
40 struct iovec
*i
= iov
+ *idx
;
42 if (i
->iov_len
> size
) {
43 i
->iov_base
= (uint8_t*) i
->iov_base
+ size
;
50 *i
= IOVEC_MAKE(NULL
, 0);
56 static int append_iovec(sd_bus_message
*m
, const void *p
, size_t sz
) {
61 m
->iovec
[m
->n_iovec
++] = IOVEC_MAKE((void*) p
, sz
);
66 static int bus_message_setup_iovec(sd_bus_message
*m
) {
67 BusMessageBodyPart
*part
;
79 n
= 1 + m
->n_body_parts
;
80 if (n
< ELEMENTSOF(m
->iovec_fixed
))
81 m
->iovec
= m
->iovec_fixed
;
83 m
->iovec
= new(struct iovec
, n
);
90 r
= append_iovec(m
, m
->header
, BUS_MESSAGE_BODY_BEGIN(m
));
94 MESSAGE_FOREACH_PART(part
, i
, m
) {
95 r
= bus_body_part_map(part
);
99 r
= append_iovec(m
, part
->data
, part
->size
);
104 assert(n
== m
->n_iovec
);
113 bool bus_socket_auth_needs_write(sd_bus
*b
) {
117 if (b
->auth_index
>= ELEMENTSOF(b
->auth_iovec
))
120 for (i
= b
->auth_index
; i
< ELEMENTSOF(b
->auth_iovec
); i
++) {
121 struct iovec
*j
= b
->auth_iovec
+ i
;
130 static int bus_socket_auth_verify_client(sd_bus
*b
) {
131 char *l
, *lines
[4] = {};
139 * We expect up to three response lines:
140 * "DATA\r\n" (optional)
141 * "OK <server-id>\r\n"
142 * "AGREE_UNIX_FD\r\n" (optional)
146 lines
[n
] = b
->rbuffer
;
147 for (i
= 0; i
< 3; ++i
) {
148 l
= memmem_safe(lines
[n
], b
->rbuffer_size
- (lines
[n
] - (char*) b
->rbuffer
), "\r\n", 2);
156 * If we sent a non-empty initial response, then we just expect an OK
157 * reply. We currently do this if, and only if, we picked ANONYMOUS.
158 * If we did not send an initial response, then we expect a DATA
159 * challenge, reply with our own DATA, and expect an OK reply. We do
161 * If FD negotiation was requested, we additionally expect
162 * an AGREE_UNIX_FD response in all cases.
164 if (n
< (b
->anonymous_auth
? 1U : 2U) + !!b
->accept_fd
)
165 return 0; /* wait for more data */
169 /* In case of EXTERNAL, verify the first response was DATA. */
170 if (!b
->anonymous_auth
) {
172 if (lines
[i
] - l
== 4 + 2) {
173 if (memcmp(l
, "DATA", 4))
175 } else if (lines
[i
] - l
== 3 + 32 + 2) {
177 * Old versions of the server-side implementation of
178 * `sd-bus` replied with "OK <id>" to "AUTH" requests
179 * from a client, even if the "AUTH" line did not
180 * contain inlined arguments. Therefore, we also accept
181 * "OK <id>" here, even though it is technically the
182 * wrong reply. We ignore the "<id>" parameter, though,
183 * since it has no real value.
185 if (memcmp(l
, "OK ", 3))
191 /* Now check the OK line. */
194 if (lines
[i
] - l
!= 3 + 32 + 2)
196 if (memcmp(l
, "OK ", 3))
199 b
->auth
= b
->anonymous_auth
? BUS_AUTH_ANONYMOUS
: BUS_AUTH_EXTERNAL
;
201 for (unsigned j
= 0; j
< 32; j
+= 2) {
204 x
= unhexchar(l
[3 + j
]);
205 y
= unhexchar(l
[3 + j
+ 1]);
210 peer
.bytes
[j
/2] = ((uint8_t) x
<< 4 | (uint8_t) y
);
213 if (!sd_id128_is_null(b
->server_id
) &&
214 !sd_id128_equal(b
->server_id
, peer
))
219 /* And possibly check the third line, too */
222 b
->can_fds
= memory_startswith(l
, lines
[i
] - l
, "AGREE_UNIX_FD");
227 b
->rbuffer_size
-= (lines
[i
] - (char*) b
->rbuffer
);
228 memmove(b
->rbuffer
, lines
[i
], b
->rbuffer_size
);
230 r
= bus_start_running(b
);
237 static bool line_equals(const char *s
, size_t m
, const char *line
) {
244 return memcmp(s
, line
, l
) == 0;
247 static bool line_begins(const char *s
, size_t m
, const char *word
) {
250 p
= memory_startswith(s
, m
, word
);
251 return p
&& (p
== (s
+ m
) || *p
== ' ');
254 static int verify_anonymous_token(sd_bus
*b
, const char *p
, size_t l
) {
255 _cleanup_free_
char *token
= NULL
;
259 if (!b
->anonymous_auth
)
271 r
= unhexmem_full(p
, l
, /* secure = */ false, (void**) &token
, &len
);
275 if (memchr(token
, 0, len
))
278 return !!utf8_is_valid(token
);
281 static int verify_external_token(sd_bus
*b
, const char *p
, size_t l
) {
282 _cleanup_free_
char *token
= NULL
;
287 /* We don't do any real authentication here. Instead, if
288 * the owner of this bus wanted authentication they should have
289 * checked SO_PEERCRED before even creating the bus object. */
291 if (!b
->anonymous_auth
&& !b
->ucred_valid
)
303 r
= unhexmem_full(p
, l
, /* secure = */ false, (void**) &token
, &len
);
307 if (memchr(token
, 0, len
))
310 r
= parse_uid(token
, &u
);
314 /* We ignore the passed value if anonymous authentication is
316 if (!b
->anonymous_auth
&& u
!= b
->ucred
.uid
)
322 static int bus_socket_auth_write(sd_bus
*b
, const char *t
) {
329 /* We only make use of the first iovec */
330 assert(IN_SET(b
->auth_index
, 0, 1));
333 p
= malloc(b
->auth_iovec
[0].iov_len
+ l
);
337 memcpy_safe(p
, b
->auth_iovec
[0].iov_base
, b
->auth_iovec
[0].iov_len
);
338 memcpy(p
+ b
->auth_iovec
[0].iov_len
, t
, l
);
340 b
->auth_iovec
[0].iov_base
= p
;
341 b
->auth_iovec
[0].iov_len
+= l
;
343 free_and_replace(b
->auth_buffer
, p
);
348 static int bus_socket_auth_write_ok(sd_bus
*b
) {
349 char t
[3 + 32 + 2 + 1];
353 xsprintf(t
, "OK " SD_ID128_FORMAT_STR
"\r\n", SD_ID128_FORMAT_VAL(b
->server_id
));
355 return bus_socket_auth_write(b
, t
);
358 static int bus_socket_auth_verify_server(sd_bus
*b
) {
362 bool processed
= false;
367 if (b
->rbuffer_size
< 1)
370 /* First char must be a NUL byte */
371 if (*(char*) b
->rbuffer
!= 0)
374 if (b
->rbuffer_size
< 3)
377 /* Begin with the first line */
378 if (b
->auth_rbegin
<= 0)
382 /* Check if line is complete */
383 line
= (char*) b
->rbuffer
+ b
->auth_rbegin
;
384 e
= memmem_safe(line
, b
->rbuffer_size
- b
->auth_rbegin
, "\r\n", 2);
390 if (line_begins(line
, l
, "AUTH ANONYMOUS")) {
392 r
= verify_anonymous_token(b
,
393 line
+ strlen("AUTH ANONYMOUS"),
394 l
- strlen("AUTH ANONYMOUS"));
398 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
400 b
->auth
= BUS_AUTH_ANONYMOUS
;
401 if (l
<= strlen("AUTH ANONYMOUS"))
402 r
= bus_socket_auth_write(b
, "DATA\r\n");
404 r
= bus_socket_auth_write_ok(b
);
407 } else if (line_begins(line
, l
, "AUTH EXTERNAL")) {
409 r
= verify_external_token(b
,
410 line
+ strlen("AUTH EXTERNAL"),
411 l
- strlen("AUTH EXTERNAL"));
415 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
417 b
->auth
= BUS_AUTH_EXTERNAL
;
418 if (l
<= strlen("AUTH EXTERNAL"))
419 r
= bus_socket_auth_write(b
, "DATA\r\n");
421 r
= bus_socket_auth_write_ok(b
);
424 } else if (line_begins(line
, l
, "AUTH"))
425 r
= bus_socket_auth_write(b
, "REJECTED EXTERNAL ANONYMOUS\r\n");
426 else if (line_equals(line
, l
, "CANCEL") ||
427 line_begins(line
, l
, "ERROR")) {
429 b
->auth
= _BUS_AUTH_INVALID
;
430 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
432 } else if (line_equals(line
, l
, "BEGIN")) {
434 if (b
->auth
== _BUS_AUTH_INVALID
)
435 r
= bus_socket_auth_write(b
, "ERROR\r\n");
437 /* We can't leave from the auth phase
438 * before we haven't written
439 * everything queued, so let's check
442 if (bus_socket_auth_needs_write(b
))
445 b
->rbuffer_size
-= (e
+ 2 - (char*) b
->rbuffer
);
446 memmove(b
->rbuffer
, e
+ 2, b
->rbuffer_size
);
447 return bus_start_running(b
);
450 } else if (line_begins(line
, l
, "DATA")) {
452 if (b
->auth
== _BUS_AUTH_INVALID
)
453 r
= bus_socket_auth_write(b
, "ERROR\r\n");
455 if (b
->auth
== BUS_AUTH_ANONYMOUS
)
456 r
= verify_anonymous_token(b
, line
+ 4, l
- 4);
458 r
= verify_external_token(b
, line
+ 4, l
- 4);
463 b
->auth
= _BUS_AUTH_INVALID
;
464 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
466 r
= bus_socket_auth_write_ok(b
);
468 } else if (line_equals(line
, l
, "NEGOTIATE_UNIX_FD")) {
469 if (b
->auth
== _BUS_AUTH_INVALID
|| !b
->accept_fd
)
470 r
= bus_socket_auth_write(b
, "ERROR\r\n");
473 r
= bus_socket_auth_write(b
, "AGREE_UNIX_FD\r\n");
476 r
= bus_socket_auth_write(b
, "ERROR\r\n");
481 b
->auth_rbegin
= e
+ 2 - (char*) b
->rbuffer
;
487 static int bus_socket_auth_verify(sd_bus
*b
) {
491 return bus_socket_auth_verify_server(b
);
493 return bus_socket_auth_verify_client(b
);
496 static int bus_socket_write_auth(sd_bus
*b
) {
500 assert(b
->state
== BUS_AUTHENTICATING
);
502 if (!bus_socket_auth_needs_write(b
))
505 if (b
->prefer_writev
)
506 k
= writev(b
->output_fd
, b
->auth_iovec
+ b
->auth_index
, ELEMENTSOF(b
->auth_iovec
) - b
->auth_index
);
508 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred
))) control
= {};
511 .msg_iov
= b
->auth_iovec
+ b
->auth_index
,
512 .msg_iovlen
= ELEMENTSOF(b
->auth_iovec
) - b
->auth_index
,
515 if (uid_is_valid(b
->connect_as_uid
) || gid_is_valid(b
->connect_as_gid
)) {
517 /* If we shall connect under some specific UID/GID, then synthesize an
518 * SCM_CREDENTIALS record accordingly. After all we want to adopt this UID/GID both
519 * for SO_PEERCRED (where we have to fork()) and SCM_CREDENTIALS (where we can just
520 * fake it via sendmsg()) */
522 struct ucred ucred
= {
523 .pid
= getpid_cached(),
524 .uid
= uid_is_valid(b
->connect_as_uid
) ? b
->connect_as_uid
: getuid(),
525 .gid
= gid_is_valid(b
->connect_as_gid
) ? b
->connect_as_gid
: getgid(),
528 mh
.msg_control
= &control
;
529 mh
.msg_controllen
= sizeof(control
);
530 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(&mh
);
531 *cmsg
= (struct cmsghdr
) {
532 .cmsg_level
= SOL_SOCKET
,
533 .cmsg_type
= SCM_CREDENTIALS
,
534 .cmsg_len
= CMSG_LEN(sizeof(struct ucred
)),
537 memcpy(CMSG_DATA(cmsg
), &ucred
, sizeof(struct ucred
));
540 k
= sendmsg(b
->output_fd
, &mh
, MSG_DONTWAIT
|MSG_NOSIGNAL
);
541 if (k
< 0 && errno
== ENOTSOCK
) {
542 b
->prefer_writev
= true;
543 k
= writev(b
->output_fd
, b
->auth_iovec
+ b
->auth_index
, ELEMENTSOF(b
->auth_iovec
) - b
->auth_index
);
548 return ERRNO_IS_TRANSIENT(errno
) ? 0 : -errno
;
550 iovec_advance(b
->auth_iovec
, &b
->auth_index
, (size_t) k
);
552 /* Now crank the state machine since we might be able to make progress after writing. For example,
553 * the server only processes "BEGIN" when the write buffer is empty.
555 return bus_socket_auth_verify(b
);
558 static int bus_process_cmsg(sd_bus
*bus
, struct msghdr
*mh
, bool allow_fds
) {
559 _cleanup_close_
int pidfd
= -EBADF
;
560 const int *fds
= NULL
;
566 CLEANUP_ARRAY(fds
, n_fds
, close_many
);
568 struct cmsghdr
*cmsg
;
569 CMSG_FOREACH(cmsg
, mh
)
570 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
572 fds
= CMSG_TYPED_DATA(cmsg
, int);
573 n_fds
= (cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int);
575 } else if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_PIDFD
) {
576 log_debug("Got unexpected auxiliary pidfd, ignoring.");
578 pidfd
= *CMSG_TYPED_DATA(cmsg
, int);
581 log_debug("Got unexpected auxiliary data with level=%d and type=%d, ignoring.",
582 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
586 /* Whut? We received fds during the auth protocol or so? Somebody is playing games
587 * with us. Close them all, and fail */
593 if (!GREEDY_REALLOC(bus
->fds
, bus
->n_fds
+ n_fds
))
596 FOREACH_ARRAY(i
, fds
, n_fds
)
597 bus
->fds
[bus
->n_fds
++] = fd_move_above_stdio(*i
);
604 static int bus_socket_read_auth(sd_bus
*b
) {
606 struct iovec iov
= {};
611 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int) * BUS_FDS_MAX
)) control
;
612 bool handle_cmsg
= false;
615 assert(b
->state
== BUS_AUTHENTICATING
);
617 r
= bus_socket_auth_verify(b
);
621 n
= MAX(256u, b
->rbuffer_size
* 2);
623 if (n
> BUS_AUTH_SIZE_MAX
)
624 n
= BUS_AUTH_SIZE_MAX
;
626 if (b
->rbuffer_size
>= n
)
629 p
= realloc(b
->rbuffer
, n
);
635 iov
= IOVEC_MAKE((uint8_t *)b
->rbuffer
+ b
->rbuffer_size
, n
- b
->rbuffer_size
);
637 if (b
->prefer_readv
) {
638 k
= readv(b
->input_fd
, &iov
, 1);
642 mh
= (struct msghdr
) {
645 .msg_control
= &control
,
646 .msg_controllen
= sizeof(control
),
649 k
= recvmsg_safe(b
->input_fd
, &mh
, MSG_DONTWAIT
|MSG_CMSG_CLOEXEC
);
650 if (k
== -ENOTSOCK
) {
651 b
->prefer_readv
= true;
652 k
= readv(b
->input_fd
, &iov
, 1);
658 if (ERRNO_IS_NEG_TRANSIENT(k
))
664 cmsg_close_all(&mh
); /* paranoia, we shouldn't have gotten any fds on EOF */
668 b
->rbuffer_size
+= k
;
671 r
= bus_process_cmsg(b
, &mh
, /* allow_fds = */ false);
676 r
= bus_socket_auth_verify(b
);
683 void bus_socket_setup(sd_bus
*b
) {
686 /* Increase the buffers to 8 MB */
687 (void) fd_increase_rxbuf(b
->input_fd
, SNDBUF_SIZE
);
688 (void) fd_inc_sndbuf(b
->output_fd
, SNDBUF_SIZE
);
690 b
->message_version
= 1;
691 b
->message_endian
= 0;
694 static void bus_get_peercred(sd_bus
*b
) {
698 assert(!b
->ucred_valid
);
700 assert(b
->n_groups
== SIZE_MAX
);
702 /* Get the peer for socketpair() sockets */
703 b
->ucred_valid
= getpeercred(b
->input_fd
, &b
->ucred
) >= 0;
705 /* Get the SELinux context of the peer */
706 r
= getpeersec(b
->input_fd
, &b
->label
);
707 if (r
< 0 && !IN_SET(r
, -EOPNOTSUPP
, -ENOPROTOOPT
))
708 log_debug_errno(r
, "Failed to determine peer security context, ignoring: %m");
710 /* Get the list of auxiliary groups of the peer */
711 r
= getpeergroups(b
->input_fd
, &b
->groups
);
713 b
->n_groups
= (size_t) r
;
714 else if (!IN_SET(r
, -EOPNOTSUPP
, -ENOPROTOOPT
))
715 log_debug_errno(r
, "Failed to determine peer's group list, ignoring: %m");
717 r
= getpeerpidfd(b
->input_fd
);
719 log_debug_errno(r
, "Failed to determine peer pidfd, ignoring: %m");
721 close_and_replace(b
->pidfd
, r
);
723 /* Let's query the peers socket address, it might carry information such as the peer's comm or
724 * description string */
725 zero(b
->sockaddr_peer
);
726 b
->sockaddr_size_peer
= 0;
728 socklen_t l
= sizeof(b
->sockaddr_peer
) - 1; /* Leave space for a NUL */
729 if (getpeername(b
->input_fd
, &b
->sockaddr_peer
.sa
, &l
) < 0)
730 log_debug_errno(errno
, "Failed to get peer's socket address, ignoring: %m");
732 b
->sockaddr_size_peer
= l
;
735 static int bus_socket_start_auth_client(sd_bus
*b
) {
736 static const char sasl_auth_anonymous
[] = {
738 * We use an arbitrary trace-string for the ANONYMOUS authentication. It can be used by the
739 * message broker to aid debugging of clients. We fully anonymize the connection and use a
742 /* HEX a n o n y m o u s */
743 "\0AUTH ANONYMOUS 616e6f6e796d6f7573\r\n"
745 static const char sasl_auth_external
[] = {
746 "\0AUTH EXTERNAL\r\n"
749 static const char sasl_negotiate_unix_fd
[] = {
750 "NEGOTIATE_UNIX_FD\r\n"
752 static const char sasl_begin
[] = {
759 if (b
->anonymous_auth
)
760 b
->auth_iovec
[i
++] = IOVEC_MAKE((char*) sasl_auth_anonymous
, sizeof(sasl_auth_anonymous
) - 1);
762 b
->auth_iovec
[i
++] = IOVEC_MAKE((char*) sasl_auth_external
, sizeof(sasl_auth_external
) - 1);
765 b
->auth_iovec
[i
++] = IOVEC_MAKE_STRING(sasl_negotiate_unix_fd
);
767 b
->auth_iovec
[i
++] = IOVEC_MAKE_STRING(sasl_begin
);
769 return bus_socket_write_auth(b
);
772 int bus_socket_start_auth(sd_bus
*b
) {
777 bus_set_state(b
, BUS_AUTHENTICATING
);
778 b
->auth_timeout
= now(CLOCK_MONOTONIC
) + BUS_AUTH_TIMEOUT
;
780 if (sd_is_socket(b
->input_fd
, AF_UNIX
, 0, 0) <= 0)
781 b
->accept_fd
= false;
783 if (b
->output_fd
!= b
->input_fd
)
784 if (sd_is_socket(b
->output_fd
, AF_UNIX
, 0, 0) <= 0)
785 b
->accept_fd
= false;
788 return bus_socket_read_auth(b
);
790 return bus_socket_start_auth_client(b
);
793 static int bus_socket_inotify_setup(sd_bus
*b
) {
794 _cleanup_free_
int *new_watches
= NULL
;
795 _cleanup_free_
char *absolute
= NULL
;
796 size_t n
= 0, done
= 0, i
;
797 unsigned max_follow
= 32;
802 assert(b
->watch_bind
);
803 assert(b
->sockaddr
.sa
.sa_family
== AF_UNIX
);
804 assert(b
->sockaddr
.un
.sun_path
[0] != 0);
806 /* Sets up an inotify fd in case watch_bind is enabled: wait until the configured AF_UNIX file system
807 * socket appears before connecting to it. The implemented is pretty simplistic: we just subscribe to
808 * relevant changes to all components of the path, and every time we get an event for that we try to
809 * reconnect again, without actually caring what precisely the event we got told us. If we still
810 * can't connect we re-subscribe to all relevant changes of anything in the path, so that our watches
811 * include any possibly newly created path components. */
813 if (b
->inotify_fd
< 0) {
814 b
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
815 if (b
->inotify_fd
< 0)
818 b
->inotify_fd
= fd_move_above_stdio(b
->inotify_fd
);
821 /* Make sure the path is NUL terminated */
822 p
= strndupa_safe(b
->sockaddr
.un
.sun_path
,
823 sizeof(b
->sockaddr
.un
.sun_path
));
825 /* Make sure the path is absolute */
826 r
= path_make_absolute_cwd(p
, &absolute
);
830 /* Watch all components of the path, and don't mind any prefix that doesn't exist yet. For the
831 * innermost directory that exists we want to know when files are created or moved into it. For all
832 * parents of it we just care if they are removed or renamed. */
834 if (!GREEDY_REALLOC(new_watches
, n
+ 1)) {
839 /* Start with the top-level directory, which is a bit simpler than the rest, since it can't be a
840 * symlink, and always exists */
841 wd
= inotify_add_watch(b
->inotify_fd
, "/", IN_CREATE
|IN_MOVED_TO
);
843 r
= log_debug_errno(errno
, "Failed to add inotify watch on /: %m");
846 new_watches
[n
++] = wd
;
849 _cleanup_free_
char *component
= NULL
, *prefix
= NULL
, *destination
= NULL
;
850 size_t n_slashes
, n_component
;
853 n_slashes
= strspn(absolute
+ done
, "/");
854 n_component
= n_slashes
+ strcspn(absolute
+ done
+ n_slashes
, "/");
856 if (n_component
== 0) /* The end */
859 component
= strndup(absolute
+ done
, n_component
);
865 /* A trailing slash? That's a directory, and not a socket then */
866 if (path_equal(component
, "/")) {
871 /* A single dot? Let's eat this up */
872 if (path_equal(component
, "/.")) {
877 prefix
= strndup(absolute
, done
+ n_component
);
883 if (!GREEDY_REALLOC(new_watches
, n
+ 1)) {
888 wd
= inotify_add_watch(b
->inotify_fd
, prefix
, IN_DELETE_SELF
|IN_MOVE_SELF
|IN_ATTRIB
|IN_CREATE
|IN_MOVED_TO
|IN_DONT_FOLLOW
);
889 log_debug("Added inotify watch for %s on bus %s: %i", prefix
, strna(b
->description
), wd
);
892 if (IN_SET(errno
, ENOENT
, ELOOP
))
893 break; /* This component doesn't exist yet, or the path contains a cyclic symlink right now */
895 r
= log_debug_errno(errno
, "Failed to add inotify watch on %s: %m", empty_to_root(prefix
));
898 new_watches
[n
++] = wd
;
900 /* Check if this is possibly a symlink. If so, let's follow it and watch it too. */
901 r
= readlink_malloc(prefix
, &destination
);
902 if (r
== -EINVAL
) { /* not a symlink */
909 if (isempty(destination
)) { /* Empty symlink target? Yuck! */
914 if (max_follow
<= 0) { /* Let's make sure we don't follow symlinks forever */
919 if (path_is_absolute(destination
)) {
920 /* For absolute symlinks we build the new path and start anew */
921 c
= strjoin(destination
, absolute
+ done
+ n_component
);
924 _cleanup_free_
char *t
= NULL
;
926 /* For relative symlinks we replace the last component, and try again */
927 t
= strndup(absolute
, done
);
931 c
= strjoin(t
, "/", destination
, absolute
+ done
+ n_component
);
938 free_and_replace(absolute
, c
);
943 /* And now, let's remove all watches from the previous iteration we don't need anymore */
944 for (i
= 0; i
< b
->n_inotify_watches
; i
++) {
948 for (j
= 0; j
< n
; j
++)
949 if (new_watches
[j
] == b
->inotify_watches
[i
]) {
957 (void) inotify_rm_watch(b
->inotify_fd
, b
->inotify_watches
[i
]);
960 free_and_replace(b
->inotify_watches
, new_watches
);
961 b
->n_inotify_watches
= n
;
966 bus_close_inotify_fd(b
);
970 static int bind_description(sd_bus
*b
, int fd
, int family
) {
971 _cleanup_free_
char *bind_name
= NULL
, *comm
= NULL
;
972 union sockaddr_union bsa
;
973 const char *d
= NULL
;
979 /* If this is an AF_UNIX socket, let's set our client's socket address to carry the description
980 * string for this bus connection. This is useful for debugging things, as the connection name is
981 * visible in various socket-related tools, and can even be queried by the server side. */
983 if (family
!= AF_UNIX
)
986 (void) sd_bus_get_description(b
, &d
);
988 /* Generate a recognizable source address in the abstract namespace. We'll include:
989 * - a random 64-bit value (to avoid collisions)
990 * - our "comm" process name (suppressed if contains "/" to avoid parsing issues)
991 * - the description string of the bus connection. */
992 (void) pid_get_comm(0, &comm
);
993 if (comm
&& strchr(comm
, '/'))
996 if (!d
&& !comm
) /* skip if we don't have either field, rely on kernel autobind instead */
999 if (asprintf(&bind_name
, "@%" PRIx64
"/bus/%s/%s", random_u64(), strempty(comm
), strempty(d
)) < 0)
1002 strshorten(bind_name
, sizeof_field(struct sockaddr_un
, sun_path
));
1004 r
= sockaddr_un_set_path(&bsa
.un
, bind_name
);
1008 if (bind(fd
, &bsa
.sa
, r
) < 0)
1014 static int connect_as(int fd
, const struct sockaddr
*sa
, socklen_t salen
, uid_t uid
, gid_t gid
) {
1015 _cleanup_close_pair_
int pfd
[2] = EBADF_PAIR
;
1019 /* Shortcut if we are not supposed to drop privileges */
1020 if (!uid_is_valid(uid
) && !gid_is_valid(gid
))
1021 return RET_NERRNO(connect(fd
, sa
, salen
));
1023 /* This changes identity to the specified uid/gid and issues connect() as that. This is useful to
1024 * make sure SO_PEERCRED reports the selected UID/GID rather than the usual one of the caller. */
1026 if (pipe2(pfd
, O_CLOEXEC
) < 0)
1029 r
= safe_fork("(sd-setresuid)", FORK_RESET_SIGNALS
|FORK_DEATHSIG_SIGKILL
|FORK_WAIT
, /* ret_pid= */ NULL
);
1035 pfd
[0] = safe_close(pfd
[0]);
1037 r
= RET_NERRNO(setgroups(0, NULL
));
1041 if (gid_is_valid(gid
)) {
1042 r
= RET_NERRNO(setresgid(gid
, gid
, gid
));
1047 if (uid_is_valid(uid
)) {
1048 r
= RET_NERRNO(setresuid(uid
, uid
, uid
));
1053 r
= RET_NERRNO(connect(fd
, sa
, salen
));
1060 n
= write(pfd
[1], &r
, sizeof(r
));
1062 _exit(EXIT_FAILURE
);
1064 _exit(EXIT_SUCCESS
);
1067 n
= read(pfd
[0], &r
, sizeof(r
));
1074 int bus_socket_connect(sd_bus
*b
) {
1075 bool inotify_done
= false;
1081 assert(b
->input_fd
< 0);
1082 assert(b
->output_fd
< 0);
1083 assert(b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
);
1085 if (DEBUG_LOGGING
) {
1086 _cleanup_free_
char *pretty
= NULL
;
1087 (void) sockaddr_pretty(&b
->sockaddr
.sa
, b
->sockaddr_size
, false, true, &pretty
);
1088 log_debug("sd-bus: starting bus%s%s by connecting to %s...",
1089 b
->description
? " " : "", strempty(b
->description
), strnull(pretty
));
1092 b
->input_fd
= socket(b
->sockaddr
.sa
.sa_family
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
1093 if (b
->input_fd
< 0)
1096 r
= bind_description(b
, b
->input_fd
, b
->sockaddr
.sa
.sa_family
);
1100 b
->input_fd
= fd_move_above_stdio(b
->input_fd
);
1102 b
->output_fd
= b
->input_fd
;
1103 bus_socket_setup(b
);
1105 r
= connect_as(b
->input_fd
, &b
->sockaddr
.sa
, b
->sockaddr_size
, b
->connect_as_uid
, b
->connect_as_gid
);
1107 if (r
== -EINPROGRESS
) {
1109 /* If we have any inotify watches open, close them now, we don't need them anymore, as
1110 * we have successfully initiated a connection */
1111 bus_close_inotify_fd(b
);
1113 /* Note that very likely we are already in BUS_OPENING state here, as we enter it when
1114 * we start parsing the address string. The only reason we set the state explicitly
1115 * here, is to undo BUS_WATCH_BIND, in case we did the inotify magic. */
1116 bus_set_state(b
, BUS_OPENING
);
1120 if (IN_SET(r
, -ENOENT
, -ECONNREFUSED
) && /* ENOENT → unix socket doesn't exist at all; ECONNREFUSED → unix socket stale */
1122 b
->sockaddr
.sa
.sa_family
== AF_UNIX
&&
1123 b
->sockaddr
.un
.sun_path
[0] != 0) {
1125 /* This connection attempt failed, let's release the socket for now, and start with a
1126 * fresh one when reconnecting. */
1127 bus_close_io_fds(b
);
1130 /* inotify set up already, don't do it again, just return now, and remember
1131 * that we are waiting for inotify events now. */
1132 bus_set_state(b
, BUS_WATCH_BIND
);
1136 /* This is a file system socket, and the inotify logic is enabled. Let's create the necessary inotify fd. */
1137 r
= bus_socket_inotify_setup(b
);
1141 /* Let's now try to connect a second time, because in theory there's otherwise a race
1142 * here: the socket might have been created in the time between our first connect() and
1143 * the time we set up the inotify logic. But let's remember that we set up inotify now,
1144 * so that we don't do the connect() more than twice. */
1145 inotify_done
= true;
1153 /* Yay, established, we don't need no inotify anymore! */
1154 bus_close_inotify_fd(b
);
1156 return bus_socket_start_auth(b
);
1159 int bus_socket_exec(sd_bus
*b
) {
1163 assert(b
->input_fd
< 0);
1164 assert(b
->output_fd
< 0);
1165 assert(b
->exec_path
);
1166 assert(b
->busexec_pid
== 0);
1168 if (DEBUG_LOGGING
) {
1169 _cleanup_free_
char *line
= NULL
;
1172 line
= quote_command_line(b
->exec_argv
, SHELL_ESCAPE_EMPTY
);
1174 log_debug("sd-bus: starting bus%s%s with %s%s",
1175 b
->description
? " " : "", strempty(b
->description
),
1176 line
?: b
->exec_path
,
1177 b
->exec_argv
&& !line
? "…" : "");
1180 r
= socketpair(AF_UNIX
, SOCK_STREAM
|SOCK_NONBLOCK
|SOCK_CLOEXEC
, 0, s
);
1184 r
= safe_fork_full("(sd-busexec)",
1185 (int[]) { s
[1], s
[1], STDERR_FILENO
},
1187 FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_REARRANGE_STDIO
|FORK_RLIMIT_NOFILE_SAFE
, &b
->busexec_pid
);
1196 execvp(b
->exec_path
, b
->exec_argv
);
1198 execvp(b
->exec_path
, STRV_MAKE(b
->exec_path
));
1200 _exit(EXIT_FAILURE
);
1204 b
->output_fd
= b
->input_fd
= fd_move_above_stdio(s
[0]);
1206 bus_socket_setup(b
);
1208 return bus_socket_start_auth(b
);
1211 int bus_socket_take_fd(sd_bus
*b
) {
1214 bus_socket_setup(b
);
1216 return bus_socket_start_auth(b
);
1219 int bus_socket_write_message(sd_bus
*bus
, sd_bus_message
*m
, size_t *idx
) {
1229 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1231 if (*idx
>= BUS_MESSAGE_SIZE(m
))
1234 r
= bus_message_setup_iovec(m
);
1238 n
= m
->n_iovec
* sizeof(struct iovec
);
1239 iov
= newa(struct iovec
, n
);
1240 memcpy_safe(iov
, m
->iovec
, n
);
1243 iovec_advance(iov
, &j
, *idx
);
1245 if (bus
->prefer_writev
)
1246 k
= writev(bus
->output_fd
, iov
, m
->n_iovec
);
1248 struct msghdr mh
= {
1250 .msg_iovlen
= m
->n_iovec
,
1253 if (m
->n_fds
> 0 && *idx
== 0) {
1254 struct cmsghdr
*control
;
1256 mh
.msg_controllen
= CMSG_SPACE(sizeof(int) * m
->n_fds
);
1257 mh
.msg_control
= alloca0(mh
.msg_controllen
);
1258 control
= CMSG_FIRSTHDR(&mh
);
1259 control
->cmsg_len
= CMSG_LEN(sizeof(int) * m
->n_fds
);
1260 control
->cmsg_level
= SOL_SOCKET
;
1261 control
->cmsg_type
= SCM_RIGHTS
;
1262 memcpy(CMSG_DATA(control
), m
->fds
, sizeof(int) * m
->n_fds
);
1265 k
= sendmsg(bus
->output_fd
, &mh
, MSG_DONTWAIT
|MSG_NOSIGNAL
);
1266 if (k
< 0 && errno
== ENOTSOCK
) {
1267 bus
->prefer_writev
= true;
1268 k
= writev(bus
->output_fd
, iov
, m
->n_iovec
);
1273 return ERRNO_IS_TRANSIENT(errno
) ? 0 : -errno
;
1279 static int bus_socket_read_message_need(sd_bus
*bus
, size_t *need
) {
1286 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1288 if (bus
->rbuffer_size
< sizeof(BusMessageHeader
)) {
1289 *need
= sizeof(BusMessageHeader
) + 8;
1291 /* Minimum message size:
1295 * Method Call: +2 string headers
1296 * Signal: +3 string headers
1297 * Method Error: +1 string headers
1299 * Method Reply: +1 uint32 headers
1301 * A string header is at least 9 bytes
1302 * A uint32 header is at least 8 bytes
1304 * Hence the minimum message size of a valid message
1305 * is header + 8 bytes */
1310 a
= ((const uint32_t*) bus
->rbuffer
)[1];
1311 b
= ((const uint32_t*) bus
->rbuffer
)[3];
1313 e
= ((const uint8_t*) bus
->rbuffer
)[0];
1314 if (e
== BUS_LITTLE_ENDIAN
) {
1317 } else if (e
== BUS_BIG_ENDIAN
) {
1323 sum
= (uint64_t) sizeof(BusMessageHeader
) + (uint64_t) ALIGN8(b
) + (uint64_t) a
;
1324 if (sum
>= BUS_MESSAGE_SIZE_MAX
)
1327 *need
= (size_t) sum
;
1331 static int bus_socket_make_message(sd_bus
*bus
, size_t size
) {
1332 sd_bus_message
*t
= NULL
;
1337 assert(bus
->rbuffer_size
>= size
);
1338 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1340 r
= bus_rqueue_make_room(bus
);
1344 if (bus
->rbuffer_size
> size
) {
1345 b
= memdup((const uint8_t*) bus
->rbuffer
+ size
,
1346 bus
->rbuffer_size
- size
);
1352 r
= bus_message_from_malloc(bus
,
1354 bus
->fds
, bus
->n_fds
,
1357 if (r
== -EBADMSG
) {
1358 log_debug_errno(r
, "Received invalid message from connection %s, dropping.", strna(bus
->description
));
1359 free(bus
->rbuffer
); /* We want to drop current rbuffer and proceed with whatever remains in b */
1365 /* rbuffer ownership was either transferred to t, or we got EBADMSG and dropped it. */
1367 bus
->rbuffer_size
-= size
;
1373 t
->read_counter
= ++bus
->read_counter
;
1374 bus
->rqueue
[bus
->rqueue_size
++] = bus_message_ref_queued(t
, bus
);
1375 sd_bus_message_unref(t
);
1381 int bus_socket_read_message(sd_bus
*bus
) {
1383 struct iovec iov
= {};
1388 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int) * BUS_FDS_MAX
)) control
;
1389 bool handle_cmsg
= false;
1392 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1394 r
= bus_socket_read_message_need(bus
, &need
);
1398 if (bus
->rbuffer_size
>= need
)
1399 return bus_socket_make_message(bus
, need
);
1401 b
= realloc(bus
->rbuffer
, need
);
1407 iov
= IOVEC_MAKE((uint8_t *)bus
->rbuffer
+ bus
->rbuffer_size
, need
- bus
->rbuffer_size
);
1409 if (bus
->prefer_readv
) {
1410 k
= readv(bus
->input_fd
, &iov
, 1);
1414 mh
= (struct msghdr
) {
1417 .msg_control
= &control
,
1418 .msg_controllen
= sizeof(control
),
1421 k
= recvmsg_safe(bus
->input_fd
, &mh
, MSG_DONTWAIT
|MSG_CMSG_CLOEXEC
);
1422 if (k
== -ENOTSOCK
) {
1423 bus
->prefer_readv
= true;
1424 k
= readv(bus
->input_fd
, &iov
, 1);
1430 if (ERRNO_IS_NEG_TRANSIENT(k
))
1436 cmsg_close_all(&mh
); /* On EOF we shouldn't have gotten an fd, but let's make sure */
1440 bus
->rbuffer_size
+= k
;
1443 r
= bus_process_cmsg(bus
, &mh
, bus
->can_fds
);
1448 r
= bus_socket_read_message_need(bus
, &need
);
1452 if (bus
->rbuffer_size
>= need
)
1453 return bus_socket_make_message(bus
, need
);
1458 int bus_socket_process_opening(sd_bus
*b
) {
1459 int error
= 0, events
, r
;
1460 socklen_t slen
= sizeof(error
);
1462 assert(b
->state
== BUS_OPENING
);
1464 events
= fd_wait_for_event(b
->output_fd
, POLLOUT
, 0);
1465 if (ERRNO_IS_NEG_TRANSIENT(events
))
1469 if (!(events
& (POLLOUT
|POLLERR
|POLLHUP
)))
1472 r
= getsockopt(b
->output_fd
, SOL_SOCKET
, SO_ERROR
, &error
, &slen
);
1474 b
->last_connect_error
= errno
;
1475 else if (error
!= 0)
1476 b
->last_connect_error
= error
;
1477 else if (events
& (POLLERR
|POLLHUP
))
1478 b
->last_connect_error
= ECONNREFUSED
;
1480 return bus_socket_start_auth(b
);
1482 return bus_next_address(b
);
1485 int bus_socket_process_authenticating(sd_bus
*b
) {
1489 assert(b
->state
== BUS_AUTHENTICATING
);
1491 if (now(CLOCK_MONOTONIC
) >= b
->auth_timeout
)
1494 r
= bus_socket_write_auth(b
);
1498 return bus_socket_read_auth(b
);
1501 int bus_socket_process_watch_bind(sd_bus
*b
) {
1505 assert(b
->state
== BUS_WATCH_BIND
);
1506 assert(b
->inotify_fd
>= 0);
1508 r
= flush_fd(b
->inotify_fd
);
1512 log_debug("Got inotify event on bus %s.", strna(b
->description
));
1514 /* We flushed events out of the inotify fd. In that case, maybe the socket is valid now? Let's try to connect
1517 r
= bus_socket_connect(b
);
1521 q
= bus_attach_io_events(b
);
1525 q
= bus_attach_inotify_event(b
);