X-Git-Url: http://git.archive.openwrt.org/?p=project%2Frpcd.git;a=blobdiff_plain;f=uci.c;h=a1b83117abaed2823193ab110bfd4d211cf02d38;hp=a49c8d0d4ad86ea0353a4399525314b30f63627f;hb=HEAD;hpb=2f5a613718f5297febddb3ca7dc860ab6ed8c513 diff --git a/uci.c b/uci.c index a49c8d0..a1b8311 100644 --- a/uci.c +++ b/uci.c @@ -1,7 +1,7 @@ /* * rpcd - UBUS RPC server * - * Copyright (C) 2013 Jo-Philipp Wich + * Copyright (C) 2013-2014 Jo-Philipp Wich * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -23,13 +23,15 @@ #include #include +#include #include 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; + +char apply_sid[RPC_SID_LEN + 1]; enum { RPC_G_CONFIG, @@ -156,14 +158,14 @@ static const struct blobmsg_policy rpc_uci_config_policy[__RPC_C_MAX] = { }; 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 }, @@ -202,6 +204,29 @@ rpc_uci_status(void) } /* + * Clear all save directories from the uci cursor and append the given path + * as new save directory. + */ +static void +rpc_uci_replace_savedir(const char *path) +{ + struct uci_element *e, *tmp; + + uci_foreach_element_safe(&cursor->delta_path, tmp, e) { + if (e->name) + free(e->name); + + free(e); + } + + cursor->delta_path.prev = &cursor->delta_path; + cursor->delta_path.next = &cursor->delta_path; + + if (path) + uci_set_savedir(cursor, path); +} + +/* * 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. @@ -213,14 +238,14 @@ rpc_uci_set_savedir(struct blob_attr *sid) if (!sid) { - uci_set_savedir(cursor, "/tmp/.uci"); + rpc_uci_replace_savedir("/tmp/.uci"); return; } snprintf(path, sizeof(path) - 1, RPC_UCI_SAVEDIR_PREFIX "%s", blobmsg_get_string(sid)); - uci_set_savedir(cursor, path); + rpc_uci_replace_savedir(path); } /* @@ -271,8 +296,7 @@ rpc_uci_format_blob(struct blob_attr *v, const char **p) switch (blobmsg_type(v)) { case BLOBMSG_TYPE_STRING: - if (blobmsg_data_len(v) > 1) - *p = blobmsg_data(v); + *p = blobmsg_data(v); break; case BLOBMSG_TYPE_INT64: @@ -948,7 +972,7 @@ rpc_uci_order(struct ubus_context *ctx, struct ubus_object *obj, struct blob_attr *cur; struct uci_package *p = NULL; struct uci_ptr ptr = { 0 }; - int rem, i = 1; + int rem, i = 0; blobmsg_parse(rpc_uci_order_policy, __RPC_O_MAX, tb, blob_data(msg), blob_len(msg)); @@ -1028,33 +1052,73 @@ rpc_uci_changes(struct ubus_context *ctx, struct ubus_object *obj, struct blob_attr *tb[__RPC_C_MAX]; struct uci_package *p = NULL; struct uci_element *e; - void *c; + char **configs; + void *c, *d; + int i; 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; + if (tb[RPC_C_CONFIG]) + { + if (!rpc_uci_read_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG])) + return UBUS_STATUS_PERMISSION_DENIED; - if (!rpc_uci_read_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG])) - return UBUS_STATUS_PERMISSION_DENIED; + 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"); + + uci_foreach_element(&p->saved_delta, e) + rpc_uci_dump_change(uci_to_delta(e)); + + blobmsg_close_array(&buf, c); + + uci_unload(cursor, p); + + ubus_send_reply(ctx, req, buf.head); - if (uci_load(cursor, blobmsg_data(tb[RPC_C_CONFIG]), &p)) + return rpc_uci_status(); + } + + rpc_uci_set_savedir(tb[RPC_C_SESSION]); + + if (uci_list_configs(cursor, &configs)) return rpc_uci_status(); blob_buf_init(&buf, 0); - c = blobmsg_open_array(&buf, "changes"); - uci_foreach_element(&p->saved_delta, e) - rpc_uci_dump_change(uci_to_delta(e)); + c = blobmsg_open_table(&buf, "changes"); - blobmsg_close_array(&buf, c); + for (i = 0; configs[i]; i++) + { + if (tb[RPC_C_SESSION] && + !rpc_session_access(blobmsg_data(tb[RPC_C_SESSION]), "uci", + configs[i], "read")) + continue; - ubus_send_reply(ctx, req, buf.head); + if (uci_load(cursor, configs[i], &p)) + continue; - uci_unload(cursor, p); + if (!uci_list_empty(&p->saved_delta)) + { + d = blobmsg_open_array(&buf, configs[i]); - return rpc_uci_status(); + uci_foreach_element(&p->saved_delta, e) + rpc_uci_dump_change(uci_to_delta(e)); + + blobmsg_close_array(&buf, d); + } + + uci_unload(cursor, p); + } + + blobmsg_close_table(&buf, c); + + ubus_send_reply(ctx, req, buf.head); + + return 0; } static void @@ -1084,7 +1148,7 @@ rpc_uci_revert_commit(struct ubus_context *ctx, struct blob_attr *msg, bool comm 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, @@ -1104,13 +1168,16 @@ rpc_uci_revert_commit(struct ubus_context *ctx, struct blob_attr *msg, bool comm { 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 { if (!uci_lookup_ptr(cursor, &ptr, NULL, true) && ptr.p) + { uci_revert(cursor, &ptr); + uci_unload(cursor, ptr.p); + } } return rpc_uci_status(); @@ -1196,11 +1263,8 @@ 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)) { + if (!uci_load(cursor, config, &p)) { uci_commit(cursor, &p, false); uci_unload(cursor, p); } @@ -1232,17 +1296,53 @@ rpc_uci_copy_file(const char *src, const char *target, const char *file) fclose(out); } +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 void -rpc_uci_do_rollback(struct ubus_context *ctx, const char *sid, glob_t *gl) +rpc_uci_do_rollback(struct ubus_context *ctx, glob_t *gl) { - int i; + int i, deny; char tmp[PATH_MAX]; - if (sid) { - snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid); + /* Test apply permission to see if the initiator session still exists. + * If it does, restore the delta files as well, else just restore the + * main configuration files. */ + deny = apply_sid[0] + ? rpc_uci_apply_access(apply_sid, gl) : UBUS_STATUS_NOT_FOUND; + + if (!deny) { + snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", apply_sid); mkdir(tmp, 0700); } + /* avoid merging unrelated uci changes when restoring old configs */ + rpc_uci_replace_savedir("/dev/null"); + for (i = 0; i < gl->gl_pathc; i++) { char *config = basename(gl->gl_pathv[i]); @@ -1251,15 +1351,18 @@ rpc_uci_do_rollback(struct ubus_context *ctx, const char *sid, glob_t *gl) 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); + + if (deny) + continue; + + 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; + memset(apply_sid, 0, sizeof(apply_sid)); apply_ctx = NULL; } @@ -1273,34 +1376,9 @@ rpc_uci_apply_timeout(struct uloop_timeout *t) 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]); + rpc_uci_do_rollback(apply_ctx, &gl); - 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; + globfree(&gl); } static int @@ -1311,7 +1389,7 @@ rpc_uci_apply(struct ubus_context *ctx, struct ubus_object *obj, 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; @@ -1319,10 +1397,10 @@ rpc_uci_apply(struct ubus_context *ctx, struct ubus_object *obj, 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]) @@ -1336,13 +1414,15 @@ rpc_uci_apply(struct ubus_context *ctx, struct ubus_object *obj, rpc_uci_purge_dir(RPC_SNAPSHOT_FILES); rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA); - if (!apply_running) { + if (!apply_sid[0]) { + rpc_uci_set_savedir(tb[RPC_T_SESSION]); + 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); @@ -1352,6 +1432,10 @@ rpc_uci_apply(struct ubus_context *ctx, struct ubus_object *obj, 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; @@ -1369,20 +1453,44 @@ rpc_uci_apply(struct ubus_context *ctx, struct ubus_object *obj, 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; } @@ -1396,36 +1504,47 @@ rpc_uci_rollback(struct ubus_context *ctx, struct ubus_object *obj, 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); + rpc_uci_do_rollback(ctx, &gl); globfree(&gl); return 0; } +static int +rpc_uci_reload(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + char * const cmd[2] = { "/sbin/reload_config", NULL }; + + if (!fork()) { + /* wait for the RPC call to complete */ + sleep(2); + return execv(cmd[0], cmd); + } + + return 0; +} /* * Session destroy callback to purge associated delta directory. @@ -1472,7 +1591,9 @@ int rpc_uci_api_init(struct ubus_context *ctx) 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), + UBUS_METHOD_NOARG("reload_config", rpc_uci_reload), }; static struct ubus_object_type uci_type =