chiark / gitweb /
bus-proxyd: assorted cleanups and fixes
[elogind.git] / src / bus-proxyd / bus-policy.c
index 165e763f57ba0b7f8bacc0516ade966541cf2cf6..aea8541d50a2cc6559622065b4807cec5b477b2b 100644 (file)
@@ -604,18 +604,26 @@ struct policy_check_filter {
         int message_type;
         const char *interface;
         const char *path;
-        const char *member;
+        union {
+                const char *name;
+                const char *member;
+        };
         char **names_strv;
         Hashmap *names_hash;
 };
 
 static int is_permissive(PolicyItem *i) {
 
+        assert(i);
+
         return (i->type == POLICY_ITEM_ALLOW) ? ALLOW : DENY;
 }
 
 static int check_policy_item(PolicyItem *i, const struct policy_check_filter *filter) {
 
+        assert(i);
+        assert(filter);
+
         switch (i->class) {
         case POLICY_ITEM_SEND:
         case POLICY_ITEM_RECV:
@@ -628,7 +636,7 @@ static int check_policy_item(PolicyItem *i, const struct policy_check_filter *fi
                                 break;
                 }
 
-                if (i->message_type && (i->message_type != filter->message_type))
+                if ((i->message_type != _POLICY_ITEM_CLASS_UNSET) && (i->message_type != filter->message_type))
                         break;
 
                 if (i->path && !streq_ptr(i->path, filter->path))
@@ -643,21 +651,29 @@ static int check_policy_item(PolicyItem *i, const struct policy_check_filter *fi
                 return is_permissive(i);
 
         case POLICY_ITEM_OWN:
-                if (streq(i->name, filter->member))
+                assert(filter->member);
+
+                if (streq(i->name, "*") || streq(i->name, filter->name))
                         return is_permissive(i);
                 break;
 
         case POLICY_ITEM_OWN_PREFIX:
-                if (startswith(i->name, filter->member))
+                assert(filter->member);
+
+                if (streq(i->name, "*") || startswith(i->name, filter->name))
                         return is_permissive(i);
                 break;
 
         case POLICY_ITEM_USER:
+                assert(filter->ucred);
+
                 if ((streq_ptr(i->name, "*") || (i->uid_valid && i->uid == filter->ucred->uid)))
                         return is_permissive(i);
                 break;
 
         case POLICY_ITEM_GROUP:
+                assert(filter->ucred);
+
                 if ((streq_ptr(i->name, "*") || (i->gid_valid && i->gid == filter->ucred->gid)))
                         return is_permissive(i);
                 break;
@@ -675,6 +691,8 @@ static int check_policy_items(PolicyItem *items, const struct policy_check_filte
         PolicyItem *i;
         int r, ret = DUNNO;
 
+        assert(filter);
+
         /* Check all policies in a set - a broader one might be followed by a more specific one,
          * and the order of rules in policy definitions matters */
         LIST_FOREACH(items, i, items) {
@@ -694,6 +712,9 @@ static int policy_check(Policy *p, const struct policy_check_filter *filter) {
         PolicyItem *items;
         int r;
 
+        assert(p);
+        assert(filter);
+
         /*
          * The policy check is implemented by the following logic:
          *
@@ -729,9 +750,9 @@ static int policy_check(Policy *p, const struct policy_check_filter *filter) {
 bool policy_check_own(Policy *p, const struct ucred *ucred, const char *name) {
 
         struct policy_check_filter filter = {
-                .class  = POLICY_ITEM_OWN,
-                .ucred  = ucred,
-                .member = name,
+                .class = POLICY_ITEM_OWN,
+                .ucred = ucred,
+                .name  = name,
         };
 
         return policy_check(p, &filter) == ALLOW;
@@ -740,21 +761,21 @@ bool policy_check_own(Policy *p, const struct ucred *ucred, const char *name) {
 bool policy_check_hello(Policy *p, const struct ucred *ucred) {
 
         struct policy_check_filter filter = {
-                .class  = POLICY_ITEM_USER,
                 .ucred  = ucred,
         };
         int user, group;
 
+        filter.class = POLICY_ITEM_USER;
         user = policy_check(p, &filter);
         if (user == DENY)
                 return false;
 
         filter.class = POLICY_ITEM_GROUP;
         group = policy_check(p, &filter);
-        if (user == DUNNO && group == DUNNO)
+        if (group == DENY)
                 return false;
 
-        return !(user == DENY || group == DENY);
+        return !(user == DUNNO && group == DUNNO);
 }
 
 bool policy_check_recv(Policy *p,