2 * rpcd - UBUS RPC server
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <libubox/blobmsg.h>
23 #include <libubox/blobmsg_json.h>
26 #include <rpcd/session.h>
28 static struct blob_buf buf;
29 static struct uci_context *cursor;
30 static struct uloop_timeout apply_timer;
31 static struct ubus_context *apply_ctx;
32 static bool apply_running;
44 static const struct blobmsg_policy rpc_uci_get_policy[__RPC_G_MAX] = {
45 [RPC_G_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
46 [RPC_G_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
47 [RPC_G_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
48 [RPC_G_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
49 [RPC_G_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
50 [RPC_G_SESSION] = { .name = "ubus_rpc_session",
51 .type = BLOBMSG_TYPE_STRING },
63 static const struct blobmsg_policy rpc_uci_add_policy[__RPC_A_MAX] = {
64 [RPC_A_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
65 [RPC_A_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
66 [RPC_A_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
67 [RPC_A_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
68 [RPC_A_SESSION] = { .name = "ubus_rpc_session",
69 .type = BLOBMSG_TYPE_STRING },
82 static const struct blobmsg_policy rpc_uci_set_policy[__RPC_S_MAX] = {
83 [RPC_S_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
84 [RPC_S_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
85 [RPC_S_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
86 [RPC_S_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
87 [RPC_S_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
88 [RPC_S_SESSION] = { .name = "ubus_rpc_session",
89 .type = BLOBMSG_TYPE_STRING },
103 static const struct blobmsg_policy rpc_uci_delete_policy[__RPC_D_MAX] = {
104 [RPC_D_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
105 [RPC_D_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
106 [RPC_D_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
107 [RPC_D_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
108 [RPC_D_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
109 [RPC_D_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_ARRAY },
110 [RPC_D_SESSION] = { .name = "ubus_rpc_session",
111 .type = BLOBMSG_TYPE_STRING },
123 static const struct blobmsg_policy rpc_uci_rename_policy[__RPC_R_MAX] = {
124 [RPC_R_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
125 [RPC_R_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
126 [RPC_R_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
127 [RPC_R_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
128 [RPC_R_SESSION] = { .name = "ubus_rpc_session",
129 .type = BLOBMSG_TYPE_STRING },
139 static const struct blobmsg_policy rpc_uci_order_policy[__RPC_O_MAX] = {
140 [RPC_O_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
141 [RPC_O_SECTIONS] = { .name = "sections", .type = BLOBMSG_TYPE_ARRAY },
142 [RPC_O_SESSION] = { .name = "ubus_rpc_session",
143 .type = BLOBMSG_TYPE_STRING },
152 static const struct blobmsg_policy rpc_uci_config_policy[__RPC_C_MAX] = {
153 [RPC_C_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
154 [RPC_C_SESSION] = { .name = "ubus_rpc_session",
155 .type = BLOBMSG_TYPE_STRING },
165 static const struct blobmsg_policy rpc_uci_apply_policy[__RPC_T_MAX] = {
166 [RPC_T_COMMIT] = { .name = "commit", .type = BLOBMSG_TYPE_BOOL },
167 [RPC_T_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
168 [RPC_T_SESSION] = { .name = "ubus_rpc_session",
169 .type = BLOBMSG_TYPE_STRING },
177 static const struct blobmsg_policy rpc_uci_rollback_policy[__RPC_B_MAX] = {
178 [RPC_B_SESSION] = { .name = "ubus_rpc_session",
179 .type = BLOBMSG_TYPE_STRING },
183 * Turn uci error state into ubus return code
191 return UBUS_STATUS_OK;
194 return UBUS_STATUS_INVALID_ARGUMENT;
196 case UCI_ERR_NOTFOUND:
197 return UBUS_STATUS_NOT_FOUND;
200 return UBUS_STATUS_UNKNOWN_ERROR;
205 * Setup per-session delta save directory. If the passed "sid" blob attribute
206 * pointer is NULL then the precedure was not invoked through the ubus-rpc so
207 * we do not perform session isolation and use the default save directory.
210 rpc_uci_set_savedir(struct blob_attr *sid)
216 uci_set_savedir(cursor, "/tmp/.uci");
220 snprintf(path, sizeof(path) - 1,
221 RPC_UCI_SAVEDIR_PREFIX "%s", blobmsg_get_string(sid));
223 uci_set_savedir(cursor, path);
227 * Test read access to given config. If the passed "sid" blob attribute pointer
228 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
229 * perform access control and always assume true.
232 rpc_uci_read_access(struct blob_attr *sid, struct blob_attr *config)
234 rpc_uci_set_savedir(sid);
239 return rpc_session_access(blobmsg_data(sid), "uci",
240 blobmsg_data(config), "read");
244 * Test write access to given config. If the passed "sid" blob attribute pointer
245 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
246 * perform access control and always assume true.
249 rpc_uci_write_access(struct blob_attr *sid, struct blob_attr *config)
251 rpc_uci_set_savedir(sid);
256 return rpc_session_access(blobmsg_data(sid), "uci",
257 blobmsg_data(config), "write");
261 * Format applicable blob value as string and place a pointer to the string
262 * buffer in "p". Uses a static string buffer.
265 rpc_uci_format_blob(struct blob_attr *v, const char **p)
271 switch (blobmsg_type(v))
273 case BLOBMSG_TYPE_STRING:
274 if (blobmsg_data_len(v) > 1)
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_get(struct ubus_context *ctx, struct ubus_object *obj,
524 struct ubus_request_data *req, const char *method,
525 struct blob_attr *msg)
527 struct blob_attr *tb[__RPC_G_MAX];
528 struct uci_package *p = NULL;
529 struct uci_ptr ptr = { 0 };
531 blobmsg_parse(rpc_uci_get_policy, __RPC_G_MAX, tb,
532 blob_data(msg), blob_len(msg));
534 if (!tb[RPC_G_CONFIG])
535 return UBUS_STATUS_INVALID_ARGUMENT;
537 if (!rpc_uci_read_access(tb[RPC_G_SESSION], tb[RPC_G_CONFIG]))
538 return UBUS_STATUS_PERMISSION_DENIED;
540 ptr.package = blobmsg_data(tb[RPC_G_CONFIG]);
542 if (uci_load(cursor, ptr.package, &p))
543 return rpc_uci_status();
545 if (tb[RPC_G_SECTION])
547 ptr.section = blobmsg_data(tb[RPC_G_SECTION]);
549 if (tb[RPC_G_OPTION])
550 ptr.option = blobmsg_data(tb[RPC_G_OPTION]);
553 if (rpc_uci_lookup(&ptr) || !(ptr.flags & UCI_LOOKUP_COMPLETE))
556 blob_buf_init(&buf, 0);
558 switch (ptr.last->type)
560 case UCI_TYPE_PACKAGE:
561 rpc_uci_dump_package(ptr.p, "values", tb[RPC_G_TYPE], tb[RPC_G_MATCH]);
564 case UCI_TYPE_SECTION:
565 rpc_uci_dump_section(ptr.s, "values", -1);
568 case UCI_TYPE_OPTION:
569 rpc_uci_dump_option(ptr.o, "value");
576 ubus_send_reply(ctx, req, buf.head);
579 uci_unload(cursor, p);
581 return rpc_uci_status();
585 rpc_uci_add(struct ubus_context *ctx, struct ubus_object *obj,
586 struct ubus_request_data *req, const char *method,
587 struct blob_attr *msg)
589 struct blob_attr *tb[__RPC_A_MAX];
590 struct blob_attr *cur, *elem;
591 struct uci_package *p = NULL;
592 struct uci_section *s;
593 struct uci_ptr ptr = { 0 };
596 blobmsg_parse(rpc_uci_add_policy, __RPC_A_MAX, tb,
597 blob_data(msg), blob_len(msg));
599 if (!tb[RPC_A_CONFIG] || !tb[RPC_A_TYPE])
600 return UBUS_STATUS_INVALID_ARGUMENT;
602 if (!rpc_uci_write_access(tb[RPC_A_SESSION], tb[RPC_A_CONFIG]))
603 return UBUS_STATUS_PERMISSION_DENIED;
605 ptr.package = blobmsg_data(tb[RPC_A_CONFIG]);
607 if (uci_load(cursor, ptr.package, &p))
608 return rpc_uci_status();
610 /* add named section */
613 ptr.section = blobmsg_data(tb[RPC_A_NAME]);
614 ptr.value = blobmsg_data(tb[RPC_A_TYPE]);
617 if (rpc_uci_lookup(&ptr) || uci_set(cursor, &ptr))
621 /* add anon section */
624 if (uci_add_section(cursor, p, blobmsg_data(tb[RPC_A_TYPE]), &s) || !s)
627 ptr.section = s->e.name;
630 if (tb[RPC_A_VALUES])
632 blobmsg_for_each_attr(cur, tb[RPC_A_VALUES], rem)
635 ptr.option = blobmsg_name(cur);
637 if (rpc_uci_lookup(&ptr) || !ptr.s)
640 switch (blobmsg_type(cur))
642 case BLOBMSG_TYPE_ARRAY:
643 blobmsg_for_each_attr(elem, cur, rem2)
644 if (rpc_uci_format_blob(elem, &ptr.value))
645 uci_add_list(cursor, &ptr);
649 if (rpc_uci_format_blob(cur, &ptr.value))
650 uci_set(cursor, &ptr);
658 blob_buf_init(&buf, 0);
659 blobmsg_add_string(&buf, "section", ptr.section);
660 ubus_send_reply(ctx, req, buf.head);
663 uci_unload(cursor, p);
665 return rpc_uci_status();
669 * Turn value from a blob attribute into uci set operation
670 * 1) if the blob is of type array, delete existing option (if any) and
671 * emit uci add_list operations for each element
672 * 2) if the blob is not an array but an option of type list exists,
673 * delete existing list and emit uci set operation for the blob value
674 * 3) in all other cases only emit a set operation if there is no existing
675 * option of if the existing options value differs from the blob value
678 rpc_uci_merge_set(struct blob_attr *opt, struct uci_ptr *ptr)
680 struct blob_attr *cur;
684 ptr->option = blobmsg_name(opt);
687 if (rpc_uci_lookup(ptr) || !ptr->s)
690 if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
693 uci_delete(cursor, ptr);
695 blobmsg_for_each_attr(cur, opt, rem)
696 if (rpc_uci_format_blob(cur, &ptr->value))
697 uci_add_list(cursor, ptr);
699 else if (ptr->o && ptr->o->type == UCI_TYPE_LIST)
701 uci_delete(cursor, ptr);
703 if (rpc_uci_format_blob(opt, &ptr->value))
704 uci_set(cursor, ptr);
706 else if (rpc_uci_format_blob(opt, &ptr->value))
708 if (!ptr->o || !ptr->o->v.string || strcmp(ptr->o->v.string, ptr->value))
709 uci_set(cursor, ptr);
714 rpc_uci_set(struct ubus_context *ctx, struct ubus_object *obj,
715 struct ubus_request_data *req, const char *method,
716 struct blob_attr *msg)
718 struct blob_attr *tb[__RPC_S_MAX];
719 struct blob_attr *cur;
720 struct uci_package *p = NULL;
721 struct uci_element *e;
722 struct uci_ptr ptr = { 0 };
725 blobmsg_parse(rpc_uci_set_policy, __RPC_S_MAX, tb,
726 blob_data(msg), blob_len(msg));
728 if (!tb[RPC_S_CONFIG] || !tb[RPC_S_VALUES] ||
729 (!tb[RPC_S_SECTION] && !tb[RPC_S_TYPE] && !tb[RPC_S_MATCH]))
730 return UBUS_STATUS_INVALID_ARGUMENT;
732 if (!rpc_uci_write_access(tb[RPC_S_SESSION], tb[RPC_S_CONFIG]))
733 return UBUS_STATUS_PERMISSION_DENIED;
735 ptr.package = blobmsg_data(tb[RPC_S_CONFIG]);
737 if (uci_load(cursor, ptr.package, &p))
738 return rpc_uci_status();
740 if (tb[RPC_S_SECTION])
742 ptr.section = blobmsg_data(tb[RPC_S_SECTION]);
743 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
744 rpc_uci_merge_set(cur, &ptr);
748 uci_foreach_element(&p->sections, e)
750 if (!rpc_uci_match_section(uci_to_section(e),
751 tb[RPC_S_TYPE], tb[RPC_S_MATCH]))
755 ptr.section = e->name;
757 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
758 rpc_uci_merge_set(cur, &ptr);
763 uci_unload(cursor, p);
765 return rpc_uci_status();
769 * Delete option or section from uci specified by given blob attribute pointer
770 * 1) if the blob is of type array, delete any option named after each element
771 * 2) if the blob is of type string, delete the option named after its value
772 * 3) if the blob is NULL, delete entire section
775 rpc_uci_merge_delete(struct blob_attr *opt, struct uci_ptr *ptr)
777 struct blob_attr *cur;
780 if (rpc_uci_lookup(ptr) || !ptr->s)
788 uci_delete(cursor, ptr);
790 else if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
792 blobmsg_for_each_attr(cur, opt, rem)
794 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
798 ptr->option = blobmsg_data(cur);
800 if (rpc_uci_lookup(ptr) || !ptr->o)
803 uci_delete(cursor, ptr);
806 else if (blobmsg_type(opt) == BLOBMSG_TYPE_STRING)
809 ptr->option = blobmsg_data(opt);
811 if (rpc_uci_lookup(ptr) || !ptr->o)
814 uci_delete(cursor, ptr);
819 rpc_uci_delete(struct ubus_context *ctx, struct ubus_object *obj,
820 struct ubus_request_data *req, const char *method,
821 struct blob_attr *msg)
823 struct blob_attr *tb[__RPC_D_MAX];
824 struct uci_package *p = NULL;
825 struct uci_element *e, *tmp;
826 struct uci_ptr ptr = { 0 };
828 blobmsg_parse(rpc_uci_delete_policy, __RPC_D_MAX, tb,
829 blob_data(msg), blob_len(msg));
831 if (!tb[RPC_D_CONFIG] ||
832 (!tb[RPC_D_SECTION] && !tb[RPC_D_TYPE] && !tb[RPC_D_MATCH]))
833 return UBUS_STATUS_INVALID_ARGUMENT;
835 if (!rpc_uci_write_access(tb[RPC_D_SESSION], tb[RPC_D_CONFIG]))
836 return UBUS_STATUS_PERMISSION_DENIED;
838 ptr.package = blobmsg_data(tb[RPC_D_CONFIG]);
840 if (uci_load(cursor, ptr.package, &p))
841 return rpc_uci_status();
843 if (tb[RPC_D_SECTION])
845 ptr.section = blobmsg_data(tb[RPC_D_SECTION]);
847 if (tb[RPC_D_OPTIONS])
848 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
850 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
854 uci_foreach_element_safe(&p->sections, tmp, e)
856 if (!rpc_uci_match_section(uci_to_section(e),
857 tb[RPC_D_TYPE], tb[RPC_D_MATCH]))
861 ptr.section = e->name;
863 if (tb[RPC_D_OPTIONS])
864 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
866 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
871 uci_unload(cursor, p);
873 return rpc_uci_status();
877 rpc_uci_rename(struct ubus_context *ctx, struct ubus_object *obj,
878 struct ubus_request_data *req, const char *method,
879 struct blob_attr *msg)
881 struct blob_attr *tb[__RPC_R_MAX];
882 struct uci_package *p = NULL;
883 struct uci_ptr ptr = { 0 };
885 blobmsg_parse(rpc_uci_rename_policy, __RPC_R_MAX, tb,
886 blob_data(msg), blob_len(msg));
888 if (!tb[RPC_R_CONFIG] || !tb[RPC_R_SECTION] || !tb[RPC_R_NAME])
889 return UBUS_STATUS_INVALID_ARGUMENT;
891 if (!rpc_uci_write_access(tb[RPC_R_SESSION], tb[RPC_R_CONFIG]))
892 return UBUS_STATUS_PERMISSION_DENIED;
894 ptr.package = blobmsg_data(tb[RPC_R_CONFIG]);
895 ptr.section = blobmsg_data(tb[RPC_R_SECTION]);
896 ptr.value = blobmsg_data(tb[RPC_R_NAME]);
898 if (tb[RPC_R_OPTION])
899 ptr.option = blobmsg_data(tb[RPC_R_OPTION]);
901 if (uci_load(cursor, ptr.package, &p))
902 return rpc_uci_status();
904 if (uci_lookup_ptr(cursor, &ptr, NULL, true))
907 if ((ptr.option && !ptr.o) || !ptr.s)
909 cursor->err = UCI_ERR_NOTFOUND;
913 if (uci_rename(cursor, &ptr))
919 uci_unload(cursor, p);
921 return rpc_uci_status();
925 rpc_uci_order(struct ubus_context *ctx, struct ubus_object *obj,
926 struct ubus_request_data *req, const char *method,
927 struct blob_attr *msg)
929 struct blob_attr *tb[__RPC_O_MAX];
930 struct blob_attr *cur;
931 struct uci_package *p = NULL;
932 struct uci_ptr ptr = { 0 };
935 blobmsg_parse(rpc_uci_order_policy, __RPC_O_MAX, tb,
936 blob_data(msg), blob_len(msg));
938 if (!tb[RPC_O_CONFIG] || !tb[RPC_O_SECTIONS])
939 return UBUS_STATUS_INVALID_ARGUMENT;
941 if (!rpc_uci_write_access(tb[RPC_O_SESSION], tb[RPC_O_CONFIG]))
942 return UBUS_STATUS_PERMISSION_DENIED;
944 ptr.package = blobmsg_data(tb[RPC_O_CONFIG]);
946 if (uci_load(cursor, ptr.package, &p))
947 return rpc_uci_status();
949 blobmsg_for_each_attr(cur, tb[RPC_O_SECTIONS], rem)
951 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
955 ptr.section = blobmsg_data(cur);
957 if (uci_lookup_ptr(cursor, &ptr, NULL, true) || !ptr.s)
960 uci_reorder_section(cursor, ptr.s, i++);
964 uci_unload(cursor, p);
966 return rpc_uci_status();
970 rpc_uci_dump_change(struct uci_delta *d)
973 const char *types[] = {
974 [UCI_CMD_REORDER] = "order",
975 [UCI_CMD_REMOVE] = "remove",
976 [UCI_CMD_RENAME] = "rename",
977 [UCI_CMD_ADD] = "add",
978 [UCI_CMD_LIST_ADD] = "list-add",
979 [UCI_CMD_LIST_DEL] = "list-del",
980 [UCI_CMD_CHANGE] = "set",
986 c = blobmsg_open_array(&buf, NULL);
988 blobmsg_add_string(&buf, NULL, types[d->cmd]);
989 blobmsg_add_string(&buf, NULL, d->section);
992 blobmsg_add_string(&buf, NULL, d->e.name);
996 if (d->cmd == UCI_CMD_REORDER)
997 blobmsg_add_u32(&buf, NULL, strtoul(d->value, NULL, 10));
999 blobmsg_add_string(&buf, NULL, d->value);
1002 blobmsg_close_array(&buf, c);
1006 rpc_uci_changes(struct ubus_context *ctx, struct ubus_object *obj,
1007 struct ubus_request_data *req, const char *method,
1008 struct blob_attr *msg)
1010 struct blob_attr *tb[__RPC_C_MAX];
1011 struct uci_package *p = NULL;
1012 struct uci_element *e;
1015 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
1016 blob_data(msg), blob_len(msg));
1018 if (!tb[RPC_C_CONFIG])
1019 return UBUS_STATUS_INVALID_ARGUMENT;
1021 if (!rpc_uci_read_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
1022 return UBUS_STATUS_PERMISSION_DENIED;
1024 if (uci_load(cursor, blobmsg_data(tb[RPC_C_CONFIG]), &p))
1025 return rpc_uci_status();
1027 blob_buf_init(&buf, 0);
1028 c = blobmsg_open_array(&buf, "changes");
1030 uci_foreach_element(&p->saved_delta, e)
1031 rpc_uci_dump_change(uci_to_delta(e));
1033 blobmsg_close_array(&buf, c);
1035 ubus_send_reply(ctx, req, buf.head);
1037 uci_unload(cursor, p);
1039 return rpc_uci_status();
1043 rpc_uci_trigger_event(struct ubus_context *ctx, const char *config)
1045 char *pkg = strdup(config);
1046 static struct blob_buf b;
1049 if (!ubus_lookup_id(ctx, "service", &id)) {
1052 blob_buf_init(&b, 0);
1053 blobmsg_add_string(&b, "type", "config.change");
1054 c = blobmsg_open_table(&b, "data");
1055 blobmsg_add_string(&b, "package", pkg);
1056 blobmsg_close_table(&b, c);
1057 ubus_invoke(ctx, id, "event", b.head, NULL, 0, 1000);
1063 rpc_uci_revert_commit(struct ubus_context *ctx, struct blob_attr *msg, bool commit)
1065 struct blob_attr *tb[__RPC_C_MAX];
1066 struct uci_package *p = NULL;
1067 struct uci_ptr ptr = { 0 };
1070 return UBUS_STATUS_PERMISSION_DENIED;
1072 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
1073 blob_data(msg), blob_len(msg));
1075 if (!tb[RPC_C_CONFIG])
1076 return UBUS_STATUS_INVALID_ARGUMENT;
1078 if (!rpc_uci_write_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
1079 return UBUS_STATUS_PERMISSION_DENIED;
1081 ptr.package = blobmsg_data(tb[RPC_C_CONFIG]);
1085 if (!uci_load(cursor, ptr.package, &p))
1087 uci_commit(cursor, &p, false);
1088 uci_unload(cursor, p);
1090 rpc_uci_trigger_event(ctx, blobmsg_get_string(tb[RPC_C_CONFIG]));
1094 if (!uci_lookup_ptr(cursor, &ptr, NULL, true) && ptr.p)
1095 uci_revert(cursor, &ptr);
1098 return rpc_uci_status();
1102 rpc_uci_revert(struct ubus_context *ctx, struct ubus_object *obj,
1103 struct ubus_request_data *req, const char *method,
1104 struct blob_attr *msg)
1106 return rpc_uci_revert_commit(ctx, msg, false);
1110 rpc_uci_commit(struct ubus_context *ctx, struct ubus_object *obj,
1111 struct ubus_request_data *req, const char *method,
1112 struct blob_attr *msg)
1114 return rpc_uci_revert_commit(ctx, msg, true);
1118 rpc_uci_configs(struct ubus_context *ctx, struct ubus_object *obj,
1119 struct ubus_request_data *req, const char *method,
1120 struct blob_attr *msg)
1126 if (uci_list_configs(cursor, &configs))
1129 blob_buf_init(&buf, 0);
1131 c = blobmsg_open_array(&buf, "configs");
1133 for (i = 0; configs[i]; i++)
1134 blobmsg_add_string(&buf, NULL, configs[i]);
1136 blobmsg_close_array(&buf, c);
1138 ubus_send_reply(ctx, req, buf.head);
1141 return rpc_uci_status();
1146 * Remove given delta save directory (if any).
1149 rpc_uci_purge_dir(const char *path)
1154 char file[PATH_MAX];
1156 if (stat(path, &s) || !S_ISDIR(s.st_mode))
1159 if ((d = opendir(path)) != NULL)
1161 while ((e = readdir(d)) != NULL)
1163 snprintf(file, sizeof(file) - 1, "%s/%s", path, e->d_name);
1165 if (stat(file, &s) || !S_ISREG(s.st_mode))
1178 rpc_uci_apply_config(struct ubus_context *ctx, char *config)
1180 struct uci_package *p = NULL;
1181 struct uci_ptr ptr = { 0 };
1183 ptr.package = config;
1185 if (!uci_load(cursor, ptr.package, &p)) {
1186 uci_commit(cursor, &p, false);
1187 uci_unload(cursor, p);
1189 rpc_uci_trigger_event(ctx, config);
1195 rpc_uci_copy_file(const char *src, const char *target, const char *file)
1200 snprintf(tmp, sizeof(tmp), "%s%s", src, file);
1201 in = fopen(tmp, "rb");
1202 snprintf(tmp, sizeof(tmp), "%s%s", target, file);
1203 out = fopen(tmp, "wb+");
1206 int len = fread(tmp, 1, sizeof(tmp), in);
1209 fwrite(tmp, 1, len, out);
1218 rpc_uci_do_rollback(struct ubus_context *ctx, const char *sid, glob_t *gl)
1224 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
1228 for (i = 0; i < gl->gl_pathc; i++) {
1229 char *config = basename(gl->gl_pathv[i]);
1234 rpc_uci_copy_file(RPC_SNAPSHOT_FILES, RPC_UCI_DIR, config);
1235 rpc_uci_apply_config(ctx, config);
1237 rpc_uci_copy_file(RPC_SNAPSHOT_DELTA, tmp, config);
1240 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1241 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1243 uloop_timeout_cancel(&apply_timer);
1244 apply_running = false;
1249 rpc_uci_apply_timeout(struct uloop_timeout *t)
1254 snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
1255 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1258 rpc_uci_do_rollback(apply_ctx, NULL, &gl);
1262 rpc_uci_apply_access(const char *sid, glob_t *gl)
1267 if (gl->gl_pathc < 3)
1268 return UBUS_STATUS_NO_DATA;
1270 for (i = 0; i < gl->gl_pathc; i++) {
1271 char *config = basename(gl->gl_pathv[i]);
1275 if (stat(gl->gl_pathv[i], &s) || !s.st_size)
1277 if (!rpc_session_access(sid, "uci", config, "write"))
1278 return UBUS_STATUS_PERMISSION_DENIED;
1283 return UBUS_STATUS_NO_DATA;
1289 rpc_uci_apply(struct ubus_context *ctx, struct ubus_object *obj,
1290 struct ubus_request_data *req, const char *method,
1291 struct blob_attr *msg)
1293 struct blob_attr *tb[__RPC_T_MAX];
1294 int timeout = RPC_APPLY_TIMEOUT;
1296 bool commit = false;
1301 blobmsg_parse(rpc_uci_apply_policy, __RPC_T_MAX, tb,
1302 blob_data(msg), blob_len(msg));
1304 if (tb[RPC_T_COMMIT])
1305 commit = blobmsg_get_bool(tb[RPC_T_COMMIT]);
1307 if (apply_running && !commit)
1308 return UBUS_STATUS_PERMISSION_DENIED;
1310 if (!tb[RPC_T_SESSION])
1311 return UBUS_STATUS_INVALID_ARGUMENT;
1313 sid = blobmsg_data(tb[RPC_T_SESSION]);
1315 if (tb[RPC_T_TIMEOUT])
1316 timeout = blobmsg_get_u32(tb[RPC_T_TIMEOUT]);
1318 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1319 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1321 if (!apply_running) {
1322 mkdir(RPC_SNAPSHOT_FILES, 0700);
1323 mkdir(RPC_SNAPSHOT_DELTA, 0700);
1325 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/*", sid);
1326 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1329 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
1331 ret = rpc_uci_apply_access(sid, &gl);
1337 for (i = 0; i < gl.gl_pathc; i++) {
1338 char *config = basename(gl.gl_pathv[i]);
1344 if (stat(gl.gl_pathv[i], &s) || !s.st_size)
1347 rpc_uci_copy_file(RPC_UCI_DIR, RPC_SNAPSHOT_FILES, config);
1348 rpc_uci_copy_file(tmp, RPC_SNAPSHOT_DELTA, config);
1349 rpc_uci_apply_config(ctx, config);
1354 apply_running = true;
1355 apply_timer.cb = rpc_uci_apply_timeout;
1356 uloop_timeout_set(&apply_timer, timeout * 1000);
1360 if (apply_running && commit) {
1361 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1362 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1364 uloop_timeout_cancel(&apply_timer);
1365 apply_running = false;
1373 rpc_uci_rollback(struct ubus_context *ctx, struct ubus_object *obj,
1374 struct ubus_request_data *req, const char *method,
1375 struct blob_attr *msg)
1377 struct blob_attr *tb[__RPC_B_MAX];
1383 blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
1384 blob_data(msg), blob_len(msg));
1387 return UBUS_STATUS_PERMISSION_DENIED;
1389 if (!tb[RPC_B_SESSION])
1390 return UBUS_STATUS_INVALID_ARGUMENT;
1392 sid = blobmsg_data(tb[RPC_B_SESSION]);
1394 snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
1395 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1398 ret = rpc_uci_apply_access(sid, &gl);
1404 rpc_uci_do_rollback(ctx, sid, &gl);
1413 * Session destroy callback to purge associated delta directory.
1416 rpc_uci_purge_savedir_cb(struct rpc_session *ses, void *priv)
1418 char path[PATH_MAX];
1420 snprintf(path, sizeof(path) - 1, RPC_UCI_SAVEDIR_PREFIX "%s", ses->id);
1421 rpc_uci_purge_dir(path);
1425 * Removes all delta directories which match the RPC_UCI_SAVEDIR_PREFIX.
1426 * This is used to clean up garbage when starting rpcd.
1428 void rpc_uci_purge_savedirs(void)
1433 if (!glob(RPC_UCI_SAVEDIR_PREFIX "*", 0, NULL, &gl))
1435 for (i = 0; i < gl.gl_pathc; i++)
1436 rpc_uci_purge_dir(gl.gl_pathv[i]);
1442 int rpc_uci_api_init(struct ubus_context *ctx)
1444 static const struct ubus_method uci_methods[] = {
1445 { .name = "configs", .handler = rpc_uci_configs },
1446 UBUS_METHOD("get", rpc_uci_get, rpc_uci_get_policy),
1447 UBUS_METHOD("add", rpc_uci_add, rpc_uci_add_policy),
1448 UBUS_METHOD("set", rpc_uci_set, rpc_uci_set_policy),
1449 UBUS_METHOD("delete", rpc_uci_delete, rpc_uci_delete_policy),
1450 UBUS_METHOD("rename", rpc_uci_rename, rpc_uci_rename_policy),
1451 UBUS_METHOD("order", rpc_uci_order, rpc_uci_order_policy),
1452 UBUS_METHOD("changes", rpc_uci_changes, rpc_uci_config_policy),
1453 UBUS_METHOD("revert", rpc_uci_revert, rpc_uci_config_policy),
1454 UBUS_METHOD("commit", rpc_uci_commit, rpc_uci_config_policy),
1455 UBUS_METHOD("apply", rpc_uci_apply, rpc_uci_apply_policy),
1456 UBUS_METHOD("rollback", rpc_uci_rollback, rpc_uci_rollback_policy),
1459 static struct ubus_object_type uci_type =
1460 UBUS_OBJECT_TYPE("luci-rpc-uci", uci_methods);
1462 static struct ubus_object obj = {
1465 .methods = uci_methods,
1466 .n_methods = ARRAY_SIZE(uci_methods),
1469 static struct rpc_session_cb cb = {
1470 .cb = rpc_uci_purge_savedir_cb
1473 cursor = uci_alloc_context();
1476 return UBUS_STATUS_UNKNOWN_ERROR;
1478 rpc_session_destroy_cb(&cb);
1480 return ubus_add_object(ctx, &obj);