From a7e3212d89d5aefee67de79c1e7eaccf2f5645ac Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 25 Mar 2013 03:04:03 +0100 Subject: [PATCH] bus: split socket related code into bus-socket.[ch], to prepare for kdbus backend --- Makefile.am | 2 + src/libsystemd-bus/bus-internal.h | 2 + src/libsystemd-bus/bus-socket.c | 628 +++++++++++++++++++++++++ src/libsystemd-bus/bus-socket.h | 34 ++ src/libsystemd-bus/sd-bus.c | 734 ++++-------------------------- src/libsystemd-bus/sd-bus.h | 2 +- 6 files changed, 759 insertions(+), 643 deletions(-) create mode 100644 src/libsystemd-bus/bus-socket.c create mode 100644 src/libsystemd-bus/bus-socket.h diff --git a/Makefile.am b/Makefile.am index d81a3054418..f9589748338 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1661,6 +1661,8 @@ libsystemd_bus_la_SOURCES = \ src/libsystemd-bus/bus-error.h \ src/libsystemd-bus/bus-internal.c \ src/libsystemd-bus/bus-internal.h \ + src/libsystemd-bus/bus-socket.c \ + src/libsystemd-bus/bus-socket.h \ src/libsystemd-bus/bus-message.c \ src/libsystemd-bus/bus-message.h \ src/libsystemd-bus/bus-signature.c \ diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h index b7ed42ccd44..78d955d00f2 100644 --- a/src/libsystemd-bus/bus-internal.h +++ b/src/libsystemd-bus/bus-internal.h @@ -158,3 +158,5 @@ bool member_name_is_valid(const char *p); #define error_name_is_valid interface_name_is_valid int bus_ensure_running(sd_bus *bus); +int bus_start_running(sd_bus *bus); +int bus_next_address(sd_bus *bus); diff --git a/src/libsystemd-bus/bus-socket.c b/src/libsystemd-bus/bus-socket.c new file mode 100644 index 00000000000..f40aa59f511 --- /dev/null +++ b/src/libsystemd-bus/bus-socket.c @@ -0,0 +1,628 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2013 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include +#include +#include +#include +#include + +#include "util.h" +#include "macro.h" +#include "missing.h" +#include "strv.h" + +#include "sd-bus.h" +#include "bus-socket.h" +#include "bus-internal.h" +#include "bus-message.h" + +static void iovec_advance(struct iovec *iov, unsigned *idx, size_t size) { + + while (size > 0) { + struct iovec *i = iov + *idx; + + if (i->iov_len > size) { + i->iov_base = (uint8_t*) i->iov_base + size; + i->iov_len -= size; + return; + } + + size -= i->iov_len; + + i->iov_base = NULL; + i->iov_len = 0; + + (*idx) ++; + } +} + +static int bus_socket_write_auth(sd_bus *b) { + struct msghdr mh; + ssize_t k; + + assert(b); + assert(b->state == BUS_AUTHENTICATING); + + if (b->auth_index >= ELEMENTSOF(b->auth_iovec)) + return 0; + + if (b->auth_timeout == 0) + b->auth_timeout = now(CLOCK_MONOTONIC) + BUS_DEFAULT_TIMEOUT; + + zero(mh); + mh.msg_iov = b->auth_iovec + b->auth_index; + mh.msg_iovlen = ELEMENTSOF(b->auth_iovec) - b->auth_index; + + k = sendmsg(b->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL); + if (k < 0) + return errno == EAGAIN ? 0 : -errno; + + iovec_advance(b->auth_iovec, &b->auth_index, (size_t) k); + + return 1; +} + +static int bus_socket_auth_verify(sd_bus *b) { + char *e, *f, *start; + sd_id128_t peer; + unsigned i; + int r; + + /* We expect two response lines: "OK" and possibly + * "AGREE_UNIX_FD" */ + + e = memmem(b->rbuffer, b->rbuffer_size, "\r\n", 2); + if (!e) + return 0; + + if (b->negotiate_fds) { + f = memmem(e + 2, b->rbuffer_size - (e - (char*) b->rbuffer) - 2, "\r\n", 2); + if (!f) + return 0; + + start = f + 2; + } else { + f = NULL; + start = e + 2; + } + + /* Nice! We got all the lines we need. First check the OK + * line */ + + if (e - (char*) b->rbuffer != 3 + 32) + return -EPERM; + + if (memcmp(b->rbuffer, "OK ", 3)) + return -EPERM; + + for (i = 0; i < 32; i += 2) { + int x, y; + + x = unhexchar(((char*) b->rbuffer)[3 + i]); + y = unhexchar(((char*) b->rbuffer)[3 + i + 1]); + + if (x < 0 || y < 0) + return -EINVAL; + + peer.bytes[i/2] = ((uint8_t) x << 4 | (uint8_t) y); + } + + if (!sd_id128_equal(b->peer, SD_ID128_NULL) && + !sd_id128_equal(b->peer, peer)) + return -EPERM; + + b->peer = peer; + + /* And possibly check the second line, too */ + + if (f) + b->can_fds = + (f - e == sizeof("\r\nAGREE_UNIX_FD") - 1) && + memcmp(e + 2, "AGREE_UNIX_FD", sizeof("AGREE_UNIX_FD") - 1) == 0; + + b->rbuffer_size -= (start - (char*) b->rbuffer); + memmove(b->rbuffer, start, b->rbuffer_size); + + r = bus_start_running(b); + if (r < 0) + return r; + + return 1; +} + +static int bus_socket_read_auth(sd_bus *b) { + struct msghdr mh; + struct iovec iov; + size_t n; + ssize_t k; + int r; + void *p; + + assert(b); + + r = bus_socket_auth_verify(b); + if (r != 0) + return r; + + n = MAX(3 + 32 + 2 + sizeof("AGREE_UNIX_FD") - 1 + 2, b->rbuffer_size * 2); + + if (n > BUS_AUTH_SIZE_MAX) + n = BUS_AUTH_SIZE_MAX; + + if (b->rbuffer_size >= n) + return -ENOBUFS; + + p = realloc(b->rbuffer, n); + if (!p) + return -ENOMEM; + + b->rbuffer = p; + + zero(iov); + iov.iov_base = (uint8_t*) b->rbuffer + b->rbuffer_size; + iov.iov_len = n - b->rbuffer_size; + + zero(mh); + mh.msg_iov = &iov; + mh.msg_iovlen = 1; + + k = recvmsg(b->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL); + if (k < 0) + return errno == EAGAIN ? 0 : -errno; + if (k == 0) + return -ECONNRESET; + + b->rbuffer_size += k; + + r = bus_socket_auth_verify(b); + if (r != 0) + return r; + + return 1; +} + +static int bus_socket_setup(sd_bus *b) { + int one; + + assert(b); + + /* Enable SO_PASSCRED + SO_PASSEC. We try this on any socket, + * just in case. This is actually irrelavant for */ + one = 1; + setsockopt(b->fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)); + setsockopt(b->fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one)); + + /* Increase the buffers to a MB */ + fd_inc_rcvbuf(b->fd, 1024*1024); + fd_inc_sndbuf(b->fd, 1024*1024); + + return 0; +} + +static int bus_socket_start_auth(sd_bus *b) { + static const char auth_prefix[] = "\0AUTH EXTERNAL "; + static const char auth_suffix_with_unix_fd[] = "\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n"; + static const char auth_suffix_without_unix_fd[] = "\r\nBEGIN\r\n"; + + char text[20 + 1]; /* enough space for a 64bit integer plus NUL */ + size_t l; + const char *auth_suffix; + int domain = 0, r; + socklen_t sl; + + assert(b); + + b->state = BUS_AUTHENTICATING; + + sl = sizeof(domain); + r = getsockopt(b->fd, SOL_SOCKET, SO_DOMAIN, &domain, &sl); + if (r < 0) + return -errno; + + if (domain != AF_UNIX) + b->negotiate_fds = false; + + snprintf(text, sizeof(text), "%llu", (unsigned long long) geteuid()); + char_array_0(text); + + l = strlen(text); + b->auth_uid = hexmem(text, l); + if (!b->auth_uid) + return -ENOMEM; + + auth_suffix = b->negotiate_fds ? auth_suffix_with_unix_fd : auth_suffix_without_unix_fd; + + b->auth_iovec[0].iov_base = (void*) auth_prefix; + b->auth_iovec[0].iov_len = sizeof(auth_prefix) -1; + b->auth_iovec[1].iov_base = (void*) b->auth_uid; + b->auth_iovec[1].iov_len = l * 2; + b->auth_iovec[2].iov_base = (void*) auth_suffix; + b->auth_iovec[2].iov_len = strlen(auth_suffix); + b->auth_size = sizeof(auth_prefix) - 1 + l * 2 + sizeof(auth_suffix) - 1; + + return bus_socket_write_auth(b); +} + +int bus_socket_connect(sd_bus *b) { + int r; + + assert(b); + assert(b->fd < 0); + assert(b->sockaddr.sa.sa_family != AF_UNSPEC); + + b->fd = socket(b->sockaddr.sa.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); + if (b->fd < 0) + return -errno; + + r = bus_socket_setup(b); + if (r < 0) + return r; + + r = connect(b->fd, &b->sockaddr.sa, b->sockaddr_size); + if (r < 0) { + if (errno == EINPROGRESS) + return 1; + + return -errno; + } + + return bus_socket_start_auth(b); +} + +int bus_socket_exec(sd_bus *b) { + int s[2]; + pid_t pid; + + assert(b); + assert(b->fd < 0); + assert(b->exec_path); + + b->fd = socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0, s); + if (b->fd < 0) + return -errno; + + pid = fork(); + if (pid < 0) { + close_pipe(s); + return -errno; + } + if (pid == 0) { + /* Child */ + + close_all_fds(s, 2); + close_nointr_nofail(s[0]); + + assert_se(dup3(s[1], STDIN_FILENO, 0) == STDIN_FILENO); + assert_se(dup3(s[1], STDOUT_FILENO, 0) == STDOUT_FILENO); + + if (s[1] != STDIN_FILENO && s[1] != STDOUT_FILENO) + close_nointr_nofail(s[1]); + + fd_cloexec(STDIN_FILENO, false); + fd_cloexec(STDOUT_FILENO, false); + fd_nonblock(STDIN_FILENO, false); + fd_nonblock(STDOUT_FILENO, false); + + if (b->exec_argv) + execvp(b->exec_path, b->exec_argv); + else { + const char *argv[] = { b->exec_path, NULL }; + execvp(b->exec_path, (char**) argv); + } + + _exit(EXIT_FAILURE); + } + + close_nointr_nofail(s[1]); + b->fd = s[0]; + + return bus_socket_start_auth(b); +} + +int bus_socket_take_fd(sd_bus *b) { + int r; + assert(b); + + r = bus_socket_setup(b); + if (r < 0) + return r; + + return bus_socket_start_auth(b); +} + +int bus_socket_write_message(sd_bus *bus, sd_bus_message *m, size_t *idx) { + struct msghdr mh; + struct iovec *iov; + ssize_t k; + size_t n; + unsigned j; + + assert(bus); + assert(m); + assert(idx); + assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO); + + if (*idx >= m->size) + return 0; + zero(mh); + + if (m->n_fds > 0) { + struct cmsghdr *control; + control = alloca(CMSG_SPACE(sizeof(int) * m->n_fds)); + + mh.msg_control = control; + control->cmsg_level = SOL_SOCKET; + control->cmsg_type = SCM_RIGHTS; + mh.msg_controllen = control->cmsg_len = CMSG_LEN(sizeof(int) * m->n_fds); + memcpy(CMSG_DATA(control), m->fds, sizeof(int) * m->n_fds); + } + + n = m->n_iovec * sizeof(struct iovec); + iov = alloca(n); + memcpy(iov, m->iovec, n); + + j = 0; + iovec_advance(iov, &j, *idx); + + mh.msg_iov = iov; + mh.msg_iovlen = m->n_iovec; + + k = sendmsg(bus->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL); + if (k < 0) + return errno == EAGAIN ? 0 : -errno; + + *idx += (size_t) k; + return 1; +} + +static int bus_socket_read_message_need(sd_bus *bus, size_t *need) { + uint32_t a, b; + uint8_t e; + uint64_t sum; + + assert(bus); + assert(need); + assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO); + + if (bus->rbuffer_size < sizeof(struct bus_header)) { + *need = sizeof(struct bus_header) + 8; + + /* Minimum message size: + * + * Header + + * + * Method Call: +2 string headers + * Signal: +3 string headers + * Method Error: +1 string headers + * +1 uint32 headers + * Method Reply: +1 uint32 headers + * + * A string header is at least 9 bytes + * A uint32 header is at least 8 bytes + * + * Hence the minimum message size of a valid message + * is header + 8 bytes */ + + return 0; + } + + a = ((const uint32_t*) bus->rbuffer)[1]; + b = ((const uint32_t*) bus->rbuffer)[3]; + + e = ((const uint8_t*) bus->rbuffer)[0]; + if (e == SD_BUS_LITTLE_ENDIAN) { + a = le32toh(a); + b = le32toh(b); + } else if (e == SD_BUS_BIG_ENDIAN) { + a = be32toh(a); + b = be32toh(b); + } else + return -EBADMSG; + + sum = (uint64_t) sizeof(struct bus_header) + (uint64_t) ALIGN_TO(b, 8) + (uint64_t) a; + if (sum >= BUS_MESSAGE_SIZE_MAX) + return -ENOBUFS; + + *need = (size_t) sum; + return 0; +} + +static int bus_socket_make_message(sd_bus *bus, size_t size, sd_bus_message **m) { + sd_bus_message *t; + void *b; + int r; + + assert(bus); + assert(m); + assert(bus->rbuffer_size >= size); + assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO); + + if (bus->rbuffer_size > size) { + b = memdup((const uint8_t*) bus->rbuffer + size, + bus->rbuffer_size - size); + if (!b) + return -ENOMEM; + } else + b = NULL; + + r = bus_message_from_malloc(bus->rbuffer, size, + bus->fds, bus->n_fds, + bus->ucred_valid ? &bus->ucred : NULL, + bus->label[0] ? bus->label : NULL, + &t); + if (r < 0) { + free(b); + return r; + } + + bus->rbuffer = b; + bus->rbuffer_size -= size; + + bus->fds = NULL; + bus->n_fds = 0; + + *m = t; + return 1; +} + +int bus_socket_read_message(sd_bus *bus, sd_bus_message **m) { + struct msghdr mh; + struct iovec iov; + ssize_t k; + size_t need; + int r; + void *b; + union { + struct cmsghdr cmsghdr; + uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX) + + CMSG_SPACE(sizeof(struct ucred)) + + CMSG_SPACE(NAME_MAX)]; /*selinux label */ + } control; + struct cmsghdr *cmsg; + + assert(bus); + assert(m); + assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO); + + r = bus_socket_read_message_need(bus, &need); + if (r < 0) + return r; + + if (bus->rbuffer_size >= need) + return bus_socket_make_message(bus, need, m); + + b = realloc(bus->rbuffer, need); + if (!b) + return -ENOMEM; + + bus->rbuffer = b; + + zero(iov); + iov.iov_base = (uint8_t*) bus->rbuffer + bus->rbuffer_size; + iov.iov_len = need - bus->rbuffer_size; + + zero(mh); + mh.msg_iov = &iov; + mh.msg_iovlen = 1; + mh.msg_control = &control; + mh.msg_controllen = sizeof(control); + + k = recvmsg(bus->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_CMSG_CLOEXEC); + if (k < 0) + return errno == EAGAIN ? 0 : -errno; + if (k == 0) + return -ECONNRESET; + + bus->rbuffer_size += k; + + for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) { + if (cmsg->cmsg_level == SOL_SOCKET && + cmsg->cmsg_type == SCM_RIGHTS) { + int n, *f; + + n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); + + f = realloc(bus->fds, sizeof(int) + (bus->n_fds + n)); + if (!f) { + close_many((int*) CMSG_DATA(cmsg), n); + return -ENOMEM; + } + + memcpy(f + bus->n_fds, CMSG_DATA(cmsg), n * sizeof(int)); + bus->fds = f; + bus->n_fds += n; + } else if (cmsg->cmsg_level == SOL_SOCKET && + cmsg->cmsg_type == SCM_CREDENTIALS && + cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) { + + memcpy(&bus->ucred, CMSG_DATA(cmsg), sizeof(struct ucred)); + bus->ucred_valid = true; + + } else if (cmsg->cmsg_level == SOL_SOCKET && + cmsg->cmsg_type == SCM_SECURITY) { + + size_t l; + l = cmsg->cmsg_len - CMSG_LEN(0); + memcpy(&bus->label, CMSG_DATA(cmsg), l); + bus->label[l] = 0; + } + } + + r = bus_socket_read_message_need(bus, &need); + if (r < 0) + return r; + + if (bus->rbuffer_size >= need) + return bus_socket_make_message(bus, need, m); + + return 1; +} + +int bus_socket_process_opening(sd_bus *b) { + int error = 0; + socklen_t slen = sizeof(error); + struct pollfd p; + int r; + + assert(b); + assert(b->state == BUS_OPENING); + + zero(p); + p.fd = b->fd; + p.events = POLLOUT; + + r = poll(&p, 1, 0); + if (r < 0) + return -errno; + + if (!(p.revents & (POLLOUT|POLLERR|POLLHUP))) + return 0; + + r = getsockopt(b->fd, SOL_SOCKET, SO_ERROR, &error, &slen); + if (r < 0) + b->last_connect_error = errno; + else if (error != 0) + b->last_connect_error = error; + else if (p.revents & (POLLERR|POLLHUP)) + b->last_connect_error = ECONNREFUSED; + else + return bus_socket_start_auth(b); + + return bus_next_address(b); +} + +int bus_socket_process_authenticating(sd_bus *b) { + int r; + + assert(b); + assert(b->state == BUS_AUTHENTICATING); + + if (now(CLOCK_MONOTONIC) >= b->auth_timeout) + return -ETIMEDOUT; + + r = bus_socket_write_auth(b); + if (r != 0) + return r; + + return bus_socket_read_auth(b); +} diff --git a/src/libsystemd-bus/bus-socket.h b/src/libsystemd-bus/bus-socket.h new file mode 100644 index 00000000000..3656a442a3e --- /dev/null +++ b/src/libsystemd-bus/bus-socket.h @@ -0,0 +1,34 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +#pragma once + +/*** + This file is part of systemd. + + Copyright 2013 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include "sd-bus.h" + +int bus_socket_connect(sd_bus *b); +int bus_socket_exec(sd_bus *b); +int bus_socket_take_fd(sd_bus *b); + +int bus_socket_write_message(sd_bus *bus, sd_bus_message *m, size_t *idx); +int bus_socket_read_message(sd_bus *bus, sd_bus_message **m); + +int bus_socket_process_opening(sd_bus *b); +int bus_socket_process_authenticating(sd_bus *b); diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index 3bdbd111325..6eab4a5469c 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -29,13 +29,13 @@ #include "util.h" #include "macro.h" -#include "missing.h" #include "strv.h" #include "sd-bus.h" #include "bus-internal.h" #include "bus-message.h" #include "bus-type.h" +#include "bus-socket.h" static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec); @@ -250,7 +250,7 @@ static int bus_send_hello(sd_bus *bus) { return r; } -static int bus_start_running(sd_bus *bus) { +int bus_start_running(sd_bus *bus) { assert(bus); if (bus->send_hello) { @@ -648,310 +648,7 @@ static int bus_parse_next_address(sd_bus *b) { return 1; } -static void iovec_advance(struct iovec *iov, unsigned *idx, size_t size) { - - while (size > 0) { - struct iovec *i = iov + *idx; - - if (i->iov_len > size) { - i->iov_base = (uint8_t*) i->iov_base + size; - i->iov_len -= size; - return; - } - - size -= i->iov_len; - - i->iov_base = NULL; - i->iov_len = 0; - - (*idx) ++; - } -} - -static int bus_write_auth(sd_bus *b) { - struct msghdr mh; - ssize_t k; - - assert(b); - assert(b->state == BUS_AUTHENTICATING); - - if (b->auth_index >= ELEMENTSOF(b->auth_iovec)) - return 0; - - if (b->auth_timeout == 0) - b->auth_timeout = now(CLOCK_MONOTONIC) + BUS_DEFAULT_TIMEOUT; - - zero(mh); - mh.msg_iov = b->auth_iovec + b->auth_index; - mh.msg_iovlen = ELEMENTSOF(b->auth_iovec) - b->auth_index; - - k = sendmsg(b->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL); - if (k < 0) - return errno == EAGAIN ? 0 : -errno; - - iovec_advance(b->auth_iovec, &b->auth_index, (size_t) k); - - return 1; -} - -static int bus_auth_verify(sd_bus *b) { - char *e, *f, *start; - sd_id128_t peer; - unsigned i; - int r; - - /* We expect two response lines: "OK" and possibly - * "AGREE_UNIX_FD" */ - - e = memmem(b->rbuffer, b->rbuffer_size, "\r\n", 2); - if (!e) - return 0; - - if (b->negotiate_fds) { - f = memmem(e + 2, b->rbuffer_size - (e - (char*) b->rbuffer) - 2, "\r\n", 2); - if (!f) - return 0; - - start = f + 2; - } else { - f = NULL; - start = e + 2; - } - - /* Nice! We got all the lines we need. First check the OK - * line */ - - if (e - (char*) b->rbuffer != 3 + 32) - return -EPERM; - - if (memcmp(b->rbuffer, "OK ", 3)) - return -EPERM; - - for (i = 0; i < 32; i += 2) { - int x, y; - - x = unhexchar(((char*) b->rbuffer)[3 + i]); - y = unhexchar(((char*) b->rbuffer)[3 + i + 1]); - - if (x < 0 || y < 0) - return -EINVAL; - - peer.bytes[i/2] = ((uint8_t) x << 4 | (uint8_t) y); - } - - if (!sd_id128_equal(b->peer, SD_ID128_NULL) && - !sd_id128_equal(b->peer, peer)) - return -EPERM; - - b->peer = peer; - - /* And possibly check the second line, too */ - - if (f) - b->can_fds = - (f - e == sizeof("\r\nAGREE_UNIX_FD") - 1) && - memcmp(e + 2, "AGREE_UNIX_FD", sizeof("AGREE_UNIX_FD") - 1) == 0; - - b->rbuffer_size -= (start - (char*) b->rbuffer); - memmove(b->rbuffer, start, b->rbuffer_size); - - r = bus_start_running(b); - if (r < 0) - return r; - - return 1; -} - -static int bus_read_auth(sd_bus *b) { - struct msghdr mh; - struct iovec iov; - size_t n; - ssize_t k; - int r; - void *p; - - assert(b); - - r = bus_auth_verify(b); - if (r != 0) - return r; - - n = MAX(3 + 32 + 2 + sizeof("AGREE_UNIX_FD") - 1 + 2, b->rbuffer_size * 2); - - if (n > BUS_AUTH_SIZE_MAX) - n = BUS_AUTH_SIZE_MAX; - - if (b->rbuffer_size >= n) - return -ENOBUFS; - - p = realloc(b->rbuffer, n); - if (!p) - return -ENOMEM; - - b->rbuffer = p; - - zero(iov); - iov.iov_base = (uint8_t*) b->rbuffer + b->rbuffer_size; - iov.iov_len = n - b->rbuffer_size; - - zero(mh); - mh.msg_iov = &iov; - mh.msg_iovlen = 1; - - k = recvmsg(b->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL); - if (k < 0) - return errno == EAGAIN ? 0 : -errno; - if (k == 0) - return -ECONNRESET; - - b->rbuffer_size += k; - - r = bus_auth_verify(b); - if (r != 0) - return r; - - return 1; -} - -static int bus_setup_fd(sd_bus *b) { - int one; - - assert(b); - - /* Enable SO_PASSCRED + SO_PASSEC. We try this on any socket, - * just in case. This is actually irrelavant for */ - one = 1; - setsockopt(b->fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)); - setsockopt(b->fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one)); - - /* Increase the buffers to a MB */ - fd_inc_rcvbuf(b->fd, 1024*1024); - fd_inc_sndbuf(b->fd, 1024*1024); - - return 0; -} - -static int bus_start_auth(sd_bus *b) { - static const char auth_prefix[] = "\0AUTH EXTERNAL "; - static const char auth_suffix_with_unix_fd[] = "\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n"; - static const char auth_suffix_without_unix_fd[] = "\r\nBEGIN\r\n"; - - char text[20 + 1]; /* enough space for a 64bit integer plus NUL */ - size_t l; - const char *auth_suffix; - int domain = 0, r; - socklen_t sl; - - assert(b); - - b->state = BUS_AUTHENTICATING; - - sl = sizeof(domain); - r = getsockopt(b->fd, SOL_SOCKET, SO_DOMAIN, &domain, &sl); - if (r < 0) - return -errno; - - if (domain != AF_UNIX) - b->negotiate_fds = false; - - snprintf(text, sizeof(text), "%llu", (unsigned long long) geteuid()); - char_array_0(text); - - l = strlen(text); - b->auth_uid = hexmem(text, l); - if (!b->auth_uid) - return -ENOMEM; - - auth_suffix = b->negotiate_fds ? auth_suffix_with_unix_fd : auth_suffix_without_unix_fd; - - b->auth_iovec[0].iov_base = (void*) auth_prefix; - b->auth_iovec[0].iov_len = sizeof(auth_prefix) -1; - b->auth_iovec[1].iov_base = (void*) b->auth_uid; - b->auth_iovec[1].iov_len = l * 2; - b->auth_iovec[2].iov_base = (void*) auth_suffix; - b->auth_iovec[2].iov_len = strlen(auth_suffix); - b->auth_size = sizeof(auth_prefix) - 1 + l * 2 + sizeof(auth_suffix) - 1; - - return bus_write_auth(b); -} - -static int bus_connect(sd_bus *b) { - int r; - - assert(b); - assert(b->fd < 0); - assert(b->sockaddr.sa.sa_family != AF_UNSPEC); - - b->fd = socket(b->sockaddr.sa.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); - if (b->fd < 0) - return -errno; - - r = bus_setup_fd(b); - if (r < 0) - return r; - - r = connect(b->fd, &b->sockaddr.sa, b->sockaddr_size); - if (r < 0) { - if (errno == EINPROGRESS) - return 1; - - return -errno; - } - - return bus_start_auth(b); -} - -static int bus_exec(sd_bus *b) { - int s[2]; - pid_t pid; - - assert(b); - assert(b->fd < 0); - assert(b->exec_path); - - b->fd = socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0, s); - if (b->fd < 0) - return -errno; - - pid = fork(); - if (pid < 0) { - close_pipe(s); - return -errno; - } - if (pid == 0) { - /* Child */ - - close_all_fds(s, 2); - close_nointr_nofail(s[0]); - - assert_se(dup3(s[1], STDIN_FILENO, 0) == STDIN_FILENO); - assert_se(dup3(s[1], STDOUT_FILENO, 0) == STDOUT_FILENO); - - if (s[1] != STDIN_FILENO && s[1] != STDOUT_FILENO) - close_nointr_nofail(s[1]); - - fd_cloexec(STDIN_FILENO, false); - fd_cloexec(STDOUT_FILENO, false); - fd_nonblock(STDIN_FILENO, false); - fd_nonblock(STDOUT_FILENO, false); - - if (b->exec_argv) - execvp(b->exec_path, b->exec_argv); - else { - const char *argv[] = { b->exec_path, NULL }; - execvp(b->exec_path, (char**) argv); - } - - _exit(EXIT_FAILURE); - } - - close_nointr_nofail(s[1]); - b->fd = s[0]; - - return bus_start_auth(b); -} - -static int bus_start_connect(sd_bus *b) { +static int bus_start_address(sd_bus *b) { int r; assert(b); @@ -963,7 +660,8 @@ static int bus_start_connect(sd_bus *b) { } if (b->sockaddr.sa.sa_family != AF_UNSPEC) { - r = bus_connect(b); + + r = bus_socket_connect(b); if (r >= 0) return r; @@ -971,7 +669,7 @@ static int bus_start_connect(sd_bus *b) { } else if (b->exec_path) { - r = bus_exec(b); + r = bus_socket_exec(b); if (r >= 0) return r; @@ -986,6 +684,13 @@ static int bus_start_connect(sd_bus *b) { } } +int bus_next_address(sd_bus *b) { + assert(b); + + bus_reset_parsed_address(b); + return bus_start_address(b); +} + static int bus_start_fd(sd_bus *b) { int r; @@ -999,11 +704,7 @@ static int bus_start_fd(sd_bus *b) { if (r < 0) return r; - r = bus_setup_fd(b); - if (r < 0) - return r; - - return bus_start_auth(b); + return bus_socket_take_fd(b); } int sd_bus_start(sd_bus *bus) { @@ -1019,7 +720,7 @@ int sd_bus_start(sd_bus *bus) { if (bus->fd >= 0) r = bus_start_fd(bus); else if (bus->address) - r = bus_start_connect(bus); + r = bus_start_address(bus); else return -EINVAL; @@ -1206,234 +907,6 @@ static int bus_seal_message(sd_bus *b, sd_bus_message *m) { return bus_message_seal(m, ++b->serial); } -static int message_write(sd_bus *bus, sd_bus_message *m, size_t *idx) { - struct msghdr mh; - struct iovec *iov; - ssize_t k; - size_t n; - unsigned j; - - assert(bus); - assert(m); - assert(idx); - assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO); - - if (*idx >= m->size) - return 0; - zero(mh); - - if (m->n_fds > 0) { - struct cmsghdr *control; - control = alloca(CMSG_SPACE(sizeof(int) * m->n_fds)); - - mh.msg_control = control; - control->cmsg_level = SOL_SOCKET; - control->cmsg_type = SCM_RIGHTS; - mh.msg_controllen = control->cmsg_len = CMSG_LEN(sizeof(int) * m->n_fds); - memcpy(CMSG_DATA(control), m->fds, sizeof(int) * m->n_fds); - } - - n = m->n_iovec * sizeof(struct iovec); - iov = alloca(n); - memcpy(iov, m->iovec, n); - - j = 0; - iovec_advance(iov, &j, *idx); - - mh.msg_iov = iov; - mh.msg_iovlen = m->n_iovec; - - k = sendmsg(bus->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL); - if (k < 0) - return errno == EAGAIN ? 0 : -errno; - - *idx += (size_t) k; - return 1; -} - -static int message_read_need(sd_bus *bus, size_t *need) { - uint32_t a, b; - uint8_t e; - uint64_t sum; - - assert(bus); - assert(need); - assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO); - - if (bus->rbuffer_size < sizeof(struct bus_header)) { - *need = sizeof(struct bus_header) + 8; - - /* Minimum message size: - * - * Header + - * - * Method Call: +2 string headers - * Signal: +3 string headers - * Method Error: +1 string headers - * +1 uint32 headers - * Method Reply: +1 uint32 headers - * - * A string header is at least 9 bytes - * A uint32 header is at least 8 bytes - * - * Hence the minimum message size of a valid message - * is header + 8 bytes */ - - return 0; - } - - a = ((const uint32_t*) bus->rbuffer)[1]; - b = ((const uint32_t*) bus->rbuffer)[3]; - - e = ((const uint8_t*) bus->rbuffer)[0]; - if (e == SD_BUS_LITTLE_ENDIAN) { - a = le32toh(a); - b = le32toh(b); - } else if (e == SD_BUS_BIG_ENDIAN) { - a = be32toh(a); - b = be32toh(b); - } else - return -EBADMSG; - - sum = (uint64_t) sizeof(struct bus_header) + (uint64_t) ALIGN_TO(b, 8) + (uint64_t) a; - if (sum >= BUS_MESSAGE_SIZE_MAX) - return -ENOBUFS; - - *need = (size_t) sum; - return 0; -} - -static int message_make(sd_bus *bus, size_t size, sd_bus_message **m) { - sd_bus_message *t; - void *b; - int r; - - assert(bus); - assert(m); - assert(bus->rbuffer_size >= size); - assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO); - - if (bus->rbuffer_size > size) { - b = memdup((const uint8_t*) bus->rbuffer + size, - bus->rbuffer_size - size); - if (!b) - return -ENOMEM; - } else - b = NULL; - - r = bus_message_from_malloc(bus->rbuffer, size, - bus->fds, bus->n_fds, - bus->ucred_valid ? &bus->ucred : NULL, - bus->label[0] ? bus->label : NULL, - &t); - if (r < 0) { - free(b); - return r; - } - - bus->rbuffer = b; - bus->rbuffer_size -= size; - - bus->fds = NULL; - bus->n_fds = 0; - - *m = t; - return 1; -} - -static int message_read(sd_bus *bus, sd_bus_message **m) { - struct msghdr mh; - struct iovec iov; - ssize_t k; - size_t need; - int r; - void *b; - union { - struct cmsghdr cmsghdr; - uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX) + - CMSG_SPACE(sizeof(struct ucred)) + - CMSG_SPACE(NAME_MAX)]; /*selinux label */ - } control; - struct cmsghdr *cmsg; - - assert(bus); - assert(m); - assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO); - - r = message_read_need(bus, &need); - if (r < 0) - return r; - - if (bus->rbuffer_size >= need) - return message_make(bus, need, m); - - b = realloc(bus->rbuffer, need); - if (!b) - return -ENOMEM; - - bus->rbuffer = b; - - zero(iov); - iov.iov_base = (uint8_t*) bus->rbuffer + bus->rbuffer_size; - iov.iov_len = need - bus->rbuffer_size; - - zero(mh); - mh.msg_iov = &iov; - mh.msg_iovlen = 1; - mh.msg_control = &control; - mh.msg_controllen = sizeof(control); - - k = recvmsg(bus->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_CMSG_CLOEXEC); - if (k < 0) - return errno == EAGAIN ? 0 : -errno; - if (k == 0) - return -ECONNRESET; - - bus->rbuffer_size += k; - - for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) { - if (cmsg->cmsg_level == SOL_SOCKET && - cmsg->cmsg_type == SCM_RIGHTS) { - int n, *f; - - n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); - - f = realloc(bus->fds, sizeof(int) + (bus->n_fds + n)); - if (!f) { - close_many((int*) CMSG_DATA(cmsg), n); - return -ENOMEM; - } - - memcpy(f + bus->n_fds, CMSG_DATA(cmsg), n * sizeof(int)); - bus->fds = f; - bus->n_fds += n; - } else if (cmsg->cmsg_level == SOL_SOCKET && - cmsg->cmsg_type == SCM_CREDENTIALS && - cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) { - - memcpy(&bus->ucred, CMSG_DATA(cmsg), sizeof(struct ucred)); - bus->ucred_valid = true; - - } else if (cmsg->cmsg_level == SOL_SOCKET && - cmsg->cmsg_type == SCM_SECURITY) { - - size_t l; - l = cmsg->cmsg_len - CMSG_LEN(0); - memcpy(&bus->label, CMSG_DATA(cmsg), l); - bus->label[l] = 0; - } - } - - r = message_read_need(bus, &need); - if (r < 0) - return r; - - if (bus->rbuffer_size >= need) - return message_make(bus, need, m); - - return 1; -} - static int dispatch_wqueue(sd_bus *bus) { int r, ret = 0; @@ -1445,7 +918,7 @@ static int dispatch_wqueue(sd_bus *bus) { while (bus->wqueue_size > 0) { - r = message_write(bus, bus->wqueue[0], &bus->windex); + r = bus_socket_write_message(bus, bus->wqueue[0], &bus->windex); if (r < 0) { sd_bus_close(bus); return r; @@ -1497,7 +970,7 @@ static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) { /* Try to read a new message */ do { - r = message_read(bus, &z); + r = bus_socket_read_message(bus, &z); if (r < 0) { sd_bus_close(bus); return r; @@ -1549,7 +1022,7 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) { if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) { size_t idx = 0; - r = message_write(bus, m, &idx); + r = bus_socket_write_message(bus, m, &idx); if (r < 0) { sd_bus_close(bus); return r; @@ -1791,7 +1264,7 @@ int sd_bus_send_with_reply_and_block( room = true; } - r = message_read(bus, &incoming); + r = bus_socket_read_message(bus, &incoming); if (r < 0) return r; if (incoming) { @@ -2119,6 +1592,61 @@ static int process_message(sd_bus *bus, sd_bus_message *m) { return process_object(bus, m); } +static int process_running(sd_bus *bus, sd_bus_message **ret) { + _cleanup_bus_message_unref_ sd_bus_message *m = NULL; + int r; + + assert(bus); + assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO); + + r = process_timeout(bus); + if (r != 0) + goto null_message; + + r = dispatch_wqueue(bus); + if (r != 0) + goto null_message; + + r = dispatch_rqueue(bus, &m); + if (r < 0) + return r; + if (!m) + goto null_message; + + r = process_message(bus, m); + if (r != 0) + goto null_message; + + if (ret) { + *ret = m; + m = NULL; + return 1; + } + + if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL) { + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_INIT; + + sd_bus_error_set(&error, "org.freedesktop.DBus.Error.UnknownObject", "Unknown object '%s'.", m->path); + + r = sd_bus_message_new_method_error(bus, m, &error, &reply); + if (r < 0) + return r; + + r = sd_bus_send(bus, reply, NULL); + if (r < 0) + return r; + } + + return 1; + +null_message: + if (r >= 0 && ret) + *ret = NULL; + + return r; +} + int sd_bus_process(sd_bus *bus, sd_bus_message **ret) { int r; @@ -2129,116 +1657,38 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) { if (!bus) return -EINVAL; - if (bus->state == BUS_UNSET) - return -ENOTCONN; if (bus->fd < 0) return -ENOTCONN; - if (bus->state == BUS_OPENING) { - struct pollfd p; + switch (bus->state) { - zero(p); - p.fd = bus->fd; - p.events = POLLOUT; + case BUS_UNSET: + return -ENOTCONN; - r = poll(&p, 1, 0); - if (r < 0) - return -errno; - - if (p.revents & (POLLOUT|POLLERR|POLLHUP)) { - int error = 0; - socklen_t slen = sizeof(error); - - r = getsockopt(bus->fd, SOL_SOCKET, SO_ERROR, &error, &slen); - if (r < 0) - bus->last_connect_error = errno; - else if (error != 0) - bus->last_connect_error = error; - else if (p.revents & (POLLERR|POLLHUP)) - bus->last_connect_error = ECONNREFUSED; - else { - r = bus_start_auth(bus); - goto null_message; - } - - /* Try next address */ - bus_reset_parsed_address(bus); - r = bus_start_connect(bus); - goto null_message; - } - - r = 0; - goto null_message; - - } else if (bus->state == BUS_AUTHENTICATING) { - - if (now(CLOCK_MONOTONIC) >= bus->auth_timeout) - return -ETIMEDOUT; - - r = bus_write_auth(bus); - if (r != 0) - goto null_message; - - r = bus_read_auth(bus); - goto null_message; - - } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) { - _cleanup_bus_message_unref_ sd_bus_message *m = NULL; - int k; - - r = process_timeout(bus); - if (r != 0) - goto null_message; - - r = dispatch_wqueue(bus); - if (r != 0) - goto null_message; - - k = r; - r = dispatch_rqueue(bus, &m); + case BUS_OPENING: + r = bus_socket_process_opening(bus); if (r < 0) return r; - if (!m) { - if (r == 0) - r = k; - goto null_message; - } + if (ret) + *ret = NULL; + return r; - r = process_message(bus, m); - if (r != 0) - goto null_message; + case BUS_AUTHENTICATING: - if (ret) { - *ret = m; - m = NULL; - return 1; - } + r = bus_socket_process_authenticating(bus); + if (r < 0) + return r; + if (ret) + *ret = NULL; + return r; - if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL) { - _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; - _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_INIT; + case BUS_RUNNING: + case BUS_HELLO: - sd_bus_error_set(&error, "org.freedesktop.DBus.Error.UnknownObject", "Unknown object '%s'.", m->path); - - r = sd_bus_message_new_method_error(bus, m, &error, &reply); - if (r < 0) - return r; - - r = sd_bus_send(bus, reply, NULL); - if (r < 0) - return r; - } - - return 1; + return process_running(bus, ret); } assert_not_reached("Unknown state"); - -null_message: - if (r >= 0 && ret) - *ret = NULL; - - return r; } static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) { diff --git a/src/libsystemd-bus/sd-bus.h b/src/libsystemd-bus/sd-bus.h index e5990ad4310..a15cc26e3f0 100644 --- a/src/libsystemd-bus/sd-bus.h +++ b/src/libsystemd-bus/sd-bus.h @@ -25,12 +25,12 @@ #include #include +#include #include "sd-bus-protocol.h" /* TODO: * - implicitly add stub introspection calls * - server side - * - split out actual sending logic into backend-socket.c * * Later: * - add page donation logic