chiark / gitweb /
bus-policy: move name list iteration to policy users
authorDaniel Mack <daniel@zonque.org>
Thu, 9 Oct 2014 11:26:53 +0000 (13:26 +0200)
committerDaniel Mack <daniel@zonque.org>
Thu, 9 Oct 2014 11:31:01 +0000 (13:31 +0200)
We need to figure out which of the possible names satisfied the policy,
so we cannot do the iteration in check_policy_item() but have to leave it
to the users.

Test cases amended accordingly.

src/bus-proxyd/bus-policy.c
src/bus-proxyd/bus-policy.h
src/bus-proxyd/test-bus-policy.c

index aea8541d50a2cc6559622065b4807cec5b477b2b..625f5ddaeee6648bd207d2026772afa26b55ff5b 100644 (file)
@@ -602,14 +602,10 @@ struct policy_check_filter {
         int class;
         const struct ucred *ucred;
         int message_type;
+        const char *name;
         const char *interface;
         const char *path;
-        union {
-                const char *name;
-                const char *member;
-        };
-        char **names_strv;
-        Hashmap *names_hash;
+        const char *member;
 };
 
 static int is_permissive(PolicyItem *i) {
@@ -628,13 +624,8 @@ static int check_policy_item(PolicyItem *i, const struct policy_check_filter *fi
         case POLICY_ITEM_SEND:
         case POLICY_ITEM_RECV:
 
-                if (i->name) {
-                        if (filter->names_hash && !hashmap_contains(filter->names_hash, i->name))
-                                break;
-
-                        if (filter->names_strv && !strv_contains(filter->names_strv, i->name))
-                                break;
-                }
+                if (i->name && !streq_ptr(i->name, filter->name))
+                        break;
 
                 if ((i->message_type != _POLICY_ITEM_CLASS_UNSET) && (i->message_type != filter->message_type))
                         break;
@@ -651,14 +642,14 @@ static int check_policy_item(PolicyItem *i, const struct policy_check_filter *fi
                 return is_permissive(i);
 
         case POLICY_ITEM_OWN:
-                assert(filter->member);
+                assert(filter->name);
 
                 if (streq(i->name, "*") || streq(i->name, filter->name))
                         return is_permissive(i);
                 break;
 
         case POLICY_ITEM_OWN_PREFIX:
-                assert(filter->member);
+                assert(filter->name);
 
                 if (streq(i->name, "*") || startswith(i->name, filter->name))
                         return is_permissive(i);
@@ -780,8 +771,8 @@ bool policy_check_hello(Policy *p, const struct ucred *ucred) {
 
 bool policy_check_recv(Policy *p,
                        const struct ucred *ucred,
-                       Hashmap *names,
                        int message_type,
+                       const char *name,
                        const char *path,
                        const char *interface,
                        const char *member) {
@@ -789,8 +780,8 @@ bool policy_check_recv(Policy *p,
         struct policy_check_filter filter = {
                 .class        = POLICY_ITEM_RECV,
                 .ucred        = ucred,
-                .names_hash   = names,
                 .message_type = message_type,
+                .name         = name,
                 .interface    = interface,
                 .path         = path,
                 .member       = member,
@@ -801,8 +792,8 @@ bool policy_check_recv(Policy *p,
 
 bool policy_check_send(Policy *p,
                        const struct ucred *ucred,
-                       char **names,
                        int message_type,
+                       const char *name,
                        const char *path,
                        const char *interface,
                        const char *member) {
@@ -810,8 +801,8 @@ bool policy_check_send(Policy *p,
         struct policy_check_filter filter = {
                 .class        = POLICY_ITEM_SEND,
                 .ucred        = ucred,
-                .names_strv   = names,
                 .message_type = message_type,
+                .name         = name,
                 .interface    = interface,
                 .path         = path,
                 .member       = member,
index 5b4d9d0c1065bf9d6c8a673df329a7faa0dcf081..64fe1ffac5cf9befe51cfba20c8d48ca5166cd89 100644 (file)
@@ -80,15 +80,15 @@ bool policy_check_own(Policy *p, const struct ucred *ucred, const char *name);
 bool policy_check_hello(Policy *p, const struct ucred *ucred);
 bool policy_check_recv(Policy *p,
                        const struct ucred *ucred,
-                       Hashmap *names,
                        int message_type,
+                       const char *name,
                        const char *path,
                        const char *interface,
                        const char *member);
 bool policy_check_send(Policy *p,
                        const struct ucred *ucred,
-                       char **names,
                        int message_type,
+                       const char *name,
                        const char *path,
                        const char *interface,
                        const char *member);
index 5267788b704d145e3b2241b9eed81901b601f4b6..c9a027e8771bd9a4cd08e59fadf09b1199533b59 100644 (file)
@@ -63,8 +63,6 @@ int main(int argc, char *argv[]) {
 
         Policy p = {};
         struct ucred ucred = {};
-        char **names_strv;
-        Hashmap *names_hash;
 
         /* Ownership tests */
         assert_se(test_policy_load(&p, "ownerships.conf") == 0);
@@ -93,32 +91,27 @@ int main(int argc, char *argv[]) {
 
         /* Signaltest */
         assert_se(test_policy_load(&p, "signals.conf") == 0);
-        names_strv = STRV_MAKE("bli.bla.blubb");
 
         ucred.uid = 0;
-        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_SIGNAL, NULL, "/an/object/path", NULL) == true);
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_SIGNAL, "bli.bla.blubb", NULL, "/an/object/path", NULL) == true);
 
         ucred.uid = 1;
-        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_SIGNAL, NULL, "/an/object/path", NULL) == false);
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_SIGNAL, "bli.bla.blubb", NULL, "/an/object/path", NULL) == false);
 
         policy_free(&p);
 
         /* Method calls */
         assert_se(test_policy_load(&p, "methods.conf") == 0);
-        names_strv = STRV_MAKE("org.test.test1");
         policy_dump(&p);
 
         ucred.uid = 0;
 
-        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "bli.bla.blubb", "Member") == false);
-        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "bli.bla.blubb", "Member") == false);
-        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "org.test.int1", "Member") == true);
-        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "org.test.int2", "Member") == true);
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "bli.bla.blubb", "Member") == false);
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "bli.bla.blubb", "Member") == false);
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int1", "Member") == true);
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int2", "Member") == true);
 
-        names_hash = hashmap_new(&string_hash_ops);
-        assert(names_hash != NULL);
-        assert_se(hashmap_put(names_hash, "org.test.test3", NULL) >= 0);
-        assert_se(policy_check_recv(&p, &ucred, names_hash, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "org.test.int3", "Member111") == true);
+        assert_se(policy_check_recv(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test3", "/an/object/path", "org.test.int3", "Member111") == true);
 
         policy_free(&p);