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 },
81 static const struct blobmsg_policy rpc_uci_rename_policy[__RPC_R_MAX] = {
82 [RPC_R_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
83 [RPC_R_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
84 [RPC_R_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
85 [RPC_R_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
94 static const struct blobmsg_policy rpc_uci_order_policy[__RPC_O_MAX] = {
95 [RPC_O_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
96 [RPC_O_SECTIONS] = { .name = "sections", .type = BLOBMSG_TYPE_ARRAY },
104 static const struct blobmsg_policy rpc_uci_config_policy[__RPC_C_MAX] = {
105 [RPC_C_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
109 * Turn uci error state into ubus return code
117 return UBUS_STATUS_OK;
120 return UBUS_STATUS_INVALID_ARGUMENT;
122 case UCI_ERR_NOTFOUND:
123 return UBUS_STATUS_NOT_FOUND;
126 return UBUS_STATUS_UNKNOWN_ERROR;
131 * Format applicable blob value as string and place a pointer to the string
132 * buffer in "p". Uses a static string buffer.
135 rpc_uci_format_blob(struct blob_attr *v, const char **p)
141 switch (blobmsg_type(v))
143 case BLOBMSG_TYPE_STRING:
144 if (blobmsg_data_len(v) > 1)
145 *p = blobmsg_data(v);
148 case BLOBMSG_TYPE_INT64:
149 snprintf(buf, sizeof(buf), "%"PRIu64, blobmsg_get_u64(v));
153 case BLOBMSG_TYPE_INT32:
154 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u32(v));
158 case BLOBMSG_TYPE_INT16:
159 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u16(v));
163 case BLOBMSG_TYPE_INT8:
164 snprintf(buf, sizeof(buf), "%u", !!blobmsg_get_u8(v));
176 * Lookup the given uci_ptr and enable extended lookup format if the .section
177 * value of the uci_ptr looks like extended syntax. Uses an internal copy
178 * of the given uci_ptr to perform the lookup as failing extended section
179 * lookup operations in libuci will zero our the uci_ptr struct.
180 * Copies the internal uci_ptr back to given the uci_ptr on success.
183 rpc_uci_lookup(struct uci_ptr *ptr)
186 struct uci_ptr lookup = *ptr;
188 if (!lookup.s && lookup.section && *lookup.section == '@')
189 lookup.flags |= UCI_LOOKUP_EXTENDED;
191 rv = uci_lookup_ptr(cursor, &lookup, NULL, true);
200 * Checks whether the given uci_option object matches the given string value.
201 * 1) If the uci_option is of type list, check whether any of the list elements
202 * equals to the given string
203 * 2) If the uci_option is of type string, parse it into space separated tokens
204 * and check if any of the tokens equals to the given string.
205 * Returns true if a list element or token matched the given string.
208 rpc_uci_match_option(struct uci_option *o, const char *cmp)
210 struct uci_element *e;
213 if (o->type == UCI_TYPE_LIST)
215 uci_foreach_element(&o->v.list, e)
216 if (e->name && !strcmp(e->name, cmp))
225 s = strdup(o->v.string);
230 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
244 * Checks whether the given uci_section matches the type and value blob attrs.
245 * 1) Returns false if "type" is given and the section type does not match
246 * the value specified in the "type" string blob attribute, else continue.
247 * 2) Tests whether any key in the "matches" table blob attribute exists in
248 * the given uci_section and whether each value is contained in the
249 * corresponding uci option value (see rpc_uci_match_option()).
250 * 3) A missing or empty "matches" table blob attribute is always considered
252 * Returns true if "type" matches or is NULL and "matches" matches or is NULL.
255 rpc_uci_match_section(struct uci_section *s,
256 struct blob_attr *type, struct blob_attr *matches)
258 struct uci_element *e;
259 struct blob_attr *cur;
265 if (type && strcmp(s->type, blobmsg_data(type)))
271 blobmsg_for_each_attr(cur, matches, rem)
273 if (!rpc_uci_format_blob(cur, &cmp))
276 uci_foreach_element(&s->options, e)
278 if (strcmp(e->name, blobmsg_name(cur)))
281 if (!rpc_uci_match_option(uci_to_option(e), cmp))
290 return (empty || match);
294 * Dump the given uci_option value into the global blobmsg buffer and use
295 * given "name" as key.
296 * 1) If the uci_option is of type list, put a table into the blob buffer and
297 * add each list item as string to it.
298 * 2) If the uci_option is of type string, put its value directly into the blob
302 rpc_uci_dump_option(struct uci_option *o, const char *name)
305 struct uci_element *e;
309 case UCI_TYPE_STRING:
310 blobmsg_add_string(&buf, name, o->v.string);
314 c = blobmsg_open_array(&buf, name);
316 uci_foreach_element(&o->v.list, e)
317 blobmsg_add_string(&buf, NULL, e->name);
319 blobmsg_close_array(&buf, c);
328 * Dump the given uci_section object into the global blobmsg buffer and use
329 * given "name" as key.
330 * Puts a table into the blob buffer and puts each section option member value
331 * as value into the table using the option name as key.
332 * Adds three special keys ".anonymous", ".type" and ".name" which specify the
333 * corresponding section properties.
336 rpc_uci_dump_section(struct uci_section *s, const char *name)
339 struct uci_option *o;
340 struct uci_element *e;
342 c = blobmsg_open_table(&buf, name);
344 blobmsg_add_u8(&buf, ".anonymous", s->anonymous);
345 blobmsg_add_string(&buf, ".type", s->type);
346 blobmsg_add_string(&buf, ".name", s->e.name);
348 uci_foreach_element(&s->options, e)
350 o = uci_to_option(e);
351 rpc_uci_dump_option(o, o->e.name);
354 blobmsg_close_table(&buf, c);
358 * Dump the given uci_package object into the global blobmsg buffer and use
359 * given "name" as key.
360 * Puts a table into the blob buffer and puts each package section member as
361 * value into the table using the section name as key.
362 * Only dumps sections matching the given "type" and "matches", see explaination
363 * of rpc_uci_match_section() for details.
366 rpc_uci_dump_package(struct uci_package *p, const char *name,
367 struct blob_attr *type, struct blob_attr *matches)
370 struct uci_element *e;
372 c = blobmsg_open_table(&buf, name);
374 uci_foreach_element(&p->sections, e)
376 if (!rpc_uci_match_section(uci_to_section(e), type, matches))
379 rpc_uci_dump_section(uci_to_section(e), e->name);
382 blobmsg_close_table(&buf, c);
387 rpc_uci_get(struct ubus_context *ctx, struct ubus_object *obj,
388 struct ubus_request_data *req, const char *method,
389 struct blob_attr *msg)
391 struct blob_attr *tb[__RPC_G_MAX];
392 struct uci_package *p = NULL;
393 struct uci_ptr ptr = { 0 };
395 blobmsg_parse(rpc_uci_get_policy, __RPC_G_MAX, tb,
396 blob_data(msg), blob_len(msg));
398 if (!tb[RPC_G_CONFIG])
399 return UBUS_STATUS_INVALID_ARGUMENT;
401 ptr.package = blobmsg_data(tb[RPC_G_CONFIG]);
402 uci_load(cursor, ptr.package, &p);
407 if (tb[RPC_G_SECTION])
409 ptr.section = blobmsg_data(tb[RPC_G_SECTION]);
411 if (tb[RPC_G_OPTION])
412 ptr.option = blobmsg_data(tb[RPC_G_OPTION]);
415 if (rpc_uci_lookup(&ptr) || !(ptr.flags & UCI_LOOKUP_COMPLETE))
418 blob_buf_init(&buf, 0);
420 switch (ptr.last->type)
422 case UCI_TYPE_PACKAGE:
423 rpc_uci_dump_package(ptr.p, "values", tb[RPC_G_TYPE], tb[RPC_G_MATCH]);
426 case UCI_TYPE_SECTION:
427 rpc_uci_dump_section(ptr.s, "values");
430 case UCI_TYPE_OPTION:
431 rpc_uci_dump_option(ptr.o, "value");
438 ubus_send_reply(ctx, req, buf.head);
442 uci_unload(cursor, p);
444 return rpc_uci_status();
448 rpc_uci_add(struct ubus_context *ctx, struct ubus_object *obj,
449 struct ubus_request_data *req, const char *method,
450 struct blob_attr *msg)
452 struct blob_attr *tb[__RPC_A_MAX];
453 struct blob_attr *cur, *elem;
454 struct uci_package *p = NULL;
455 struct uci_section *s;
456 struct uci_ptr ptr = { 0 };
459 blobmsg_parse(rpc_uci_add_policy, __RPC_A_MAX, tb,
460 blob_data(msg), blob_len(msg));
462 if (!tb[RPC_A_CONFIG] || !tb[RPC_A_TYPE])
463 return UBUS_STATUS_INVALID_ARGUMENT;
465 ptr.package = blobmsg_data(tb[RPC_A_CONFIG]);
467 uci_load(cursor, ptr.package, &p);
472 /* add named section */
475 ptr.section = blobmsg_data(tb[RPC_A_NAME]);
476 ptr.value = blobmsg_data(tb[RPC_A_TYPE]);
479 if (rpc_uci_lookup(&ptr) || uci_set(cursor, &ptr))
483 /* add anon section */
486 if (uci_add_section(cursor, p, blobmsg_data(tb[RPC_A_TYPE]), &s) || !s)
489 ptr.section = s->e.name;
492 if (tb[RPC_A_VALUES])
494 blobmsg_for_each_attr(cur, tb[RPC_A_VALUES], rem)
497 ptr.option = blobmsg_name(cur);
499 if (rpc_uci_lookup(&ptr) || !ptr.s)
502 switch (blobmsg_type(cur))
504 case BLOBMSG_TYPE_ARRAY:
505 blobmsg_for_each_attr(elem, cur, rem2)
506 if (rpc_uci_format_blob(elem, &ptr.value))
507 uci_add_list(cursor, &ptr);
511 if (rpc_uci_format_blob(cur, &ptr.value))
512 uci_set(cursor, &ptr);
520 blob_buf_init(&buf, 0);
521 blobmsg_add_string(&buf, "section", ptr.section);
522 ubus_send_reply(ctx, req, buf.head);
526 uci_unload(cursor, p);
528 return rpc_uci_status();
532 * Turn value from a blob attribute into uci set operation
533 * 1) if the blob is of type array, delete existing option (if any) and
534 * emit uci add_list operations for each element
535 * 2) if the blob is not an array but an option of type list exists,
536 * delete existing list and emit uci set operation for the blob value
537 * 3) in all other cases only emit a set operation if there is no existing
538 * option of if the existing options value differs from the blob value
541 rpc_uci_merge_set(struct blob_attr *opt, struct uci_ptr *ptr)
543 struct blob_attr *cur;
547 ptr->option = blobmsg_name(opt);
549 if (rpc_uci_lookup(ptr) || !ptr->s)
552 if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
555 uci_delete(cursor, ptr);
557 blobmsg_for_each_attr(cur, opt, rem)
558 if (rpc_uci_format_blob(cur, &ptr->value))
559 uci_add_list(cursor, ptr);
561 else if (ptr->o && ptr->o->type == UCI_TYPE_LIST)
563 uci_delete(cursor, ptr);
565 if (rpc_uci_format_blob(opt, &ptr->value))
566 uci_set(cursor, ptr);
568 else if (rpc_uci_format_blob(opt, &ptr->value))
570 if (!ptr->o || !ptr->o->v.string || strcmp(ptr->o->v.string, ptr->value))
571 uci_set(cursor, ptr);
576 rpc_uci_set(struct ubus_context *ctx, struct ubus_object *obj,
577 struct ubus_request_data *req, const char *method,
578 struct blob_attr *msg)
580 struct blob_attr *tb[__RPC_S_MAX];
581 struct blob_attr *cur;
582 struct uci_package *p = NULL;
583 struct uci_element *e;
584 struct uci_ptr ptr = { 0 };
587 blobmsg_parse(rpc_uci_set_policy, __RPC_S_MAX, tb,
588 blob_data(msg), blob_len(msg));
590 if (!tb[RPC_S_CONFIG] || !tb[RPC_S_VALUES] ||
591 (!tb[RPC_S_SECTION] && !tb[RPC_S_TYPE] && !tb[RPC_S_MATCH]))
592 return UBUS_STATUS_INVALID_ARGUMENT;
594 ptr.package = blobmsg_data(tb[RPC_S_CONFIG]);
595 uci_load(cursor, ptr.package, &p);
600 if (tb[RPC_S_SECTION])
602 ptr.section = blobmsg_data(tb[RPC_S_SECTION]);
603 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
604 rpc_uci_merge_set(cur, &ptr);
608 uci_foreach_element(&p->sections, e)
610 if (!rpc_uci_match_section(uci_to_section(e),
611 tb[RPC_S_TYPE], tb[RPC_S_MATCH]))
615 ptr.section = e->name;
617 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
618 rpc_uci_merge_set(cur, &ptr);
626 uci_unload(cursor, p);
628 return rpc_uci_status();
632 rpc_uci_rename(struct ubus_context *ctx, struct ubus_object *obj,
633 struct ubus_request_data *req, const char *method,
634 struct blob_attr *msg)
636 struct blob_attr *tb[__RPC_R_MAX];
637 struct uci_package *p = NULL;
638 struct uci_ptr ptr = { 0 };
640 blobmsg_parse(rpc_uci_rename_policy, __RPC_R_MAX, tb,
641 blob_data(msg), blob_len(msg));
643 if (!tb[RPC_R_CONFIG] || !tb[RPC_R_SECTION] || !tb[RPC_R_NAME])
644 return UBUS_STATUS_INVALID_ARGUMENT;
646 ptr.package = blobmsg_data(tb[RPC_R_CONFIG]);
647 ptr.section = blobmsg_data(tb[RPC_R_SECTION]);
648 ptr.value = blobmsg_data(tb[RPC_R_NAME]);
650 if (tb[RPC_R_OPTION])
651 ptr.option = blobmsg_data(tb[RPC_R_OPTION]);
653 uci_load(cursor, ptr.package, &p);
658 if (uci_lookup_ptr(cursor, &ptr, NULL, true))
661 if ((ptr.option && !ptr.o) || !ptr.s)
663 cursor->err = UCI_ERR_NOTFOUND;
667 if (uci_rename(cursor, &ptr))
674 uci_unload(cursor, p);
676 return rpc_uci_status();
680 rpc_uci_order(struct ubus_context *ctx, struct ubus_object *obj,
681 struct ubus_request_data *req, const char *method,
682 struct blob_attr *msg)
684 struct blob_attr *tb[__RPC_O_MAX];
685 struct blob_attr *cur;
686 struct uci_package *p = NULL;
687 struct uci_ptr ptr = { 0 };
690 blobmsg_parse(rpc_uci_order_policy, __RPC_O_MAX, tb,
691 blob_data(msg), blob_len(msg));
693 if (!tb[RPC_O_CONFIG] || !tb[RPC_O_SECTIONS])
694 return UBUS_STATUS_INVALID_ARGUMENT;
696 ptr.package = blobmsg_data(tb[RPC_O_CONFIG]);
698 uci_load(cursor, ptr.package, &p);
703 blobmsg_for_each_attr(cur, tb[RPC_O_SECTIONS], rem)
705 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
709 ptr.section = blobmsg_data(cur);
711 if (uci_lookup_ptr(cursor, &ptr, NULL, true) || !ptr.s)
714 uci_reorder_section(cursor, ptr.s, i++);
721 uci_unload(cursor, p);
723 return rpc_uci_status();
727 rpc_uci_dump_change(struct uci_delta *d)
730 const char *types[] = {
731 [UCI_CMD_REORDER] = "order",
732 [UCI_CMD_REMOVE] = "remove",
733 [UCI_CMD_RENAME] = "rename",
734 [UCI_CMD_ADD] = "add",
735 [UCI_CMD_LIST_ADD] = "list-add",
736 [UCI_CMD_LIST_DEL] = "list-del",
737 [UCI_CMD_CHANGE] = "set",
743 c = blobmsg_open_array(&buf, NULL);
745 blobmsg_add_string(&buf, NULL, types[d->cmd]);
746 blobmsg_add_string(&buf, NULL, d->section);
749 blobmsg_add_string(&buf, NULL, d->e.name);
753 if (d->cmd == UCI_CMD_REORDER)
754 blobmsg_add_u32(&buf, NULL, strtoul(d->value, NULL, 10));
756 blobmsg_add_string(&buf, NULL, d->value);
759 blobmsg_close_array(&buf, c);
763 rpc_uci_changes(struct ubus_context *ctx, struct ubus_object *obj,
764 struct ubus_request_data *req, const char *method,
765 struct blob_attr *msg)
767 struct blob_attr *tb[__RPC_C_MAX];
768 struct uci_package *p = NULL;
769 struct uci_element *e;
772 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
773 blob_data(msg), blob_len(msg));
775 if (!tb[RPC_C_CONFIG])
776 return UBUS_STATUS_INVALID_ARGUMENT;
778 uci_load(cursor, blobmsg_data(tb[RPC_C_CONFIG]), &p);
783 blob_buf_init(&buf, 0);
784 c = blobmsg_open_array(&buf, "changes");
786 uci_foreach_element(&p->saved_delta, e)
787 rpc_uci_dump_change(uci_to_delta(e));
789 blobmsg_close_array(&buf, c);
791 ubus_send_reply(ctx, req, buf.head);
795 uci_unload(cursor, p);
797 return rpc_uci_status();
801 rpc_uci_revert_commit(struct blob_attr *msg, bool commit)
803 struct blob_attr *tb[__RPC_C_MAX];
804 struct uci_package *p = NULL;
805 struct uci_ptr ptr = { 0 };
807 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
808 blob_data(msg), blob_len(msg));
810 if (!tb[RPC_C_CONFIG])
811 return UBUS_STATUS_INVALID_ARGUMENT;
813 ptr.package = blobmsg_data(tb[RPC_C_CONFIG]);
814 uci_load(cursor, ptr.package, &p);
816 if (!p || uci_lookup_ptr(cursor, &ptr, NULL, true) || !ptr.p)
820 uci_commit(cursor, &p, false);
822 uci_revert(cursor, &ptr);
826 uci_unload(cursor, p);
828 return rpc_uci_status();
832 rpc_uci_revert(struct ubus_context *ctx, struct ubus_object *obj,
833 struct ubus_request_data *req, const char *method,
834 struct blob_attr *msg)
836 return rpc_uci_revert_commit(msg, false);
840 rpc_uci_commit(struct ubus_context *ctx, struct ubus_object *obj,
841 struct ubus_request_data *req, const char *method,
842 struct blob_attr *msg)
844 return rpc_uci_revert_commit(msg, true);
848 int rpc_uci_api_init(struct ubus_context *ctx)
850 static const struct ubus_method uci_methods[] = {
851 UBUS_METHOD("get", rpc_uci_get, rpc_uci_get_policy),
852 UBUS_METHOD("add", rpc_uci_add, rpc_uci_add_policy),
853 UBUS_METHOD("set", rpc_uci_set, rpc_uci_set_policy),
854 UBUS_METHOD("rename", rpc_uci_rename, rpc_uci_rename_policy),
855 UBUS_METHOD("order", rpc_uci_order, rpc_uci_order_policy),
856 UBUS_METHOD("changes", rpc_uci_changes, rpc_uci_config_policy),
857 UBUS_METHOD("revert", rpc_uci_revert, rpc_uci_config_policy),
858 UBUS_METHOD("commit", rpc_uci_commit, rpc_uci_config_policy),
861 static struct ubus_object_type uci_type =
862 UBUS_OBJECT_TYPE("luci-rpc-uci", uci_methods);
864 static struct ubus_object obj = {
867 .methods = uci_methods,
868 .n_methods = ARRAY_SIZE(uci_methods),
871 cursor = uci_alloc_context();
874 return UBUS_STATUS_UNKNOWN_ERROR;
876 return ubus_add_object(ctx, &obj);