ubusd: make ACL path configurable on the command line
[project/ubus.git] / ubusd_acl.c
index 31f8b45f1a7b5558d3ff59f510486b154df75650..dbfa83ad4b94b4d547d155154d83095ce28ac447 100644 (file)
 
 #include "ubusd.h"
 
+#ifndef SO_PEERCRED
+struct ucred {
+       int pid;
+       int uid;
+       int gid;
+};
+#endif
+
 struct ubusd_acl_obj {
        struct avl_node avl;
        struct list_head list;
@@ -54,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;
@@ -92,11 +101,11 @@ ubusd_acl_check(struct ubus_client *cl, const char *obj,
        struct blob_attr *cur;
        int rem;
 
-       if (!cl->gid && !cl->gid)
+       if (!cl->gid && !cl->uid)
                return 0;
 
        acl = avl_find_ge_element(&ubusd_acls, obj, acl, avl);
-       while (acl && !avl_is_last(&ubusd_acls, &acl->avl)) {
+       while (acl) {
                int diff = ubusd_acl_match_path(obj, acl->avl.key, NULL);
 
                if (diff)
@@ -135,13 +144,18 @@ ubusd_acl_check(struct ubus_client *cl, const char *obj,
 int
 ubusd_acl_init_client(struct ubus_client *cl, int fd)
 {
-       unsigned int len = sizeof(struct ucred);
        struct ucred cred;
        struct passwd *pwd;
        struct group *group;
 
+#ifdef SO_PEERCRED
+       unsigned int len = sizeof(struct ucred);
+
        if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len) == -1)
                return -1;
+#else
+       memset(&cred, 0, sizeof(cred));
+#endif
 
        pwd = getpwuid(cred.uid);
        if (!pwd)
@@ -193,7 +207,7 @@ ubusd_acl_alloc_obj(struct ubusd_acl_file *file, const char *obj)
        struct ubusd_acl_obj *o;
        char *k;
 
-       o = calloc_a(1, sizeof(*o), &k, strlen(obj) + 1);
+       o = calloc_a(sizeof(*o), &k, strlen(obj) + 1);
        o->user = file->user;
        o->group = file->group;
        o->avl.key = k;
@@ -284,9 +298,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);
@@ -369,8 +380,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);