Lines Matching refs:b
110 bool bus_socket_auth_needs_write(sd_bus *b) { in bus_socket_auth_needs_write() argument
114 if (b->auth_index >= ELEMENTSOF(b->auth_iovec)) in bus_socket_auth_needs_write()
117 for (i = b->auth_index; i < ELEMENTSOF(b->auth_iovec); i++) { in bus_socket_auth_needs_write()
118 struct iovec *j = b->auth_iovec + i; in bus_socket_auth_needs_write()
127 static int bus_socket_write_auth(sd_bus *b) { in bus_socket_write_auth() argument
130 assert(b); in bus_socket_write_auth()
131 assert(b->state == BUS_AUTHENTICATING); in bus_socket_write_auth()
133 if (!bus_socket_auth_needs_write(b)) in bus_socket_write_auth()
136 if (b->prefer_writev) in bus_socket_write_auth()
137 …k = writev(b->output_fd, b->auth_iovec + b->auth_index, ELEMENTSOF(b->auth_iovec) - b->auth_index); in bus_socket_write_auth()
140 .msg_iov = b->auth_iovec + b->auth_index, in bus_socket_write_auth()
141 .msg_iovlen = ELEMENTSOF(b->auth_iovec) - b->auth_index, in bus_socket_write_auth()
144 k = sendmsg(b->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL); in bus_socket_write_auth()
146 b->prefer_writev = true; in bus_socket_write_auth()
147 …k = writev(b->output_fd, b->auth_iovec + b->auth_index, ELEMENTSOF(b->auth_iovec) - b->auth_index); in bus_socket_write_auth()
154 iovec_advance(b->auth_iovec, &b->auth_index, (size_t) k); in bus_socket_write_auth()
158 static int bus_socket_auth_verify_client(sd_bus *b) { in bus_socket_auth_verify_client() argument
163 assert(b); in bus_socket_auth_verify_client()
172 d = memmem_safe(b->rbuffer, b->rbuffer_size, "\r\n", 2); in bus_socket_auth_verify_client()
176 e = memmem_safe(d + 2, b->rbuffer_size - (d - (char*) b->rbuffer) - 2, "\r\n", 2); in bus_socket_auth_verify_client()
180 if (b->accept_fd) { in bus_socket_auth_verify_client()
181 f = memmem_safe(e + 2, b->rbuffer_size - (e - (char*) b->rbuffer) - 2, "\r\n", 2); in bus_socket_auth_verify_client()
193 if (d - (char*) b->rbuffer == 4) { in bus_socket_auth_verify_client()
194 if (memcmp(b->rbuffer, "DATA", 4)) in bus_socket_auth_verify_client()
196 } else if (d - (char*) b->rbuffer == 3 + 32) { in bus_socket_auth_verify_client()
203 if (memcmp(b->rbuffer, "OK ", 3)) in bus_socket_auth_verify_client()
216 b->auth = b->anonymous_auth ? BUS_AUTH_ANONYMOUS : BUS_AUTH_EXTERNAL; in bus_socket_auth_verify_client()
230 if (!sd_id128_is_null(b->server_id) && in bus_socket_auth_verify_client()
231 !sd_id128_equal(b->server_id, peer)) in bus_socket_auth_verify_client()
234 b->server_id = peer; in bus_socket_auth_verify_client()
239 b->can_fds = in bus_socket_auth_verify_client()
244 b->rbuffer_size -= (start - (char*) b->rbuffer); in bus_socket_auth_verify_client()
245 memmove(b->rbuffer, start, b->rbuffer_size); in bus_socket_auth_verify_client()
247 r = bus_start_running(b); in bus_socket_auth_verify_client()
271 static int verify_anonymous_token(sd_bus *b, const char *p, size_t l) { in verify_anonymous_token() argument
276 if (!b->anonymous_auth) in verify_anonymous_token()
298 static int verify_external_token(sd_bus *b, const char *p, size_t l) { in verify_external_token() argument
308 if (!b->anonymous_auth && !b->ucred_valid) in verify_external_token()
333 if (!b->anonymous_auth && u != b->ucred.uid) in verify_external_token()
339 static int bus_socket_auth_write(sd_bus *b, const char *t) { in bus_socket_auth_write() argument
343 assert(b); in bus_socket_auth_write()
347 assert(IN_SET(b->auth_index, 0, 1)); in bus_socket_auth_write()
350 p = malloc(b->auth_iovec[0].iov_len + l); in bus_socket_auth_write()
354 memcpy_safe(p, b->auth_iovec[0].iov_base, b->auth_iovec[0].iov_len); in bus_socket_auth_write()
355 memcpy(p + b->auth_iovec[0].iov_len, t, l); in bus_socket_auth_write()
357 b->auth_iovec[0].iov_base = p; in bus_socket_auth_write()
358 b->auth_iovec[0].iov_len += l; in bus_socket_auth_write()
360 free(b->auth_buffer); in bus_socket_auth_write()
361 b->auth_buffer = p; in bus_socket_auth_write()
362 b->auth_index = 0; in bus_socket_auth_write()
366 static int bus_socket_auth_write_ok(sd_bus *b) { in bus_socket_auth_write_ok() argument
369 assert(b); in bus_socket_auth_write_ok()
371 xsprintf(t, "OK " SD_ID128_FORMAT_STR "\r\n", SD_ID128_FORMAT_VAL(b->server_id)); in bus_socket_auth_write_ok()
373 return bus_socket_auth_write(b, t); in bus_socket_auth_write_ok()
376 static int bus_socket_auth_verify_server(sd_bus *b) { in bus_socket_auth_verify_server() argument
383 assert(b); in bus_socket_auth_verify_server()
385 if (b->rbuffer_size < 1) in bus_socket_auth_verify_server()
389 if (*(char*) b->rbuffer != 0) in bus_socket_auth_verify_server()
392 if (b->rbuffer_size < 3) in bus_socket_auth_verify_server()
396 if (b->auth_rbegin <= 0) in bus_socket_auth_verify_server()
397 b->auth_rbegin = 1; in bus_socket_auth_verify_server()
401 line = (char*) b->rbuffer + b->auth_rbegin; in bus_socket_auth_verify_server()
402 e = memmem_safe(line, b->rbuffer_size - b->auth_rbegin, "\r\n", 2); in bus_socket_auth_verify_server()
410 r = verify_anonymous_token(b, in bus_socket_auth_verify_server()
416 r = bus_socket_auth_write(b, "REJECTED\r\n"); in bus_socket_auth_verify_server()
418 b->auth = BUS_AUTH_ANONYMOUS; in bus_socket_auth_verify_server()
420 r = bus_socket_auth_write(b, "DATA\r\n"); in bus_socket_auth_verify_server()
422 r = bus_socket_auth_write_ok(b); in bus_socket_auth_verify_server()
427 r = verify_external_token(b, in bus_socket_auth_verify_server()
433 r = bus_socket_auth_write(b, "REJECTED\r\n"); in bus_socket_auth_verify_server()
435 b->auth = BUS_AUTH_EXTERNAL; in bus_socket_auth_verify_server()
437 r = bus_socket_auth_write(b, "DATA\r\n"); in bus_socket_auth_verify_server()
439 r = bus_socket_auth_write_ok(b); in bus_socket_auth_verify_server()
443 r = bus_socket_auth_write(b, "REJECTED EXTERNAL ANONYMOUS\r\n"); in bus_socket_auth_verify_server()
447 b->auth = _BUS_AUTH_INVALID; in bus_socket_auth_verify_server()
448 r = bus_socket_auth_write(b, "REJECTED\r\n"); in bus_socket_auth_verify_server()
452 if (b->auth == _BUS_AUTH_INVALID) in bus_socket_auth_verify_server()
453 r = bus_socket_auth_write(b, "ERROR\r\n"); in bus_socket_auth_verify_server()
460 if (bus_socket_auth_needs_write(b)) in bus_socket_auth_verify_server()
463 b->rbuffer_size -= (e + 2 - (char*) b->rbuffer); in bus_socket_auth_verify_server()
464 memmove(b->rbuffer, e + 2, b->rbuffer_size); in bus_socket_auth_verify_server()
465 return bus_start_running(b); in bus_socket_auth_verify_server()
470 if (b->auth == _BUS_AUTH_INVALID) in bus_socket_auth_verify_server()
471 r = bus_socket_auth_write(b, "ERROR\r\n"); in bus_socket_auth_verify_server()
473 if (b->auth == BUS_AUTH_ANONYMOUS) in bus_socket_auth_verify_server()
474 r = verify_anonymous_token(b, line + 4, l - 4); in bus_socket_auth_verify_server()
476 r = verify_external_token(b, line + 4, l - 4); in bus_socket_auth_verify_server()
481 b->auth = _BUS_AUTH_INVALID; in bus_socket_auth_verify_server()
482 r = bus_socket_auth_write(b, "REJECTED\r\n"); in bus_socket_auth_verify_server()
484 r = bus_socket_auth_write_ok(b); in bus_socket_auth_verify_server()
487 if (b->auth == _BUS_AUTH_INVALID || !b->accept_fd) in bus_socket_auth_verify_server()
488 r = bus_socket_auth_write(b, "ERROR\r\n"); in bus_socket_auth_verify_server()
490 b->can_fds = true; in bus_socket_auth_verify_server()
491 r = bus_socket_auth_write(b, "AGREE_UNIX_FD\r\n"); in bus_socket_auth_verify_server()
494 r = bus_socket_auth_write(b, "ERROR\r\n"); in bus_socket_auth_verify_server()
499 b->auth_rbegin = e + 2 - (char*) b->rbuffer; in bus_socket_auth_verify_server()
505 static int bus_socket_auth_verify(sd_bus *b) { in bus_socket_auth_verify() argument
506 assert(b); in bus_socket_auth_verify()
508 if (b->is_server) in bus_socket_auth_verify()
509 return bus_socket_auth_verify_server(b); in bus_socket_auth_verify()
511 return bus_socket_auth_verify_client(b); in bus_socket_auth_verify()
514 static int bus_socket_read_auth(sd_bus *b) { in bus_socket_read_auth() argument
524 assert(b); in bus_socket_read_auth()
525 assert(b->state == BUS_AUTHENTICATING); in bus_socket_read_auth()
527 r = bus_socket_auth_verify(b); in bus_socket_read_auth()
531 n = MAX(256u, b->rbuffer_size * 2); in bus_socket_read_auth()
536 if (b->rbuffer_size >= n) in bus_socket_read_auth()
539 p = realloc(b->rbuffer, n); in bus_socket_read_auth()
543 b->rbuffer = p; in bus_socket_read_auth()
545 iov = IOVEC_MAKE((uint8_t *)b->rbuffer + b->rbuffer_size, n - b->rbuffer_size); in bus_socket_read_auth()
547 if (b->prefer_readv) { in bus_socket_read_auth()
548 k = readv(b->input_fd, &iov, 1); in bus_socket_read_auth()
559 k = recvmsg_safe(b->input_fd, &mh, MSG_DONTWAIT|MSG_CMSG_CLOEXEC); in bus_socket_read_auth()
561 b->prefer_readv = true; in bus_socket_read_auth()
562 k = readv(b->input_fd, &iov, 1); in bus_socket_read_auth()
579 b->rbuffer_size += k; in bus_socket_read_auth()
600 r = bus_socket_auth_verify(b); in bus_socket_read_auth()
607 void bus_socket_setup(sd_bus *b) { in bus_socket_setup() argument
608 assert(b); in bus_socket_setup()
611 (void) fd_inc_rcvbuf(b->input_fd, SNDBUF_SIZE); in bus_socket_setup()
612 (void) fd_inc_sndbuf(b->output_fd, SNDBUF_SIZE); in bus_socket_setup()
614 b->message_version = 1; in bus_socket_setup()
615 b->message_endian = 0; in bus_socket_setup()
618 static void bus_get_peercred(sd_bus *b) { in bus_get_peercred() argument
621 assert(b); in bus_get_peercred()
622 assert(!b->ucred_valid); in bus_get_peercred()
623 assert(!b->label); in bus_get_peercred()
624 assert(b->n_groups == SIZE_MAX); in bus_get_peercred()
627 b->ucred_valid = getpeercred(b->input_fd, &b->ucred) >= 0; in bus_get_peercred()
630 r = getpeersec(b->input_fd, &b->label); in bus_get_peercred()
635 r = getpeergroups(b->input_fd, &b->groups); in bus_get_peercred()
637 b->n_groups = (size_t) r; in bus_get_peercred()
642 static int bus_socket_start_auth_client(sd_bus *b) { in bus_socket_start_auth_client() argument
665 assert(b); in bus_socket_start_auth_client()
667 if (b->anonymous_auth) in bus_socket_start_auth_client()
668 … b->auth_iovec[i++] = IOVEC_MAKE((char*) sasl_auth_anonymous, sizeof(sasl_auth_anonymous) - 1); in bus_socket_start_auth_client()
670 … b->auth_iovec[i++] = IOVEC_MAKE((char*) sasl_auth_external, sizeof(sasl_auth_external) - 1); in bus_socket_start_auth_client()
672 if (b->accept_fd) in bus_socket_start_auth_client()
673 b->auth_iovec[i++] = IOVEC_MAKE_STRING(sasl_negotiate_unix_fd); in bus_socket_start_auth_client()
675 b->auth_iovec[i++] = IOVEC_MAKE_STRING(sasl_begin); in bus_socket_start_auth_client()
677 return bus_socket_write_auth(b); in bus_socket_start_auth_client()
680 int bus_socket_start_auth(sd_bus *b) { in bus_socket_start_auth() argument
681 assert(b); in bus_socket_start_auth()
683 bus_get_peercred(b); in bus_socket_start_auth()
685 bus_set_state(b, BUS_AUTHENTICATING); in bus_socket_start_auth()
686 b->auth_timeout = now(CLOCK_MONOTONIC) + BUS_AUTH_TIMEOUT; in bus_socket_start_auth()
688 if (sd_is_socket(b->input_fd, AF_UNIX, 0, 0) <= 0) in bus_socket_start_auth()
689 b->accept_fd = false; in bus_socket_start_auth()
691 if (b->output_fd != b->input_fd) in bus_socket_start_auth()
692 if (sd_is_socket(b->output_fd, AF_UNIX, 0, 0) <= 0) in bus_socket_start_auth()
693 b->accept_fd = false; in bus_socket_start_auth()
695 if (b->is_server) in bus_socket_start_auth()
696 return bus_socket_read_auth(b); in bus_socket_start_auth()
698 return bus_socket_start_auth_client(b); in bus_socket_start_auth()
701 static int bus_socket_inotify_setup(sd_bus *b) { in bus_socket_inotify_setup() argument
709 assert(b); in bus_socket_inotify_setup()
710 assert(b->watch_bind); in bus_socket_inotify_setup()
711 assert(b->sockaddr.sa.sa_family == AF_UNIX); in bus_socket_inotify_setup()
712 assert(b->sockaddr.un.sun_path[0] != 0); in bus_socket_inotify_setup()
721 if (b->inotify_fd < 0) { in bus_socket_inotify_setup()
722 b->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); in bus_socket_inotify_setup()
723 if (b->inotify_fd < 0) in bus_socket_inotify_setup()
726 b->inotify_fd = fd_move_above_stdio(b->inotify_fd); in bus_socket_inotify_setup()
730 p = strndupa_safe(b->sockaddr.un.sun_path, in bus_socket_inotify_setup()
731 sizeof(b->sockaddr.un.sun_path)); in bus_socket_inotify_setup()
749 wd = inotify_add_watch(b->inotify_fd, "/", IN_CREATE|IN_MOVED_TO); in bus_socket_inotify_setup()
796 …wd = inotify_add_watch(b->inotify_fd, prefix, IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CREATE|IN_M… in bus_socket_inotify_setup()
797 … log_debug("Added inotify watch for %s on bus %s: %i", prefix, strna(b->description), wd); in bus_socket_inotify_setup()
853 for (i = 0; i < b->n_inotify_watches; i++) { in bus_socket_inotify_setup()
858 if (new_watches[j] == b->inotify_watches[i]) { in bus_socket_inotify_setup()
866 (void) inotify_rm_watch(b->inotify_fd, b->inotify_watches[i]); in bus_socket_inotify_setup()
869 free_and_replace(b->inotify_watches, new_watches); in bus_socket_inotify_setup()
870 b->n_inotify_watches = n; in bus_socket_inotify_setup()
875 bus_close_inotify_fd(b); in bus_socket_inotify_setup()
879 int bus_socket_connect(sd_bus *b) { in bus_socket_connect() argument
883 assert(b); in bus_socket_connect()
886 assert(b->input_fd < 0); in bus_socket_connect()
887 assert(b->output_fd < 0); in bus_socket_connect()
888 assert(b->sockaddr.sa.sa_family != AF_UNSPEC); in bus_socket_connect()
892 … (void) sockaddr_pretty(&b->sockaddr.sa, b->sockaddr_size, false, true, &pretty); in bus_socket_connect()
894 … b->description ? " " : "", strempty(b->description), strnull(pretty)); in bus_socket_connect()
897 … b->input_fd = socket(b->sockaddr.sa.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); in bus_socket_connect()
898 if (b->input_fd < 0) in bus_socket_connect()
901 b->input_fd = fd_move_above_stdio(b->input_fd); in bus_socket_connect()
903 b->output_fd = b->input_fd; in bus_socket_connect()
904 bus_socket_setup(b); in bus_socket_connect()
906 if (connect(b->input_fd, &b->sockaddr.sa, b->sockaddr_size) < 0) { in bus_socket_connect()
911 bus_close_inotify_fd(b); in bus_socket_connect()
916 bus_set_state(b, BUS_OPENING); in bus_socket_connect()
921 b->watch_bind && in bus_socket_connect()
922 b->sockaddr.sa.sa_family == AF_UNIX && in bus_socket_connect()
923 b->sockaddr.un.sun_path[0] != 0) { in bus_socket_connect()
927 bus_close_io_fds(b); in bus_socket_connect()
932 bus_set_state(b, BUS_WATCH_BIND); in bus_socket_connect()
937 r = bus_socket_inotify_setup(b); in bus_socket_connect()
954 bus_close_inotify_fd(b); in bus_socket_connect()
956 return bus_socket_start_auth(b); in bus_socket_connect()
959 int bus_socket_exec(sd_bus *b) { in bus_socket_exec() argument
962 assert(b); in bus_socket_exec()
963 assert(b->input_fd < 0); in bus_socket_exec()
964 assert(b->output_fd < 0); in bus_socket_exec()
965 assert(b->exec_path); in bus_socket_exec()
966 assert(b->busexec_pid == 0); in bus_socket_exec()
971 if (b->exec_argv) in bus_socket_exec()
972 line = quote_command_line(b->exec_argv, SHELL_ESCAPE_EMPTY); in bus_socket_exec()
975 b->description ? " " : "", strempty(b->description), in bus_socket_exec()
976 line ?: b->exec_path, in bus_socket_exec()
977 b->exec_argv && !line ? "…" : ""); in bus_socket_exec()
984 …r = safe_fork_full("(sd-busexec)", s+1, 1, FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, &b->busexec_pid); in bus_socket_exec()
999 if (b->exec_argv) in bus_socket_exec()
1000 execvp(b->exec_path, b->exec_argv); in bus_socket_exec()
1002 execvp(b->exec_path, STRV_MAKE(b->exec_path)); in bus_socket_exec()
1008 b->output_fd = b->input_fd = fd_move_above_stdio(s[0]); in bus_socket_exec()
1010 bus_socket_setup(b); in bus_socket_exec()
1012 return bus_socket_start_auth(b); in bus_socket_exec()
1015 int bus_socket_take_fd(sd_bus *b) { in bus_socket_take_fd() argument
1016 assert(b); in bus_socket_take_fd()
1018 bus_socket_setup(b); in bus_socket_take_fd()
1020 return bus_socket_start_auth(b); in bus_socket_take_fd()
1084 uint32_t a, b; in bus_socket_read_message_need() local
1115 b = ((const uint32_t*) bus->rbuffer)[3]; in bus_socket_read_message_need()
1120 b = le32toh(b); in bus_socket_read_message_need()
1123 b = be32toh(b); in bus_socket_read_message_need()
1127 sum = (uint64_t) sizeof(struct bus_header) + (uint64_t) ALIGN_TO(b, 8) + (uint64_t) a; in bus_socket_read_message_need()
1137 void *b; in bus_socket_make_message() local
1149 b = memdup((const uint8_t*) bus->rbuffer + size, in bus_socket_make_message()
1151 if (!b) in bus_socket_make_message()
1154 b = NULL; in bus_socket_make_message()
1165 free(b); in bus_socket_make_message()
1170 bus->rbuffer = b; in bus_socket_make_message()
1191 void *b; in bus_socket_read_message() local
1205 b = realloc(bus->rbuffer, need); in bus_socket_read_message()
1206 if (!b) in bus_socket_read_message()
1209 bus->rbuffer = b; in bus_socket_read_message()
1290 int bus_socket_process_opening(sd_bus *b) { in bus_socket_process_opening() argument
1294 assert(b->state == BUS_OPENING); in bus_socket_process_opening()
1296 events = fd_wait_for_event(b->output_fd, POLLOUT, 0); in bus_socket_process_opening()
1302 r = getsockopt(b->output_fd, SOL_SOCKET, SO_ERROR, &error, &slen); in bus_socket_process_opening()
1304 b->last_connect_error = errno; in bus_socket_process_opening()
1306 b->last_connect_error = error; in bus_socket_process_opening()
1308 b->last_connect_error = ECONNREFUSED; in bus_socket_process_opening()
1310 return bus_socket_start_auth(b); in bus_socket_process_opening()
1312 return bus_next_address(b); in bus_socket_process_opening()
1315 int bus_socket_process_authenticating(sd_bus *b) { in bus_socket_process_authenticating() argument
1318 assert(b); in bus_socket_process_authenticating()
1319 assert(b->state == BUS_AUTHENTICATING); in bus_socket_process_authenticating()
1321 if (now(CLOCK_MONOTONIC) >= b->auth_timeout) in bus_socket_process_authenticating()
1324 r = bus_socket_write_auth(b); in bus_socket_process_authenticating()
1328 return bus_socket_read_auth(b); in bus_socket_process_authenticating()
1331 int bus_socket_process_watch_bind(sd_bus *b) { in bus_socket_process_watch_bind() argument
1334 assert(b); in bus_socket_process_watch_bind()
1335 assert(b->state == BUS_WATCH_BIND); in bus_socket_process_watch_bind()
1336 assert(b->inotify_fd >= 0); in bus_socket_process_watch_bind()
1338 r = flush_fd(b->inotify_fd); in bus_socket_process_watch_bind()
1342 log_debug("Got inotify event on bus %s.", strna(b->description)); in bus_socket_process_watch_bind()
1347 r = bus_socket_connect(b); in bus_socket_process_watch_bind()
1351 q = bus_attach_io_events(b); in bus_socket_process_watch_bind()
1355 q = bus_attach_inotify_event(b); in bus_socket_process_watch_bind()