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