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