+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 rollback = 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_ROLLBACK])
+ rollback = blobmsg_get_bool(tb[RPC_T_ROLLBACK]);
+
+ if (apply_sid[0] && rollback)
+ 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_sid[0]) {
+ 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 UBUS_STATUS_NOT_FOUND;
+
+ snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
+
+ ret = rpc_uci_apply_access(sid, &gl);
+ if (ret) {
+ globfree(&gl);
+ return ret;
+ }
+
+ /* copy SID early because rpc_uci_apply_config() will clobber buf */
+ if (rollback)
+ strncpy(apply_sid, sid, RPC_SID_LEN);
+
+ 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);
+
+ if (rollback) {
+ apply_timer.cb = rpc_uci_apply_timeout;
+ uloop_timeout_set(&apply_timer, timeout * 1000);
+ apply_ctx = ctx;
+ }
+ }
+
+ return 0;
+}
+
+static int
+rpc_uci_confirm(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 *sid;
+
+ blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
+ blob_data(msg), blob_len(msg));
+
+ if (!tb[RPC_B_SESSION])
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ sid = blobmsg_data(tb[RPC_B_SESSION]);
+
+ if (!apply_sid[0])
+ return UBUS_STATUS_NO_DATA;
+
+ if (strcmp(apply_sid, sid))
+ return UBUS_STATUS_PERMISSION_DENIED;
+
+ rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
+ rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
+
+ uloop_timeout_cancel(&apply_timer);
+ memset(apply_sid, 0, sizeof(apply_sid));
+ 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;
+
+ blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
+ blob_data(msg), blob_len(msg));
+
+ if (!apply_sid[0])
+ return UBUS_STATUS_NO_DATA;
+
+ if (!tb[RPC_B_SESSION])
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ sid = blobmsg_data(tb[RPC_B_SESSION]);
+
+ if (strcmp(apply_sid, sid))
+ return UBUS_STATUS_PERMISSION_DENIED;
+
+ snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
+ if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
+ return UBUS_STATUS_NOT_FOUND;
+
+ 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);
+ }
+}