utils: do not emit eof chunk for 204/304 responses
[project/uhttpd.git] / ubus.c
diff --git a/ubus.c b/ubus.c
index 2b4ac5b..269f7cd 100644 (file)
--- a/ubus.c
+++ b/ubus.c
 /*
  * uhttpd - Tiny single-threaded httpd
  *
- *   Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
- *   Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
+ *   Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
+ *   Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  *
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
  *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-
 #include <libubox/blobmsg.h>
 #include <libubox/blobmsg_json.h>
 #include <libubox/avl.h>
 #include <libubox/avl-cmp.h>
-#include <fnmatch.h>
 #include <stdio.h>
 #include <poll.h>
 
 #include "uhttpd.h"
 #include "plugin.h"
-#include "ubus.h"
 
 static const struct uhttpd_ops *ops;
 static struct config *_conf;
 #define conf (*_conf)
 
 static struct ubus_context *ctx;
-static struct avl_tree sessions;
 static struct blob_buf buf;
 
-static const struct blobmsg_policy new_policy = {
-       .name = "timeout", .type = BLOBMSG_TYPE_INT32
+#define UH_UBUS_MAX_POST_SIZE  4096
+#define UH_UBUS_DEFAULT_SID    "00000000000000000000000000000000"
+
+enum {
+       RPC_JSONRPC,
+       RPC_METHOD,
+       RPC_PARAMS,
+       RPC_ID,
+       __RPC_MAX,
 };
 
-static const struct blobmsg_policy sid_policy = {
-       .name = "sid", .type = BLOBMSG_TYPE_STRING
+static const struct blobmsg_policy rpc_policy[__RPC_MAX] = {
+       [RPC_JSONRPC] = { .name = "jsonrpc", .type = BLOBMSG_TYPE_STRING },
+       [RPC_METHOD] = { .name = "method", .type = BLOBMSG_TYPE_STRING },
+       [RPC_PARAMS] = { .name = "params", .type = BLOBMSG_TYPE_ARRAY },
+       [RPC_ID] = { .name = "id", .type = BLOBMSG_TYPE_UNSPEC },
 };
 
 enum {
-       UH_UBUS_SS_SID,
-       UH_UBUS_SS_VALUES,
-       __UH_UBUS_SS_MAX,
+       SES_ACCESS,
+       __SES_MAX,
 };
-static const struct blobmsg_policy set_policy[__UH_UBUS_SS_MAX] = {
-       [UH_UBUS_SS_SID] = { .name = "sid", .type = BLOBMSG_TYPE_STRING },
-       [UH_UBUS_SS_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
+
+static const struct blobmsg_policy ses_policy[__SES_MAX] = {
+       [SES_ACCESS] = { .name = "access", .type = BLOBMSG_TYPE_BOOL },
 };
 
-enum {
-       UH_UBUS_SG_SID,
-       UH_UBUS_SG_KEYS,
-       __UH_UBUS_SG_MAX,
+struct rpc_data {
+       struct blob_attr *id;
+       const char *sid;
+       const char *method;
+       const char *object;
+       const char *function;
+       struct blob_attr *data;
+       struct blob_attr *params;
 };
-static const struct blobmsg_policy get_policy[__UH_UBUS_SG_MAX] = {
-       [UH_UBUS_SG_SID] = { .name = "sid", .type = BLOBMSG_TYPE_STRING },
-       [UH_UBUS_SG_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY },
+
+struct list_data {
+       bool verbose;
+       struct blob_buf *buf;
 };
 
-enum {
-       UH_UBUS_SA_SID,
-       UH_UBUS_SA_OBJECTS,
-       __UH_UBUS_SA_MAX,
+enum rpc_error {
+       ERROR_PARSE,
+       ERROR_REQUEST,
+       ERROR_METHOD,
+       ERROR_PARAMS,
+       ERROR_INTERNAL,
+       ERROR_OBJECT,
+       ERROR_SESSION,
+       ERROR_ACCESS,
+       ERROR_TIMEOUT,
+       __ERROR_MAX
 };
-static const struct blobmsg_policy acl_policy[__UH_UBUS_SA_MAX] = {
-       [UH_UBUS_SA_SID] = { .name = "sid", .type = BLOBMSG_TYPE_STRING },
-       [UH_UBUS_SA_OBJECTS] = { .name = "objects", .type = BLOBMSG_TYPE_ARRAY },
+
+static const struct {
+       int code;
+       const char *msg;
+} json_errors[__ERROR_MAX] = {
+       [ERROR_PARSE] = { -32700, "Parse error" },
+       [ERROR_REQUEST] = { -32600, "Invalid request" },
+       [ERROR_METHOD] = { -32601, "Method not found" },
+       [ERROR_PARAMS] = { -32602, "Invalid parameters" },
+       [ERROR_INTERNAL] = { -32603, "Internal error" },
+       [ERROR_OBJECT] = { -32000, "Object not found" },
+       [ERROR_SESSION] = { -32001, "Session not found" },
+       [ERROR_ACCESS] = { -32002, "Access denied" },
+       [ERROR_TIMEOUT] = { -32003, "ubus request timed out" },
 };
 
-/*
- * Keys in the AVL tree contain all pattern characters up to the first wildcard.
- * To look up entries, start with the last entry that has a key less than or
- * equal to the method name, then work backwards as long as the AVL key still
- * matches its counterpart in the object name
- */
-#define uh_foreach_matching_acl_prefix(_acl, _ses, _obj, _func)                        \
-       for (_acl = avl_find_le_element(&(_ses)->acls, _obj, _acl, avl);        \
-            _acl && !strncmp((_acl)->object, _obj, (_acl)->sort_len);          \
-            _acl = avl_is_first(&(ses)->acls, &(_acl)->avl) ? NULL :           \
-                   avl_prev_element((_acl), avl))
+enum cors_hdr {
+       HDR_ORIGIN,
+       HDR_ACCESS_CONTROL_REQUEST_METHOD,
+       HDR_ACCESS_CONTROL_REQUEST_HEADERS,
+       __HDR_MAX
+};
 
-#define uh_foreach_matching_acl(_acl, _ses, _obj, _func)                       \
-       uh_foreach_matching_acl_prefix(_acl, _ses, _obj, _func)                 \
-               if (!fnmatch((_acl)->object, (_obj), FNM_NOESCAPE) &&           \
-                   !fnmatch((_acl)->function, (_func), FNM_NOESCAPE))
+static void __uh_ubus_next_batched_request(struct uloop_timeout *timeout);
 
-static void
-uh_ubus_random(char *dest)
+static void uh_ubus_next_batched_request(struct client *cl)
 {
-       unsigned char buf[16] = { 0 };
-       FILE *f;
-       int i;
-
-       f = fopen("/dev/urandom", "r");
-       if (!f)
-               return;
-
-       fread(buf, 1, sizeof(buf), f);
-       fclose(f);
+       struct dispatch_ubus *du = &cl->dispatch.ubus;
 
-       for (i = 0; i < sizeof(buf); i++)
-               sprintf(dest + (i<<1), "%02x", buf[i]);
+       du->timeout.cb = __uh_ubus_next_batched_request;
+       uloop_timeout_set(&du->timeout, 1);
 }
 
-static void
-uh_ubus_session_dump_data(struct uh_ubus_session *ses, struct blob_buf *b)
+static void uh_ubus_add_cors_headers(struct client *cl)
 {
-       struct uh_ubus_session_data *d;
+       struct blob_attr *tb[__HDR_MAX];
+       static const struct blobmsg_policy hdr_policy[__HDR_MAX] = {
+               [HDR_ORIGIN] = { "origin", BLOBMSG_TYPE_STRING },
+               [HDR_ACCESS_CONTROL_REQUEST_METHOD] = { "access-control-request-method", BLOBMSG_TYPE_STRING },
+               [HDR_ACCESS_CONTROL_REQUEST_HEADERS] = { "access-control-request-headers", BLOBMSG_TYPE_STRING },
+       };
 
-       avl_for_each_element(&ses->data, d, avl) {
-               blobmsg_add_field(b, blobmsg_type(d->attr), blobmsg_name(d->attr),
-                                 blobmsg_data(d->attr), blobmsg_data_len(d->attr));
-       }
-}
+       blobmsg_parse(hdr_policy, __HDR_MAX, tb, blob_data(cl->hdr.head), blob_len(cl->hdr.head));
 
-static void
-uh_ubus_session_dump_acls(struct uh_ubus_session *ses, struct blob_buf *b)
-{
-       struct uh_ubus_session_acl *acl;
-       const char *lastobj = NULL;
-       void *c = NULL;
+       if (!tb[HDR_ORIGIN])
+               return;
 
-       avl_for_each_element(&ses->acls, acl, avl) {
-               if (!lastobj || strcmp(acl->object, lastobj))
-               {
-                       if (c) blobmsg_close_array(b, c);
-                       c = blobmsg_open_array(b, acl->object);
-               }
+       if (tb[HDR_ACCESS_CONTROL_REQUEST_METHOD])
+       {
+               char *hdr = (char *) blobmsg_data(tb[HDR_ACCESS_CONTROL_REQUEST_METHOD]);
 
-               blobmsg_add_string(b, NULL, acl->function);
-               lastobj = acl->object;
+               if (strcmp(hdr, "POST") && strcmp(hdr, "OPTIONS"))
+                       return;
        }
 
-       if (c) blobmsg_close_array(b, c);
+       ustream_printf(cl->us, "Access-Control-Allow-Origin: %s\r\n",
+                      blobmsg_data(tb[HDR_ORIGIN]));
+
+       if (tb[HDR_ACCESS_CONTROL_REQUEST_HEADERS])
+               ustream_printf(cl->us, "Access-Control-Allow-Headers: %s\r\n",
+                              blobmsg_data(tb[HDR_ACCESS_CONTROL_REQUEST_HEADERS]));
+
+       ustream_printf(cl->us, "Access-Control-Allow-Methods: POST, OPTIONS\r\n");
+       ustream_printf(cl->us, "Access-Control-Allow-Credentials: true\r\n");
 }
 
-static void
-uh_ubus_session_dump(struct uh_ubus_session *ses,
-                                        struct ubus_context *ctx,
-                                        struct ubus_request_data *req)
+static void uh_ubus_send_header(struct client *cl)
 {
-       void *c;
-       struct blob_buf b;
-
-       memset(&b, 0, sizeof(b));
-       blob_buf_init(&b, 0);
+       ops->http_header(cl, 200, "OK");
 
-       blobmsg_add_string(&b, "sid", ses->id);
-       blobmsg_add_u32(&b, "timeout", ses->timeout);
+       if (conf.ubus_cors)
+               uh_ubus_add_cors_headers(cl);
 
-       c = blobmsg_open_table(&b, "acls");
-       uh_ubus_session_dump_acls(ses, &b);
-       blobmsg_close_table(&b, c);
+       ustream_printf(cl->us, "Content-Type: application/json\r\n");
 
-       c = blobmsg_open_table(&b, "data");
-       uh_ubus_session_dump_data(ses, &b);
-       blobmsg_close_table(&b, c);
+       if (cl->request.method == UH_HTTP_MSG_OPTIONS)
+               ustream_printf(cl->us, "Content-Length: 0\r\n");
 
-       ubus_send_reply(ctx, req, b.head);
-       blob_buf_free(&b);
+       ustream_printf(cl->us, "\r\n");
 }
 
-static void
-uh_ubus_touch_session(struct uh_ubus_session *ses)
+static void uh_ubus_send_response(struct client *cl)
 {
-       uloop_timeout_set(&ses->t, ses->timeout * 1000);
+       struct dispatch_ubus *du = &cl->dispatch.ubus;
+       const char *sep = "";
+       char *str;
+
+       if (du->array && du->array_idx > 1)
+               sep = ",";
+
+       str = blobmsg_format_json(buf.head, true);
+       ops->chunk_printf(cl, "%s%s", sep, str);
+       free(str);
+
+       du->jsobj_cur = NULL;
+       if (du->array)
+               uh_ubus_next_batched_request(cl);
+       else
+               return ops->request_done(cl);
 }
 
-static void
-uh_ubus_session_destroy(struct uh_ubus_session *ses)
+static void uh_ubus_init_response(struct client *cl)
 {
-       struct uh_ubus_session_acl *acl, *nacl;
-       struct uh_ubus_session_data *data, *ndata;
+       struct dispatch_ubus *du = &cl->dispatch.ubus;
+       struct json_object *obj = du->jsobj_cur;
 
-       uloop_timeout_cancel(&ses->t);
-       avl_remove_all_elements(&ses->acls, acl, avl, nacl)
-               free(acl);
+       blob_buf_init(&buf, 0);
+       blobmsg_add_string(&buf, "jsonrpc", "2.0");
 
-       avl_remove_all_elements(&ses->data, data, avl, ndata)
-               free(data);
+       if (obj)
+               obj = json_object_object_get(obj, "id");
 
-       avl_delete(&sessions, &ses->avl);
-       free(ses);
+       if (obj)
+               blobmsg_add_json_element(&buf, "id", obj);
+       else
+               blobmsg_add_field(&buf, BLOBMSG_TYPE_UNSPEC, "id", NULL, 0);
 }
 
-static void uh_ubus_session_timeout(struct uloop_timeout *t)
+static void uh_ubus_json_error(struct client *cl, enum rpc_error type)
 {
-       struct uh_ubus_session *ses;
+       void *c;
 
-       ses = container_of(t, struct uh_ubus_session, t);
-       uh_ubus_session_destroy(ses);
+       uh_ubus_init_response(cl);
+       c = blobmsg_open_table(&buf, "error");
+       blobmsg_add_u32(&buf, "code", json_errors[type].code);
+       blobmsg_add_string(&buf, "message", json_errors[type].msg);
+       blobmsg_close_table(&buf, c);
+       uh_ubus_send_response(cl);
 }
 
-static struct uh_ubus_session *
-uh_ubus_session_create(int timeout)
+static void
+uh_ubus_request_data_cb(struct ubus_request *req, int type, struct blob_attr *msg)
 {
-       struct uh_ubus_session *ses;
-
-       ses = calloc(1, sizeof(*ses));
-       if (!ses)
-               return NULL;
-
-       ses->timeout  = timeout;
-       ses->avl.key  = ses->id;
-       uh_ubus_random(ses->id);
-
-       avl_insert(&sessions, &ses->avl);
-       avl_init(&ses->acls, avl_strcmp, true, NULL);
-       avl_init(&ses->data, avl_strcmp, false, NULL);
-
-       ses->t.cb = uh_ubus_session_timeout;
-       uh_ubus_touch_session(ses);
+       struct dispatch_ubus *du = container_of(req, struct dispatch_ubus, req);
 
-       return ses;
+       blobmsg_add_field(&du->buf, BLOBMSG_TYPE_TABLE, "", blob_data(msg), blob_len(msg));
 }
 
-static struct uh_ubus_session *
-uh_ubus_session_get(const char *id)
+static void
+uh_ubus_request_cb(struct ubus_request *req, int ret)
 {
-       struct uh_ubus_session *ses;
-
-       ses = avl_find_element(&sessions, id, ses, avl);
-       if (!ses)
-               return NULL;
+       struct dispatch_ubus *du = container_of(req, struct dispatch_ubus, req);
+       struct client *cl = container_of(du, struct client, dispatch.ubus);
+       struct blob_attr *cur;
+       void *r;
+       int rem;
 
-       uh_ubus_touch_session(ses);
-       return ses;
+       uloop_timeout_cancel(&du->timeout);
+       uh_ubus_init_response(cl);
+       r = blobmsg_open_array(&buf, "result");
+       blobmsg_add_u32(&buf, "", ret);
+       blob_for_each_attr(cur, du->buf.head, rem)
+               blobmsg_add_blob(&buf, cur);
+       blobmsg_close_array(&buf, r);
+       uh_ubus_send_response(cl);
 }
 
-static int
-uh_ubus_handle_create(struct ubus_context *ctx, struct ubus_object *obj,
-                                         struct ubus_request_data *req, const char *method,
-                                         struct blob_attr *msg)
+static void
+uh_ubus_timeout_cb(struct uloop_timeout *timeout)
 {
-       struct uh_ubus_session *ses;
-       struct blob_attr *tb;
-       int timeout = conf.script_timeout;
+       struct dispatch_ubus *du = container_of(timeout, struct dispatch_ubus, timeout);
+       struct client *cl = container_of(du, struct client, dispatch.ubus);
 
-       blobmsg_parse(&new_policy, 1, &tb, blob_data(msg), blob_len(msg));
-       if (tb)
-               timeout = blobmsg_get_u32(tb);
+       ubus_abort_request(ctx, &du->req);
+       uh_ubus_json_error(cl, ERROR_TIMEOUT);
+}
 
-       ses = uh_ubus_session_create(timeout);
-       if (ses)
-               uh_ubus_session_dump(ses, ctx, req);
+static void uh_ubus_close_fds(struct client *cl)
+{
+       if (ctx->sock.fd < 0)
+               return;
 
-       return 0;
+       close(ctx->sock.fd);
+       ctx->sock.fd = -1;
 }
 
-static int
-uh_ubus_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
-                                       struct ubus_request_data *req, const char *method,
-                                       struct blob_attr *msg)
+static void uh_ubus_request_free(struct client *cl)
 {
-       struct uh_ubus_session *ses;
-       struct blob_attr *tb;
-
-       blobmsg_parse(&sid_policy, 1, &tb, blob_data(msg), blob_len(msg));
+       struct dispatch_ubus *du = &cl->dispatch.ubus;
 
-       if (!tb) {
-               avl_for_each_element(&sessions, ses, avl)
-                       uh_ubus_session_dump(ses, ctx, req);
-               return 0;
-       }
+       blob_buf_free(&du->buf);
+       uloop_timeout_cancel(&du->timeout);
 
-       ses = uh_ubus_session_get(blobmsg_data(tb));
-       if (!ses)
-               return UBUS_STATUS_NOT_FOUND;
+       if (du->jsobj)
+               json_object_put(du->jsobj);
 
-       uh_ubus_session_dump(ses, ctx, req);
+       if (du->jstok)
+               json_tokener_free(du->jstok);
 
-       return 0;
+       if (du->req_pending)
+               ubus_abort_request(ctx, &du->req);
 }
 
-static int
-uh_id_len(const char *str)
+static void uh_ubus_single_error(struct client *cl, enum rpc_error type)
 {
-       return strcspn(str, "*?[");
+       uh_ubus_send_header(cl);
+       uh_ubus_json_error(cl, type);
+       ops->request_done(cl);
 }
 
-static int
-uh_ubus_session_grant(struct uh_ubus_session *ses, struct ubus_context *ctx,
-                     const char *object, const char *function)
+static void uh_ubus_send_request(struct client *cl, json_object *obj, const char *sid, struct blob_attr *args)
 {
-       struct uh_ubus_session_acl *acl;
-       char *new_obj, *new_func, *new_id;
-       int id_len;
-
-       if (!object || !function)
-               return UBUS_STATUS_INVALID_ARGUMENT;
-
-       uh_foreach_matching_acl_prefix(acl, ses, object, function) {
-               if (!strcmp(acl->object, object) &&
-                   !strcmp(acl->function, function))
-                       return 0;
+       struct dispatch *d = &cl->dispatch;
+       struct dispatch_ubus *du = &d->ubus;
+       struct blob_attr *cur;
+       static struct blob_buf req;
+       int ret, rem;
+
+       blob_buf_init(&req, 0);
+       blobmsg_for_each_attr(cur, args, rem) {
+               if (!strcmp(blobmsg_name(cur), "ubus_rpc_session"))
+                       return uh_ubus_json_error(cl, ERROR_PARAMS);
+               blobmsg_add_blob(&req, cur);
        }
 
-       id_len = uh_id_len(object);
-       acl = calloc_a(sizeof(*acl),
-               &new_obj, strlen(object) + 1,
-               &new_func, strlen(function) + 1,
-               &new_id, id_len + 1);
-
-       if (!acl)
-               return UBUS_STATUS_UNKNOWN_ERROR;
+       blobmsg_add_string(&req, "ubus_rpc_session", sid);
 
-       acl->object = strcpy(new_obj, object);
-       acl->function = strcpy(new_func, function);
-       acl->avl.key = strncpy(new_id, object, id_len);
-       avl_insert(&ses->acls, &acl->avl);
-
-       return 0;
-}
+       blob_buf_init(&du->buf, 0);
+       memset(&du->req, 0, sizeof(du->req));
+       ret = ubus_invoke_async(ctx, du->obj, du->func, req.head, &du->req);
+       if (ret)
+               return uh_ubus_json_error(cl, ERROR_INTERNAL);
 
-static int
-uh_ubus_session_revoke(struct uh_ubus_session *ses, struct ubus_context *ctx,
-                      const char *object, const char *function)
-{
-       struct uh_ubus_session_acl *acl, *next;
-       int id_len;
-       char *id;
-
-       if (!object && !function) {
-               avl_remove_all_elements(&ses->acls, acl, avl, next)
-                       free(acl);
-               return 0;
-       }
+       du->req.data_cb = uh_ubus_request_data_cb;
+       du->req.complete_cb = uh_ubus_request_cb;
+       ubus_complete_request_async(ctx, &du->req);
 
-       id_len = uh_id_len(object);
-       id = alloca(id_len + 1);
-       strncpy(id, object, id_len);
-       id[id_len] = 0;
-
-       acl = avl_find_element(&ses->acls, id, acl, avl);
-       while (acl) {
-               if (!avl_is_last(&ses->acls, &acl->avl))
-                       next = avl_next_element(acl, avl);
-               else
-                       next = NULL;
-
-               if (strcmp(id, acl->avl.key) != 0)
-                       break;
-
-               if (!strcmp(acl->object, object) &&
-                   !strcmp(acl->function, function)) {
-                       avl_delete(&ses->acls, &acl->avl);
-                       free(acl);
-               }
-               acl = next;
-       }
+       du->timeout.cb = uh_ubus_timeout_cb;
+       uloop_timeout_set(&du->timeout, conf.script_timeout * 1000);
 
-       return 0;
+       du->req_pending = true;
 }
 
-
-static int
-uh_ubus_handle_acl(struct ubus_context *ctx, struct ubus_object *obj,
-                  struct ubus_request_data *req, const char *method,
-                  struct blob_attr *msg)
+static void uh_ubus_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
 {
-       struct uh_ubus_session *ses;
-       struct blob_attr *tb[__UH_UBUS_SA_MAX];
-       struct blob_attr *attr, *sattr;
-       const char *object, *function;
-       int rem1, rem2;
-
-       int (*cb)(struct uh_ubus_session *ses, struct ubus_context *ctx,
-                 const char *object, const char *function);
-
-       blobmsg_parse(acl_policy, __UH_UBUS_SA_MAX, tb, blob_data(msg), blob_len(msg));
-
-       if (!tb[UH_UBUS_SA_SID])
-               return UBUS_STATUS_INVALID_ARGUMENT;
-
-       ses = uh_ubus_session_get(blobmsg_data(tb[UH_UBUS_SA_SID]));
-       if (!ses)
-               return UBUS_STATUS_NOT_FOUND;
+       struct blob_attr *sig, *attr;
+       struct list_data *data = priv;
+       int rem, rem2;
+       void *t, *o;
 
-       if (!strcmp(method, "grant"))
-               cb = uh_ubus_session_grant;
-       else
-               cb = uh_ubus_session_revoke;
-
-       if (!tb[UH_UBUS_SA_OBJECTS])
-               return cb(ses, ctx, NULL, NULL);
-
-       blobmsg_for_each_attr(attr, tb[UH_UBUS_SA_OBJECTS], rem1) {
-               if (blob_id(attr) != BLOBMSG_TYPE_ARRAY)
-                       continue;
+       if (!data->verbose) {
+               blobmsg_add_string(data->buf, NULL, obj->path);
+               return;
+       }
 
-               object = NULL;
-               function = NULL;
+       if (!obj->signature)
+               return;
 
-               blobmsg_for_each_attr(sattr, attr, rem2) {
-                       if (blob_id(sattr) != BLOBMSG_TYPE_STRING)
+       o = blobmsg_open_table(data->buf, obj->path);
+       blob_for_each_attr(sig, obj->signature, rem) {
+               t = blobmsg_open_table(data->buf, blobmsg_name(sig));
+               rem2 = blobmsg_data_len(sig);
+               __blob_for_each_attr(attr, blobmsg_data(sig), rem2) {
+                       if (blob_id(attr) != BLOBMSG_TYPE_INT32)
                                continue;
 
-                       if (!object)
-                               object = blobmsg_data(sattr);
-                       else if (!function)
-                               function = blobmsg_data(sattr);
-                       else
+                       switch (blobmsg_get_u32(attr)) {
+                       case BLOBMSG_TYPE_INT8:
+                               blobmsg_add_string(data->buf, blobmsg_name(attr), "boolean");
+                               break;
+                       case BLOBMSG_TYPE_INT32:
+                               blobmsg_add_string(data->buf, blobmsg_name(attr), "number");
+                               break;
+                       case BLOBMSG_TYPE_STRING:
+                               blobmsg_add_string(data->buf, blobmsg_name(attr), "string");
                                break;
+                       case BLOBMSG_TYPE_ARRAY:
+                               blobmsg_add_string(data->buf, blobmsg_name(attr), "array");
+                               break;
+                       case BLOBMSG_TYPE_TABLE:
+                               blobmsg_add_string(data->buf, blobmsg_name(attr), "object");
+                               break;
+                       default:
+                               blobmsg_add_string(data->buf, blobmsg_name(attr), "unknown");
+                               break;
+                       }
                }
-
-               if (object && function)
-                       cb(ses, ctx, object, function);
+               blobmsg_close_table(data->buf, t);
        }
-
-       return 0;
+       blobmsg_close_table(data->buf, o);
 }
 
-static int
-uh_ubus_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
-                                  struct ubus_request_data *req, const char *method,
-                                  struct blob_attr *msg)
+static void uh_ubus_send_list(struct client *cl, json_object *obj, struct blob_attr *params)
 {
-       struct uh_ubus_session *ses;
-       struct uh_ubus_session_data *data;
-       struct blob_attr *tb[__UH_UBUS_SA_MAX];
-       struct blob_attr *attr;
+       struct blob_attr *cur, *dup;
+       struct list_data data = { .buf = &cl->dispatch.ubus.buf, .verbose = false };
+       void *r;
        int rem;
 
-       blobmsg_parse(set_policy, __UH_UBUS_SS_MAX, tb, blob_data(msg), blob_len(msg));
+       blob_buf_init(data.buf, 0);
 
-       if (!tb[UH_UBUS_SS_SID] || !tb[UH_UBUS_SS_VALUES])
-               return UBUS_STATUS_INVALID_ARGUMENT;
+       uh_client_ref(cl);
 
-       ses = uh_ubus_session_get(blobmsg_data(tb[UH_UBUS_SS_SID]));
-       if (!ses)
-               return UBUS_STATUS_NOT_FOUND;
-
-       blobmsg_for_each_attr(attr, tb[UH_UBUS_SS_VALUES], rem) {
-               if (!blobmsg_name(attr)[0])
-                       continue;
-
-               data = avl_find_element(&ses->data, blobmsg_name(attr), data, avl);
-               if (data) {
-                       avl_delete(&ses->data, &data->avl);
-                       free(data);
+       if (!params || blob_id(params) != BLOBMSG_TYPE_ARRAY) {
+               r = blobmsg_open_array(data.buf, "result");
+               ubus_lookup(ctx, NULL, uh_ubus_list_cb, &data);
+               blobmsg_close_array(data.buf, r);
+       }
+       else {
+               r = blobmsg_open_table(data.buf, "result");
+               dup = blob_memdup(params);
+               if (dup)
+               {
+                       rem = blobmsg_data_len(dup);
+                       data.verbose = true;
+                       __blob_for_each_attr(cur, blobmsg_data(dup), rem)
+                               ubus_lookup(ctx, blobmsg_data(cur), uh_ubus_list_cb, &data);
+                       free(dup);
                }
-
-               data = calloc(1, sizeof(*data) + blob_pad_len(attr));
-               if (!data)
-                       break;
-
-               memcpy(data->attr, attr, blob_pad_len(attr));
-               data->avl.key = blobmsg_name(data->attr);
-               avl_insert(&ses->data, &data->avl);
+               blobmsg_close_table(data.buf, r);
        }
 
-       return 0;
+       uh_client_unref(cl);
+
+       uh_ubus_init_response(cl);
+       blobmsg_add_blob(&buf, blob_data(data.buf->head));
+       uh_ubus_send_response(cl);
 }
 
-static int
-uh_ubus_handle_get(struct ubus_context *ctx, struct ubus_object *obj,
-                                  struct ubus_request_data *req, const char *method,
-                                  struct blob_attr *msg)
+static bool parse_json_rpc(struct rpc_data *d, struct blob_attr *data)
 {
-       struct uh_ubus_session *ses;
-       struct uh_ubus_session_data *data;
-       struct blob_attr *tb[__UH_UBUS_SA_MAX];
-       struct blob_attr *attr;
-       struct blob_buf b;
-       void *c;
-       int rem;
-
-       blobmsg_parse(get_policy, __UH_UBUS_SG_MAX, tb, blob_data(msg), blob_len(msg));
-
-       if (!tb[UH_UBUS_SG_SID])
-               return UBUS_STATUS_INVALID_ARGUMENT;
-
-       ses = uh_ubus_session_get(blobmsg_data(tb[UH_UBUS_SG_SID]));
-       if (!ses)
-               return UBUS_STATUS_NOT_FOUND;
-
-       memset(&b, 0, sizeof(b));
-       blob_buf_init(&b, 0);
-       c = blobmsg_open_table(&b, "values");
-
-       if (!tb[UH_UBUS_SG_KEYS]) {
-               uh_ubus_session_dump_data(ses, &b);
-               return 0;
-       }
+       const struct blobmsg_policy data_policy[] = {
+               { .type = BLOBMSG_TYPE_STRING },
+               { .type = BLOBMSG_TYPE_STRING },
+               { .type = BLOBMSG_TYPE_STRING },
+               { .type = BLOBMSG_TYPE_TABLE },
+       };
+       struct blob_attr *tb[__RPC_MAX];
+       struct blob_attr *tb2[4];
+       struct blob_attr *cur;
 
-       blobmsg_for_each_attr(attr, tb[UH_UBUS_SG_KEYS], rem) {
-               if (blob_id(attr) != BLOBMSG_TYPE_STRING)
-                       continue;
+       blobmsg_parse(rpc_policy, __RPC_MAX, tb, blob_data(data), blob_len(data));
 
-               data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl);
-               if (!data)
-                       continue;
+       cur = tb[RPC_JSONRPC];
+       if (!cur || strcmp(blobmsg_data(cur), "2.0") != 0)
+               return false;
 
-               blobmsg_add_field(&b, blobmsg_type(data->attr),
-                                 blobmsg_name(data->attr),
-                                 blobmsg_data(data->attr),
-                                 blobmsg_data_len(data->attr));
-       }
+       cur = tb[RPC_METHOD];
+       if (!cur)
+               return false;
 
-       blobmsg_close_table(&b, c);
-       ubus_send_reply(ctx, req, b.head);
-       blob_buf_free(&b);
+       d->id = tb[RPC_ID];
+       d->method = blobmsg_data(cur);
 
-       return 0;
-}
+       cur = tb[RPC_PARAMS];
+       if (!cur)
+               return true;
 
-static int
-uh_ubus_handle_unset(struct ubus_context *ctx, struct ubus_object *obj,
-                                    struct ubus_request_data *req, const char *method,
-                                    struct blob_attr *msg)
-{
-       struct uh_ubus_session *ses;
-       struct uh_ubus_session_data *data, *ndata;
-       struct blob_attr *tb[__UH_UBUS_SA_MAX];
-       struct blob_attr *attr;
-       int rem;
+       d->params = blob_memdup(cur);
+       if (!d->params)
+               return false;
 
-       blobmsg_parse(get_policy, __UH_UBUS_SG_MAX, tb, blob_data(msg), blob_len(msg));
+       blobmsg_parse_array(data_policy, ARRAY_SIZE(data_policy), tb2,
+                           blobmsg_data(d->params), blobmsg_data_len(d->params));
 
-       if (!tb[UH_UBUS_SG_SID])
-               return UBUS_STATUS_INVALID_ARGUMENT;
+       if (tb2[0])
+               d->sid = blobmsg_data(tb2[0]);
 
-       ses = uh_ubus_session_get(blobmsg_data(tb[UH_UBUS_SG_SID]));
-       if (!ses)
-               return UBUS_STATUS_NOT_FOUND;
+       if (conf.ubus_noauth && (!d->sid || !*d->sid))
+               d->sid = UH_UBUS_DEFAULT_SID;
 
-       if (!tb[UH_UBUS_SG_KEYS]) {
-               avl_remove_all_elements(&ses->data, data, avl, ndata)
-                       free(data);
-               return 0;
-       }
-
-       blobmsg_for_each_attr(attr, tb[UH_UBUS_SG_KEYS], rem) {
-               if (blob_id(attr) != BLOBMSG_TYPE_STRING)
-                       continue;
+       if (tb2[1])
+               d->object = blobmsg_data(tb2[1]);
 
-               data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl);
-               if (!data)
-                       continue;
+       if (tb2[2])
+               d->function = blobmsg_data(tb2[2]);
 
-               avl_delete(&ses->data, &data->avl);
-               free(data);
-       }
+       d->data = tb2[3];
 
-       return 0;
+       return true;
 }
 
-static int
-uh_ubus_handle_destroy(struct ubus_context *ctx, struct ubus_object *obj,
-                                          struct ubus_request_data *req, const char *method,
-                                          struct blob_attr *msg)
+static void uh_ubus_init_batch(struct client *cl)
 {
-       struct uh_ubus_session *ses;
-       struct blob_attr *tb;
-
-       blobmsg_parse(&sid_policy, 1, &tb, blob_data(msg), blob_len(msg));
-
-       if (!tb)
-               return UBUS_STATUS_INVALID_ARGUMENT;
-
-       ses = uh_ubus_session_get(blobmsg_data(tb));
-       if (!ses)
-               return UBUS_STATUS_NOT_FOUND;
-
-       uh_ubus_session_destroy(ses);
+       struct dispatch_ubus *du = &cl->dispatch.ubus;
 
-       return 0;
+       du->array = true;
+       uh_ubus_send_header(cl);
+       ops->chunk_printf(cl, "[");
 }
 
-static char *split_str(char *str)
+static void uh_ubus_complete_batch(struct client *cl)
 {
-       if (str)
-               str = strchr(str, '/');
-
-       while (str && *str == '/') {
-               *str = 0;
-               str++;
-       }
-       return str;
+       ops->chunk_printf(cl, "]");
+       ops->request_done(cl);
 }
 
-static bool
-uh_ubus_request_parse_url(struct client *cl, char *url, char **sid, char **obj, char **fun)
+static void uh_ubus_allowed_cb(struct ubus_request *req, int type, struct blob_attr *msg)
 {
-       url += strlen(conf.ubus_prefix);
-       while (url && *url == '/')
-               url++;
+       struct blob_attr *tb[__SES_MAX];
+       bool *allow = (bool *)req->priv;
 
-       *sid = url;
-
-       url = split_str(url);
-       *obj = url;
+       if (!msg)
+               return;
 
-       url = split_str(url);
-       *fun = url;
+       blobmsg_parse(ses_policy, __SES_MAX, tb, blob_data(msg), blob_len(msg));
 
-       return *sid && *obj && *fun;
+       if (tb[SES_ACCESS])
+               *allow = blobmsg_get_bool(tb[SES_ACCESS]);
 }
 
-static void
-uh_ubus_request_data_cb(struct ubus_request *req, int type, struct blob_attr *msg)
+static bool uh_ubus_allowed(const char *sid, const char *obj, const char *fun)
 {
-       struct dispatch_ubus *du = container_of(req, struct dispatch_ubus, req);
-       struct client *cl = container_of(du, struct client, dispatch.ubus);
-       char *str;
+       uint32_t id;
+       bool allow = false;
+       static struct blob_buf req;
 
-       if (!du->header_sent) {
-               ops->http_header(cl, 200, "OK");
-               ustream_printf(cl->us, "Content-Type: application/json\r\n\r\n");
-               du->header_sent = true;
-       }
+       if (ubus_lookup_id(ctx, "session", &id))
+               return false;
 
-       str = blobmsg_format_json_indent(msg, true, 0);
-       ops->chunk_write(cl, str, strlen(str));
-       free(str);
-}
+       blob_buf_init(&req, 0);
+       blobmsg_add_string(&req, "ubus_rpc_session", sid);
+       blobmsg_add_string(&req, "object", obj);
+       blobmsg_add_string(&req, "function", fun);
 
-static void
-uh_ubus_request_cb(struct ubus_request *req, int ret)
-{
-       struct dispatch_ubus *du = container_of(req, struct dispatch_ubus, req);
-       struct client *cl = container_of(du, struct client, dispatch.ubus);
+       ubus_invoke(ctx, id, "access", req.head, uh_ubus_allowed_cb, &allow, conf.script_timeout * 500);
 
-       if (!du->header_sent)
-               return ops->client_error(cl, 204, "No content", "Function did not return data");
-
-       ops->request_done(cl);
+       return allow;
 }
 
-static void uh_ubus_close_fds(struct client *cl)
+static void uh_ubus_handle_request_object(struct client *cl, struct json_object *obj)
 {
-       if (ctx->sock.fd < 0)
-               return;
+       struct dispatch_ubus *du = &cl->dispatch.ubus;
+       struct rpc_data data = {};
+       enum rpc_error err = ERROR_PARSE;
 
-       close(ctx->sock.fd);
-       ctx->sock.fd = -1;
-}
+       uh_client_ref(cl);
 
-static void uh_ubus_request_free(struct client *cl)
-{
-       struct dispatch_ubus *du = &cl->dispatch.ubus;
+       if (json_object_get_type(obj) != json_type_object)
+               goto error;
 
-       if (du->jsobj)
-               json_object_put(du->jsobj);
+       du->jsobj_cur = obj;
+       blob_buf_init(&buf, 0);
+       if (!blobmsg_add_object(&buf, obj))
+               goto error;
 
-       if (du->jstok)
-               json_tokener_free(du->jstok);
+       if (!parse_json_rpc(&data, buf.head))
+               goto error;
 
-       if (du->req_pending)
-               ubus_abort_request(ctx, &du->req);
-}
+       if (!strcmp(data.method, "call")) {
+               if (!data.sid || !data.object || !data.function || !data.data)
+                       goto error;
 
-static void uh_ubus_json_error(struct client *cl)
-{
-       ops->client_error(cl, 400, "Bad Request", "Invalid JSON data");
-}
+               du->func = data.function;
+               if (ubus_lookup_id(ctx, data.object, &du->obj)) {
+                       err = ERROR_OBJECT;
+                       goto error;
+               }
 
-static void uh_ubus_send_request(struct client *cl, json_object *obj)
-{
-       struct dispatch *d = &cl->dispatch;
-       struct dispatch_ubus *du = &d->ubus;
-       int ret;
+               if (!conf.ubus_noauth && !uh_ubus_allowed(data.sid, data.object, data.function)) {
+                       err = ERROR_ACCESS;
+                       goto error;
+               }
 
-       blob_buf_init(&buf, 0);
+               uh_ubus_send_request(cl, obj, data.sid, data.data);
+               goto out;
+       }
+       else if (!strcmp(data.method, "list")) {
+               uh_ubus_send_list(cl, obj, data.params);
+               goto out;
+       }
+       else {
+               err = ERROR_METHOD;
+               goto error;
+       }
 
-       if (obj && !blobmsg_add_object(&buf, obj))
-               return uh_ubus_json_error(cl);
+error:
+       uh_ubus_json_error(cl, err);
+out:
+       if (data.params)
+               free(data.params);
 
-       ret = ubus_invoke_async(ctx, du->obj, du->func, buf.head, &du->req);
-       if (ret)
-               return ops->client_error(cl, 500, "Internal Error",
-                       "Error sending ubus request: %s", ubus_strerror(ret));
+       uh_client_unref(cl);
+}
 
-       du->req.data_cb = uh_ubus_request_data_cb;
-       du->req.complete_cb = uh_ubus_request_cb;
-       ubus_complete_request_async(ctx, &du->req);
+static void __uh_ubus_next_batched_request(struct uloop_timeout *timeout)
+{
+       struct dispatch_ubus *du = container_of(timeout, struct dispatch_ubus, timeout);
+       struct client *cl = container_of(du, struct client, dispatch.ubus);
+       struct json_object *obj = du->jsobj;
+       int len;
 
-       du->req_pending = true;
+       len = json_object_array_length(obj);
+       if (du->array_idx >= len)
+               return uh_ubus_complete_batch(cl);
+
+       obj = json_object_array_get_idx(obj, du->array_idx++);
+       uh_ubus_handle_request_object(cl, obj);
 }
 
 static void uh_ubus_data_done(struct client *cl)
@@ -692,79 +583,61 @@ static void uh_ubus_data_done(struct client *cl)
        struct dispatch_ubus *du = &cl->dispatch.ubus;
        struct json_object *obj = du->jsobj;
 
-       if (!obj || json_object_get_type(obj) != json_type_object)
-               return uh_ubus_json_error(cl);
-
-       uh_ubus_send_request(cl, obj);
+       switch (obj ? json_object_get_type(obj) : json_type_null) {
+       case json_type_object:
+               uh_ubus_send_header(cl);
+               return uh_ubus_handle_request_object(cl, obj);
+       case json_type_array:
+               uh_ubus_init_batch(cl);
+               return uh_ubus_next_batched_request(cl);
+       default:
+               return uh_ubus_single_error(cl, ERROR_PARSE);
+       }
 }
 
 static int uh_ubus_data_send(struct client *cl, const char *data, int len)
 {
        struct dispatch_ubus *du = &cl->dispatch.ubus;
 
-       if (du->jsobj) {
-               uh_ubus_json_error(cl);
-               return 0;
-       }
+       if (du->jsobj || !du->jstok)
+               goto error;
 
        du->post_len += len;
-       if (du->post_len > UH_UBUS_MAX_POST_SIZE) {
-               ops->client_error(cl, 413, "Too Large", "Message too big");
-               return 0;
-       }
+       if (du->post_len > UH_UBUS_MAX_POST_SIZE)
+               goto error;
 
        du->jsobj = json_tokener_parse_ex(du->jstok, data, len);
        return len;
-}
 
-static void uh_ubus_defer_post(struct client *cl)
-{
-       struct dispatch *d = &cl->dispatch;
-
-       d->ubus.jstok = json_tokener_new();
-       if (d->ubus.jstok)
-               return ops->client_error(cl, 500, "Internal Error", "Internal Error");
-
-       d->data_send = uh_ubus_data_send;
-       d->data_done = uh_ubus_data_done;
+error:
+       uh_ubus_single_error(cl, ERROR_PARSE);
+       return 0;
 }
 
 static void uh_ubus_handle_request(struct client *cl, char *url, struct path_info *pi)
 {
-       struct uh_ubus_session_acl *acl;
-       struct uh_ubus_session *ses;
        struct dispatch *d = &cl->dispatch;
-       char *sid, *obj, *fun;
-       bool access = false;
 
        blob_buf_init(&buf, 0);
 
-       if (!uh_ubus_request_parse_url(cl, url, &sid, &obj, &fun))
-               return ops->client_error(cl, 400, "Bad Request", "Invalid Request");
-
-       ses = uh_ubus_session_get(sid);
-       if (!ses)
-               return ops->client_error(cl, 404, "Not Found", "No such session %s", sid);
-
-       uh_foreach_matching_acl(acl, ses, obj, fun) {
-               access = true;
+       switch (cl->request.method)
+       {
+       case UH_HTTP_MSG_POST:
+               d->data_send = uh_ubus_data_send;
+               d->data_done = uh_ubus_data_done;
+               d->close_fds = uh_ubus_close_fds;
+               d->free = uh_ubus_request_free;
+               d->ubus.jstok = json_tokener_new();
                break;
-       }
-
-       if (!access)
-               return ops->client_error(cl, 403, "Denied", "Access to object denied");
 
-       if (ubus_lookup_id(ctx, obj, &d->ubus.obj))
-               return ops->client_error(cl, 500, "Not Found", "No such object");
-
-       d->close_fds = uh_ubus_close_fds;
-       d->free = uh_ubus_request_free;
-       d->ubus.func = fun;
+       case UH_HTTP_MSG_OPTIONS:
+               uh_ubus_send_header(cl);
+               ops->request_done(cl);
+               break;
 
-       if (cl->request.method == UH_HTTP_MSG_POST)
-               uh_ubus_defer_post(cl);
-       else
-               uh_ubus_send_request(cl, NULL);
+       default:
+               ops->client_error(cl, 400, "Bad Request", "Invalid Request");
+       }
 }
 
 static bool
@@ -781,43 +654,12 @@ uh_ubus_init(void)
                .handle_request = uh_ubus_handle_request,
        };
 
-       static const struct ubus_method session_methods[] = {
-               UBUS_METHOD("create",  uh_ubus_handle_create,  &new_policy),
-               UBUS_METHOD("list",    uh_ubus_handle_list,    &sid_policy),
-               UBUS_METHOD("grant",   uh_ubus_handle_acl,     acl_policy),
-               UBUS_METHOD("revoke",  uh_ubus_handle_acl,     acl_policy),
-               UBUS_METHOD("set",     uh_ubus_handle_set,     set_policy),
-               UBUS_METHOD("get",     uh_ubus_handle_get,     get_policy),
-               UBUS_METHOD("unset",   uh_ubus_handle_unset,   get_policy),
-               UBUS_METHOD("destroy", uh_ubus_handle_destroy, &sid_policy),
-       };
-
-       static struct ubus_object_type session_type =
-               UBUS_OBJECT_TYPE("uhttpd", session_methods);
-
-       static struct ubus_object obj = {
-               .name = "session",
-               .type = &session_type,
-               .methods = session_methods,
-               .n_methods = ARRAY_SIZE(session_methods),
-       };
-
-       int ret;
-
        ctx = ubus_connect(conf.ubus_socket);
        if (!ctx) {
                fprintf(stderr, "Unable to connect to ubus socket\n");
                exit(1);
        }
 
-       ret = ubus_add_object(ctx, &obj);
-       if (ret) {
-               fprintf(stderr, "Unable to publish ubus object: %s\n",
-                               ubus_strerror(ret));
-               exit(1);
-       }
-
-       avl_init(&sessions, avl_strcmp, false, NULL);
        ops->dispatch_add(&ubus_dispatch);
 
        uloop_done();