]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/bus-socket.c
cryptenroll: use root device by default
[thirdparty/systemd.git] / src / libsystemd / sd-bus / bus-socket.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <endian.h>
4 #include <poll.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7
8 #include "sd-bus.h"
9 #include "sd-daemon.h"
10
11 #include "alloc-util.h"
12 #include "bus-internal.h"
13 #include "bus-message.h"
14 #include "bus-socket.h"
15 #include "escape.h"
16 #include "fd-util.h"
17 #include "format-util.h"
18 #include "fs-util.h"
19 #include "hexdecoct.h"
20 #include "io-util.h"
21 #include "iovec-util.h"
22 #include "macro.h"
23 #include "memory-util.h"
24 #include "path-util.h"
25 #include "process-util.h"
26 #include "random-util.h"
27 #include "signal-util.h"
28 #include "stdio-util.h"
29 #include "string-util.h"
30 #include "user-util.h"
31 #include "utf8.h"
32
33 #define SNDBUF_SIZE (8*1024*1024)
34
35 static void iovec_advance(struct iovec iov[], unsigned *idx, size_t size) {
36
37 while (size > 0) {
38 struct iovec *i = iov + *idx;
39
40 if (i->iov_len > size) {
41 i->iov_base = (uint8_t*) i->iov_base + size;
42 i->iov_len -= size;
43 return;
44 }
45
46 size -= i->iov_len;
47
48 *i = IOVEC_MAKE(NULL, 0);
49
50 (*idx)++;
51 }
52 }
53
54 static int append_iovec(sd_bus_message *m, const void *p, size_t sz) {
55 assert(m);
56 assert(p);
57 assert(sz > 0);
58
59 m->iovec[m->n_iovec++] = IOVEC_MAKE((void*) p, sz);
60
61 return 0;
62 }
63
64 static int bus_message_setup_iovec(sd_bus_message *m) {
65 struct bus_body_part *part;
66 unsigned n, i;
67 int r;
68
69 assert(m);
70 assert(m->sealed);
71
72 if (m->n_iovec > 0)
73 return 0;
74
75 assert(!m->iovec);
76
77 n = 1 + m->n_body_parts;
78 if (n < ELEMENTSOF(m->iovec_fixed))
79 m->iovec = m->iovec_fixed;
80 else {
81 m->iovec = new(struct iovec, n);
82 if (!m->iovec) {
83 r = -ENOMEM;
84 goto fail;
85 }
86 }
87
88 r = append_iovec(m, m->header, BUS_MESSAGE_BODY_BEGIN(m));
89 if (r < 0)
90 goto fail;
91
92 MESSAGE_FOREACH_PART(part, i, m) {
93 r = bus_body_part_map(part);
94 if (r < 0)
95 goto fail;
96
97 r = append_iovec(m, part->data, part->size);
98 if (r < 0)
99 goto fail;
100 }
101
102 assert(n == m->n_iovec);
103
104 return 0;
105
106 fail:
107 m->poisoned = true;
108 return r;
109 }
110
111 bool bus_socket_auth_needs_write(sd_bus *b) {
112
113 unsigned i;
114
115 if (b->auth_index >= ELEMENTSOF(b->auth_iovec))
116 return false;
117
118 for (i = b->auth_index; i < ELEMENTSOF(b->auth_iovec); i++) {
119 struct iovec *j = b->auth_iovec + i;
120
121 if (j->iov_len > 0)
122 return true;
123 }
124
125 return false;
126 }
127
128 static int bus_socket_auth_verify_client(sd_bus *b) {
129 char *l, *lines[4] = {};
130 sd_id128_t peer;
131 size_t i, n;
132 int r;
133
134 assert(b);
135
136 /*
137 * We expect up to three response lines:
138 * "DATA\r\n" (optional)
139 * "OK <server-id>\r\n"
140 * "AGREE_UNIX_FD\r\n" (optional)
141 */
142
143 n = 0;
144 lines[n] = b->rbuffer;
145 for (i = 0; i < 3; ++i) {
146 l = memmem_safe(lines[n], b->rbuffer_size - (lines[n] - (char*) b->rbuffer), "\r\n", 2);
147 if (l)
148 lines[++n] = l + 2;
149 else
150 break;
151 }
152
153 /*
154 * If we sent a non-empty initial response, then we just expect an OK
155 * reply. We currently do this if, and only if, we picked ANONYMOUS.
156 * If we did not send an initial response, then we expect a DATA
157 * challenge, reply with our own DATA, and expect an OK reply. We do
158 * this for EXTERNAL.
159 * If FD negotiation was requested, we additionally expect
160 * an AGREE_UNIX_FD response in all cases.
161 */
162 if (n < (b->anonymous_auth ? 1U : 2U) + !!b->accept_fd)
163 return 0; /* wait for more data */
164
165 i = 0;
166
167 /* In case of EXTERNAL, verify the first response was DATA. */
168 if (!b->anonymous_auth) {
169 l = lines[i++];
170 if (lines[i] - l == 4 + 2) {
171 if (memcmp(l, "DATA", 4))
172 return -EPERM;
173 } else if (lines[i] - l == 3 + 32 + 2) {
174 /*
175 * Old versions of the server-side implementation of
176 * `sd-bus` replied with "OK <id>" to "AUTH" requests
177 * from a client, even if the "AUTH" line did not
178 * contain inlined arguments. Therefore, we also accept
179 * "OK <id>" here, even though it is technically the
180 * wrong reply. We ignore the "<id>" parameter, though,
181 * since it has no real value.
182 */
183 if (memcmp(l, "OK ", 3))
184 return -EPERM;
185 } else
186 return -EPERM;
187 }
188
189 /* Now check the OK line. */
190 l = lines[i++];
191
192 if (lines[i] - l != 3 + 32 + 2)
193 return -EPERM;
194 if (memcmp(l, "OK ", 3))
195 return -EPERM;
196
197 b->auth = b->anonymous_auth ? BUS_AUTH_ANONYMOUS : BUS_AUTH_EXTERNAL;
198
199 for (unsigned j = 0; j < 32; j += 2) {
200 int x, y;
201
202 x = unhexchar(l[3 + j]);
203 y = unhexchar(l[3 + j + 1]);
204
205 if (x < 0 || y < 0)
206 return -EINVAL;
207
208 peer.bytes[j/2] = ((uint8_t) x << 4 | (uint8_t) y);
209 }
210
211 if (!sd_id128_is_null(b->server_id) &&
212 !sd_id128_equal(b->server_id, peer))
213 return -EPERM;
214
215 b->server_id = peer;
216
217 /* And possibly check the third line, too */
218 if (b->accept_fd) {
219 l = lines[i++];
220 b->can_fds = memory_startswith(l, lines[i] - l, "AGREE_UNIX_FD");
221 }
222
223 assert(i == n);
224
225 b->rbuffer_size -= (lines[i] - (char*) b->rbuffer);
226 memmove(b->rbuffer, lines[i], b->rbuffer_size);
227
228 r = bus_start_running(b);
229 if (r < 0)
230 return r;
231
232 return 1;
233 }
234
235 static bool line_equals(const char *s, size_t m, const char *line) {
236 size_t l;
237
238 l = strlen(line);
239 if (l != m)
240 return false;
241
242 return memcmp(s, line, l) == 0;
243 }
244
245 static bool line_begins(const char *s, size_t m, const char *word) {
246 const char *p;
247
248 p = memory_startswith(s, m, word);
249 return p && (p == (s + m) || *p == ' ');
250 }
251
252 static int verify_anonymous_token(sd_bus *b, const char *p, size_t l) {
253 _cleanup_free_ char *token = NULL;
254 size_t len;
255 int r;
256
257 if (!b->anonymous_auth)
258 return 0;
259
260 if (l <= 0)
261 return 1;
262
263 assert(p[0] == ' ');
264 p++; l--;
265
266 if (l % 2 != 0)
267 return 0;
268
269 r = unhexmem_full(p, l, /* secure = */ false, (void**) &token, &len);
270 if (r < 0)
271 return 0;
272
273 if (memchr(token, 0, len))
274 return 0;
275
276 return !!utf8_is_valid(token);
277 }
278
279 static int verify_external_token(sd_bus *b, const char *p, size_t l) {
280 _cleanup_free_ char *token = NULL;
281 size_t len;
282 uid_t u;
283 int r;
284
285 /* We don't do any real authentication here. Instead, if
286 * the owner of this bus wanted authentication they should have
287 * checked SO_PEERCRED before even creating the bus object. */
288
289 if (!b->anonymous_auth && !b->ucred_valid)
290 return 0;
291
292 if (l <= 0)
293 return 1;
294
295 assert(p[0] == ' ');
296 p++; l--;
297
298 if (l % 2 != 0)
299 return 0;
300
301 r = unhexmem_full(p, l, /* secure = */ false, (void**) &token, &len);
302 if (r < 0)
303 return 0;
304
305 if (memchr(token, 0, len))
306 return 0;
307
308 r = parse_uid(token, &u);
309 if (r < 0)
310 return 0;
311
312 /* We ignore the passed value if anonymous authentication is
313 * on anyway. */
314 if (!b->anonymous_auth && u != b->ucred.uid)
315 return 0;
316
317 return 1;
318 }
319
320 static int bus_socket_auth_write(sd_bus *b, const char *t) {
321 char *p;
322 size_t l;
323
324 assert(b);
325 assert(t);
326
327 /* We only make use of the first iovec */
328 assert(IN_SET(b->auth_index, 0, 1));
329
330 l = strlen(t);
331 p = malloc(b->auth_iovec[0].iov_len + l);
332 if (!p)
333 return -ENOMEM;
334
335 memcpy_safe(p, b->auth_iovec[0].iov_base, b->auth_iovec[0].iov_len);
336 memcpy(p + b->auth_iovec[0].iov_len, t, l);
337
338 b->auth_iovec[0].iov_base = p;
339 b->auth_iovec[0].iov_len += l;
340
341 free_and_replace(b->auth_buffer, p);
342 b->auth_index = 0;
343 return 0;
344 }
345
346 static int bus_socket_auth_write_ok(sd_bus *b) {
347 char t[3 + 32 + 2 + 1];
348
349 assert(b);
350
351 xsprintf(t, "OK " SD_ID128_FORMAT_STR "\r\n", SD_ID128_FORMAT_VAL(b->server_id));
352
353 return bus_socket_auth_write(b, t);
354 }
355
356 static int bus_socket_auth_verify_server(sd_bus *b) {
357 char *e;
358 const char *line;
359 size_t l;
360 bool processed = false;
361 int r;
362
363 assert(b);
364
365 if (b->rbuffer_size < 1)
366 return 0;
367
368 /* First char must be a NUL byte */
369 if (*(char*) b->rbuffer != 0)
370 return -EIO;
371
372 if (b->rbuffer_size < 3)
373 return 0;
374
375 /* Begin with the first line */
376 if (b->auth_rbegin <= 0)
377 b->auth_rbegin = 1;
378
379 for (;;) {
380 /* Check if line is complete */
381 line = (char*) b->rbuffer + b->auth_rbegin;
382 e = memmem_safe(line, b->rbuffer_size - b->auth_rbegin, "\r\n", 2);
383 if (!e)
384 return processed;
385
386 l = e - line;
387
388 if (line_begins(line, l, "AUTH ANONYMOUS")) {
389
390 r = verify_anonymous_token(b,
391 line + strlen("AUTH ANONYMOUS"),
392 l - strlen("AUTH ANONYMOUS"));
393 if (r < 0)
394 return r;
395 if (r == 0)
396 r = bus_socket_auth_write(b, "REJECTED\r\n");
397 else {
398 b->auth = BUS_AUTH_ANONYMOUS;
399 if (l <= strlen("AUTH ANONYMOUS"))
400 r = bus_socket_auth_write(b, "DATA\r\n");
401 else
402 r = bus_socket_auth_write_ok(b);
403 }
404
405 } else if (line_begins(line, l, "AUTH EXTERNAL")) {
406
407 r = verify_external_token(b,
408 line + strlen("AUTH EXTERNAL"),
409 l - strlen("AUTH EXTERNAL"));
410 if (r < 0)
411 return r;
412 if (r == 0)
413 r = bus_socket_auth_write(b, "REJECTED\r\n");
414 else {
415 b->auth = BUS_AUTH_EXTERNAL;
416 if (l <= strlen("AUTH EXTERNAL"))
417 r = bus_socket_auth_write(b, "DATA\r\n");
418 else
419 r = bus_socket_auth_write_ok(b);
420 }
421
422 } else if (line_begins(line, l, "AUTH"))
423 r = bus_socket_auth_write(b, "REJECTED EXTERNAL ANONYMOUS\r\n");
424 else if (line_equals(line, l, "CANCEL") ||
425 line_begins(line, l, "ERROR")) {
426
427 b->auth = _BUS_AUTH_INVALID;
428 r = bus_socket_auth_write(b, "REJECTED\r\n");
429
430 } else if (line_equals(line, l, "BEGIN")) {
431
432 if (b->auth == _BUS_AUTH_INVALID)
433 r = bus_socket_auth_write(b, "ERROR\r\n");
434 else {
435 /* We can't leave from the auth phase
436 * before we haven't written
437 * everything queued, so let's check
438 * that */
439
440 if (bus_socket_auth_needs_write(b))
441 return 1;
442
443 b->rbuffer_size -= (e + 2 - (char*) b->rbuffer);
444 memmove(b->rbuffer, e + 2, b->rbuffer_size);
445 return bus_start_running(b);
446 }
447
448 } else if (line_begins(line, l, "DATA")) {
449
450 if (b->auth == _BUS_AUTH_INVALID)
451 r = bus_socket_auth_write(b, "ERROR\r\n");
452 else {
453 if (b->auth == BUS_AUTH_ANONYMOUS)
454 r = verify_anonymous_token(b, line + 4, l - 4);
455 else
456 r = verify_external_token(b, line + 4, l - 4);
457
458 if (r < 0)
459 return r;
460 if (r == 0) {
461 b->auth = _BUS_AUTH_INVALID;
462 r = bus_socket_auth_write(b, "REJECTED\r\n");
463 } else
464 r = bus_socket_auth_write_ok(b);
465 }
466 } else if (line_equals(line, l, "NEGOTIATE_UNIX_FD")) {
467 if (b->auth == _BUS_AUTH_INVALID || !b->accept_fd)
468 r = bus_socket_auth_write(b, "ERROR\r\n");
469 else {
470 b->can_fds = true;
471 r = bus_socket_auth_write(b, "AGREE_UNIX_FD\r\n");
472 }
473 } else
474 r = bus_socket_auth_write(b, "ERROR\r\n");
475
476 if (r < 0)
477 return r;
478
479 b->auth_rbegin = e + 2 - (char*) b->rbuffer;
480
481 processed = true;
482 }
483 }
484
485 static int bus_socket_auth_verify(sd_bus *b) {
486 assert(b);
487
488 if (b->is_server)
489 return bus_socket_auth_verify_server(b);
490 else
491 return bus_socket_auth_verify_client(b);
492 }
493
494 static int bus_socket_write_auth(sd_bus *b) {
495 ssize_t k;
496
497 assert(b);
498 assert(b->state == BUS_AUTHENTICATING);
499
500 if (!bus_socket_auth_needs_write(b))
501 return 0;
502
503 if (b->prefer_writev)
504 k = writev(b->output_fd, b->auth_iovec + b->auth_index, ELEMENTSOF(b->auth_iovec) - b->auth_index);
505 else {
506 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control = {};
507
508 struct msghdr mh = {
509 .msg_iov = b->auth_iovec + b->auth_index,
510 .msg_iovlen = ELEMENTSOF(b->auth_iovec) - b->auth_index,
511 };
512
513 if (uid_is_valid(b->connect_as_uid) || gid_is_valid(b->connect_as_gid)) {
514
515 /* If we shall connect under some specific UID/GID, then synthesize an
516 * SCM_CREDENTIALS record accordingly. After all we want to adopt this UID/GID both
517 * for SO_PEERCRED (where we have to fork()) and SCM_CREDENTIALS (where we can just
518 * fake it via sendmsg()) */
519
520 struct ucred ucred = {
521 .pid = getpid_cached(),
522 .uid = uid_is_valid(b->connect_as_uid) ? b->connect_as_uid : getuid(),
523 .gid = gid_is_valid(b->connect_as_gid) ? b->connect_as_gid : getgid(),
524 };
525
526 mh.msg_control = &control;
527 mh.msg_controllen = sizeof(control);
528 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&mh);
529 *cmsg = (struct cmsghdr) {
530 .cmsg_level = SOL_SOCKET,
531 .cmsg_type = SCM_CREDENTIALS,
532 .cmsg_len = CMSG_LEN(sizeof(struct ucred)),
533 };
534
535 memcpy(CMSG_DATA(cmsg), &ucred, sizeof(struct ucred));
536 }
537
538 k = sendmsg(b->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
539 if (k < 0 && errno == ENOTSOCK) {
540 b->prefer_writev = true;
541 k = writev(b->output_fd, b->auth_iovec + b->auth_index, ELEMENTSOF(b->auth_iovec) - b->auth_index);
542 }
543 }
544
545 if (k < 0)
546 return ERRNO_IS_TRANSIENT(errno) ? 0 : -errno;
547
548 iovec_advance(b->auth_iovec, &b->auth_index, (size_t) k);
549
550 /* Now crank the state machine since we might be able to make progress after writing. For example,
551 * the server only processes "BEGIN" when the write buffer is empty.
552 */
553 return bus_socket_auth_verify(b);
554 }
555
556 static int bus_socket_read_auth(sd_bus *b) {
557 struct msghdr mh;
558 struct iovec iov = {};
559 size_t n;
560 ssize_t k;
561 int r;
562 void *p;
563 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int) * BUS_FDS_MAX)) control;
564 bool handle_cmsg = false;
565
566 assert(b);
567 assert(b->state == BUS_AUTHENTICATING);
568
569 r = bus_socket_auth_verify(b);
570 if (r != 0)
571 return r;
572
573 n = MAX(256u, b->rbuffer_size * 2);
574
575 if (n > BUS_AUTH_SIZE_MAX)
576 n = BUS_AUTH_SIZE_MAX;
577
578 if (b->rbuffer_size >= n)
579 return -ENOBUFS;
580
581 p = realloc(b->rbuffer, n);
582 if (!p)
583 return -ENOMEM;
584
585 b->rbuffer = p;
586
587 iov = IOVEC_MAKE((uint8_t *)b->rbuffer + b->rbuffer_size, n - b->rbuffer_size);
588
589 if (b->prefer_readv) {
590 k = readv(b->input_fd, &iov, 1);
591 if (k < 0)
592 k = -errno;
593 } else {
594 mh = (struct msghdr) {
595 .msg_iov = &iov,
596 .msg_iovlen = 1,
597 .msg_control = &control,
598 .msg_controllen = sizeof(control),
599 };
600
601 k = recvmsg_safe(b->input_fd, &mh, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
602 if (k == -ENOTSOCK) {
603 b->prefer_readv = true;
604 k = readv(b->input_fd, &iov, 1);
605 if (k < 0)
606 k = -errno;
607 } else
608 handle_cmsg = true;
609 }
610 if (ERRNO_IS_NEG_TRANSIENT(k))
611 return 0;
612 if (k < 0)
613 return (int) k;
614 if (k == 0) {
615 if (handle_cmsg)
616 cmsg_close_all(&mh); /* paranoia, we shouldn't have gotten any fds on EOF */
617 return -ECONNRESET;
618 }
619
620 b->rbuffer_size += k;
621
622 if (handle_cmsg) {
623 struct cmsghdr *cmsg;
624
625 CMSG_FOREACH(cmsg, &mh)
626 if (cmsg->cmsg_level == SOL_SOCKET &&
627 cmsg->cmsg_type == SCM_RIGHTS) {
628 int j;
629
630 /* Whut? We received fds during the auth
631 * protocol? Somebody is playing games with
632 * us. Close them all, and fail */
633 j = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
634 close_many(CMSG_TYPED_DATA(cmsg, int), j);
635 return -EIO;
636 } else
637 log_debug("Got unexpected auxiliary data with level=%d and type=%d",
638 cmsg->cmsg_level, cmsg->cmsg_type);
639 }
640
641 r = bus_socket_auth_verify(b);
642 if (r != 0)
643 return r;
644
645 return 1;
646 }
647
648 void bus_socket_setup(sd_bus *b) {
649 assert(b);
650
651 /* Increase the buffers to 8 MB */
652 (void) fd_increase_rxbuf(b->input_fd, SNDBUF_SIZE);
653 (void) fd_inc_sndbuf(b->output_fd, SNDBUF_SIZE);
654
655 b->message_version = 1;
656 b->message_endian = 0;
657 }
658
659 static void bus_get_peercred(sd_bus *b) {
660 int r;
661
662 assert(b);
663 assert(!b->ucred_valid);
664 assert(!b->label);
665 assert(b->n_groups == SIZE_MAX);
666
667 /* Get the peer for socketpair() sockets */
668 b->ucred_valid = getpeercred(b->input_fd, &b->ucred) >= 0;
669
670 /* Get the SELinux context of the peer */
671 r = getpeersec(b->input_fd, &b->label);
672 if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT))
673 log_debug_errno(r, "Failed to determine peer security context, ignoring: %m");
674
675 /* Get the list of auxiliary groups of the peer */
676 r = getpeergroups(b->input_fd, &b->groups);
677 if (r >= 0)
678 b->n_groups = (size_t) r;
679 else if (!IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT))
680 log_debug_errno(r, "Failed to determine peer's group list, ignoring: %m");
681
682 r = getpeerpidfd(b->input_fd);
683 if (r < 0)
684 log_debug_errno(r, "Failed to determine peer pidfd, ignoring: %m");
685 else
686 close_and_replace(b->pidfd, r);
687
688 /* Let's query the peers socket address, it might carry information such as the peer's comm or
689 * description string */
690 zero(b->sockaddr_peer);
691 b->sockaddr_size_peer = 0;
692
693 socklen_t l = sizeof(b->sockaddr_peer) - 1; /* Leave space for a NUL */
694 if (getpeername(b->input_fd, &b->sockaddr_peer.sa, &l) < 0)
695 log_debug_errno(errno, "Failed to get peer's socket address, ignoring: %m");
696 else
697 b->sockaddr_size_peer = l;
698 }
699
700 static int bus_socket_start_auth_client(sd_bus *b) {
701 static const char sasl_auth_anonymous[] = {
702 /*
703 * We use an arbitrary trace-string for the ANONYMOUS authentication. It can be used by the
704 * message broker to aid debugging of clients. We fully anonymize the connection and use a
705 * static default.
706 */
707 /* HEX a n o n y m o u s */
708 "\0AUTH ANONYMOUS 616e6f6e796d6f7573\r\n"
709 };
710 static const char sasl_auth_external[] = {
711 "\0AUTH EXTERNAL\r\n"
712 "DATA\r\n"
713 };
714 static const char sasl_negotiate_unix_fd[] = {
715 "NEGOTIATE_UNIX_FD\r\n"
716 };
717 static const char sasl_begin[] = {
718 "BEGIN\r\n"
719 };
720 size_t i = 0;
721
722 assert(b);
723
724 if (b->anonymous_auth)
725 b->auth_iovec[i++] = IOVEC_MAKE((char*) sasl_auth_anonymous, sizeof(sasl_auth_anonymous) - 1);
726 else
727 b->auth_iovec[i++] = IOVEC_MAKE((char*) sasl_auth_external, sizeof(sasl_auth_external) - 1);
728
729 if (b->accept_fd)
730 b->auth_iovec[i++] = IOVEC_MAKE_STRING(sasl_negotiate_unix_fd);
731
732 b->auth_iovec[i++] = IOVEC_MAKE_STRING(sasl_begin);
733
734 return bus_socket_write_auth(b);
735 }
736
737 int bus_socket_start_auth(sd_bus *b) {
738 assert(b);
739
740 bus_get_peercred(b);
741
742 bus_set_state(b, BUS_AUTHENTICATING);
743 b->auth_timeout = now(CLOCK_MONOTONIC) + BUS_AUTH_TIMEOUT;
744
745 if (sd_is_socket(b->input_fd, AF_UNIX, 0, 0) <= 0)
746 b->accept_fd = false;
747
748 if (b->output_fd != b->input_fd)
749 if (sd_is_socket(b->output_fd, AF_UNIX, 0, 0) <= 0)
750 b->accept_fd = false;
751
752 if (b->is_server)
753 return bus_socket_read_auth(b);
754 else
755 return bus_socket_start_auth_client(b);
756 }
757
758 static int bus_socket_inotify_setup(sd_bus *b) {
759 _cleanup_free_ int *new_watches = NULL;
760 _cleanup_free_ char *absolute = NULL;
761 size_t n = 0, done = 0, i;
762 unsigned max_follow = 32;
763 const char *p;
764 int wd, r;
765
766 assert(b);
767 assert(b->watch_bind);
768 assert(b->sockaddr.sa.sa_family == AF_UNIX);
769 assert(b->sockaddr.un.sun_path[0] != 0);
770
771 /* Sets up an inotify fd in case watch_bind is enabled: wait until the configured AF_UNIX file system
772 * socket appears before connecting to it. The implemented is pretty simplistic: we just subscribe to
773 * relevant changes to all components of the path, and every time we get an event for that we try to
774 * reconnect again, without actually caring what precisely the event we got told us. If we still
775 * can't connect we re-subscribe to all relevant changes of anything in the path, so that our watches
776 * include any possibly newly created path components. */
777
778 if (b->inotify_fd < 0) {
779 b->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
780 if (b->inotify_fd < 0)
781 return -errno;
782
783 b->inotify_fd = fd_move_above_stdio(b->inotify_fd);
784 }
785
786 /* Make sure the path is NUL terminated */
787 p = strndupa_safe(b->sockaddr.un.sun_path,
788 sizeof(b->sockaddr.un.sun_path));
789
790 /* Make sure the path is absolute */
791 r = path_make_absolute_cwd(p, &absolute);
792 if (r < 0)
793 goto fail;
794
795 /* Watch all components of the path, and don't mind any prefix that doesn't exist yet. For the
796 * innermost directory that exists we want to know when files are created or moved into it. For all
797 * parents of it we just care if they are removed or renamed. */
798
799 if (!GREEDY_REALLOC(new_watches, n + 1)) {
800 r = -ENOMEM;
801 goto fail;
802 }
803
804 /* Start with the top-level directory, which is a bit simpler than the rest, since it can't be a
805 * symlink, and always exists */
806 wd = inotify_add_watch(b->inotify_fd, "/", IN_CREATE|IN_MOVED_TO);
807 if (wd < 0) {
808 r = log_debug_errno(errno, "Failed to add inotify watch on /: %m");
809 goto fail;
810 } else
811 new_watches[n++] = wd;
812
813 for (;;) {
814 _cleanup_free_ char *component = NULL, *prefix = NULL, *destination = NULL;
815 size_t n_slashes, n_component;
816 char *c = NULL;
817
818 n_slashes = strspn(absolute + done, "/");
819 n_component = n_slashes + strcspn(absolute + done + n_slashes, "/");
820
821 if (n_component == 0) /* The end */
822 break;
823
824 component = strndup(absolute + done, n_component);
825 if (!component) {
826 r = -ENOMEM;
827 goto fail;
828 }
829
830 /* A trailing slash? That's a directory, and not a socket then */
831 if (path_equal(component, "/")) {
832 r = -EISDIR;
833 goto fail;
834 }
835
836 /* A single dot? Let's eat this up */
837 if (path_equal(component, "/.")) {
838 done += n_component;
839 continue;
840 }
841
842 prefix = strndup(absolute, done + n_component);
843 if (!prefix) {
844 r = -ENOMEM;
845 goto fail;
846 }
847
848 if (!GREEDY_REALLOC(new_watches, n + 1)) {
849 r = -ENOMEM;
850 goto fail;
851 }
852
853 wd = inotify_add_watch(b->inotify_fd, prefix, IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CREATE|IN_MOVED_TO|IN_DONT_FOLLOW);
854 log_debug("Added inotify watch for %s on bus %s: %i", prefix, strna(b->description), wd);
855
856 if (wd < 0) {
857 if (IN_SET(errno, ENOENT, ELOOP))
858 break; /* This component doesn't exist yet, or the path contains a cyclic symlink right now */
859
860 r = log_debug_errno(errno, "Failed to add inotify watch on %s: %m", empty_to_root(prefix));
861 goto fail;
862 } else
863 new_watches[n++] = wd;
864
865 /* Check if this is possibly a symlink. If so, let's follow it and watch it too. */
866 r = readlink_malloc(prefix, &destination);
867 if (r == -EINVAL) { /* not a symlink */
868 done += n_component;
869 continue;
870 }
871 if (r < 0)
872 goto fail;
873
874 if (isempty(destination)) { /* Empty symlink target? Yuck! */
875 r = -EINVAL;
876 goto fail;
877 }
878
879 if (max_follow <= 0) { /* Let's make sure we don't follow symlinks forever */
880 r = -ELOOP;
881 goto fail;
882 }
883
884 if (path_is_absolute(destination)) {
885 /* For absolute symlinks we build the new path and start anew */
886 c = strjoin(destination, absolute + done + n_component);
887 done = 0;
888 } else {
889 _cleanup_free_ char *t = NULL;
890
891 /* For relative symlinks we replace the last component, and try again */
892 t = strndup(absolute, done);
893 if (!t)
894 return -ENOMEM;
895
896 c = strjoin(t, "/", destination, absolute + done + n_component);
897 }
898 if (!c) {
899 r = -ENOMEM;
900 goto fail;
901 }
902
903 free_and_replace(absolute, c);
904
905 max_follow--;
906 }
907
908 /* And now, let's remove all watches from the previous iteration we don't need anymore */
909 for (i = 0; i < b->n_inotify_watches; i++) {
910 bool found = false;
911 size_t j;
912
913 for (j = 0; j < n; j++)
914 if (new_watches[j] == b->inotify_watches[i]) {
915 found = true;
916 break;
917 }
918
919 if (found)
920 continue;
921
922 (void) inotify_rm_watch(b->inotify_fd, b->inotify_watches[i]);
923 }
924
925 free_and_replace(b->inotify_watches, new_watches);
926 b->n_inotify_watches = n;
927
928 return 0;
929
930 fail:
931 bus_close_inotify_fd(b);
932 return r;
933 }
934
935 static int bind_description(sd_bus *b, int fd, int family) {
936 _cleanup_free_ char *bind_name = NULL, *comm = NULL;
937 union sockaddr_union bsa;
938 const char *d = NULL;
939 int r;
940
941 assert(b);
942 assert(fd >= 0);
943
944 /* If this is an AF_UNIX socket, let's set our client's socket address to carry the description
945 * string for this bus connection. This is useful for debugging things, as the connection name is
946 * visible in various socket-related tools, and can even be queried by the server side. */
947
948 if (family != AF_UNIX)
949 return 0;
950
951 (void) sd_bus_get_description(b, &d);
952
953 /* Generate a recognizable source address in the abstract namespace. We'll include:
954 * - a random 64-bit value (to avoid collisions)
955 * - our "comm" process name (suppressed if contains "/" to avoid parsing issues)
956 * - the description string of the bus connection. */
957 (void) pid_get_comm(0, &comm);
958 if (comm && strchr(comm, '/'))
959 comm = mfree(comm);
960
961 if (!d && !comm) /* skip if we don't have either field, rely on kernel autobind instead */
962 return 0;
963
964 if (asprintf(&bind_name, "@%" PRIx64 "/bus/%s/%s", random_u64(), strempty(comm), strempty(d)) < 0)
965 return -ENOMEM;
966
967 strshorten(bind_name, sizeof_field(struct sockaddr_un, sun_path));
968
969 r = sockaddr_un_set_path(&bsa.un, bind_name);
970 if (r < 0)
971 return r;
972
973 if (bind(fd, &bsa.sa, r) < 0)
974 return -errno;
975
976 return 0;
977 }
978
979 static int connect_as(int fd, const struct sockaddr *sa, socklen_t salen, uid_t uid, gid_t gid) {
980 _cleanup_(close_pairp) int pfd[2] = EBADF_PAIR;
981 ssize_t n;
982 int r;
983
984 /* Shortcut if we are not supposed to drop privileges */
985 if (!uid_is_valid(uid) && !gid_is_valid(gid))
986 return RET_NERRNO(connect(fd, sa, salen));
987
988 /* This changes identity to the specified uid/gid and issues connect() as that. This is useful to
989 * make sure SO_PEERCRED reports the selected UID/GID rather than the usual one of the caller. */
990
991 if (pipe2(pfd, O_CLOEXEC) < 0)
992 return -errno;
993
994 r = safe_fork("(sd-setresuid)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL|FORK_WAIT, /* ret_pid= */ NULL);
995 if (r < 0)
996 return r;
997 if (r == 0) {
998 /* child */
999
1000 pfd[0] = safe_close(pfd[0]);
1001
1002 r = RET_NERRNO(setgroups(0, NULL));
1003 if (r < 0)
1004 goto child_finish;
1005
1006 if (gid_is_valid(gid)) {
1007 r = RET_NERRNO(setresgid(gid, gid, gid));
1008 if (r < 0)
1009 goto child_finish;
1010 }
1011
1012 if (uid_is_valid(uid)) {
1013 r = RET_NERRNO(setresuid(uid, uid, uid));
1014 if (r < 0)
1015 goto child_finish;
1016 }
1017
1018 r = RET_NERRNO(connect(fd, sa, salen));
1019 if (r < 0)
1020 goto child_finish;
1021
1022 r = 0;
1023
1024 child_finish:
1025 n = write(pfd[1], &r, sizeof(r));
1026 if (n != sizeof(r))
1027 _exit(EXIT_FAILURE);
1028
1029 _exit(EXIT_SUCCESS);
1030 }
1031
1032 n = read(pfd[0], &r, sizeof(r));
1033 if (n != sizeof(r))
1034 return -EIO;
1035
1036 return r;
1037 }
1038
1039 int bus_socket_connect(sd_bus *b) {
1040 bool inotify_done = false;
1041 int r;
1042
1043 assert(b);
1044
1045 for (;;) {
1046 assert(b->input_fd < 0);
1047 assert(b->output_fd < 0);
1048 assert(b->sockaddr.sa.sa_family != AF_UNSPEC);
1049
1050 if (DEBUG_LOGGING) {
1051 _cleanup_free_ char *pretty = NULL;
1052 (void) sockaddr_pretty(&b->sockaddr.sa, b->sockaddr_size, false, true, &pretty);
1053 log_debug("sd-bus: starting bus%s%s by connecting to %s...",
1054 b->description ? " " : "", strempty(b->description), strnull(pretty));
1055 }
1056
1057 b->input_fd = socket(b->sockaddr.sa.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1058 if (b->input_fd < 0)
1059 return -errno;
1060
1061 r = bind_description(b, b->input_fd, b->sockaddr.sa.sa_family);
1062 if (r < 0)
1063 return r;
1064
1065 b->input_fd = fd_move_above_stdio(b->input_fd);
1066
1067 b->output_fd = b->input_fd;
1068 bus_socket_setup(b);
1069
1070 r = connect_as(b->input_fd, &b->sockaddr.sa, b->sockaddr_size, b->connect_as_uid, b->connect_as_gid);
1071 if (r < 0) {
1072 if (r == -EINPROGRESS) {
1073
1074 /* If we have any inotify watches open, close them now, we don't need them anymore, as
1075 * we have successfully initiated a connection */
1076 bus_close_inotify_fd(b);
1077
1078 /* Note that very likely we are already in BUS_OPENING state here, as we enter it when
1079 * we start parsing the address string. The only reason we set the state explicitly
1080 * here, is to undo BUS_WATCH_BIND, in case we did the inotify magic. */
1081 bus_set_state(b, BUS_OPENING);
1082 return 1;
1083 }
1084
1085 if (IN_SET(r, -ENOENT, -ECONNREFUSED) && /* ENOENT → unix socket doesn't exist at all; ECONNREFUSED → unix socket stale */
1086 b->watch_bind &&
1087 b->sockaddr.sa.sa_family == AF_UNIX &&
1088 b->sockaddr.un.sun_path[0] != 0) {
1089
1090 /* This connection attempt failed, let's release the socket for now, and start with a
1091 * fresh one when reconnecting. */
1092 bus_close_io_fds(b);
1093
1094 if (inotify_done) {
1095 /* inotify set up already, don't do it again, just return now, and remember
1096 * that we are waiting for inotify events now. */
1097 bus_set_state(b, BUS_WATCH_BIND);
1098 return 1;
1099 }
1100
1101 /* This is a file system socket, and the inotify logic is enabled. Let's create the necessary inotify fd. */
1102 r = bus_socket_inotify_setup(b);
1103 if (r < 0)
1104 return r;
1105
1106 /* Let's now try to connect a second time, because in theory there's otherwise a race
1107 * here: the socket might have been created in the time between our first connect() and
1108 * the time we set up the inotify logic. But let's remember that we set up inotify now,
1109 * so that we don't do the connect() more than twice. */
1110 inotify_done = true;
1111
1112 } else
1113 return r;
1114 } else
1115 break;
1116 }
1117
1118 /* Yay, established, we don't need no inotify anymore! */
1119 bus_close_inotify_fd(b);
1120
1121 return bus_socket_start_auth(b);
1122 }
1123
1124 int bus_socket_exec(sd_bus *b) {
1125 int s[2], r;
1126
1127 assert(b);
1128 assert(b->input_fd < 0);
1129 assert(b->output_fd < 0);
1130 assert(b->exec_path);
1131 assert(b->busexec_pid == 0);
1132
1133 if (DEBUG_LOGGING) {
1134 _cleanup_free_ char *line = NULL;
1135
1136 if (b->exec_argv)
1137 line = quote_command_line(b->exec_argv, SHELL_ESCAPE_EMPTY);
1138
1139 log_debug("sd-bus: starting bus%s%s with %s%s",
1140 b->description ? " " : "", strempty(b->description),
1141 line ?: b->exec_path,
1142 b->exec_argv && !line ? "…" : "");
1143 }
1144
1145 r = socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0, s);
1146 if (r < 0)
1147 return -errno;
1148
1149 r = safe_fork_full("(sd-busexec)",
1150 (int[]) { s[1], s[1], STDERR_FILENO },
1151 NULL, 0,
1152 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_REARRANGE_STDIO|FORK_RLIMIT_NOFILE_SAFE, &b->busexec_pid);
1153 if (r < 0) {
1154 safe_close_pair(s);
1155 return r;
1156 }
1157 if (r == 0) {
1158 /* Child */
1159
1160 if (b->exec_argv)
1161 execvp(b->exec_path, b->exec_argv);
1162 else
1163 execvp(b->exec_path, STRV_MAKE(b->exec_path));
1164
1165 _exit(EXIT_FAILURE);
1166 }
1167
1168 safe_close(s[1]);
1169 b->output_fd = b->input_fd = fd_move_above_stdio(s[0]);
1170
1171 bus_socket_setup(b);
1172
1173 return bus_socket_start_auth(b);
1174 }
1175
1176 int bus_socket_take_fd(sd_bus *b) {
1177 assert(b);
1178
1179 bus_socket_setup(b);
1180
1181 return bus_socket_start_auth(b);
1182 }
1183
1184 int bus_socket_write_message(sd_bus *bus, sd_bus_message *m, size_t *idx) {
1185 struct iovec *iov;
1186 ssize_t k;
1187 size_t n;
1188 unsigned j;
1189 int r;
1190
1191 assert(bus);
1192 assert(m);
1193 assert(idx);
1194 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
1195
1196 if (*idx >= BUS_MESSAGE_SIZE(m))
1197 return 0;
1198
1199 r = bus_message_setup_iovec(m);
1200 if (r < 0)
1201 return r;
1202
1203 n = m->n_iovec * sizeof(struct iovec);
1204 iov = newa(struct iovec, n);
1205 memcpy_safe(iov, m->iovec, n);
1206
1207 j = 0;
1208 iovec_advance(iov, &j, *idx);
1209
1210 if (bus->prefer_writev)
1211 k = writev(bus->output_fd, iov, m->n_iovec);
1212 else {
1213 struct msghdr mh = {
1214 .msg_iov = iov,
1215 .msg_iovlen = m->n_iovec,
1216 };
1217
1218 if (m->n_fds > 0 && *idx == 0) {
1219 struct cmsghdr *control;
1220
1221 mh.msg_controllen = CMSG_SPACE(sizeof(int) * m->n_fds);
1222 mh.msg_control = alloca0(mh.msg_controllen);
1223 control = CMSG_FIRSTHDR(&mh);
1224 control->cmsg_len = CMSG_LEN(sizeof(int) * m->n_fds);
1225 control->cmsg_level = SOL_SOCKET;
1226 control->cmsg_type = SCM_RIGHTS;
1227 memcpy(CMSG_DATA(control), m->fds, sizeof(int) * m->n_fds);
1228 }
1229
1230 k = sendmsg(bus->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
1231 if (k < 0 && errno == ENOTSOCK) {
1232 bus->prefer_writev = true;
1233 k = writev(bus->output_fd, iov, m->n_iovec);
1234 }
1235 }
1236
1237 if (k < 0)
1238 return ERRNO_IS_TRANSIENT(errno) ? 0 : -errno;
1239
1240 *idx += (size_t) k;
1241 return 1;
1242 }
1243
1244 static int bus_socket_read_message_need(sd_bus *bus, size_t *need) {
1245 uint32_t a, b;
1246 uint8_t e;
1247 uint64_t sum;
1248
1249 assert(bus);
1250 assert(need);
1251 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
1252
1253 if (bus->rbuffer_size < sizeof(struct bus_header)) {
1254 *need = sizeof(struct bus_header) + 8;
1255
1256 /* Minimum message size:
1257 *
1258 * Header +
1259 *
1260 * Method Call: +2 string headers
1261 * Signal: +3 string headers
1262 * Method Error: +1 string headers
1263 * +1 uint32 headers
1264 * Method Reply: +1 uint32 headers
1265 *
1266 * A string header is at least 9 bytes
1267 * A uint32 header is at least 8 bytes
1268 *
1269 * Hence the minimum message size of a valid message
1270 * is header + 8 bytes */
1271
1272 return 0;
1273 }
1274
1275 a = ((const uint32_t*) bus->rbuffer)[1];
1276 b = ((const uint32_t*) bus->rbuffer)[3];
1277
1278 e = ((const uint8_t*) bus->rbuffer)[0];
1279 if (e == BUS_LITTLE_ENDIAN) {
1280 a = le32toh(a);
1281 b = le32toh(b);
1282 } else if (e == BUS_BIG_ENDIAN) {
1283 a = be32toh(a);
1284 b = be32toh(b);
1285 } else
1286 return -EBADMSG;
1287
1288 sum = (uint64_t) sizeof(struct bus_header) + (uint64_t) ALIGN8(b) + (uint64_t) a;
1289 if (sum >= BUS_MESSAGE_SIZE_MAX)
1290 return -ENOBUFS;
1291
1292 *need = (size_t) sum;
1293 return 0;
1294 }
1295
1296 static int bus_socket_make_message(sd_bus *bus, size_t size) {
1297 sd_bus_message *t = NULL;
1298 void *b;
1299 int r;
1300
1301 assert(bus);
1302 assert(bus->rbuffer_size >= size);
1303 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
1304
1305 r = bus_rqueue_make_room(bus);
1306 if (r < 0)
1307 return r;
1308
1309 if (bus->rbuffer_size > size) {
1310 b = memdup((const uint8_t*) bus->rbuffer + size,
1311 bus->rbuffer_size - size);
1312 if (!b)
1313 return -ENOMEM;
1314 } else
1315 b = NULL;
1316
1317 r = bus_message_from_malloc(bus,
1318 bus->rbuffer, size,
1319 bus->fds, bus->n_fds,
1320 NULL,
1321 &t);
1322 if (r == -EBADMSG) {
1323 log_debug_errno(r, "Received invalid message from connection %s, dropping.", strna(bus->description));
1324 free(bus->rbuffer); /* We want to drop current rbuffer and proceed with whatever remains in b */
1325 } else if (r < 0) {
1326 free(b);
1327 return r;
1328 }
1329
1330 /* rbuffer ownership was either transferred to t, or we got EBADMSG and dropped it. */
1331 bus->rbuffer = b;
1332 bus->rbuffer_size -= size;
1333
1334 bus->fds = NULL;
1335 bus->n_fds = 0;
1336
1337 if (t) {
1338 t->read_counter = ++bus->read_counter;
1339 bus->rqueue[bus->rqueue_size++] = bus_message_ref_queued(t, bus);
1340 sd_bus_message_unref(t);
1341 }
1342
1343 return 1;
1344 }
1345
1346 int bus_socket_read_message(sd_bus *bus) {
1347 struct msghdr mh;
1348 struct iovec iov = {};
1349 ssize_t k;
1350 size_t need;
1351 int r;
1352 void *b;
1353 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int) * BUS_FDS_MAX)) control;
1354 bool handle_cmsg = false;
1355
1356 assert(bus);
1357 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
1358
1359 r = bus_socket_read_message_need(bus, &need);
1360 if (r < 0)
1361 return r;
1362
1363 if (bus->rbuffer_size >= need)
1364 return bus_socket_make_message(bus, need);
1365
1366 b = realloc(bus->rbuffer, need);
1367 if (!b)
1368 return -ENOMEM;
1369
1370 bus->rbuffer = b;
1371
1372 iov = IOVEC_MAKE((uint8_t *)bus->rbuffer + bus->rbuffer_size, need - bus->rbuffer_size);
1373
1374 if (bus->prefer_readv) {
1375 k = readv(bus->input_fd, &iov, 1);
1376 if (k < 0)
1377 k = -errno;
1378 } else {
1379 mh = (struct msghdr) {
1380 .msg_iov = &iov,
1381 .msg_iovlen = 1,
1382 .msg_control = &control,
1383 .msg_controllen = sizeof(control),
1384 };
1385
1386 k = recvmsg_safe(bus->input_fd, &mh, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
1387 if (k == -ENOTSOCK) {
1388 bus->prefer_readv = true;
1389 k = readv(bus->input_fd, &iov, 1);
1390 if (k < 0)
1391 k = -errno;
1392 } else
1393 handle_cmsg = true;
1394 }
1395 if (ERRNO_IS_NEG_TRANSIENT(k))
1396 return 0;
1397 if (k < 0)
1398 return (int) k;
1399 if (k == 0) {
1400 if (handle_cmsg)
1401 cmsg_close_all(&mh); /* On EOF we shouldn't have gotten an fd, but let's make sure */
1402 return -ECONNRESET;
1403 }
1404
1405 bus->rbuffer_size += k;
1406
1407 if (handle_cmsg) {
1408 struct cmsghdr *cmsg;
1409
1410 CMSG_FOREACH(cmsg, &mh)
1411 if (cmsg->cmsg_level == SOL_SOCKET &&
1412 cmsg->cmsg_type == SCM_RIGHTS) {
1413 int n, *f, i;
1414
1415 n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
1416
1417 if (!bus->can_fds) {
1418 /* Whut? We received fds but this
1419 * isn't actually enabled? Close them,
1420 * and fail */
1421
1422 close_many(CMSG_TYPED_DATA(cmsg, int), n);
1423 return -EIO;
1424 }
1425
1426 f = reallocarray(bus->fds, bus->n_fds + n, sizeof(int));
1427 if (!f) {
1428 close_many(CMSG_TYPED_DATA(cmsg, int), n);
1429 return -ENOMEM;
1430 }
1431
1432 for (i = 0; i < n; i++)
1433 f[bus->n_fds++] = fd_move_above_stdio(CMSG_TYPED_DATA(cmsg, int)[i]);
1434 bus->fds = f;
1435 } else
1436 log_debug("Got unexpected auxiliary data with level=%d and type=%d",
1437 cmsg->cmsg_level, cmsg->cmsg_type);
1438 }
1439
1440 r = bus_socket_read_message_need(bus, &need);
1441 if (r < 0)
1442 return r;
1443
1444 if (bus->rbuffer_size >= need)
1445 return bus_socket_make_message(bus, need);
1446
1447 return 1;
1448 }
1449
1450 int bus_socket_process_opening(sd_bus *b) {
1451 int error = 0, events, r;
1452 socklen_t slen = sizeof(error);
1453
1454 assert(b->state == BUS_OPENING);
1455
1456 events = fd_wait_for_event(b->output_fd, POLLOUT, 0);
1457 if (ERRNO_IS_NEG_TRANSIENT(events))
1458 return 0;
1459 if (events < 0)
1460 return events;
1461 if (!(events & (POLLOUT|POLLERR|POLLHUP)))
1462 return 0;
1463
1464 r = getsockopt(b->output_fd, SOL_SOCKET, SO_ERROR, &error, &slen);
1465 if (r < 0)
1466 b->last_connect_error = errno;
1467 else if (error != 0)
1468 b->last_connect_error = error;
1469 else if (events & (POLLERR|POLLHUP))
1470 b->last_connect_error = ECONNREFUSED;
1471 else
1472 return bus_socket_start_auth(b);
1473
1474 return bus_next_address(b);
1475 }
1476
1477 int bus_socket_process_authenticating(sd_bus *b) {
1478 int r;
1479
1480 assert(b);
1481 assert(b->state == BUS_AUTHENTICATING);
1482
1483 if (now(CLOCK_MONOTONIC) >= b->auth_timeout)
1484 return -ETIMEDOUT;
1485
1486 r = bus_socket_write_auth(b);
1487 if (r != 0)
1488 return r;
1489
1490 return bus_socket_read_auth(b);
1491 }
1492
1493 int bus_socket_process_watch_bind(sd_bus *b) {
1494 int r, q;
1495
1496 assert(b);
1497 assert(b->state == BUS_WATCH_BIND);
1498 assert(b->inotify_fd >= 0);
1499
1500 r = flush_fd(b->inotify_fd);
1501 if (r <= 0)
1502 return r;
1503
1504 log_debug("Got inotify event on bus %s.", strna(b->description));
1505
1506 /* We flushed events out of the inotify fd. In that case, maybe the socket is valid now? Let's try to connect
1507 * to it again */
1508
1509 r = bus_socket_connect(b);
1510 if (r < 0)
1511 return r;
1512
1513 q = bus_attach_io_events(b);
1514 if (q < 0)
1515 return q;
1516
1517 q = bus_attach_inotify_event(b);
1518 if (q < 0)
1519 return q;
1520
1521 return r;
1522 }