2 * luci-rpcd - LuCI UBUS RPC server
4 * Copyright (C) 2013 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 static struct blob_buf buf;
23 static struct uci_context *cursor;
35 static const struct blobmsg_policy rpc_uci_get_policy[__RPC_G_MAX] = {
36 [RPC_G_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
37 [RPC_G_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
38 [RPC_G_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
39 [RPC_G_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
40 [RPC_G_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
41 [RPC_G_SESSION] = { .name = "ubus_rpc_session",
42 .type = BLOBMSG_TYPE_STRING },
54 static const struct blobmsg_policy rpc_uci_add_policy[__RPC_A_MAX] = {
55 [RPC_A_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
56 [RPC_A_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
57 [RPC_A_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
58 [RPC_A_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
59 [RPC_A_SESSION] = { .name = "ubus_rpc_session",
60 .type = BLOBMSG_TYPE_STRING },
73 static const struct blobmsg_policy rpc_uci_set_policy[__RPC_S_MAX] = {
74 [RPC_S_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
75 [RPC_S_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
76 [RPC_S_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
77 [RPC_S_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
78 [RPC_S_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
79 [RPC_S_SESSION] = { .name = "ubus_rpc_session",
80 .type = BLOBMSG_TYPE_STRING },
94 static const struct blobmsg_policy rpc_uci_delete_policy[__RPC_D_MAX] = {
95 [RPC_D_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
96 [RPC_D_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
97 [RPC_D_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
98 [RPC_D_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
99 [RPC_D_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
100 [RPC_D_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_ARRAY },
101 [RPC_D_SESSION] = { .name = "ubus_rpc_session",
102 .type = BLOBMSG_TYPE_STRING },
114 static const struct blobmsg_policy rpc_uci_rename_policy[__RPC_R_MAX] = {
115 [RPC_R_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
116 [RPC_R_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
117 [RPC_R_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
118 [RPC_R_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
119 [RPC_R_SESSION] = { .name = "ubus_rpc_session",
120 .type = BLOBMSG_TYPE_STRING },
130 static const struct blobmsg_policy rpc_uci_order_policy[__RPC_O_MAX] = {
131 [RPC_O_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
132 [RPC_O_SECTIONS] = { .name = "sections", .type = BLOBMSG_TYPE_ARRAY },
133 [RPC_O_SESSION] = { .name = "ubus_rpc_session",
134 .type = BLOBMSG_TYPE_STRING },
143 static const struct blobmsg_policy rpc_uci_config_policy[__RPC_C_MAX] = {
144 [RPC_C_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
145 [RPC_C_SESSION] = { .name = "ubus_rpc_session",
146 .type = BLOBMSG_TYPE_STRING },
150 * Turn uci error state into ubus return code
158 return UBUS_STATUS_OK;
161 return UBUS_STATUS_INVALID_ARGUMENT;
163 case UCI_ERR_NOTFOUND:
164 return UBUS_STATUS_NOT_FOUND;
167 return UBUS_STATUS_UNKNOWN_ERROR;
172 * Test read access to given config. If the passed "sid" blob attribute pointer
173 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
174 * perform access control and always assume true.
177 rpc_uci_read_access(struct blob_attr *sid, struct blob_attr *config)
182 return rpc_session_access(blobmsg_data(sid), "uci",
183 blobmsg_data(config), "read");
187 * Test write access to given config. If the passed "sid" blob attribute pointer
188 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
189 * perform access control and always assume true.
192 rpc_uci_write_access(struct blob_attr *sid, struct blob_attr *config)
197 return rpc_session_access(blobmsg_data(sid), "uci",
198 blobmsg_data(config), "write");
202 * Format applicable blob value as string and place a pointer to the string
203 * buffer in "p". Uses a static string buffer.
206 rpc_uci_format_blob(struct blob_attr *v, const char **p)
212 switch (blobmsg_type(v))
214 case BLOBMSG_TYPE_STRING:
215 if (blobmsg_data_len(v) > 1)
216 *p = blobmsg_data(v);
219 case BLOBMSG_TYPE_INT64:
220 snprintf(buf, sizeof(buf), "%"PRIu64, blobmsg_get_u64(v));
224 case BLOBMSG_TYPE_INT32:
225 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u32(v));
229 case BLOBMSG_TYPE_INT16:
230 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u16(v));
234 case BLOBMSG_TYPE_INT8:
235 snprintf(buf, sizeof(buf), "%u", !!blobmsg_get_u8(v));
247 * Lookup the given uci_ptr and enable extended lookup format if the .section
248 * value of the uci_ptr looks like extended syntax. Uses an internal copy
249 * of the given uci_ptr to perform the lookup as failing extended section
250 * lookup operations in libuci will zero our the uci_ptr struct.
251 * Copies the internal uci_ptr back to given the uci_ptr on success.
254 rpc_uci_lookup(struct uci_ptr *ptr)
257 struct uci_ptr lookup = *ptr;
259 if (!lookup.s && lookup.section && *lookup.section == '@')
260 lookup.flags |= UCI_LOOKUP_EXTENDED;
262 rv = uci_lookup_ptr(cursor, &lookup, NULL, true);
271 * Checks whether the given uci_option object matches the given string value.
272 * 1) If the uci_option is of type list, check whether any of the list elements
273 * equals to the given string
274 * 2) If the uci_option is of type string, parse it into space separated tokens
275 * and check if any of the tokens equals to the given string.
276 * Returns true if a list element or token matched the given string.
279 rpc_uci_match_option(struct uci_option *o, const char *cmp)
281 struct uci_element *e;
284 if (o->type == UCI_TYPE_LIST)
286 uci_foreach_element(&o->v.list, e)
287 if (e->name && !strcmp(e->name, cmp))
296 s = strdup(o->v.string);
301 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
315 * Checks whether the given uci_section matches the type and value blob attrs.
316 * 1) Returns false if "type" is given and the section type does not match
317 * the value specified in the "type" string blob attribute, else continue.
318 * 2) Tests whether any key in the "matches" table blob attribute exists in
319 * the given uci_section and whether each value is contained in the
320 * corresponding uci option value (see rpc_uci_match_option()).
321 * 3) A missing or empty "matches" table blob attribute is always considered
323 * Returns true if "type" matches or is NULL and "matches" matches or is NULL.
326 rpc_uci_match_section(struct uci_section *s,
327 struct blob_attr *type, struct blob_attr *matches)
329 struct uci_element *e;
330 struct blob_attr *cur;
336 if (type && strcmp(s->type, blobmsg_data(type)))
342 blobmsg_for_each_attr(cur, matches, rem)
344 if (!rpc_uci_format_blob(cur, &cmp))
347 uci_foreach_element(&s->options, e)
349 if (strcmp(e->name, blobmsg_name(cur)))
352 if (!rpc_uci_match_option(uci_to_option(e), cmp))
361 return (empty || match);
365 * Dump the given uci_option value into the global blobmsg buffer and use
366 * given "name" as key.
367 * 1) If the uci_option is of type list, put a table into the blob buffer and
368 * add each list item as string to it.
369 * 2) If the uci_option is of type string, put its value directly into the blob
373 rpc_uci_dump_option(struct uci_option *o, const char *name)
376 struct uci_element *e;
380 case UCI_TYPE_STRING:
381 blobmsg_add_string(&buf, name, o->v.string);
385 c = blobmsg_open_array(&buf, name);
387 uci_foreach_element(&o->v.list, e)
388 blobmsg_add_string(&buf, NULL, e->name);
390 blobmsg_close_array(&buf, c);
399 * Dump the given uci_section object into the global blobmsg buffer and use
400 * given "name" as key.
401 * Puts a table into the blob buffer and puts each section option member value
402 * as value into the table using the option name as key.
403 * Adds three special keys ".anonymous", ".type" and ".name" which specify the
404 * corresponding section properties.
407 rpc_uci_dump_section(struct uci_section *s, const char *name, int index)
410 struct uci_option *o;
411 struct uci_element *e;
413 c = blobmsg_open_table(&buf, name);
415 blobmsg_add_u8(&buf, ".anonymous", s->anonymous);
416 blobmsg_add_string(&buf, ".type", s->type);
417 blobmsg_add_string(&buf, ".name", s->e.name);
420 blobmsg_add_u32(&buf, ".index", index);
422 uci_foreach_element(&s->options, e)
424 o = uci_to_option(e);
425 rpc_uci_dump_option(o, o->e.name);
428 blobmsg_close_table(&buf, c);
432 * Dump the given uci_package object into the global blobmsg buffer and use
433 * given "name" as key.
434 * Puts a table into the blob buffer and puts each package section member as
435 * value into the table using the section name as key.
436 * Only dumps sections matching the given "type" and "matches", see explaination
437 * of rpc_uci_match_section() for details.
440 rpc_uci_dump_package(struct uci_package *p, const char *name,
441 struct blob_attr *type, struct blob_attr *matches)
444 struct uci_element *e;
447 c = blobmsg_open_table(&buf, name);
449 uci_foreach_element(&p->sections, e)
453 if (!rpc_uci_match_section(uci_to_section(e), type, matches))
456 rpc_uci_dump_section(uci_to_section(e), e->name, i);
459 blobmsg_close_table(&buf, c);
464 rpc_uci_get(struct ubus_context *ctx, struct ubus_object *obj,
465 struct ubus_request_data *req, const char *method,
466 struct blob_attr *msg)
468 struct blob_attr *tb[__RPC_G_MAX];
469 struct uci_package *p = NULL;
470 struct uci_ptr ptr = { 0 };
472 blobmsg_parse(rpc_uci_get_policy, __RPC_G_MAX, tb,
473 blob_data(msg), blob_len(msg));
475 if (!tb[RPC_G_CONFIG])
476 return UBUS_STATUS_INVALID_ARGUMENT;
478 if (!rpc_uci_read_access(tb[RPC_G_SESSION], tb[RPC_G_CONFIG]))
479 return UBUS_STATUS_PERMISSION_DENIED;
481 ptr.package = blobmsg_data(tb[RPC_G_CONFIG]);
482 uci_load(cursor, ptr.package, &p);
487 if (tb[RPC_G_SECTION])
489 ptr.section = blobmsg_data(tb[RPC_G_SECTION]);
491 if (tb[RPC_G_OPTION])
492 ptr.option = blobmsg_data(tb[RPC_G_OPTION]);
495 if (rpc_uci_lookup(&ptr) || !(ptr.flags & UCI_LOOKUP_COMPLETE))
498 blob_buf_init(&buf, 0);
500 switch (ptr.last->type)
502 case UCI_TYPE_PACKAGE:
503 rpc_uci_dump_package(ptr.p, "values", tb[RPC_G_TYPE], tb[RPC_G_MATCH]);
506 case UCI_TYPE_SECTION:
507 rpc_uci_dump_section(ptr.s, "values", -1);
510 case UCI_TYPE_OPTION:
511 rpc_uci_dump_option(ptr.o, "value");
518 ubus_send_reply(ctx, req, buf.head);
522 uci_unload(cursor, p);
524 return rpc_uci_status();
528 rpc_uci_add(struct ubus_context *ctx, struct ubus_object *obj,
529 struct ubus_request_data *req, const char *method,
530 struct blob_attr *msg)
532 struct blob_attr *tb[__RPC_A_MAX];
533 struct blob_attr *cur, *elem;
534 struct uci_package *p = NULL;
535 struct uci_section *s;
536 struct uci_ptr ptr = { 0 };
539 blobmsg_parse(rpc_uci_add_policy, __RPC_A_MAX, tb,
540 blob_data(msg), blob_len(msg));
542 if (!tb[RPC_A_CONFIG] || !tb[RPC_A_TYPE])
543 return UBUS_STATUS_INVALID_ARGUMENT;
545 if (!rpc_uci_write_access(tb[RPC_A_SESSION], tb[RPC_A_CONFIG]))
546 return UBUS_STATUS_PERMISSION_DENIED;
548 ptr.package = blobmsg_data(tb[RPC_A_CONFIG]);
550 uci_load(cursor, ptr.package, &p);
555 /* add named section */
558 ptr.section = blobmsg_data(tb[RPC_A_NAME]);
559 ptr.value = blobmsg_data(tb[RPC_A_TYPE]);
562 if (rpc_uci_lookup(&ptr) || uci_set(cursor, &ptr))
566 /* add anon section */
569 if (uci_add_section(cursor, p, blobmsg_data(tb[RPC_A_TYPE]), &s) || !s)
572 ptr.section = s->e.name;
575 if (tb[RPC_A_VALUES])
577 blobmsg_for_each_attr(cur, tb[RPC_A_VALUES], rem)
580 ptr.option = blobmsg_name(cur);
582 if (rpc_uci_lookup(&ptr) || !ptr.s)
585 switch (blobmsg_type(cur))
587 case BLOBMSG_TYPE_ARRAY:
588 blobmsg_for_each_attr(elem, cur, rem2)
589 if (rpc_uci_format_blob(elem, &ptr.value))
590 uci_add_list(cursor, &ptr);
594 if (rpc_uci_format_blob(cur, &ptr.value))
595 uci_set(cursor, &ptr);
603 blob_buf_init(&buf, 0);
604 blobmsg_add_string(&buf, "section", ptr.section);
605 ubus_send_reply(ctx, req, buf.head);
609 uci_unload(cursor, p);
611 return rpc_uci_status();
615 * Turn value from a blob attribute into uci set operation
616 * 1) if the blob is of type array, delete existing option (if any) and
617 * emit uci add_list operations for each element
618 * 2) if the blob is not an array but an option of type list exists,
619 * delete existing list and emit uci set operation for the blob value
620 * 3) in all other cases only emit a set operation if there is no existing
621 * option of if the existing options value differs from the blob value
624 rpc_uci_merge_set(struct blob_attr *opt, struct uci_ptr *ptr)
626 struct blob_attr *cur;
630 ptr->option = blobmsg_name(opt);
632 if (rpc_uci_lookup(ptr) || !ptr->s)
635 if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
638 uci_delete(cursor, ptr);
640 blobmsg_for_each_attr(cur, opt, rem)
641 if (rpc_uci_format_blob(cur, &ptr->value))
642 uci_add_list(cursor, ptr);
644 else if (ptr->o && ptr->o->type == UCI_TYPE_LIST)
646 uci_delete(cursor, ptr);
648 if (rpc_uci_format_blob(opt, &ptr->value))
649 uci_set(cursor, ptr);
651 else if (rpc_uci_format_blob(opt, &ptr->value))
653 if (!ptr->o || !ptr->o->v.string || strcmp(ptr->o->v.string, ptr->value))
654 uci_set(cursor, ptr);
659 rpc_uci_set(struct ubus_context *ctx, struct ubus_object *obj,
660 struct ubus_request_data *req, const char *method,
661 struct blob_attr *msg)
663 struct blob_attr *tb[__RPC_S_MAX];
664 struct blob_attr *cur;
665 struct uci_package *p = NULL;
666 struct uci_element *e;
667 struct uci_ptr ptr = { 0 };
670 blobmsg_parse(rpc_uci_set_policy, __RPC_S_MAX, tb,
671 blob_data(msg), blob_len(msg));
673 if (!tb[RPC_S_CONFIG] || !tb[RPC_S_VALUES] ||
674 (!tb[RPC_S_SECTION] && !tb[RPC_S_TYPE] && !tb[RPC_S_MATCH]))
675 return UBUS_STATUS_INVALID_ARGUMENT;
677 if (!rpc_uci_write_access(tb[RPC_S_SESSION], tb[RPC_S_CONFIG]))
678 return UBUS_STATUS_PERMISSION_DENIED;
680 ptr.package = blobmsg_data(tb[RPC_S_CONFIG]);
681 uci_load(cursor, ptr.package, &p);
686 if (tb[RPC_S_SECTION])
688 ptr.section = blobmsg_data(tb[RPC_S_SECTION]);
689 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
690 rpc_uci_merge_set(cur, &ptr);
694 uci_foreach_element(&p->sections, e)
696 if (!rpc_uci_match_section(uci_to_section(e),
697 tb[RPC_S_TYPE], tb[RPC_S_MATCH]))
701 ptr.section = e->name;
703 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
704 rpc_uci_merge_set(cur, &ptr);
712 uci_unload(cursor, p);
714 return rpc_uci_status();
718 * Delete option or section from uci specified by given blob attribute pointer
719 * 1) if the blob is of type array, delete any option named after each element
720 * 2) if the blob is of type string, delete the option named after its value
721 * 3) if the blob is NULL, delete entire section
724 rpc_uci_merge_delete(struct blob_attr *opt, struct uci_ptr *ptr)
726 struct blob_attr *cur;
729 if (rpc_uci_lookup(ptr) || !ptr->s)
737 uci_delete(cursor, ptr);
739 else if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
741 blobmsg_for_each_attr(cur, opt, rem)
743 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
747 ptr->option = blobmsg_data(cur);
749 if (rpc_uci_lookup(ptr) || !ptr->o)
752 uci_delete(cursor, ptr);
755 else if (blobmsg_type(opt) == BLOBMSG_TYPE_STRING)
758 ptr->option = blobmsg_data(opt);
760 if (rpc_uci_lookup(ptr) || !ptr->o)
763 uci_delete(cursor, ptr);
768 rpc_uci_delete(struct ubus_context *ctx, struct ubus_object *obj,
769 struct ubus_request_data *req, const char *method,
770 struct blob_attr *msg)
772 struct blob_attr *tb[__RPC_D_MAX];
773 struct uci_package *p = NULL;
774 struct uci_element *e, *tmp;
775 struct uci_ptr ptr = { 0 };
777 blobmsg_parse(rpc_uci_delete_policy, __RPC_D_MAX, tb,
778 blob_data(msg), blob_len(msg));
780 if (!tb[RPC_D_CONFIG] ||
781 (!tb[RPC_D_SECTION] && !tb[RPC_D_TYPE] && !tb[RPC_D_MATCH]))
782 return UBUS_STATUS_INVALID_ARGUMENT;
784 if (!rpc_uci_write_access(tb[RPC_D_SESSION], tb[RPC_D_CONFIG]))
785 return UBUS_STATUS_PERMISSION_DENIED;
787 ptr.package = blobmsg_data(tb[RPC_D_CONFIG]);
788 uci_load(cursor, ptr.package, &p);
793 if (tb[RPC_D_SECTION])
795 ptr.section = blobmsg_data(tb[RPC_D_SECTION]);
797 if (tb[RPC_D_OPTIONS])
798 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
800 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
804 uci_foreach_element_safe(&p->sections, tmp, e)
806 if (!rpc_uci_match_section(uci_to_section(e),
807 tb[RPC_D_TYPE], tb[RPC_D_MATCH]))
811 ptr.section = e->name;
813 if (tb[RPC_D_OPTIONS])
814 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
816 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
824 uci_unload(cursor, p);
826 return rpc_uci_status();
830 rpc_uci_rename(struct ubus_context *ctx, struct ubus_object *obj,
831 struct ubus_request_data *req, const char *method,
832 struct blob_attr *msg)
834 struct blob_attr *tb[__RPC_R_MAX];
835 struct uci_package *p = NULL;
836 struct uci_ptr ptr = { 0 };
838 blobmsg_parse(rpc_uci_rename_policy, __RPC_R_MAX, tb,
839 blob_data(msg), blob_len(msg));
841 if (!tb[RPC_R_CONFIG] || !tb[RPC_R_SECTION] || !tb[RPC_R_NAME])
842 return UBUS_STATUS_INVALID_ARGUMENT;
844 if (!rpc_uci_write_access(tb[RPC_R_SESSION], tb[RPC_R_CONFIG]))
845 return UBUS_STATUS_PERMISSION_DENIED;
847 ptr.package = blobmsg_data(tb[RPC_R_CONFIG]);
848 ptr.section = blobmsg_data(tb[RPC_R_SECTION]);
849 ptr.value = blobmsg_data(tb[RPC_R_NAME]);
851 if (tb[RPC_R_OPTION])
852 ptr.option = blobmsg_data(tb[RPC_R_OPTION]);
854 uci_load(cursor, ptr.package, &p);
859 if (uci_lookup_ptr(cursor, &ptr, NULL, true))
862 if ((ptr.option && !ptr.o) || !ptr.s)
864 cursor->err = UCI_ERR_NOTFOUND;
868 if (uci_rename(cursor, &ptr))
875 uci_unload(cursor, p);
877 return rpc_uci_status();
881 rpc_uci_order(struct ubus_context *ctx, struct ubus_object *obj,
882 struct ubus_request_data *req, const char *method,
883 struct blob_attr *msg)
885 struct blob_attr *tb[__RPC_O_MAX];
886 struct blob_attr *cur;
887 struct uci_package *p = NULL;
888 struct uci_ptr ptr = { 0 };
891 blobmsg_parse(rpc_uci_order_policy, __RPC_O_MAX, tb,
892 blob_data(msg), blob_len(msg));
894 if (!tb[RPC_O_CONFIG] || !tb[RPC_O_SECTIONS])
895 return UBUS_STATUS_INVALID_ARGUMENT;
897 if (!rpc_uci_write_access(tb[RPC_O_SESSION], tb[RPC_O_CONFIG]))
898 return UBUS_STATUS_PERMISSION_DENIED;
900 ptr.package = blobmsg_data(tb[RPC_O_CONFIG]);
902 uci_load(cursor, ptr.package, &p);
907 blobmsg_for_each_attr(cur, tb[RPC_O_SECTIONS], rem)
909 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
913 ptr.section = blobmsg_data(cur);
915 if (uci_lookup_ptr(cursor, &ptr, NULL, true) || !ptr.s)
918 uci_reorder_section(cursor, ptr.s, i++);
925 uci_unload(cursor, p);
927 return rpc_uci_status();
931 rpc_uci_dump_change(struct uci_delta *d)
934 const char *types[] = {
935 [UCI_CMD_REORDER] = "order",
936 [UCI_CMD_REMOVE] = "remove",
937 [UCI_CMD_RENAME] = "rename",
938 [UCI_CMD_ADD] = "add",
939 [UCI_CMD_LIST_ADD] = "list-add",
940 [UCI_CMD_LIST_DEL] = "list-del",
941 [UCI_CMD_CHANGE] = "set",
947 c = blobmsg_open_array(&buf, NULL);
949 blobmsg_add_string(&buf, NULL, types[d->cmd]);
950 blobmsg_add_string(&buf, NULL, d->section);
953 blobmsg_add_string(&buf, NULL, d->e.name);
957 if (d->cmd == UCI_CMD_REORDER)
958 blobmsg_add_u32(&buf, NULL, strtoul(d->value, NULL, 10));
960 blobmsg_add_string(&buf, NULL, d->value);
963 blobmsg_close_array(&buf, c);
967 rpc_uci_changes(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_C_MAX];
972 struct uci_package *p = NULL;
973 struct uci_element *e;
976 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
977 blob_data(msg), blob_len(msg));
979 if (!tb[RPC_C_CONFIG])
980 return UBUS_STATUS_INVALID_ARGUMENT;
982 if (!rpc_uci_read_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
983 return UBUS_STATUS_PERMISSION_DENIED;
985 uci_load(cursor, blobmsg_data(tb[RPC_C_CONFIG]), &p);
990 blob_buf_init(&buf, 0);
991 c = blobmsg_open_array(&buf, "changes");
993 uci_foreach_element(&p->saved_delta, e)
994 rpc_uci_dump_change(uci_to_delta(e));
996 blobmsg_close_array(&buf, c);
998 ubus_send_reply(ctx, req, buf.head);
1002 uci_unload(cursor, p);
1004 return rpc_uci_status();
1008 rpc_uci_revert_commit(struct blob_attr *msg, bool commit)
1010 struct blob_attr *tb[__RPC_C_MAX];
1011 struct uci_package *p = NULL;
1012 struct uci_ptr ptr = { 0 };
1014 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
1015 blob_data(msg), blob_len(msg));
1017 if (!tb[RPC_C_CONFIG])
1018 return UBUS_STATUS_INVALID_ARGUMENT;
1020 if (!rpc_uci_write_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
1021 return UBUS_STATUS_PERMISSION_DENIED;
1023 ptr.package = blobmsg_data(tb[RPC_C_CONFIG]);
1027 uci_load(cursor, ptr.package, &p);
1031 uci_commit(cursor, &p, false);
1032 uci_unload(cursor, p);
1037 if (!uci_lookup_ptr(cursor, &ptr, NULL, true) && ptr.p)
1038 uci_revert(cursor, &ptr);
1041 return rpc_uci_status();
1045 rpc_uci_revert(struct ubus_context *ctx, struct ubus_object *obj,
1046 struct ubus_request_data *req, const char *method,
1047 struct blob_attr *msg)
1049 return rpc_uci_revert_commit(msg, false);
1053 rpc_uci_commit(struct ubus_context *ctx, struct ubus_object *obj,
1054 struct ubus_request_data *req, const char *method,
1055 struct blob_attr *msg)
1057 return rpc_uci_revert_commit(msg, true);
1061 rpc_uci_configs(struct ubus_context *ctx, struct ubus_object *obj,
1062 struct ubus_request_data *req, const char *method,
1063 struct blob_attr *msg)
1069 if (uci_list_configs(cursor, &configs))
1072 blob_buf_init(&buf, 0);
1074 c = blobmsg_open_array(&buf, "configs");
1076 for (i = 0; configs[i]; i++)
1077 blobmsg_add_string(&buf, NULL, configs[i]);
1079 blobmsg_close_array(&buf, c);
1081 ubus_send_reply(ctx, req, buf.head);
1084 return rpc_uci_status();
1088 int rpc_uci_api_init(struct ubus_context *ctx)
1090 static const struct ubus_method uci_methods[] = {
1091 { .name = "configs", .handler = rpc_uci_configs },
1092 UBUS_METHOD("get", rpc_uci_get, rpc_uci_get_policy),
1093 UBUS_METHOD("add", rpc_uci_add, rpc_uci_add_policy),
1094 UBUS_METHOD("set", rpc_uci_set, rpc_uci_set_policy),
1095 UBUS_METHOD("delete", rpc_uci_delete, rpc_uci_delete_policy),
1096 UBUS_METHOD("rename", rpc_uci_rename, rpc_uci_rename_policy),
1097 UBUS_METHOD("order", rpc_uci_order, rpc_uci_order_policy),
1098 UBUS_METHOD("changes", rpc_uci_changes, rpc_uci_config_policy),
1099 UBUS_METHOD("revert", rpc_uci_revert, rpc_uci_config_policy),
1100 UBUS_METHOD("commit", rpc_uci_commit, rpc_uci_config_policy),
1103 static struct ubus_object_type uci_type =
1104 UBUS_OBJECT_TYPE("luci-rpc-uci", uci_methods);
1106 static struct ubus_object obj = {
1109 .methods = uci_methods,
1110 .n_methods = ARRAY_SIZE(uci_methods),
1113 cursor = uci_alloc_context();
1116 return UBUS_STATUS_UNKNOWN_ERROR;
1118 return ubus_add_object(ctx, &obj);