file: use blob_buf_free()
[project/rpcd.git] / session.c
index df96ced..b45d9fe 100644 (file)
--- a/session.c
+++ b/session.c
@@ -2,7 +2,7 @@
  * rpcd - UBUS RPC server
  *
  *   Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
- *   Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
+ *   Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
  *
  * Permission to use, copy, modify, and/or distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -26,6 +26,7 @@
 #include <fnmatch.h>
 #include <glob.h>
 #include <uci.h>
+#include <limits.h>
 
 #ifdef HAVE_SHADOW
 #include <shadow.h>
@@ -39,12 +40,20 @@ static struct blob_buf buf;
 static LIST_HEAD(create_callbacks);
 static LIST_HEAD(destroy_callbacks);
 
-static const struct blobmsg_policy new_policy = {
-       .name = "timeout", .type = BLOBMSG_TYPE_INT32
+enum {
+       RPC_SN_TIMEOUT,
+       __RPC_SN_MAX,
+};
+static const struct blobmsg_policy new_policy[__RPC_SN_MAX] = {
+       [RPC_SN_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
 };
 
-static const struct blobmsg_policy sid_policy = {
-       .name = "sid", .type = BLOBMSG_TYPE_STRING
+enum {
+       RPC_SI_SID,
+       __RPC_SI_MAX,
+};
+static const struct blobmsg_policy sid_policy[__RPC_SI_MAX] = {
+       [RPC_SI_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
 };
 
 enum {
@@ -53,7 +62,7 @@ enum {
        __RPC_SS_MAX,
 };
 static const struct blobmsg_policy set_policy[__RPC_SS_MAX] = {
-       [RPC_SS_SID] = { .name = "sid", .type = BLOBMSG_TYPE_STRING },
+       [RPC_SS_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
        [RPC_SS_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
 };
 
@@ -63,7 +72,7 @@ enum {
        __RPC_SG_MAX,
 };
 static const struct blobmsg_policy get_policy[__RPC_SG_MAX] = {
-       [RPC_SG_SID] = { .name = "sid", .type = BLOBMSG_TYPE_STRING },
+       [RPC_SG_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
        [RPC_SG_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY },
 };
 
@@ -74,7 +83,7 @@ enum {
        __RPC_SA_MAX,
 };
 static const struct blobmsg_policy acl_policy[__RPC_SA_MAX] = {
-       [RPC_SA_SID] = { .name = "sid", .type = BLOBMSG_TYPE_STRING },
+       [RPC_SA_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
        [RPC_SA_SCOPE] = { .name = "scope", .type = BLOBMSG_TYPE_STRING },
        [RPC_SA_OBJECTS] = { .name = "objects", .type = BLOBMSG_TYPE_ARRAY },
 };
@@ -87,7 +96,7 @@ enum {
        __RPC_SP_MAX,
 };
 static const struct blobmsg_policy perm_policy[__RPC_SP_MAX] = {
-       [RPC_SP_SID] = { .name = "sid", .type = BLOBMSG_TYPE_STRING },
+       [RPC_SP_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
        [RPC_SP_SCOPE] = { .name = "scope", .type = BLOBMSG_TYPE_STRING },
        [RPC_SP_OBJECT] = { .name = "object", .type = BLOBMSG_TYPE_STRING },
        [RPC_SP_FUNCTION] = { .name = "function", .type = BLOBMSG_TYPE_STRING },
@@ -97,15 +106,13 @@ enum {
        RPC_DUMP_SID,
        RPC_DUMP_TIMEOUT,
        RPC_DUMP_EXPIRES,
-       RPC_DUMP_ACLS,
        RPC_DUMP_DATA,
        __RPC_DUMP_MAX,
 };
 static const struct blobmsg_policy dump_policy[__RPC_DUMP_MAX] = {
-       [RPC_DUMP_SID] = { .name = "sid", .type = BLOBMSG_TYPE_STRING },
+       [RPC_DUMP_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
        [RPC_DUMP_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
        [RPC_DUMP_EXPIRES] = { .name = "expires", .type = BLOBMSG_TYPE_INT32 },
-       [RPC_DUMP_ACLS] = { .name = "acls", .type = BLOBMSG_TYPE_TABLE },
        [RPC_DUMP_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
 };
 
@@ -205,19 +212,21 @@ rpc_session_dump_acls(struct rpc_session *ses, struct blob_buf *b)
 }
 
 static void
-rpc_session_to_blob(struct rpc_session *ses)
+rpc_session_to_blob(struct rpc_session *ses, bool acls)
 {
        void *c;
 
        blob_buf_init(&buf, 0);
 
-       blobmsg_add_string(&buf, "sid", ses->id);
+       blobmsg_add_string(&buf, "ubus_rpc_session", ses->id);
        blobmsg_add_u32(&buf, "timeout", ses->timeout);
        blobmsg_add_u32(&buf, "expires", uloop_timeout_remaining(&ses->t) / 1000);
 
-       c = blobmsg_open_table(&buf, "acls");
-       rpc_session_dump_acls(ses, &buf);
-       blobmsg_close_table(&buf, c);
+       if (acls) {
+               c = blobmsg_open_table(&buf, "acls");
+               rpc_session_dump_acls(ses, &buf);
+               blobmsg_close_table(&buf, c);
+       }
 
        c = blobmsg_open_table(&buf, "data");
        rpc_session_dump_data(ses, &buf);
@@ -228,7 +237,7 @@ static void
 rpc_session_dump(struct rpc_session *ses, struct ubus_context *ctx,
                  struct ubus_request_data *req)
 {
-       rpc_session_to_blob(ses);
+       rpc_session_to_blob(ses, true);
 
        ubus_send_reply(ctx, req, buf.head);
 }
@@ -343,7 +352,7 @@ rpc_handle_create(struct ubus_context *ctx, struct ubus_object *obj,
        struct blob_attr *tb;
        int timeout = RPC_DEFAULT_SESSION_TIMEOUT;
 
-       blobmsg_parse(&new_policy, 1, &tb, blob_data(msg), blob_len(msg));
+       blobmsg_parse(new_policy, __RPC_SN_MAX, &tb, blob_data(msg), blob_len(msg));
        if (tb)
                timeout = blobmsg_get_u32(tb);
 
@@ -362,7 +371,7 @@ rpc_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
        struct rpc_session *ses;
        struct blob_attr *tb;
 
-       blobmsg_parse(&sid_policy, 1, &tb, blob_data(msg), blob_len(msg));
+       blobmsg_parse(sid_policy, __RPC_SI_MAX, &tb, blob_data(msg), blob_len(msg));
 
        if (!tb) {
                avl_for_each_element(&sessions, ses, avl)
@@ -386,7 +395,7 @@ uh_id_len(const char *str)
 }
 
 static int
-rpc_session_grant(struct rpc_session *ses, struct ubus_context *ctx,
+rpc_session_grant(struct rpc_session *ses,
                   const char *scope, const char *object, const char *function)
 {
        struct rpc_session_acl *acl;
@@ -437,7 +446,7 @@ rpc_session_grant(struct rpc_session *ses, struct ubus_context *ctx,
 }
 
 static int
-rpc_session_revoke(struct rpc_session *ses, struct ubus_context *ctx,
+rpc_session_revoke(struct rpc_session *ses,
                    const char *scope, const char *object, const char *function)
 {
        struct rpc_session_acl *acl, *next;
@@ -502,8 +511,8 @@ rpc_handle_acl(struct ubus_context *ctx, struct ubus_object *obj,
        const char *scope = "ubus";
        int rem1, rem2;
 
-       int (*cb)(struct rpc_session *ses, struct ubus_context *ctx,
-                 const char *scope, const char *object, const char *function);
+       int (*cb)(struct rpc_session *ses,
+                 const char *scope, const char *object, const char *function);
 
        blobmsg_parse(acl_policy, __RPC_SA_MAX, tb, blob_data(msg), blob_len(msg));
 
@@ -523,17 +532,17 @@ rpc_handle_acl(struct ubus_context *ctx, struct ubus_object *obj,
                cb = rpc_session_revoke;
 
        if (!tb[RPC_SA_OBJECTS])
-               return cb(ses, ctx, scope, NULL, NULL);
+               return cb(ses, scope, NULL, NULL);
 
        blobmsg_for_each_attr(attr, tb[RPC_SA_OBJECTS], rem1) {
-               if (blob_id(attr) != BLOBMSG_TYPE_ARRAY)
+               if (blobmsg_type(attr) != BLOBMSG_TYPE_ARRAY)
                        continue;
 
                object = NULL;
                function = NULL;
 
                blobmsg_for_each_attr(sattr, attr, rem2) {
-                       if (blob_id(sattr) != BLOBMSG_TYPE_STRING)
+                       if (blobmsg_type(sattr) != BLOBMSG_TYPE_STRING)
                                continue;
 
                        if (!object)
@@ -545,7 +554,7 @@ rpc_handle_acl(struct ubus_context *ctx, struct ubus_object *obj,
                }
 
                if (object && function)
-                       cb(ses, ctx, scope, object, function);
+                       cb(ses, scope, object, function);
        }
 
        return 0;
@@ -580,22 +589,31 @@ rpc_handle_access(struct ubus_context *ctx, struct ubus_object *obj,
 
        blobmsg_parse(perm_policy, __RPC_SP_MAX, tb, blob_data(msg), blob_len(msg));
 
-       if (!tb[RPC_SP_SID] || !tb[RPC_SP_OBJECT] || !tb[RPC_SP_FUNCTION])
+       if (!tb[RPC_SP_SID])
                return UBUS_STATUS_INVALID_ARGUMENT;
 
        ses = rpc_session_get(blobmsg_data(tb[RPC_SP_SID]));
        if (!ses)
                return UBUS_STATUS_NOT_FOUND;
 
-       if (tb[RPC_SP_SCOPE])
-               scope = blobmsg_data(tb[RPC_SP_SCOPE]);
+       blob_buf_init(&buf, 0);
 
-       allow = rpc_session_acl_allowed(ses, scope,
-                                                                       blobmsg_data(tb[RPC_SP_OBJECT]),
-                                                                       blobmsg_data(tb[RPC_SP_FUNCTION]));
+       if (tb[RPC_SP_OBJECT] && tb[RPC_SP_FUNCTION])
+       {
+               if (tb[RPC_SP_SCOPE])
+                       scope = blobmsg_data(tb[RPC_SP_SCOPE]);
+
+               allow = rpc_session_acl_allowed(ses, scope,
+                                               blobmsg_data(tb[RPC_SP_OBJECT]),
+                                               blobmsg_data(tb[RPC_SP_FUNCTION]));
+
+               blobmsg_add_u8(&buf, "access", allow);
+       }
+       else
+       {
+               rpc_session_dump_acls(ses, &buf);
+       }
 
-       blob_buf_init(&buf, 0);
-       blobmsg_add_u8(&buf, "access", allow);
        ubus_send_reply(ctx, req, buf.head);
 
        return 0;
@@ -627,7 +645,7 @@ rpc_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
                struct blob_attr *msg)
 {
        struct rpc_session *ses;
-       struct blob_attr *tb[__RPC_SA_MAX];
+       struct blob_attr *tb[__RPC_SS_MAX];
        struct blob_attr *attr;
        int rem;
 
@@ -657,7 +675,7 @@ rpc_handle_get(struct ubus_context *ctx, struct ubus_object *obj,
 {
        struct rpc_session *ses;
        struct rpc_session_data *data;
-       struct blob_attr *tb[__RPC_SA_MAX];
+       struct blob_attr *tb[__RPC_SG_MAX];
        struct blob_attr *attr;
        void *c;
        int rem;
@@ -676,7 +694,7 @@ rpc_handle_get(struct ubus_context *ctx, struct ubus_object *obj,
 
        if (tb[RPC_SG_KEYS])
                blobmsg_for_each_attr(attr, tb[RPC_SG_KEYS], rem) {
-                       if (blob_id(attr) != BLOBMSG_TYPE_STRING)
+                       if (blobmsg_type(attr) != BLOBMSG_TYPE_STRING)
                                continue;
 
                        data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl);
@@ -724,7 +742,7 @@ rpc_handle_unset(struct ubus_context *ctx, struct ubus_object *obj,
        }
 
        blobmsg_for_each_attr(attr, tb[RPC_SG_KEYS], rem) {
-               if (blob_id(attr) != BLOBMSG_TYPE_STRING)
+               if (blobmsg_type(attr) != BLOBMSG_TYPE_STRING)
                        continue;
 
                data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl);
@@ -746,11 +764,14 @@ rpc_handle_destroy(struct ubus_context *ctx, struct ubus_object *obj,
        struct rpc_session *ses;
        struct blob_attr *tb;
 
-       blobmsg_parse(&sid_policy, 1, &tb, blob_data(msg), blob_len(msg));
+       blobmsg_parse(sid_policy, __RPC_SI_MAX, &tb, blob_data(msg), blob_len(msg));
 
        if (!tb)
                return UBUS_STATUS_INVALID_ARGUMENT;
 
+       if (!strcmp(blobmsg_get_string(tb), RPC_DEFAULT_SESSION_ID))
+               return UBUS_STATUS_PERMISSION_DENIED;
+
        ses = rpc_session_get(blobmsg_data(tb));
        if (!ses)
                return UBUS_STATUS_NOT_FOUND;
@@ -792,7 +813,7 @@ rpc_login_test_password(const char *hash, const char *password)
 #endif
        }
 
-       crypt_hash = crypt(hash, password);
+       crypt_hash = crypt(password, hash);
 
        return !strcmp(crypt_hash, hash);
 }
@@ -801,7 +822,7 @@ static struct uci_section *
 rpc_login_test_login(struct uci_context *uci,
                      const char *username, const char *password)
 {
-       struct uci_package *p;
+       struct uci_package *p = NULL;
        struct uci_section *s;
        struct uci_element *e;
        struct uci_ptr ptr = { .package = "rpcd" };
@@ -834,6 +855,11 @@ rpc_login_test_login(struct uci_context *uci,
                if (strcmp(ptr.o->v.string, username))
                        continue;
 
+               /* If password is NULL, we're restoring ACLs for an existing session,
+                * in this case do not check the password again. */
+               if (!password)
+                       return ptr.s;
+
                /* test for matching password */
                ptr.option = "password";
                ptr.o = NULL;
@@ -855,6 +881,7 @@ static bool
 rpc_login_test_permission(struct uci_section *s,
                           const char *perm, const char *group)
 {
+       const char *p;
        struct uci_option *o;
        struct uci_element *e, *l;
 
@@ -875,9 +902,30 @@ rpc_login_test_permission(struct uci_section *s,
                if (strcmp(o->e.name, perm))
                        continue;
 
-               uci_foreach_element(&o->v.list, l)
-                       if (l->name && !fnmatch(l->name, group, 0))
+               /* Match negative expressions first. If a negative expression matches
+                * the current group name then deny access. */
+               uci_foreach_element(&o->v.list, l) {
+                       p = l->name;
+
+                       if (!p || *p != '!')
+                               continue;
+
+                       while (isspace(*++p));
+
+                       if (!*p)
+                               continue;
+
+                       if (!fnmatch(p, group, 0))
+                               return false;
+               }
+
+               uci_foreach_element(&o->v.list, l) {
+                       if (!l->name || !*l->name || *l->name == '!')
+                               continue;
+
+                       if (!fnmatch(l->name, group, 0))
                                return true;
+               }
        }
 
        /* make sure that write permission implies read permission */
@@ -906,18 +954,18 @@ rpc_login_setup_acl_scope(struct rpc_session *ses,
         *              ]
         *      }
         */
-       if (blob_id(acl_scope) == BLOBMSG_TYPE_TABLE) {
+       if (blobmsg_type(acl_scope) == BLOBMSG_TYPE_TABLE) {
                blobmsg_for_each_attr(acl_obj, acl_scope, rem) {
-                       if (blob_id(acl_obj) != BLOBMSG_TYPE_ARRAY)
+                       if (blobmsg_type(acl_obj) != BLOBMSG_TYPE_ARRAY)
                                continue;
 
                        blobmsg_for_each_attr(acl_func, acl_obj, rem2) {
-                               if (blob_id(acl_func) != BLOBMSG_TYPE_STRING)
+                               if (blobmsg_type(acl_func) != BLOBMSG_TYPE_STRING)
                                        continue;
 
-                               rpc_session_grant(ses, NULL, blobmsg_name(acl_scope),
-                                                            blobmsg_name(acl_obj),
-                                                            blobmsg_data(acl_func));
+                               rpc_session_grant(ses, blobmsg_name(acl_scope),
+                                                      blobmsg_name(acl_obj),
+                                                      blobmsg_data(acl_func));
                        }
                }
        }
@@ -932,14 +980,14 @@ rpc_login_setup_acl_scope(struct rpc_session *ses,
         *              ...
         *      ]
         */
-       else if (blob_id(acl_scope) == BLOBMSG_TYPE_ARRAY) {
+       else if (blobmsg_type(acl_scope) == BLOBMSG_TYPE_ARRAY) {
                blobmsg_for_each_attr(acl_obj, acl_scope, rem) {
-                       if (blob_id(acl_obj) != BLOBMSG_TYPE_STRING)
+                       if (blobmsg_type(acl_obj) != BLOBMSG_TYPE_STRING)
                                continue;
 
-                       rpc_session_grant(ses, NULL, blobmsg_name(acl_scope),
-                                                        blobmsg_data(acl_obj),
-                                                        blobmsg_name(acl_perm));
+                       rpc_session_grant(ses, blobmsg_name(acl_scope),
+                                              blobmsg_data(acl_obj),
+                                              blobmsg_name(acl_perm));
                }
        }
 }
@@ -963,7 +1011,7 @@ rpc_login_setup_acl_file(struct rpc_session *ses, struct uci_section *login,
        blob_for_each_attr(acl_group, acl.head, rem) {
                /* Iterate permission objects in each access group object */
                blobmsg_for_each_attr(acl_perm, acl_group, rem2) {
-                       if (blob_id(acl_perm) != BLOBMSG_TYPE_TABLE)
+                       if (blobmsg_type(acl_perm) != BLOBMSG_TYPE_TABLE)
                                continue;
 
                        /* Only "read" and "write" permissions are defined */
@@ -1004,9 +1052,9 @@ rpc_login_setup_acl_file(struct rpc_session *ses, struct uci_section *login,
                                 * access groups without having to test access of each single
                                 * <scope>/<object>/<function> tuple defined in a group.
                                 */
-                               rpc_session_grant(ses, NULL, "access-group",
-                                                            blobmsg_name(acl_group),
-                                                            blobmsg_name(acl_perm));
+                               rpc_session_grant(ses, "access-group",
+                                                      blobmsg_name(acl_group),
+                                                      blobmsg_name(acl_perm));
                        }
                }
        }
@@ -1158,6 +1206,8 @@ rpc_blob_from_file(const char *path)
        if (len != blob_pad_len(&head))
                goto fail;
 
+       close(fd);
+
        return attr;
 
 fail:
@@ -1171,11 +1221,13 @@ fail:
 }
 
 static bool
-rpc_session_from_blob(struct blob_attr *attr)
+rpc_session_from_blob(struct uci_context *uci, struct blob_attr *attr)
 {
-       int i, rem, rem2, rem3;
+       int i, rem;
+       const char *user = NULL;
        struct rpc_session *ses;
-       struct blob_attr *tb[__RPC_DUMP_MAX], *scope, *object, *function;
+       struct uci_section *login;
+       struct blob_attr *tb[__RPC_DUMP_MAX], *data;
 
        blobmsg_parse(dump_policy, __RPC_DUMP_MAX, tb,
                      blob_data(attr), blob_len(attr));
@@ -1193,18 +1245,17 @@ rpc_session_from_blob(struct blob_attr *attr)
 
        ses->timeout = blobmsg_get_u32(tb[RPC_DUMP_TIMEOUT]);
 
-       blobmsg_for_each_attr(scope, tb[RPC_DUMP_ACLS], rem) {
-               blobmsg_for_each_attr(object, scope, rem2) {
-                       blobmsg_for_each_attr(function, object, rem3) {
-                               rpc_session_grant(ses, NULL, blobmsg_name(scope),
-                                                            blobmsg_name(object),
-                                                            blobmsg_data(function));
-                       }
-               }
+       blobmsg_for_each_attr(data, tb[RPC_DUMP_DATA], rem) {
+               rpc_session_set(ses, blobmsg_name(data), data);
+
+               if (!strcmp(blobmsg_name(data), "username"))
+                       user = blobmsg_get_string(data);
        }
 
-       blobmsg_for_each_attr(object, tb[RPC_DUMP_DATA], rem) {
-               rpc_session_set(ses, blobmsg_name(object), object);
+       if (uci && user) {
+               login = rpc_login_test_login(uci, user, NULL);
+               if (login)
+                       rpc_login_setup_acls(ses, login);
        }
 
        avl_insert(&sessions, &ses->avl);
@@ -1219,15 +1270,15 @@ int rpc_session_api_init(struct ubus_context *ctx)
        struct rpc_session *ses;
 
        static const struct ubus_method session_methods[] = {
-               UBUS_METHOD("create",  rpc_handle_create,  &new_policy),
-               UBUS_METHOD("list",    rpc_handle_list,    &sid_policy),
+               UBUS_METHOD("create",  rpc_handle_create,  new_policy),
+               UBUS_METHOD("list",    rpc_handle_list,    sid_policy),
                UBUS_METHOD("grant",   rpc_handle_acl,     acl_policy),
                UBUS_METHOD("revoke",  rpc_handle_acl,     acl_policy),
                UBUS_METHOD("access",  rpc_handle_access,  perm_policy),
                UBUS_METHOD("set",     rpc_handle_set,     set_policy),
                UBUS_METHOD("get",     rpc_handle_get,     get_policy),
                UBUS_METHOD("unset",   rpc_handle_unset,   get_policy),
-               UBUS_METHOD("destroy", rpc_handle_destroy, &sid_policy),
+               UBUS_METHOD("destroy", rpc_handle_destroy, sid_policy),
                UBUS_METHOD("login",   rpc_handle_login,   login_policy),
        };
 
@@ -1293,7 +1344,7 @@ void rpc_session_freeze(void)
                        continue;
 
                snprintf(path, sizeof(path) - 1, RPC_SESSION_DIRECTORY "/%s", ses->id);
-               rpc_session_to_blob(ses);
+               rpc_session_to_blob(ses, false);
                rpc_blob_to_file(path, buf.head);
        }
 }
@@ -1304,12 +1355,18 @@ void rpc_session_thaw(void)
        char path[PATH_MAX];
        struct dirent *e;
        struct blob_attr *attr;
+       struct uci_context *uci;
 
        d = opendir(RPC_SESSION_DIRECTORY);
 
        if (!d)
                return;
 
+       uci = uci_alloc_context();
+
+       if (!uci)
+               return;
+
        while ((e = readdir(d)) != NULL) {
                if (!rpc_validate_sid(e->d_name))
                        continue;
@@ -1320,7 +1377,7 @@ void rpc_session_thaw(void)
                attr = rpc_blob_from_file(path);
 
                if (attr) {
-                       rpc_session_from_blob(attr);
+                       rpc_session_from_blob(uci, attr);
                        free(attr);
                }
 
@@ -1328,4 +1385,6 @@ void rpc_session_thaw(void)
        }
 
        closedir(d);
+
+       uci_free_context(uci);
 }