X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flibsystemd-bus%2Fbus-match.c;h=ffc97562fd8e286d12db6f6deeabf82f3c440f57;hb=843f737ade9c73609a2280dd3dd16e18222a5dcb;hp=61b8a5cd8878570b034b52b8df33b97e8d33e89a;hpb=38911893c55adfe0ec2c01785dfa49059b409d69;p=elogind.git diff --git a/src/libsystemd-bus/bus-match.c b/src/libsystemd-bus/bus-match.c index 61b8a5cd8..ffc97562f 100644 --- a/src/libsystemd-bus/bus-match.c +++ b/src/libsystemd-bus/bus-match.c @@ -22,6 +22,9 @@ #include "bus-internal.h" #include "bus-message.h" #include "bus-match.h" +#include "bus-error.h" +#include "bus-util.h" +#include "strv.h" /* Example: * @@ -127,7 +130,8 @@ static bool value_node_test( struct bus_match_node *node, enum bus_match_node_type parent_type, uint8_t value_u8, - const char *value_str) { + const char *value_str, + sd_bus_message *m) { assert(node); assert(node->type == BUS_MATCH_VALUE); @@ -141,23 +145,34 @@ static bool value_node_test( return node->value.u8 == value_u8; case BUS_MATCH_SENDER: - case BUS_MATCH_DESTINATION: if (streq_ptr(node->value.str, value_str)) return true; - /* FIXME: So here's an ugliness: if the match is for a - * well-known name then we cannot actually check this - * correctly here. This doesn't matter much for dbus1 - * where no false positives exist, hence we just - * ignore this case here. For kdbus the messages - * should contain all well-known names of the sender, - * hence we can fix things there correctly. */ + if (m->creds.mask & SD_BUS_CREDS_WELL_KNOWN_NAMES) { + char **i; - if (node->value.str[0] != ':' && value_str && value_str[0] == ':') - return true; + /* on kdbus we have the well known names list + * in the credentials, let's make use of that + * for an accurate match */ + + STRV_FOREACH(i, m->creds.well_known_names) + if (streq_ptr(node->value.str, *i)) + return true; + + } else { + + /* If we don't have kdbus, we don't know the + * well-known names of the senders. In that, + * let's just hope that dbus-daemon doesn't + * send us stuff we didn't want. */ + + if (node->value.str[0] != ':' && value_str && value_str[0] == ':') + return true; + } return false; + case BUS_MATCH_DESTINATION: case BUS_MATCH_INTERFACE: case BUS_MATCH_MEMBER: case BUS_MATCH_PATH: @@ -272,7 +287,10 @@ int bus_match_run( /* Run the callback. And then invoke siblings. */ if (node->leaf.callback) { - r = node->leaf.callback(bus, m, node->leaf.userdata); + _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL; + + r = node->leaf.callback(bus, m, node->leaf.userdata, &error_buffer); + r = bus_maybe_reply_error(m, r, &error_buffer); if (r != 0) return r; } @@ -344,7 +362,7 @@ int bus_match_run( /* No hash table, so let's iterate manually... */ for (c = node->child; c; c = c->next) { - if (!value_node_test(c, node->type, test_u8, test_str)) + if (!value_node_test(c, node->type, test_u8, test_str, m)) continue; r = bus_match_run(bus, c, m); @@ -708,25 +726,31 @@ int bus_match_parse( enum bus_match_node_type t; unsigned j = 0; size_t value_allocated = 0; - bool escaped = false; + bool escaped = false, quoted; uint8_t u; eq = strchr(p, '='); if (!eq) return -EINVAL; - if (eq[1] != '\'') - return -EINVAL; - t = bus_match_node_type_from_string(p, eq - p); if (t < 0) return -EINVAL; - for (q = eq + 2;; q++) { + quoted = eq[1] == '\''; + + for (q = eq + 1 + quoted;; q++) { if (*q == 0) { - r = -EINVAL; - goto fail; + + if (quoted) { + r = -EINVAL; + goto fail; + } else { + if (value) + value[j] = 0; + break; + } } if (!escaped) { @@ -734,10 +758,20 @@ int bus_match_parse( escaped = true; continue; } - if (*q == '\'') { - if (value) - value[j] = 0; - break; + + if (quoted) { + if (*q == '\'') { + if (value) + value[j] = 0; + break; + } + } else { + if (*q == ',') { + if (value) + value[j] = 0; + + break; + } } } @@ -750,6 +784,14 @@ int bus_match_parse( escaped = false; } + if (!value) { + value = strdup(""); + if (!value) { + r = -ENOMEM; + goto fail; + } + } + if (t == BUS_MATCH_MESSAGE_TYPE) { r = bus_message_type_from_string(value, &u); if (r < 0) @@ -775,12 +817,12 @@ int bus_match_parse( if (q[1] == 0) break; - if (q[1] != ',') { + if (q[quoted] != ',') { r = -EINVAL; goto fail; } - p = q + 2; + p = q + 1 + quoted; } /* Order the whole thing, so that we always generate the same tree */ @@ -803,6 +845,46 @@ fail: return r; } +char *bus_match_to_string(struct bus_match_component *components, unsigned n_components) { + _cleanup_free_ FILE *f = NULL; + char *buffer = NULL; + size_t size = 0; + unsigned i; + + if (n_components <= 0) + return strdup(""); + + assert(components); + + f = open_memstream(&buffer, &size); + if (!f) + return NULL; + + for (i = 0; i < n_components; i++) { + char buf[32]; + + if (i != 0) + fputc(',', f); + + fputs(bus_match_node_type_to_string(components[i].type, buf, sizeof(buf)), f); + fputc('=', f); + fputc('\'', f); + + if (components[i].type == BUS_MATCH_MESSAGE_TYPE) + fputs(bus_message_type_to_string(components[i].value_u8), f); + else + fputs(components[i].value_str, f); + + fputc('\'', f); + } + + fflush(f); + if (ferror(f)) + return NULL; + + return buffer; +} + int bus_match_add( struct bus_match_node *root, struct bus_match_component *components,