uci: add state call to retrieve uci state variables
[project/rpcd.git] / uci.c
diff --git a/uci.c b/uci.c
index 81a6b2f..a49c8d0 100644 (file)
--- a/uci.c
+++ b/uci.c
@@ -1,5 +1,5 @@
 /*
- * luci-rpcd - LuCI UBUS RPC server
+ * rpcd - UBUS RPC server
  *
  *   Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
  *
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include "uci.h"
+#include <libgen.h>
+#include <glob.h>
+
+#include <libubox/blobmsg.h>
+#include <libubox/blobmsg_json.h>
+
+#include <rpcd/uci.h>
+#include <rpcd/session.h>
 
 static struct blob_buf buf;
 static struct uci_context *cursor;
+static struct uloop_timeout apply_timer;
+static struct ubus_context *apply_ctx;
+static bool apply_running;
 
 enum {
        RPC_G_CONFIG,
@@ -27,6 +37,7 @@ enum {
        RPC_G_OPTION,
        RPC_G_TYPE,
        RPC_G_MATCH,
+       RPC_G_SESSION,
        __RPC_G_MAX,
 };
 
@@ -36,6 +47,8 @@ static const struct blobmsg_policy rpc_uci_get_policy[__RPC_G_MAX] = {
        [RPC_G_OPTION]  = { .name = "option",  .type = BLOBMSG_TYPE_STRING },
        [RPC_G_TYPE]    = { .name = "type",    .type = BLOBMSG_TYPE_STRING },
        [RPC_G_MATCH]   = { .name = "match",   .type = BLOBMSG_TYPE_TABLE  },
+       [RPC_G_SESSION] = { .name = "ubus_rpc_session",
+                                              .type = BLOBMSG_TYPE_STRING },
 };
 
 enum {
@@ -43,6 +56,7 @@ enum {
        RPC_A_TYPE,
        RPC_A_NAME,
        RPC_A_VALUES,
+       RPC_A_SESSION,
        __RPC_A_MAX,
 };
 
@@ -51,6 +65,8 @@ static const struct blobmsg_policy rpc_uci_add_policy[__RPC_A_MAX] = {
        [RPC_A_TYPE]    = { .name = "type",    .type = BLOBMSG_TYPE_STRING },
        [RPC_A_NAME]    = { .name = "name",    .type = BLOBMSG_TYPE_STRING },
        [RPC_A_VALUES]  = { .name = "values",  .type = BLOBMSG_TYPE_TABLE  },
+       [RPC_A_SESSION] = { .name = "ubus_rpc_session",
+                                              .type = BLOBMSG_TYPE_STRING },
 };
 
 enum {
@@ -59,6 +75,7 @@ enum {
        RPC_S_TYPE,
        RPC_S_MATCH,
        RPC_S_VALUES,
+       RPC_S_SESSION,
        __RPC_S_MAX,
 };
 
@@ -68,6 +85,30 @@ static const struct blobmsg_policy rpc_uci_set_policy[__RPC_S_MAX] = {
        [RPC_S_TYPE]    = { .name = "type",     .type = BLOBMSG_TYPE_STRING },
        [RPC_S_MATCH]   = { .name = "match",    .type = BLOBMSG_TYPE_TABLE  },
        [RPC_S_VALUES]  = { .name = "values",   .type = BLOBMSG_TYPE_TABLE  },
+       [RPC_S_SESSION] = { .name = "ubus_rpc_session",
+                                               .type = BLOBMSG_TYPE_STRING },
+};
+
+enum {
+       RPC_D_CONFIG,
+       RPC_D_SECTION,
+       RPC_D_TYPE,
+       RPC_D_MATCH,
+       RPC_D_OPTION,
+       RPC_D_OPTIONS,
+       RPC_D_SESSION,
+       __RPC_D_MAX,
+};
+
+static const struct blobmsg_policy rpc_uci_delete_policy[__RPC_D_MAX] = {
+       [RPC_D_CONFIG]  = { .name = "config",   .type = BLOBMSG_TYPE_STRING },
+       [RPC_D_SECTION] = { .name = "section",  .type = BLOBMSG_TYPE_STRING },
+       [RPC_D_TYPE]    = { .name = "type",     .type = BLOBMSG_TYPE_STRING },
+       [RPC_D_MATCH]   = { .name = "match",    .type = BLOBMSG_TYPE_TABLE  },
+       [RPC_D_OPTION]  = { .name = "option",   .type = BLOBMSG_TYPE_STRING },
+       [RPC_D_OPTIONS] = { .name = "options",  .type = BLOBMSG_TYPE_ARRAY  },
+       [RPC_D_SESSION] = { .name = "ubus_rpc_session",
+                                               .type = BLOBMSG_TYPE_STRING },
 };
 
 enum {
@@ -75,6 +116,7 @@ enum {
        RPC_R_SECTION,
        RPC_R_OPTION,
        RPC_R_NAME,
+       RPC_R_SESSION,
        __RPC_R_MAX,
 };
 
@@ -83,26 +125,58 @@ static const struct blobmsg_policy rpc_uci_rename_policy[__RPC_R_MAX] = {
        [RPC_R_SECTION] = { .name = "section",  .type = BLOBMSG_TYPE_STRING },
        [RPC_R_OPTION]  = { .name = "option",   .type = BLOBMSG_TYPE_STRING },
        [RPC_R_NAME]    = { .name = "name",     .type = BLOBMSG_TYPE_STRING },
+       [RPC_R_SESSION] = { .name = "ubus_rpc_session",
+                                               .type = BLOBMSG_TYPE_STRING },
 };
 
 enum {
        RPC_O_CONFIG,
        RPC_O_SECTIONS,
+       RPC_O_SESSION,
        __RPC_O_MAX,
 };
 
 static const struct blobmsg_policy rpc_uci_order_policy[__RPC_O_MAX] = {
        [RPC_O_CONFIG]   = { .name = "config",   .type = BLOBMSG_TYPE_STRING },
        [RPC_O_SECTIONS] = { .name = "sections", .type = BLOBMSG_TYPE_ARRAY  },
+       [RPC_O_SESSION]  = { .name = "ubus_rpc_session",
+                                                .type = BLOBMSG_TYPE_STRING },
 };
 
 enum {
        RPC_C_CONFIG,
+       RPC_C_SESSION,
        __RPC_C_MAX,
 };
 
 static const struct blobmsg_policy rpc_uci_config_policy[__RPC_C_MAX] = {
        [RPC_C_CONFIG]   = { .name = "config",  .type = BLOBMSG_TYPE_STRING },
+       [RPC_C_SESSION]  = { .name = "ubus_rpc_session",
+                                               .type = BLOBMSG_TYPE_STRING },
+};
+
+enum {
+       RPC_T_COMMIT,
+       RPC_T_TIMEOUT,
+       RPC_T_SESSION,
+       __RPC_T_MAX,
+};
+
+static const struct blobmsg_policy rpc_uci_apply_policy[__RPC_T_MAX] = {
+       [RPC_T_COMMIT]   = { .name = "commit",  .type = BLOBMSG_TYPE_BOOL },
+       [RPC_T_TIMEOUT]  = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
+       [RPC_T_SESSION]  = { .name = "ubus_rpc_session",
+                                               .type = BLOBMSG_TYPE_STRING },
+};
+
+enum {
+       RPC_B_SESSION,
+       __RPC_B_MAX,
+};
+
+static const struct blobmsg_policy rpc_uci_rollback_policy[__RPC_B_MAX] = {
+       [RPC_B_SESSION]  = { .name = "ubus_rpc_session",
+                                               .type = BLOBMSG_TYPE_STRING },
 };
 
 /*
@@ -128,6 +202,62 @@ rpc_uci_status(void)
 }
 
 /*
+ * Setup per-session delta save directory. If the passed "sid" blob attribute
+ * pointer is NULL then the precedure was not invoked through the ubus-rpc so
+ * we do not perform session isolation and use the default save directory.
+ */
+static void
+rpc_uci_set_savedir(struct blob_attr *sid)
+{
+       char path[PATH_MAX];
+
+       if (!sid)
+       {
+               uci_set_savedir(cursor, "/tmp/.uci");
+               return;
+       }
+
+       snprintf(path, sizeof(path) - 1,
+                RPC_UCI_SAVEDIR_PREFIX "%s", blobmsg_get_string(sid));
+
+       uci_set_savedir(cursor, path);
+}
+
+/*
+ * Test read access to given config. If the passed "sid" blob attribute pointer
+ * is NULL then the precedure was not invoked through the ubus-rpc so we do not
+ * perform access control and always assume true.
+ */
+static bool
+rpc_uci_read_access(struct blob_attr *sid, struct blob_attr *config)
+{
+       rpc_uci_set_savedir(sid);
+
+       if (!sid)
+               return true;
+
+       return rpc_session_access(blobmsg_data(sid), "uci",
+                                 blobmsg_data(config), "read");
+}
+
+/*
+ * Test write access to given config. If the passed "sid" blob attribute pointer
+ * is NULL then the precedure was not invoked through the ubus-rpc so we do not
+ * perform access control and always assume true.
+ */
+static bool
+rpc_uci_write_access(struct blob_attr *sid, struct blob_attr *config)
+{
+       rpc_uci_set_savedir(sid);
+
+       if (!sid)
+               return true;
+
+       return rpc_session_access(blobmsg_data(sid), "uci",
+                                 blobmsg_data(config), "write");
+}
+
+/*
  * Format applicable blob value as string and place a pointer to the string
  * buffer in "p". Uses a static string buffer.
  */
@@ -333,7 +463,7 @@ rpc_uci_dump_option(struct uci_option *o, const char *name)
  * corresponding section properties.
  */
 static void
-rpc_uci_dump_section(struct uci_section *s, const char *name)
+rpc_uci_dump_section(struct uci_section *s, const char *name, int index)
 {
        void *c;
        struct uci_option *o;
@@ -345,6 +475,9 @@ rpc_uci_dump_section(struct uci_section *s, const char *name)
        blobmsg_add_string(&buf, ".type", s->type);
        blobmsg_add_string(&buf, ".name", s->e.name);
 
+       if (index >= 0)
+               blobmsg_add_u32(&buf, ".index", index);
+
        uci_foreach_element(&s->options, e)
        {
                o = uci_to_option(e);
@@ -368,15 +501,18 @@ rpc_uci_dump_package(struct uci_package *p, const char *name,
 {
        void *c;
        struct uci_element *e;
+       int i = -1;
 
        c = blobmsg_open_table(&buf, name);
 
        uci_foreach_element(&p->sections, e)
        {
+               i++;
+
                if (!rpc_uci_match_section(uci_to_section(e), type, matches))
                        continue;
 
-               rpc_uci_dump_section(uci_to_section(e), e->name);
+               rpc_uci_dump_section(uci_to_section(e), e->name, i);
        }
 
        blobmsg_close_table(&buf, c);
@@ -384,9 +520,8 @@ rpc_uci_dump_package(struct uci_package *p, const char *name,
 
 
 static int
-rpc_uci_get(struct ubus_context *ctx, struct ubus_object *obj,
-            struct ubus_request_data *req, const char *method,
-            struct blob_attr *msg)
+rpc_uci_getcommon(struct ubus_context *ctx, struct ubus_request_data *req,
+                  struct blob_attr *msg, bool use_state)
 {
        struct blob_attr *tb[__RPC_G_MAX];
        struct uci_package *p = NULL;
@@ -398,11 +533,16 @@ rpc_uci_get(struct ubus_context *ctx, struct ubus_object *obj,
        if (!tb[RPC_G_CONFIG])
                return UBUS_STATUS_INVALID_ARGUMENT;
 
+       if (!rpc_uci_read_access(tb[RPC_G_SESSION], tb[RPC_G_CONFIG]))
+               return UBUS_STATUS_PERMISSION_DENIED;
+
        ptr.package = blobmsg_data(tb[RPC_G_CONFIG]);
-       uci_load(cursor, ptr.package, &p);
 
-       if (!p)
-               goto out;
+       if (use_state)
+               uci_set_savedir(cursor, "/var/state");
+
+       if (uci_load(cursor, ptr.package, &p))
+               return rpc_uci_status();
 
        if (tb[RPC_G_SECTION])
        {
@@ -424,7 +564,7 @@ rpc_uci_get(struct ubus_context *ctx, struct ubus_object *obj,
                break;
 
        case UCI_TYPE_SECTION:
-               rpc_uci_dump_section(ptr.s, "values");
+               rpc_uci_dump_section(ptr.s, "values", -1);
                break;
 
        case UCI_TYPE_OPTION:
@@ -438,13 +578,28 @@ rpc_uci_get(struct ubus_context *ctx, struct ubus_object *obj,
        ubus_send_reply(ctx, req, buf.head);
 
 out:
-       if (p)
-               uci_unload(cursor, p);
+       uci_unload(cursor, p);
 
        return rpc_uci_status();
 }
 
 static int
+rpc_uci_get(struct ubus_context *ctx, struct ubus_object *obj,
+            struct ubus_request_data *req, const char *method,
+            struct blob_attr *msg)
+{
+       return rpc_uci_getcommon(ctx, req, msg, false);
+}
+
+static int
+rpc_uci_state(struct ubus_context *ctx, struct ubus_object *obj,
+              struct ubus_request_data *req, const char *method,
+              struct blob_attr *msg)
+{
+       return rpc_uci_getcommon(ctx, req, msg, true);
+}
+
+static int
 rpc_uci_add(struct ubus_context *ctx, struct ubus_object *obj,
             struct ubus_request_data *req, const char *method,
             struct blob_attr *msg)
@@ -462,12 +617,13 @@ rpc_uci_add(struct ubus_context *ctx, struct ubus_object *obj,
        if (!tb[RPC_A_CONFIG] || !tb[RPC_A_TYPE])
                return UBUS_STATUS_INVALID_ARGUMENT;
 
-       ptr.package = blobmsg_data(tb[RPC_A_CONFIG]);
+       if (!rpc_uci_write_access(tb[RPC_A_SESSION], tb[RPC_A_CONFIG]))
+               return UBUS_STATUS_PERMISSION_DENIED;
 
-       uci_load(cursor, ptr.package, &p);
+       ptr.package = blobmsg_data(tb[RPC_A_CONFIG]);
 
-       if (!p)
-               goto out;
+       if (uci_load(cursor, ptr.package, &p))
+               return rpc_uci_status();
 
        /* add named section */
        if (tb[RPC_A_NAME])
@@ -522,8 +678,7 @@ rpc_uci_add(struct ubus_context *ctx, struct ubus_object *obj,
        ubus_send_reply(ctx, req, buf.head);
 
 out:
-       if (p)
-               uci_unload(cursor, p);
+       uci_unload(cursor, p);
 
        return rpc_uci_status();
 }
@@ -545,6 +700,7 @@ rpc_uci_merge_set(struct blob_attr *opt, struct uci_ptr *ptr)
 
        ptr->o = NULL;
        ptr->option = blobmsg_name(opt);
+       ptr->value = NULL;
 
        if (rpc_uci_lookup(ptr) || !ptr->s)
                return;
@@ -591,11 +747,13 @@ rpc_uci_set(struct ubus_context *ctx, struct ubus_object *obj,
                (!tb[RPC_S_SECTION] && !tb[RPC_S_TYPE] && !tb[RPC_S_MATCH]))
                return UBUS_STATUS_INVALID_ARGUMENT;
 
+       if (!rpc_uci_write_access(tb[RPC_S_SESSION], tb[RPC_S_CONFIG]))
+               return UBUS_STATUS_PERMISSION_DENIED;
+
        ptr.package = blobmsg_data(tb[RPC_S_CONFIG]);
-       uci_load(cursor, ptr.package, &p);
 
-       if (!p)
-               goto out;
+       if (uci_load(cursor, ptr.package, &p))
+               return rpc_uci_status();
 
        if (tb[RPC_S_SECTION])
        {
@@ -620,10 +778,115 @@ rpc_uci_set(struct ubus_context *ctx, struct ubus_object *obj,
        }
 
        uci_save(cursor, p);
+       uci_unload(cursor, p);
 
-out:
-       if (p)
-               uci_unload(cursor, p);
+       return rpc_uci_status();
+}
+
+/*
+ * Delete option or section from uci specified by given blob attribute pointer
+ *  1) if the blob is of type array, delete any option named after each element
+ *  2) if the blob is of type string, delete the option named after its value
+ *  3) if the blob is NULL, delete entire section
+ */
+static void
+rpc_uci_merge_delete(struct blob_attr *opt, struct uci_ptr *ptr)
+{
+       struct blob_attr *cur;
+       int rem;
+
+       if (rpc_uci_lookup(ptr) || !ptr->s)
+               return;
+
+       if (!opt)
+       {
+               ptr->o = NULL;
+               ptr->option = NULL;
+
+               uci_delete(cursor, ptr);
+       }
+       else if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
+       {
+               blobmsg_for_each_attr(cur, opt, rem)
+               {
+                       if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
+                               continue;
+
+                       ptr->o = NULL;
+                       ptr->option = blobmsg_data(cur);
+
+                       if (rpc_uci_lookup(ptr) || !ptr->o)
+                               continue;
+
+                       uci_delete(cursor, ptr);
+               }
+       }
+       else if (blobmsg_type(opt) == BLOBMSG_TYPE_STRING)
+       {
+               ptr->o = NULL;
+               ptr->option = blobmsg_data(opt);
+
+               if (rpc_uci_lookup(ptr) || !ptr->o)
+                       return;
+
+               uci_delete(cursor, ptr);
+       }
+}
+
+static int
+rpc_uci_delete(struct ubus_context *ctx, struct ubus_object *obj,
+               struct ubus_request_data *req, const char *method,
+               struct blob_attr *msg)
+{
+       struct blob_attr *tb[__RPC_D_MAX];
+       struct uci_package *p = NULL;
+       struct uci_element *e, *tmp;
+       struct uci_ptr ptr = { 0 };
+
+       blobmsg_parse(rpc_uci_delete_policy, __RPC_D_MAX, tb,
+                     blob_data(msg), blob_len(msg));
+
+       if (!tb[RPC_D_CONFIG] ||
+               (!tb[RPC_D_SECTION] && !tb[RPC_D_TYPE] && !tb[RPC_D_MATCH]))
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       if (!rpc_uci_write_access(tb[RPC_D_SESSION], tb[RPC_D_CONFIG]))
+               return UBUS_STATUS_PERMISSION_DENIED;
+
+       ptr.package = blobmsg_data(tb[RPC_D_CONFIG]);
+
+       if (uci_load(cursor, ptr.package, &p))
+               return rpc_uci_status();
+
+       if (tb[RPC_D_SECTION])
+       {
+               ptr.section = blobmsg_data(tb[RPC_D_SECTION]);
+
+               if (tb[RPC_D_OPTIONS])
+                       rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
+               else
+                       rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
+       }
+       else
+       {
+               uci_foreach_element_safe(&p->sections, tmp, e)
+               {
+                       if (!rpc_uci_match_section(uci_to_section(e),
+                                                  tb[RPC_D_TYPE], tb[RPC_D_MATCH]))
+                               continue;
+
+                       ptr.s = NULL;
+                       ptr.section = e->name;
+
+                       if (tb[RPC_D_OPTIONS])
+                               rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
+                       else
+                               rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
+               }
+       }
+
+       uci_save(cursor, p);
+       uci_unload(cursor, p);
 
        return rpc_uci_status();
 }
@@ -643,6 +906,9 @@ rpc_uci_rename(struct ubus_context *ctx, struct ubus_object *obj,
        if (!tb[RPC_R_CONFIG] || !tb[RPC_R_SECTION] || !tb[RPC_R_NAME])
                return UBUS_STATUS_INVALID_ARGUMENT;
 
+       if (!rpc_uci_write_access(tb[RPC_R_SESSION], tb[RPC_R_CONFIG]))
+               return UBUS_STATUS_PERMISSION_DENIED;
+
        ptr.package = blobmsg_data(tb[RPC_R_CONFIG]);
        ptr.section = blobmsg_data(tb[RPC_R_SECTION]);
        ptr.value   = blobmsg_data(tb[RPC_R_NAME]);
@@ -650,10 +916,8 @@ rpc_uci_rename(struct ubus_context *ctx, struct ubus_object *obj,
        if (tb[RPC_R_OPTION])
                ptr.option = blobmsg_data(tb[RPC_R_OPTION]);
 
-       uci_load(cursor, ptr.package, &p);
-
-       if (!p)
-               goto out;
+       if (uci_load(cursor, ptr.package, &p))
+               return rpc_uci_status();
 
        if (uci_lookup_ptr(cursor, &ptr, NULL, true))
                goto out;
@@ -670,8 +934,7 @@ rpc_uci_rename(struct ubus_context *ctx, struct ubus_object *obj,
        uci_save(cursor, p);
 
 out:
-       if (p)
-               uci_unload(cursor, p);
+       uci_unload(cursor, p);
 
        return rpc_uci_status();
 }
@@ -693,12 +956,13 @@ rpc_uci_order(struct ubus_context *ctx, struct ubus_object *obj,
        if (!tb[RPC_O_CONFIG] || !tb[RPC_O_SECTIONS])
                return UBUS_STATUS_INVALID_ARGUMENT;
 
-       ptr.package = blobmsg_data(tb[RPC_O_CONFIG]);
+       if (!rpc_uci_write_access(tb[RPC_O_SESSION], tb[RPC_O_CONFIG]))
+               return UBUS_STATUS_PERMISSION_DENIED;
 
-       uci_load(cursor, ptr.package, &p);
+       ptr.package = blobmsg_data(tb[RPC_O_CONFIG]);
 
-       if (!p)
-               goto out;
+       if (uci_load(cursor, ptr.package, &p))
+               return rpc_uci_status();
 
        blobmsg_for_each_attr(cur, tb[RPC_O_SECTIONS], rem)
        {
@@ -715,10 +979,7 @@ rpc_uci_order(struct ubus_context *ctx, struct ubus_object *obj,
        }
 
        uci_save(cursor, p);
-
-out:
-       if (p)
-               uci_unload(cursor, p);
+       uci_unload(cursor, p);
 
        return rpc_uci_status();
 }
@@ -775,10 +1036,11 @@ rpc_uci_changes(struct ubus_context *ctx, struct ubus_object *obj,
        if (!tb[RPC_C_CONFIG])
                return UBUS_STATUS_INVALID_ARGUMENT;
 
-       uci_load(cursor, blobmsg_data(tb[RPC_C_CONFIG]), &p);
+       if (!rpc_uci_read_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
+               return UBUS_STATUS_PERMISSION_DENIED;
 
-       if (!p)
-               goto out;
+       if (uci_load(cursor, blobmsg_data(tb[RPC_C_CONFIG]), &p))
+               return rpc_uci_status();
 
        blob_buf_init(&buf, 0);
        c = blobmsg_open_array(&buf, "changes");
@@ -790,40 +1052,66 @@ rpc_uci_changes(struct ubus_context *ctx, struct ubus_object *obj,
 
        ubus_send_reply(ctx, req, buf.head);
 
-out:
-       if (p)
-               uci_unload(cursor, p);
+       uci_unload(cursor, p);
 
        return rpc_uci_status();
 }
 
+static void
+rpc_uci_trigger_event(struct ubus_context *ctx, const char *config)
+{
+       char *pkg = strdup(config);
+       static struct blob_buf b;
+       uint32_t id;
+
+       if (!ubus_lookup_id(ctx, "service", &id)) {
+               void *c;
+
+               blob_buf_init(&b, 0);
+               blobmsg_add_string(&b, "type", "config.change");
+               c = blobmsg_open_table(&b, "data");
+               blobmsg_add_string(&b, "package", pkg);
+               blobmsg_close_table(&b, c);
+               ubus_invoke(ctx, id, "event", b.head, NULL, 0, 1000);
+       }
+       free(pkg);
+}
+
 static int
-rpc_uci_revert_commit(struct blob_attr *msg, bool commit)
+rpc_uci_revert_commit(struct ubus_context *ctx, struct blob_attr *msg, bool commit)
 {
        struct blob_attr *tb[__RPC_C_MAX];
        struct uci_package *p = NULL;
        struct uci_ptr ptr = { 0 };
 
+       if (!apply_running)
+               return UBUS_STATUS_PERMISSION_DENIED;
+
        blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
                      blob_data(msg), blob_len(msg));
 
        if (!tb[RPC_C_CONFIG])
                return UBUS_STATUS_INVALID_ARGUMENT;
 
-       ptr.package = blobmsg_data(tb[RPC_C_CONFIG]);
-       uci_load(cursor, ptr.package, &p);
+       if (!rpc_uci_write_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
+               return UBUS_STATUS_PERMISSION_DENIED;
 
-       if (!p || uci_lookup_ptr(cursor, &ptr, NULL, true) || !ptr.p)
-               goto out;
+       ptr.package = blobmsg_data(tb[RPC_C_CONFIG]);
 
        if (commit)
-               uci_commit(cursor, &p, false);
+       {
+               if (!uci_load(cursor, ptr.package, &p))
+               {
+                       uci_commit(cursor, &p, false);
+                       uci_unload(cursor, p);
+               }
+               rpc_uci_trigger_event(ctx, blobmsg_get_string(tb[RPC_C_CONFIG]));
+       }
        else
-               uci_revert(cursor, &ptr);
-
-out:
-       if (p)
-               uci_unload(cursor, p);
+       {
+               if (!uci_lookup_ptr(cursor, &ptr, NULL, true) && ptr.p)
+                       uci_revert(cursor, &ptr);
+       }
 
        return rpc_uci_status();
 }
@@ -833,7 +1121,7 @@ rpc_uci_revert(struct ubus_context *ctx, struct ubus_object *obj,
                struct ubus_request_data *req, const char *method,
                struct blob_attr *msg)
 {
-       return rpc_uci_revert_commit(msg, false);
+       return rpc_uci_revert_commit(ctx, msg, false);
 }
 
 static int
@@ -841,7 +1129,7 @@ rpc_uci_commit(struct ubus_context *ctx, struct ubus_object *obj,
                struct ubus_request_data *req, const char *method,
                struct blob_attr *msg)
 {
-       return rpc_uci_revert_commit(msg, true);
+       return rpc_uci_revert_commit(ctx, msg, true);
 }
 
 static int
@@ -872,18 +1160,319 @@ out:
 }
 
 
+/*
+ * Remove given delta save directory (if any).
+ */
+static void
+rpc_uci_purge_dir(const char *path)
+{
+       DIR *d;
+       struct stat s;
+       struct dirent *e;
+       char file[PATH_MAX];
+
+       if (stat(path, &s) || !S_ISDIR(s.st_mode))
+               return;
+
+       if ((d = opendir(path)) != NULL)
+       {
+               while ((e = readdir(d)) != NULL)
+               {
+                       snprintf(file, sizeof(file) - 1, "%s/%s", path, e->d_name);
+
+                       if (stat(file, &s) || !S_ISREG(s.st_mode))
+                               continue;
+
+                       unlink(file);
+               }
+
+               closedir(d);
+
+               rmdir(path);
+       }
+}
+
+static int
+rpc_uci_apply_config(struct ubus_context *ctx, char *config)
+{
+       struct uci_package *p = NULL;
+       struct uci_ptr ptr = { 0 };
+
+       ptr.package = config;
+
+       if (!uci_load(cursor, ptr.package, &p)) {
+               uci_commit(cursor, &p, false);
+               uci_unload(cursor, p);
+       }
+       rpc_uci_trigger_event(ctx, config);
+
+       return 0;
+}
+
+static void
+rpc_uci_copy_file(const char *src, const char *target, const char *file)
+{
+       char tmp[256];
+       FILE *in, *out;
+
+       snprintf(tmp, sizeof(tmp), "%s%s", src, file);
+       in = fopen(tmp, "rb");
+       snprintf(tmp, sizeof(tmp), "%s%s", target, file);
+       out = fopen(tmp, "wb+");
+       if (in && out)
+               while (!feof(in)) {
+                       int len = fread(tmp, 1, sizeof(tmp), in);
+
+                       if(len > 0)
+                               fwrite(tmp, 1, len, out);
+               }
+       if(in)
+               fclose(in);
+       if(out)
+               fclose(out);
+}
+
+static void
+rpc_uci_do_rollback(struct ubus_context *ctx, const char *sid, glob_t *gl)
+{
+       int i;
+       char tmp[PATH_MAX];
+
+       if (sid) {
+               snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
+               mkdir(tmp, 0700);
+       }
+
+       for (i = 0; i < gl->gl_pathc; i++) {
+               char *config = basename(gl->gl_pathv[i]);
+
+               if (*config == '.')
+                       continue;
+
+               rpc_uci_copy_file(RPC_SNAPSHOT_FILES, RPC_UCI_DIR, config);
+               rpc_uci_apply_config(ctx, config);
+               if (sid)
+                       rpc_uci_copy_file(RPC_SNAPSHOT_DELTA, tmp, config);
+       }
+
+       rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
+       rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
+
+       uloop_timeout_cancel(&apply_timer);
+       apply_running = false;
+       apply_ctx = NULL;
+}
+
+static void
+rpc_uci_apply_timeout(struct uloop_timeout *t)
+{
+       glob_t gl;
+       char tmp[PATH_MAX];
+
+       snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
+       if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
+               return;
+
+       rpc_uci_do_rollback(apply_ctx, NULL, &gl);
+}
+
+static int
+rpc_uci_apply_access(const char *sid, glob_t *gl)
+{
+       struct stat s;
+       int i, c = 0;
+
+       if (gl->gl_pathc < 3)
+               return UBUS_STATUS_NO_DATA;
+
+       for (i = 0; i < gl->gl_pathc; i++) {
+               char *config = basename(gl->gl_pathv[i]);
+
+               if (*config == '.')
+                       continue;
+               if (stat(gl->gl_pathv[i], &s) || !s.st_size)
+                       continue;
+               if (!rpc_session_access(sid, "uci", config, "write"))
+                       return UBUS_STATUS_PERMISSION_DENIED;
+               c++;
+       }
+
+       if (!c)
+               return UBUS_STATUS_NO_DATA;
+
+       return 0;
+}
+
+static int
+rpc_uci_apply(struct ubus_context *ctx, struct ubus_object *obj,
+              struct ubus_request_data *req, const char *method,
+              struct blob_attr *msg)
+{
+       struct blob_attr *tb[__RPC_T_MAX];
+       int timeout = RPC_APPLY_TIMEOUT;
+       char tmp[PATH_MAX];
+       bool commit = false;
+       int ret, i;
+       char *sid;
+       glob_t gl;
+
+       blobmsg_parse(rpc_uci_apply_policy, __RPC_T_MAX, tb,
+                     blob_data(msg), blob_len(msg));
+
+       if (tb[RPC_T_COMMIT])
+               commit = blobmsg_get_bool(tb[RPC_T_COMMIT]);
+
+       if (apply_running && !commit)
+               return UBUS_STATUS_PERMISSION_DENIED;
+
+       if (!tb[RPC_T_SESSION])
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       sid = blobmsg_data(tb[RPC_T_SESSION]);
+
+       if (tb[RPC_T_TIMEOUT])
+               timeout = blobmsg_get_u32(tb[RPC_T_TIMEOUT]);
+
+       rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
+       rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
+
+       if (!apply_running) {
+               mkdir(RPC_SNAPSHOT_FILES, 0700);
+               mkdir(RPC_SNAPSHOT_DELTA, 0700);
+
+               snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/*", sid);
+               if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
+                       return -1;
+
+               snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
+
+               ret = rpc_uci_apply_access(sid, &gl);
+               if (ret) {
+                       globfree(&gl);
+                       return ret;
+               }
+
+               for (i = 0; i < gl.gl_pathc; i++) {
+                       char *config = basename(gl.gl_pathv[i]);
+                       struct stat s;
+
+                       if (*config == '.')
+                               continue;
+
+                       if (stat(gl.gl_pathv[i], &s) || !s.st_size)
+                               continue;
+
+                       rpc_uci_copy_file(RPC_UCI_DIR, RPC_SNAPSHOT_FILES, config);
+                       rpc_uci_copy_file(tmp, RPC_SNAPSHOT_DELTA, config);
+                       rpc_uci_apply_config(ctx, config);
+               }
+
+               globfree(&gl);
+
+               apply_running = true;
+               apply_timer.cb = rpc_uci_apply_timeout;
+               uloop_timeout_set(&apply_timer, timeout * 1000);
+               apply_ctx = ctx;
+       }
+
+       if (apply_running && commit) {
+               rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
+               rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
+
+               uloop_timeout_cancel(&apply_timer);
+               apply_running = false;
+               apply_ctx = NULL;
+       }
+
+       return 0;
+}
+
+static int
+rpc_uci_rollback(struct ubus_context *ctx, struct ubus_object *obj,
+                 struct ubus_request_data *req, const char *method,
+                 struct blob_attr *msg)
+{
+       struct blob_attr *tb[__RPC_B_MAX];
+       char tmp[PATH_MAX];
+       glob_t gl;
+       char *sid;
+       int ret;
+
+       blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
+                     blob_data(msg), blob_len(msg));
+
+       if (!apply_running)
+               return UBUS_STATUS_PERMISSION_DENIED;
+
+       if (!tb[RPC_B_SESSION])
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       sid = blobmsg_data(tb[RPC_B_SESSION]);
+
+       snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
+       if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
+               return -1;
+
+       ret = rpc_uci_apply_access(sid, &gl);
+       if (ret) {
+               globfree(&gl);
+               return ret;
+       }
+
+       rpc_uci_do_rollback(ctx, sid, &gl);
+
+       globfree(&gl);
+
+       return 0;
+}
+
+
+/*
+ * Session destroy callback to purge associated delta directory.
+ */
+static void
+rpc_uci_purge_savedir_cb(struct rpc_session *ses, void *priv)
+{
+       char path[PATH_MAX];
+
+       snprintf(path, sizeof(path) - 1, RPC_UCI_SAVEDIR_PREFIX "%s", ses->id);
+       rpc_uci_purge_dir(path);
+}
+
+/*
+ * Removes all delta directories which match the RPC_UCI_SAVEDIR_PREFIX.
+ * This is used to clean up garbage when starting rpcd.
+ */
+void rpc_uci_purge_savedirs(void)
+{
+       int i;
+       glob_t gl;
+
+       if (!glob(RPC_UCI_SAVEDIR_PREFIX "*", 0, NULL, &gl))
+       {
+               for (i = 0; i < gl.gl_pathc; i++)
+                       rpc_uci_purge_dir(gl.gl_pathv[i]);
+
+               globfree(&gl);
+       }
+}
+
 int rpc_uci_api_init(struct ubus_context *ctx)
 {
        static const struct ubus_method uci_methods[] = {
                { .name = "configs", .handler = rpc_uci_configs },
-               UBUS_METHOD("get",     rpc_uci_get,     rpc_uci_get_policy),
-               UBUS_METHOD("add",     rpc_uci_add,     rpc_uci_add_policy),
-               UBUS_METHOD("set",     rpc_uci_set,     rpc_uci_set_policy),
-               UBUS_METHOD("rename",  rpc_uci_rename,  rpc_uci_rename_policy),
-               UBUS_METHOD("order",   rpc_uci_order,   rpc_uci_order_policy),
-               UBUS_METHOD("changes", rpc_uci_changes, rpc_uci_config_policy),
-               UBUS_METHOD("revert",  rpc_uci_revert,  rpc_uci_config_policy),
-               UBUS_METHOD("commit",  rpc_uci_commit,  rpc_uci_config_policy),
+               UBUS_METHOD("get",      rpc_uci_get,      rpc_uci_get_policy),
+               UBUS_METHOD("state",    rpc_uci_state,    rpc_uci_get_policy),
+               UBUS_METHOD("add",      rpc_uci_add,      rpc_uci_add_policy),
+               UBUS_METHOD("set",      rpc_uci_set,      rpc_uci_set_policy),
+               UBUS_METHOD("delete",   rpc_uci_delete,   rpc_uci_delete_policy),
+               UBUS_METHOD("rename",   rpc_uci_rename,   rpc_uci_rename_policy),
+               UBUS_METHOD("order",    rpc_uci_order,    rpc_uci_order_policy),
+               UBUS_METHOD("changes",  rpc_uci_changes,  rpc_uci_config_policy),
+               UBUS_METHOD("revert",   rpc_uci_revert,   rpc_uci_config_policy),
+               UBUS_METHOD("commit",   rpc_uci_commit,   rpc_uci_config_policy),
+               UBUS_METHOD("apply",    rpc_uci_apply,    rpc_uci_apply_policy),
+               UBUS_METHOD("rollback", rpc_uci_rollback, rpc_uci_rollback_policy),
        };
 
        static struct ubus_object_type uci_type =
@@ -896,10 +1485,16 @@ int rpc_uci_api_init(struct ubus_context *ctx)
                .n_methods = ARRAY_SIZE(uci_methods),
        };
 
+       static struct rpc_session_cb cb = {
+               .cb = rpc_uci_purge_savedir_cb
+       };
+
        cursor = uci_alloc_context();
 
        if (!cursor)
                return UBUS_STATUS_UNKNOWN_ERROR;
 
+       rpc_session_destroy_cb(&cb);
+
        return ubus_add_object(ctx, &obj);
 }