]>
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 { | |
fc772c61 LP |
506 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control = {}; |
507 | ||
4e2baf2f MV |
508 | struct msghdr mh = { |
509 | .msg_iov = b->auth_iovec + b->auth_index, | |
510 | .msg_iovlen = ELEMENTSOF(b->auth_iovec) - b->auth_index, | |
511 | }; | |
512 | ||
fc772c61 LP |
513 | if (uid_is_valid(b->connect_as_uid) || gid_is_valid(b->connect_as_gid)) { |
514 | ||
515 | /* If we shall connect under some specific UID/GID, then synthesize an | |
516 | * SCM_CREDENTIALS record accordingly. After all we want to adopt this UID/GID both | |
517 | * for SO_PEERCRED (where we have to fork()) and SCM_CREDENTIALS (where we can just | |
518 | * fake it via sendmsg()) */ | |
519 | ||
520 | struct ucred ucred = { | |
521 | .pid = getpid_cached(), | |
522 | .uid = uid_is_valid(b->connect_as_uid) ? b->connect_as_uid : getuid(), | |
523 | .gid = gid_is_valid(b->connect_as_gid) ? b->connect_as_gid : getgid(), | |
524 | }; | |
525 | ||
526 | mh.msg_control = &control; | |
527 | mh.msg_controllen = sizeof(control); | |
528 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(&mh); | |
529 | *cmsg = (struct cmsghdr) { | |
530 | .cmsg_level = SOL_SOCKET, | |
531 | .cmsg_type = SCM_CREDENTIALS, | |
532 | .cmsg_len = CMSG_LEN(sizeof(struct ucred)), | |
533 | }; | |
534 | ||
535 | memcpy(CMSG_DATA(cmsg), &ucred, sizeof(struct ucred)); | |
536 | } | |
537 | ||
4e2baf2f MV |
538 | k = sendmsg(b->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL); |
539 | if (k < 0 && errno == ENOTSOCK) { | |
540 | b->prefer_writev = true; | |
541 | k = writev(b->output_fd, b->auth_iovec + b->auth_index, ELEMENTSOF(b->auth_iovec) - b->auth_index); | |
542 | } | |
543 | } | |
544 | ||
545 | if (k < 0) | |
546 | return ERRNO_IS_TRANSIENT(errno) ? 0 : -errno; | |
547 | ||
548 | iovec_advance(b->auth_iovec, &b->auth_index, (size_t) k); | |
549 | ||
550 | /* Now crank the state machine since we might be able to make progress after writing. For example, | |
551 | * the server only processes "BEGIN" when the write buffer is empty. | |
552 | */ | |
553 | return bus_socket_auth_verify(b); | |
554 | } | |
555 | ||
a7e3212d LP |
556 | static int bus_socket_read_auth(sd_bus *b) { |
557 | struct msghdr mh; | |
7f4e6a1c | 558 | struct iovec iov = {}; |
a7e3212d LP |
559 | size_t n; |
560 | ssize_t k; | |
561 | int r; | |
562 | void *p; | |
fb29cdbe | 563 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int) * BUS_FDS_MAX)) control; |
15d5af81 | 564 | bool handle_cmsg = false; |
a7e3212d LP |
565 | |
566 | assert(b); | |
2181a7f5 | 567 | assert(b->state == BUS_AUTHENTICATING); |
a7e3212d LP |
568 | |
569 | r = bus_socket_auth_verify(b); | |
570 | if (r != 0) | |
571 | return r; | |
572 | ||
9607d947 | 573 | n = MAX(256u, b->rbuffer_size * 2); |
a7e3212d LP |
574 | |
575 | if (n > BUS_AUTH_SIZE_MAX) | |
576 | n = BUS_AUTH_SIZE_MAX; | |
577 | ||
578 | if (b->rbuffer_size >= n) | |
579 | return -ENOBUFS; | |
580 | ||
581 | p = realloc(b->rbuffer, n); | |
582 | if (!p) | |
583 | return -ENOMEM; | |
584 | ||
585 | b->rbuffer = p; | |
586 | ||
5cfa2c3d | 587 | iov = IOVEC_MAKE((uint8_t *)b->rbuffer + b->rbuffer_size, n - b->rbuffer_size); |
a7e3212d | 588 | |
c1093c34 | 589 | if (b->prefer_readv) { |
15d5af81 | 590 | k = readv(b->input_fd, &iov, 1); |
c1093c34 LP |
591 | if (k < 0) |
592 | k = -errno; | |
593 | } else { | |
41ab8c67 LP |
594 | mh = (struct msghdr) { |
595 | .msg_iov = &iov, | |
596 | .msg_iovlen = 1, | |
597 | .msg_control = &control, | |
598 | .msg_controllen = sizeof(control), | |
599 | }; | |
15d5af81 | 600 | |
3691bcf3 LP |
601 | k = recvmsg_safe(b->input_fd, &mh, MSG_DONTWAIT|MSG_CMSG_CLOEXEC); |
602 | if (k == -ENOTSOCK) { | |
15d5af81 LP |
603 | b->prefer_readv = true; |
604 | k = readv(b->input_fd, &iov, 1); | |
3691bcf3 LP |
605 | if (k < 0) |
606 | k = -errno; | |
15d5af81 LP |
607 | } else |
608 | handle_cmsg = true; | |
609 | } | |
c5ed77b2 ZJS |
610 | if (ERRNO_IS_NEG_TRANSIENT(k)) |
611 | return 0; | |
612 | if (k < 0) | |
3691bcf3 LP |
613 | return (int) k; |
614 | if (k == 0) { | |
615 | if (handle_cmsg) | |
616 | cmsg_close_all(&mh); /* paranoia, we shouldn't have gotten any fds on EOF */ | |
a7e3212d | 617 | return -ECONNRESET; |
3691bcf3 | 618 | } |
a7e3212d LP |
619 | |
620 | b->rbuffer_size += k; | |
621 | ||
2a1288ff LP |
622 | if (handle_cmsg) { |
623 | struct cmsghdr *cmsg; | |
624 | ||
625 | CMSG_FOREACH(cmsg, &mh) | |
15d5af81 LP |
626 | if (cmsg->cmsg_level == SOL_SOCKET && |
627 | cmsg->cmsg_type == SCM_RIGHTS) { | |
628 | int j; | |
629 | ||
630 | /* Whut? We received fds during the auth | |
631 | * protocol? Somebody is playing games with | |
632 | * us. Close them all, and fail */ | |
633 | j = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); | |
b1d02191 | 634 | close_many(CMSG_TYPED_DATA(cmsg, int), j); |
15d5af81 | 635 | return -EIO; |
d868f2a3 ZJS |
636 | } else |
637 | log_debug("Got unexpected auxiliary data with level=%d and type=%d", | |
638 | cmsg->cmsg_level, cmsg->cmsg_type); | |
2a1288ff | 639 | } |
2181a7f5 | 640 | |
a7e3212d LP |
641 | r = bus_socket_auth_verify(b); |
642 | if (r != 0) | |
643 | return r; | |
644 | ||
645 | return 1; | |
646 | } | |
647 | ||
8f04d2eb | 648 | void bus_socket_setup(sd_bus *b) { |
a7e3212d LP |
649 | assert(b); |
650 | ||
aec6d91f | 651 | /* Increase the buffers to 8 MB */ |
28e7e934 | 652 | (void) fd_increase_rxbuf(b->input_fd, SNDBUF_SIZE); |
6ae22ffb | 653 | (void) fd_inc_sndbuf(b->output_fd, SNDBUF_SIZE); |
a7e3212d | 654 | |
e1d337d4 | 655 | b->message_version = 1; |
0f437184 | 656 | b->message_endian = 0; |
8f04d2eb | 657 | } |
e1d337d4 | 658 | |
8f04d2eb | 659 | static void bus_get_peercred(sd_bus *b) { |
c4e6556c ZJS |
660 | int r; |
661 | ||
8f04d2eb | 662 | assert(b); |
18ac4643 LP |
663 | assert(!b->ucred_valid); |
664 | assert(!b->label); | |
f5fbe71d | 665 | assert(b->n_groups == SIZE_MAX); |
8f04d2eb LP |
666 | |
667 | /* Get the peer for socketpair() sockets */ | |
eff05270 | 668 | b->ucred_valid = getpeercred(b->input_fd, &b->ucred) >= 0; |
c4e6556c ZJS |
669 | |
670 | /* Get the SELinux context of the peer */ | |
db7d1dca | 671 | r = getpeersec(b->input_fd, &b->label); |
18ac4643 | 672 | if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT)) |
71be6406 | 673 | log_debug_errno(r, "Failed to determine peer security context, ignoring: %m"); |
18ac4643 LP |
674 | |
675 | /* Get the list of auxiliary groups of the peer */ | |
676 | r = getpeergroups(b->input_fd, &b->groups); | |
c599b325 | 677 | if (r >= 0) |
18ac4643 | 678 | b->n_groups = (size_t) r; |
c599b325 | 679 | else if (!IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT)) |
71be6406 LP |
680 | log_debug_errno(r, "Failed to determine peer's group list, ignoring: %m"); |
681 | ||
682 | r = getpeerpidfd(b->input_fd); | |
683 | if (r < 0) | |
431042e9 | 684 | log_debug_errno(r, "Failed to determine peer pidfd, ignoring: %m"); |
71be6406 LP |
685 | else |
686 | close_and_replace(b->pidfd, r); | |
c32f9648 LP |
687 | |
688 | /* Let's query the peers socket address, it might carry information such as the peer's comm or | |
689 | * description string */ | |
690 | zero(b->sockaddr_peer); | |
691 | b->sockaddr_size_peer = 0; | |
692 | ||
693 | socklen_t l = sizeof(b->sockaddr_peer) - 1; /* Leave space for a NUL */ | |
694 | if (getpeername(b->input_fd, &b->sockaddr_peer.sa, &l) < 0) | |
695 | log_debug_errno(errno, "Failed to get peer's socket address, ignoring: %m"); | |
696 | else | |
697 | b->sockaddr_size_peer = l; | |
a7e3212d LP |
698 | } |
699 | ||
2181a7f5 | 700 | static int bus_socket_start_auth_client(sd_bus *b) { |
1ed4723d DR |
701 | static const char sasl_auth_anonymous[] = { |
702 | /* | |
703 | * We use an arbitrary trace-string for the ANONYMOUS authentication. It can be used by the | |
704 | * message broker to aid debugging of clients. We fully anonymize the connection and use a | |
705 | * static default. | |
706 | */ | |
347f4824 DR |
707 | /* HEX a n o n y m o u s */ |
708 | "\0AUTH ANONYMOUS 616e6f6e796d6f7573\r\n" | |
1ed4723d DR |
709 | }; |
710 | static const char sasl_auth_external[] = { | |
711 | "\0AUTH EXTERNAL\r\n" | |
712 | "DATA\r\n" | |
713 | }; | |
714 | static const char sasl_negotiate_unix_fd[] = { | |
715 | "NEGOTIATE_UNIX_FD\r\n" | |
716 | }; | |
717 | static const char sasl_begin[] = { | |
718 | "BEGIN\r\n" | |
719 | }; | |
720 | size_t i = 0; | |
a7e3212d LP |
721 | |
722 | assert(b); | |
723 | ||
1ed4723d DR |
724 | if (b->anonymous_auth) |
725 | b->auth_iovec[i++] = IOVEC_MAKE((char*) sasl_auth_anonymous, sizeof(sasl_auth_anonymous) - 1); | |
726 | else | |
727 | b->auth_iovec[i++] = IOVEC_MAKE((char*) sasl_auth_external, sizeof(sasl_auth_external) - 1); | |
a7e3212d | 728 | |
c7db1984 | 729 | if (b->accept_fd) |
1ed4723d | 730 | b->auth_iovec[i++] = IOVEC_MAKE_STRING(sasl_negotiate_unix_fd); |
a7e3212d | 731 | |
1ed4723d | 732 | b->auth_iovec[i++] = IOVEC_MAKE_STRING(sasl_begin); |
a7e3212d LP |
733 | |
734 | return bus_socket_write_auth(b); | |
735 | } | |
736 | ||
a7893c6b | 737 | int bus_socket_start_auth(sd_bus *b) { |
2181a7f5 LP |
738 | assert(b); |
739 | ||
8f04d2eb LP |
740 | bus_get_peercred(b); |
741 | ||
3e0e196e | 742 | bus_set_state(b, BUS_AUTHENTICATING); |
036d61b3 | 743 | b->auth_timeout = now(CLOCK_MONOTONIC) + BUS_AUTH_TIMEOUT; |
2181a7f5 | 744 | |
9ab32f9d | 745 | if (sd_is_socket(b->input_fd, AF_UNIX, 0, 0) <= 0) |
c7db1984 | 746 | b->accept_fd = false; |
2181a7f5 | 747 | |
9ab32f9d LP |
748 | if (b->output_fd != b->input_fd) |
749 | if (sd_is_socket(b->output_fd, AF_UNIX, 0, 0) <= 0) | |
c7db1984 | 750 | b->accept_fd = false; |
e82c9509 | 751 | |
2181a7f5 LP |
752 | if (b->is_server) |
753 | return bus_socket_read_auth(b); | |
754 | else | |
755 | return bus_socket_start_auth_client(b); | |
756 | } | |
757 | ||
8a5cd31e LP |
758 | static int bus_socket_inotify_setup(sd_bus *b) { |
759 | _cleanup_free_ int *new_watches = NULL; | |
760 | _cleanup_free_ char *absolute = NULL; | |
319a4f4b | 761 | size_t n = 0, done = 0, i; |
8a5cd31e LP |
762 | unsigned max_follow = 32; |
763 | const char *p; | |
764 | int wd, r; | |
765 | ||
766 | assert(b); | |
767 | assert(b->watch_bind); | |
768 | assert(b->sockaddr.sa.sa_family == AF_UNIX); | |
769 | assert(b->sockaddr.un.sun_path[0] != 0); | |
770 | ||
0e2f18ee DDM |
771 | /* Sets up an inotify fd in case watch_bind is enabled: wait until the configured AF_UNIX file system |
772 | * socket appears before connecting to it. The implemented is pretty simplistic: we just subscribe to | |
773 | * relevant changes to all components of the path, and every time we get an event for that we try to | |
774 | * reconnect again, without actually caring what precisely the event we got told us. If we still | |
775 | * can't connect we re-subscribe to all relevant changes of anything in the path, so that our watches | |
776 | * include any possibly newly created path components. */ | |
8a5cd31e LP |
777 | |
778 | if (b->inotify_fd < 0) { | |
779 | b->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); | |
780 | if (b->inotify_fd < 0) | |
781 | return -errno; | |
7fe2903c LP |
782 | |
783 | b->inotify_fd = fd_move_above_stdio(b->inotify_fd); | |
8a5cd31e LP |
784 | } |
785 | ||
786 | /* Make sure the path is NUL terminated */ | |
2f82562b LP |
787 | p = strndupa_safe(b->sockaddr.un.sun_path, |
788 | sizeof(b->sockaddr.un.sun_path)); | |
8a5cd31e LP |
789 | |
790 | /* Make sure the path is absolute */ | |
791 | r = path_make_absolute_cwd(p, &absolute); | |
792 | if (r < 0) | |
793 | goto fail; | |
794 | ||
0e2f18ee DDM |
795 | /* Watch all components of the path, and don't mind any prefix that doesn't exist yet. For the |
796 | * innermost directory that exists we want to know when files are created or moved into it. For all | |
797 | * parents of it we just care if they are removed or renamed. */ | |
8a5cd31e | 798 | |
319a4f4b | 799 | if (!GREEDY_REALLOC(new_watches, n + 1)) { |
8a5cd31e LP |
800 | r = -ENOMEM; |
801 | goto fail; | |
802 | } | |
803 | ||
0e2f18ee DDM |
804 | /* Start with the top-level directory, which is a bit simpler than the rest, since it can't be a |
805 | * symlink, and always exists */ | |
8a5cd31e LP |
806 | wd = inotify_add_watch(b->inotify_fd, "/", IN_CREATE|IN_MOVED_TO); |
807 | if (wd < 0) { | |
808 | r = log_debug_errno(errno, "Failed to add inotify watch on /: %m"); | |
809 | goto fail; | |
810 | } else | |
811 | new_watches[n++] = wd; | |
812 | ||
813 | for (;;) { | |
814 | _cleanup_free_ char *component = NULL, *prefix = NULL, *destination = NULL; | |
815 | size_t n_slashes, n_component; | |
816 | char *c = NULL; | |
817 | ||
818 | n_slashes = strspn(absolute + done, "/"); | |
819 | n_component = n_slashes + strcspn(absolute + done + n_slashes, "/"); | |
820 | ||
821 | if (n_component == 0) /* The end */ | |
822 | break; | |
823 | ||
824 | component = strndup(absolute + done, n_component); | |
825 | if (!component) { | |
826 | r = -ENOMEM; | |
827 | goto fail; | |
828 | } | |
829 | ||
830 | /* A trailing slash? That's a directory, and not a socket then */ | |
831 | if (path_equal(component, "/")) { | |
832 | r = -EISDIR; | |
833 | goto fail; | |
834 | } | |
835 | ||
836 | /* A single dot? Let's eat this up */ | |
837 | if (path_equal(component, "/.")) { | |
838 | done += n_component; | |
839 | continue; | |
840 | } | |
841 | ||
842 | prefix = strndup(absolute, done + n_component); | |
843 | if (!prefix) { | |
844 | r = -ENOMEM; | |
845 | goto fail; | |
846 | } | |
847 | ||
319a4f4b | 848 | if (!GREEDY_REALLOC(new_watches, n + 1)) { |
8a5cd31e LP |
849 | r = -ENOMEM; |
850 | goto fail; | |
851 | } | |
852 | ||
853 | wd = inotify_add_watch(b->inotify_fd, prefix, IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CREATE|IN_MOVED_TO|IN_DONT_FOLLOW); | |
854 | log_debug("Added inotify watch for %s on bus %s: %i", prefix, strna(b->description), wd); | |
855 | ||
856 | if (wd < 0) { | |
857 | if (IN_SET(errno, ENOENT, ELOOP)) | |
858 | break; /* This component doesn't exist yet, or the path contains a cyclic symlink right now */ | |
859 | ||
945403e6 | 860 | r = log_debug_errno(errno, "Failed to add inotify watch on %s: %m", empty_to_root(prefix)); |
8a5cd31e LP |
861 | goto fail; |
862 | } else | |
863 | new_watches[n++] = wd; | |
864 | ||
865 | /* Check if this is possibly a symlink. If so, let's follow it and watch it too. */ | |
866 | r = readlink_malloc(prefix, &destination); | |
867 | if (r == -EINVAL) { /* not a symlink */ | |
868 | done += n_component; | |
869 | continue; | |
870 | } | |
871 | if (r < 0) | |
872 | goto fail; | |
873 | ||
874 | if (isempty(destination)) { /* Empty symlink target? Yuck! */ | |
875 | r = -EINVAL; | |
876 | goto fail; | |
877 | } | |
878 | ||
879 | if (max_follow <= 0) { /* Let's make sure we don't follow symlinks forever */ | |
880 | r = -ELOOP; | |
881 | goto fail; | |
882 | } | |
883 | ||
884 | if (path_is_absolute(destination)) { | |
885 | /* For absolute symlinks we build the new path and start anew */ | |
886 | c = strjoin(destination, absolute + done + n_component); | |
887 | done = 0; | |
888 | } else { | |
889 | _cleanup_free_ char *t = NULL; | |
890 | ||
891 | /* For relative symlinks we replace the last component, and try again */ | |
892 | t = strndup(absolute, done); | |
893 | if (!t) | |
894 | return -ENOMEM; | |
895 | ||
896 | c = strjoin(t, "/", destination, absolute + done + n_component); | |
897 | } | |
898 | if (!c) { | |
899 | r = -ENOMEM; | |
900 | goto fail; | |
901 | } | |
902 | ||
d6f2cd67 | 903 | free_and_replace(absolute, c); |
8a5cd31e LP |
904 | |
905 | max_follow--; | |
906 | } | |
907 | ||
908 | /* And now, let's remove all watches from the previous iteration we don't need anymore */ | |
909 | for (i = 0; i < b->n_inotify_watches; i++) { | |
910 | bool found = false; | |
911 | size_t j; | |
912 | ||
913 | for (j = 0; j < n; j++) | |
914 | if (new_watches[j] == b->inotify_watches[i]) { | |
915 | found = true; | |
916 | break; | |
917 | } | |
918 | ||
919 | if (found) | |
920 | continue; | |
921 | ||
922 | (void) inotify_rm_watch(b->inotify_fd, b->inotify_watches[i]); | |
923 | } | |
924 | ||
925 | free_and_replace(b->inotify_watches, new_watches); | |
926 | b->n_inotify_watches = n; | |
927 | ||
928 | return 0; | |
929 | ||
930 | fail: | |
931 | bus_close_inotify_fd(b); | |
932 | return r; | |
933 | } | |
934 | ||
a0cb3358 LP |
935 | static int bind_description(sd_bus *b, int fd, int family) { |
936 | _cleanup_free_ char *bind_name = NULL, *comm = NULL; | |
937 | union sockaddr_union bsa; | |
938 | const char *d = NULL; | |
939 | int r; | |
940 | ||
941 | assert(b); | |
942 | assert(fd >= 0); | |
943 | ||
944 | /* If this is an AF_UNIX socket, let's set our client's socket address to carry the description | |
945 | * string for this bus connection. This is useful for debugging things, as the connection name is | |
946 | * visible in various socket-related tools, and can even be queried by the server side. */ | |
947 | ||
948 | if (family != AF_UNIX) | |
949 | return 0; | |
950 | ||
951 | (void) sd_bus_get_description(b, &d); | |
952 | ||
953 | /* Generate a recognizable source address in the abstract namespace. We'll include: | |
da890466 | 954 | * - a random 64-bit value (to avoid collisions) |
a0cb3358 LP |
955 | * - our "comm" process name (suppressed if contains "/" to avoid parsing issues) |
956 | * - the description string of the bus connection. */ | |
d7d74854 | 957 | (void) pid_get_comm(0, &comm); |
a0cb3358 LP |
958 | if (comm && strchr(comm, '/')) |
959 | comm = mfree(comm); | |
960 | ||
961 | if (!d && !comm) /* skip if we don't have either field, rely on kernel autobind instead */ | |
962 | return 0; | |
963 | ||
964 | if (asprintf(&bind_name, "@%" PRIx64 "/bus/%s/%s", random_u64(), strempty(comm), strempty(d)) < 0) | |
965 | return -ENOMEM; | |
966 | ||
967 | strshorten(bind_name, sizeof_field(struct sockaddr_un, sun_path)); | |
968 | ||
969 | r = sockaddr_un_set_path(&bsa.un, bind_name); | |
970 | if (r < 0) | |
971 | return r; | |
972 | ||
973 | if (bind(fd, &bsa.sa, r) < 0) | |
974 | return -errno; | |
975 | ||
976 | return 0; | |
977 | } | |
978 | ||
fc772c61 LP |
979 | static int connect_as(int fd, const struct sockaddr *sa, socklen_t salen, uid_t uid, gid_t gid) { |
980 | _cleanup_(close_pairp) int pfd[2] = EBADF_PAIR; | |
981 | ssize_t n; | |
982 | int r; | |
983 | ||
984 | /* Shortcut if we are not supposed to drop privileges */ | |
985 | if (!uid_is_valid(uid) && !gid_is_valid(gid)) | |
986 | return RET_NERRNO(connect(fd, sa, salen)); | |
987 | ||
988 | /* This changes identity to the specified uid/gid and issues connect() as that. This is useful to | |
989 | * make sure SO_PEERCRED reports the selected UID/GID rather than the usual one of the caller. */ | |
990 | ||
991 | if (pipe2(pfd, O_CLOEXEC) < 0) | |
992 | return -errno; | |
993 | ||
994 | r = safe_fork("(sd-setresuid)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL|FORK_WAIT, /* ret_pid= */ NULL); | |
995 | if (r < 0) | |
996 | return r; | |
997 | if (r == 0) { | |
998 | /* child */ | |
999 | ||
1000 | pfd[0] = safe_close(pfd[0]); | |
1001 | ||
1002 | r = RET_NERRNO(setgroups(0, NULL)); | |
1003 | if (r < 0) | |
1004 | goto child_finish; | |
1005 | ||
1006 | if (gid_is_valid(gid)) { | |
1007 | r = RET_NERRNO(setresgid(gid, gid, gid)); | |
1008 | if (r < 0) | |
1009 | goto child_finish; | |
1010 | } | |
1011 | ||
1012 | if (uid_is_valid(uid)) { | |
1013 | r = RET_NERRNO(setresuid(uid, uid, uid)); | |
1014 | if (r < 0) | |
1015 | goto child_finish; | |
1016 | } | |
1017 | ||
1018 | r = RET_NERRNO(connect(fd, sa, salen)); | |
1019 | if (r < 0) | |
1020 | goto child_finish; | |
1021 | ||
1022 | r = 0; | |
1023 | ||
1024 | child_finish: | |
1025 | n = write(pfd[1], &r, sizeof(r)); | |
1026 | if (n != sizeof(r)) | |
1027 | _exit(EXIT_FAILURE); | |
1028 | ||
1029 | _exit(EXIT_SUCCESS); | |
1030 | } | |
1031 | ||
1032 | n = read(pfd[0], &r, sizeof(r)); | |
1033 | if (n != sizeof(r)) | |
1034 | return -EIO; | |
1035 | ||
1036 | return r; | |
1037 | } | |
1038 | ||
a7e3212d | 1039 | int bus_socket_connect(sd_bus *b) { |
8a5cd31e | 1040 | bool inotify_done = false; |
a7e3212d LP |
1041 | int r; |
1042 | ||
1043 | assert(b); | |
a7e3212d | 1044 | |
8a5cd31e LP |
1045 | for (;;) { |
1046 | assert(b->input_fd < 0); | |
1047 | assert(b->output_fd < 0); | |
1048 | assert(b->sockaddr.sa.sa_family != AF_UNSPEC); | |
a7e3212d | 1049 | |
165fee86 ZJS |
1050 | if (DEBUG_LOGGING) { |
1051 | _cleanup_free_ char *pretty = NULL; | |
1052 | (void) sockaddr_pretty(&b->sockaddr.sa, b->sockaddr_size, false, true, &pretty); | |
1053 | log_debug("sd-bus: starting bus%s%s by connecting to %s...", | |
1054 | b->description ? " " : "", strempty(b->description), strnull(pretty)); | |
1055 | } | |
1056 | ||
8a5cd31e LP |
1057 | b->input_fd = socket(b->sockaddr.sa.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
1058 | if (b->input_fd < 0) | |
1059 | return -errno; | |
e82c9509 | 1060 | |
a0cb3358 LP |
1061 | r = bind_description(b, b->input_fd, b->sockaddr.sa.sa_family); |
1062 | if (r < 0) | |
1063 | return r; | |
1064 | ||
7fe2903c LP |
1065 | b->input_fd = fd_move_above_stdio(b->input_fd); |
1066 | ||
8a5cd31e LP |
1067 | b->output_fd = b->input_fd; |
1068 | bus_socket_setup(b); | |
a7e3212d | 1069 | |
fc772c61 LP |
1070 | r = connect_as(b->input_fd, &b->sockaddr.sa, b->sockaddr_size, b->connect_as_uid, b->connect_as_gid); |
1071 | if (r < 0) { | |
1072 | if (r == -EINPROGRESS) { | |
a7e3212d | 1073 | |
8a5cd31e LP |
1074 | /* If we have any inotify watches open, close them now, we don't need them anymore, as |
1075 | * we have successfully initiated a connection */ | |
1076 | bus_close_inotify_fd(b); | |
1077 | ||
1078 | /* Note that very likely we are already in BUS_OPENING state here, as we enter it when | |
1079 | * we start parsing the address string. The only reason we set the state explicitly | |
1080 | * here, is to undo BUS_WATCH_BIND, in case we did the inotify magic. */ | |
3e0e196e | 1081 | bus_set_state(b, BUS_OPENING); |
8a5cd31e LP |
1082 | return 1; |
1083 | } | |
1084 | ||
fc772c61 | 1085 | if (IN_SET(r, -ENOENT, -ECONNREFUSED) && /* ENOENT → unix socket doesn't exist at all; ECONNREFUSED → unix socket stale */ |
8a5cd31e LP |
1086 | b->watch_bind && |
1087 | b->sockaddr.sa.sa_family == AF_UNIX && | |
1088 | b->sockaddr.un.sun_path[0] != 0) { | |
1089 | ||
1090 | /* This connection attempt failed, let's release the socket for now, and start with a | |
1091 | * fresh one when reconnecting. */ | |
1092 | bus_close_io_fds(b); | |
1093 | ||
1094 | if (inotify_done) { | |
1095 | /* inotify set up already, don't do it again, just return now, and remember | |
1096 | * that we are waiting for inotify events now. */ | |
3e0e196e | 1097 | bus_set_state(b, BUS_WATCH_BIND); |
8a5cd31e LP |
1098 | return 1; |
1099 | } | |
1100 | ||
1101 | /* This is a file system socket, and the inotify logic is enabled. Let's create the necessary inotify fd. */ | |
1102 | r = bus_socket_inotify_setup(b); | |
1103 | if (r < 0) | |
1104 | return r; | |
1105 | ||
1106 | /* Let's now try to connect a second time, because in theory there's otherwise a race | |
1107 | * here: the socket might have been created in the time between our first connect() and | |
1108 | * the time we set up the inotify logic. But let's remember that we set up inotify now, | |
1109 | * so that we don't do the connect() more than twice. */ | |
1110 | inotify_done = true; | |
1111 | ||
1112 | } else | |
fc772c61 | 1113 | return r; |
8a5cd31e LP |
1114 | } else |
1115 | break; | |
a7e3212d LP |
1116 | } |
1117 | ||
8a5cd31e LP |
1118 | /* Yay, established, we don't need no inotify anymore! */ |
1119 | bus_close_inotify_fd(b); | |
1120 | ||
a7e3212d LP |
1121 | return bus_socket_start_auth(b); |
1122 | } | |
1123 | ||
1124 | int bus_socket_exec(sd_bus *b) { | |
e82c9509 | 1125 | int s[2], r; |
a7e3212d LP |
1126 | |
1127 | assert(b); | |
e82c9509 LP |
1128 | assert(b->input_fd < 0); |
1129 | assert(b->output_fd < 0); | |
a7e3212d | 1130 | assert(b->exec_path); |
392cf1d0 | 1131 | assert(b->busexec_pid == 0); |
a7e3212d | 1132 | |
87fa2e21 ZJS |
1133 | if (DEBUG_LOGGING) { |
1134 | _cleanup_free_ char *line = NULL; | |
1135 | ||
1136 | if (b->exec_argv) | |
4ef15008 | 1137 | line = quote_command_line(b->exec_argv, SHELL_ESCAPE_EMPTY); |
87fa2e21 ZJS |
1138 | |
1139 | log_debug("sd-bus: starting bus%s%s with %s%s", | |
1140 | b->description ? " " : "", strempty(b->description), | |
1141 | line ?: b->exec_path, | |
1142 | b->exec_argv && !line ? "…" : ""); | |
1143 | } | |
165fee86 | 1144 | |
e82c9509 LP |
1145 | r = socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0, s); |
1146 | if (r < 0) | |
a7e3212d LP |
1147 | return -errno; |
1148 | ||
911f8f01 | 1149 | r = safe_fork_full("(sd-busexec)", |
0c2aedb4 YW |
1150 | (int[]) { s[1], s[1], STDERR_FILENO }, |
1151 | NULL, 0, | |
1152 | FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_REARRANGE_STDIO|FORK_RLIMIT_NOFILE_SAFE, &b->busexec_pid); | |
4c253ed1 | 1153 | if (r < 0) { |
3d94f76c | 1154 | safe_close_pair(s); |
4c253ed1 | 1155 | return r; |
a7e3212d | 1156 | } |
4c253ed1 | 1157 | if (r == 0) { |
a7e3212d LP |
1158 | /* Child */ |
1159 | ||
a7e3212d LP |
1160 | if (b->exec_argv) |
1161 | execvp(b->exec_path, b->exec_argv); | |
87fa2e21 ZJS |
1162 | else |
1163 | execvp(b->exec_path, STRV_MAKE(b->exec_path)); | |
a7e3212d LP |
1164 | |
1165 | _exit(EXIT_FAILURE); | |
1166 | } | |
1167 | ||
03e334a1 | 1168 | safe_close(s[1]); |
7fe2903c | 1169 | b->output_fd = b->input_fd = fd_move_above_stdio(s[0]); |
a7e3212d | 1170 | |
8f04d2eb | 1171 | bus_socket_setup(b); |
e1d337d4 | 1172 | |
a7e3212d LP |
1173 | return bus_socket_start_auth(b); |
1174 | } | |
1175 | ||
1176 | int bus_socket_take_fd(sd_bus *b) { | |
a7e3212d LP |
1177 | assert(b); |
1178 | ||
8f04d2eb | 1179 | bus_socket_setup(b); |
a7e3212d LP |
1180 | |
1181 | return bus_socket_start_auth(b); | |
1182 | } | |
1183 | ||
1184 | int bus_socket_write_message(sd_bus *bus, sd_bus_message *m, size_t *idx) { | |
a7e3212d LP |
1185 | struct iovec *iov; |
1186 | ssize_t k; | |
1187 | size_t n; | |
1188 | unsigned j; | |
bc7fd8cd | 1189 | int r; |
a7e3212d LP |
1190 | |
1191 | assert(bus); | |
1192 | assert(m); | |
1193 | assert(idx); | |
945c2931 | 1194 | assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)); |
a7e3212d | 1195 | |
6629161f | 1196 | if (*idx >= BUS_MESSAGE_SIZE(m)) |
a7e3212d | 1197 | return 0; |
a7e3212d | 1198 | |
bc7fd8cd LP |
1199 | r = bus_message_setup_iovec(m); |
1200 | if (r < 0) | |
1201 | return r; | |
2100fa10 | 1202 | |
a7e3212d | 1203 | n = m->n_iovec * sizeof(struct iovec); |
6e9417f5 | 1204 | iov = newa(struct iovec, n); |
75f32f04 | 1205 | memcpy_safe(iov, m->iovec, n); |
a7e3212d LP |
1206 | |
1207 | j = 0; | |
1208 | iovec_advance(iov, &j, *idx); | |
1209 | ||
15d5af81 LP |
1210 | if (bus->prefer_writev) |
1211 | k = writev(bus->output_fd, iov, m->n_iovec); | |
1212 | else { | |
7f4e6a1c ZJS |
1213 | struct msghdr mh = { |
1214 | .msg_iov = iov, | |
1215 | .msg_iovlen = m->n_iovec, | |
1216 | }; | |
15d5af81 | 1217 | |
f29eef2e | 1218 | if (m->n_fds > 0 && *idx == 0) { |
15d5af81 | 1219 | struct cmsghdr *control; |
15d5af81 | 1220 | |
a258f491 LP |
1221 | mh.msg_controllen = CMSG_SPACE(sizeof(int) * m->n_fds); |
1222 | mh.msg_control = alloca0(mh.msg_controllen); | |
1223 | control = CMSG_FIRSTHDR(&mh); | |
1224 | control->cmsg_len = CMSG_LEN(sizeof(int) * m->n_fds); | |
15d5af81 LP |
1225 | control->cmsg_level = SOL_SOCKET; |
1226 | control->cmsg_type = SCM_RIGHTS; | |
15d5af81 LP |
1227 | memcpy(CMSG_DATA(control), m->fds, sizeof(int) * m->n_fds); |
1228 | } | |
1229 | ||
15d5af81 LP |
1230 | k = sendmsg(bus->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL); |
1231 | if (k < 0 && errno == ENOTSOCK) { | |
1232 | bus->prefer_writev = true; | |
1233 | k = writev(bus->output_fd, iov, m->n_iovec); | |
1234 | } | |
1235 | } | |
a7e3212d | 1236 | |
a7e3212d | 1237 | if (k < 0) |
b3d06b92 | 1238 | return ERRNO_IS_TRANSIENT(errno) ? 0 : -errno; |
a7e3212d LP |
1239 | |
1240 | *idx += (size_t) k; | |
1241 | return 1; | |
1242 | } | |
1243 | ||
1244 | static int bus_socket_read_message_need(sd_bus *bus, size_t *need) { | |
1245 | uint32_t a, b; | |
1246 | uint8_t e; | |
1247 | uint64_t sum; | |
1248 | ||
1249 | assert(bus); | |
1250 | assert(need); | |
945c2931 | 1251 | assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)); |
a7e3212d LP |
1252 | |
1253 | if (bus->rbuffer_size < sizeof(struct bus_header)) { | |
1254 | *need = sizeof(struct bus_header) + 8; | |
1255 | ||
1256 | /* Minimum message size: | |
1257 | * | |
1258 | * Header + | |
1259 | * | |
1260 | * Method Call: +2 string headers | |
1261 | * Signal: +3 string headers | |
1262 | * Method Error: +1 string headers | |
1263 | * +1 uint32 headers | |
1264 | * Method Reply: +1 uint32 headers | |
1265 | * | |
1266 | * A string header is at least 9 bytes | |
1267 | * A uint32 header is at least 8 bytes | |
1268 | * | |
1269 | * Hence the minimum message size of a valid message | |
1270 | * is header + 8 bytes */ | |
1271 | ||
1272 | return 0; | |
1273 | } | |
1274 | ||
1275 | a = ((const uint32_t*) bus->rbuffer)[1]; | |
1276 | b = ((const uint32_t*) bus->rbuffer)[3]; | |
1277 | ||
1278 | e = ((const uint8_t*) bus->rbuffer)[0]; | |
0461f8cd | 1279 | if (e == BUS_LITTLE_ENDIAN) { |
a7e3212d LP |
1280 | a = le32toh(a); |
1281 | b = le32toh(b); | |
0461f8cd | 1282 | } else if (e == BUS_BIG_ENDIAN) { |
a7e3212d LP |
1283 | a = be32toh(a); |
1284 | b = be32toh(b); | |
1285 | } else | |
1286 | return -EBADMSG; | |
1287 | ||
dc7be332 | 1288 | sum = (uint64_t) sizeof(struct bus_header) + (uint64_t) ALIGN8(b) + (uint64_t) a; |
a7e3212d LP |
1289 | if (sum >= BUS_MESSAGE_SIZE_MAX) |
1290 | return -ENOBUFS; | |
1291 | ||
1292 | *need = (size_t) sum; | |
1293 | return 0; | |
1294 | } | |
1295 | ||
7d22c717 | 1296 | static int bus_socket_make_message(sd_bus *bus, size_t size) { |
6d586a13 | 1297 | sd_bus_message *t = NULL; |
a7e3212d LP |
1298 | void *b; |
1299 | int r; | |
1300 | ||
1301 | assert(bus); | |
a7e3212d | 1302 | assert(bus->rbuffer_size >= size); |
945c2931 | 1303 | assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)); |
a7e3212d | 1304 | |
7adc46fc LP |
1305 | r = bus_rqueue_make_room(bus); |
1306 | if (r < 0) | |
1307 | return r; | |
1308 | ||
a7e3212d LP |
1309 | if (bus->rbuffer_size > size) { |
1310 | b = memdup((const uint8_t*) bus->rbuffer + size, | |
1311 | bus->rbuffer_size - size); | |
1312 | if (!b) | |
1313 | return -ENOMEM; | |
1314 | } else | |
1315 | b = NULL; | |
1316 | ||
df2d202e LP |
1317 | r = bus_message_from_malloc(bus, |
1318 | bus->rbuffer, size, | |
a7e3212d | 1319 | bus->fds, bus->n_fds, |
038f9863 | 1320 | NULL, |
a7e3212d | 1321 | &t); |
3dec5201 | 1322 | if (r == -EBADMSG) { |
6d586a13 | 1323 | log_debug_errno(r, "Received invalid message from connection %s, dropping.", strna(bus->description)); |
3dec5201 ZJS |
1324 | free(bus->rbuffer); /* We want to drop current rbuffer and proceed with whatever remains in b */ |
1325 | } else if (r < 0) { | |
a7e3212d LP |
1326 | free(b); |
1327 | return r; | |
1328 | } | |
1329 | ||
3dec5201 | 1330 | /* rbuffer ownership was either transferred to t, or we got EBADMSG and dropped it. */ |
a7e3212d LP |
1331 | bus->rbuffer = b; |
1332 | bus->rbuffer_size -= size; | |
1333 | ||
1334 | bus->fds = NULL; | |
1335 | bus->n_fds = 0; | |
1336 | ||
c1757a70 | 1337 | if (t) { |
f1617a3b | 1338 | t->read_counter = ++bus->read_counter; |
c1757a70 LP |
1339 | bus->rqueue[bus->rqueue_size++] = bus_message_ref_queued(t, bus); |
1340 | sd_bus_message_unref(t); | |
1341 | } | |
7d22c717 | 1342 | |
a7e3212d LP |
1343 | return 1; |
1344 | } | |
1345 | ||
7d22c717 | 1346 | int bus_socket_read_message(sd_bus *bus) { |
a7e3212d | 1347 | struct msghdr mh; |
7f4e6a1c | 1348 | struct iovec iov = {}; |
a7e3212d LP |
1349 | ssize_t k; |
1350 | size_t need; | |
1351 | int r; | |
1352 | void *b; | |
fb29cdbe | 1353 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int) * BUS_FDS_MAX)) control; |
4d3a5b10 | 1354 | bool handle_cmsg = false; |
a7e3212d LP |
1355 | |
1356 | assert(bus); | |
945c2931 | 1357 | assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)); |
a7e3212d LP |
1358 | |
1359 | r = bus_socket_read_message_need(bus, &need); | |
1360 | if (r < 0) | |
1361 | return r; | |
1362 | ||
1363 | if (bus->rbuffer_size >= need) | |
7d22c717 | 1364 | return bus_socket_make_message(bus, need); |
a7e3212d LP |
1365 | |
1366 | b = realloc(bus->rbuffer, need); | |
1367 | if (!b) | |
1368 | return -ENOMEM; | |
1369 | ||
1370 | bus->rbuffer = b; | |
1371 | ||
5cfa2c3d | 1372 | iov = IOVEC_MAKE((uint8_t *)bus->rbuffer + bus->rbuffer_size, need - bus->rbuffer_size); |
a7e3212d | 1373 | |
c1093c34 | 1374 | if (bus->prefer_readv) { |
15d5af81 | 1375 | k = readv(bus->input_fd, &iov, 1); |
c1093c34 LP |
1376 | if (k < 0) |
1377 | k = -errno; | |
1378 | } else { | |
41ab8c67 LP |
1379 | mh = (struct msghdr) { |
1380 | .msg_iov = &iov, | |
1381 | .msg_iovlen = 1, | |
1382 | .msg_control = &control, | |
1383 | .msg_controllen = sizeof(control), | |
1384 | }; | |
15d5af81 | 1385 | |
3691bcf3 LP |
1386 | k = recvmsg_safe(bus->input_fd, &mh, MSG_DONTWAIT|MSG_CMSG_CLOEXEC); |
1387 | if (k == -ENOTSOCK) { | |
15d5af81 LP |
1388 | bus->prefer_readv = true; |
1389 | k = readv(bus->input_fd, &iov, 1); | |
3691bcf3 LP |
1390 | if (k < 0) |
1391 | k = -errno; | |
15d5af81 LP |
1392 | } else |
1393 | handle_cmsg = true; | |
1394 | } | |
c5ed77b2 ZJS |
1395 | if (ERRNO_IS_NEG_TRANSIENT(k)) |
1396 | return 0; | |
1397 | if (k < 0) | |
3691bcf3 LP |
1398 | return (int) k; |
1399 | if (k == 0) { | |
1400 | if (handle_cmsg) | |
1401 | cmsg_close_all(&mh); /* On EOF we shouldn't have gotten an fd, but let's make sure */ | |
a7e3212d | 1402 | return -ECONNRESET; |
3691bcf3 | 1403 | } |
a7e3212d LP |
1404 | |
1405 | bus->rbuffer_size += k; | |
1406 | ||
2a1288ff LP |
1407 | if (handle_cmsg) { |
1408 | struct cmsghdr *cmsg; | |
1409 | ||
1410 | CMSG_FOREACH(cmsg, &mh) | |
15d5af81 LP |
1411 | if (cmsg->cmsg_level == SOL_SOCKET && |
1412 | cmsg->cmsg_type == SCM_RIGHTS) { | |
7fe2903c | 1413 | int n, *f, i; |
15d5af81 LP |
1414 | |
1415 | n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); | |
1416 | ||
1417 | if (!bus->can_fds) { | |
1418 | /* Whut? We received fds but this | |
1419 | * isn't actually enabled? Close them, | |
1420 | * and fail */ | |
1421 | ||
b1d02191 | 1422 | close_many(CMSG_TYPED_DATA(cmsg, int), n); |
15d5af81 LP |
1423 | return -EIO; |
1424 | } | |
1425 | ||
62d74c78 | 1426 | f = reallocarray(bus->fds, bus->n_fds + n, sizeof(int)); |
15d5af81 | 1427 | if (!f) { |
b1d02191 | 1428 | close_many(CMSG_TYPED_DATA(cmsg, int), n); |
15d5af81 LP |
1429 | return -ENOMEM; |
1430 | } | |
1431 | ||
7fe2903c | 1432 | for (i = 0; i < n; i++) |
b1d02191 | 1433 | f[bus->n_fds++] = fd_move_above_stdio(CMSG_TYPED_DATA(cmsg, int)[i]); |
15d5af81 | 1434 | bus->fds = f; |
d868f2a3 ZJS |
1435 | } else |
1436 | log_debug("Got unexpected auxiliary data with level=%d and type=%d", | |
1437 | cmsg->cmsg_level, cmsg->cmsg_type); | |
2a1288ff | 1438 | } |
a7e3212d LP |
1439 | |
1440 | r = bus_socket_read_message_need(bus, &need); | |
1441 | if (r < 0) | |
1442 | return r; | |
1443 | ||
1444 | if (bus->rbuffer_size >= need) | |
7d22c717 | 1445 | return bus_socket_make_message(bus, need); |
a7e3212d LP |
1446 | |
1447 | return 1; | |
1448 | } | |
1449 | ||
1450 | int bus_socket_process_opening(sd_bus *b) { | |
0f2d351f | 1451 | int error = 0, events, r; |
a7e3212d | 1452 | socklen_t slen = sizeof(error); |
a7e3212d | 1453 | |
a7e3212d LP |
1454 | assert(b->state == BUS_OPENING); |
1455 | ||
0f2d351f | 1456 | events = fd_wait_for_event(b->output_fd, POLLOUT, 0); |
c5ed77b2 ZJS |
1457 | if (ERRNO_IS_NEG_TRANSIENT(events)) |
1458 | return 0; | |
1459 | if (events < 0) | |
0f2d351f LP |
1460 | return events; |
1461 | if (!(events & (POLLOUT|POLLERR|POLLHUP))) | |
a7e3212d LP |
1462 | return 0; |
1463 | ||
e82c9509 | 1464 | r = getsockopt(b->output_fd, SOL_SOCKET, SO_ERROR, &error, &slen); |
a7e3212d LP |
1465 | if (r < 0) |
1466 | b->last_connect_error = errno; | |
1467 | else if (error != 0) | |
1468 | b->last_connect_error = error; | |
0f2d351f | 1469 | else if (events & (POLLERR|POLLHUP)) |
a7e3212d LP |
1470 | b->last_connect_error = ECONNREFUSED; |
1471 | else | |
1472 | return bus_socket_start_auth(b); | |
1473 | ||
1474 | return bus_next_address(b); | |
1475 | } | |
1476 | ||
1477 | int bus_socket_process_authenticating(sd_bus *b) { | |
1478 | int r; | |
1479 | ||
1480 | assert(b); | |
1481 | assert(b->state == BUS_AUTHENTICATING); | |
1482 | ||
1483 | if (now(CLOCK_MONOTONIC) >= b->auth_timeout) | |
1484 | return -ETIMEDOUT; | |
1485 | ||
1486 | r = bus_socket_write_auth(b); | |
1487 | if (r != 0) | |
1488 | return r; | |
1489 | ||
1490 | return bus_socket_read_auth(b); | |
1491 | } | |
8a5cd31e LP |
1492 | |
1493 | int bus_socket_process_watch_bind(sd_bus *b) { | |
1494 | int r, q; | |
1495 | ||
1496 | assert(b); | |
1497 | assert(b->state == BUS_WATCH_BIND); | |
1498 | assert(b->inotify_fd >= 0); | |
1499 | ||
1500 | r = flush_fd(b->inotify_fd); | |
1501 | if (r <= 0) | |
1502 | return r; | |
1503 | ||
1504 | log_debug("Got inotify event on bus %s.", strna(b->description)); | |
1505 | ||
1506 | /* We flushed events out of the inotify fd. In that case, maybe the socket is valid now? Let's try to connect | |
1507 | * to it again */ | |
1508 | ||
1509 | r = bus_socket_connect(b); | |
1510 | if (r < 0) | |
1511 | return r; | |
1512 | ||
1513 | q = bus_attach_io_events(b); | |
1514 | if (q < 0) | |
1515 | return q; | |
1516 | ||
1517 | q = bus_attach_inotify_event(b); | |
1518 | if (q < 0) | |
1519 | return q; | |
1520 | ||
1521 | return r; | |
1522 | } |