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;
34 char apply_sid[RPC_SID_LEN + 1];
46 static const struct blobmsg_policy rpc_uci_get_policy[__RPC_G_MAX] = {
47 [RPC_G_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
48 [RPC_G_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
49 [RPC_G_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
50 [RPC_G_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
51 [RPC_G_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
52 [RPC_G_SESSION] = { .name = "ubus_rpc_session",
53 .type = BLOBMSG_TYPE_STRING },
65 static const struct blobmsg_policy rpc_uci_add_policy[__RPC_A_MAX] = {
66 [RPC_A_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
67 [RPC_A_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
68 [RPC_A_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
69 [RPC_A_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
70 [RPC_A_SESSION] = { .name = "ubus_rpc_session",
71 .type = BLOBMSG_TYPE_STRING },
84 static const struct blobmsg_policy rpc_uci_set_policy[__RPC_S_MAX] = {
85 [RPC_S_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
86 [RPC_S_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
87 [RPC_S_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
88 [RPC_S_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
89 [RPC_S_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
90 [RPC_S_SESSION] = { .name = "ubus_rpc_session",
91 .type = BLOBMSG_TYPE_STRING },
105 static const struct blobmsg_policy rpc_uci_delete_policy[__RPC_D_MAX] = {
106 [RPC_D_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
107 [RPC_D_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
108 [RPC_D_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
109 [RPC_D_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
110 [RPC_D_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
111 [RPC_D_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_ARRAY },
112 [RPC_D_SESSION] = { .name = "ubus_rpc_session",
113 .type = BLOBMSG_TYPE_STRING },
125 static const struct blobmsg_policy rpc_uci_rename_policy[__RPC_R_MAX] = {
126 [RPC_R_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
127 [RPC_R_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
128 [RPC_R_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
129 [RPC_R_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
130 [RPC_R_SESSION] = { .name = "ubus_rpc_session",
131 .type = BLOBMSG_TYPE_STRING },
141 static const struct blobmsg_policy rpc_uci_order_policy[__RPC_O_MAX] = {
142 [RPC_O_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
143 [RPC_O_SECTIONS] = { .name = "sections", .type = BLOBMSG_TYPE_ARRAY },
144 [RPC_O_SESSION] = { .name = "ubus_rpc_session",
145 .type = BLOBMSG_TYPE_STRING },
154 static const struct blobmsg_policy rpc_uci_config_policy[__RPC_C_MAX] = {
155 [RPC_C_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
156 [RPC_C_SESSION] = { .name = "ubus_rpc_session",
157 .type = BLOBMSG_TYPE_STRING },
167 static const struct blobmsg_policy rpc_uci_apply_policy[__RPC_T_MAX] = {
168 [RPC_T_ROLLBACK] = { .name = "rollback", .type = BLOBMSG_TYPE_BOOL },
169 [RPC_T_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
170 [RPC_T_SESSION] = { .name = "ubus_rpc_session",
171 .type = BLOBMSG_TYPE_STRING },
179 static const struct blobmsg_policy rpc_uci_rollback_policy[__RPC_B_MAX] = {
180 [RPC_B_SESSION] = { .name = "ubus_rpc_session",
181 .type = BLOBMSG_TYPE_STRING },
185 * Turn uci error state into ubus return code
193 return UBUS_STATUS_OK;
196 return UBUS_STATUS_INVALID_ARGUMENT;
198 case UCI_ERR_NOTFOUND:
199 return UBUS_STATUS_NOT_FOUND;
202 return UBUS_STATUS_UNKNOWN_ERROR;
207 * Clear all save directories from the uci cursor and append the given path
208 * as new save directory.
211 rpc_uci_replace_savedir(const char *path)
213 struct uci_element *e, *tmp;
215 uci_foreach_element_safe(&cursor->delta_path, tmp, e) {
222 cursor->delta_path.prev = &cursor->delta_path;
223 cursor->delta_path.next = &cursor->delta_path;
226 uci_set_savedir(cursor, path);
230 * Setup per-session delta save directory. If the passed "sid" blob attribute
231 * pointer is NULL then the precedure was not invoked through the ubus-rpc so
232 * we do not perform session isolation and use the default save directory.
235 rpc_uci_set_savedir(struct blob_attr *sid)
241 rpc_uci_replace_savedir("/tmp/.uci");
245 snprintf(path, sizeof(path) - 1,
246 RPC_UCI_SAVEDIR_PREFIX "%s", blobmsg_get_string(sid));
248 rpc_uci_replace_savedir(path);
252 * Test read access to given config. If the passed "sid" blob attribute pointer
253 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
254 * perform access control and always assume true.
257 rpc_uci_read_access(struct blob_attr *sid, struct blob_attr *config)
259 rpc_uci_set_savedir(sid);
264 return rpc_session_access(blobmsg_data(sid), "uci",
265 blobmsg_data(config), "read");
269 * Test write access to given config. If the passed "sid" blob attribute pointer
270 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
271 * perform access control and always assume true.
274 rpc_uci_write_access(struct blob_attr *sid, struct blob_attr *config)
276 rpc_uci_set_savedir(sid);
281 return rpc_session_access(blobmsg_data(sid), "uci",
282 blobmsg_data(config), "write");
286 * Format applicable blob value as string and place a pointer to the string
287 * buffer in "p". Uses a static string buffer.
290 rpc_uci_format_blob(struct blob_attr *v, const char **p)
296 switch (blobmsg_type(v))
298 case BLOBMSG_TYPE_STRING:
299 *p = blobmsg_data(v);
302 case BLOBMSG_TYPE_INT64:
303 snprintf(buf, sizeof(buf), "%"PRIu64, blobmsg_get_u64(v));
307 case BLOBMSG_TYPE_INT32:
308 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u32(v));
312 case BLOBMSG_TYPE_INT16:
313 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u16(v));
317 case BLOBMSG_TYPE_INT8:
318 snprintf(buf, sizeof(buf), "%u", !!blobmsg_get_u8(v));
330 * Lookup the given uci_ptr and enable extended lookup format if the .section
331 * value of the uci_ptr looks like extended syntax. Uses an internal copy
332 * of the given uci_ptr to perform the lookup as failing extended section
333 * lookup operations in libuci will zero our the uci_ptr struct.
334 * Copies the internal uci_ptr back to given the uci_ptr on success.
337 rpc_uci_lookup(struct uci_ptr *ptr)
340 struct uci_ptr lookup = *ptr;
342 if (!lookup.s && lookup.section && *lookup.section == '@')
343 lookup.flags |= UCI_LOOKUP_EXTENDED;
345 rv = uci_lookup_ptr(cursor, &lookup, NULL, true);
354 * Checks whether the given uci_option object matches the given string value.
355 * 1) If the uci_option is of type list, check whether any of the list elements
356 * equals to the given string
357 * 2) If the uci_option is of type string, parse it into space separated tokens
358 * and check if any of the tokens equals to the given string.
359 * Returns true if a list element or token matched the given string.
362 rpc_uci_match_option(struct uci_option *o, const char *cmp)
364 struct uci_element *e;
367 if (o->type == UCI_TYPE_LIST)
369 uci_foreach_element(&o->v.list, e)
370 if (e->name && !strcmp(e->name, cmp))
379 s = strdup(o->v.string);
384 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
398 * Checks whether the given uci_section matches the type and value blob attrs.
399 * 1) Returns false if "type" is given and the section type does not match
400 * the value specified in the "type" string blob attribute, else continue.
401 * 2) Tests whether any key in the "matches" table blob attribute exists in
402 * the given uci_section and whether each value is contained in the
403 * corresponding uci option value (see rpc_uci_match_option()).
404 * 3) A missing or empty "matches" table blob attribute is always considered
406 * Returns true if "type" matches or is NULL and "matches" matches or is NULL.
409 rpc_uci_match_section(struct uci_section *s,
410 struct blob_attr *type, struct blob_attr *matches)
412 struct uci_element *e;
413 struct blob_attr *cur;
419 if (type && strcmp(s->type, blobmsg_data(type)))
425 blobmsg_for_each_attr(cur, matches, rem)
427 if (!rpc_uci_format_blob(cur, &cmp))
430 uci_foreach_element(&s->options, e)
432 if (strcmp(e->name, blobmsg_name(cur)))
435 if (!rpc_uci_match_option(uci_to_option(e), cmp))
444 return (empty || match);
448 * Dump the given uci_option value into the global blobmsg buffer and use
449 * given "name" as key.
450 * 1) If the uci_option is of type list, put a table into the blob buffer and
451 * add each list item as string to it.
452 * 2) If the uci_option is of type string, put its value directly into the blob
456 rpc_uci_dump_option(struct uci_option *o, const char *name)
459 struct uci_element *e;
463 case UCI_TYPE_STRING:
464 blobmsg_add_string(&buf, name, o->v.string);
468 c = blobmsg_open_array(&buf, name);
470 uci_foreach_element(&o->v.list, e)
471 blobmsg_add_string(&buf, NULL, e->name);
473 blobmsg_close_array(&buf, c);
482 * Dump the given uci_section object into the global blobmsg buffer and use
483 * given "name" as key.
484 * Puts a table into the blob buffer and puts each section option member value
485 * as value into the table using the option name as key.
486 * Adds three special keys ".anonymous", ".type" and ".name" which specify the
487 * corresponding section properties.
490 rpc_uci_dump_section(struct uci_section *s, const char *name, int index)
493 struct uci_option *o;
494 struct uci_element *e;
496 c = blobmsg_open_table(&buf, name);
498 blobmsg_add_u8(&buf, ".anonymous", s->anonymous);
499 blobmsg_add_string(&buf, ".type", s->type);
500 blobmsg_add_string(&buf, ".name", s->e.name);
503 blobmsg_add_u32(&buf, ".index", index);
505 uci_foreach_element(&s->options, e)
507 o = uci_to_option(e);
508 rpc_uci_dump_option(o, o->e.name);
511 blobmsg_close_table(&buf, c);
515 * Dump the given uci_package object into the global blobmsg buffer and use
516 * given "name" as key.
517 * Puts a table into the blob buffer and puts each package section member as
518 * value into the table using the section name as key.
519 * Only dumps sections matching the given "type" and "matches", see explaination
520 * of rpc_uci_match_section() for details.
523 rpc_uci_dump_package(struct uci_package *p, const char *name,
524 struct blob_attr *type, struct blob_attr *matches)
527 struct uci_element *e;
530 c = blobmsg_open_table(&buf, name);
532 uci_foreach_element(&p->sections, e)
536 if (!rpc_uci_match_section(uci_to_section(e), type, matches))
539 rpc_uci_dump_section(uci_to_section(e), e->name, i);
542 blobmsg_close_table(&buf, c);
547 rpc_uci_getcommon(struct ubus_context *ctx, struct ubus_request_data *req,
548 struct blob_attr *msg, bool use_state)
550 struct blob_attr *tb[__RPC_G_MAX];
551 struct uci_package *p = NULL;
552 struct uci_ptr ptr = { 0 };
554 blobmsg_parse(rpc_uci_get_policy, __RPC_G_MAX, tb,
555 blob_data(msg), blob_len(msg));
557 if (!tb[RPC_G_CONFIG])
558 return UBUS_STATUS_INVALID_ARGUMENT;
560 if (!rpc_uci_read_access(tb[RPC_G_SESSION], tb[RPC_G_CONFIG]))
561 return UBUS_STATUS_PERMISSION_DENIED;
563 ptr.package = blobmsg_data(tb[RPC_G_CONFIG]);
566 uci_set_savedir(cursor, "/var/state");
568 if (uci_load(cursor, ptr.package, &p))
569 return rpc_uci_status();
571 if (tb[RPC_G_SECTION])
573 ptr.section = blobmsg_data(tb[RPC_G_SECTION]);
575 if (tb[RPC_G_OPTION])
576 ptr.option = blobmsg_data(tb[RPC_G_OPTION]);
579 if (rpc_uci_lookup(&ptr) || !(ptr.flags & UCI_LOOKUP_COMPLETE))
582 blob_buf_init(&buf, 0);
584 switch (ptr.last->type)
586 case UCI_TYPE_PACKAGE:
587 rpc_uci_dump_package(ptr.p, "values", tb[RPC_G_TYPE], tb[RPC_G_MATCH]);
590 case UCI_TYPE_SECTION:
591 rpc_uci_dump_section(ptr.s, "values", -1);
594 case UCI_TYPE_OPTION:
595 rpc_uci_dump_option(ptr.o, "value");
602 ubus_send_reply(ctx, req, buf.head);
605 uci_unload(cursor, p);
607 return rpc_uci_status();
611 rpc_uci_get(struct ubus_context *ctx, struct ubus_object *obj,
612 struct ubus_request_data *req, const char *method,
613 struct blob_attr *msg)
615 return rpc_uci_getcommon(ctx, req, msg, false);
619 rpc_uci_state(struct ubus_context *ctx, struct ubus_object *obj,
620 struct ubus_request_data *req, const char *method,
621 struct blob_attr *msg)
623 return rpc_uci_getcommon(ctx, req, msg, true);
627 rpc_uci_add(struct ubus_context *ctx, struct ubus_object *obj,
628 struct ubus_request_data *req, const char *method,
629 struct blob_attr *msg)
631 struct blob_attr *tb[__RPC_A_MAX];
632 struct blob_attr *cur, *elem;
633 struct uci_package *p = NULL;
634 struct uci_section *s;
635 struct uci_ptr ptr = { 0 };
638 blobmsg_parse(rpc_uci_add_policy, __RPC_A_MAX, tb,
639 blob_data(msg), blob_len(msg));
641 if (!tb[RPC_A_CONFIG] || !tb[RPC_A_TYPE])
642 return UBUS_STATUS_INVALID_ARGUMENT;
644 if (!rpc_uci_write_access(tb[RPC_A_SESSION], tb[RPC_A_CONFIG]))
645 return UBUS_STATUS_PERMISSION_DENIED;
647 ptr.package = blobmsg_data(tb[RPC_A_CONFIG]);
649 if (uci_load(cursor, ptr.package, &p))
650 return rpc_uci_status();
652 /* add named section */
655 ptr.section = blobmsg_data(tb[RPC_A_NAME]);
656 ptr.value = blobmsg_data(tb[RPC_A_TYPE]);
659 if (rpc_uci_lookup(&ptr) || uci_set(cursor, &ptr))
663 /* add anon section */
666 if (uci_add_section(cursor, p, blobmsg_data(tb[RPC_A_TYPE]), &s) || !s)
669 ptr.section = s->e.name;
672 if (tb[RPC_A_VALUES])
674 blobmsg_for_each_attr(cur, tb[RPC_A_VALUES], rem)
677 ptr.option = blobmsg_name(cur);
679 if (rpc_uci_lookup(&ptr) || !ptr.s)
682 switch (blobmsg_type(cur))
684 case BLOBMSG_TYPE_ARRAY:
685 blobmsg_for_each_attr(elem, cur, rem2)
686 if (rpc_uci_format_blob(elem, &ptr.value))
687 uci_add_list(cursor, &ptr);
691 if (rpc_uci_format_blob(cur, &ptr.value))
692 uci_set(cursor, &ptr);
700 blob_buf_init(&buf, 0);
701 blobmsg_add_string(&buf, "section", ptr.section);
702 ubus_send_reply(ctx, req, buf.head);
705 uci_unload(cursor, p);
707 return rpc_uci_status();
711 * Turn value from a blob attribute into uci set operation
712 * 1) if the blob is of type array, delete existing option (if any) and
713 * emit uci add_list operations for each element
714 * 2) if the blob is not an array but an option of type list exists,
715 * delete existing list and emit uci set operation for the blob value
716 * 3) in all other cases only emit a set operation if there is no existing
717 * option of if the existing options value differs from the blob value
720 rpc_uci_merge_set(struct blob_attr *opt, struct uci_ptr *ptr)
722 struct blob_attr *cur;
726 ptr->option = blobmsg_name(opt);
729 if (rpc_uci_lookup(ptr) || !ptr->s)
732 if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
735 uci_delete(cursor, ptr);
737 blobmsg_for_each_attr(cur, opt, rem)
738 if (rpc_uci_format_blob(cur, &ptr->value))
739 uci_add_list(cursor, ptr);
741 else if (ptr->o && ptr->o->type == UCI_TYPE_LIST)
743 uci_delete(cursor, ptr);
745 if (rpc_uci_format_blob(opt, &ptr->value))
746 uci_set(cursor, ptr);
748 else if (rpc_uci_format_blob(opt, &ptr->value))
750 if (!ptr->o || !ptr->o->v.string || strcmp(ptr->o->v.string, ptr->value))
751 uci_set(cursor, ptr);
756 rpc_uci_set(struct ubus_context *ctx, struct ubus_object *obj,
757 struct ubus_request_data *req, const char *method,
758 struct blob_attr *msg)
760 struct blob_attr *tb[__RPC_S_MAX];
761 struct blob_attr *cur;
762 struct uci_package *p = NULL;
763 struct uci_element *e;
764 struct uci_ptr ptr = { 0 };
767 blobmsg_parse(rpc_uci_set_policy, __RPC_S_MAX, tb,
768 blob_data(msg), blob_len(msg));
770 if (!tb[RPC_S_CONFIG] || !tb[RPC_S_VALUES] ||
771 (!tb[RPC_S_SECTION] && !tb[RPC_S_TYPE] && !tb[RPC_S_MATCH]))
772 return UBUS_STATUS_INVALID_ARGUMENT;
774 if (!rpc_uci_write_access(tb[RPC_S_SESSION], tb[RPC_S_CONFIG]))
775 return UBUS_STATUS_PERMISSION_DENIED;
777 ptr.package = blobmsg_data(tb[RPC_S_CONFIG]);
779 if (uci_load(cursor, ptr.package, &p))
780 return rpc_uci_status();
782 if (tb[RPC_S_SECTION])
784 ptr.section = blobmsg_data(tb[RPC_S_SECTION]);
785 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
786 rpc_uci_merge_set(cur, &ptr);
790 uci_foreach_element(&p->sections, e)
792 if (!rpc_uci_match_section(uci_to_section(e),
793 tb[RPC_S_TYPE], tb[RPC_S_MATCH]))
797 ptr.section = e->name;
799 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
800 rpc_uci_merge_set(cur, &ptr);
805 uci_unload(cursor, p);
807 return rpc_uci_status();
811 * Delete option or section from uci specified by given blob attribute pointer
812 * 1) if the blob is of type array, delete any option named after each element
813 * 2) if the blob is of type string, delete the option named after its value
814 * 3) if the blob is NULL, delete entire section
817 rpc_uci_merge_delete(struct blob_attr *opt, struct uci_ptr *ptr)
819 struct blob_attr *cur;
822 if (rpc_uci_lookup(ptr) || !ptr->s)
830 uci_delete(cursor, ptr);
832 else if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
834 blobmsg_for_each_attr(cur, opt, rem)
836 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
840 ptr->option = blobmsg_data(cur);
842 if (rpc_uci_lookup(ptr) || !ptr->o)
845 uci_delete(cursor, ptr);
848 else if (blobmsg_type(opt) == BLOBMSG_TYPE_STRING)
851 ptr->option = blobmsg_data(opt);
853 if (rpc_uci_lookup(ptr) || !ptr->o)
856 uci_delete(cursor, ptr);
861 rpc_uci_delete(struct ubus_context *ctx, struct ubus_object *obj,
862 struct ubus_request_data *req, const char *method,
863 struct blob_attr *msg)
865 struct blob_attr *tb[__RPC_D_MAX];
866 struct uci_package *p = NULL;
867 struct uci_element *e, *tmp;
868 struct uci_ptr ptr = { 0 };
870 blobmsg_parse(rpc_uci_delete_policy, __RPC_D_MAX, tb,
871 blob_data(msg), blob_len(msg));
873 if (!tb[RPC_D_CONFIG] ||
874 (!tb[RPC_D_SECTION] && !tb[RPC_D_TYPE] && !tb[RPC_D_MATCH]))
875 return UBUS_STATUS_INVALID_ARGUMENT;
877 if (!rpc_uci_write_access(tb[RPC_D_SESSION], tb[RPC_D_CONFIG]))
878 return UBUS_STATUS_PERMISSION_DENIED;
880 ptr.package = blobmsg_data(tb[RPC_D_CONFIG]);
882 if (uci_load(cursor, ptr.package, &p))
883 return rpc_uci_status();
885 if (tb[RPC_D_SECTION])
887 ptr.section = blobmsg_data(tb[RPC_D_SECTION]);
889 if (tb[RPC_D_OPTIONS])
890 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
892 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
896 uci_foreach_element_safe(&p->sections, tmp, e)
898 if (!rpc_uci_match_section(uci_to_section(e),
899 tb[RPC_D_TYPE], tb[RPC_D_MATCH]))
903 ptr.section = e->name;
905 if (tb[RPC_D_OPTIONS])
906 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
908 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
913 uci_unload(cursor, p);
915 return rpc_uci_status();
919 rpc_uci_rename(struct ubus_context *ctx, struct ubus_object *obj,
920 struct ubus_request_data *req, const char *method,
921 struct blob_attr *msg)
923 struct blob_attr *tb[__RPC_R_MAX];
924 struct uci_package *p = NULL;
925 struct uci_ptr ptr = { 0 };
927 blobmsg_parse(rpc_uci_rename_policy, __RPC_R_MAX, tb,
928 blob_data(msg), blob_len(msg));
930 if (!tb[RPC_R_CONFIG] || !tb[RPC_R_SECTION] || !tb[RPC_R_NAME])
931 return UBUS_STATUS_INVALID_ARGUMENT;
933 if (!rpc_uci_write_access(tb[RPC_R_SESSION], tb[RPC_R_CONFIG]))
934 return UBUS_STATUS_PERMISSION_DENIED;
936 ptr.package = blobmsg_data(tb[RPC_R_CONFIG]);
937 ptr.section = blobmsg_data(tb[RPC_R_SECTION]);
938 ptr.value = blobmsg_data(tb[RPC_R_NAME]);
940 if (tb[RPC_R_OPTION])
941 ptr.option = blobmsg_data(tb[RPC_R_OPTION]);
943 if (uci_load(cursor, ptr.package, &p))
944 return rpc_uci_status();
946 if (uci_lookup_ptr(cursor, &ptr, NULL, true))
949 if ((ptr.option && !ptr.o) || !ptr.s)
951 cursor->err = UCI_ERR_NOTFOUND;
955 if (uci_rename(cursor, &ptr))
961 uci_unload(cursor, p);
963 return rpc_uci_status();
967 rpc_uci_order(struct ubus_context *ctx, struct ubus_object *obj,
968 struct ubus_request_data *req, const char *method,
969 struct blob_attr *msg)
971 struct blob_attr *tb[__RPC_O_MAX];
972 struct blob_attr *cur;
973 struct uci_package *p = NULL;
974 struct uci_ptr ptr = { 0 };
977 blobmsg_parse(rpc_uci_order_policy, __RPC_O_MAX, tb,
978 blob_data(msg), blob_len(msg));
980 if (!tb[RPC_O_CONFIG] || !tb[RPC_O_SECTIONS])
981 return UBUS_STATUS_INVALID_ARGUMENT;
983 if (!rpc_uci_write_access(tb[RPC_O_SESSION], tb[RPC_O_CONFIG]))
984 return UBUS_STATUS_PERMISSION_DENIED;
986 ptr.package = blobmsg_data(tb[RPC_O_CONFIG]);
988 if (uci_load(cursor, ptr.package, &p))
989 return rpc_uci_status();
991 blobmsg_for_each_attr(cur, tb[RPC_O_SECTIONS], rem)
993 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
997 ptr.section = blobmsg_data(cur);
999 if (uci_lookup_ptr(cursor, &ptr, NULL, true) || !ptr.s)
1002 uci_reorder_section(cursor, ptr.s, i++);
1005 uci_save(cursor, p);
1006 uci_unload(cursor, p);
1008 return rpc_uci_status();
1012 rpc_uci_dump_change(struct uci_delta *d)
1015 const char *types[] = {
1016 [UCI_CMD_REORDER] = "order",
1017 [UCI_CMD_REMOVE] = "remove",
1018 [UCI_CMD_RENAME] = "rename",
1019 [UCI_CMD_ADD] = "add",
1020 [UCI_CMD_LIST_ADD] = "list-add",
1021 [UCI_CMD_LIST_DEL] = "list-del",
1022 [UCI_CMD_CHANGE] = "set",
1028 c = blobmsg_open_array(&buf, NULL);
1030 blobmsg_add_string(&buf, NULL, types[d->cmd]);
1031 blobmsg_add_string(&buf, NULL, d->section);
1034 blobmsg_add_string(&buf, NULL, d->e.name);
1038 if (d->cmd == UCI_CMD_REORDER)
1039 blobmsg_add_u32(&buf, NULL, strtoul(d->value, NULL, 10));
1041 blobmsg_add_string(&buf, NULL, d->value);
1044 blobmsg_close_array(&buf, c);
1048 rpc_uci_changes(struct ubus_context *ctx, struct ubus_object *obj,
1049 struct ubus_request_data *req, const char *method,
1050 struct blob_attr *msg)
1052 struct blob_attr *tb[__RPC_C_MAX];
1053 struct uci_package *p = NULL;
1054 struct uci_element *e;
1059 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
1060 blob_data(msg), blob_len(msg));
1062 if (tb[RPC_C_CONFIG])
1064 if (!rpc_uci_read_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
1065 return UBUS_STATUS_PERMISSION_DENIED;
1067 if (uci_load(cursor, blobmsg_data(tb[RPC_C_CONFIG]), &p))
1068 return rpc_uci_status();
1070 blob_buf_init(&buf, 0);
1071 c = blobmsg_open_array(&buf, "changes");
1073 uci_foreach_element(&p->saved_delta, e)
1074 rpc_uci_dump_change(uci_to_delta(e));
1076 blobmsg_close_array(&buf, c);
1078 uci_unload(cursor, p);
1080 ubus_send_reply(ctx, req, buf.head);
1082 return rpc_uci_status();
1085 rpc_uci_set_savedir(tb[RPC_C_SESSION]);
1087 if (uci_list_configs(cursor, &configs))
1088 return rpc_uci_status();
1090 blob_buf_init(&buf, 0);
1092 c = blobmsg_open_table(&buf, "changes");
1094 for (i = 0; configs[i]; i++)
1096 if (tb[RPC_C_SESSION] &&
1097 !rpc_session_access(blobmsg_data(tb[RPC_C_SESSION]), "uci",
1098 configs[i], "read"))
1101 if (uci_load(cursor, configs[i], &p))
1104 if (!uci_list_empty(&p->saved_delta))
1106 d = blobmsg_open_array(&buf, configs[i]);
1108 uci_foreach_element(&p->saved_delta, e)
1109 rpc_uci_dump_change(uci_to_delta(e));
1111 blobmsg_close_array(&buf, d);
1114 uci_unload(cursor, p);
1117 blobmsg_close_table(&buf, c);
1119 ubus_send_reply(ctx, req, buf.head);
1125 rpc_uci_trigger_event(struct ubus_context *ctx, const char *config)
1127 char *pkg = strdup(config);
1128 static struct blob_buf b;
1131 if (!ubus_lookup_id(ctx, "service", &id)) {
1134 blob_buf_init(&b, 0);
1135 blobmsg_add_string(&b, "type", "config.change");
1136 c = blobmsg_open_table(&b, "data");
1137 blobmsg_add_string(&b, "package", pkg);
1138 blobmsg_close_table(&b, c);
1139 ubus_invoke(ctx, id, "event", b.head, NULL, 0, 1000);
1145 rpc_uci_revert_commit(struct ubus_context *ctx, struct blob_attr *msg, bool commit)
1147 struct blob_attr *tb[__RPC_C_MAX];
1148 struct uci_package *p = NULL;
1149 struct uci_ptr ptr = { 0 };
1152 return UBUS_STATUS_PERMISSION_DENIED;
1154 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
1155 blob_data(msg), blob_len(msg));
1157 if (!tb[RPC_C_CONFIG])
1158 return UBUS_STATUS_INVALID_ARGUMENT;
1160 if (!rpc_uci_write_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
1161 return UBUS_STATUS_PERMISSION_DENIED;
1163 ptr.package = blobmsg_data(tb[RPC_C_CONFIG]);
1167 if (!uci_load(cursor, ptr.package, &p))
1169 uci_commit(cursor, &p, false);
1170 uci_unload(cursor, p);
1171 rpc_uci_trigger_event(ctx, blobmsg_get_string(tb[RPC_C_CONFIG]));
1176 if (!uci_lookup_ptr(cursor, &ptr, NULL, true) && ptr.p)
1178 uci_revert(cursor, &ptr);
1179 uci_unload(cursor, ptr.p);
1183 return rpc_uci_status();
1187 rpc_uci_revert(struct ubus_context *ctx, struct ubus_object *obj,
1188 struct ubus_request_data *req, const char *method,
1189 struct blob_attr *msg)
1191 return rpc_uci_revert_commit(ctx, msg, false);
1195 rpc_uci_commit(struct ubus_context *ctx, struct ubus_object *obj,
1196 struct ubus_request_data *req, const char *method,
1197 struct blob_attr *msg)
1199 return rpc_uci_revert_commit(ctx, msg, true);
1203 rpc_uci_configs(struct ubus_context *ctx, struct ubus_object *obj,
1204 struct ubus_request_data *req, const char *method,
1205 struct blob_attr *msg)
1211 if (uci_list_configs(cursor, &configs))
1214 blob_buf_init(&buf, 0);
1216 c = blobmsg_open_array(&buf, "configs");
1218 for (i = 0; configs[i]; i++)
1219 blobmsg_add_string(&buf, NULL, configs[i]);
1221 blobmsg_close_array(&buf, c);
1223 ubus_send_reply(ctx, req, buf.head);
1226 return rpc_uci_status();
1231 * Remove given delta save directory (if any).
1234 rpc_uci_purge_dir(const char *path)
1239 char file[PATH_MAX];
1241 if (stat(path, &s) || !S_ISDIR(s.st_mode))
1244 if ((d = opendir(path)) != NULL)
1246 while ((e = readdir(d)) != NULL)
1248 snprintf(file, sizeof(file) - 1, "%s/%s", path, e->d_name);
1250 if (stat(file, &s) || !S_ISREG(s.st_mode))
1263 rpc_uci_apply_config(struct ubus_context *ctx, char *config)
1265 struct uci_package *p = NULL;
1267 if (!uci_load(cursor, config, &p)) {
1268 uci_commit(cursor, &p, false);
1269 uci_unload(cursor, p);
1271 rpc_uci_trigger_event(ctx, config);
1277 rpc_uci_copy_file(const char *src, const char *target, const char *file)
1282 snprintf(tmp, sizeof(tmp), "%s%s", src, file);
1283 in = fopen(tmp, "rb");
1284 snprintf(tmp, sizeof(tmp), "%s%s", target, file);
1285 out = fopen(tmp, "wb+");
1288 int len = fread(tmp, 1, sizeof(tmp), in);
1291 fwrite(tmp, 1, len, out);
1300 rpc_uci_apply_access(const char *sid, glob_t *gl)
1305 if (gl->gl_pathc < 3)
1306 return UBUS_STATUS_NO_DATA;
1308 for (i = 0; i < gl->gl_pathc; i++) {
1309 char *config = basename(gl->gl_pathv[i]);
1313 if (stat(gl->gl_pathv[i], &s) || !s.st_size)
1315 if (!rpc_session_access(sid, "uci", config, "write"))
1316 return UBUS_STATUS_PERMISSION_DENIED;
1321 return UBUS_STATUS_NO_DATA;
1327 rpc_uci_do_rollback(struct ubus_context *ctx, glob_t *gl)
1332 /* Test apply permission to see if the initiator session still exists.
1333 * If it does, restore the delta files as well, else just restore the
1334 * main configuration files. */
1336 ? rpc_uci_apply_access(apply_sid, gl) : UBUS_STATUS_NOT_FOUND;
1339 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", apply_sid);
1343 /* avoid merging unrelated uci changes when restoring old configs */
1344 rpc_uci_replace_savedir("/dev/null");
1346 for (i = 0; i < gl->gl_pathc; i++) {
1347 char *config = basename(gl->gl_pathv[i]);
1352 rpc_uci_copy_file(RPC_SNAPSHOT_FILES, RPC_UCI_DIR, config);
1353 rpc_uci_apply_config(ctx, config);
1358 rpc_uci_copy_file(RPC_SNAPSHOT_DELTA, tmp, config);
1361 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1362 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1364 uloop_timeout_cancel(&apply_timer);
1365 memset(apply_sid, 0, sizeof(apply_sid));
1370 rpc_uci_apply_timeout(struct uloop_timeout *t)
1375 snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
1376 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1379 rpc_uci_do_rollback(apply_ctx, &gl);
1385 rpc_uci_apply(struct ubus_context *ctx, struct ubus_object *obj,
1386 struct ubus_request_data *req, const char *method,
1387 struct blob_attr *msg)
1389 struct blob_attr *tb[__RPC_T_MAX];
1390 int timeout = RPC_APPLY_TIMEOUT;
1392 bool rollback = false;
1397 blobmsg_parse(rpc_uci_apply_policy, __RPC_T_MAX, tb,
1398 blob_data(msg), blob_len(msg));
1400 if (tb[RPC_T_ROLLBACK])
1401 rollback = blobmsg_get_bool(tb[RPC_T_ROLLBACK]);
1403 if (apply_sid[0] && rollback)
1404 return UBUS_STATUS_PERMISSION_DENIED;
1406 if (!tb[RPC_T_SESSION])
1407 return UBUS_STATUS_INVALID_ARGUMENT;
1409 sid = blobmsg_data(tb[RPC_T_SESSION]);
1411 if (tb[RPC_T_TIMEOUT])
1412 timeout = blobmsg_get_u32(tb[RPC_T_TIMEOUT]);
1414 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1415 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1417 if (!apply_sid[0]) {
1418 rpc_uci_set_savedir(tb[RPC_T_SESSION]);
1420 mkdir(RPC_SNAPSHOT_FILES, 0700);
1421 mkdir(RPC_SNAPSHOT_DELTA, 0700);
1423 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/*", sid);
1424 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1425 return UBUS_STATUS_NOT_FOUND;
1427 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
1429 ret = rpc_uci_apply_access(sid, &gl);
1435 /* copy SID early because rpc_uci_apply_config() will clobber buf */
1437 strncpy(apply_sid, sid, RPC_SID_LEN);
1439 for (i = 0; i < gl.gl_pathc; i++) {
1440 char *config = basename(gl.gl_pathv[i]);
1446 if (stat(gl.gl_pathv[i], &s) || !s.st_size)
1449 rpc_uci_copy_file(RPC_UCI_DIR, RPC_SNAPSHOT_FILES, config);
1450 rpc_uci_copy_file(tmp, RPC_SNAPSHOT_DELTA, config);
1451 rpc_uci_apply_config(ctx, config);
1457 apply_timer.cb = rpc_uci_apply_timeout;
1458 uloop_timeout_set(&apply_timer, timeout * 1000);
1467 rpc_uci_confirm(struct ubus_context *ctx, struct ubus_object *obj,
1468 struct ubus_request_data *req, const char *method,
1469 struct blob_attr *msg)
1471 struct blob_attr *tb[__RPC_B_MAX];
1474 blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
1475 blob_data(msg), blob_len(msg));
1477 if (!tb[RPC_B_SESSION])
1478 return UBUS_STATUS_INVALID_ARGUMENT;
1480 sid = blobmsg_data(tb[RPC_B_SESSION]);
1483 return UBUS_STATUS_NO_DATA;
1485 if (strcmp(apply_sid, sid))
1486 return UBUS_STATUS_PERMISSION_DENIED;
1488 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1489 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1491 uloop_timeout_cancel(&apply_timer);
1492 memset(apply_sid, 0, sizeof(apply_sid));
1499 rpc_uci_rollback(struct ubus_context *ctx, struct ubus_object *obj,
1500 struct ubus_request_data *req, const char *method,
1501 struct blob_attr *msg)
1503 struct blob_attr *tb[__RPC_B_MAX];
1508 blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
1509 blob_data(msg), blob_len(msg));
1512 return UBUS_STATUS_NO_DATA;
1514 if (!tb[RPC_B_SESSION])
1515 return UBUS_STATUS_INVALID_ARGUMENT;
1517 sid = blobmsg_data(tb[RPC_B_SESSION]);
1519 if (strcmp(apply_sid, sid))
1520 return UBUS_STATUS_PERMISSION_DENIED;
1522 snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
1523 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1524 return UBUS_STATUS_NOT_FOUND;
1526 rpc_uci_do_rollback(ctx, &gl);
1534 rpc_uci_reload(struct ubus_context *ctx, struct ubus_object *obj,
1535 struct ubus_request_data *req, const char *method,
1536 struct blob_attr *msg)
1538 char * const cmd[2] = { "/sbin/reload_config", NULL };
1541 /* wait for the RPC call to complete */
1543 return execv(cmd[0], cmd);
1550 * Session destroy callback to purge associated delta directory.
1553 rpc_uci_purge_savedir_cb(struct rpc_session *ses, void *priv)
1555 char path[PATH_MAX];
1557 snprintf(path, sizeof(path) - 1, RPC_UCI_SAVEDIR_PREFIX "%s", ses->id);
1558 rpc_uci_purge_dir(path);
1562 * Removes all delta directories which match the RPC_UCI_SAVEDIR_PREFIX.
1563 * This is used to clean up garbage when starting rpcd.
1565 void rpc_uci_purge_savedirs(void)
1570 if (!glob(RPC_UCI_SAVEDIR_PREFIX "*", 0, NULL, &gl))
1572 for (i = 0; i < gl.gl_pathc; i++)
1573 rpc_uci_purge_dir(gl.gl_pathv[i]);
1579 int rpc_uci_api_init(struct ubus_context *ctx)
1581 static const struct ubus_method uci_methods[] = {
1582 { .name = "configs", .handler = rpc_uci_configs },
1583 UBUS_METHOD("get", rpc_uci_get, rpc_uci_get_policy),
1584 UBUS_METHOD("state", rpc_uci_state, rpc_uci_get_policy),
1585 UBUS_METHOD("add", rpc_uci_add, rpc_uci_add_policy),
1586 UBUS_METHOD("set", rpc_uci_set, rpc_uci_set_policy),
1587 UBUS_METHOD("delete", rpc_uci_delete, rpc_uci_delete_policy),
1588 UBUS_METHOD("rename", rpc_uci_rename, rpc_uci_rename_policy),
1589 UBUS_METHOD("order", rpc_uci_order, rpc_uci_order_policy),
1590 UBUS_METHOD("changes", rpc_uci_changes, rpc_uci_config_policy),
1591 UBUS_METHOD("revert", rpc_uci_revert, rpc_uci_config_policy),
1592 UBUS_METHOD("commit", rpc_uci_commit, rpc_uci_config_policy),
1593 UBUS_METHOD("apply", rpc_uci_apply, rpc_uci_apply_policy),
1594 UBUS_METHOD("confirm", rpc_uci_confirm, rpc_uci_rollback_policy),
1595 UBUS_METHOD("rollback", rpc_uci_rollback, rpc_uci_rollback_policy),
1596 UBUS_METHOD_NOARG("reload_config", rpc_uci_reload),
1599 static struct ubus_object_type uci_type =
1600 UBUS_OBJECT_TYPE("luci-rpc-uci", uci_methods);
1602 static struct ubus_object obj = {
1605 .methods = uci_methods,
1606 .n_methods = ARRAY_SIZE(uci_methods),
1609 static struct rpc_session_cb cb = {
1610 .cb = rpc_uci_purge_savedir_cb
1613 cursor = uci_alloc_context();
1616 return UBUS_STATUS_UNKNOWN_ERROR;
1618 rpc_session_destroy_cb(&cb);
1620 return ubus_add_object(ctx, &obj);