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