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.
21 static struct blob_buf buf;
22 static struct uci_context *cursor;
33 static const struct blobmsg_policy rpc_uci_get_policy[__RPC_G_MAX] = {
34 [RPC_G_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
35 [RPC_G_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
36 [RPC_G_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
37 [RPC_G_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
38 [RPC_G_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
49 static const struct blobmsg_policy rpc_uci_add_policy[__RPC_A_MAX] = {
50 [RPC_A_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
51 [RPC_A_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
52 [RPC_A_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
53 [RPC_A_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
65 static const struct blobmsg_policy rpc_uci_set_policy[__RPC_S_MAX] = {
66 [RPC_S_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
67 [RPC_S_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
68 [RPC_S_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
69 [RPC_S_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
70 [RPC_S_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
83 static const struct blobmsg_policy rpc_uci_delete_policy[__RPC_D_MAX] = {
84 [RPC_D_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
85 [RPC_D_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
86 [RPC_D_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
87 [RPC_D_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
88 [RPC_D_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
89 [RPC_D_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_ARRAY },
100 static const struct blobmsg_policy rpc_uci_rename_policy[__RPC_R_MAX] = {
101 [RPC_R_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
102 [RPC_R_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
103 [RPC_R_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
104 [RPC_R_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
113 static const struct blobmsg_policy rpc_uci_order_policy[__RPC_O_MAX] = {
114 [RPC_O_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
115 [RPC_O_SECTIONS] = { .name = "sections", .type = BLOBMSG_TYPE_ARRAY },
123 static const struct blobmsg_policy rpc_uci_config_policy[__RPC_C_MAX] = {
124 [RPC_C_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
128 * Turn uci error state into ubus return code
136 return UBUS_STATUS_OK;
139 return UBUS_STATUS_INVALID_ARGUMENT;
141 case UCI_ERR_NOTFOUND:
142 return UBUS_STATUS_NOT_FOUND;
145 return UBUS_STATUS_UNKNOWN_ERROR;
150 * Format applicable blob value as string and place a pointer to the string
151 * buffer in "p". Uses a static string buffer.
154 rpc_uci_format_blob(struct blob_attr *v, const char **p)
160 switch (blobmsg_type(v))
162 case BLOBMSG_TYPE_STRING:
163 if (blobmsg_data_len(v) > 1)
164 *p = blobmsg_data(v);
167 case BLOBMSG_TYPE_INT64:
168 snprintf(buf, sizeof(buf), "%"PRIu64, blobmsg_get_u64(v));
172 case BLOBMSG_TYPE_INT32:
173 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u32(v));
177 case BLOBMSG_TYPE_INT16:
178 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u16(v));
182 case BLOBMSG_TYPE_INT8:
183 snprintf(buf, sizeof(buf), "%u", !!blobmsg_get_u8(v));
195 * Lookup the given uci_ptr and enable extended lookup format if the .section
196 * value of the uci_ptr looks like extended syntax. Uses an internal copy
197 * of the given uci_ptr to perform the lookup as failing extended section
198 * lookup operations in libuci will zero our the uci_ptr struct.
199 * Copies the internal uci_ptr back to given the uci_ptr on success.
202 rpc_uci_lookup(struct uci_ptr *ptr)
205 struct uci_ptr lookup = *ptr;
207 if (!lookup.s && lookup.section && *lookup.section == '@')
208 lookup.flags |= UCI_LOOKUP_EXTENDED;
210 rv = uci_lookup_ptr(cursor, &lookup, NULL, true);
219 * Checks whether the given uci_option object matches the given string value.
220 * 1) If the uci_option is of type list, check whether any of the list elements
221 * equals to the given string
222 * 2) If the uci_option is of type string, parse it into space separated tokens
223 * and check if any of the tokens equals to the given string.
224 * Returns true if a list element or token matched the given string.
227 rpc_uci_match_option(struct uci_option *o, const char *cmp)
229 struct uci_element *e;
232 if (o->type == UCI_TYPE_LIST)
234 uci_foreach_element(&o->v.list, e)
235 if (e->name && !strcmp(e->name, cmp))
244 s = strdup(o->v.string);
249 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
263 * Checks whether the given uci_section matches the type and value blob attrs.
264 * 1) Returns false if "type" is given and the section type does not match
265 * the value specified in the "type" string blob attribute, else continue.
266 * 2) Tests whether any key in the "matches" table blob attribute exists in
267 * the given uci_section and whether each value is contained in the
268 * corresponding uci option value (see rpc_uci_match_option()).
269 * 3) A missing or empty "matches" table blob attribute is always considered
271 * Returns true if "type" matches or is NULL and "matches" matches or is NULL.
274 rpc_uci_match_section(struct uci_section *s,
275 struct blob_attr *type, struct blob_attr *matches)
277 struct uci_element *e;
278 struct blob_attr *cur;
284 if (type && strcmp(s->type, blobmsg_data(type)))
290 blobmsg_for_each_attr(cur, matches, rem)
292 if (!rpc_uci_format_blob(cur, &cmp))
295 uci_foreach_element(&s->options, e)
297 if (strcmp(e->name, blobmsg_name(cur)))
300 if (!rpc_uci_match_option(uci_to_option(e), cmp))
309 return (empty || match);
313 * Dump the given uci_option value into the global blobmsg buffer and use
314 * given "name" as key.
315 * 1) If the uci_option is of type list, put a table into the blob buffer and
316 * add each list item as string to it.
317 * 2) If the uci_option is of type string, put its value directly into the blob
321 rpc_uci_dump_option(struct uci_option *o, const char *name)
324 struct uci_element *e;
328 case UCI_TYPE_STRING:
329 blobmsg_add_string(&buf, name, o->v.string);
333 c = blobmsg_open_array(&buf, name);
335 uci_foreach_element(&o->v.list, e)
336 blobmsg_add_string(&buf, NULL, e->name);
338 blobmsg_close_array(&buf, c);
347 * Dump the given uci_section object into the global blobmsg buffer and use
348 * given "name" as key.
349 * Puts a table into the blob buffer and puts each section option member value
350 * as value into the table using the option name as key.
351 * Adds three special keys ".anonymous", ".type" and ".name" which specify the
352 * corresponding section properties.
355 rpc_uci_dump_section(struct uci_section *s, const char *name)
358 struct uci_option *o;
359 struct uci_element *e;
361 c = blobmsg_open_table(&buf, name);
363 blobmsg_add_u8(&buf, ".anonymous", s->anonymous);
364 blobmsg_add_string(&buf, ".type", s->type);
365 blobmsg_add_string(&buf, ".name", s->e.name);
367 uci_foreach_element(&s->options, e)
369 o = uci_to_option(e);
370 rpc_uci_dump_option(o, o->e.name);
373 blobmsg_close_table(&buf, c);
377 * Dump the given uci_package object into the global blobmsg buffer and use
378 * given "name" as key.
379 * Puts a table into the blob buffer and puts each package section member as
380 * value into the table using the section name as key.
381 * Only dumps sections matching the given "type" and "matches", see explaination
382 * of rpc_uci_match_section() for details.
385 rpc_uci_dump_package(struct uci_package *p, const char *name,
386 struct blob_attr *type, struct blob_attr *matches)
389 struct uci_element *e;
391 c = blobmsg_open_table(&buf, name);
393 uci_foreach_element(&p->sections, e)
395 if (!rpc_uci_match_section(uci_to_section(e), type, matches))
398 rpc_uci_dump_section(uci_to_section(e), e->name);
401 blobmsg_close_table(&buf, c);
406 rpc_uci_get(struct ubus_context *ctx, struct ubus_object *obj,
407 struct ubus_request_data *req, const char *method,
408 struct blob_attr *msg)
410 struct blob_attr *tb[__RPC_G_MAX];
411 struct uci_package *p = NULL;
412 struct uci_ptr ptr = { 0 };
414 blobmsg_parse(rpc_uci_get_policy, __RPC_G_MAX, tb,
415 blob_data(msg), blob_len(msg));
417 if (!tb[RPC_G_CONFIG])
418 return UBUS_STATUS_INVALID_ARGUMENT;
420 ptr.package = blobmsg_data(tb[RPC_G_CONFIG]);
421 uci_load(cursor, ptr.package, &p);
426 if (tb[RPC_G_SECTION])
428 ptr.section = blobmsg_data(tb[RPC_G_SECTION]);
430 if (tb[RPC_G_OPTION])
431 ptr.option = blobmsg_data(tb[RPC_G_OPTION]);
434 if (rpc_uci_lookup(&ptr) || !(ptr.flags & UCI_LOOKUP_COMPLETE))
437 blob_buf_init(&buf, 0);
439 switch (ptr.last->type)
441 case UCI_TYPE_PACKAGE:
442 rpc_uci_dump_package(ptr.p, "values", tb[RPC_G_TYPE], tb[RPC_G_MATCH]);
445 case UCI_TYPE_SECTION:
446 rpc_uci_dump_section(ptr.s, "values");
449 case UCI_TYPE_OPTION:
450 rpc_uci_dump_option(ptr.o, "value");
457 ubus_send_reply(ctx, req, buf.head);
461 uci_unload(cursor, p);
463 return rpc_uci_status();
467 rpc_uci_add(struct ubus_context *ctx, struct ubus_object *obj,
468 struct ubus_request_data *req, const char *method,
469 struct blob_attr *msg)
471 struct blob_attr *tb[__RPC_A_MAX];
472 struct blob_attr *cur, *elem;
473 struct uci_package *p = NULL;
474 struct uci_section *s;
475 struct uci_ptr ptr = { 0 };
478 blobmsg_parse(rpc_uci_add_policy, __RPC_A_MAX, tb,
479 blob_data(msg), blob_len(msg));
481 if (!tb[RPC_A_CONFIG] || !tb[RPC_A_TYPE])
482 return UBUS_STATUS_INVALID_ARGUMENT;
484 ptr.package = blobmsg_data(tb[RPC_A_CONFIG]);
486 uci_load(cursor, ptr.package, &p);
491 /* add named section */
494 ptr.section = blobmsg_data(tb[RPC_A_NAME]);
495 ptr.value = blobmsg_data(tb[RPC_A_TYPE]);
498 if (rpc_uci_lookup(&ptr) || uci_set(cursor, &ptr))
502 /* add anon section */
505 if (uci_add_section(cursor, p, blobmsg_data(tb[RPC_A_TYPE]), &s) || !s)
508 ptr.section = s->e.name;
511 if (tb[RPC_A_VALUES])
513 blobmsg_for_each_attr(cur, tb[RPC_A_VALUES], rem)
516 ptr.option = blobmsg_name(cur);
518 if (rpc_uci_lookup(&ptr) || !ptr.s)
521 switch (blobmsg_type(cur))
523 case BLOBMSG_TYPE_ARRAY:
524 blobmsg_for_each_attr(elem, cur, rem2)
525 if (rpc_uci_format_blob(elem, &ptr.value))
526 uci_add_list(cursor, &ptr);
530 if (rpc_uci_format_blob(cur, &ptr.value))
531 uci_set(cursor, &ptr);
539 blob_buf_init(&buf, 0);
540 blobmsg_add_string(&buf, "section", ptr.section);
541 ubus_send_reply(ctx, req, buf.head);
545 uci_unload(cursor, p);
547 return rpc_uci_status();
551 * Turn value from a blob attribute into uci set operation
552 * 1) if the blob is of type array, delete existing option (if any) and
553 * emit uci add_list operations for each element
554 * 2) if the blob is not an array but an option of type list exists,
555 * delete existing list and emit uci set operation for the blob value
556 * 3) in all other cases only emit a set operation if there is no existing
557 * option of if the existing options value differs from the blob value
560 rpc_uci_merge_set(struct blob_attr *opt, struct uci_ptr *ptr)
562 struct blob_attr *cur;
566 ptr->option = blobmsg_name(opt);
568 if (rpc_uci_lookup(ptr) || !ptr->s)
571 if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
574 uci_delete(cursor, ptr);
576 blobmsg_for_each_attr(cur, opt, rem)
577 if (rpc_uci_format_blob(cur, &ptr->value))
578 uci_add_list(cursor, ptr);
580 else if (ptr->o && ptr->o->type == UCI_TYPE_LIST)
582 uci_delete(cursor, ptr);
584 if (rpc_uci_format_blob(opt, &ptr->value))
585 uci_set(cursor, ptr);
587 else if (rpc_uci_format_blob(opt, &ptr->value))
589 if (!ptr->o || !ptr->o->v.string || strcmp(ptr->o->v.string, ptr->value))
590 uci_set(cursor, ptr);
595 rpc_uci_set(struct ubus_context *ctx, struct ubus_object *obj,
596 struct ubus_request_data *req, const char *method,
597 struct blob_attr *msg)
599 struct blob_attr *tb[__RPC_S_MAX];
600 struct blob_attr *cur;
601 struct uci_package *p = NULL;
602 struct uci_element *e;
603 struct uci_ptr ptr = { 0 };
606 blobmsg_parse(rpc_uci_set_policy, __RPC_S_MAX, tb,
607 blob_data(msg), blob_len(msg));
609 if (!tb[RPC_S_CONFIG] || !tb[RPC_S_VALUES] ||
610 (!tb[RPC_S_SECTION] && !tb[RPC_S_TYPE] && !tb[RPC_S_MATCH]))
611 return UBUS_STATUS_INVALID_ARGUMENT;
613 ptr.package = blobmsg_data(tb[RPC_S_CONFIG]);
614 uci_load(cursor, ptr.package, &p);
619 if (tb[RPC_S_SECTION])
621 ptr.section = blobmsg_data(tb[RPC_S_SECTION]);
622 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
623 rpc_uci_merge_set(cur, &ptr);
627 uci_foreach_element(&p->sections, e)
629 if (!rpc_uci_match_section(uci_to_section(e),
630 tb[RPC_S_TYPE], tb[RPC_S_MATCH]))
634 ptr.section = e->name;
636 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
637 rpc_uci_merge_set(cur, &ptr);
645 uci_unload(cursor, p);
647 return rpc_uci_status();
651 * Delete option or section from uci specified by given blob attribute pointer
652 * 1) if the blob is of type array, delete any option named after each element
653 * 2) if the blob is of type string, delete the option named after its value
654 * 3) if the blob is NULL, delete entire section
657 rpc_uci_merge_delete(struct blob_attr *opt, struct uci_ptr *ptr)
659 struct blob_attr *cur;
662 if (rpc_uci_lookup(ptr) || !ptr->s)
670 uci_delete(cursor, ptr);
672 else if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
674 blobmsg_for_each_attr(cur, opt, rem)
676 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
680 ptr->option = blobmsg_data(cur);
682 if (rpc_uci_lookup(ptr) || !ptr->o)
685 uci_delete(cursor, ptr);
688 else if (blobmsg_type(opt) == BLOBMSG_TYPE_STRING)
691 ptr->option = blobmsg_data(opt);
693 if (rpc_uci_lookup(ptr) || !ptr->o)
696 uci_delete(cursor, ptr);
701 rpc_uci_delete(struct ubus_context *ctx, struct ubus_object *obj,
702 struct ubus_request_data *req, const char *method,
703 struct blob_attr *msg)
705 struct blob_attr *tb[__RPC_D_MAX];
706 struct uci_package *p = NULL;
707 struct uci_element *e, *tmp;
708 struct uci_ptr ptr = { 0 };
710 blobmsg_parse(rpc_uci_delete_policy, __RPC_D_MAX, tb,
711 blob_data(msg), blob_len(msg));
713 if (!tb[RPC_D_CONFIG] ||
714 (!tb[RPC_D_SECTION] && !tb[RPC_D_TYPE] && !tb[RPC_D_MATCH]))
715 return UBUS_STATUS_INVALID_ARGUMENT;
717 ptr.package = blobmsg_data(tb[RPC_D_CONFIG]);
718 uci_load(cursor, ptr.package, &p);
723 if (tb[RPC_D_SECTION])
725 ptr.section = blobmsg_data(tb[RPC_D_SECTION]);
727 if (tb[RPC_D_OPTIONS])
728 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
730 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
734 uci_foreach_element_safe(&p->sections, tmp, e)
736 if (!rpc_uci_match_section(uci_to_section(e),
737 tb[RPC_D_TYPE], tb[RPC_D_MATCH]))
741 ptr.section = e->name;
743 if (tb[RPC_D_OPTIONS])
744 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
746 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
754 uci_unload(cursor, p);
756 return rpc_uci_status();
760 rpc_uci_rename(struct ubus_context *ctx, struct ubus_object *obj,
761 struct ubus_request_data *req, const char *method,
762 struct blob_attr *msg)
764 struct blob_attr *tb[__RPC_R_MAX];
765 struct uci_package *p = NULL;
766 struct uci_ptr ptr = { 0 };
768 blobmsg_parse(rpc_uci_rename_policy, __RPC_R_MAX, tb,
769 blob_data(msg), blob_len(msg));
771 if (!tb[RPC_R_CONFIG] || !tb[RPC_R_SECTION] || !tb[RPC_R_NAME])
772 return UBUS_STATUS_INVALID_ARGUMENT;
774 ptr.package = blobmsg_data(tb[RPC_R_CONFIG]);
775 ptr.section = blobmsg_data(tb[RPC_R_SECTION]);
776 ptr.value = blobmsg_data(tb[RPC_R_NAME]);
778 if (tb[RPC_R_OPTION])
779 ptr.option = blobmsg_data(tb[RPC_R_OPTION]);
781 uci_load(cursor, ptr.package, &p);
786 if (uci_lookup_ptr(cursor, &ptr, NULL, true))
789 if ((ptr.option && !ptr.o) || !ptr.s)
791 cursor->err = UCI_ERR_NOTFOUND;
795 if (uci_rename(cursor, &ptr))
802 uci_unload(cursor, p);
804 return rpc_uci_status();
808 rpc_uci_order(struct ubus_context *ctx, struct ubus_object *obj,
809 struct ubus_request_data *req, const char *method,
810 struct blob_attr *msg)
812 struct blob_attr *tb[__RPC_O_MAX];
813 struct blob_attr *cur;
814 struct uci_package *p = NULL;
815 struct uci_ptr ptr = { 0 };
818 blobmsg_parse(rpc_uci_order_policy, __RPC_O_MAX, tb,
819 blob_data(msg), blob_len(msg));
821 if (!tb[RPC_O_CONFIG] || !tb[RPC_O_SECTIONS])
822 return UBUS_STATUS_INVALID_ARGUMENT;
824 ptr.package = blobmsg_data(tb[RPC_O_CONFIG]);
826 uci_load(cursor, ptr.package, &p);
831 blobmsg_for_each_attr(cur, tb[RPC_O_SECTIONS], rem)
833 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
837 ptr.section = blobmsg_data(cur);
839 if (uci_lookup_ptr(cursor, &ptr, NULL, true) || !ptr.s)
842 uci_reorder_section(cursor, ptr.s, i++);
849 uci_unload(cursor, p);
851 return rpc_uci_status();
855 rpc_uci_dump_change(struct uci_delta *d)
858 const char *types[] = {
859 [UCI_CMD_REORDER] = "order",
860 [UCI_CMD_REMOVE] = "remove",
861 [UCI_CMD_RENAME] = "rename",
862 [UCI_CMD_ADD] = "add",
863 [UCI_CMD_LIST_ADD] = "list-add",
864 [UCI_CMD_LIST_DEL] = "list-del",
865 [UCI_CMD_CHANGE] = "set",
871 c = blobmsg_open_array(&buf, NULL);
873 blobmsg_add_string(&buf, NULL, types[d->cmd]);
874 blobmsg_add_string(&buf, NULL, d->section);
877 blobmsg_add_string(&buf, NULL, d->e.name);
881 if (d->cmd == UCI_CMD_REORDER)
882 blobmsg_add_u32(&buf, NULL, strtoul(d->value, NULL, 10));
884 blobmsg_add_string(&buf, NULL, d->value);
887 blobmsg_close_array(&buf, c);
891 rpc_uci_changes(struct ubus_context *ctx, struct ubus_object *obj,
892 struct ubus_request_data *req, const char *method,
893 struct blob_attr *msg)
895 struct blob_attr *tb[__RPC_C_MAX];
896 struct uci_package *p = NULL;
897 struct uci_element *e;
900 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
901 blob_data(msg), blob_len(msg));
903 if (!tb[RPC_C_CONFIG])
904 return UBUS_STATUS_INVALID_ARGUMENT;
906 uci_load(cursor, blobmsg_data(tb[RPC_C_CONFIG]), &p);
911 blob_buf_init(&buf, 0);
912 c = blobmsg_open_array(&buf, "changes");
914 uci_foreach_element(&p->saved_delta, e)
915 rpc_uci_dump_change(uci_to_delta(e));
917 blobmsg_close_array(&buf, c);
919 ubus_send_reply(ctx, req, buf.head);
923 uci_unload(cursor, p);
925 return rpc_uci_status();
929 rpc_uci_revert_commit(struct blob_attr *msg, bool commit)
931 struct blob_attr *tb[__RPC_C_MAX];
932 struct uci_package *p = NULL;
933 struct uci_ptr ptr = { 0 };
935 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
936 blob_data(msg), blob_len(msg));
938 if (!tb[RPC_C_CONFIG])
939 return UBUS_STATUS_INVALID_ARGUMENT;
941 ptr.package = blobmsg_data(tb[RPC_C_CONFIG]);
942 uci_load(cursor, ptr.package, &p);
944 if (!p || uci_lookup_ptr(cursor, &ptr, NULL, true) || !ptr.p)
948 uci_commit(cursor, &p, false);
950 uci_revert(cursor, &ptr);
954 uci_unload(cursor, p);
956 return rpc_uci_status();
960 rpc_uci_revert(struct ubus_context *ctx, struct ubus_object *obj,
961 struct ubus_request_data *req, const char *method,
962 struct blob_attr *msg)
964 return rpc_uci_revert_commit(msg, false);
968 rpc_uci_commit(struct ubus_context *ctx, struct ubus_object *obj,
969 struct ubus_request_data *req, const char *method,
970 struct blob_attr *msg)
972 return rpc_uci_revert_commit(msg, true);
976 rpc_uci_configs(struct ubus_context *ctx, struct ubus_object *obj,
977 struct ubus_request_data *req, const char *method,
978 struct blob_attr *msg)
984 if (uci_list_configs(cursor, &configs))
987 blob_buf_init(&buf, 0);
989 c = blobmsg_open_array(&buf, "configs");
991 for (i = 0; configs[i]; i++)
992 blobmsg_add_string(&buf, NULL, configs[i]);
994 blobmsg_close_array(&buf, c);
996 ubus_send_reply(ctx, req, buf.head);
999 return rpc_uci_status();
1003 int rpc_uci_api_init(struct ubus_context *ctx)
1005 static const struct ubus_method uci_methods[] = {
1006 { .name = "configs", .handler = rpc_uci_configs },
1007 UBUS_METHOD("get", rpc_uci_get, rpc_uci_get_policy),
1008 UBUS_METHOD("add", rpc_uci_add, rpc_uci_add_policy),
1009 UBUS_METHOD("set", rpc_uci_set, rpc_uci_set_policy),
1010 UBUS_METHOD("delete", rpc_uci_delete, rpc_uci_delete_policy),
1011 UBUS_METHOD("rename", rpc_uci_rename, rpc_uci_rename_policy),
1012 UBUS_METHOD("order", rpc_uci_order, rpc_uci_order_policy),
1013 UBUS_METHOD("changes", rpc_uci_changes, rpc_uci_config_policy),
1014 UBUS_METHOD("revert", rpc_uci_revert, rpc_uci_config_policy),
1015 UBUS_METHOD("commit", rpc_uci_commit, rpc_uci_config_policy),
1018 static struct ubus_object_type uci_type =
1019 UBUS_OBJECT_TYPE("luci-rpc-uci", uci_methods);
1021 static struct ubus_object obj = {
1024 .methods = uci_methods,
1025 .n_methods = ARRAY_SIZE(uci_methods),
1028 cursor = uci_alloc_context();
1031 return UBUS_STATUS_UNKNOWN_ERROR;
1033 return ubus_add_object(ctx, &obj);