]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-bus/bus-socket.c
process-util: add new FORK_RLIMIT_NOFILE_SAFE flag for safe_fork()
[thirdparty/systemd.git] / src / libsystemd / sd-bus / bus-socket.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
a7e3212d
LP
2
3#include <endian.h>
07630cea 4#include <poll.h>
a7e3212d
LP
5#include <stdlib.h>
6#include <unistd.h>
a7e3212d 7
07630cea 8#include "sd-bus.h"
24882e06 9#include "sd-daemon.h"
07630cea 10
b5efdb8a 11#include "alloc-util.h"
07630cea
LP
12#include "bus-internal.h"
13#include "bus-message.h"
3ffd4af2
LP
14#include "bus-socket.h"
15#include "fd-util.h"
f97b34a6 16#include "format-util.h"
8a5cd31e 17#include "fs-util.h"
15a5e950 18#include "hexdecoct.h"
8a5cd31e 19#include "io-util.h"
a7e3212d
LP
20#include "macro.h"
21#include "missing.h"
8a5cd31e 22#include "path-util.h"
dccca82b 23#include "process-util.h"
7fc04b12 24#include "selinux-util.h"
24882e06 25#include "signal-util.h"
15a5e950 26#include "stdio-util.h"
07630cea 27#include "string-util.h"
b1d4f8e1 28#include "user-util.h"
07630cea
LP
29#include "utf8.h"
30#include "util.h"
a7e3212d 31
aec6d91f
LP
32#define SNDBUF_SIZE (8*1024*1024)
33
2181a7f5 34static void iovec_advance(struct iovec iov[], unsigned *idx, size_t size) {
a7e3212d
LP
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
5cfa2c3d 47 *i = IOVEC_MAKE(NULL, 0);
a7e3212d 48
313cefa1 49 (*idx)++;
a7e3212d
LP
50 }
51}
52
bc7fd8cd 53static int append_iovec(sd_bus_message *m, const void *p, size_t sz) {
2100fa10
LP
54 assert(m);
55 assert(p);
56 assert(sz > 0);
57
5cfa2c3d 58 m->iovec[m->n_iovec++] = IOVEC_MAKE((void*) p, sz);
bc7fd8cd
LP
59
60 return 0;
2100fa10
LP
61}
62
bc7fd8cd
LP
63static int bus_message_setup_iovec(sd_bus_message *m) {
64 struct bus_body_part *part;
13c299d3 65 unsigned n, i;
bc7fd8cd
LP
66 int r;
67
2100fa10
LP
68 assert(m);
69 assert(m->sealed);
70
71 if (m->n_iovec > 0)
bc7fd8cd
LP
72 return 0;
73
74 assert(!m->iovec);
2100fa10 75
c91cb83c 76 n = 1 + m->n_body_parts;
bc7fd8cd
LP
77 if (n < ELEMENTSOF(m->iovec_fixed))
78 m->iovec = m->iovec_fixed;
79 else {
80 m->iovec = new(struct iovec, n);
66b26c5c
LP
81 if (!m->iovec) {
82 r = -ENOMEM;
83 goto fail;
84 }
bc7fd8cd 85 }
2100fa10 86
c91cb83c 87 r = append_iovec(m, m->header, BUS_MESSAGE_BODY_BEGIN(m));
bc7fd8cd 88 if (r < 0)
66b26c5c 89 goto fail;
2100fa10 90
9b29bb68 91 MESSAGE_FOREACH_PART(part, i, m) {
66b26c5c
LP
92 r = bus_body_part_map(part);
93 if (r < 0)
94 goto fail;
95
bc7fd8cd
LP
96 r = append_iovec(m, part->data, part->size);
97 if (r < 0)
66b26c5c 98 goto fail;
bc7fd8cd
LP
99 }
100
101 assert(n == m->n_iovec);
102
103 return 0;
66b26c5c
LP
104
105fail:
106 m->poisoned = true;
107 return r;
2100fa10
LP
108}
109
2181a7f5
LP
110bool 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
a7e3212d 127static int bus_socket_write_auth(sd_bus *b) {
a7e3212d
LP
128 ssize_t k;
129
130 assert(b);
131 assert(b->state == BUS_AUTHENTICATING);
132
2181a7f5 133 if (!bus_socket_auth_needs_write(b))
a7e3212d
LP
134 return 0;
135
15d5af81
LP
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 }
a7e3212d 151
a7e3212d
LP
152 if (k < 0)
153 return errno == EAGAIN ? 0 : -errno;
154
155 iovec_advance(b->auth_iovec, &b->auth_index, (size_t) k);
a7e3212d
LP
156 return 1;
157}
158
2181a7f5 159static int bus_socket_auth_verify_client(sd_bus *b) {
a7e3212d
LP
160 char *e, *f, *start;
161 sd_id128_t peer;
162 unsigned i;
163 int r;
164
2181a7f5
LP
165 assert(b);
166
a7e3212d
LP
167 /* We expect two response lines: "OK" and possibly
168 * "AGREE_UNIX_FD" */
169
6e6c21c8 170 e = memmem_safe(b->rbuffer, b->rbuffer_size, "\r\n", 2);
a7e3212d
LP
171 if (!e)
172 return 0;
173
c7db1984 174 if (b->accept_fd) {
a7e3212d
LP
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
2181a7f5
LP
194 b->auth = b->anonymous_auth ? BUS_AUTH_ANONYMOUS : BUS_AUTH_EXTERNAL;
195
a7e3212d
LP
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
3bbaff3e 208 if (!sd_id128_is_null(b->server_id) &&
98178d39 209 !sd_id128_equal(b->server_id, peer))
a7e3212d
LP
210 return -EPERM;
211
98178d39 212 b->server_id = peer;
a7e3212d
LP
213
214 /* And possibly check the second line, too */
215
216 if (f)
217 b->can_fds =
fbd0b64f
LP
218 (f - e == STRLEN("\r\nAGREE_UNIX_FD")) &&
219 memcmp(e + 2, "AGREE_UNIX_FD",
220 STRLEN("AGREE_UNIX_FD")) == 0;
a7e3212d
LP
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
2181a7f5
LP
232static 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
242static bool line_begins(const char *s, size_t m, const char *word) {
d27b725a 243 const char *p;
2181a7f5 244
d27b725a 245 p = memory_startswith(s, m, word);
3f10c662 246 return p && (p == (s + m) || *p == ' ');
2181a7f5
LP
247}
248
249static int verify_anonymous_token(sd_bus *b, const char *p, size_t l) {
250 _cleanup_free_ char *token = NULL;
30494563
TG
251 size_t len;
252 int r;
2181a7f5
LP
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;
2181a7f5 265
30494563
TG
266 r = unhexmem(p, l, (void **) &token, &len);
267 if (r < 0)
268 return 0;
269
270 if (memchr(token, 0, len))
2181a7f5
LP
271 return 0;
272
273 return !!utf8_is_valid(token);
274}
275
276static int verify_external_token(sd_bus *b, const char *p, size_t l) {
277 _cleanup_free_ char *token = NULL;
30494563 278 size_t len;
2181a7f5
LP
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
8411d2a2 286 if (!b->anonymous_auth && !b->ucred_valid)
2181a7f5
LP
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
30494563
TG
298 r = unhexmem(p, l, (void**) &token, &len);
299 if (r < 0)
300 return 0;
2181a7f5 301
30494563 302 if (memchr(token, 0, len))
2181a7f5
LP
303 return 0;
304
305 r = parse_uid(token, &u);
306 if (r < 0)
307 return 0;
308
8411d2a2
LP
309 /* We ignore the passed value if anonymous authentication is
310 * on anyway. */
311 if (!b->anonymous_auth && u != b->ucred.uid)
2181a7f5
LP
312 return 0;
313
314 return 1;
315}
316
317static 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 */
945c2931 325 assert(IN_SET(b->auth_index, 0, 1));
2181a7f5
LP
326
327 l = strlen(t);
328 p = malloc(b->auth_iovec[0].iov_len + l);
329 if (!p)
330 return -ENOMEM;
331
75f32f04 332 memcpy_safe(p, b->auth_iovec[0].iov_base, b->auth_iovec[0].iov_len);
2181a7f5
LP
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
344static int bus_socket_auth_write_ok(sd_bus *b) {
345 char t[3 + 32 + 2 + 1];
346
347 assert(b);
348
5ffa8c81 349 xsprintf(t, "OK " SD_ID128_FORMAT_STR "\r\n", SD_ID128_FORMAT_VAL(b->server_id));
2181a7f5
LP
350
351 return bus_socket_auth_write(b, t);
352}
353
354static 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
2b4ac889 363 if (b->rbuffer_size < 1)
2181a7f5
LP
364 return 0;
365
366 /* First char must be a NUL byte */
367 if (*(char*) b->rbuffer != 0)
368 return -EIO;
369
2b4ac889
LP
370 if (b->rbuffer_size < 3)
371 return 0;
372
2181a7f5
LP
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")) {
c7db1984 455 if (b->auth == _BUS_AUTH_INVALID || !b->accept_fd)
2181a7f5
LP
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
473static 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
a7e3212d
LP
482static int bus_socket_read_auth(sd_bus *b) {
483 struct msghdr mh;
7f4e6a1c 484 struct iovec iov = {};
a7e3212d
LP
485 size_t n;
486 ssize_t k;
487 int r;
488 void *p;
2181a7f5
LP
489 union {
490 struct cmsghdr cmsghdr;
d868f2a3 491 uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX)];
2181a7f5 492 } control;
15d5af81 493 bool handle_cmsg = false;
a7e3212d
LP
494
495 assert(b);
2181a7f5 496 assert(b->state == BUS_AUTHENTICATING);
a7e3212d
LP
497
498 r = bus_socket_auth_verify(b);
499 if (r != 0)
500 return r;
501
9607d947 502 n = MAX(256u, b->rbuffer_size * 2);
a7e3212d
LP
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
5cfa2c3d 516 iov = IOVEC_MAKE((uint8_t *)b->rbuffer + b->rbuffer_size, n - b->rbuffer_size);
a7e3212d 517
15d5af81
LP
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
a6887cc0 527 k = recvmsg(b->input_fd, &mh, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
15d5af81
LP
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 }
a7e3212d
LP
534 if (k < 0)
535 return errno == EAGAIN ? 0 : -errno;
536 if (k == 0)
537 return -ECONNRESET;
538
539 b->rbuffer_size += k;
540
2a1288ff
LP
541 if (handle_cmsg) {
542 struct cmsghdr *cmsg;
543
544 CMSG_FOREACH(cmsg, &mh)
15d5af81
LP
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;
d868f2a3
ZJS
555 } else
556 log_debug("Got unexpected auxiliary data with level=%d and type=%d",
557 cmsg->cmsg_level, cmsg->cmsg_type);
2a1288ff 558 }
2181a7f5 559
a7e3212d
LP
560 r = bus_socket_auth_verify(b);
561 if (r != 0)
562 return r;
563
564 return 1;
565}
566
8f04d2eb 567void bus_socket_setup(sd_bus *b) {
a7e3212d
LP
568 assert(b);
569
aec6d91f 570 /* Increase the buffers to 8 MB */
6ae22ffb
LP
571 (void) fd_inc_rcvbuf(b->input_fd, SNDBUF_SIZE);
572 (void) fd_inc_sndbuf(b->output_fd, SNDBUF_SIZE);
a7e3212d 573
e1d337d4 574 b->message_version = 1;
0f437184 575 b->message_endian = 0;
8f04d2eb 576}
e1d337d4 577
8f04d2eb 578static void bus_get_peercred(sd_bus *b) {
c4e6556c
ZJS
579 int r;
580
8f04d2eb 581 assert(b);
18ac4643
LP
582 assert(!b->ucred_valid);
583 assert(!b->label);
584 assert(b->n_groups == (size_t) -1);
8f04d2eb
LP
585
586 /* Get the peer for socketpair() sockets */
eff05270 587 b->ucred_valid = getpeercred(b->input_fd, &b->ucred) >= 0;
c4e6556c
ZJS
588
589 /* Get the SELinux context of the peer */
db7d1dca 590 r = getpeersec(b->input_fd, &b->label);
18ac4643 591 if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT))
db7d1dca 592 log_debug_errno(r, "Failed to determine peer security context: %m");
18ac4643
LP
593
594 /* Get the list of auxiliary groups of the peer */
595 r = getpeergroups(b->input_fd, &b->groups);
c599b325 596 if (r >= 0)
18ac4643 597 b->n_groups = (size_t) r;
c599b325
ZJS
598 else if (!IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT))
599 log_debug_errno(r, "Failed to determine peer's group list: %m");
a7e3212d
LP
600}
601
2181a7f5 602static int bus_socket_start_auth_client(sd_bus *b) {
a7e3212d 603 size_t l;
2181a7f5 604 const char *auth_suffix, *auth_prefix;
a7e3212d
LP
605
606 assert(b);
607
2181a7f5
LP
608 if (b->anonymous_auth) {
609 auth_prefix = "\0AUTH ANONYMOUS ";
a7e3212d 610
2181a7f5
LP
611 /* For ANONYMOUS auth we send some arbitrary "trace" string */
612 l = 9;
613 b->auth_buffer = hexmem("anonymous", l);
614 } else {
5ffa8c81 615 char text[DECIMAL_STR_MAX(uid_t) + 1];
a7e3212d 616
2181a7f5 617 auth_prefix = "\0AUTH EXTERNAL ";
a7e3212d 618
5ffa8c81 619 xsprintf(text, UID_FMT, geteuid());
a7e3212d 620
2181a7f5
LP
621 l = strlen(text);
622 b->auth_buffer = hexmem(text, l);
623 }
624
625 if (!b->auth_buffer)
a7e3212d
LP
626 return -ENOMEM;
627
c7db1984 628 if (b->accept_fd)
2181a7f5
LP
629 auth_suffix = "\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n";
630 else
631 auth_suffix = "\r\nBEGIN\r\n";
a7e3212d 632
5cfa2c3d
LP
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);
a7e3212d
LP
636
637 return bus_socket_write_auth(b);
638}
639
a7893c6b 640int bus_socket_start_auth(sd_bus *b) {
2181a7f5
LP
641 assert(b);
642
8f04d2eb
LP
643 bus_get_peercred(b);
644
3e0e196e 645 bus_set_state(b, BUS_AUTHENTICATING);
036d61b3 646 b->auth_timeout = now(CLOCK_MONOTONIC) + BUS_AUTH_TIMEOUT;
2181a7f5 647
9ab32f9d 648 if (sd_is_socket(b->input_fd, AF_UNIX, 0, 0) <= 0)
c7db1984 649 b->accept_fd = false;
2181a7f5 650
9ab32f9d
LP
651 if (b->output_fd != b->input_fd)
652 if (sd_is_socket(b->output_fd, AF_UNIX, 0, 0) <= 0)
c7db1984 653 b->accept_fd = false;
e82c9509 654
2181a7f5
LP
655 if (b->is_server)
656 return bus_socket_read_auth(b);
657 else
658 return bus_socket_start_auth_client(b);
659}
660
8a5cd31e
LP
661static 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;
7fe2903c
LP
685
686 b->inotify_fd = fd_move_above_stdio(b->inotify_fd);
8a5cd31e
LP
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
945403e6 762 r = log_debug_errno(errno, "Failed to add inotify watch on %s: %m", empty_to_root(prefix));
8a5cd31e
LP
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
833fail:
834 bus_close_inotify_fd(b);
835 return r;
836}
837
a7e3212d 838int bus_socket_connect(sd_bus *b) {
8a5cd31e 839 bool inotify_done = false;
a7e3212d
LP
840 int r;
841
842 assert(b);
a7e3212d 843
8a5cd31e
LP
844 for (;;) {
845 assert(b->input_fd < 0);
846 assert(b->output_fd < 0);
847 assert(b->sockaddr.sa.sa_family != AF_UNSPEC);
a7e3212d 848
8a5cd31e
LP
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;
e82c9509 852
7fe2903c
LP
853 b->input_fd = fd_move_above_stdio(b->input_fd);
854
8a5cd31e
LP
855 b->output_fd = b->input_fd;
856 bus_socket_setup(b);
a7e3212d 857
8a5cd31e
LP
858 if (connect(b->input_fd, &b->sockaddr.sa, b->sockaddr_size) < 0) {
859 if (errno == EINPROGRESS) {
a7e3212d 860
8a5cd31e
LP
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. */
3e0e196e 868 bus_set_state(b, BUS_OPENING);
8a5cd31e
LP
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. */
3e0e196e 884 bus_set_state(b, BUS_WATCH_BIND);
8a5cd31e
LP
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;
a7e3212d
LP
903 }
904
8a5cd31e
LP
905 /* Yay, established, we don't need no inotify anymore! */
906 bus_close_inotify_fd(b);
907
a7e3212d
LP
908 return bus_socket_start_auth(b);
909}
910
911int bus_socket_exec(sd_bus *b) {
e82c9509 912 int s[2], r;
a7e3212d
LP
913
914 assert(b);
e82c9509
LP
915 assert(b->input_fd < 0);
916 assert(b->output_fd < 0);
a7e3212d 917 assert(b->exec_path);
392cf1d0 918 assert(b->busexec_pid == 0);
a7e3212d 919
e82c9509
LP
920 r = socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0, s);
921 if (r < 0)
a7e3212d
LP
922 return -errno;
923
392cf1d0 924 r = safe_fork_full("(sd-busexec)", s+1, 1, FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, &b->busexec_pid);
4c253ed1 925 if (r < 0) {
3d94f76c 926 safe_close_pair(s);
4c253ed1 927 return r;
a7e3212d 928 }
4c253ed1 929 if (r == 0) {
a7e3212d
LP
930 /* Child */
931
2b33ab09
LP
932 if (rearrange_stdio(s[1], s[1], STDERR_FILENO) < 0)
933 _exit(EXIT_FAILURE);
a7e3212d 934
a7e3212d
LP
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
03e334a1 945 safe_close(s[1]);
7fe2903c 946 b->output_fd = b->input_fd = fd_move_above_stdio(s[0]);
a7e3212d 947
8f04d2eb 948 bus_socket_setup(b);
e1d337d4 949
a7e3212d
LP
950 return bus_socket_start_auth(b);
951}
952
953int bus_socket_take_fd(sd_bus *b) {
a7e3212d
LP
954 assert(b);
955
8f04d2eb 956 bus_socket_setup(b);
a7e3212d
LP
957
958 return bus_socket_start_auth(b);
959}
960
961int bus_socket_write_message(sd_bus *bus, sd_bus_message *m, size_t *idx) {
a7e3212d
LP
962 struct iovec *iov;
963 ssize_t k;
964 size_t n;
965 unsigned j;
bc7fd8cd 966 int r;
a7e3212d
LP
967
968 assert(bus);
969 assert(m);
970 assert(idx);
945c2931 971 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
a7e3212d 972
6629161f 973 if (*idx >= BUS_MESSAGE_SIZE(m))
a7e3212d 974 return 0;
a7e3212d 975
bc7fd8cd
LP
976 r = bus_message_setup_iovec(m);
977 if (r < 0)
978 return r;
2100fa10 979
a7e3212d
LP
980 n = m->n_iovec * sizeof(struct iovec);
981 iov = alloca(n);
75f32f04 982 memcpy_safe(iov, m->iovec, n);
a7e3212d
LP
983
984 j = 0;
985 iovec_advance(iov, &j, *idx);
986
15d5af81
LP
987 if (bus->prefer_writev)
988 k = writev(bus->output_fd, iov, m->n_iovec);
989 else {
7f4e6a1c
ZJS
990 struct msghdr mh = {
991 .msg_iov = iov,
992 .msg_iovlen = m->n_iovec,
993 };
15d5af81 994
f29eef2e 995 if (m->n_fds > 0 && *idx == 0) {
15d5af81 996 struct cmsghdr *control;
15d5af81 997
7f4e6a1c
ZJS
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);
15d5af81
LP
1000 control->cmsg_level = SOL_SOCKET;
1001 control->cmsg_type = SCM_RIGHTS;
15d5af81
LP
1002 memcpy(CMSG_DATA(control), m->fds, sizeof(int) * m->n_fds);
1003 }
1004
15d5af81
LP
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 }
a7e3212d 1011
a7e3212d
LP
1012 if (k < 0)
1013 return errno == EAGAIN ? 0 : -errno;
1014
1015 *idx += (size_t) k;
1016 return 1;
1017}
1018
1019static 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);
945c2931 1026 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
a7e3212d
LP
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];
0461f8cd 1054 if (e == BUS_LITTLE_ENDIAN) {
a7e3212d
LP
1055 a = le32toh(a);
1056 b = le32toh(b);
0461f8cd 1057 } else if (e == BUS_BIG_ENDIAN) {
a7e3212d
LP
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
7d22c717 1071static int bus_socket_make_message(sd_bus *bus, size_t size) {
a7e3212d
LP
1072 sd_bus_message *t;
1073 void *b;
1074 int r;
1075
1076 assert(bus);
a7e3212d 1077 assert(bus->rbuffer_size >= size);
945c2931 1078 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
a7e3212d 1079
7adc46fc
LP
1080 r = bus_rqueue_make_room(bus);
1081 if (r < 0)
1082 return r;
1083
a7e3212d
LP
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
df2d202e
LP
1092 r = bus_message_from_malloc(bus,
1093 bus->rbuffer, size,
a7e3212d 1094 bus->fds, bus->n_fds,
038f9863 1095 NULL,
a7e3212d
LP
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
7adc46fc 1108 bus->rqueue[bus->rqueue_size++] = t;
7d22c717 1109
a7e3212d
LP
1110 return 1;
1111}
1112
7d22c717 1113int bus_socket_read_message(sd_bus *bus) {
a7e3212d 1114 struct msghdr mh;
7f4e6a1c 1115 struct iovec iov = {};
a7e3212d
LP
1116 ssize_t k;
1117 size_t need;
1118 int r;
1119 void *b;
1120 union {
1121 struct cmsghdr cmsghdr;
d868f2a3 1122 uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX)];
a7e3212d 1123 } control;
4d3a5b10 1124 bool handle_cmsg = false;
a7e3212d
LP
1125
1126 assert(bus);
945c2931 1127 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
a7e3212d
LP
1128
1129 r = bus_socket_read_message_need(bus, &need);
1130 if (r < 0)
1131 return r;
1132
1133 if (bus->rbuffer_size >= need)
7d22c717 1134 return bus_socket_make_message(bus, need);
a7e3212d
LP
1135
1136 b = realloc(bus->rbuffer, need);
1137 if (!b)
1138 return -ENOMEM;
1139
1140 bus->rbuffer = b;
1141
5cfa2c3d 1142 iov = IOVEC_MAKE((uint8_t *)bus->rbuffer + bus->rbuffer_size, need - bus->rbuffer_size);
a7e3212d 1143
15d5af81
LP
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
a6887cc0 1153 k = recvmsg(bus->input_fd, &mh, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
15d5af81
LP
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 }
a7e3212d
LP
1160 if (k < 0)
1161 return errno == EAGAIN ? 0 : -errno;
1162 if (k == 0)
1163 return -ECONNRESET;
1164
1165 bus->rbuffer_size += k;
1166
2a1288ff
LP
1167 if (handle_cmsg) {
1168 struct cmsghdr *cmsg;
1169
1170 CMSG_FOREACH(cmsg, &mh)
15d5af81
LP
1171 if (cmsg->cmsg_level == SOL_SOCKET &&
1172 cmsg->cmsg_type == SCM_RIGHTS) {
7fe2903c 1173 int n, *f, i;
15d5af81
LP
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
62d74c78 1186 f = reallocarray(bus->fds, bus->n_fds + n, sizeof(int));
15d5af81
LP
1187 if (!f) {
1188 close_many((int*) CMSG_DATA(cmsg), n);
1189 return -ENOMEM;
1190 }
1191
7fe2903c
LP
1192 for (i = 0; i < n; i++)
1193 f[bus->n_fds++] = fd_move_above_stdio(((int*) CMSG_DATA(cmsg))[i]);
15d5af81 1194 bus->fds = f;
d868f2a3
ZJS
1195 } else
1196 log_debug("Got unexpected auxiliary data with level=%d and type=%d",
1197 cmsg->cmsg_level, cmsg->cmsg_type);
2a1288ff 1198 }
a7e3212d
LP
1199
1200 r = bus_socket_read_message_need(bus, &need);
1201 if (r < 0)
1202 return r;
1203
1204 if (bus->rbuffer_size >= need)
7d22c717 1205 return bus_socket_make_message(bus, need);
a7e3212d
LP
1206
1207 return 1;
1208}
1209
1210int bus_socket_process_opening(sd_bus *b) {
1211 int error = 0;
1212 socklen_t slen = sizeof(error);
b92bea5d
ZJS
1213 struct pollfd p = {
1214 .fd = b->output_fd,
1215 .events = POLLOUT,
1216 };
a7e3212d
LP
1217 int r;
1218
a7e3212d
LP
1219 assert(b->state == BUS_OPENING);
1220
a7e3212d
LP
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
e82c9509 1228 r = getsockopt(b->output_fd, SOL_SOCKET, SO_ERROR, &error, &slen);
a7e3212d
LP
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
1241int 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}
8a5cd31e
LP
1256
1257int 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}