file: use blob_buf_free()
[project/rpcd.git] / session.c
index e053979..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 = "ubus_rpc_session", .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 {
@@ -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)
@@ -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);
+
+       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]));
+               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;
@@ -746,7 +764,7 @@ 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;
@@ -1188,6 +1206,8 @@ rpc_blob_from_file(const char *path)
        if (len != blob_pad_len(&head))
                goto fail;
 
+       close(fd);
+
        return attr;
 
 fail:
@@ -1250,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),
        };