ubusd: simplify/fix avl loop in ubusd_acl_check()
[project/ubus.git] / ubusd_acl.c
index 1d965c0e57d195f4251755072b3d8e250b2762a6..9f0f0fccd79197a98bf113f746a9b5862d727af6 100644 (file)
@@ -62,6 +62,7 @@ struct ubusd_acl_file {
        int ok;
 };
 
+const char *ubusd_acl_dir = "/usr/share/acl.d";
 static struct blob_buf bbuf;
 static struct avl_tree ubusd_acls;
 static int ubusd_acl_seq;
@@ -104,16 +105,14 @@ ubusd_acl_check(struct ubus_client *cl, const char *obj,
                return 0;
 
        acl = avl_find_ge_element(&ubusd_acls, obj, acl, avl);
-       while (acl && !avl_is_last(&ubusd_acls, &acl->avl)) {
+       avl_for_element_to_last(&ubusd_acls, acl, acl, avl) {
                int diff = ubusd_acl_match_path(obj, acl->avl.key, NULL);
 
                if (diff)
                        break;
 
-               if (ubusd_acl_match_cred(cl, acl)) {
-                       acl = avl_next_element(acl, avl);
+               if (ubusd_acl_match_cred(cl, acl))
                        continue;
-               }
 
                switch (type) {
                case UBUS_ACL_PUBLISH:
@@ -134,7 +133,6 @@ ubusd_acl_check(struct ubus_client *cl, const char *obj,
                                                        return 0;
                        break;
                }
-               acl = avl_next_element(acl, avl);
        }
 
        return -1;
@@ -297,9 +295,6 @@ ubusd_acl_file_add(struct ubusd_acl_file *file)
        else
                return;
 
-       if (!tb[ACL_ACCESS] && !tb[ACL_PUBLISH] && !tb[ACL_INHERIT])
-               return;
-
        if (tb[ACL_ACCESS])
                blobmsg_for_each_attr(cur, tb[ACL_ACCESS], rem)
                        ubusd_acl_add_access(file, cur);
@@ -382,8 +377,11 @@ ubusd_acl_load(void)
        struct stat st;
        glob_t gl;
        int j;
+       const char *suffix = "/*.json";
+       char *path = alloca(strlen(ubusd_acl_dir) + strlen(suffix) + 1);
 
-       if (glob("/usr/share/acl.d/*.json", GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
+       sprintf(path, "%s%s", ubusd_acl_dir, suffix);
+       if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
                return;
 
        vlist_update(&ubusd_acl_files);