luci2: change ops->access to ops->session_access to accomodate for changed plugin api
[project/rpcd.git] / luci2.c
diff --git a/luci2.c b/luci2.c
index 64f3d07..50381a2 100644 (file)
--- a/luci2.c
+++ b/luci2.c
@@ -1,5 +1,5 @@
 /*
- * luci-rpcd - LuCI UBUS RPC server
+ * rpcd - UBUS RPC server
  *
  *   Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
  *
 #include <arpa/inet.h>
 #include <signal.h>
 #include <glob.h>
+#include <libubox/blobmsg_json.h>
+#include <libubus.h>
+#include <uci.h>
 
-#include "luci2.h"
-#include "exec.h"
-#include "session.h"
+#include "plugin.h"
+
+/* limit of log size buffer */
+#define RPC_LUCI2_MAX_LOGSIZE          (128 * 1024)
+#define RPC_LUCI2_DEF_LOGSIZE       (16 * 1024)
+
+/* location of menu definitions */
+#define RPC_LUCI2_MENU_FILES        "/usr/share/luci2/menu.d/*.json" /* */
+
+
+static const struct rpc_daemon_ops *ops;
 
 static struct blob_buf buf;
 static struct uci_context *cursor;
@@ -964,7 +975,7 @@ rpc_luci2_upgrade_test(struct ubus_context *ctx, struct ubus_object *obj,
                        struct blob_attr *msg)
 {
        const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
-       return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
+       return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
 }
 
 static int
@@ -994,7 +1005,7 @@ rpc_luci2_backup_restore(struct ubus_context *ctx, struct ubus_object *obj,
        const char *cmd[4] = { "sysupgrade", "--restore-backup",
                               "/tmp/backup.tar.gz", NULL };
 
-       return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
+       return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
 }
 
 static int
@@ -1082,15 +1093,17 @@ backup_parse_list(struct blob_buf *blob, char *buf, int len, void *priv)
        return (nl - buf + 1);
 }
 
-static void
+static int
 backup_finish_list(struct blob_buf *blob, int status, void *priv)
 {
        struct backup_state *s = priv;
 
        if (!s->open)
-               return;
+               return UBUS_STATUS_NO_DATA;
 
        blobmsg_close_array(blob, s->array);
+
+       return UBUS_STATUS_OK;
 }
 
 static int
@@ -1108,8 +1121,8 @@ rpc_luci2_backup_list(struct ubus_context *ctx, struct ubus_object *obj,
 
        memset(state, 0, sizeof(*state));
 
-       return rpc_exec(cmd, backup_parse_list, NULL, backup_finish_list,
-                       state, ctx, req);
+       return ops->exec(cmd, NULL, backup_parse_list, NULL, backup_finish_list,
+                        state, ctx, req);
 }
 
 static int
@@ -1799,16 +1812,18 @@ skip:
        return (nl - buf + 1);
 }
 
-static void
+static int
 opkg_finish_list(struct blob_buf *blob, int status, void *priv)
 {
        struct opkg_state *s = priv;
 
        if (!s->open)
-               return;
+               return UBUS_STATUS_NO_DATA;
 
        blobmsg_close_array(blob, s->array);
        blobmsg_add_u32(blob, "total", s->total);
+
+       return UBUS_STATUS_OK;
 }
 
 static int
@@ -1844,8 +1859,8 @@ opkg_exec_list(const char *action, struct blob_attr *msg,
        if (state->req_count <= 0 || state->req_count > 100)
                state->req_count = 100;
 
-       return rpc_exec(cmd, opkg_parse_list, NULL, opkg_finish_list,
-                       state, ctx, req);
+       return ops->exec(cmd, NULL, opkg_parse_list, NULL, opkg_finish_list,
+                        state, ctx, req);
 }
 
 
@@ -1879,7 +1894,7 @@ rpc_luci2_opkg_update(struct ubus_context *ctx, struct ubus_object *obj,
                       struct blob_attr *msg)
 {
        const char *cmd[3] = { "opkg", "update", NULL };
-       return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
+       return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
 }
 
 static int
@@ -1899,7 +1914,7 @@ rpc_luci2_opkg_install(struct ubus_context *ctx, struct ubus_object *obj,
 
        cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
 
-       return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
+       return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
 }
 
 static int
@@ -1919,7 +1934,7 @@ rpc_luci2_opkg_remove(struct ubus_context *ctx, struct ubus_object *obj,
 
        cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
 
-       return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
+       return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
 }
 
 static int
@@ -1972,19 +1987,32 @@ rpc_luci2_opkg_config_set(struct ubus_context *ctx, struct ubus_object *obj,
 
 
 static bool
-menu_access(struct blob_attr *sid, struct blob_attr *acls)
+menu_access(struct blob_attr *sid, struct blob_attr *acls, struct blob_buf *e)
 {
        int rem;
        struct blob_attr *acl;
+       bool rv = true;
+       void *c;
+
+       c = blobmsg_open_table(e, "write");
 
        blobmsg_for_each_attr(acl, acls, rem)
        {
-               if (!rpc_session_access(blobmsg_data(sid), "luci-ui",
-                                       blobmsg_data(acl), "read"))
-                       return false;
+               if (!ops->session_access(blobmsg_data(sid), "luci-ui",
+                                        blobmsg_data(acl), "read"))
+               {
+                       rv = false;
+                       break;
+               }
+
+               blobmsg_add_u8(e, blobmsg_data(acl),
+                              ops->session_access(blobmsg_data(sid), "luci-ui",
+                                                  blobmsg_data(acl), "write"));
        }
 
-       return true;
+       blobmsg_close_table(e, c);
+
+       return rv;
 }
 
 static int
@@ -1995,10 +2023,11 @@ rpc_luci2_ui_menu(struct ubus_context *ctx, struct ubus_object *obj,
        int i, rem, rem2;
        glob_t gl;
        struct blob_buf menu = { 0 };
+       struct blob_buf item = { 0 };
        struct blob_attr *entry, *attr;
        struct blob_attr *tb[__RPC_MENU_MAX];
        bool access;
-       void *c;
+       void *c, *d;
 
        blobmsg_parse(rpc_menu_policy, __RPC_MENU_MAX, tb,
                      blob_data(msg), blob_len(msg));
@@ -2017,29 +2046,35 @@ rpc_luci2_ui_menu(struct ubus_context *ctx, struct ubus_object *obj,
                        blob_buf_init(&menu, 0);
 
                        if (!blobmsg_add_json_from_file(&menu, gl.gl_pathv[i]))
-                               continue;
+                               goto skip;
 
                        blob_for_each_attr(entry, menu.head, rem)
                        {
                                access = true;
 
+                               blob_buf_init(&item, 0);
+                               d = blobmsg_open_table(&item, blobmsg_name(entry));
+
                                blobmsg_for_each_attr(attr, entry, rem2)
                                {
-                                       if (blob_id(attr) != BLOBMSG_TYPE_ARRAY)
-                                               continue;
-
-                                       if (strcmp(blobmsg_name(attr), "acls"))
-                                               continue;
-
-                                       access = menu_access(tb[RPC_MENU_SESSION], attr);
-                                       break;
+                                       if (blob_id(attr) == BLOBMSG_TYPE_ARRAY &&
+                                           !strcmp(blobmsg_name(attr), "acls"))
+                                               access = menu_access(tb[RPC_MENU_SESSION], attr, &item);
+                                       else
+                                               blobmsg_add_blob(&item, attr);
                                }
 
-                               if (!access)
-                                       continue;
+                               blobmsg_close_table(&item, d);
+
+                               if (access)
+                                       blob_for_each_attr(attr, item.head, rem2)
+                                               blobmsg_add_blob(&buf, attr);
 
-                               blobmsg_add_blob(&buf, entry);
+                               blob_buf_free(&item);
                        }
+
+skip:
+                       blob_buf_free(&menu);
                }
 
                globfree(&gl);
@@ -2052,7 +2087,8 @@ rpc_luci2_ui_menu(struct ubus_context *ctx, struct ubus_object *obj,
 }
 
 
-int rpc_luci2_api_init(struct ubus_context *ctx)
+static int
+rpc_luci2_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
 {
        int rv = 0;
 
@@ -2173,6 +2209,8 @@ int rpc_luci2_api_init(struct ubus_context *ctx)
        if (!cursor)
                return UBUS_STATUS_UNKNOWN_ERROR;
 
+       ops = o;
+
        rv |= ubus_add_object(ctx, &system_obj);
        rv |= ubus_add_object(ctx, &network_obj);
        rv |= ubus_add_object(ctx, &opkg_obj);
@@ -2180,3 +2218,7 @@ int rpc_luci2_api_init(struct ubus_context *ctx)
 
        return rv;
 }
+
+const struct rpc_plugin rpc_plugin = {
+       .init = rpc_luci2_api_init
+};