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