chiark / gitweb /
util: unify SO_PEERCRED/SO_PEERSEC invocations
[elogind.git] / src / libsystemd-bus / bus-match.c
index f7fca5f573de962ec250927bff15a84d648b6a3a..34488d86730f1bf125b860b47c1d59cbffea9bfd 100644 (file)
@@ -24,6 +24,7 @@
 #include "bus-match.h"
 #include "bus-error.h"
 #include "bus-util.h"
+#include "strv.h"
 
 /* Example:
  *
@@ -129,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);
@@ -143,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:
@@ -349,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);
@@ -755,6 +768,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)
@@ -808,6 +829,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,