2 * rpcd - UBUS RPC server
4 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <libubox/blobmsg.h>
23 #include <libubox/blobmsg_json.h>
26 #include <rpcd/exec.h>
27 #include <rpcd/session.h>
29 static struct blob_buf buf;
30 static struct uci_context *cursor;
31 static struct uloop_timeout apply_timer;
32 static struct ubus_context *apply_ctx;
33 static char apply_sid[RPC_SID_LEN + 1];
45 static const struct blobmsg_policy rpc_uci_get_policy[__RPC_G_MAX] = {
46 [RPC_G_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
47 [RPC_G_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
48 [RPC_G_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
49 [RPC_G_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
50 [RPC_G_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
51 [RPC_G_SESSION] = { .name = "ubus_rpc_session",
52 .type = BLOBMSG_TYPE_STRING },
64 static const struct blobmsg_policy rpc_uci_add_policy[__RPC_A_MAX] = {
65 [RPC_A_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
66 [RPC_A_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
67 [RPC_A_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
68 [RPC_A_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
69 [RPC_A_SESSION] = { .name = "ubus_rpc_session",
70 .type = BLOBMSG_TYPE_STRING },
83 static const struct blobmsg_policy rpc_uci_set_policy[__RPC_S_MAX] = {
84 [RPC_S_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
85 [RPC_S_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
86 [RPC_S_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
87 [RPC_S_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
88 [RPC_S_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
89 [RPC_S_SESSION] = { .name = "ubus_rpc_session",
90 .type = BLOBMSG_TYPE_STRING },
104 static const struct blobmsg_policy rpc_uci_delete_policy[__RPC_D_MAX] = {
105 [RPC_D_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
106 [RPC_D_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
107 [RPC_D_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
108 [RPC_D_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
109 [RPC_D_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
110 [RPC_D_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_ARRAY },
111 [RPC_D_SESSION] = { .name = "ubus_rpc_session",
112 .type = BLOBMSG_TYPE_STRING },
124 static const struct blobmsg_policy rpc_uci_rename_policy[__RPC_R_MAX] = {
125 [RPC_R_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
126 [RPC_R_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
127 [RPC_R_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
128 [RPC_R_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
129 [RPC_R_SESSION] = { .name = "ubus_rpc_session",
130 .type = BLOBMSG_TYPE_STRING },
140 static const struct blobmsg_policy rpc_uci_order_policy[__RPC_O_MAX] = {
141 [RPC_O_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
142 [RPC_O_SECTIONS] = { .name = "sections", .type = BLOBMSG_TYPE_ARRAY },
143 [RPC_O_SESSION] = { .name = "ubus_rpc_session",
144 .type = BLOBMSG_TYPE_STRING },
153 static const struct blobmsg_policy rpc_uci_config_policy[__RPC_C_MAX] = {
154 [RPC_C_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
155 [RPC_C_SESSION] = { .name = "ubus_rpc_session",
156 .type = BLOBMSG_TYPE_STRING },
166 static const struct blobmsg_policy rpc_uci_apply_policy[__RPC_T_MAX] = {
167 [RPC_T_ROLLBACK] = { .name = "rollback", .type = BLOBMSG_TYPE_BOOL },
168 [RPC_T_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
169 [RPC_T_SESSION] = { .name = "ubus_rpc_session",
170 .type = BLOBMSG_TYPE_STRING },
178 static const struct blobmsg_policy rpc_uci_rollback_policy[__RPC_B_MAX] = {
179 [RPC_B_SESSION] = { .name = "ubus_rpc_session",
180 .type = BLOBMSG_TYPE_STRING },
184 * Turn uci error state into ubus return code
192 return UBUS_STATUS_OK;
195 return UBUS_STATUS_INVALID_ARGUMENT;
197 case UCI_ERR_NOTFOUND:
198 return UBUS_STATUS_NOT_FOUND;
201 return UBUS_STATUS_UNKNOWN_ERROR;
206 * Setup per-session delta save directory. If the passed "sid" blob attribute
207 * pointer is NULL then the precedure was not invoked through the ubus-rpc so
208 * we do not perform session isolation and use the default save directory.
211 rpc_uci_set_savedir(struct blob_attr *sid)
217 uci_set_savedir(cursor, "/tmp/.uci");
221 snprintf(path, sizeof(path) - 1,
222 RPC_UCI_SAVEDIR_PREFIX "%s", blobmsg_get_string(sid));
224 uci_set_savedir(cursor, path);
228 * Test read access to given config. If the passed "sid" blob attribute pointer
229 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
230 * perform access control and always assume true.
233 rpc_uci_read_access(struct blob_attr *sid, struct blob_attr *config)
235 rpc_uci_set_savedir(sid);
240 return rpc_session_access(blobmsg_data(sid), "uci",
241 blobmsg_data(config), "read");
245 * Test write access to given config. If the passed "sid" blob attribute pointer
246 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
247 * perform access control and always assume true.
250 rpc_uci_write_access(struct blob_attr *sid, struct blob_attr *config)
252 rpc_uci_set_savedir(sid);
257 return rpc_session_access(blobmsg_data(sid), "uci",
258 blobmsg_data(config), "write");
262 * Format applicable blob value as string and place a pointer to the string
263 * buffer in "p". Uses a static string buffer.
266 rpc_uci_format_blob(struct blob_attr *v, const char **p)
272 switch (blobmsg_type(v))
274 case BLOBMSG_TYPE_STRING:
275 if (blobmsg_data_len(v) > 1)
276 *p = blobmsg_data(v);
279 case BLOBMSG_TYPE_INT64:
280 snprintf(buf, sizeof(buf), "%"PRIu64, blobmsg_get_u64(v));
284 case BLOBMSG_TYPE_INT32:
285 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u32(v));
289 case BLOBMSG_TYPE_INT16:
290 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u16(v));
294 case BLOBMSG_TYPE_INT8:
295 snprintf(buf, sizeof(buf), "%u", !!blobmsg_get_u8(v));
307 * Lookup the given uci_ptr and enable extended lookup format if the .section
308 * value of the uci_ptr looks like extended syntax. Uses an internal copy
309 * of the given uci_ptr to perform the lookup as failing extended section
310 * lookup operations in libuci will zero our the uci_ptr struct.
311 * Copies the internal uci_ptr back to given the uci_ptr on success.
314 rpc_uci_lookup(struct uci_ptr *ptr)
317 struct uci_ptr lookup = *ptr;
319 if (!lookup.s && lookup.section && *lookup.section == '@')
320 lookup.flags |= UCI_LOOKUP_EXTENDED;
322 rv = uci_lookup_ptr(cursor, &lookup, NULL, true);
331 * Checks whether the given uci_option object matches the given string value.
332 * 1) If the uci_option is of type list, check whether any of the list elements
333 * equals to the given string
334 * 2) If the uci_option is of type string, parse it into space separated tokens
335 * and check if any of the tokens equals to the given string.
336 * Returns true if a list element or token matched the given string.
339 rpc_uci_match_option(struct uci_option *o, const char *cmp)
341 struct uci_element *e;
344 if (o->type == UCI_TYPE_LIST)
346 uci_foreach_element(&o->v.list, e)
347 if (e->name && !strcmp(e->name, cmp))
356 s = strdup(o->v.string);
361 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
375 * Checks whether the given uci_section matches the type and value blob attrs.
376 * 1) Returns false if "type" is given and the section type does not match
377 * the value specified in the "type" string blob attribute, else continue.
378 * 2) Tests whether any key in the "matches" table blob attribute exists in
379 * the given uci_section and whether each value is contained in the
380 * corresponding uci option value (see rpc_uci_match_option()).
381 * 3) A missing or empty "matches" table blob attribute is always considered
383 * Returns true if "type" matches or is NULL and "matches" matches or is NULL.
386 rpc_uci_match_section(struct uci_section *s,
387 struct blob_attr *type, struct blob_attr *matches)
389 struct uci_element *e;
390 struct blob_attr *cur;
396 if (type && strcmp(s->type, blobmsg_data(type)))
402 blobmsg_for_each_attr(cur, matches, rem)
404 if (!rpc_uci_format_blob(cur, &cmp))
407 uci_foreach_element(&s->options, e)
409 if (strcmp(e->name, blobmsg_name(cur)))
412 if (!rpc_uci_match_option(uci_to_option(e), cmp))
421 return (empty || match);
425 * Dump the given uci_option value into the global blobmsg buffer and use
426 * given "name" as key.
427 * 1) If the uci_option is of type list, put a table into the blob buffer and
428 * add each list item as string to it.
429 * 2) If the uci_option is of type string, put its value directly into the blob
433 rpc_uci_dump_option(struct uci_option *o, const char *name)
436 struct uci_element *e;
440 case UCI_TYPE_STRING:
441 blobmsg_add_string(&buf, name, o->v.string);
445 c = blobmsg_open_array(&buf, name);
447 uci_foreach_element(&o->v.list, e)
448 blobmsg_add_string(&buf, NULL, e->name);
450 blobmsg_close_array(&buf, c);
459 * Dump the given uci_section object into the global blobmsg buffer and use
460 * given "name" as key.
461 * Puts a table into the blob buffer and puts each section option member value
462 * as value into the table using the option name as key.
463 * Adds three special keys ".anonymous", ".type" and ".name" which specify the
464 * corresponding section properties.
467 rpc_uci_dump_section(struct uci_section *s, const char *name, int index)
470 struct uci_option *o;
471 struct uci_element *e;
473 c = blobmsg_open_table(&buf, name);
475 blobmsg_add_u8(&buf, ".anonymous", s->anonymous);
476 blobmsg_add_string(&buf, ".type", s->type);
477 blobmsg_add_string(&buf, ".name", s->e.name);
480 blobmsg_add_u32(&buf, ".index", index);
482 uci_foreach_element(&s->options, e)
484 o = uci_to_option(e);
485 rpc_uci_dump_option(o, o->e.name);
488 blobmsg_close_table(&buf, c);
492 * Dump the given uci_package object into the global blobmsg buffer and use
493 * given "name" as key.
494 * Puts a table into the blob buffer and puts each package section member as
495 * value into the table using the section name as key.
496 * Only dumps sections matching the given "type" and "matches", see explaination
497 * of rpc_uci_match_section() for details.
500 rpc_uci_dump_package(struct uci_package *p, const char *name,
501 struct blob_attr *type, struct blob_attr *matches)
504 struct uci_element *e;
507 c = blobmsg_open_table(&buf, name);
509 uci_foreach_element(&p->sections, e)
513 if (!rpc_uci_match_section(uci_to_section(e), type, matches))
516 rpc_uci_dump_section(uci_to_section(e), e->name, i);
519 blobmsg_close_table(&buf, c);
524 rpc_uci_getcommon(struct ubus_context *ctx, struct ubus_request_data *req,
525 struct blob_attr *msg, bool use_state)
527 struct blob_attr *tb[__RPC_G_MAX];
528 struct uci_package *p = NULL;
529 struct uci_ptr ptr = { 0 };
531 blobmsg_parse(rpc_uci_get_policy, __RPC_G_MAX, tb,
532 blob_data(msg), blob_len(msg));
534 if (!tb[RPC_G_CONFIG])
535 return UBUS_STATUS_INVALID_ARGUMENT;
537 if (!rpc_uci_read_access(tb[RPC_G_SESSION], tb[RPC_G_CONFIG]))
538 return UBUS_STATUS_PERMISSION_DENIED;
540 ptr.package = blobmsg_data(tb[RPC_G_CONFIG]);
543 uci_set_savedir(cursor, "/var/state");
545 if (uci_load(cursor, ptr.package, &p))
546 return rpc_uci_status();
548 if (tb[RPC_G_SECTION])
550 ptr.section = blobmsg_data(tb[RPC_G_SECTION]);
552 if (tb[RPC_G_OPTION])
553 ptr.option = blobmsg_data(tb[RPC_G_OPTION]);
556 if (rpc_uci_lookup(&ptr) || !(ptr.flags & UCI_LOOKUP_COMPLETE))
559 blob_buf_init(&buf, 0);
561 switch (ptr.last->type)
563 case UCI_TYPE_PACKAGE:
564 rpc_uci_dump_package(ptr.p, "values", tb[RPC_G_TYPE], tb[RPC_G_MATCH]);
567 case UCI_TYPE_SECTION:
568 rpc_uci_dump_section(ptr.s, "values", -1);
571 case UCI_TYPE_OPTION:
572 rpc_uci_dump_option(ptr.o, "value");
579 ubus_send_reply(ctx, req, buf.head);
582 uci_unload(cursor, p);
584 return rpc_uci_status();
588 rpc_uci_get(struct ubus_context *ctx, struct ubus_object *obj,
589 struct ubus_request_data *req, const char *method,
590 struct blob_attr *msg)
592 return rpc_uci_getcommon(ctx, req, msg, false);
596 rpc_uci_state(struct ubus_context *ctx, struct ubus_object *obj,
597 struct ubus_request_data *req, const char *method,
598 struct blob_attr *msg)
600 return rpc_uci_getcommon(ctx, req, msg, true);
604 rpc_uci_add(struct ubus_context *ctx, struct ubus_object *obj,
605 struct ubus_request_data *req, const char *method,
606 struct blob_attr *msg)
608 struct blob_attr *tb[__RPC_A_MAX];
609 struct blob_attr *cur, *elem;
610 struct uci_package *p = NULL;
611 struct uci_section *s;
612 struct uci_ptr ptr = { 0 };
615 blobmsg_parse(rpc_uci_add_policy, __RPC_A_MAX, tb,
616 blob_data(msg), blob_len(msg));
618 if (!tb[RPC_A_CONFIG] || !tb[RPC_A_TYPE])
619 return UBUS_STATUS_INVALID_ARGUMENT;
621 if (!rpc_uci_write_access(tb[RPC_A_SESSION], tb[RPC_A_CONFIG]))
622 return UBUS_STATUS_PERMISSION_DENIED;
624 ptr.package = blobmsg_data(tb[RPC_A_CONFIG]);
626 if (uci_load(cursor, ptr.package, &p))
627 return rpc_uci_status();
629 /* add named section */
632 ptr.section = blobmsg_data(tb[RPC_A_NAME]);
633 ptr.value = blobmsg_data(tb[RPC_A_TYPE]);
636 if (rpc_uci_lookup(&ptr) || uci_set(cursor, &ptr))
640 /* add anon section */
643 if (uci_add_section(cursor, p, blobmsg_data(tb[RPC_A_TYPE]), &s) || !s)
646 ptr.section = s->e.name;
649 if (tb[RPC_A_VALUES])
651 blobmsg_for_each_attr(cur, tb[RPC_A_VALUES], rem)
654 ptr.option = blobmsg_name(cur);
656 if (rpc_uci_lookup(&ptr) || !ptr.s)
659 switch (blobmsg_type(cur))
661 case BLOBMSG_TYPE_ARRAY:
662 blobmsg_for_each_attr(elem, cur, rem2)
663 if (rpc_uci_format_blob(elem, &ptr.value))
664 uci_add_list(cursor, &ptr);
668 if (rpc_uci_format_blob(cur, &ptr.value))
669 uci_set(cursor, &ptr);
677 blob_buf_init(&buf, 0);
678 blobmsg_add_string(&buf, "section", ptr.section);
679 ubus_send_reply(ctx, req, buf.head);
682 uci_unload(cursor, p);
684 return rpc_uci_status();
688 * Turn value from a blob attribute into uci set operation
689 * 1) if the blob is of type array, delete existing option (if any) and
690 * emit uci add_list operations for each element
691 * 2) if the blob is not an array but an option of type list exists,
692 * delete existing list and emit uci set operation for the blob value
693 * 3) in all other cases only emit a set operation if there is no existing
694 * option of if the existing options value differs from the blob value
697 rpc_uci_merge_set(struct blob_attr *opt, struct uci_ptr *ptr)
699 struct blob_attr *cur;
703 ptr->option = blobmsg_name(opt);
706 if (rpc_uci_lookup(ptr) || !ptr->s)
709 if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
712 uci_delete(cursor, ptr);
714 blobmsg_for_each_attr(cur, opt, rem)
715 if (rpc_uci_format_blob(cur, &ptr->value))
716 uci_add_list(cursor, ptr);
718 else if (ptr->o && ptr->o->type == UCI_TYPE_LIST)
720 uci_delete(cursor, ptr);
722 if (rpc_uci_format_blob(opt, &ptr->value))
723 uci_set(cursor, ptr);
725 else if (rpc_uci_format_blob(opt, &ptr->value))
727 if (!ptr->o || !ptr->o->v.string || strcmp(ptr->o->v.string, ptr->value))
728 uci_set(cursor, ptr);
733 rpc_uci_set(struct ubus_context *ctx, struct ubus_object *obj,
734 struct ubus_request_data *req, const char *method,
735 struct blob_attr *msg)
737 struct blob_attr *tb[__RPC_S_MAX];
738 struct blob_attr *cur;
739 struct uci_package *p = NULL;
740 struct uci_element *e;
741 struct uci_ptr ptr = { 0 };
744 blobmsg_parse(rpc_uci_set_policy, __RPC_S_MAX, tb,
745 blob_data(msg), blob_len(msg));
747 if (!tb[RPC_S_CONFIG] || !tb[RPC_S_VALUES] ||
748 (!tb[RPC_S_SECTION] && !tb[RPC_S_TYPE] && !tb[RPC_S_MATCH]))
749 return UBUS_STATUS_INVALID_ARGUMENT;
751 if (!rpc_uci_write_access(tb[RPC_S_SESSION], tb[RPC_S_CONFIG]))
752 return UBUS_STATUS_PERMISSION_DENIED;
754 ptr.package = blobmsg_data(tb[RPC_S_CONFIG]);
756 if (uci_load(cursor, ptr.package, &p))
757 return rpc_uci_status();
759 if (tb[RPC_S_SECTION])
761 ptr.section = blobmsg_data(tb[RPC_S_SECTION]);
762 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
763 rpc_uci_merge_set(cur, &ptr);
767 uci_foreach_element(&p->sections, e)
769 if (!rpc_uci_match_section(uci_to_section(e),
770 tb[RPC_S_TYPE], tb[RPC_S_MATCH]))
774 ptr.section = e->name;
776 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
777 rpc_uci_merge_set(cur, &ptr);
782 uci_unload(cursor, p);
784 return rpc_uci_status();
788 * Delete option or section from uci specified by given blob attribute pointer
789 * 1) if the blob is of type array, delete any option named after each element
790 * 2) if the blob is of type string, delete the option named after its value
791 * 3) if the blob is NULL, delete entire section
794 rpc_uci_merge_delete(struct blob_attr *opt, struct uci_ptr *ptr)
796 struct blob_attr *cur;
799 if (rpc_uci_lookup(ptr) || !ptr->s)
807 uci_delete(cursor, ptr);
809 else if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
811 blobmsg_for_each_attr(cur, opt, rem)
813 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
817 ptr->option = blobmsg_data(cur);
819 if (rpc_uci_lookup(ptr) || !ptr->o)
822 uci_delete(cursor, ptr);
825 else if (blobmsg_type(opt) == BLOBMSG_TYPE_STRING)
828 ptr->option = blobmsg_data(opt);
830 if (rpc_uci_lookup(ptr) || !ptr->o)
833 uci_delete(cursor, ptr);
838 rpc_uci_delete(struct ubus_context *ctx, struct ubus_object *obj,
839 struct ubus_request_data *req, const char *method,
840 struct blob_attr *msg)
842 struct blob_attr *tb[__RPC_D_MAX];
843 struct uci_package *p = NULL;
844 struct uci_element *e, *tmp;
845 struct uci_ptr ptr = { 0 };
847 blobmsg_parse(rpc_uci_delete_policy, __RPC_D_MAX, tb,
848 blob_data(msg), blob_len(msg));
850 if (!tb[RPC_D_CONFIG] ||
851 (!tb[RPC_D_SECTION] && !tb[RPC_D_TYPE] && !tb[RPC_D_MATCH]))
852 return UBUS_STATUS_INVALID_ARGUMENT;
854 if (!rpc_uci_write_access(tb[RPC_D_SESSION], tb[RPC_D_CONFIG]))
855 return UBUS_STATUS_PERMISSION_DENIED;
857 ptr.package = blobmsg_data(tb[RPC_D_CONFIG]);
859 if (uci_load(cursor, ptr.package, &p))
860 return rpc_uci_status();
862 if (tb[RPC_D_SECTION])
864 ptr.section = blobmsg_data(tb[RPC_D_SECTION]);
866 if (tb[RPC_D_OPTIONS])
867 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
869 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
873 uci_foreach_element_safe(&p->sections, tmp, e)
875 if (!rpc_uci_match_section(uci_to_section(e),
876 tb[RPC_D_TYPE], tb[RPC_D_MATCH]))
880 ptr.section = e->name;
882 if (tb[RPC_D_OPTIONS])
883 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
885 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
890 uci_unload(cursor, p);
892 return rpc_uci_status();
896 rpc_uci_rename(struct ubus_context *ctx, struct ubus_object *obj,
897 struct ubus_request_data *req, const char *method,
898 struct blob_attr *msg)
900 struct blob_attr *tb[__RPC_R_MAX];
901 struct uci_package *p = NULL;
902 struct uci_ptr ptr = { 0 };
904 blobmsg_parse(rpc_uci_rename_policy, __RPC_R_MAX, tb,
905 blob_data(msg), blob_len(msg));
907 if (!tb[RPC_R_CONFIG] || !tb[RPC_R_SECTION] || !tb[RPC_R_NAME])
908 return UBUS_STATUS_INVALID_ARGUMENT;
910 if (!rpc_uci_write_access(tb[RPC_R_SESSION], tb[RPC_R_CONFIG]))
911 return UBUS_STATUS_PERMISSION_DENIED;
913 ptr.package = blobmsg_data(tb[RPC_R_CONFIG]);
914 ptr.section = blobmsg_data(tb[RPC_R_SECTION]);
915 ptr.value = blobmsg_data(tb[RPC_R_NAME]);
917 if (tb[RPC_R_OPTION])
918 ptr.option = blobmsg_data(tb[RPC_R_OPTION]);
920 if (uci_load(cursor, ptr.package, &p))
921 return rpc_uci_status();
923 if (uci_lookup_ptr(cursor, &ptr, NULL, true))
926 if ((ptr.option && !ptr.o) || !ptr.s)
928 cursor->err = UCI_ERR_NOTFOUND;
932 if (uci_rename(cursor, &ptr))
938 uci_unload(cursor, p);
940 return rpc_uci_status();
944 rpc_uci_order(struct ubus_context *ctx, struct ubus_object *obj,
945 struct ubus_request_data *req, const char *method,
946 struct blob_attr *msg)
948 struct blob_attr *tb[__RPC_O_MAX];
949 struct blob_attr *cur;
950 struct uci_package *p = NULL;
951 struct uci_ptr ptr = { 0 };
954 blobmsg_parse(rpc_uci_order_policy, __RPC_O_MAX, tb,
955 blob_data(msg), blob_len(msg));
957 if (!tb[RPC_O_CONFIG] || !tb[RPC_O_SECTIONS])
958 return UBUS_STATUS_INVALID_ARGUMENT;
960 if (!rpc_uci_write_access(tb[RPC_O_SESSION], tb[RPC_O_CONFIG]))
961 return UBUS_STATUS_PERMISSION_DENIED;
963 ptr.package = blobmsg_data(tb[RPC_O_CONFIG]);
965 if (uci_load(cursor, ptr.package, &p))
966 return rpc_uci_status();
968 blobmsg_for_each_attr(cur, tb[RPC_O_SECTIONS], rem)
970 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
974 ptr.section = blobmsg_data(cur);
976 if (uci_lookup_ptr(cursor, &ptr, NULL, true) || !ptr.s)
979 uci_reorder_section(cursor, ptr.s, i++);
983 uci_unload(cursor, p);
985 return rpc_uci_status();
989 rpc_uci_dump_change(struct uci_delta *d)
992 const char *types[] = {
993 [UCI_CMD_REORDER] = "order",
994 [UCI_CMD_REMOVE] = "remove",
995 [UCI_CMD_RENAME] = "rename",
996 [UCI_CMD_ADD] = "add",
997 [UCI_CMD_LIST_ADD] = "list-add",
998 [UCI_CMD_LIST_DEL] = "list-del",
999 [UCI_CMD_CHANGE] = "set",
1005 c = blobmsg_open_array(&buf, NULL);
1007 blobmsg_add_string(&buf, NULL, types[d->cmd]);
1008 blobmsg_add_string(&buf, NULL, d->section);
1011 blobmsg_add_string(&buf, NULL, d->e.name);
1015 if (d->cmd == UCI_CMD_REORDER)
1016 blobmsg_add_u32(&buf, NULL, strtoul(d->value, NULL, 10));
1018 blobmsg_add_string(&buf, NULL, d->value);
1021 blobmsg_close_array(&buf, c);
1025 rpc_uci_changes(struct ubus_context *ctx, struct ubus_object *obj,
1026 struct ubus_request_data *req, const char *method,
1027 struct blob_attr *msg)
1029 struct blob_attr *tb[__RPC_C_MAX];
1030 struct uci_package *p = NULL;
1031 struct uci_element *e;
1036 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
1037 blob_data(msg), blob_len(msg));
1039 if (tb[RPC_C_CONFIG])
1041 if (!rpc_uci_read_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
1042 return UBUS_STATUS_PERMISSION_DENIED;
1044 if (uci_load(cursor, blobmsg_data(tb[RPC_C_CONFIG]), &p))
1045 return rpc_uci_status();
1047 blob_buf_init(&buf, 0);
1048 c = blobmsg_open_array(&buf, "changes");
1050 uci_foreach_element(&p->saved_delta, e)
1051 rpc_uci_dump_change(uci_to_delta(e));
1053 blobmsg_close_array(&buf, c);
1055 uci_unload(cursor, p);
1057 ubus_send_reply(ctx, req, buf.head);
1059 return rpc_uci_status();
1062 rpc_uci_set_savedir(tb[RPC_C_SESSION]);
1064 if (uci_list_configs(cursor, &configs))
1065 return rpc_uci_status();
1067 blob_buf_init(&buf, 0);
1069 c = blobmsg_open_table(&buf, "changes");
1071 for (i = 0; configs[i]; i++)
1073 if (tb[RPC_C_SESSION] &&
1074 !rpc_session_access(blobmsg_data(tb[RPC_C_SESSION]), "uci",
1075 configs[i], "read"))
1078 if (uci_load(cursor, configs[i], &p))
1081 if (!uci_list_empty(&p->saved_delta))
1083 d = blobmsg_open_array(&buf, configs[i]);
1085 uci_foreach_element(&p->saved_delta, e)
1086 rpc_uci_dump_change(uci_to_delta(e));
1088 blobmsg_close_array(&buf, d);
1091 uci_unload(cursor, p);
1094 blobmsg_close_table(&buf, c);
1096 ubus_send_reply(ctx, req, buf.head);
1102 rpc_uci_trigger_event(struct ubus_context *ctx, const char *config)
1104 char *pkg = strdup(config);
1105 static struct blob_buf b;
1108 if (!ubus_lookup_id(ctx, "service", &id)) {
1111 blob_buf_init(&b, 0);
1112 blobmsg_add_string(&b, "type", "config.change");
1113 c = blobmsg_open_table(&b, "data");
1114 blobmsg_add_string(&b, "package", pkg);
1115 blobmsg_close_table(&b, c);
1116 ubus_invoke(ctx, id, "event", b.head, NULL, 0, 1000);
1122 rpc_uci_revert_commit(struct ubus_context *ctx, struct blob_attr *msg, bool commit)
1124 struct blob_attr *tb[__RPC_C_MAX];
1125 struct uci_package *p = NULL;
1126 struct uci_ptr ptr = { 0 };
1129 return UBUS_STATUS_PERMISSION_DENIED;
1131 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
1132 blob_data(msg), blob_len(msg));
1134 if (!tb[RPC_C_CONFIG])
1135 return UBUS_STATUS_INVALID_ARGUMENT;
1137 if (!rpc_uci_write_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
1138 return UBUS_STATUS_PERMISSION_DENIED;
1140 ptr.package = blobmsg_data(tb[RPC_C_CONFIG]);
1144 if (!uci_load(cursor, ptr.package, &p))
1146 uci_commit(cursor, &p, false);
1147 uci_unload(cursor, p);
1148 rpc_uci_trigger_event(ctx, blobmsg_get_string(tb[RPC_C_CONFIG]));
1153 if (!uci_lookup_ptr(cursor, &ptr, NULL, true) && ptr.p)
1155 uci_revert(cursor, &ptr);
1156 uci_unload(cursor, ptr.p);
1160 return rpc_uci_status();
1164 rpc_uci_revert(struct ubus_context *ctx, struct ubus_object *obj,
1165 struct ubus_request_data *req, const char *method,
1166 struct blob_attr *msg)
1168 return rpc_uci_revert_commit(ctx, msg, false);
1172 rpc_uci_commit(struct ubus_context *ctx, struct ubus_object *obj,
1173 struct ubus_request_data *req, const char *method,
1174 struct blob_attr *msg)
1176 return rpc_uci_revert_commit(ctx, msg, true);
1180 rpc_uci_configs(struct ubus_context *ctx, struct ubus_object *obj,
1181 struct ubus_request_data *req, const char *method,
1182 struct blob_attr *msg)
1188 if (uci_list_configs(cursor, &configs))
1191 blob_buf_init(&buf, 0);
1193 c = blobmsg_open_array(&buf, "configs");
1195 for (i = 0; configs[i]; i++)
1196 blobmsg_add_string(&buf, NULL, configs[i]);
1198 blobmsg_close_array(&buf, c);
1200 ubus_send_reply(ctx, req, buf.head);
1203 return rpc_uci_status();
1208 * Remove given delta save directory (if any).
1211 rpc_uci_purge_dir(const char *path)
1216 char file[PATH_MAX];
1218 if (stat(path, &s) || !S_ISDIR(s.st_mode))
1221 if ((d = opendir(path)) != NULL)
1223 while ((e = readdir(d)) != NULL)
1225 snprintf(file, sizeof(file) - 1, "%s/%s", path, e->d_name);
1227 if (stat(file, &s) || !S_ISREG(s.st_mode))
1240 rpc_uci_apply_config(struct ubus_context *ctx, char *config)
1242 struct uci_package *p = NULL;
1244 if (!uci_load(cursor, config, &p)) {
1245 uci_commit(cursor, &p, false);
1246 uci_unload(cursor, p);
1248 rpc_uci_trigger_event(ctx, config);
1254 rpc_uci_copy_file(const char *src, const char *target, const char *file)
1259 snprintf(tmp, sizeof(tmp), "%s%s", src, file);
1260 in = fopen(tmp, "rb");
1261 snprintf(tmp, sizeof(tmp), "%s%s", target, file);
1262 out = fopen(tmp, "wb+");
1265 int len = fread(tmp, 1, sizeof(tmp), in);
1268 fwrite(tmp, 1, len, out);
1277 rpc_uci_do_rollback(struct ubus_context *ctx, const char *sid, glob_t *gl)
1283 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
1287 for (i = 0; i < gl->gl_pathc; i++) {
1288 char *config = basename(gl->gl_pathv[i]);
1293 rpc_uci_copy_file(RPC_SNAPSHOT_FILES, RPC_UCI_DIR, config);
1294 rpc_uci_apply_config(ctx, config);
1296 rpc_uci_copy_file(RPC_SNAPSHOT_DELTA, tmp, config);
1299 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1300 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1302 uloop_timeout_cancel(&apply_timer);
1303 memset(apply_sid, 0, sizeof(apply_sid));
1308 rpc_uci_apply_timeout(struct uloop_timeout *t)
1313 snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
1314 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1317 rpc_uci_do_rollback(apply_ctx, NULL, &gl);
1321 rpc_uci_apply_access(const char *sid, glob_t *gl)
1326 if (gl->gl_pathc < 3)
1327 return UBUS_STATUS_NO_DATA;
1329 for (i = 0; i < gl->gl_pathc; i++) {
1330 char *config = basename(gl->gl_pathv[i]);
1334 if (stat(gl->gl_pathv[i], &s) || !s.st_size)
1336 if (!rpc_session_access(sid, "uci", config, "write"))
1337 return UBUS_STATUS_PERMISSION_DENIED;
1342 return UBUS_STATUS_NO_DATA;
1348 rpc_uci_apply(struct ubus_context *ctx, struct ubus_object *obj,
1349 struct ubus_request_data *req, const char *method,
1350 struct blob_attr *msg)
1352 struct blob_attr *tb[__RPC_T_MAX];
1353 int timeout = RPC_APPLY_TIMEOUT;
1355 bool rollback = false;
1360 blobmsg_parse(rpc_uci_apply_policy, __RPC_T_MAX, tb,
1361 blob_data(msg), blob_len(msg));
1363 if (tb[RPC_T_ROLLBACK])
1364 rollback = blobmsg_get_bool(tb[RPC_T_ROLLBACK]);
1366 if (apply_sid[0] && rollback)
1367 return UBUS_STATUS_PERMISSION_DENIED;
1369 if (!tb[RPC_T_SESSION])
1370 return UBUS_STATUS_INVALID_ARGUMENT;
1372 sid = blobmsg_data(tb[RPC_T_SESSION]);
1374 if (tb[RPC_T_TIMEOUT])
1375 timeout = blobmsg_get_u32(tb[RPC_T_TIMEOUT]);
1377 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1378 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1380 if (!apply_sid[0]) {
1381 mkdir(RPC_SNAPSHOT_FILES, 0700);
1382 mkdir(RPC_SNAPSHOT_DELTA, 0700);
1384 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/*", sid);
1385 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1386 return UBUS_STATUS_NOT_FOUND;
1388 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
1390 ret = rpc_uci_apply_access(sid, &gl);
1396 /* copy SID early because rpc_uci_apply_config() will clobber buf */
1398 strncpy(apply_sid, sid, RPC_SID_LEN);
1400 for (i = 0; i < gl.gl_pathc; i++) {
1401 char *config = basename(gl.gl_pathv[i]);
1407 if (stat(gl.gl_pathv[i], &s) || !s.st_size)
1410 rpc_uci_copy_file(RPC_UCI_DIR, RPC_SNAPSHOT_FILES, config);
1411 rpc_uci_copy_file(tmp, RPC_SNAPSHOT_DELTA, config);
1412 rpc_uci_apply_config(ctx, config);
1418 apply_timer.cb = rpc_uci_apply_timeout;
1419 uloop_timeout_set(&apply_timer, timeout * 1000);
1428 rpc_uci_confirm(struct ubus_context *ctx, struct ubus_object *obj,
1429 struct ubus_request_data *req, const char *method,
1430 struct blob_attr *msg)
1432 struct blob_attr *tb[__RPC_B_MAX];
1435 blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
1436 blob_data(msg), blob_len(msg));
1438 if (!tb[RPC_B_SESSION])
1439 return UBUS_STATUS_INVALID_ARGUMENT;
1441 sid = blobmsg_data(tb[RPC_B_SESSION]);
1444 return UBUS_STATUS_NO_DATA;
1446 if (strcmp(apply_sid, sid))
1447 return UBUS_STATUS_PERMISSION_DENIED;
1449 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1450 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1452 uloop_timeout_cancel(&apply_timer);
1453 memset(apply_sid, 0, sizeof(apply_sid));
1460 rpc_uci_rollback(struct ubus_context *ctx, struct ubus_object *obj,
1461 struct ubus_request_data *req, const char *method,
1462 struct blob_attr *msg)
1464 struct blob_attr *tb[__RPC_B_MAX];
1469 blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
1470 blob_data(msg), blob_len(msg));
1473 return UBUS_STATUS_NO_DATA;
1475 if (!tb[RPC_B_SESSION])
1476 return UBUS_STATUS_INVALID_ARGUMENT;
1478 sid = blobmsg_data(tb[RPC_B_SESSION]);
1480 if (strcmp(apply_sid, sid))
1481 return UBUS_STATUS_PERMISSION_DENIED;
1483 snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
1484 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1485 return UBUS_STATUS_NOT_FOUND;
1487 rpc_uci_do_rollback(ctx, sid, &gl);
1495 rpc_uci_reload(struct ubus_context *ctx, struct ubus_object *obj,
1496 struct ubus_request_data *req, const char *method,
1497 struct blob_attr *msg)
1499 char * const cmd[2] = { "/sbin/reload_config", NULL };
1502 /* wait for the RPC call to complete */
1504 return execv(cmd[0], cmd);
1511 * Session destroy callback to purge associated delta directory.
1514 rpc_uci_purge_savedir_cb(struct rpc_session *ses, void *priv)
1516 char path[PATH_MAX];
1518 snprintf(path, sizeof(path) - 1, RPC_UCI_SAVEDIR_PREFIX "%s", ses->id);
1519 rpc_uci_purge_dir(path);
1523 * Removes all delta directories which match the RPC_UCI_SAVEDIR_PREFIX.
1524 * This is used to clean up garbage when starting rpcd.
1526 void rpc_uci_purge_savedirs(void)
1531 if (!glob(RPC_UCI_SAVEDIR_PREFIX "*", 0, NULL, &gl))
1533 for (i = 0; i < gl.gl_pathc; i++)
1534 rpc_uci_purge_dir(gl.gl_pathv[i]);
1540 int rpc_uci_api_init(struct ubus_context *ctx)
1542 static const struct ubus_method uci_methods[] = {
1543 { .name = "configs", .handler = rpc_uci_configs },
1544 UBUS_METHOD("get", rpc_uci_get, rpc_uci_get_policy),
1545 UBUS_METHOD("state", rpc_uci_state, rpc_uci_get_policy),
1546 UBUS_METHOD("add", rpc_uci_add, rpc_uci_add_policy),
1547 UBUS_METHOD("set", rpc_uci_set, rpc_uci_set_policy),
1548 UBUS_METHOD("delete", rpc_uci_delete, rpc_uci_delete_policy),
1549 UBUS_METHOD("rename", rpc_uci_rename, rpc_uci_rename_policy),
1550 UBUS_METHOD("order", rpc_uci_order, rpc_uci_order_policy),
1551 UBUS_METHOD("changes", rpc_uci_changes, rpc_uci_config_policy),
1552 UBUS_METHOD("revert", rpc_uci_revert, rpc_uci_config_policy),
1553 UBUS_METHOD("commit", rpc_uci_commit, rpc_uci_config_policy),
1554 UBUS_METHOD("apply", rpc_uci_apply, rpc_uci_apply_policy),
1555 UBUS_METHOD("confirm", rpc_uci_confirm, rpc_uci_rollback_policy),
1556 UBUS_METHOD("rollback", rpc_uci_rollback, rpc_uci_rollback_policy),
1557 UBUS_METHOD_NOARG("reload_config", rpc_uci_reload),
1560 static struct ubus_object_type uci_type =
1561 UBUS_OBJECT_TYPE("luci-rpc-uci", uci_methods);
1563 static struct ubus_object obj = {
1566 .methods = uci_methods,
1567 .n_methods = ARRAY_SIZE(uci_methods),
1570 static struct rpc_session_cb cb = {
1571 .cb = rpc_uci_purge_savedir_cb
1574 cursor = uci_alloc_context();
1577 return UBUS_STATUS_UNKNOWN_ERROR;
1579 rpc_session_destroy_cb(&cb);
1581 return ubus_add_object(ctx, &obj);