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