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