/*
* rpcd - UBUS RPC server
*
- * 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
static struct uci_context *cursor;
static struct uloop_timeout apply_timer;
static struct ubus_context *apply_ctx;
-static bool apply_running;
+static char apply_sid[RPC_SID_LEN + 1];
enum {
RPC_G_CONFIG,
};
enum {
- RPC_T_COMMIT,
+ RPC_T_ROLLBACK,
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_ROLLBACK] = { .name = "rollback", .type = BLOBMSG_TYPE_BOOL },
[RPC_T_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
[RPC_T_SESSION] = { .name = "ubus_rpc_session",
.type = BLOBMSG_TYPE_STRING },
struct uci_package *p = NULL;
struct uci_ptr ptr = { 0 };
- if (!apply_running)
+ if (apply_sid[0])
return UBUS_STATUS_PERMISSION_DENIED;
blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
{
uci_commit(cursor, &p, false);
uci_unload(cursor, p);
+ rpc_uci_trigger_event(ctx, blobmsg_get_string(tb[RPC_C_CONFIG]));
}
- rpc_uci_trigger_event(ctx, blobmsg_get_string(tb[RPC_C_CONFIG]));
}
else
{
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)) {
+ if (!uci_load(cursor, config, &p)) {
uci_commit(cursor, &p, false);
uci_unload(cursor, p);
}
rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
uloop_timeout_cancel(&apply_timer);
- apply_running = false;
+ memset(apply_sid, 0, sizeof(apply_sid));
apply_ctx = NULL;
}
struct blob_attr *tb[__RPC_T_MAX];
int timeout = RPC_APPLY_TIMEOUT;
char tmp[PATH_MAX];
- bool commit = false;
+ 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_COMMIT])
- commit = blobmsg_get_bool(tb[RPC_T_COMMIT]);
+ if (tb[RPC_T_ROLLBACK])
+ rollback = blobmsg_get_bool(tb[RPC_T_ROLLBACK]);
- if (apply_running && !commit)
+ if (apply_sid[0] && rollback)
return UBUS_STATUS_PERMISSION_DENIED;
if (!tb[RPC_T_SESSION])
rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
- if (!apply_running) {
+ 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 -1;
+ return UBUS_STATUS_NOT_FOUND;
snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
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;
globfree(&gl);
- apply_running = true;
- apply_timer.cb = rpc_uci_apply_timeout;
- uloop_timeout_set(&apply_timer, timeout * 1000);
- apply_ctx = ctx;
+ if (rollback) {
+ 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);
+ return 0;
+}
- uloop_timeout_cancel(&apply_timer);
- apply_running = false;
- apply_ctx = NULL;
- }
+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;
}
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 (!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 -1;
-
- ret = rpc_uci_apply_access(sid, &gl);
- if (ret) {
- globfree(&gl);
- return ret;
- }
+ return UBUS_STATUS_NOT_FOUND;
rpc_uci_do_rollback(ctx, sid, &gl);
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("confirm", rpc_uci_confirm, rpc_uci_rollback_policy),
UBUS_METHOD("rollback", rpc_uci_rollback, rpc_uci_rollback_policy),
};