iwinfo: add 802.11ac hw mode
[project/rpcd.git] / session.c
index 6d27ff6..09f4104 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>
@@ -44,7 +45,7 @@ static const struct blobmsg_policy new_policy = {
 };
 
 static const struct blobmsg_policy sid_policy = {
-       .name = "sid", .type = BLOBMSG_TYPE_STRING
+       .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING
 };
 
 enum {
@@ -53,7 +54,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 +64,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 +75,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 +88,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 },
@@ -101,7 +102,7 @@ enum {
        __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_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
@@ -209,7 +210,7 @@ rpc_session_to_blob(struct rpc_session *ses, bool acls)
 
        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);
 
@@ -580,22 +581,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;
@@ -1188,6 +1198,8 @@ rpc_blob_from_file(const char *path)
        if (len != blob_pad_len(&head))
                goto fail;
 
+       close(fd);
+
        return attr;
 
 fail: