HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/libsystemd/sd-bus/bus-message.c
Git blob: 81f78f8036f3d70b14af45edd5d008e1d7ca1d64
Lines 4001–4752 of 4,752
if (r < 0)
return r;
}
(*signature) += 1 + l;
} else if (t == SD_BUS_TYPE_VARIANT) {
const char *s;
r = message_peek_field_signature(m, ri, 0, &s);
if (r < 0)
return r;
r = message_skip_fields(m, ri, UINT32_MAX, &s, depth + 1);
if (r < 0)
return r;
(*signature)++;
} else if (IN_SET(t, SD_BUS_TYPE_STRUCT, SD_BUS_TYPE_DICT_ENTRY)) {
r = signature_element_length(*signature, &l);
if (r < 0)
return r;
assert(l >= 2);
{
char sig[l + 1], *s = sig;
strncpy(sig, *signature + 1, l);
sig[l] = '\0';
r = message_skip_fields(m, ri, UINT32_MAX, (const char**) &s, depth + 1);
if (r < 0)
return r;
}
*signature += l;
} else
return -EBADMSG;
}
}
static int message_parse_fields(sd_bus_message *m, bool got_ctrunc) {
uint32_t n_unix_fds_declared = 0;
bool unix_fds_set = false;
int r;
assert(m);
m->user_body_size = m->body_size;
for (size_t ri = 0; ri < m->fields_size; ) {
const char *signature;
uint64_t field_type;
size_t item_size = SIZE_MAX;
uint8_t *u8;
r = message_peek_fields(m, &ri, 8, 1, (void**) &u8);
if (r < 0)
return r;
field_type = *u8;
r = message_peek_field_signature(m, &ri, 0, &signature);
if (r < 0)
return r;
switch (field_type) {
case _BUS_MESSAGE_HEADER_INVALID:
return -EBADMSG;
case BUS_MESSAGE_HEADER_PATH:
if (m->path)
return -EBADMSG;
if (!streq(signature, "o"))
return -EBADMSG;
r = message_peek_field_string(m, object_path_is_valid, &ri, item_size, &m->path);
break;
case BUS_MESSAGE_HEADER_INTERFACE:
if (m->interface)
return -EBADMSG;
if (!streq(signature, "s"))
return -EBADMSG;
r = message_peek_field_string(m, interface_name_is_valid, &ri, item_size, &m->interface);
break;
case BUS_MESSAGE_HEADER_MEMBER:
if (m->member)
return -EBADMSG;
if (!streq(signature, "s"))
return -EBADMSG;
r = message_peek_field_string(m, member_name_is_valid, &ri, item_size, &m->member);
break;
case BUS_MESSAGE_HEADER_ERROR_NAME:
if (m->error.name)
return -EBADMSG;
if (!streq(signature, "s"))
return -EBADMSG;
r = message_peek_field_string(m, error_name_is_valid, &ri, item_size, &m->error.name);
if (r >= 0)
m->error._need_free = -1;
break;
case BUS_MESSAGE_HEADER_DESTINATION:
if (m->destination)
return -EBADMSG;
if (!streq(signature, "s"))
return -EBADMSG;
r = message_peek_field_string(m, service_name_is_valid, &ri, item_size, &m->destination);
break;
case BUS_MESSAGE_HEADER_SENDER:
if (m->sender)
return -EBADMSG;
if (!streq(signature, "s"))
return -EBADMSG;
r = message_peek_field_string(m, service_name_is_valid, &ri, item_size, &m->sender);
if (r >= 0 && m->sender[0] == ':' && m->bus->bus_client) {
m->creds.unique_name = (char*) m->sender;
m->creds.mask |= SD_BUS_CREDS_UNIQUE_NAME & m->bus->creds_mask;
}
break;
case BUS_MESSAGE_HEADER_SIGNATURE: {
const char *s;
char *c;
if (m->root_container.signature)
return -EBADMSG;
if (!streq(signature, "g"))
return -EBADMSG;
r = message_peek_field_signature(m, &ri, item_size, &s);
if (r < 0)
return r;
c = strdup(s);
if (!c)
return -ENOMEM;
free_and_replace(m->root_container.signature, c);
break;
}
case BUS_MESSAGE_HEADER_REPLY_SERIAL: {
uint32_t serial;
if (m->reply_cookie != 0)
return -EBADMSG;
if (!streq(signature, "u"))
return -EBADMSG;
r = message_peek_field_uint32(m, &ri, item_size, &serial);
if (r < 0)
return r;
m->reply_cookie = serial;
if (m->reply_cookie == 0)
return -EBADMSG;
break;
}
case BUS_MESSAGE_HEADER_UNIX_FDS:
if (unix_fds_set)
return -EBADMSG;
if (!streq(signature, "u"))
return -EBADMSG;
r = message_peek_field_uint32(m, &ri, item_size, &n_unix_fds_declared);
if (r < 0)
return -EBADMSG;
unix_fds_set = true;
break;
default:
r = message_skip_fields(m, &ri, UINT32_MAX, &signature, 0);
}
if (r < 0)
return r;
}
/* Validate that the number of fds we actually received via SCM_RIGHTS matches (or is compatible
* with) the number declared in the message header.
*
* Normally these must match exactly. However, when MSG_CTRUNC was set during recvmsg(), the kernel
* might have truncated the fd array (e.g., due to LSM denials blocking fd passing). In that case,
* we also accept fewer fds than declared. Any attempt to actually use a truncated fd will fail later
* when sd_bus_message_read_basic() finds the fd index out of range. Too many fds is always wrong. */
if (m->n_fds > n_unix_fds_declared)
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
"Received a bus message with too many fds: %" PRIu32 " received vs. %" PRIu32 " declared.",
m->n_fds, n_unix_fds_declared);
if (m->n_fds < n_unix_fds_declared) {
if (!got_ctrunc)
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
"Received a bus message with too few fds: %" PRIu32 " received vs. %" PRIu32 " declared.",
m->n_fds, n_unix_fds_declared);
log_debug("Received a bus message with MSG_CTRUNC set with %" PRIu32 " fds received vs. %" PRIu32 " declared.",
m->n_fds, n_unix_fds_declared);
} else if (got_ctrunc)
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
"Received a bus message with truncated control data, refusing.");
switch (m->header->type) {
case SD_BUS_MESSAGE_SIGNAL:
if (!m->path || !m->interface || !m->member)
return -EBADMSG;
if (m->reply_cookie != 0)
return -EBADMSG;
break;
case SD_BUS_MESSAGE_METHOD_CALL:
if (!m->path || !m->member)
return -EBADMSG;
if (m->reply_cookie != 0)
return -EBADMSG;
break;
case SD_BUS_MESSAGE_METHOD_RETURN:
if (m->reply_cookie == 0)
return -EBADMSG;
break;
case SD_BUS_MESSAGE_METHOD_ERROR:
if (m->reply_cookie == 0 || !m->error.name)
return -EBADMSG;
break;
}
/* Refuse non-local messages that claim they are local */
if (streq_ptr(m->path, "/org/freedesktop/DBus/Local"))
return -EBADMSG;
if (streq_ptr(m->interface, "org.freedesktop.DBus.Local"))
return -EBADMSG;
if (streq_ptr(m->sender, "org.freedesktop.DBus.Local"))
return -EBADMSG;
m->root_container.end = m->user_body_size;
/* Try to read the error message, but if we can't it's a non-issue */
if (m->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
(void) sd_bus_message_read(m, "s", &m->error.message);
return 0;
}
_public_ int sd_bus_message_set_destination(sd_bus_message *m, const char *destination) {
assert_return(m, -EINVAL);
assert_return(destination, -EINVAL);
assert_return(service_name_is_valid(destination), -EINVAL);
assert_return(!m->sealed, -EPERM);
assert_return(!m->destination, -EEXIST);
return message_append_field_string(m, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &m->destination);
}
_public_ int sd_bus_message_set_sender(sd_bus_message *m, const char *sender) {
assert_return(m, -EINVAL);
assert_return(sender, -EINVAL);
assert_return(service_name_is_valid(sender), -EINVAL);
assert_return(!m->sealed, -EPERM);
assert_return(!m->sender, -EEXIST);
return message_append_field_string(m, BUS_MESSAGE_HEADER_SENDER, SD_BUS_TYPE_STRING, sender, &m->sender);
}
int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) {
size_t total;
void *p, *e;
size_t i;
BusMessageBodyPart *part;
assert(m);
assert(buffer);
assert(sz);
total = BUS_MESSAGE_SIZE(m);
p = malloc(total);
if (!p)
return -ENOMEM;
e = mempcpy(p, m->header, BUS_MESSAGE_BODY_BEGIN(m));
MESSAGE_FOREACH_PART(part, i, m)
e = mempcpy(e, part->data, part->size);
assert(total == (size_t) ((uint8_t*) e - (uint8_t*) p));
*buffer = p;
*sz = total;
return 0;
}
_public_ int sd_bus_message_read_strv_extend(sd_bus_message *m, char ***l) {
char type;
const char *contents, *s;
size_t n;
int r;
assert(m);
assert(l);
r = sd_bus_message_peek_type(m, &type, &contents);
if (r < 0)
return r;
if (type != SD_BUS_TYPE_ARRAY || !STR_IN_SET(contents, "s", "o", "g"))
return -ENXIO;
r = sd_bus_message_enter_container(m, 'a', NULL);
if (r <= 0)
return r;
n = strv_length(*l);
/* sd_bus_message_read_basic() does content validation for us. */
while ((r = sd_bus_message_read_basic(m, *contents, &s)) > 0) {
r = strv_extend_with_size(l, &n, s);
if (r < 0)
return r;
}
if (r < 0)
return r;
r = sd_bus_message_exit_container(m);
if (r < 0)
return r;
return 1;
}
_public_ int sd_bus_message_read_strv(sd_bus_message *m, char ***ret) {
_cleanup_strv_free_ char **strv = NULL;
int r;
assert_return(m, -EINVAL);
assert_return(m->sealed, -EPERM);
assert_return(ret, -EINVAL);
r = sd_bus_message_read_strv_extend(m, &strv);
if (r <= 0)
return r;
*ret = TAKE_PTR(strv);
return 1;
}
static int bus_message_get_arg_skip(
sd_bus_message *m,
unsigned i,
char *_type,
const char **_contents) {
unsigned j;
int r;
r = sd_bus_message_rewind(m, true);
if (r < 0)
return r;
for (j = 0;; j++) {
const char *contents;
char type;
r = sd_bus_message_peek_type(m, &type, &contents);
if (r < 0)
return r;
if (r == 0)
return -ENXIO;
/* Don't match against arguments after the first one we don't understand */
if (!IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH, SD_BUS_TYPE_SIGNATURE) &&
!(type == SD_BUS_TYPE_ARRAY && STR_IN_SET(contents, "s", "o", "g")))
return -ENXIO;
if (j >= i) {
if (_contents)
*_contents = contents;
if (_type)
*_type = type;
return 0;
}
r = sd_bus_message_skip(m, NULL);
if (r < 0)
return r;
}
}
int bus_message_get_arg(sd_bus_message *m, unsigned i, const char **str) {
char type;
int r;
assert(m);
assert(str);
r = bus_message_get_arg_skip(m, i, &type, NULL);
if (r < 0)
return r;
if (!IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH, SD_BUS_TYPE_SIGNATURE))
return -ENXIO;
return sd_bus_message_read_basic(m, type, str);
}
int bus_message_get_arg_strv(sd_bus_message *m, unsigned i, char ***strv) {
const char *contents;
char type;
int r;
assert(m);
assert(strv);
r = bus_message_get_arg_skip(m, i, &type, &contents);
if (r < 0)
return r;
if (type != SD_BUS_TYPE_ARRAY)
return -ENXIO;
if (!STR_IN_SET(contents, "s", "o", "g"))
return -ENXIO;
return sd_bus_message_read_strv(m, strv);
}
_public_ int sd_bus_message_get_errno(sd_bus_message *m) {
assert_return(m, EINVAL);
if (m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
return 0;
return sd_bus_error_get_errno(&m->error);
}
_public_ const char* sd_bus_message_get_signature(sd_bus_message *m, int complete) {
BusMessageContainer *c;
assert_return(m, NULL);
c = complete ? &m->root_container : message_get_last_container(m);
return strempty(c->signature);
}
_public_ int sd_bus_message_is_empty(sd_bus_message *m) {
assert_return(m, -EINVAL);
return isempty(m->root_container.signature);
}
_public_ int sd_bus_message_has_signature(sd_bus_message *m, const char *signature) {
assert_return(m, -EINVAL);
return streq(strempty(m->root_container.signature), strempty(signature));
}
_public_ int sd_bus_message_copy(sd_bus_message *m, sd_bus_message *source, int all) {
bool done_something = false;
int r;
assert_return(m, -EINVAL);
assert_return(source, -EINVAL);
assert_return(!m->sealed, -EPERM);
assert_return(source->sealed, -EPERM);
do {
const char *contents;
char type;
union {
uint8_t u8;
uint16_t u16;
int16_t s16;
uint32_t u32;
int32_t s32;
uint64_t u64;
int64_t s64;
double d64;
const char *string;
int i;
} basic;
r = sd_bus_message_peek_type(source, &type, &contents);
if (r < 0)
return r;
if (r == 0)
break;
done_something = true;
if (bus_type_is_container(type) > 0) {
r = sd_bus_message_enter_container(source, type, contents);
if (r < 0)
return r;
r = sd_bus_message_open_container(m, type, contents);
if (r < 0)
return r;
r = sd_bus_message_copy(m, source, true);
if (r < 0)
return r;
r = sd_bus_message_close_container(m);
if (r < 0)
return r;
r = sd_bus_message_exit_container(source);
if (r < 0)
return r;
continue;
}
r = sd_bus_message_read_basic(source, type, &basic);
if (r < 0)
return r;
assert(r > 0);
if (IN_SET(type, SD_BUS_TYPE_OBJECT_PATH, SD_BUS_TYPE_SIGNATURE, SD_BUS_TYPE_STRING))
r = sd_bus_message_append_basic(m, type, basic.string);
else
r = sd_bus_message_append_basic(m, type, &basic);
if (r < 0)
return r;
} while (all);
return done_something;
}
_public_ int sd_bus_message_verify_type(sd_bus_message *m, char type, const char *contents) {
const char *c;
char t;
int r;
assert_return(m, -EINVAL);
assert_return(m->sealed, -EPERM);
assert_return(!type || bus_type_is_valid(type), -EINVAL);
assert_return(!contents || signature_is_valid(contents, true), -EINVAL);
assert_return(type || contents, -EINVAL);
assert_return(!contents || !type || bus_type_is_container(type), -EINVAL);
r = sd_bus_message_peek_type(m, &t, &c);
if (r <= 0)
return r;
if (type != 0 && type != t)
return 0;
if (contents && !streq_ptr(contents, c))
return 0;
return 1;
}
_public_ sd_bus* sd_bus_message_get_bus(sd_bus_message *m) {
assert_return(m, NULL);
return m->bus;
}
int bus_message_remarshal(sd_bus *bus, sd_bus_message **m) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *n = NULL;
usec_t timeout;
int r;
assert(bus);
assert(m);
assert(*m);
switch ((*m)->header->type) {
case SD_BUS_MESSAGE_SIGNAL:
r = sd_bus_message_new_signal(bus, &n, (*m)->path, (*m)->interface, (*m)->member);
if (r < 0)
return r;
break;
case SD_BUS_MESSAGE_METHOD_CALL:
r = sd_bus_message_new_method_call(bus, &n, (*m)->destination, (*m)->path, (*m)->interface, (*m)->member);
if (r < 0)
return r;
break;
case SD_BUS_MESSAGE_METHOD_RETURN:
case SD_BUS_MESSAGE_METHOD_ERROR:
r = sd_bus_message_new(bus, &n, (*m)->header->type);
if (r < 0)
return -ENOMEM;
assert(n);
n->reply_cookie = (*m)->reply_cookie;
r = message_append_reply_cookie(n, n->reply_cookie);
if (r < 0)
return r;
if ((*m)->header->type == SD_BUS_MESSAGE_METHOD_ERROR && (*m)->error.name) {
r = message_append_field_string(n, BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, (*m)->error.name, &n->error.message);
if (r < 0)
return r;
n->error._need_free = -1;
}
break;
default:
return -EINVAL;
}
if ((*m)->destination && !n->destination) {
r = message_append_field_string(n, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, (*m)->destination, &n->destination);
if (r < 0)
return r;
}
if ((*m)->sender && !n->sender) {
r = message_append_field_string(n, BUS_MESSAGE_HEADER_SENDER, SD_BUS_TYPE_STRING, (*m)->sender, &n->sender);
if (r < 0)
return r;
}
n->header->flags |= (*m)->header->flags & (BUS_MESSAGE_NO_REPLY_EXPECTED|BUS_MESSAGE_NO_AUTO_START);
r = sd_bus_message_copy(n, *m, true);
if (r < 0)
return r;
timeout = (*m)->timeout;
if (timeout == 0 && !((*m)->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)) {
r = sd_bus_get_method_call_timeout(bus, &timeout);
if (r < 0)
return r;
}
r = sd_bus_message_seal(n, BUS_MESSAGE_COOKIE(*m), timeout);
if (r < 0)
return r;
sd_bus_message_unref(*m);
*m = TAKE_PTR(n);
return 0;
}
_public_ int sd_bus_message_get_priority(sd_bus_message *m, int64_t *ret) {
static bool warned = false;
assert_return(m, -EINVAL);
assert_return(ret, -EINVAL);
if (!warned) {
log_debug("sd_bus_message_get_priority() is deprecated and always returns 0.");
warned = true;
}
*ret = 0;
return 0;
}
_public_ int sd_bus_message_set_priority(sd_bus_message *m, int64_t priority) {
static bool warned = false;
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
if (!warned) {
log_debug("sd_bus_message_set_priority() is deprecated and does nothing.");
warned = true;
}
return 0;
}
_public_ int sd_bus_message_sensitive(sd_bus_message *m) {
assert_return(m, -EINVAL);
m->sensitive = true;
return 0;
}
char** bus_message_make_log_fields(sd_bus_message *m) {
_cleanup_strv_free_ char **strv = NULL;
assert(m);
(void) strv_extend_assignment(&strv, "DBUS_MESSAGE_TYPE", bus_message_type_to_string(m->header->type));
(void) strv_extend_assignment(&strv, "DBUS_SENDER", sd_bus_message_get_sender(m));
(void) strv_extend_assignment(&strv, "DBUS_DESTINATION", sd_bus_message_get_destination(m));
(void) strv_extend_assignment(&strv, "DBUS_PATH", sd_bus_message_get_path(m));
(void) strv_extend_assignment(&strv, "DBUS_INTERFACE", sd_bus_message_get_interface(m));
(void) strv_extend_assignment(&strv, "DBUS_MEMBER", sd_bus_message_get_member(m));
(void) strv_extendf(&strv, "DBUS_MESSAGE_COOKIE=%" PRIu64, BUS_MESSAGE_COOKIE(m));
if (m->reply_cookie != 0)
(void) strv_extendf(&strv, "DBUS_MESSAGE_REPLY_COOKIE=%" PRIu64, m->reply_cookie);
(void) strv_extend_assignment(&strv, "DBUS_SIGNATURE", m->root_container.signature);
(void) strv_extend_assignment(&strv, "DBUS_ERROR_NAME", m->error.name);
(void) strv_extend_assignment(&strv, "DBUS_ERROR_MESSAGE", m->error.message);
return TAKE_PTR(strv);
}