2 * rpcd - UBUS RPC server
4 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <libubox/blobmsg.h>
23 #include <libubox/blobmsg_json.h>
26 #include <rpcd/exec.h>
27 #include <rpcd/session.h>
29 static struct blob_buf buf;
30 static struct uci_context *cursor;
31 static struct uloop_timeout apply_timer;
32 static struct ubus_context *apply_ctx;
34 char apply_sid[RPC_SID_LEN + 1];
46 static const struct blobmsg_policy rpc_uci_get_policy[__RPC_G_MAX] = {
47 [RPC_G_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
48 [RPC_G_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
49 [RPC_G_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
50 [RPC_G_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
51 [RPC_G_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
52 [RPC_G_SESSION] = { .name = "ubus_rpc_session",
53 .type = BLOBMSG_TYPE_STRING },
65 static const struct blobmsg_policy rpc_uci_add_policy[__RPC_A_MAX] = {
66 [RPC_A_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
67 [RPC_A_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
68 [RPC_A_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
69 [RPC_A_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
70 [RPC_A_SESSION] = { .name = "ubus_rpc_session",
71 .type = BLOBMSG_TYPE_STRING },
84 static const struct blobmsg_policy rpc_uci_set_policy[__RPC_S_MAX] = {
85 [RPC_S_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
86 [RPC_S_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
87 [RPC_S_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
88 [RPC_S_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
89 [RPC_S_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
90 [RPC_S_SESSION] = { .name = "ubus_rpc_session",
91 .type = BLOBMSG_TYPE_STRING },
105 static const struct blobmsg_policy rpc_uci_delete_policy[__RPC_D_MAX] = {
106 [RPC_D_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
107 [RPC_D_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
108 [RPC_D_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
109 [RPC_D_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
110 [RPC_D_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
111 [RPC_D_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_ARRAY },
112 [RPC_D_SESSION] = { .name = "ubus_rpc_session",
113 .type = BLOBMSG_TYPE_STRING },
125 static const struct blobmsg_policy rpc_uci_rename_policy[__RPC_R_MAX] = {
126 [RPC_R_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
127 [RPC_R_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
128 [RPC_R_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
129 [RPC_R_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
130 [RPC_R_SESSION] = { .name = "ubus_rpc_session",
131 .type = BLOBMSG_TYPE_STRING },
141 static const struct blobmsg_policy rpc_uci_order_policy[__RPC_O_MAX] = {
142 [RPC_O_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
143 [RPC_O_SECTIONS] = { .name = "sections", .type = BLOBMSG_TYPE_ARRAY },
144 [RPC_O_SESSION] = { .name = "ubus_rpc_session",
145 .type = BLOBMSG_TYPE_STRING },
154 static const struct blobmsg_policy rpc_uci_config_policy[__RPC_C_MAX] = {
155 [RPC_C_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
156 [RPC_C_SESSION] = { .name = "ubus_rpc_session",
157 .type = BLOBMSG_TYPE_STRING },
167 static const struct blobmsg_policy rpc_uci_apply_policy[__RPC_T_MAX] = {
168 [RPC_T_ROLLBACK] = { .name = "rollback", .type = BLOBMSG_TYPE_BOOL },
169 [RPC_T_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
170 [RPC_T_SESSION] = { .name = "ubus_rpc_session",
171 .type = BLOBMSG_TYPE_STRING },
179 static const struct blobmsg_policy rpc_uci_rollback_policy[__RPC_B_MAX] = {
180 [RPC_B_SESSION] = { .name = "ubus_rpc_session",
181 .type = BLOBMSG_TYPE_STRING },
185 * Turn uci error state into ubus return code
193 return UBUS_STATUS_OK;
196 return UBUS_STATUS_INVALID_ARGUMENT;
198 case UCI_ERR_NOTFOUND:
199 return UBUS_STATUS_NOT_FOUND;
202 return UBUS_STATUS_UNKNOWN_ERROR;
207 * Clear all save directories from the uci cursor and append the given path
208 * as new save directory.
211 rpc_uci_replace_savedir(const char *path)
213 struct uci_element *e, *tmp;
215 uci_foreach_element_safe(&cursor->delta_path, tmp, e)
218 cursor->delta_path.prev = &cursor->delta_path;
219 cursor->delta_path.next = &cursor->delta_path;
222 uci_set_savedir(cursor, path);
226 * Setup per-session delta save directory. If the passed "sid" blob attribute
227 * pointer is NULL then the precedure was not invoked through the ubus-rpc so
228 * we do not perform session isolation and use the default save directory.
231 rpc_uci_set_savedir(struct blob_attr *sid)
237 rpc_uci_replace_savedir("/tmp/.uci");
241 snprintf(path, sizeof(path) - 1,
242 RPC_UCI_SAVEDIR_PREFIX "%s", blobmsg_get_string(sid));
244 rpc_uci_replace_savedir(path);
248 * Test read access to given config. If the passed "sid" blob attribute pointer
249 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
250 * perform access control and always assume true.
253 rpc_uci_read_access(struct blob_attr *sid, struct blob_attr *config)
255 rpc_uci_set_savedir(sid);
260 return rpc_session_access(blobmsg_data(sid), "uci",
261 blobmsg_data(config), "read");
265 * Test write access to given config. If the passed "sid" blob attribute pointer
266 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
267 * perform access control and always assume true.
270 rpc_uci_write_access(struct blob_attr *sid, struct blob_attr *config)
272 rpc_uci_set_savedir(sid);
277 return rpc_session_access(blobmsg_data(sid), "uci",
278 blobmsg_data(config), "write");
282 * Format applicable blob value as string and place a pointer to the string
283 * buffer in "p". Uses a static string buffer.
286 rpc_uci_format_blob(struct blob_attr *v, const char **p)
292 switch (blobmsg_type(v))
294 case BLOBMSG_TYPE_STRING:
295 *p = blobmsg_data(v);
298 case BLOBMSG_TYPE_INT64:
299 snprintf(buf, sizeof(buf), "%"PRIu64, blobmsg_get_u64(v));
303 case BLOBMSG_TYPE_INT32:
304 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u32(v));
308 case BLOBMSG_TYPE_INT16:
309 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u16(v));
313 case BLOBMSG_TYPE_INT8:
314 snprintf(buf, sizeof(buf), "%u", !!blobmsg_get_u8(v));
326 * Lookup the given uci_ptr and enable extended lookup format if the .section
327 * value of the uci_ptr looks like extended syntax. Uses an internal copy
328 * of the given uci_ptr to perform the lookup as failing extended section
329 * lookup operations in libuci will zero our the uci_ptr struct.
330 * Copies the internal uci_ptr back to given the uci_ptr on success.
333 rpc_uci_lookup(struct uci_ptr *ptr)
336 struct uci_ptr lookup = *ptr;
338 if (!lookup.s && lookup.section && *lookup.section == '@')
339 lookup.flags |= UCI_LOOKUP_EXTENDED;
341 rv = uci_lookup_ptr(cursor, &lookup, NULL, true);
350 * Checks whether the given uci_option object matches the given string value.
351 * 1) If the uci_option is of type list, check whether any of the list elements
352 * equals to the given string
353 * 2) If the uci_option is of type string, parse it into space separated tokens
354 * and check if any of the tokens equals to the given string.
355 * Returns true if a list element or token matched the given string.
358 rpc_uci_match_option(struct uci_option *o, const char *cmp)
360 struct uci_element *e;
363 if (o->type == UCI_TYPE_LIST)
365 uci_foreach_element(&o->v.list, e)
366 if (e->name && !strcmp(e->name, cmp))
375 s = strdup(o->v.string);
380 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
394 * Checks whether the given uci_section matches the type and value blob attrs.
395 * 1) Returns false if "type" is given and the section type does not match
396 * the value specified in the "type" string blob attribute, else continue.
397 * 2) Tests whether any key in the "matches" table blob attribute exists in
398 * the given uci_section and whether each value is contained in the
399 * corresponding uci option value (see rpc_uci_match_option()).
400 * 3) A missing or empty "matches" table blob attribute is always considered
402 * Returns true if "type" matches or is NULL and "matches" matches or is NULL.
405 rpc_uci_match_section(struct uci_section *s,
406 struct blob_attr *type, struct blob_attr *matches)
408 struct uci_element *e;
409 struct blob_attr *cur;
415 if (type && strcmp(s->type, blobmsg_data(type)))
421 blobmsg_for_each_attr(cur, matches, rem)
423 if (!rpc_uci_format_blob(cur, &cmp))
426 uci_foreach_element(&s->options, e)
428 if (strcmp(e->name, blobmsg_name(cur)))
431 if (!rpc_uci_match_option(uci_to_option(e), cmp))
440 return (empty || match);
444 * Dump the given uci_option value into the global blobmsg buffer and use
445 * given "name" as key.
446 * 1) If the uci_option is of type list, put a table into the blob buffer and
447 * add each list item as string to it.
448 * 2) If the uci_option is of type string, put its value directly into the blob
452 rpc_uci_dump_option(struct uci_option *o, const char *name)
455 struct uci_element *e;
459 case UCI_TYPE_STRING:
460 blobmsg_add_string(&buf, name, o->v.string);
464 c = blobmsg_open_array(&buf, name);
466 uci_foreach_element(&o->v.list, e)
467 blobmsg_add_string(&buf, NULL, e->name);
469 blobmsg_close_array(&buf, c);
478 * Dump the given uci_section object into the global blobmsg buffer and use
479 * given "name" as key.
480 * Puts a table into the blob buffer and puts each section option member value
481 * as value into the table using the option name as key.
482 * Adds three special keys ".anonymous", ".type" and ".name" which specify the
483 * corresponding section properties.
486 rpc_uci_dump_section(struct uci_section *s, const char *name, int index)
489 struct uci_option *o;
490 struct uci_element *e;
492 c = blobmsg_open_table(&buf, name);
494 blobmsg_add_u8(&buf, ".anonymous", s->anonymous);
495 blobmsg_add_string(&buf, ".type", s->type);
496 blobmsg_add_string(&buf, ".name", s->e.name);
499 blobmsg_add_u32(&buf, ".index", index);
501 uci_foreach_element(&s->options, e)
503 o = uci_to_option(e);
504 rpc_uci_dump_option(o, o->e.name);
507 blobmsg_close_table(&buf, c);
511 * Dump the given uci_package object into the global blobmsg buffer and use
512 * given "name" as key.
513 * Puts a table into the blob buffer and puts each package section member as
514 * value into the table using the section name as key.
515 * Only dumps sections matching the given "type" and "matches", see explaination
516 * of rpc_uci_match_section() for details.
519 rpc_uci_dump_package(struct uci_package *p, const char *name,
520 struct blob_attr *type, struct blob_attr *matches)
523 struct uci_element *e;
526 c = blobmsg_open_table(&buf, name);
528 uci_foreach_element(&p->sections, e)
532 if (!rpc_uci_match_section(uci_to_section(e), type, matches))
535 rpc_uci_dump_section(uci_to_section(e), e->name, i);
538 blobmsg_close_table(&buf, c);
543 rpc_uci_getcommon(struct ubus_context *ctx, struct ubus_request_data *req,
544 struct blob_attr *msg, bool use_state)
546 struct blob_attr *tb[__RPC_G_MAX];
547 struct uci_package *p = NULL;
548 struct uci_ptr ptr = { 0 };
550 blobmsg_parse(rpc_uci_get_policy, __RPC_G_MAX, tb,
551 blob_data(msg), blob_len(msg));
553 if (!tb[RPC_G_CONFIG])
554 return UBUS_STATUS_INVALID_ARGUMENT;
556 if (!rpc_uci_read_access(tb[RPC_G_SESSION], tb[RPC_G_CONFIG]))
557 return UBUS_STATUS_PERMISSION_DENIED;
559 ptr.package = blobmsg_data(tb[RPC_G_CONFIG]);
562 uci_set_savedir(cursor, "/var/state");
564 if (uci_load(cursor, ptr.package, &p))
565 return rpc_uci_status();
567 if (tb[RPC_G_SECTION])
569 ptr.section = blobmsg_data(tb[RPC_G_SECTION]);
571 if (tb[RPC_G_OPTION])
572 ptr.option = blobmsg_data(tb[RPC_G_OPTION]);
575 if (rpc_uci_lookup(&ptr) || !(ptr.flags & UCI_LOOKUP_COMPLETE))
578 blob_buf_init(&buf, 0);
580 switch (ptr.last->type)
582 case UCI_TYPE_PACKAGE:
583 rpc_uci_dump_package(ptr.p, "values", tb[RPC_G_TYPE], tb[RPC_G_MATCH]);
586 case UCI_TYPE_SECTION:
587 rpc_uci_dump_section(ptr.s, "values", -1);
590 case UCI_TYPE_OPTION:
591 rpc_uci_dump_option(ptr.o, "value");
598 ubus_send_reply(ctx, req, buf.head);
601 uci_unload(cursor, p);
603 return rpc_uci_status();
607 rpc_uci_get(struct ubus_context *ctx, struct ubus_object *obj,
608 struct ubus_request_data *req, const char *method,
609 struct blob_attr *msg)
611 return rpc_uci_getcommon(ctx, req, msg, false);
615 rpc_uci_state(struct ubus_context *ctx, struct ubus_object *obj,
616 struct ubus_request_data *req, const char *method,
617 struct blob_attr *msg)
619 return rpc_uci_getcommon(ctx, req, msg, true);
623 rpc_uci_add(struct ubus_context *ctx, struct ubus_object *obj,
624 struct ubus_request_data *req, const char *method,
625 struct blob_attr *msg)
627 struct blob_attr *tb[__RPC_A_MAX];
628 struct blob_attr *cur, *elem;
629 struct uci_package *p = NULL;
630 struct uci_section *s;
631 struct uci_ptr ptr = { 0 };
634 blobmsg_parse(rpc_uci_add_policy, __RPC_A_MAX, tb,
635 blob_data(msg), blob_len(msg));
637 if (!tb[RPC_A_CONFIG] || !tb[RPC_A_TYPE])
638 return UBUS_STATUS_INVALID_ARGUMENT;
640 if (!rpc_uci_write_access(tb[RPC_A_SESSION], tb[RPC_A_CONFIG]))
641 return UBUS_STATUS_PERMISSION_DENIED;
643 ptr.package = blobmsg_data(tb[RPC_A_CONFIG]);
645 if (uci_load(cursor, ptr.package, &p))
646 return rpc_uci_status();
648 /* add named section */
651 ptr.section = blobmsg_data(tb[RPC_A_NAME]);
652 ptr.value = blobmsg_data(tb[RPC_A_TYPE]);
655 if (rpc_uci_lookup(&ptr) || uci_set(cursor, &ptr))
659 /* add anon section */
662 if (uci_add_section(cursor, p, blobmsg_data(tb[RPC_A_TYPE]), &s) || !s)
665 ptr.section = s->e.name;
668 if (tb[RPC_A_VALUES])
670 blobmsg_for_each_attr(cur, tb[RPC_A_VALUES], rem)
673 ptr.option = blobmsg_name(cur);
675 if (rpc_uci_lookup(&ptr) || !ptr.s)
678 switch (blobmsg_type(cur))
680 case BLOBMSG_TYPE_ARRAY:
681 blobmsg_for_each_attr(elem, cur, rem2)
682 if (rpc_uci_format_blob(elem, &ptr.value))
683 uci_add_list(cursor, &ptr);
687 if (rpc_uci_format_blob(cur, &ptr.value))
688 uci_set(cursor, &ptr);
696 blob_buf_init(&buf, 0);
697 blobmsg_add_string(&buf, "section", ptr.section);
698 ubus_send_reply(ctx, req, buf.head);
701 uci_unload(cursor, p);
703 return rpc_uci_status();
707 * Turn value from a blob attribute into uci set operation
708 * 1) if the blob is of type array, delete existing option (if any) and
709 * emit uci add_list operations for each element
710 * 2) if the blob is not an array but an option of type list exists,
711 * delete existing list and emit uci set operation for the blob value
712 * 3) in all other cases only emit a set operation if there is no existing
713 * option of if the existing options value differs from the blob value
716 rpc_uci_merge_set(struct blob_attr *opt, struct uci_ptr *ptr)
718 struct blob_attr *cur;
722 ptr->option = blobmsg_name(opt);
725 if (rpc_uci_lookup(ptr) || !ptr->s)
728 if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
731 uci_delete(cursor, ptr);
733 blobmsg_for_each_attr(cur, opt, rem)
734 if (rpc_uci_format_blob(cur, &ptr->value))
735 uci_add_list(cursor, ptr);
737 else if (ptr->o && ptr->o->type == UCI_TYPE_LIST)
739 uci_delete(cursor, ptr);
741 if (rpc_uci_format_blob(opt, &ptr->value))
742 uci_set(cursor, ptr);
744 else if (rpc_uci_format_blob(opt, &ptr->value))
746 if (!ptr->o || !ptr->o->v.string || strcmp(ptr->o->v.string, ptr->value))
747 uci_set(cursor, ptr);
752 rpc_uci_set(struct ubus_context *ctx, struct ubus_object *obj,
753 struct ubus_request_data *req, const char *method,
754 struct blob_attr *msg)
756 struct blob_attr *tb[__RPC_S_MAX];
757 struct blob_attr *cur;
758 struct uci_package *p = NULL;
759 struct uci_element *e;
760 struct uci_ptr ptr = { 0 };
763 blobmsg_parse(rpc_uci_set_policy, __RPC_S_MAX, tb,
764 blob_data(msg), blob_len(msg));
766 if (!tb[RPC_S_CONFIG] || !tb[RPC_S_VALUES] ||
767 (!tb[RPC_S_SECTION] && !tb[RPC_S_TYPE] && !tb[RPC_S_MATCH]))
768 return UBUS_STATUS_INVALID_ARGUMENT;
770 if (!rpc_uci_write_access(tb[RPC_S_SESSION], tb[RPC_S_CONFIG]))
771 return UBUS_STATUS_PERMISSION_DENIED;
773 ptr.package = blobmsg_data(tb[RPC_S_CONFIG]);
775 if (uci_load(cursor, ptr.package, &p))
776 return rpc_uci_status();
778 if (tb[RPC_S_SECTION])
780 ptr.section = blobmsg_data(tb[RPC_S_SECTION]);
781 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
782 rpc_uci_merge_set(cur, &ptr);
786 uci_foreach_element(&p->sections, e)
788 if (!rpc_uci_match_section(uci_to_section(e),
789 tb[RPC_S_TYPE], tb[RPC_S_MATCH]))
793 ptr.section = e->name;
795 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
796 rpc_uci_merge_set(cur, &ptr);
801 uci_unload(cursor, p);
803 return rpc_uci_status();
807 * Delete option or section from uci specified by given blob attribute pointer
808 * 1) if the blob is of type array, delete any option named after each element
809 * 2) if the blob is of type string, delete the option named after its value
810 * 3) if the blob is NULL, delete entire section
813 rpc_uci_merge_delete(struct blob_attr *opt, struct uci_ptr *ptr)
815 struct blob_attr *cur;
818 if (rpc_uci_lookup(ptr) || !ptr->s)
826 uci_delete(cursor, ptr);
828 else if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
830 blobmsg_for_each_attr(cur, opt, rem)
832 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
836 ptr->option = blobmsg_data(cur);
838 if (rpc_uci_lookup(ptr) || !ptr->o)
841 uci_delete(cursor, ptr);
844 else if (blobmsg_type(opt) == BLOBMSG_TYPE_STRING)
847 ptr->option = blobmsg_data(opt);
849 if (rpc_uci_lookup(ptr) || !ptr->o)
852 uci_delete(cursor, ptr);
857 rpc_uci_delete(struct ubus_context *ctx, struct ubus_object *obj,
858 struct ubus_request_data *req, const char *method,
859 struct blob_attr *msg)
861 struct blob_attr *tb[__RPC_D_MAX];
862 struct uci_package *p = NULL;
863 struct uci_element *e, *tmp;
864 struct uci_ptr ptr = { 0 };
866 blobmsg_parse(rpc_uci_delete_policy, __RPC_D_MAX, tb,
867 blob_data(msg), blob_len(msg));
869 if (!tb[RPC_D_CONFIG] ||
870 (!tb[RPC_D_SECTION] && !tb[RPC_D_TYPE] && !tb[RPC_D_MATCH]))
871 return UBUS_STATUS_INVALID_ARGUMENT;
873 if (!rpc_uci_write_access(tb[RPC_D_SESSION], tb[RPC_D_CONFIG]))
874 return UBUS_STATUS_PERMISSION_DENIED;
876 ptr.package = blobmsg_data(tb[RPC_D_CONFIG]);
878 if (uci_load(cursor, ptr.package, &p))
879 return rpc_uci_status();
881 if (tb[RPC_D_SECTION])
883 ptr.section = blobmsg_data(tb[RPC_D_SECTION]);
885 if (tb[RPC_D_OPTIONS])
886 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
888 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
892 uci_foreach_element_safe(&p->sections, tmp, e)
894 if (!rpc_uci_match_section(uci_to_section(e),
895 tb[RPC_D_TYPE], tb[RPC_D_MATCH]))
899 ptr.section = e->name;
901 if (tb[RPC_D_OPTIONS])
902 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
904 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
909 uci_unload(cursor, p);
911 return rpc_uci_status();
915 rpc_uci_rename(struct ubus_context *ctx, struct ubus_object *obj,
916 struct ubus_request_data *req, const char *method,
917 struct blob_attr *msg)
919 struct blob_attr *tb[__RPC_R_MAX];
920 struct uci_package *p = NULL;
921 struct uci_ptr ptr = { 0 };
923 blobmsg_parse(rpc_uci_rename_policy, __RPC_R_MAX, tb,
924 blob_data(msg), blob_len(msg));
926 if (!tb[RPC_R_CONFIG] || !tb[RPC_R_SECTION] || !tb[RPC_R_NAME])
927 return UBUS_STATUS_INVALID_ARGUMENT;
929 if (!rpc_uci_write_access(tb[RPC_R_SESSION], tb[RPC_R_CONFIG]))
930 return UBUS_STATUS_PERMISSION_DENIED;
932 ptr.package = blobmsg_data(tb[RPC_R_CONFIG]);
933 ptr.section = blobmsg_data(tb[RPC_R_SECTION]);
934 ptr.value = blobmsg_data(tb[RPC_R_NAME]);
936 if (tb[RPC_R_OPTION])
937 ptr.option = blobmsg_data(tb[RPC_R_OPTION]);
939 if (uci_load(cursor, ptr.package, &p))
940 return rpc_uci_status();
942 if (uci_lookup_ptr(cursor, &ptr, NULL, true))
945 if ((ptr.option && !ptr.o) || !ptr.s)
947 cursor->err = UCI_ERR_NOTFOUND;
951 if (uci_rename(cursor, &ptr))
957 uci_unload(cursor, p);
959 return rpc_uci_status();
963 rpc_uci_order(struct ubus_context *ctx, struct ubus_object *obj,
964 struct ubus_request_data *req, const char *method,
965 struct blob_attr *msg)
967 struct blob_attr *tb[__RPC_O_MAX];
968 struct blob_attr *cur;
969 struct uci_package *p = NULL;
970 struct uci_ptr ptr = { 0 };
973 blobmsg_parse(rpc_uci_order_policy, __RPC_O_MAX, tb,
974 blob_data(msg), blob_len(msg));
976 if (!tb[RPC_O_CONFIG] || !tb[RPC_O_SECTIONS])
977 return UBUS_STATUS_INVALID_ARGUMENT;
979 if (!rpc_uci_write_access(tb[RPC_O_SESSION], tb[RPC_O_CONFIG]))
980 return UBUS_STATUS_PERMISSION_DENIED;
982 ptr.package = blobmsg_data(tb[RPC_O_CONFIG]);
984 if (uci_load(cursor, ptr.package, &p))
985 return rpc_uci_status();
987 blobmsg_for_each_attr(cur, tb[RPC_O_SECTIONS], rem)
989 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
993 ptr.section = blobmsg_data(cur);
995 if (uci_lookup_ptr(cursor, &ptr, NULL, true) || !ptr.s)
998 uci_reorder_section(cursor, ptr.s, i++);
1001 uci_save(cursor, p);
1002 uci_unload(cursor, p);
1004 return rpc_uci_status();
1008 rpc_uci_dump_change(struct uci_delta *d)
1011 const char *types[] = {
1012 [UCI_CMD_REORDER] = "order",
1013 [UCI_CMD_REMOVE] = "remove",
1014 [UCI_CMD_RENAME] = "rename",
1015 [UCI_CMD_ADD] = "add",
1016 [UCI_CMD_LIST_ADD] = "list-add",
1017 [UCI_CMD_LIST_DEL] = "list-del",
1018 [UCI_CMD_CHANGE] = "set",
1024 c = blobmsg_open_array(&buf, NULL);
1026 blobmsg_add_string(&buf, NULL, types[d->cmd]);
1027 blobmsg_add_string(&buf, NULL, d->section);
1030 blobmsg_add_string(&buf, NULL, d->e.name);
1034 if (d->cmd == UCI_CMD_REORDER)
1035 blobmsg_add_u32(&buf, NULL, strtoul(d->value, NULL, 10));
1037 blobmsg_add_string(&buf, NULL, d->value);
1040 blobmsg_close_array(&buf, c);
1044 rpc_uci_changes(struct ubus_context *ctx, struct ubus_object *obj,
1045 struct ubus_request_data *req, const char *method,
1046 struct blob_attr *msg)
1048 struct blob_attr *tb[__RPC_C_MAX];
1049 struct uci_package *p = NULL;
1050 struct uci_element *e;
1055 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
1056 blob_data(msg), blob_len(msg));
1058 if (tb[RPC_C_CONFIG])
1060 if (!rpc_uci_read_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
1061 return UBUS_STATUS_PERMISSION_DENIED;
1063 if (uci_load(cursor, blobmsg_data(tb[RPC_C_CONFIG]), &p))
1064 return rpc_uci_status();
1066 blob_buf_init(&buf, 0);
1067 c = blobmsg_open_array(&buf, "changes");
1069 uci_foreach_element(&p->saved_delta, e)
1070 rpc_uci_dump_change(uci_to_delta(e));
1072 blobmsg_close_array(&buf, c);
1074 uci_unload(cursor, p);
1076 ubus_send_reply(ctx, req, buf.head);
1078 return rpc_uci_status();
1081 rpc_uci_set_savedir(tb[RPC_C_SESSION]);
1083 if (uci_list_configs(cursor, &configs))
1084 return rpc_uci_status();
1086 blob_buf_init(&buf, 0);
1088 c = blobmsg_open_table(&buf, "changes");
1090 for (i = 0; configs[i]; i++)
1092 if (tb[RPC_C_SESSION] &&
1093 !rpc_session_access(blobmsg_data(tb[RPC_C_SESSION]), "uci",
1094 configs[i], "read"))
1097 if (uci_load(cursor, configs[i], &p))
1100 if (!uci_list_empty(&p->saved_delta))
1102 d = blobmsg_open_array(&buf, configs[i]);
1104 uci_foreach_element(&p->saved_delta, e)
1105 rpc_uci_dump_change(uci_to_delta(e));
1107 blobmsg_close_array(&buf, d);
1110 uci_unload(cursor, p);
1113 blobmsg_close_table(&buf, c);
1115 ubus_send_reply(ctx, req, buf.head);
1121 rpc_uci_trigger_event(struct ubus_context *ctx, const char *config)
1123 char *pkg = strdup(config);
1124 static struct blob_buf b;
1127 if (!ubus_lookup_id(ctx, "service", &id)) {
1130 blob_buf_init(&b, 0);
1131 blobmsg_add_string(&b, "type", "config.change");
1132 c = blobmsg_open_table(&b, "data");
1133 blobmsg_add_string(&b, "package", pkg);
1134 blobmsg_close_table(&b, c);
1135 ubus_invoke(ctx, id, "event", b.head, NULL, 0, 1000);
1141 rpc_uci_revert_commit(struct ubus_context *ctx, struct blob_attr *msg, bool commit)
1143 struct blob_attr *tb[__RPC_C_MAX];
1144 struct uci_package *p = NULL;
1145 struct uci_ptr ptr = { 0 };
1148 return UBUS_STATUS_PERMISSION_DENIED;
1150 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
1151 blob_data(msg), blob_len(msg));
1153 if (!tb[RPC_C_CONFIG])
1154 return UBUS_STATUS_INVALID_ARGUMENT;
1156 if (!rpc_uci_write_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
1157 return UBUS_STATUS_PERMISSION_DENIED;
1159 ptr.package = blobmsg_data(tb[RPC_C_CONFIG]);
1163 if (!uci_load(cursor, ptr.package, &p))
1165 uci_commit(cursor, &p, false);
1166 uci_unload(cursor, p);
1167 rpc_uci_trigger_event(ctx, blobmsg_get_string(tb[RPC_C_CONFIG]));
1172 if (!uci_lookup_ptr(cursor, &ptr, NULL, true) && ptr.p)
1174 uci_revert(cursor, &ptr);
1175 uci_unload(cursor, ptr.p);
1179 return rpc_uci_status();
1183 rpc_uci_revert(struct ubus_context *ctx, struct ubus_object *obj,
1184 struct ubus_request_data *req, const char *method,
1185 struct blob_attr *msg)
1187 return rpc_uci_revert_commit(ctx, msg, false);
1191 rpc_uci_commit(struct ubus_context *ctx, struct ubus_object *obj,
1192 struct ubus_request_data *req, const char *method,
1193 struct blob_attr *msg)
1195 return rpc_uci_revert_commit(ctx, msg, true);
1199 rpc_uci_configs(struct ubus_context *ctx, struct ubus_object *obj,
1200 struct ubus_request_data *req, const char *method,
1201 struct blob_attr *msg)
1207 if (uci_list_configs(cursor, &configs))
1210 blob_buf_init(&buf, 0);
1212 c = blobmsg_open_array(&buf, "configs");
1214 for (i = 0; configs[i]; i++)
1215 blobmsg_add_string(&buf, NULL, configs[i]);
1217 blobmsg_close_array(&buf, c);
1219 ubus_send_reply(ctx, req, buf.head);
1222 return rpc_uci_status();
1227 * Remove given delta save directory (if any).
1230 rpc_uci_purge_dir(const char *path)
1235 char file[PATH_MAX];
1237 if (stat(path, &s) || !S_ISDIR(s.st_mode))
1240 if ((d = opendir(path)) != NULL)
1242 while ((e = readdir(d)) != NULL)
1244 snprintf(file, sizeof(file) - 1, "%s/%s", path, e->d_name);
1246 if (stat(file, &s) || !S_ISREG(s.st_mode))
1259 rpc_uci_apply_config(struct ubus_context *ctx, char *config)
1261 struct uci_package *p = NULL;
1263 if (!uci_load(cursor, config, &p)) {
1264 uci_commit(cursor, &p, false);
1265 uci_unload(cursor, p);
1267 rpc_uci_trigger_event(ctx, config);
1273 rpc_uci_copy_file(const char *src, const char *target, const char *file)
1278 snprintf(tmp, sizeof(tmp), "%s%s", src, file);
1279 in = fopen(tmp, "rb");
1280 snprintf(tmp, sizeof(tmp), "%s%s", target, file);
1281 out = fopen(tmp, "wb+");
1284 int len = fread(tmp, 1, sizeof(tmp), in);
1287 fwrite(tmp, 1, len, out);
1296 rpc_uci_apply_access(const char *sid, glob_t *gl)
1301 if (gl->gl_pathc < 3)
1302 return UBUS_STATUS_NO_DATA;
1304 for (i = 0; i < gl->gl_pathc; i++) {
1305 char *config = basename(gl->gl_pathv[i]);
1309 if (stat(gl->gl_pathv[i], &s) || !s.st_size)
1311 if (!rpc_session_access(sid, "uci", config, "write"))
1312 return UBUS_STATUS_PERMISSION_DENIED;
1317 return UBUS_STATUS_NO_DATA;
1323 rpc_uci_do_rollback(struct ubus_context *ctx, glob_t *gl)
1328 /* Test apply permission to see if the initiator session still exists.
1329 * If it does, restore the delta files as well, else just restore the
1330 * main configuration files. */
1332 ? rpc_uci_apply_access(apply_sid, gl) : UBUS_STATUS_NOT_FOUND;
1335 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", apply_sid);
1339 /* avoid merging unrelated uci changes when restoring old configs */
1340 rpc_uci_replace_savedir("/dev/null");
1342 for (i = 0; i < gl->gl_pathc; i++) {
1343 char *config = basename(gl->gl_pathv[i]);
1348 rpc_uci_copy_file(RPC_SNAPSHOT_FILES, RPC_UCI_DIR, config);
1349 rpc_uci_apply_config(ctx, config);
1354 rpc_uci_copy_file(RPC_SNAPSHOT_DELTA, tmp, config);
1357 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1358 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1360 uloop_timeout_cancel(&apply_timer);
1361 memset(apply_sid, 0, sizeof(apply_sid));
1366 rpc_uci_apply_timeout(struct uloop_timeout *t)
1371 snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
1372 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1375 rpc_uci_do_rollback(apply_ctx, &gl);
1381 rpc_uci_apply(struct ubus_context *ctx, struct ubus_object *obj,
1382 struct ubus_request_data *req, const char *method,
1383 struct blob_attr *msg)
1385 struct blob_attr *tb[__RPC_T_MAX];
1386 int timeout = RPC_APPLY_TIMEOUT;
1388 bool rollback = false;
1393 blobmsg_parse(rpc_uci_apply_policy, __RPC_T_MAX, tb,
1394 blob_data(msg), blob_len(msg));
1396 if (tb[RPC_T_ROLLBACK])
1397 rollback = blobmsg_get_bool(tb[RPC_T_ROLLBACK]);
1399 if (apply_sid[0] && rollback)
1400 return UBUS_STATUS_PERMISSION_DENIED;
1402 if (!tb[RPC_T_SESSION])
1403 return UBUS_STATUS_INVALID_ARGUMENT;
1405 sid = blobmsg_data(tb[RPC_T_SESSION]);
1407 if (tb[RPC_T_TIMEOUT])
1408 timeout = blobmsg_get_u32(tb[RPC_T_TIMEOUT]);
1410 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1411 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1413 if (!apply_sid[0]) {
1414 rpc_uci_set_savedir(tb[RPC_T_SESSION]);
1416 mkdir(RPC_SNAPSHOT_FILES, 0700);
1417 mkdir(RPC_SNAPSHOT_DELTA, 0700);
1419 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/*", sid);
1420 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1421 return UBUS_STATUS_NOT_FOUND;
1423 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
1425 ret = rpc_uci_apply_access(sid, &gl);
1431 /* copy SID early because rpc_uci_apply_config() will clobber buf */
1433 strncpy(apply_sid, sid, RPC_SID_LEN);
1435 for (i = 0; i < gl.gl_pathc; i++) {
1436 char *config = basename(gl.gl_pathv[i]);
1442 if (stat(gl.gl_pathv[i], &s) || !s.st_size)
1445 rpc_uci_copy_file(RPC_UCI_DIR, RPC_SNAPSHOT_FILES, config);
1446 rpc_uci_copy_file(tmp, RPC_SNAPSHOT_DELTA, config);
1447 rpc_uci_apply_config(ctx, config);
1453 apply_timer.cb = rpc_uci_apply_timeout;
1454 uloop_timeout_set(&apply_timer, timeout * 1000);
1463 rpc_uci_confirm(struct ubus_context *ctx, struct ubus_object *obj,
1464 struct ubus_request_data *req, const char *method,
1465 struct blob_attr *msg)
1467 struct blob_attr *tb[__RPC_B_MAX];
1470 blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
1471 blob_data(msg), blob_len(msg));
1473 if (!tb[RPC_B_SESSION])
1474 return UBUS_STATUS_INVALID_ARGUMENT;
1476 sid = blobmsg_data(tb[RPC_B_SESSION]);
1479 return UBUS_STATUS_NO_DATA;
1481 if (strcmp(apply_sid, sid))
1482 return UBUS_STATUS_PERMISSION_DENIED;
1484 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1485 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1487 uloop_timeout_cancel(&apply_timer);
1488 memset(apply_sid, 0, sizeof(apply_sid));
1495 rpc_uci_rollback(struct ubus_context *ctx, struct ubus_object *obj,
1496 struct ubus_request_data *req, const char *method,
1497 struct blob_attr *msg)
1499 struct blob_attr *tb[__RPC_B_MAX];
1504 blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
1505 blob_data(msg), blob_len(msg));
1508 return UBUS_STATUS_NO_DATA;
1510 if (!tb[RPC_B_SESSION])
1511 return UBUS_STATUS_INVALID_ARGUMENT;
1513 sid = blobmsg_data(tb[RPC_B_SESSION]);
1515 if (strcmp(apply_sid, sid))
1516 return UBUS_STATUS_PERMISSION_DENIED;
1518 snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
1519 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1520 return UBUS_STATUS_NOT_FOUND;
1522 rpc_uci_do_rollback(ctx, &gl);
1530 rpc_uci_reload(struct ubus_context *ctx, struct ubus_object *obj,
1531 struct ubus_request_data *req, const char *method,
1532 struct blob_attr *msg)
1534 char * const cmd[2] = { "/sbin/reload_config", NULL };
1537 /* wait for the RPC call to complete */
1539 return execv(cmd[0], cmd);
1546 * Session destroy callback to purge associated delta directory.
1549 rpc_uci_purge_savedir_cb(struct rpc_session *ses, void *priv)
1551 char path[PATH_MAX];
1553 snprintf(path, sizeof(path) - 1, RPC_UCI_SAVEDIR_PREFIX "%s", ses->id);
1554 rpc_uci_purge_dir(path);
1558 * Removes all delta directories which match the RPC_UCI_SAVEDIR_PREFIX.
1559 * This is used to clean up garbage when starting rpcd.
1561 void rpc_uci_purge_savedirs(void)
1566 if (!glob(RPC_UCI_SAVEDIR_PREFIX "*", 0, NULL, &gl))
1568 for (i = 0; i < gl.gl_pathc; i++)
1569 rpc_uci_purge_dir(gl.gl_pathv[i]);
1575 int rpc_uci_api_init(struct ubus_context *ctx)
1577 static const struct ubus_method uci_methods[] = {
1578 { .name = "configs", .handler = rpc_uci_configs },
1579 UBUS_METHOD("get", rpc_uci_get, rpc_uci_get_policy),
1580 UBUS_METHOD("state", rpc_uci_state, rpc_uci_get_policy),
1581 UBUS_METHOD("add", rpc_uci_add, rpc_uci_add_policy),
1582 UBUS_METHOD("set", rpc_uci_set, rpc_uci_set_policy),
1583 UBUS_METHOD("delete", rpc_uci_delete, rpc_uci_delete_policy),
1584 UBUS_METHOD("rename", rpc_uci_rename, rpc_uci_rename_policy),
1585 UBUS_METHOD("order", rpc_uci_order, rpc_uci_order_policy),
1586 UBUS_METHOD("changes", rpc_uci_changes, rpc_uci_config_policy),
1587 UBUS_METHOD("revert", rpc_uci_revert, rpc_uci_config_policy),
1588 UBUS_METHOD("commit", rpc_uci_commit, rpc_uci_config_policy),
1589 UBUS_METHOD("apply", rpc_uci_apply, rpc_uci_apply_policy),
1590 UBUS_METHOD("confirm", rpc_uci_confirm, rpc_uci_rollback_policy),
1591 UBUS_METHOD("rollback", rpc_uci_rollback, rpc_uci_rollback_policy),
1592 UBUS_METHOD_NOARG("reload_config", rpc_uci_reload),
1595 static struct ubus_object_type uci_type =
1596 UBUS_OBJECT_TYPE("luci-rpc-uci", uci_methods);
1598 static struct ubus_object obj = {
1601 .methods = uci_methods,
1602 .n_methods = ARRAY_SIZE(uci_methods),
1605 static struct rpc_session_cb cb = {
1606 .cb = rpc_uci_purge_savedir_cb
1609 cursor = uci_alloc_context();
1612 return UBUS_STATUS_UNKNOWN_ERROR;
1614 rpc_session_destroy_cb(&cb);
1616 return ubus_add_object(ctx, &obj);