2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2013-03-25 03:04:03 +01:00
|
|
|
/***
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <endian.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <poll.h>
|
2013-03-25 03:04:03 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "sd-bus.h"
|
2015-05-29 20:14:11 +02:00
|
|
|
#include "sd-daemon.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "bus-internal.h"
|
|
|
|
#include "bus-message.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "bus-socket.h"
|
|
|
|
#include "fd-util.h"
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2017-12-15 22:24:52 +01:00
|
|
|
#include "fs-util.h"
|
2015-10-27 01:26:31 +01:00
|
|
|
#include "hexdecoct.h"
|
2017-12-15 22:24:52 +01:00
|
|
|
#include "io-util.h"
|
2013-03-25 03:04:03 +01:00
|
|
|
#include "macro.h"
|
|
|
|
#include "missing.h"
|
2017-12-15 22:24:52 +01:00
|
|
|
#include "path-util.h"
|
2015-11-11 12:55:32 +01:00
|
|
|
#include "selinux-util.h"
|
2015-05-29 20:14:11 +02:00
|
|
|
#include "signal-util.h"
|
2015-10-27 01:26:31 +01:00
|
|
|
#include "stdio-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2015-10-25 22:32:30 +01:00
|
|
|
#include "user-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "utf8.h"
|
|
|
|
#include "util.h"
|
2013-03-25 03:04:03 +01:00
|
|
|
|
2013-12-16 17:05:51 +01:00
|
|
|
#define SNDBUF_SIZE (8*1024*1024)
|
|
|
|
|
2013-03-30 15:21:06 +01:00
|
|
|
static void iovec_advance(struct iovec iov[], unsigned *idx, size_t size) {
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-02-23 05:32:04 +01:00
|
|
|
(*idx)++;
|
2013-03-25 03:04:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-14 22:24:26 +02:00
|
|
|
static int append_iovec(sd_bus_message *m, const void *p, size_t sz) {
|
2013-04-11 02:09:36 +02:00
|
|
|
assert(m);
|
|
|
|
assert(p);
|
|
|
|
assert(sz > 0);
|
|
|
|
|
|
|
|
m->iovec[m->n_iovec].iov_base = (void*) p;
|
|
|
|
m->iovec[m->n_iovec].iov_len = sz;
|
|
|
|
m->n_iovec++;
|
2013-05-14 22:24:26 +02:00
|
|
|
|
|
|
|
return 0;
|
2013-04-11 02:09:36 +02:00
|
|
|
}
|
|
|
|
|
2013-05-14 22:24:26 +02:00
|
|
|
static int bus_message_setup_iovec(sd_bus_message *m) {
|
|
|
|
struct bus_body_part *part;
|
2013-05-15 01:36:09 +02:00
|
|
|
unsigned n, i;
|
2013-05-14 22:24:26 +02:00
|
|
|
int r;
|
|
|
|
|
2013-04-11 02:09:36 +02:00
|
|
|
assert(m);
|
|
|
|
assert(m->sealed);
|
|
|
|
|
|
|
|
if (m->n_iovec > 0)
|
2013-05-14 22:24:26 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
assert(!m->iovec);
|
2013-04-11 02:09:36 +02:00
|
|
|
|
2013-05-15 02:56:45 +02:00
|
|
|
n = 1 + m->n_body_parts;
|
2013-05-14 22:24:26 +02:00
|
|
|
if (n < ELEMENTSOF(m->iovec_fixed))
|
|
|
|
m->iovec = m->iovec_fixed;
|
|
|
|
else {
|
|
|
|
m->iovec = new(struct iovec, n);
|
2013-05-16 16:26:35 +02:00
|
|
|
if (!m->iovec) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-05-14 22:24:26 +02:00
|
|
|
}
|
2013-04-11 02:09:36 +02:00
|
|
|
|
2013-05-15 02:56:45 +02:00
|
|
|
r = append_iovec(m, m->header, BUS_MESSAGE_BODY_BEGIN(m));
|
2013-05-14 22:24:26 +02:00
|
|
|
if (r < 0)
|
2013-05-16 16:26:35 +02:00
|
|
|
goto fail;
|
2013-04-11 02:09:36 +02:00
|
|
|
|
2013-05-15 01:43:15 +02:00
|
|
|
MESSAGE_FOREACH_PART(part, i, m) {
|
2013-05-16 16:26:35 +02:00
|
|
|
r = bus_body_part_map(part);
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2013-05-14 22:24:26 +02:00
|
|
|
r = append_iovec(m, part->data, part->size);
|
|
|
|
if (r < 0)
|
2013-05-16 16:26:35 +02:00
|
|
|
goto fail;
|
2013-05-14 22:24:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(n == m->n_iovec);
|
|
|
|
|
|
|
|
return 0;
|
2013-05-16 16:26:35 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
m->poisoned = true;
|
|
|
|
return r;
|
2013-04-11 02:09:36 +02:00
|
|
|
}
|
|
|
|
|
2013-03-30 15:21:06 +01:00
|
|
|
bool bus_socket_auth_needs_write(sd_bus *b) {
|
|
|
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
if (b->auth_index >= ELEMENTSOF(b->auth_iovec))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (i = b->auth_index; i < ELEMENTSOF(b->auth_iovec); i++) {
|
|
|
|
struct iovec *j = b->auth_iovec + i;
|
|
|
|
|
|
|
|
if (j->iov_len > 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-25 03:04:03 +01:00
|
|
|
static int bus_socket_write_auth(sd_bus *b) {
|
|
|
|
ssize_t k;
|
|
|
|
|
|
|
|
assert(b);
|
|
|
|
assert(b->state == BUS_AUTHENTICATING);
|
|
|
|
|
2013-03-30 15:21:06 +01:00
|
|
|
if (!bus_socket_auth_needs_write(b))
|
2013-03-25 03:04:03 +01:00
|
|
|
return 0;
|
|
|
|
|
2013-03-31 20:19:18 +02:00
|
|
|
if (b->prefer_writev)
|
|
|
|
k = writev(b->output_fd, b->auth_iovec + b->auth_index, ELEMENTSOF(b->auth_iovec) - b->auth_index);
|
|
|
|
else {
|
|
|
|
struct msghdr mh;
|
|
|
|
zero(mh);
|
|
|
|
|
|
|
|
mh.msg_iov = b->auth_iovec + b->auth_index;
|
|
|
|
mh.msg_iovlen = ELEMENTSOF(b->auth_iovec) - b->auth_index;
|
|
|
|
|
|
|
|
k = sendmsg(b->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
|
|
|
|
if (k < 0 && errno == ENOTSOCK) {
|
|
|
|
b->prefer_writev = true;
|
|
|
|
k = writev(b->output_fd, b->auth_iovec + b->auth_index, ELEMENTSOF(b->auth_iovec) - b->auth_index);
|
|
|
|
}
|
|
|
|
}
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
if (k < 0)
|
|
|
|
return errno == EAGAIN ? 0 : -errno;
|
|
|
|
|
|
|
|
iovec_advance(b->auth_iovec, &b->auth_index, (size_t) k);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-03-30 15:21:06 +01:00
|
|
|
static int bus_socket_auth_verify_client(sd_bus *b) {
|
2013-03-25 03:04:03 +01:00
|
|
|
char *e, *f, *start;
|
|
|
|
sd_id128_t peer;
|
|
|
|
unsigned i;
|
|
|
|
int r;
|
|
|
|
|
2013-03-30 15:21:06 +01:00
|
|
|
assert(b);
|
|
|
|
|
2013-03-25 03:04:03 +01:00
|
|
|
/* We expect two response lines: "OK" and possibly
|
|
|
|
* "AGREE_UNIX_FD" */
|
|
|
|
|
2015-05-14 11:30:59 +02:00
|
|
|
e = memmem_safe(b->rbuffer, b->rbuffer_size, "\r\n", 2);
|
2013-03-25 03:04:03 +01:00
|
|
|
if (!e)
|
|
|
|
return 0;
|
|
|
|
|
2017-12-18 14:21:34 +01:00
|
|
|
if (b->accept_fd) {
|
2013-03-25 03:04:03 +01:00
|
|
|
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;
|
|
|
|
|
2013-03-30 15:21:06 +01:00
|
|
|
b->auth = b->anonymous_auth ? BUS_AUTH_ANONYMOUS : BUS_AUTH_EXTERNAL;
|
|
|
|
|
2013-03-25 03:04:03 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-07-21 16:06:31 +02:00
|
|
|
if (!sd_id128_is_null(b->server_id) &&
|
2013-03-31 16:46:21 +02:00
|
|
|
!sd_id128_equal(b->server_id, peer))
|
2013-03-25 03:04:03 +01:00
|
|
|
return -EPERM;
|
|
|
|
|
2013-03-31 16:46:21 +02:00
|
|
|
b->server_id = peer;
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
/* And possibly check the second line, too */
|
|
|
|
|
|
|
|
if (f)
|
|
|
|
b->can_fds =
|
2017-12-14 19:02:29 +01:00
|
|
|
(f - e == STRLEN("\r\nAGREE_UNIX_FD")) &&
|
|
|
|
memcmp(e + 2, "AGREE_UNIX_FD",
|
|
|
|
STRLEN("AGREE_UNIX_FD")) == 0;
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-03-30 15:21:06 +01:00
|
|
|
static bool line_equals(const char *s, size_t m, const char *line) {
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
l = strlen(line);
|
|
|
|
if (l != m)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return memcmp(s, line, l) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool line_begins(const char *s, size_t m, const char *word) {
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
l = strlen(word);
|
|
|
|
if (m < l)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (memcmp(s, word, l) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return m == l || (m > l && s[l] == ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
static int verify_anonymous_token(sd_bus *b, const char *p, size_t l) {
|
|
|
|
_cleanup_free_ char *token = NULL;
|
2015-07-11 19:14:52 +02:00
|
|
|
size_t len;
|
|
|
|
int r;
|
2013-03-30 15:21:06 +01:00
|
|
|
|
|
|
|
if (!b->anonymous_auth)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (l <= 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
assert(p[0] == ' ');
|
|
|
|
p++; l--;
|
|
|
|
|
|
|
|
if (l % 2 != 0)
|
|
|
|
return 0;
|
|
|
|
|
2015-07-11 19:14:52 +02:00
|
|
|
r = unhexmem(p, l, (void **) &token, &len);
|
|
|
|
if (r < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (memchr(token, 0, len))
|
2013-03-30 15:21:06 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return !!utf8_is_valid(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int verify_external_token(sd_bus *b, const char *p, size_t l) {
|
|
|
|
_cleanup_free_ char *token = NULL;
|
2015-07-11 19:14:52 +02:00
|
|
|
size_t len;
|
2013-03-30 15:21:06 +01:00
|
|
|
uid_t u;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
/* We don't do any real authentication here. Instead, we if
|
|
|
|
* the owner of this bus wanted authentication he should have
|
|
|
|
* checked SO_PEERCRED before even creating the bus object. */
|
|
|
|
|
2013-04-01 03:24:03 +02:00
|
|
|
if (!b->anonymous_auth && !b->ucred_valid)
|
2013-03-30 15:21:06 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (l <= 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
assert(p[0] == ' ');
|
|
|
|
p++; l--;
|
|
|
|
|
|
|
|
if (l % 2 != 0)
|
|
|
|
return 0;
|
|
|
|
|
2015-07-11 19:14:52 +02:00
|
|
|
r = unhexmem(p, l, (void**) &token, &len);
|
|
|
|
if (r < 0)
|
|
|
|
return 0;
|
2013-03-30 15:21:06 +01:00
|
|
|
|
2015-07-11 19:14:52 +02:00
|
|
|
if (memchr(token, 0, len))
|
2013-03-30 15:21:06 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = parse_uid(token, &u);
|
|
|
|
if (r < 0)
|
|
|
|
return 0;
|
|
|
|
|
2013-04-01 03:24:03 +02:00
|
|
|
/* We ignore the passed value if anonymous authentication is
|
|
|
|
* on anyway. */
|
|
|
|
if (!b->anonymous_auth && u != b->ucred.uid)
|
2013-03-30 15:21:06 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bus_socket_auth_write(sd_bus *b, const char *t) {
|
|
|
|
char *p;
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
assert(b);
|
|
|
|
assert(t);
|
|
|
|
|
|
|
|
/* We only make use of the first iovec */
|
2017-09-28 10:17:04 +02:00
|
|
|
assert(IN_SET(b->auth_index, 0, 1));
|
2013-03-30 15:21:06 +01:00
|
|
|
|
|
|
|
l = strlen(t);
|
|
|
|
p = malloc(b->auth_iovec[0].iov_len + l);
|
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2016-02-02 03:57:41 +01:00
|
|
|
memcpy_safe(p, b->auth_iovec[0].iov_base, b->auth_iovec[0].iov_len);
|
2013-03-30 15:21:06 +01:00
|
|
|
memcpy(p + b->auth_iovec[0].iov_len, t, l);
|
|
|
|
|
|
|
|
b->auth_iovec[0].iov_base = p;
|
|
|
|
b->auth_iovec[0].iov_len += l;
|
|
|
|
|
|
|
|
free(b->auth_buffer);
|
|
|
|
b->auth_buffer = p;
|
|
|
|
b->auth_index = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bus_socket_auth_write_ok(sd_bus *b) {
|
|
|
|
char t[3 + 32 + 2 + 1];
|
|
|
|
|
|
|
|
assert(b);
|
|
|
|
|
2015-01-27 14:00:11 +01:00
|
|
|
xsprintf(t, "OK " SD_ID128_FORMAT_STR "\r\n", SD_ID128_FORMAT_VAL(b->server_id));
|
2013-03-30 15:21:06 +01:00
|
|
|
|
|
|
|
return bus_socket_auth_write(b, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bus_socket_auth_verify_server(sd_bus *b) {
|
|
|
|
char *e;
|
|
|
|
const char *line;
|
|
|
|
size_t l;
|
|
|
|
bool processed = false;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(b);
|
|
|
|
|
2013-04-01 03:24:43 +02:00
|
|
|
if (b->rbuffer_size < 1)
|
2013-03-30 15:21:06 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* First char must be a NUL byte */
|
|
|
|
if (*(char*) b->rbuffer != 0)
|
|
|
|
return -EIO;
|
|
|
|
|
2013-04-01 03:24:43 +02:00
|
|
|
if (b->rbuffer_size < 3)
|
|
|
|
return 0;
|
|
|
|
|
2013-03-30 15:21:06 +01:00
|
|
|
/* Begin with the first line */
|
|
|
|
if (b->auth_rbegin <= 0)
|
|
|
|
b->auth_rbegin = 1;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
/* Check if line is complete */
|
|
|
|
line = (char*) b->rbuffer + b->auth_rbegin;
|
|
|
|
e = memmem(line, b->rbuffer_size - b->auth_rbegin, "\r\n", 2);
|
|
|
|
if (!e)
|
|
|
|
return processed;
|
|
|
|
|
|
|
|
l = e - line;
|
|
|
|
|
|
|
|
if (line_begins(line, l, "AUTH ANONYMOUS")) {
|
|
|
|
|
|
|
|
r = verify_anonymous_token(b, line + 14, l - 14);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r == 0)
|
|
|
|
r = bus_socket_auth_write(b, "REJECTED\r\n");
|
|
|
|
else {
|
|
|
|
b->auth = BUS_AUTH_ANONYMOUS;
|
|
|
|
r = bus_socket_auth_write_ok(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (line_begins(line, l, "AUTH EXTERNAL")) {
|
|
|
|
|
|
|
|
r = verify_external_token(b, line + 13, l - 13);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r == 0)
|
|
|
|
r = bus_socket_auth_write(b, "REJECTED\r\n");
|
|
|
|
else {
|
|
|
|
b->auth = BUS_AUTH_EXTERNAL;
|
|
|
|
r = bus_socket_auth_write_ok(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (line_begins(line, l, "AUTH"))
|
|
|
|
r = bus_socket_auth_write(b, "REJECTED EXTERNAL ANONYMOUS\r\n");
|
|
|
|
else if (line_equals(line, l, "CANCEL") ||
|
|
|
|
line_begins(line, l, "ERROR")) {
|
|
|
|
|
|
|
|
b->auth = _BUS_AUTH_INVALID;
|
|
|
|
r = bus_socket_auth_write(b, "REJECTED\r\n");
|
|
|
|
|
|
|
|
} else if (line_equals(line, l, "BEGIN")) {
|
|
|
|
|
|
|
|
if (b->auth == _BUS_AUTH_INVALID)
|
|
|
|
r = bus_socket_auth_write(b, "ERROR\r\n");
|
|
|
|
else {
|
|
|
|
/* We can't leave from the auth phase
|
|
|
|
* before we haven't written
|
|
|
|
* everything queued, so let's check
|
|
|
|
* that */
|
|
|
|
|
|
|
|
if (bus_socket_auth_needs_write(b))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
b->rbuffer_size -= (e + 2 - (char*) b->rbuffer);
|
|
|
|
memmove(b->rbuffer, e + 2, b->rbuffer_size);
|
|
|
|
return bus_start_running(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (line_begins(line, l, "DATA")) {
|
|
|
|
|
|
|
|
if (b->auth == _BUS_AUTH_INVALID)
|
|
|
|
r = bus_socket_auth_write(b, "ERROR\r\n");
|
|
|
|
else {
|
|
|
|
if (b->auth == BUS_AUTH_ANONYMOUS)
|
|
|
|
r = verify_anonymous_token(b, line + 4, l - 4);
|
|
|
|
else
|
|
|
|
r = verify_external_token(b, line + 4, l - 4);
|
|
|
|
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r == 0) {
|
|
|
|
b->auth = _BUS_AUTH_INVALID;
|
|
|
|
r = bus_socket_auth_write(b, "REJECTED\r\n");
|
|
|
|
} else
|
|
|
|
r = bus_socket_auth_write_ok(b);
|
|
|
|
}
|
|
|
|
} else if (line_equals(line, l, "NEGOTIATE_UNIX_FD")) {
|
2017-12-18 14:21:34 +01:00
|
|
|
if (b->auth == _BUS_AUTH_INVALID || !b->accept_fd)
|
2013-03-30 15:21:06 +01:00
|
|
|
r = bus_socket_auth_write(b, "ERROR\r\n");
|
|
|
|
else {
|
|
|
|
b->can_fds = true;
|
|
|
|
r = bus_socket_auth_write(b, "AGREE_UNIX_FD\r\n");
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
r = bus_socket_auth_write(b, "ERROR\r\n");
|
|
|
|
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
b->auth_rbegin = e + 2 - (char*) b->rbuffer;
|
|
|
|
|
|
|
|
processed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bus_socket_auth_verify(sd_bus *b) {
|
|
|
|
assert(b);
|
|
|
|
|
|
|
|
if (b->is_server)
|
|
|
|
return bus_socket_auth_verify_server(b);
|
|
|
|
else
|
|
|
|
return bus_socket_auth_verify_client(b);
|
|
|
|
}
|
|
|
|
|
2013-03-25 03:04:03 +01:00
|
|
|
static int bus_socket_read_auth(sd_bus *b) {
|
|
|
|
struct msghdr mh;
|
2015-05-16 17:06:43 +02:00
|
|
|
struct iovec iov = {};
|
2013-03-25 03:04:03 +01:00
|
|
|
size_t n;
|
|
|
|
ssize_t k;
|
|
|
|
int r;
|
|
|
|
void *p;
|
2013-03-30 15:21:06 +01:00
|
|
|
union {
|
|
|
|
struct cmsghdr cmsghdr;
|
2015-06-07 00:59:27 +02:00
|
|
|
uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX)];
|
2013-03-30 15:21:06 +01:00
|
|
|
} control;
|
2013-03-31 20:19:18 +02:00
|
|
|
bool handle_cmsg = false;
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
assert(b);
|
2013-03-30 15:21:06 +01:00
|
|
|
assert(b->state == BUS_AUTHENTICATING);
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
r = bus_socket_auth_verify(b);
|
|
|
|
if (r != 0)
|
|
|
|
return r;
|
|
|
|
|
2013-04-01 08:08:05 +02:00
|
|
|
n = MAX(256u, b->rbuffer_size * 2);
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
iov.iov_base = (uint8_t*) b->rbuffer + b->rbuffer_size;
|
|
|
|
iov.iov_len = n - b->rbuffer_size;
|
|
|
|
|
2013-03-31 20:19:18 +02:00
|
|
|
if (b->prefer_readv)
|
|
|
|
k = readv(b->input_fd, &iov, 1);
|
|
|
|
else {
|
|
|
|
zero(mh);
|
|
|
|
mh.msg_iov = &iov;
|
|
|
|
mh.msg_iovlen = 1;
|
|
|
|
mh.msg_control = &control;
|
|
|
|
mh.msg_controllen = sizeof(control);
|
|
|
|
|
|
|
|
k = recvmsg(b->input_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_CMSG_CLOEXEC);
|
|
|
|
if (k < 0 && errno == ENOTSOCK) {
|
|
|
|
b->prefer_readv = true;
|
|
|
|
k = readv(b->input_fd, &iov, 1);
|
|
|
|
} else
|
|
|
|
handle_cmsg = true;
|
|
|
|
}
|
2013-03-25 03:04:03 +01:00
|
|
|
if (k < 0)
|
|
|
|
return errno == EAGAIN ? 0 : -errno;
|
|
|
|
if (k == 0)
|
|
|
|
return -ECONNRESET;
|
|
|
|
|
|
|
|
b->rbuffer_size += k;
|
|
|
|
|
2015-06-10 19:10:47 +02:00
|
|
|
if (handle_cmsg) {
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
|
|
|
|
CMSG_FOREACH(cmsg, &mh)
|
2013-03-31 20:19:18 +02:00
|
|
|
if (cmsg->cmsg_level == SOL_SOCKET &&
|
|
|
|
cmsg->cmsg_type == SCM_RIGHTS) {
|
|
|
|
int j;
|
|
|
|
|
|
|
|
/* Whut? We received fds during the auth
|
|
|
|
* protocol? Somebody is playing games with
|
|
|
|
* us. Close them all, and fail */
|
|
|
|
j = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
|
|
|
|
close_many((int*) CMSG_DATA(cmsg), j);
|
|
|
|
return -EIO;
|
2015-06-07 00:59:27 +02:00
|
|
|
} else
|
|
|
|
log_debug("Got unexpected auxiliary data with level=%d and type=%d",
|
|
|
|
cmsg->cmsg_level, cmsg->cmsg_type);
|
2015-06-10 19:10:47 +02:00
|
|
|
}
|
2013-03-30 15:21:06 +01:00
|
|
|
|
2013-03-25 03:04:03 +01:00
|
|
|
r = bus_socket_auth_verify(b);
|
|
|
|
if (r != 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-12-23 03:43:43 +01:00
|
|
|
void bus_socket_setup(sd_bus *b) {
|
2013-03-25 03:04:03 +01:00
|
|
|
assert(b);
|
|
|
|
|
2013-12-16 17:05:51 +01:00
|
|
|
/* Increase the buffers to 8 MB */
|
2017-12-12 23:24:44 +01:00
|
|
|
(void) fd_inc_rcvbuf(b->input_fd, SNDBUF_SIZE);
|
|
|
|
(void) fd_inc_sndbuf(b->output_fd, SNDBUF_SIZE);
|
2013-03-25 03:04:03 +01:00
|
|
|
|
2013-12-10 01:07:09 +01:00
|
|
|
b->message_version = 1;
|
2013-12-12 19:58:46 +01:00
|
|
|
b->message_endian = 0;
|
2013-12-23 03:43:43 +01:00
|
|
|
}
|
2013-12-10 01:07:09 +01:00
|
|
|
|
2013-12-23 03:43:43 +01:00
|
|
|
static void bus_get_peercred(sd_bus *b) {
|
2015-06-07 03:24:45 +02:00
|
|
|
int r;
|
|
|
|
|
2013-12-23 03:43:43 +01:00
|
|
|
assert(b);
|
2017-12-30 15:19:15 +01:00
|
|
|
assert(!b->ucred_valid);
|
|
|
|
assert(!b->label);
|
|
|
|
assert(b->n_groups == (size_t) -1);
|
2013-12-23 03:43:43 +01:00
|
|
|
|
|
|
|
/* Get the peer for socketpair() sockets */
|
2013-12-24 15:53:04 +01:00
|
|
|
b->ucred_valid = getpeercred(b->input_fd, &b->ucred) >= 0;
|
2015-06-07 03:24:45 +02:00
|
|
|
|
|
|
|
/* Get the SELinux context of the peer */
|
2017-12-19 15:12:50 +01:00
|
|
|
r = getpeersec(b->input_fd, &b->label);
|
2017-12-30 15:19:15 +01:00
|
|
|
if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT))
|
2017-12-19 15:12:50 +01:00
|
|
|
log_debug_errno(r, "Failed to determine peer security context: %m");
|
2017-12-30 15:19:15 +01:00
|
|
|
|
|
|
|
/* Get the list of auxiliary groups of the peer */
|
|
|
|
r = getpeergroups(b->input_fd, &b->groups);
|
|
|
|
if (r < 0) {
|
|
|
|
if (!IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT))
|
|
|
|
log_debug_errno(r, "Failed to determine peer groups list: %m");
|
|
|
|
|
|
|
|
b->n_groups = (size_t) -1;
|
|
|
|
} else
|
|
|
|
b->n_groups = (size_t) r;
|
2013-03-25 03:04:03 +01:00
|
|
|
}
|
|
|
|
|
2013-03-30 15:21:06 +01:00
|
|
|
static int bus_socket_start_auth_client(sd_bus *b) {
|
2013-03-25 03:04:03 +01:00
|
|
|
size_t l;
|
2013-03-30 15:21:06 +01:00
|
|
|
const char *auth_suffix, *auth_prefix;
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
assert(b);
|
|
|
|
|
2013-03-30 15:21:06 +01:00
|
|
|
if (b->anonymous_auth) {
|
|
|
|
auth_prefix = "\0AUTH ANONYMOUS ";
|
2013-03-25 03:04:03 +01:00
|
|
|
|
2013-03-30 15:21:06 +01:00
|
|
|
/* For ANONYMOUS auth we send some arbitrary "trace" string */
|
|
|
|
l = 9;
|
|
|
|
b->auth_buffer = hexmem("anonymous", l);
|
|
|
|
} else {
|
2015-01-27 14:00:11 +01:00
|
|
|
char text[DECIMAL_STR_MAX(uid_t) + 1];
|
2013-03-25 03:04:03 +01:00
|
|
|
|
2013-03-30 15:21:06 +01:00
|
|
|
auth_prefix = "\0AUTH EXTERNAL ";
|
2013-03-25 03:04:03 +01:00
|
|
|
|
2015-01-27 14:00:11 +01:00
|
|
|
xsprintf(text, UID_FMT, geteuid());
|
2013-03-25 03:04:03 +01:00
|
|
|
|
2013-03-30 15:21:06 +01:00
|
|
|
l = strlen(text);
|
|
|
|
b->auth_buffer = hexmem(text, l);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!b->auth_buffer)
|
2013-03-25 03:04:03 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-12-18 14:21:34 +01:00
|
|
|
if (b->accept_fd)
|
2013-03-30 15:21:06 +01:00
|
|
|
auth_suffix = "\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n";
|
|
|
|
else
|
|
|
|
auth_suffix = "\r\nBEGIN\r\n";
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
b->auth_iovec[0].iov_base = (void*) auth_prefix;
|
2013-03-30 15:21:06 +01:00
|
|
|
b->auth_iovec[0].iov_len = 1 + strlen(auth_prefix + 1);
|
|
|
|
b->auth_iovec[1].iov_base = (void*) b->auth_buffer;
|
2013-03-25 03:04:03 +01:00
|
|
|
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);
|
|
|
|
|
|
|
|
return bus_socket_write_auth(b);
|
|
|
|
}
|
|
|
|
|
2013-10-30 15:34:50 +01:00
|
|
|
int bus_socket_start_auth(sd_bus *b) {
|
2013-03-30 15:21:06 +01:00
|
|
|
assert(b);
|
|
|
|
|
2013-12-23 03:43:43 +01:00
|
|
|
bus_get_peercred(b);
|
|
|
|
|
2017-12-20 12:50:43 +01:00
|
|
|
bus_set_state(b, BUS_AUTHENTICATING);
|
2017-09-13 19:08:26 +02:00
|
|
|
b->auth_timeout = now(CLOCK_MONOTONIC) + BUS_AUTH_TIMEOUT;
|
2013-03-30 15:21:06 +01:00
|
|
|
|
2013-03-31 20:19:44 +02:00
|
|
|
if (sd_is_socket(b->input_fd, AF_UNIX, 0, 0) <= 0)
|
2017-12-18 14:21:34 +01:00
|
|
|
b->accept_fd = false;
|
2013-03-30 15:21:06 +01:00
|
|
|
|
2013-03-31 20:19:44 +02:00
|
|
|
if (b->output_fd != b->input_fd)
|
|
|
|
if (sd_is_socket(b->output_fd, AF_UNIX, 0, 0) <= 0)
|
2017-12-18 14:21:34 +01:00
|
|
|
b->accept_fd = false;
|
2013-03-31 19:10:49 +02:00
|
|
|
|
2013-03-30 15:21:06 +01:00
|
|
|
if (b->is_server)
|
|
|
|
return bus_socket_read_auth(b);
|
|
|
|
else
|
|
|
|
return bus_socket_start_auth_client(b);
|
|
|
|
}
|
|
|
|
|
2017-12-15 22:24:52 +01:00
|
|
|
static int bus_socket_inotify_setup(sd_bus *b) {
|
|
|
|
_cleanup_free_ int *new_watches = NULL;
|
|
|
|
_cleanup_free_ char *absolute = NULL;
|
|
|
|
size_t n_allocated = 0, n = 0, done = 0, i;
|
|
|
|
unsigned max_follow = 32;
|
|
|
|
const char *p;
|
|
|
|
int wd, r;
|
|
|
|
|
|
|
|
assert(b);
|
|
|
|
assert(b->watch_bind);
|
|
|
|
assert(b->sockaddr.sa.sa_family == AF_UNIX);
|
|
|
|
assert(b->sockaddr.un.sun_path[0] != 0);
|
|
|
|
|
|
|
|
/* Sets up an inotify fd in case watch_bind is enabled: wait until the configured AF_UNIX file system socket
|
|
|
|
* appears before connecting to it. The implemented is pretty simplistic: we just subscribe to relevant changes
|
|
|
|
* to all prefix components of the path, and every time we get an event for that we try to reconnect again,
|
|
|
|
* without actually caring what precisely the event we got told us. If we still can't connect we re-subscribe
|
|
|
|
* to all relevant changes of anything in the path, so that our watches include any possibly newly created path
|
|
|
|
* components. */
|
|
|
|
|
|
|
|
if (b->inotify_fd < 0) {
|
|
|
|
b->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
|
|
|
|
if (b->inotify_fd < 0)
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure the path is NUL terminated */
|
|
|
|
p = strndupa(b->sockaddr.un.sun_path, sizeof(b->sockaddr.un.sun_path));
|
|
|
|
|
|
|
|
/* Make sure the path is absolute */
|
|
|
|
r = path_make_absolute_cwd(p, &absolute);
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Watch all parent directories, and don't mind any prefix that doesn't exist yet. For the innermost directory
|
|
|
|
* that exists we want to know when files are created or moved into it. For all parents of it we just care if
|
|
|
|
* they are removed or renamed. */
|
|
|
|
|
|
|
|
if (!GREEDY_REALLOC(new_watches, n_allocated, n + 1)) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start with the top-level directory, which is a bit simpler than the rest, since it can't be a symlink, and
|
|
|
|
* always exists */
|
|
|
|
wd = inotify_add_watch(b->inotify_fd, "/", IN_CREATE|IN_MOVED_TO);
|
|
|
|
if (wd < 0) {
|
|
|
|
r = log_debug_errno(errno, "Failed to add inotify watch on /: %m");
|
|
|
|
goto fail;
|
|
|
|
} else
|
|
|
|
new_watches[n++] = wd;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
_cleanup_free_ char *component = NULL, *prefix = NULL, *destination = NULL;
|
|
|
|
size_t n_slashes, n_component;
|
|
|
|
char *c = NULL;
|
|
|
|
|
|
|
|
n_slashes = strspn(absolute + done, "/");
|
|
|
|
n_component = n_slashes + strcspn(absolute + done + n_slashes, "/");
|
|
|
|
|
|
|
|
if (n_component == 0) /* The end */
|
|
|
|
break;
|
|
|
|
|
|
|
|
component = strndup(absolute + done, n_component);
|
|
|
|
if (!component) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A trailing slash? That's a directory, and not a socket then */
|
|
|
|
if (path_equal(component, "/")) {
|
|
|
|
r = -EISDIR;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A single dot? Let's eat this up */
|
|
|
|
if (path_equal(component, "/.")) {
|
|
|
|
done += n_component;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix = strndup(absolute, done + n_component);
|
|
|
|
if (!prefix) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GREEDY_REALLOC(new_watches, n_allocated, n + 1)) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
wd = inotify_add_watch(b->inotify_fd, prefix, IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CREATE|IN_MOVED_TO|IN_DONT_FOLLOW);
|
|
|
|
log_debug("Added inotify watch for %s on bus %s: %i", prefix, strna(b->description), wd);
|
|
|
|
|
|
|
|
if (wd < 0) {
|
|
|
|
if (IN_SET(errno, ENOENT, ELOOP))
|
|
|
|
break; /* This component doesn't exist yet, or the path contains a cyclic symlink right now */
|
|
|
|
|
|
|
|
r = log_debug_errno(errno, "Failed to add inotify watch on %s: %m", isempty(prefix) ? "/" : prefix);
|
|
|
|
goto fail;
|
|
|
|
} else
|
|
|
|
new_watches[n++] = wd;
|
|
|
|
|
|
|
|
/* Check if this is possibly a symlink. If so, let's follow it and watch it too. */
|
|
|
|
r = readlink_malloc(prefix, &destination);
|
|
|
|
if (r == -EINVAL) { /* not a symlink */
|
|
|
|
done += n_component;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (isempty(destination)) { /* Empty symlink target? Yuck! */
|
|
|
|
r = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_follow <= 0) { /* Let's make sure we don't follow symlinks forever */
|
|
|
|
r = -ELOOP;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path_is_absolute(destination)) {
|
|
|
|
/* For absolute symlinks we build the new path and start anew */
|
|
|
|
c = strjoin(destination, absolute + done + n_component);
|
|
|
|
done = 0;
|
|
|
|
} else {
|
|
|
|
_cleanup_free_ char *t = NULL;
|
|
|
|
|
|
|
|
/* For relative symlinks we replace the last component, and try again */
|
|
|
|
t = strndup(absolute, done);
|
|
|
|
if (!t)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
c = strjoin(t, "/", destination, absolute + done + n_component);
|
|
|
|
}
|
|
|
|
if (!c) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(absolute);
|
|
|
|
absolute = c;
|
|
|
|
|
|
|
|
max_follow--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And now, let's remove all watches from the previous iteration we don't need anymore */
|
|
|
|
for (i = 0; i < b->n_inotify_watches; i++) {
|
|
|
|
bool found = false;
|
|
|
|
size_t j;
|
|
|
|
|
|
|
|
for (j = 0; j < n; j++)
|
|
|
|
if (new_watches[j] == b->inotify_watches[i]) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
(void) inotify_rm_watch(b->inotify_fd, b->inotify_watches[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
free_and_replace(b->inotify_watches, new_watches);
|
|
|
|
b->n_inotify_watches = n;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
bus_close_inotify_fd(b);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-03-25 03:04:03 +01:00
|
|
|
int bus_socket_connect(sd_bus *b) {
|
2017-12-15 22:24:52 +01:00
|
|
|
bool inotify_done = false;
|
2013-03-25 03:04:03 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(b);
|
|
|
|
|
2017-12-15 22:24:52 +01:00
|
|
|
for (;;) {
|
|
|
|
assert(b->input_fd < 0);
|
|
|
|
assert(b->output_fd < 0);
|
|
|
|
assert(b->sockaddr.sa.sa_family != AF_UNSPEC);
|
2013-03-25 03:04:03 +01:00
|
|
|
|
2017-12-15 22:24:52 +01:00
|
|
|
b->input_fd = socket(b->sockaddr.sa.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
|
|
|
|
if (b->input_fd < 0)
|
|
|
|
return -errno;
|
2013-03-31 19:10:49 +02:00
|
|
|
|
2017-12-15 22:24:52 +01:00
|
|
|
b->output_fd = b->input_fd;
|
|
|
|
bus_socket_setup(b);
|
2013-03-25 03:04:03 +01:00
|
|
|
|
2017-12-15 22:24:52 +01:00
|
|
|
if (connect(b->input_fd, &b->sockaddr.sa, b->sockaddr_size) < 0) {
|
|
|
|
if (errno == EINPROGRESS) {
|
2013-03-25 03:04:03 +01:00
|
|
|
|
2017-12-15 22:24:52 +01:00
|
|
|
/* If we have any inotify watches open, close them now, we don't need them anymore, as
|
|
|
|
* we have successfully initiated a connection */
|
|
|
|
bus_close_inotify_fd(b);
|
|
|
|
|
|
|
|
/* Note that very likely we are already in BUS_OPENING state here, as we enter it when
|
|
|
|
* we start parsing the address string. The only reason we set the state explicitly
|
|
|
|
* here, is to undo BUS_WATCH_BIND, in case we did the inotify magic. */
|
2017-12-20 12:50:43 +01:00
|
|
|
bus_set_state(b, BUS_OPENING);
|
2017-12-15 22:24:52 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IN_SET(errno, ENOENT, ECONNREFUSED) && /* ENOENT → unix socket doesn't exist at all; ECONNREFUSED → unix socket stale */
|
|
|
|
b->watch_bind &&
|
|
|
|
b->sockaddr.sa.sa_family == AF_UNIX &&
|
|
|
|
b->sockaddr.un.sun_path[0] != 0) {
|
|
|
|
|
|
|
|
/* This connection attempt failed, let's release the socket for now, and start with a
|
|
|
|
* fresh one when reconnecting. */
|
|
|
|
bus_close_io_fds(b);
|
|
|
|
|
|
|
|
if (inotify_done) {
|
|
|
|
/* inotify set up already, don't do it again, just return now, and remember
|
|
|
|
* that we are waiting for inotify events now. */
|
2017-12-20 12:50:43 +01:00
|
|
|
bus_set_state(b, BUS_WATCH_BIND);
|
2017-12-15 22:24:52 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a file system socket, and the inotify logic is enabled. Let's create the necessary inotify fd. */
|
|
|
|
r = bus_socket_inotify_setup(b);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
/* Let's now try to connect a second time, because in theory there's otherwise a race
|
|
|
|
* here: the socket might have been created in the time between our first connect() and
|
|
|
|
* the time we set up the inotify logic. But let's remember that we set up inotify now,
|
|
|
|
* so that we don't do the connect() more than twice. */
|
|
|
|
inotify_done = true;
|
|
|
|
|
|
|
|
} else
|
|
|
|
return -errno;
|
|
|
|
} else
|
|
|
|
break;
|
2013-03-25 03:04:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-15 22:24:52 +01:00
|
|
|
/* Yay, established, we don't need no inotify anymore! */
|
|
|
|
bus_close_inotify_fd(b);
|
|
|
|
|
2013-03-25 03:04:03 +01:00
|
|
|
return bus_socket_start_auth(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
int bus_socket_exec(sd_bus *b) {
|
2013-03-31 19:10:49 +02:00
|
|
|
int s[2], r;
|
2013-03-25 03:04:03 +01:00
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
assert(b);
|
2013-03-31 19:10:49 +02:00
|
|
|
assert(b->input_fd < 0);
|
|
|
|
assert(b->output_fd < 0);
|
2013-03-25 03:04:03 +01:00
|
|
|
assert(b->exec_path);
|
|
|
|
|
2013-03-31 19:10:49 +02:00
|
|
|
r = socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0, s);
|
|
|
|
if (r < 0)
|
2013-03-25 03:04:03 +01:00
|
|
|
return -errno;
|
|
|
|
|
2017-12-22 13:08:14 +01:00
|
|
|
r = safe_fork_full("(sd-busexec)", s+1, 1, FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, &pid);
|
|
|
|
if (r < 0) {
|
2014-03-24 03:22:44 +01:00
|
|
|
safe_close_pair(s);
|
2017-12-22 13:08:14 +01:00
|
|
|
return r;
|
2013-03-25 03:04:03 +01:00
|
|
|
}
|
2017-12-22 13:08:14 +01:00
|
|
|
if (r == 0) {
|
2013-03-25 03:04:03 +01:00
|
|
|
/* Child */
|
|
|
|
|
|
|
|
assert_se(dup3(s[1], STDIN_FILENO, 0) == STDIN_FILENO);
|
|
|
|
assert_se(dup3(s[1], STDOUT_FILENO, 0) == STDOUT_FILENO);
|
|
|
|
|
2017-09-28 10:17:04 +02:00
|
|
|
if (!IN_SET(s[1], STDIN_FILENO, STDOUT_FILENO))
|
2014-03-18 19:22:43 +01:00
|
|
|
safe_close(s[1]);
|
2013-03-25 03:04:03 +01:00
|
|
|
|
2017-12-12 23:24:44 +01:00
|
|
|
(void) fd_cloexec(STDIN_FILENO, false);
|
|
|
|
(void) fd_cloexec(STDOUT_FILENO, false);
|
|
|
|
(void) fd_nonblock(STDIN_FILENO, false);
|
|
|
|
(void) fd_nonblock(STDOUT_FILENO, false);
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-03-18 19:22:43 +01:00
|
|
|
safe_close(s[1]);
|
2013-03-31 19:10:49 +02:00
|
|
|
b->output_fd = b->input_fd = s[0];
|
2013-03-25 03:04:03 +01:00
|
|
|
|
2013-12-23 03:43:43 +01:00
|
|
|
bus_socket_setup(b);
|
2013-12-10 01:07:09 +01:00
|
|
|
|
2013-03-25 03:04:03 +01:00
|
|
|
return bus_socket_start_auth(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
int bus_socket_take_fd(sd_bus *b) {
|
|
|
|
assert(b);
|
|
|
|
|
2013-12-23 03:43:43 +01:00
|
|
|
bus_socket_setup(b);
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
return bus_socket_start_auth(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
int bus_socket_write_message(sd_bus *bus, sd_bus_message *m, size_t *idx) {
|
|
|
|
struct iovec *iov;
|
|
|
|
ssize_t k;
|
|
|
|
size_t n;
|
|
|
|
unsigned j;
|
2013-05-14 22:24:26 +02:00
|
|
|
int r;
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(m);
|
|
|
|
assert(idx);
|
2017-09-28 10:17:04 +02:00
|
|
|
assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
|
2013-03-25 03:04:03 +01:00
|
|
|
|
2013-04-11 23:09:29 +02:00
|
|
|
if (*idx >= BUS_MESSAGE_SIZE(m))
|
2013-03-25 03:04:03 +01:00
|
|
|
return 0;
|
|
|
|
|
2013-05-14 22:24:26 +02:00
|
|
|
r = bus_message_setup_iovec(m);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-04-11 02:09:36 +02:00
|
|
|
|
2013-03-25 03:04:03 +01:00
|
|
|
n = m->n_iovec * sizeof(struct iovec);
|
|
|
|
iov = alloca(n);
|
2016-02-02 03:57:41 +01:00
|
|
|
memcpy_safe(iov, m->iovec, n);
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
j = 0;
|
|
|
|
iovec_advance(iov, &j, *idx);
|
|
|
|
|
2013-03-31 20:19:18 +02:00
|
|
|
if (bus->prefer_writev)
|
|
|
|
k = writev(bus->output_fd, iov, m->n_iovec);
|
|
|
|
else {
|
2015-05-16 17:06:43 +02:00
|
|
|
struct msghdr mh = {
|
|
|
|
.msg_iov = iov,
|
|
|
|
.msg_iovlen = m->n_iovec,
|
|
|
|
};
|
2013-03-31 20:19:18 +02:00
|
|
|
|
2017-08-30 13:09:03 +02:00
|
|
|
if (m->n_fds > 0 && *idx == 0) {
|
2013-03-31 20:19:18 +02:00
|
|
|
struct cmsghdr *control;
|
|
|
|
|
2015-05-16 17:06:43 +02:00
|
|
|
mh.msg_control = control = alloca(CMSG_SPACE(sizeof(int) * m->n_fds));
|
|
|
|
mh.msg_controllen = control->cmsg_len = CMSG_LEN(sizeof(int) * m->n_fds);
|
2013-03-31 20:19:18 +02:00
|
|
|
control->cmsg_level = SOL_SOCKET;
|
|
|
|
control->cmsg_type = SCM_RIGHTS;
|
|
|
|
memcpy(CMSG_DATA(control), m->fds, sizeof(int) * m->n_fds);
|
|
|
|
}
|
|
|
|
|
|
|
|
k = sendmsg(bus->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
|
|
|
|
if (k < 0 && errno == ENOTSOCK) {
|
|
|
|
bus->prefer_writev = true;
|
|
|
|
k = writev(bus->output_fd, iov, m->n_iovec);
|
|
|
|
}
|
|
|
|
}
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
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);
|
2017-09-28 10:17:04 +02:00
|
|
|
assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
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];
|
2013-12-03 18:13:48 +01:00
|
|
|
if (e == BUS_LITTLE_ENDIAN) {
|
2013-03-25 03:04:03 +01:00
|
|
|
a = le32toh(a);
|
|
|
|
b = le32toh(b);
|
2013-12-03 18:13:48 +01:00
|
|
|
} else if (e == BUS_BIG_ENDIAN) {
|
2013-03-25 03:04:03 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-11-29 21:29:16 +01:00
|
|
|
static int bus_socket_make_message(sd_bus *bus, size_t size) {
|
2013-03-25 03:04:03 +01:00
|
|
|
sd_bus_message *t;
|
|
|
|
void *b;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(bus->rbuffer_size >= size);
|
2017-09-28 10:17:04 +02:00
|
|
|
assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
|
2013-03-25 03:04:03 +01:00
|
|
|
|
2013-11-30 00:19:01 +01:00
|
|
|
r = bus_rqueue_make_room(bus);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2013-03-25 03:04:03 +01:00
|
|
|
if (bus->rbuffer_size > size) {
|
|
|
|
b = memdup((const uint8_t*) bus->rbuffer + size,
|
|
|
|
bus->rbuffer_size - size);
|
|
|
|
if (!b)
|
|
|
|
return -ENOMEM;
|
|
|
|
} else
|
|
|
|
b = NULL;
|
|
|
|
|
2013-11-21 01:51:16 +01:00
|
|
|
r = bus_message_from_malloc(bus,
|
|
|
|
bus->rbuffer, size,
|
2013-03-25 03:04:03 +01:00
|
|
|
bus->fds, bus->n_fds,
|
2015-04-23 13:37:03 +02:00
|
|
|
NULL,
|
2013-03-25 03:04:03 +01:00
|
|
|
&t);
|
|
|
|
if (r < 0) {
|
|
|
|
free(b);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
bus->rbuffer = b;
|
|
|
|
bus->rbuffer_size -= size;
|
|
|
|
|
|
|
|
bus->fds = NULL;
|
|
|
|
bus->n_fds = 0;
|
|
|
|
|
2013-11-30 00:19:01 +01:00
|
|
|
bus->rqueue[bus->rqueue_size++] = t;
|
2013-11-29 21:29:16 +01:00
|
|
|
|
2013-03-25 03:04:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-11-29 21:29:16 +01:00
|
|
|
int bus_socket_read_message(sd_bus *bus) {
|
2013-03-25 03:04:03 +01:00
|
|
|
struct msghdr mh;
|
2015-05-16 17:06:43 +02:00
|
|
|
struct iovec iov = {};
|
2013-03-25 03:04:03 +01:00
|
|
|
ssize_t k;
|
|
|
|
size_t need;
|
|
|
|
int r;
|
|
|
|
void *b;
|
|
|
|
union {
|
|
|
|
struct cmsghdr cmsghdr;
|
2015-06-07 00:59:27 +02:00
|
|
|
uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX)];
|
2013-03-25 03:04:03 +01:00
|
|
|
} control;
|
2013-04-10 12:54:02 +02:00
|
|
|
bool handle_cmsg = false;
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
assert(bus);
|
2017-09-28 10:17:04 +02:00
|
|
|
assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
r = bus_socket_read_message_need(bus, &need);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (bus->rbuffer_size >= need)
|
2013-11-29 21:29:16 +01:00
|
|
|
return bus_socket_make_message(bus, need);
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
b = realloc(bus->rbuffer, need);
|
|
|
|
if (!b)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
bus->rbuffer = b;
|
|
|
|
|
|
|
|
iov.iov_base = (uint8_t*) bus->rbuffer + bus->rbuffer_size;
|
|
|
|
iov.iov_len = need - bus->rbuffer_size;
|
|
|
|
|
2013-03-31 20:19:18 +02:00
|
|
|
if (bus->prefer_readv)
|
|
|
|
k = readv(bus->input_fd, &iov, 1);
|
|
|
|
else {
|
|
|
|
zero(mh);
|
|
|
|
mh.msg_iov = &iov;
|
|
|
|
mh.msg_iovlen = 1;
|
|
|
|
mh.msg_control = &control;
|
|
|
|
mh.msg_controllen = sizeof(control);
|
|
|
|
|
|
|
|
k = recvmsg(bus->input_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_CMSG_CLOEXEC);
|
|
|
|
if (k < 0 && errno == ENOTSOCK) {
|
|
|
|
bus->prefer_readv = true;
|
|
|
|
k = readv(bus->input_fd, &iov, 1);
|
|
|
|
} else
|
|
|
|
handle_cmsg = true;
|
|
|
|
}
|
2013-03-25 03:04:03 +01:00
|
|
|
if (k < 0)
|
|
|
|
return errno == EAGAIN ? 0 : -errno;
|
|
|
|
if (k == 0)
|
|
|
|
return -ECONNRESET;
|
|
|
|
|
|
|
|
bus->rbuffer_size += k;
|
|
|
|
|
2015-06-10 19:10:47 +02:00
|
|
|
if (handle_cmsg) {
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
|
|
|
|
CMSG_FOREACH(cmsg, &mh)
|
2013-03-31 20:19:18 +02:00
|
|
|
if (cmsg->cmsg_level == SOL_SOCKET &&
|
|
|
|
cmsg->cmsg_type == SCM_RIGHTS) {
|
|
|
|
int n, *f;
|
|
|
|
|
|
|
|
n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
|
|
|
|
|
|
|
|
if (!bus->can_fds) {
|
|
|
|
/* Whut? We received fds but this
|
|
|
|
* isn't actually enabled? Close them,
|
|
|
|
* and fail */
|
|
|
|
|
|
|
|
close_many((int*) CMSG_DATA(cmsg), n);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2015-09-16 22:55:02 +02:00
|
|
|
f = realloc(bus->fds, sizeof(int) * (bus->n_fds + n));
|
2013-03-31 20:19:18 +02:00
|
|
|
if (!f) {
|
|
|
|
close_many((int*) CMSG_DATA(cmsg), n);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2016-02-02 03:57:41 +01:00
|
|
|
memcpy_safe(f + bus->n_fds, CMSG_DATA(cmsg), n * sizeof(int));
|
2013-03-31 20:19:18 +02:00
|
|
|
bus->fds = f;
|
|
|
|
bus->n_fds += n;
|
2015-06-07 00:59:27 +02:00
|
|
|
} else
|
|
|
|
log_debug("Got unexpected auxiliary data with level=%d and type=%d",
|
|
|
|
cmsg->cmsg_level, cmsg->cmsg_type);
|
2015-06-10 19:10:47 +02:00
|
|
|
}
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
r = bus_socket_read_message_need(bus, &need);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (bus->rbuffer_size >= need)
|
2013-11-29 21:29:16 +01:00
|
|
|
return bus_socket_make_message(bus, need);
|
2013-03-25 03:04:03 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bus_socket_process_opening(sd_bus *b) {
|
|
|
|
int error = 0;
|
|
|
|
socklen_t slen = sizeof(error);
|
2013-03-25 00:59:00 +01:00
|
|
|
struct pollfd p = {
|
|
|
|
.fd = b->output_fd,
|
|
|
|
.events = POLLOUT,
|
|
|
|
};
|
2013-03-25 03:04:03 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(b->state == BUS_OPENING);
|
|
|
|
|
|
|
|
r = poll(&p, 1, 0);
|
|
|
|
if (r < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (!(p.revents & (POLLOUT|POLLERR|POLLHUP)))
|
|
|
|
return 0;
|
|
|
|
|
2013-03-31 19:10:49 +02:00
|
|
|
r = getsockopt(b->output_fd, SOL_SOCKET, SO_ERROR, &error, &slen);
|
2013-03-25 03:04:03 +01:00
|
|
|
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);
|
|
|
|
}
|
2017-12-15 22:24:52 +01:00
|
|
|
|
|
|
|
int bus_socket_process_watch_bind(sd_bus *b) {
|
|
|
|
int r, q;
|
|
|
|
|
|
|
|
assert(b);
|
|
|
|
assert(b->state == BUS_WATCH_BIND);
|
|
|
|
assert(b->inotify_fd >= 0);
|
|
|
|
|
|
|
|
r = flush_fd(b->inotify_fd);
|
|
|
|
if (r <= 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
log_debug("Got inotify event on bus %s.", strna(b->description));
|
|
|
|
|
|
|
|
/* We flushed events out of the inotify fd. In that case, maybe the socket is valid now? Let's try to connect
|
|
|
|
* to it again */
|
|
|
|
|
|
|
|
r = bus_socket_connect(b);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
q = bus_attach_io_events(b);
|
|
|
|
if (q < 0)
|
|
|
|
return q;
|
|
|
|
|
|
|
|
q = bus_attach_inotify_event(b);
|
|
|
|
if (q < 0)
|
|
|
|
return q;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|