/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
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>
#include <poll.h>
#include <stdlib.h>
#include <unistd.h>
#include "sd-bus.h"
#include "sd-daemon.h"
#include "alloc-util.h"
#include "bus-internal.h"
#include "bus-message.h"
#include "bus-socket.h"
#include "fd-util.h"
#include "formats-util.h"
#include "hexdecoct.h"
#include "macro.h"
#include "missing.h"
#include "selinux-util.h"
#include "signal-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "user-util.h"
#include "utf8.h"
#include "util.h"
while (size > 0) {
return;
}
i->iov_len = 0;
(*idx) ++;
}
}
assert(m);
assert(p);
m->n_iovec++;
return 0;
}
unsigned n, i;
int r;
assert(m);
if (m->n_iovec > 0)
return 0;
n = 1 + m->n_body_parts;
if (n < ELEMENTSOF(m->iovec_fixed))
m->iovec = m->iovec_fixed;
else {
if (!m->iovec) {
r = -ENOMEM;
goto fail;
}
}
if (r < 0)
goto fail;
r = bus_body_part_map(part);
if (r < 0)
goto fail;
if (r < 0)
goto fail;
}
return 0;
fail:
m->poisoned = true;
return r;
}
unsigned i;
return false;
struct iovec *j = b->auth_iovec + i;
if (j->iov_len > 0)
return true;
}
return false;
}
ssize_t k;
assert(b);
if (!bus_socket_auth_needs_write(b))
return 0;
if (b->prefer_writev)
else {
b->prefer_writev = true;
}
}
if (k < 0)
return 1;
}
char *e, *f, *start;
unsigned i;
int r;
assert(b);
/* We expect two response lines: "OK" and possibly
* "AGREE_UNIX_FD" */
if (!e)
return 0;
if (b->hello_flags & KDBUS_HELLO_ACCEPT_FD) {
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 */
return -EPERM;
return -EPERM;
for (i = 0; i < 32; i += 2) {
int x, y;
if (x < 0 || y < 0)
return -EINVAL;
}
return -EPERM;
/* And possibly check the second line, too */
if (f)
b->can_fds =
(f - e == strlen("\r\nAGREE_UNIX_FD")) &&
r = bus_start_running(b);
if (r < 0)
return r;
return 1;
}
size_t l;
if (l != m)
return false;
}
size_t l;
if (m < l)
return false;
return false;
return m == l || (m > l && s[l] == ' ');
}
int r;
if (!b->anonymous_auth)
return 0;
if (l <= 0)
return 1;
assert(p[0] == ' ');
p++; l--;
if (l % 2 != 0)
return 0;
if (r < 0)
return 0;
return 0;
return !!utf8_is_valid(token);
}
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. */
if (!b->anonymous_auth && !b->ucred_valid)
return 0;
if (l <= 0)
return 1;
assert(p[0] == ' ');
p++; l--;
if (l % 2 != 0)
return 0;
if (r < 0)
return 0;
return 0;
if (r < 0)
return 0;
/* We ignore the passed value if anonymous authentication is
* on anyway. */
return 0;
return 1;
}
char *p;
size_t l;
assert(b);
assert(t);
/* We only make use of the first iovec */
l = strlen(t);
if (!p)
return -ENOMEM;
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;
}
char t[3 + 32 + 2 + 1];
assert(b);
return bus_socket_auth_write(b, t);
}
char *e;
const char *line;
size_t l;
bool processed = false;
int r;
assert(b);
if (b->rbuffer_size < 1)
return 0;
/* First char must be a NUL byte */
if (*(char*) b->rbuffer != 0)
return -EIO;
if (b->rbuffer_size < 3)
return 0;
/* Begin with the first line */
if (b->auth_rbegin <= 0)
b->auth_rbegin = 1;
for (;;) {
/* Check if line is complete */
if (!e)
return processed;
l = e - line;
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);
}
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);
}
r = bus_socket_auth_write(b, "REJECTED EXTERNAL ANONYMOUS\r\n");
b->auth = _BUS_AUTH_INVALID;
r = bus_socket_auth_write(b, "REJECTED\r\n");
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;
return bus_start_running(b);
}
if (b->auth == _BUS_AUTH_INVALID)
r = bus_socket_auth_write(b, "ERROR\r\n");
else {
if (b->auth == BUS_AUTH_ANONYMOUS)
else
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);
}
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;
processed = true;
}
}
assert(b);
if (b->is_server)
return bus_socket_auth_verify_server(b);
else
return bus_socket_auth_verify_client(b);
}
size_t n;
ssize_t k;
int r;
void *p;
union {
} control;
bool handle_cmsg = false;
assert(b);
r = bus_socket_auth_verify(b);
if (r != 0)
return r;
if (n > BUS_AUTH_SIZE_MAX)
n = BUS_AUTH_SIZE_MAX;
if (b->rbuffer_size >= n)
return -ENOBUFS;
if (!p)
return -ENOMEM;
b->rbuffer = p;
if (b->prefer_readv)
else {
b->prefer_readv = true;
} else
handle_cmsg = true;
}
if (k < 0)
if (k == 0)
return -ECONNRESET;
b->rbuffer_size += k;
if (handle_cmsg) {
int j;
/* Whut? We received fds during the auth
* protocol? Somebody is playing games with
* us. Close them all, and fail */
return -EIO;
} else
log_debug("Got unexpected auxiliary data with level=%d and type=%d",
}
r = bus_socket_auth_verify(b);
if (r != 0)
return r;
return 1;
}
assert(b);
/* Increase the buffers to 8 MB */
b->is_kernel = false;
b->message_version = 1;
b->message_endian = 0;
}
int r;
assert(b);
/* Get the peer for socketpair() sockets */
/* Get the SELinux context of the peer */
if (mac_selinux_have()) {
if (r < 0 && r != -EOPNOTSUPP)
log_debug_errno(r, "Failed to determine peer security context: %m");
}
}
size_t l;
assert(b);
if (b->anonymous_auth) {
auth_prefix = "\0AUTH ANONYMOUS ";
/* For ANONYMOUS auth we send some arbitrary "trace" string */
l = 9;
} else {
auth_prefix = "\0AUTH EXTERNAL ";
}
if (!b->auth_buffer)
return -ENOMEM;
if (b->hello_flags & KDBUS_HELLO_ACCEPT_FD)
auth_suffix = "\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n";
else
auth_suffix = "\r\nBEGIN\r\n";
return bus_socket_write_auth(b);
}
assert(b);
bus_get_peercred(b);
b->state = BUS_AUTHENTICATING;
b->hello_flags &= ~KDBUS_HELLO_ACCEPT_FD;
b->hello_flags &= ~KDBUS_HELLO_ACCEPT_FD;
if (b->is_server)
return bus_socket_read_auth(b);
else
return bus_socket_start_auth_client(b);
}
int r;
assert(b);
if (b->input_fd < 0)
return -errno;
bus_socket_setup(b);
if (r < 0) {
if (errno == EINPROGRESS)
return 1;
return -errno;
}
return bus_socket_start_auth(b);
}
int s[2], r;
assert(b);
if (r < 0)
return -errno;
if (pid < 0) {
safe_close_pair(s);
return -errno;
}
if (pid == 0) {
/* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
safe_close(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)
else {
}
}
safe_close(s[1]);
bus_socket_setup(b);
return bus_socket_start_auth(b);
}
assert(b);
bus_socket_setup(b);
return bus_socket_start_auth(b);
}
ssize_t k;
size_t n;
unsigned j;
int r;
assert(m);
if (*idx >= BUS_MESSAGE_SIZE(m))
return 0;
r = bus_message_setup_iovec(m);
if (r < 0)
return r;
j = 0;
if (bus->prefer_writev)
else {
.msg_iovlen = m->n_iovec,
};
if (m->n_fds > 0) {
}
bus->prefer_writev = true;
}
}
if (k < 0)
return 1;
}
uint32_t a, b;
uint8_t e;
/* 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;
}
if (e == BUS_LITTLE_ENDIAN) {
a = le32toh(a);
b = le32toh(b);
} else if (e == BUS_BIG_ENDIAN) {
a = be32toh(a);
b = be32toh(b);
} else
return -EBADMSG;
if (sum >= BUS_MESSAGE_SIZE_MAX)
return -ENOBUFS;
return 0;
}
sd_bus_message *t;
void *b;
int r;
r = bus_rqueue_make_room(bus);
if (r < 0)
return r;
if (!b)
return -ENOMEM;
} else
b = NULL;
NULL,
&t);
if (r < 0) {
free(b);
return r;
}
return 1;
}
ssize_t k;
int r;
void *b;
union {
} control;
bool handle_cmsg = false;
if (r < 0)
return r;
if (!b)
return -ENOMEM;
if (bus->prefer_readv)
else {
bus->prefer_readv = true;
} else
handle_cmsg = true;
}
if (k < 0)
if (k == 0)
return -ECONNRESET;
bus->rbuffer_size += k;
if (handle_cmsg) {
int n, *f;
/* Whut? We received fds but this
* isn't actually enabled? Close them,
* and fail */
return -EIO;
}
if (!f) {
return -ENOMEM;
}
} else
log_debug("Got unexpected auxiliary data with level=%d and type=%d",
}
if (r < 0)
return r;
return 1;
}
int error = 0;
struct pollfd p = {
};
int r;
r = poll(&p, 1, 0);
if (r < 0)
return -errno;
return 0;
if (r < 0)
b->last_connect_error = errno;
else if (error != 0)
b->last_connect_error = error;
else
return bus_socket_start_auth(b);
return bus_next_address(b);
}
int r;
assert(b);
return -ETIMEDOUT;
r = bus_socket_write_auth(b);
if (r != 0)
return r;
return bus_socket_read_auth(b);
}