2 * rpcd - UBUS RPC server
4 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <libubox/blobmsg.h>
23 #include <libubox/blobmsg_json.h>
26 #include <rpcd/exec.h>
27 #include <rpcd/session.h>
29 static struct blob_buf buf;
30 static struct uci_context *cursor;
31 static struct uloop_timeout apply_timer;
32 static struct ubus_context *apply_ctx;
33 static char apply_sid[RPC_SID_LEN + 1];
45 static const struct blobmsg_policy rpc_uci_get_policy[__RPC_G_MAX] = {
46 [RPC_G_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
47 [RPC_G_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
48 [RPC_G_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
49 [RPC_G_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
50 [RPC_G_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
51 [RPC_G_SESSION] = { .name = "ubus_rpc_session",
52 .type = BLOBMSG_TYPE_STRING },
64 static const struct blobmsg_policy rpc_uci_add_policy[__RPC_A_MAX] = {
65 [RPC_A_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
66 [RPC_A_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
67 [RPC_A_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
68 [RPC_A_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
69 [RPC_A_SESSION] = { .name = "ubus_rpc_session",
70 .type = BLOBMSG_TYPE_STRING },
83 static const struct blobmsg_policy rpc_uci_set_policy[__RPC_S_MAX] = {
84 [RPC_S_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
85 [RPC_S_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
86 [RPC_S_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
87 [RPC_S_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
88 [RPC_S_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
89 [RPC_S_SESSION] = { .name = "ubus_rpc_session",
90 .type = BLOBMSG_TYPE_STRING },
104 static const struct blobmsg_policy rpc_uci_delete_policy[__RPC_D_MAX] = {
105 [RPC_D_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
106 [RPC_D_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
107 [RPC_D_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
108 [RPC_D_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
109 [RPC_D_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
110 [RPC_D_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_ARRAY },
111 [RPC_D_SESSION] = { .name = "ubus_rpc_session",
112 .type = BLOBMSG_TYPE_STRING },
124 static const struct blobmsg_policy rpc_uci_rename_policy[__RPC_R_MAX] = {
125 [RPC_R_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
126 [RPC_R_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
127 [RPC_R_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
128 [RPC_R_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
129 [RPC_R_SESSION] = { .name = "ubus_rpc_session",
130 .type = BLOBMSG_TYPE_STRING },
140 static const struct blobmsg_policy rpc_uci_order_policy[__RPC_O_MAX] = {
141 [RPC_O_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
142 [RPC_O_SECTIONS] = { .name = "sections", .type = BLOBMSG_TYPE_ARRAY },
143 [RPC_O_SESSION] = { .name = "ubus_rpc_session",
144 .type = BLOBMSG_TYPE_STRING },
153 static const struct blobmsg_policy rpc_uci_config_policy[__RPC_C_MAX] = {
154 [RPC_C_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
155 [RPC_C_SESSION] = { .name = "ubus_rpc_session",
156 .type = BLOBMSG_TYPE_STRING },
166 static const struct blobmsg_policy rpc_uci_apply_policy[__RPC_T_MAX] = {
167 [RPC_T_ROLLBACK] = { .name = "rollback", .type = BLOBMSG_TYPE_BOOL },
168 [RPC_T_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
169 [RPC_T_SESSION] = { .name = "ubus_rpc_session",
170 .type = BLOBMSG_TYPE_STRING },
178 static const struct blobmsg_policy rpc_uci_rollback_policy[__RPC_B_MAX] = {
179 [RPC_B_SESSION] = { .name = "ubus_rpc_session",
180 .type = BLOBMSG_TYPE_STRING },
184 * Turn uci error state into ubus return code
192 return UBUS_STATUS_OK;
195 return UBUS_STATUS_INVALID_ARGUMENT;
197 case UCI_ERR_NOTFOUND:
198 return UBUS_STATUS_NOT_FOUND;
201 return UBUS_STATUS_UNKNOWN_ERROR;
206 * Setup per-session delta save directory. If the passed "sid" blob attribute
207 * pointer is NULL then the precedure was not invoked through the ubus-rpc so
208 * we do not perform session isolation and use the default save directory.
211 rpc_uci_set_savedir(struct blob_attr *sid)
217 uci_set_savedir(cursor, "/tmp/.uci");
221 snprintf(path, sizeof(path) - 1,
222 RPC_UCI_SAVEDIR_PREFIX "%s", blobmsg_get_string(sid));
224 uci_set_savedir(cursor, path);
228 * Test read access to given config. If the passed "sid" blob attribute pointer
229 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
230 * perform access control and always assume true.
233 rpc_uci_read_access(struct blob_attr *sid, struct blob_attr *config)
235 rpc_uci_set_savedir(sid);
240 return rpc_session_access(blobmsg_data(sid), "uci",
241 blobmsg_data(config), "read");
245 * Test write access to given config. If the passed "sid" blob attribute pointer
246 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
247 * perform access control and always assume true.
250 rpc_uci_write_access(struct blob_attr *sid, struct blob_attr *config)
252 rpc_uci_set_savedir(sid);
257 return rpc_session_access(blobmsg_data(sid), "uci",
258 blobmsg_data(config), "write");
262 * Format applicable blob value as string and place a pointer to the string
263 * buffer in "p". Uses a static string buffer.
266 rpc_uci_format_blob(struct blob_attr *v, const char **p)
272 switch (blobmsg_type(v))
274 case BLOBMSG_TYPE_STRING:
275 *p = blobmsg_data(v);
278 case BLOBMSG_TYPE_INT64:
279 snprintf(buf, sizeof(buf), "%"PRIu64, blobmsg_get_u64(v));
283 case BLOBMSG_TYPE_INT32:
284 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u32(v));
288 case BLOBMSG_TYPE_INT16:
289 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u16(v));
293 case BLOBMSG_TYPE_INT8:
294 snprintf(buf, sizeof(buf), "%u", !!blobmsg_get_u8(v));
306 * Lookup the given uci_ptr and enable extended lookup format if the .section
307 * value of the uci_ptr looks like extended syntax. Uses an internal copy
308 * of the given uci_ptr to perform the lookup as failing extended section
309 * lookup operations in libuci will zero our the uci_ptr struct.
310 * Copies the internal uci_ptr back to given the uci_ptr on success.
313 rpc_uci_lookup(struct uci_ptr *ptr)
316 struct uci_ptr lookup = *ptr;
318 if (!lookup.s && lookup.section && *lookup.section == '@')
319 lookup.flags |= UCI_LOOKUP_EXTENDED;
321 rv = uci_lookup_ptr(cursor, &lookup, NULL, true);
330 * Checks whether the given uci_option object matches the given string value.
331 * 1) If the uci_option is of type list, check whether any of the list elements
332 * equals to the given string
333 * 2) If the uci_option is of type string, parse it into space separated tokens
334 * and check if any of the tokens equals to the given string.
335 * Returns true if a list element or token matched the given string.
338 rpc_uci_match_option(struct uci_option *o, const char *cmp)
340 struct uci_element *e;
343 if (o->type == UCI_TYPE_LIST)
345 uci_foreach_element(&o->v.list, e)
346 if (e->name && !strcmp(e->name, cmp))
355 s = strdup(o->v.string);
360 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
374 * Checks whether the given uci_section matches the type and value blob attrs.
375 * 1) Returns false if "type" is given and the section type does not match
376 * the value specified in the "type" string blob attribute, else continue.
377 * 2) Tests whether any key in the "matches" table blob attribute exists in
378 * the given uci_section and whether each value is contained in the
379 * corresponding uci option value (see rpc_uci_match_option()).
380 * 3) A missing or empty "matches" table blob attribute is always considered
382 * Returns true if "type" matches or is NULL and "matches" matches or is NULL.
385 rpc_uci_match_section(struct uci_section *s,
386 struct blob_attr *type, struct blob_attr *matches)
388 struct uci_element *e;
389 struct blob_attr *cur;
395 if (type && strcmp(s->type, blobmsg_data(type)))
401 blobmsg_for_each_attr(cur, matches, rem)
403 if (!rpc_uci_format_blob(cur, &cmp))
406 uci_foreach_element(&s->options, e)
408 if (strcmp(e->name, blobmsg_name(cur)))
411 if (!rpc_uci_match_option(uci_to_option(e), cmp))
420 return (empty || match);
424 * Dump the given uci_option value into the global blobmsg buffer and use
425 * given "name" as key.
426 * 1) If the uci_option is of type list, put a table into the blob buffer and
427 * add each list item as string to it.
428 * 2) If the uci_option is of type string, put its value directly into the blob
432 rpc_uci_dump_option(struct uci_option *o, const char *name)
435 struct uci_element *e;
439 case UCI_TYPE_STRING:
440 blobmsg_add_string(&buf, name, o->v.string);
444 c = blobmsg_open_array(&buf, name);
446 uci_foreach_element(&o->v.list, e)
447 blobmsg_add_string(&buf, NULL, e->name);
449 blobmsg_close_array(&buf, c);
458 * Dump the given uci_section object into the global blobmsg buffer and use
459 * given "name" as key.
460 * Puts a table into the blob buffer and puts each section option member value
461 * as value into the table using the option name as key.
462 * Adds three special keys ".anonymous", ".type" and ".name" which specify the
463 * corresponding section properties.
466 rpc_uci_dump_section(struct uci_section *s, const char *name, int index)
469 struct uci_option *o;
470 struct uci_element *e;
472 c = blobmsg_open_table(&buf, name);
474 blobmsg_add_u8(&buf, ".anonymous", s->anonymous);
475 blobmsg_add_string(&buf, ".type", s->type);
476 blobmsg_add_string(&buf, ".name", s->e.name);
479 blobmsg_add_u32(&buf, ".index", index);
481 uci_foreach_element(&s->options, e)
483 o = uci_to_option(e);
484 rpc_uci_dump_option(o, o->e.name);
487 blobmsg_close_table(&buf, c);
491 * Dump the given uci_package object into the global blobmsg buffer and use
492 * given "name" as key.
493 * Puts a table into the blob buffer and puts each package section member as
494 * value into the table using the section name as key.
495 * Only dumps sections matching the given "type" and "matches", see explaination
496 * of rpc_uci_match_section() for details.
499 rpc_uci_dump_package(struct uci_package *p, const char *name,
500 struct blob_attr *type, struct blob_attr *matches)
503 struct uci_element *e;
506 c = blobmsg_open_table(&buf, name);
508 uci_foreach_element(&p->sections, e)
512 if (!rpc_uci_match_section(uci_to_section(e), type, matches))
515 rpc_uci_dump_section(uci_to_section(e), e->name, i);
518 blobmsg_close_table(&buf, c);
523 rpc_uci_getcommon(struct ubus_context *ctx, struct ubus_request_data *req,
524 struct blob_attr *msg, bool use_state)
526 struct blob_attr *tb[__RPC_G_MAX];
527 struct uci_package *p = NULL;
528 struct uci_ptr ptr = { 0 };
530 blobmsg_parse(rpc_uci_get_policy, __RPC_G_MAX, tb,
531 blob_data(msg), blob_len(msg));
533 if (!tb[RPC_G_CONFIG])
534 return UBUS_STATUS_INVALID_ARGUMENT;
536 if (!rpc_uci_read_access(tb[RPC_G_SESSION], tb[RPC_G_CONFIG]))
537 return UBUS_STATUS_PERMISSION_DENIED;
539 ptr.package = blobmsg_data(tb[RPC_G_CONFIG]);
542 uci_set_savedir(cursor, "/var/state");
544 if (uci_load(cursor, ptr.package, &p))
545 return rpc_uci_status();
547 if (tb[RPC_G_SECTION])
549 ptr.section = blobmsg_data(tb[RPC_G_SECTION]);
551 if (tb[RPC_G_OPTION])
552 ptr.option = blobmsg_data(tb[RPC_G_OPTION]);
555 if (rpc_uci_lookup(&ptr) || !(ptr.flags & UCI_LOOKUP_COMPLETE))
558 blob_buf_init(&buf, 0);
560 switch (ptr.last->type)
562 case UCI_TYPE_PACKAGE:
563 rpc_uci_dump_package(ptr.p, "values", tb[RPC_G_TYPE], tb[RPC_G_MATCH]);
566 case UCI_TYPE_SECTION:
567 rpc_uci_dump_section(ptr.s, "values", -1);
570 case UCI_TYPE_OPTION:
571 rpc_uci_dump_option(ptr.o, "value");
578 ubus_send_reply(ctx, req, buf.head);
581 uci_unload(cursor, p);
583 return rpc_uci_status();
587 rpc_uci_get(struct ubus_context *ctx, struct ubus_object *obj,
588 struct ubus_request_data *req, const char *method,
589 struct blob_attr *msg)
591 return rpc_uci_getcommon(ctx, req, msg, false);
595 rpc_uci_state(struct ubus_context *ctx, struct ubus_object *obj,
596 struct ubus_request_data *req, const char *method,
597 struct blob_attr *msg)
599 return rpc_uci_getcommon(ctx, req, msg, true);
603 rpc_uci_add(struct ubus_context *ctx, struct ubus_object *obj,
604 struct ubus_request_data *req, const char *method,
605 struct blob_attr *msg)
607 struct blob_attr *tb[__RPC_A_MAX];
608 struct blob_attr *cur, *elem;
609 struct uci_package *p = NULL;
610 struct uci_section *s;
611 struct uci_ptr ptr = { 0 };
614 blobmsg_parse(rpc_uci_add_policy, __RPC_A_MAX, tb,
615 blob_data(msg), blob_len(msg));
617 if (!tb[RPC_A_CONFIG] || !tb[RPC_A_TYPE])
618 return UBUS_STATUS_INVALID_ARGUMENT;
620 if (!rpc_uci_write_access(tb[RPC_A_SESSION], tb[RPC_A_CONFIG]))
621 return UBUS_STATUS_PERMISSION_DENIED;
623 ptr.package = blobmsg_data(tb[RPC_A_CONFIG]);
625 if (uci_load(cursor, ptr.package, &p))
626 return rpc_uci_status();
628 /* add named section */
631 ptr.section = blobmsg_data(tb[RPC_A_NAME]);
632 ptr.value = blobmsg_data(tb[RPC_A_TYPE]);
635 if (rpc_uci_lookup(&ptr) || uci_set(cursor, &ptr))
639 /* add anon section */
642 if (uci_add_section(cursor, p, blobmsg_data(tb[RPC_A_TYPE]), &s) || !s)
645 ptr.section = s->e.name;
648 if (tb[RPC_A_VALUES])
650 blobmsg_for_each_attr(cur, tb[RPC_A_VALUES], rem)
653 ptr.option = blobmsg_name(cur);
655 if (rpc_uci_lookup(&ptr) || !ptr.s)
658 switch (blobmsg_type(cur))
660 case BLOBMSG_TYPE_ARRAY:
661 blobmsg_for_each_attr(elem, cur, rem2)
662 if (rpc_uci_format_blob(elem, &ptr.value))
663 uci_add_list(cursor, &ptr);
667 if (rpc_uci_format_blob(cur, &ptr.value))
668 uci_set(cursor, &ptr);
676 blob_buf_init(&buf, 0);
677 blobmsg_add_string(&buf, "section", ptr.section);
678 ubus_send_reply(ctx, req, buf.head);
681 uci_unload(cursor, p);
683 return rpc_uci_status();
687 * Turn value from a blob attribute into uci set operation
688 * 1) if the blob is of type array, delete existing option (if any) and
689 * emit uci add_list operations for each element
690 * 2) if the blob is not an array but an option of type list exists,
691 * delete existing list and emit uci set operation for the blob value
692 * 3) in all other cases only emit a set operation if there is no existing
693 * option of if the existing options value differs from the blob value
696 rpc_uci_merge_set(struct blob_attr *opt, struct uci_ptr *ptr)
698 struct blob_attr *cur;
702 ptr->option = blobmsg_name(opt);
705 if (rpc_uci_lookup(ptr) || !ptr->s)
708 if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
711 uci_delete(cursor, ptr);
713 blobmsg_for_each_attr(cur, opt, rem)
714 if (rpc_uci_format_blob(cur, &ptr->value))
715 uci_add_list(cursor, ptr);
717 else if (ptr->o && ptr->o->type == UCI_TYPE_LIST)
719 uci_delete(cursor, ptr);
721 if (rpc_uci_format_blob(opt, &ptr->value))
722 uci_set(cursor, ptr);
724 else if (rpc_uci_format_blob(opt, &ptr->value))
726 if (!ptr->o || !ptr->o->v.string || strcmp(ptr->o->v.string, ptr->value))
727 uci_set(cursor, ptr);
732 rpc_uci_set(struct ubus_context *ctx, struct ubus_object *obj,
733 struct ubus_request_data *req, const char *method,
734 struct blob_attr *msg)
736 struct blob_attr *tb[__RPC_S_MAX];
737 struct blob_attr *cur;
738 struct uci_package *p = NULL;
739 struct uci_element *e;
740 struct uci_ptr ptr = { 0 };
743 blobmsg_parse(rpc_uci_set_policy, __RPC_S_MAX, tb,
744 blob_data(msg), blob_len(msg));
746 if (!tb[RPC_S_CONFIG] || !tb[RPC_S_VALUES] ||
747 (!tb[RPC_S_SECTION] && !tb[RPC_S_TYPE] && !tb[RPC_S_MATCH]))
748 return UBUS_STATUS_INVALID_ARGUMENT;
750 if (!rpc_uci_write_access(tb[RPC_S_SESSION], tb[RPC_S_CONFIG]))
751 return UBUS_STATUS_PERMISSION_DENIED;
753 ptr.package = blobmsg_data(tb[RPC_S_CONFIG]);
755 if (uci_load(cursor, ptr.package, &p))
756 return rpc_uci_status();
758 if (tb[RPC_S_SECTION])
760 ptr.section = blobmsg_data(tb[RPC_S_SECTION]);
761 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
762 rpc_uci_merge_set(cur, &ptr);
766 uci_foreach_element(&p->sections, e)
768 if (!rpc_uci_match_section(uci_to_section(e),
769 tb[RPC_S_TYPE], tb[RPC_S_MATCH]))
773 ptr.section = e->name;
775 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
776 rpc_uci_merge_set(cur, &ptr);
781 uci_unload(cursor, p);
783 return rpc_uci_status();
787 * Delete option or section from uci specified by given blob attribute pointer
788 * 1) if the blob is of type array, delete any option named after each element
789 * 2) if the blob is of type string, delete the option named after its value
790 * 3) if the blob is NULL, delete entire section
793 rpc_uci_merge_delete(struct blob_attr *opt, struct uci_ptr *ptr)
795 struct blob_attr *cur;
798 if (rpc_uci_lookup(ptr) || !ptr->s)
806 uci_delete(cursor, ptr);
808 else if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
810 blobmsg_for_each_attr(cur, opt, rem)
812 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
816 ptr->option = blobmsg_data(cur);
818 if (rpc_uci_lookup(ptr) || !ptr->o)
821 uci_delete(cursor, ptr);
824 else if (blobmsg_type(opt) == BLOBMSG_TYPE_STRING)
827 ptr->option = blobmsg_data(opt);
829 if (rpc_uci_lookup(ptr) || !ptr->o)
832 uci_delete(cursor, ptr);
837 rpc_uci_delete(struct ubus_context *ctx, struct ubus_object *obj,
838 struct ubus_request_data *req, const char *method,
839 struct blob_attr *msg)
841 struct blob_attr *tb[__RPC_D_MAX];
842 struct uci_package *p = NULL;
843 struct uci_element *e, *tmp;
844 struct uci_ptr ptr = { 0 };
846 blobmsg_parse(rpc_uci_delete_policy, __RPC_D_MAX, tb,
847 blob_data(msg), blob_len(msg));
849 if (!tb[RPC_D_CONFIG] ||
850 (!tb[RPC_D_SECTION] && !tb[RPC_D_TYPE] && !tb[RPC_D_MATCH]))
851 return UBUS_STATUS_INVALID_ARGUMENT;
853 if (!rpc_uci_write_access(tb[RPC_D_SESSION], tb[RPC_D_CONFIG]))
854 return UBUS_STATUS_PERMISSION_DENIED;
856 ptr.package = blobmsg_data(tb[RPC_D_CONFIG]);
858 if (uci_load(cursor, ptr.package, &p))
859 return rpc_uci_status();
861 if (tb[RPC_D_SECTION])
863 ptr.section = blobmsg_data(tb[RPC_D_SECTION]);
865 if (tb[RPC_D_OPTIONS])
866 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
868 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
872 uci_foreach_element_safe(&p->sections, tmp, e)
874 if (!rpc_uci_match_section(uci_to_section(e),
875 tb[RPC_D_TYPE], tb[RPC_D_MATCH]))
879 ptr.section = e->name;
881 if (tb[RPC_D_OPTIONS])
882 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
884 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
889 uci_unload(cursor, p);
891 return rpc_uci_status();
895 rpc_uci_rename(struct ubus_context *ctx, struct ubus_object *obj,
896 struct ubus_request_data *req, const char *method,
897 struct blob_attr *msg)
899 struct blob_attr *tb[__RPC_R_MAX];
900 struct uci_package *p = NULL;
901 struct uci_ptr ptr = { 0 };
903 blobmsg_parse(rpc_uci_rename_policy, __RPC_R_MAX, tb,
904 blob_data(msg), blob_len(msg));
906 if (!tb[RPC_R_CONFIG] || !tb[RPC_R_SECTION] || !tb[RPC_R_NAME])
907 return UBUS_STATUS_INVALID_ARGUMENT;
909 if (!rpc_uci_write_access(tb[RPC_R_SESSION], tb[RPC_R_CONFIG]))
910 return UBUS_STATUS_PERMISSION_DENIED;
912 ptr.package = blobmsg_data(tb[RPC_R_CONFIG]);
913 ptr.section = blobmsg_data(tb[RPC_R_SECTION]);
914 ptr.value = blobmsg_data(tb[RPC_R_NAME]);
916 if (tb[RPC_R_OPTION])
917 ptr.option = blobmsg_data(tb[RPC_R_OPTION]);
919 if (uci_load(cursor, ptr.package, &p))
920 return rpc_uci_status();
922 if (uci_lookup_ptr(cursor, &ptr, NULL, true))
925 if ((ptr.option && !ptr.o) || !ptr.s)
927 cursor->err = UCI_ERR_NOTFOUND;
931 if (uci_rename(cursor, &ptr))
937 uci_unload(cursor, p);
939 return rpc_uci_status();
943 rpc_uci_order(struct ubus_context *ctx, struct ubus_object *obj,
944 struct ubus_request_data *req, const char *method,
945 struct blob_attr *msg)
947 struct blob_attr *tb[__RPC_O_MAX];
948 struct blob_attr *cur;
949 struct uci_package *p = NULL;
950 struct uci_ptr ptr = { 0 };
953 blobmsg_parse(rpc_uci_order_policy, __RPC_O_MAX, tb,
954 blob_data(msg), blob_len(msg));
956 if (!tb[RPC_O_CONFIG] || !tb[RPC_O_SECTIONS])
957 return UBUS_STATUS_INVALID_ARGUMENT;
959 if (!rpc_uci_write_access(tb[RPC_O_SESSION], tb[RPC_O_CONFIG]))
960 return UBUS_STATUS_PERMISSION_DENIED;
962 ptr.package = blobmsg_data(tb[RPC_O_CONFIG]);
964 if (uci_load(cursor, ptr.package, &p))
965 return rpc_uci_status();
967 blobmsg_for_each_attr(cur, tb[RPC_O_SECTIONS], rem)
969 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
973 ptr.section = blobmsg_data(cur);
975 if (uci_lookup_ptr(cursor, &ptr, NULL, true) || !ptr.s)
978 uci_reorder_section(cursor, ptr.s, i++);
982 uci_unload(cursor, p);
984 return rpc_uci_status();
988 rpc_uci_dump_change(struct uci_delta *d)
991 const char *types[] = {
992 [UCI_CMD_REORDER] = "order",
993 [UCI_CMD_REMOVE] = "remove",
994 [UCI_CMD_RENAME] = "rename",
995 [UCI_CMD_ADD] = "add",
996 [UCI_CMD_LIST_ADD] = "list-add",
997 [UCI_CMD_LIST_DEL] = "list-del",
998 [UCI_CMD_CHANGE] = "set",
1004 c = blobmsg_open_array(&buf, NULL);
1006 blobmsg_add_string(&buf, NULL, types[d->cmd]);
1007 blobmsg_add_string(&buf, NULL, d->section);
1010 blobmsg_add_string(&buf, NULL, d->e.name);
1014 if (d->cmd == UCI_CMD_REORDER)
1015 blobmsg_add_u32(&buf, NULL, strtoul(d->value, NULL, 10));
1017 blobmsg_add_string(&buf, NULL, d->value);
1020 blobmsg_close_array(&buf, c);
1024 rpc_uci_changes(struct ubus_context *ctx, struct ubus_object *obj,
1025 struct ubus_request_data *req, const char *method,
1026 struct blob_attr *msg)
1028 struct blob_attr *tb[__RPC_C_MAX];
1029 struct uci_package *p = NULL;
1030 struct uci_element *e;
1035 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
1036 blob_data(msg), blob_len(msg));
1038 if (tb[RPC_C_CONFIG])
1040 if (!rpc_uci_read_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
1041 return UBUS_STATUS_PERMISSION_DENIED;
1043 if (uci_load(cursor, blobmsg_data(tb[RPC_C_CONFIG]), &p))
1044 return rpc_uci_status();
1046 blob_buf_init(&buf, 0);
1047 c = blobmsg_open_array(&buf, "changes");
1049 uci_foreach_element(&p->saved_delta, e)
1050 rpc_uci_dump_change(uci_to_delta(e));
1052 blobmsg_close_array(&buf, c);
1054 uci_unload(cursor, p);
1056 ubus_send_reply(ctx, req, buf.head);
1058 return rpc_uci_status();
1061 rpc_uci_set_savedir(tb[RPC_C_SESSION]);
1063 if (uci_list_configs(cursor, &configs))
1064 return rpc_uci_status();
1066 blob_buf_init(&buf, 0);
1068 c = blobmsg_open_table(&buf, "changes");
1070 for (i = 0; configs[i]; i++)
1072 if (tb[RPC_C_SESSION] &&
1073 !rpc_session_access(blobmsg_data(tb[RPC_C_SESSION]), "uci",
1074 configs[i], "read"))
1077 if (uci_load(cursor, configs[i], &p))
1080 if (!uci_list_empty(&p->saved_delta))
1082 d = blobmsg_open_array(&buf, configs[i]);
1084 uci_foreach_element(&p->saved_delta, e)
1085 rpc_uci_dump_change(uci_to_delta(e));
1087 blobmsg_close_array(&buf, d);
1090 uci_unload(cursor, p);
1093 blobmsg_close_table(&buf, c);
1095 ubus_send_reply(ctx, req, buf.head);
1101 rpc_uci_trigger_event(struct ubus_context *ctx, const char *config)
1103 char *pkg = strdup(config);
1104 static struct blob_buf b;
1107 if (!ubus_lookup_id(ctx, "service", &id)) {
1110 blob_buf_init(&b, 0);
1111 blobmsg_add_string(&b, "type", "config.change");
1112 c = blobmsg_open_table(&b, "data");
1113 blobmsg_add_string(&b, "package", pkg);
1114 blobmsg_close_table(&b, c);
1115 ubus_invoke(ctx, id, "event", b.head, NULL, 0, 1000);
1121 rpc_uci_revert_commit(struct ubus_context *ctx, struct blob_attr *msg, bool commit)
1123 struct blob_attr *tb[__RPC_C_MAX];
1124 struct uci_package *p = NULL;
1125 struct uci_ptr ptr = { 0 };
1128 return UBUS_STATUS_PERMISSION_DENIED;
1130 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
1131 blob_data(msg), blob_len(msg));
1133 if (!tb[RPC_C_CONFIG])
1134 return UBUS_STATUS_INVALID_ARGUMENT;
1136 if (!rpc_uci_write_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
1137 return UBUS_STATUS_PERMISSION_DENIED;
1139 ptr.package = blobmsg_data(tb[RPC_C_CONFIG]);
1143 if (!uci_load(cursor, ptr.package, &p))
1145 uci_commit(cursor, &p, false);
1146 uci_unload(cursor, p);
1147 rpc_uci_trigger_event(ctx, blobmsg_get_string(tb[RPC_C_CONFIG]));
1152 if (!uci_lookup_ptr(cursor, &ptr, NULL, true) && ptr.p)
1154 uci_revert(cursor, &ptr);
1155 uci_unload(cursor, ptr.p);
1159 return rpc_uci_status();
1163 rpc_uci_revert(struct ubus_context *ctx, struct ubus_object *obj,
1164 struct ubus_request_data *req, const char *method,
1165 struct blob_attr *msg)
1167 return rpc_uci_revert_commit(ctx, msg, false);
1171 rpc_uci_commit(struct ubus_context *ctx, struct ubus_object *obj,
1172 struct ubus_request_data *req, const char *method,
1173 struct blob_attr *msg)
1175 return rpc_uci_revert_commit(ctx, msg, true);
1179 rpc_uci_configs(struct ubus_context *ctx, struct ubus_object *obj,
1180 struct ubus_request_data *req, const char *method,
1181 struct blob_attr *msg)
1187 if (uci_list_configs(cursor, &configs))
1190 blob_buf_init(&buf, 0);
1192 c = blobmsg_open_array(&buf, "configs");
1194 for (i = 0; configs[i]; i++)
1195 blobmsg_add_string(&buf, NULL, configs[i]);
1197 blobmsg_close_array(&buf, c);
1199 ubus_send_reply(ctx, req, buf.head);
1202 return rpc_uci_status();
1207 * Remove given delta save directory (if any).
1210 rpc_uci_purge_dir(const char *path)
1215 char file[PATH_MAX];
1217 if (stat(path, &s) || !S_ISDIR(s.st_mode))
1220 if ((d = opendir(path)) != NULL)
1222 while ((e = readdir(d)) != NULL)
1224 snprintf(file, sizeof(file) - 1, "%s/%s", path, e->d_name);
1226 if (stat(file, &s) || !S_ISREG(s.st_mode))
1239 rpc_uci_apply_config(struct ubus_context *ctx, char *config)
1241 struct uci_package *p = NULL;
1243 if (!uci_load(cursor, config, &p)) {
1244 uci_commit(cursor, &p, false);
1245 uci_unload(cursor, p);
1247 rpc_uci_trigger_event(ctx, config);
1253 rpc_uci_copy_file(const char *src, const char *target, const char *file)
1258 snprintf(tmp, sizeof(tmp), "%s%s", src, file);
1259 in = fopen(tmp, "rb");
1260 snprintf(tmp, sizeof(tmp), "%s%s", target, file);
1261 out = fopen(tmp, "wb+");
1264 int len = fread(tmp, 1, sizeof(tmp), in);
1267 fwrite(tmp, 1, len, out);
1276 rpc_uci_do_rollback(struct ubus_context *ctx, const char *sid, glob_t *gl)
1282 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
1286 for (i = 0; i < gl->gl_pathc; i++) {
1287 char *config = basename(gl->gl_pathv[i]);
1292 rpc_uci_copy_file(RPC_SNAPSHOT_FILES, RPC_UCI_DIR, config);
1293 rpc_uci_apply_config(ctx, config);
1295 rpc_uci_copy_file(RPC_SNAPSHOT_DELTA, tmp, config);
1298 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1299 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1301 uloop_timeout_cancel(&apply_timer);
1302 memset(apply_sid, 0, sizeof(apply_sid));
1307 rpc_uci_apply_timeout(struct uloop_timeout *t)
1312 snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
1313 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1316 rpc_uci_do_rollback(apply_ctx, NULL, &gl);
1320 rpc_uci_apply_access(const char *sid, glob_t *gl)
1325 if (gl->gl_pathc < 3)
1326 return UBUS_STATUS_NO_DATA;
1328 for (i = 0; i < gl->gl_pathc; i++) {
1329 char *config = basename(gl->gl_pathv[i]);
1333 if (stat(gl->gl_pathv[i], &s) || !s.st_size)
1335 if (!rpc_session_access(sid, "uci", config, "write"))
1336 return UBUS_STATUS_PERMISSION_DENIED;
1341 return UBUS_STATUS_NO_DATA;
1347 rpc_uci_apply(struct ubus_context *ctx, struct ubus_object *obj,
1348 struct ubus_request_data *req, const char *method,
1349 struct blob_attr *msg)
1351 struct blob_attr *tb[__RPC_T_MAX];
1352 int timeout = RPC_APPLY_TIMEOUT;
1354 bool rollback = false;
1359 blobmsg_parse(rpc_uci_apply_policy, __RPC_T_MAX, tb,
1360 blob_data(msg), blob_len(msg));
1362 if (tb[RPC_T_ROLLBACK])
1363 rollback = blobmsg_get_bool(tb[RPC_T_ROLLBACK]);
1365 if (apply_sid[0] && rollback)
1366 return UBUS_STATUS_PERMISSION_DENIED;
1368 if (!tb[RPC_T_SESSION])
1369 return UBUS_STATUS_INVALID_ARGUMENT;
1371 sid = blobmsg_data(tb[RPC_T_SESSION]);
1373 if (tb[RPC_T_TIMEOUT])
1374 timeout = blobmsg_get_u32(tb[RPC_T_TIMEOUT]);
1376 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1377 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1379 if (!apply_sid[0]) {
1380 mkdir(RPC_SNAPSHOT_FILES, 0700);
1381 mkdir(RPC_SNAPSHOT_DELTA, 0700);
1383 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/*", sid);
1384 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1385 return UBUS_STATUS_NOT_FOUND;
1387 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
1389 ret = rpc_uci_apply_access(sid, &gl);
1395 /* copy SID early because rpc_uci_apply_config() will clobber buf */
1397 strncpy(apply_sid, sid, RPC_SID_LEN);
1399 for (i = 0; i < gl.gl_pathc; i++) {
1400 char *config = basename(gl.gl_pathv[i]);
1406 if (stat(gl.gl_pathv[i], &s) || !s.st_size)
1409 rpc_uci_copy_file(RPC_UCI_DIR, RPC_SNAPSHOT_FILES, config);
1410 rpc_uci_copy_file(tmp, RPC_SNAPSHOT_DELTA, config);
1411 rpc_uci_apply_config(ctx, config);
1417 apply_timer.cb = rpc_uci_apply_timeout;
1418 uloop_timeout_set(&apply_timer, timeout * 1000);
1427 rpc_uci_confirm(struct ubus_context *ctx, struct ubus_object *obj,
1428 struct ubus_request_data *req, const char *method,
1429 struct blob_attr *msg)
1431 struct blob_attr *tb[__RPC_B_MAX];
1434 blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
1435 blob_data(msg), blob_len(msg));
1437 if (!tb[RPC_B_SESSION])
1438 return UBUS_STATUS_INVALID_ARGUMENT;
1440 sid = blobmsg_data(tb[RPC_B_SESSION]);
1443 return UBUS_STATUS_NO_DATA;
1445 if (strcmp(apply_sid, sid))
1446 return UBUS_STATUS_PERMISSION_DENIED;
1448 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1449 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1451 uloop_timeout_cancel(&apply_timer);
1452 memset(apply_sid, 0, sizeof(apply_sid));
1459 rpc_uci_rollback(struct ubus_context *ctx, struct ubus_object *obj,
1460 struct ubus_request_data *req, const char *method,
1461 struct blob_attr *msg)
1463 struct blob_attr *tb[__RPC_B_MAX];
1468 blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
1469 blob_data(msg), blob_len(msg));
1472 return UBUS_STATUS_NO_DATA;
1474 if (!tb[RPC_B_SESSION])
1475 return UBUS_STATUS_INVALID_ARGUMENT;
1477 sid = blobmsg_data(tb[RPC_B_SESSION]);
1479 if (strcmp(apply_sid, sid))
1480 return UBUS_STATUS_PERMISSION_DENIED;
1482 snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
1483 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1484 return UBUS_STATUS_NOT_FOUND;
1486 rpc_uci_do_rollback(ctx, sid, &gl);
1494 rpc_uci_reload(struct ubus_context *ctx, struct ubus_object *obj,
1495 struct ubus_request_data *req, const char *method,
1496 struct blob_attr *msg)
1498 char * const cmd[2] = { "/sbin/reload_config", NULL };
1501 /* wait for the RPC call to complete */
1503 return execv(cmd[0], cmd);
1510 * Session destroy callback to purge associated delta directory.
1513 rpc_uci_purge_savedir_cb(struct rpc_session *ses, void *priv)
1515 char path[PATH_MAX];
1517 snprintf(path, sizeof(path) - 1, RPC_UCI_SAVEDIR_PREFIX "%s", ses->id);
1518 rpc_uci_purge_dir(path);
1522 * Removes all delta directories which match the RPC_UCI_SAVEDIR_PREFIX.
1523 * This is used to clean up garbage when starting rpcd.
1525 void rpc_uci_purge_savedirs(void)
1530 if (!glob(RPC_UCI_SAVEDIR_PREFIX "*", 0, NULL, &gl))
1532 for (i = 0; i < gl.gl_pathc; i++)
1533 rpc_uci_purge_dir(gl.gl_pathv[i]);
1539 int rpc_uci_api_init(struct ubus_context *ctx)
1541 static const struct ubus_method uci_methods[] = {
1542 { .name = "configs", .handler = rpc_uci_configs },
1543 UBUS_METHOD("get", rpc_uci_get, rpc_uci_get_policy),
1544 UBUS_METHOD("state", rpc_uci_state, rpc_uci_get_policy),
1545 UBUS_METHOD("add", rpc_uci_add, rpc_uci_add_policy),
1546 UBUS_METHOD("set", rpc_uci_set, rpc_uci_set_policy),
1547 UBUS_METHOD("delete", rpc_uci_delete, rpc_uci_delete_policy),
1548 UBUS_METHOD("rename", rpc_uci_rename, rpc_uci_rename_policy),
1549 UBUS_METHOD("order", rpc_uci_order, rpc_uci_order_policy),
1550 UBUS_METHOD("changes", rpc_uci_changes, rpc_uci_config_policy),
1551 UBUS_METHOD("revert", rpc_uci_revert, rpc_uci_config_policy),
1552 UBUS_METHOD("commit", rpc_uci_commit, rpc_uci_config_policy),
1553 UBUS_METHOD("apply", rpc_uci_apply, rpc_uci_apply_policy),
1554 UBUS_METHOD("confirm", rpc_uci_confirm, rpc_uci_rollback_policy),
1555 UBUS_METHOD("rollback", rpc_uci_rollback, rpc_uci_rollback_policy),
1556 UBUS_METHOD_NOARG("reload_config", rpc_uci_reload),
1559 static struct ubus_object_type uci_type =
1560 UBUS_OBJECT_TYPE("luci-rpc-uci", uci_methods);
1562 static struct ubus_object obj = {
1565 .methods = uci_methods,
1566 .n_methods = ARRAY_SIZE(uci_methods),
1569 static struct rpc_session_cb cb = {
1570 .cb = rpc_uci_purge_savedir_cb
1573 cursor = uci_alloc_context();
1576 return UBUS_STATUS_UNKNOWN_ERROR;
1578 rpc_session_destroy_cb(&cb);
1580 return ubus_add_object(ctx, &obj);