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
)
213 struct uci_element
*e
, *tmp
;
216 uci_foreach_element_safe(&cursor
->delta_path
, tmp
, e
)
219 cursor
->delta_path
.prev
= &cursor
->delta_path
;
220 cursor
->delta_path
.next
= &cursor
->delta_path
;
224 uci_set_savedir(cursor
, "/tmp/.uci");
228 snprintf(path
, sizeof(path
) - 1,
229 RPC_UCI_SAVEDIR_PREFIX
"%s", blobmsg_get_string(sid
));
231 uci_set_savedir(cursor
, path
);
235 * Test read access to given config. If the passed "sid" blob attribute pointer
236 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
237 * perform access control and always assume true.
240 rpc_uci_read_access(struct blob_attr
*sid
, struct blob_attr
*config
)
242 rpc_uci_set_savedir(sid
);
247 return rpc_session_access(blobmsg_data(sid
), "uci",
248 blobmsg_data(config
), "read");
252 * Test write access to given config. If the passed "sid" blob attribute pointer
253 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
254 * perform access control and always assume true.
257 rpc_uci_write_access(struct blob_attr
*sid
, struct blob_attr
*config
)
259 rpc_uci_set_savedir(sid
);
264 return rpc_session_access(blobmsg_data(sid
), "uci",
265 blobmsg_data(config
), "write");
269 * Format applicable blob value as string and place a pointer to the string
270 * buffer in "p". Uses a static string buffer.
273 rpc_uci_format_blob(struct blob_attr
*v
, const char **p
)
279 switch (blobmsg_type(v
))
281 case BLOBMSG_TYPE_STRING
:
282 *p
= blobmsg_data(v
);
285 case BLOBMSG_TYPE_INT64
:
286 snprintf(buf
, sizeof(buf
), "%"PRIu64
, blobmsg_get_u64(v
));
290 case BLOBMSG_TYPE_INT32
:
291 snprintf(buf
, sizeof(buf
), "%u", blobmsg_get_u32(v
));
295 case BLOBMSG_TYPE_INT16
:
296 snprintf(buf
, sizeof(buf
), "%u", blobmsg_get_u16(v
));
300 case BLOBMSG_TYPE_INT8
:
301 snprintf(buf
, sizeof(buf
), "%u", !!blobmsg_get_u8(v
));
313 * Lookup the given uci_ptr and enable extended lookup format if the .section
314 * value of the uci_ptr looks like extended syntax. Uses an internal copy
315 * of the given uci_ptr to perform the lookup as failing extended section
316 * lookup operations in libuci will zero our the uci_ptr struct.
317 * Copies the internal uci_ptr back to given the uci_ptr on success.
320 rpc_uci_lookup(struct uci_ptr
*ptr
)
323 struct uci_ptr lookup
= *ptr
;
325 if (!lookup
.s
&& lookup
.section
&& *lookup
.section
== '@')
326 lookup
.flags
|= UCI_LOOKUP_EXTENDED
;
328 rv
= uci_lookup_ptr(cursor
, &lookup
, NULL
, true);
337 * Checks whether the given uci_option object matches the given string value.
338 * 1) If the uci_option is of type list, check whether any of the list elements
339 * equals to the given string
340 * 2) If the uci_option is of type string, parse it into space separated tokens
341 * and check if any of the tokens equals to the given string.
342 * Returns true if a list element or token matched the given string.
345 rpc_uci_match_option(struct uci_option
*o
, const char *cmp
)
347 struct uci_element
*e
;
350 if (o
->type
== UCI_TYPE_LIST
)
352 uci_foreach_element(&o
->v
.list
, e
)
353 if (e
->name
&& !strcmp(e
->name
, cmp
))
362 s
= strdup(o
->v
.string
);
367 for (p
= strtok(s
, " \t"); p
; p
= strtok(NULL
, " \t"))
381 * Checks whether the given uci_section matches the type and value blob attrs.
382 * 1) Returns false if "type" is given and the section type does not match
383 * the value specified in the "type" string blob attribute, else continue.
384 * 2) Tests whether any key in the "matches" table blob attribute exists in
385 * the given uci_section and whether each value is contained in the
386 * corresponding uci option value (see rpc_uci_match_option()).
387 * 3) A missing or empty "matches" table blob attribute is always considered
389 * Returns true if "type" matches or is NULL and "matches" matches or is NULL.
392 rpc_uci_match_section(struct uci_section
*s
,
393 struct blob_attr
*type
, struct blob_attr
*matches
)
395 struct uci_element
*e
;
396 struct blob_attr
*cur
;
402 if (type
&& strcmp(s
->type
, blobmsg_data(type
)))
408 blobmsg_for_each_attr(cur
, matches
, rem
)
410 if (!rpc_uci_format_blob(cur
, &cmp
))
413 uci_foreach_element(&s
->options
, e
)
415 if (strcmp(e
->name
, blobmsg_name(cur
)))
418 if (!rpc_uci_match_option(uci_to_option(e
), cmp
))
427 return (empty
|| match
);
431 * Dump the given uci_option value into the global blobmsg buffer and use
432 * given "name" as key.
433 * 1) If the uci_option is of type list, put a table into the blob buffer and
434 * add each list item as string to it.
435 * 2) If the uci_option is of type string, put its value directly into the blob
439 rpc_uci_dump_option(struct uci_option
*o
, const char *name
)
442 struct uci_element
*e
;
446 case UCI_TYPE_STRING
:
447 blobmsg_add_string(&buf
, name
, o
->v
.string
);
451 c
= blobmsg_open_array(&buf
, name
);
453 uci_foreach_element(&o
->v
.list
, e
)
454 blobmsg_add_string(&buf
, NULL
, e
->name
);
456 blobmsg_close_array(&buf
, c
);
465 * Dump the given uci_section object into the global blobmsg buffer and use
466 * given "name" as key.
467 * Puts a table into the blob buffer and puts each section option member value
468 * as value into the table using the option name as key.
469 * Adds three special keys ".anonymous", ".type" and ".name" which specify the
470 * corresponding section properties.
473 rpc_uci_dump_section(struct uci_section
*s
, const char *name
, int index
)
476 struct uci_option
*o
;
477 struct uci_element
*e
;
479 c
= blobmsg_open_table(&buf
, name
);
481 blobmsg_add_u8(&buf
, ".anonymous", s
->anonymous
);
482 blobmsg_add_string(&buf
, ".type", s
->type
);
483 blobmsg_add_string(&buf
, ".name", s
->e
.name
);
486 blobmsg_add_u32(&buf
, ".index", index
);
488 uci_foreach_element(&s
->options
, e
)
490 o
= uci_to_option(e
);
491 rpc_uci_dump_option(o
, o
->e
.name
);
494 blobmsg_close_table(&buf
, c
);
498 * Dump the given uci_package object into the global blobmsg buffer and use
499 * given "name" as key.
500 * Puts a table into the blob buffer and puts each package section member as
501 * value into the table using the section name as key.
502 * Only dumps sections matching the given "type" and "matches", see explaination
503 * of rpc_uci_match_section() for details.
506 rpc_uci_dump_package(struct uci_package
*p
, const char *name
,
507 struct blob_attr
*type
, struct blob_attr
*matches
)
510 struct uci_element
*e
;
513 c
= blobmsg_open_table(&buf
, name
);
515 uci_foreach_element(&p
->sections
, e
)
519 if (!rpc_uci_match_section(uci_to_section(e
), type
, matches
))
522 rpc_uci_dump_section(uci_to_section(e
), e
->name
, i
);
525 blobmsg_close_table(&buf
, c
);
530 rpc_uci_getcommon(struct ubus_context
*ctx
, struct ubus_request_data
*req
,
531 struct blob_attr
*msg
, bool use_state
)
533 struct blob_attr
*tb
[__RPC_G_MAX
];
534 struct uci_package
*p
= NULL
;
535 struct uci_ptr ptr
= { 0 };
537 blobmsg_parse(rpc_uci_get_policy
, __RPC_G_MAX
, tb
,
538 blob_data(msg
), blob_len(msg
));
540 if (!tb
[RPC_G_CONFIG
])
541 return UBUS_STATUS_INVALID_ARGUMENT
;
543 if (!rpc_uci_read_access(tb
[RPC_G_SESSION
], tb
[RPC_G_CONFIG
]))
544 return UBUS_STATUS_PERMISSION_DENIED
;
546 ptr
.package
= blobmsg_data(tb
[RPC_G_CONFIG
]);
549 uci_set_savedir(cursor
, "/var/state");
551 if (uci_load(cursor
, ptr
.package
, &p
))
552 return rpc_uci_status();
554 if (tb
[RPC_G_SECTION
])
556 ptr
.section
= blobmsg_data(tb
[RPC_G_SECTION
]);
558 if (tb
[RPC_G_OPTION
])
559 ptr
.option
= blobmsg_data(tb
[RPC_G_OPTION
]);
562 if (rpc_uci_lookup(&ptr
) || !(ptr
.flags
& UCI_LOOKUP_COMPLETE
))
565 blob_buf_init(&buf
, 0);
567 switch (ptr
.last
->type
)
569 case UCI_TYPE_PACKAGE
:
570 rpc_uci_dump_package(ptr
.p
, "values", tb
[RPC_G_TYPE
], tb
[RPC_G_MATCH
]);
573 case UCI_TYPE_SECTION
:
574 rpc_uci_dump_section(ptr
.s
, "values", -1);
577 case UCI_TYPE_OPTION
:
578 rpc_uci_dump_option(ptr
.o
, "value");
585 ubus_send_reply(ctx
, req
, buf
.head
);
588 uci_unload(cursor
, p
);
590 return rpc_uci_status();
594 rpc_uci_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
595 struct ubus_request_data
*req
, const char *method
,
596 struct blob_attr
*msg
)
598 return rpc_uci_getcommon(ctx
, req
, msg
, false);
602 rpc_uci_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
603 struct ubus_request_data
*req
, const char *method
,
604 struct blob_attr
*msg
)
606 return rpc_uci_getcommon(ctx
, req
, msg
, true);
610 rpc_uci_add(struct ubus_context
*ctx
, struct ubus_object
*obj
,
611 struct ubus_request_data
*req
, const char *method
,
612 struct blob_attr
*msg
)
614 struct blob_attr
*tb
[__RPC_A_MAX
];
615 struct blob_attr
*cur
, *elem
;
616 struct uci_package
*p
= NULL
;
617 struct uci_section
*s
;
618 struct uci_ptr ptr
= { 0 };
621 blobmsg_parse(rpc_uci_add_policy
, __RPC_A_MAX
, tb
,
622 blob_data(msg
), blob_len(msg
));
624 if (!tb
[RPC_A_CONFIG
] || !tb
[RPC_A_TYPE
])
625 return UBUS_STATUS_INVALID_ARGUMENT
;
627 if (!rpc_uci_write_access(tb
[RPC_A_SESSION
], tb
[RPC_A_CONFIG
]))
628 return UBUS_STATUS_PERMISSION_DENIED
;
630 ptr
.package
= blobmsg_data(tb
[RPC_A_CONFIG
]);
632 if (uci_load(cursor
, ptr
.package
, &p
))
633 return rpc_uci_status();
635 /* add named section */
638 ptr
.section
= blobmsg_data(tb
[RPC_A_NAME
]);
639 ptr
.value
= blobmsg_data(tb
[RPC_A_TYPE
]);
642 if (rpc_uci_lookup(&ptr
) || uci_set(cursor
, &ptr
))
646 /* add anon section */
649 if (uci_add_section(cursor
, p
, blobmsg_data(tb
[RPC_A_TYPE
]), &s
) || !s
)
652 ptr
.section
= s
->e
.name
;
655 if (tb
[RPC_A_VALUES
])
657 blobmsg_for_each_attr(cur
, tb
[RPC_A_VALUES
], rem
)
660 ptr
.option
= blobmsg_name(cur
);
662 if (rpc_uci_lookup(&ptr
) || !ptr
.s
)
665 switch (blobmsg_type(cur
))
667 case BLOBMSG_TYPE_ARRAY
:
668 blobmsg_for_each_attr(elem
, cur
, rem2
)
669 if (rpc_uci_format_blob(elem
, &ptr
.value
))
670 uci_add_list(cursor
, &ptr
);
674 if (rpc_uci_format_blob(cur
, &ptr
.value
))
675 uci_set(cursor
, &ptr
);
683 blob_buf_init(&buf
, 0);
684 blobmsg_add_string(&buf
, "section", ptr
.section
);
685 ubus_send_reply(ctx
, req
, buf
.head
);
688 uci_unload(cursor
, p
);
690 return rpc_uci_status();
694 * Turn value from a blob attribute into uci set operation
695 * 1) if the blob is of type array, delete existing option (if any) and
696 * emit uci add_list operations for each element
697 * 2) if the blob is not an array but an option of type list exists,
698 * delete existing list and emit uci set operation for the blob value
699 * 3) in all other cases only emit a set operation if there is no existing
700 * option of if the existing options value differs from the blob value
703 rpc_uci_merge_set(struct blob_attr
*opt
, struct uci_ptr
*ptr
)
705 struct blob_attr
*cur
;
709 ptr
->option
= blobmsg_name(opt
);
712 if (rpc_uci_lookup(ptr
) || !ptr
->s
)
715 if (blobmsg_type(opt
) == BLOBMSG_TYPE_ARRAY
)
718 uci_delete(cursor
, ptr
);
720 blobmsg_for_each_attr(cur
, opt
, rem
)
721 if (rpc_uci_format_blob(cur
, &ptr
->value
))
722 uci_add_list(cursor
, ptr
);
724 else if (ptr
->o
&& ptr
->o
->type
== UCI_TYPE_LIST
)
726 uci_delete(cursor
, ptr
);
728 if (rpc_uci_format_blob(opt
, &ptr
->value
))
729 uci_set(cursor
, ptr
);
731 else if (rpc_uci_format_blob(opt
, &ptr
->value
))
733 if (!ptr
->o
|| !ptr
->o
->v
.string
|| strcmp(ptr
->o
->v
.string
, ptr
->value
))
734 uci_set(cursor
, ptr
);
739 rpc_uci_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
740 struct ubus_request_data
*req
, const char *method
,
741 struct blob_attr
*msg
)
743 struct blob_attr
*tb
[__RPC_S_MAX
];
744 struct blob_attr
*cur
;
745 struct uci_package
*p
= NULL
;
746 struct uci_element
*e
;
747 struct uci_ptr ptr
= { 0 };
750 blobmsg_parse(rpc_uci_set_policy
, __RPC_S_MAX
, tb
,
751 blob_data(msg
), blob_len(msg
));
753 if (!tb
[RPC_S_CONFIG
] || !tb
[RPC_S_VALUES
] ||
754 (!tb
[RPC_S_SECTION
] && !tb
[RPC_S_TYPE
] && !tb
[RPC_S_MATCH
]))
755 return UBUS_STATUS_INVALID_ARGUMENT
;
757 if (!rpc_uci_write_access(tb
[RPC_S_SESSION
], tb
[RPC_S_CONFIG
]))
758 return UBUS_STATUS_PERMISSION_DENIED
;
760 ptr
.package
= blobmsg_data(tb
[RPC_S_CONFIG
]);
762 if (uci_load(cursor
, ptr
.package
, &p
))
763 return rpc_uci_status();
765 if (tb
[RPC_S_SECTION
])
767 ptr
.section
= blobmsg_data(tb
[RPC_S_SECTION
]);
768 blobmsg_for_each_attr(cur
, tb
[RPC_S_VALUES
], rem
)
769 rpc_uci_merge_set(cur
, &ptr
);
773 uci_foreach_element(&p
->sections
, e
)
775 if (!rpc_uci_match_section(uci_to_section(e
),
776 tb
[RPC_S_TYPE
], tb
[RPC_S_MATCH
]))
780 ptr
.section
= e
->name
;
782 blobmsg_for_each_attr(cur
, tb
[RPC_S_VALUES
], rem
)
783 rpc_uci_merge_set(cur
, &ptr
);
788 uci_unload(cursor
, p
);
790 return rpc_uci_status();
794 * Delete option or section from uci specified by given blob attribute pointer
795 * 1) if the blob is of type array, delete any option named after each element
796 * 2) if the blob is of type string, delete the option named after its value
797 * 3) if the blob is NULL, delete entire section
800 rpc_uci_merge_delete(struct blob_attr
*opt
, struct uci_ptr
*ptr
)
802 struct blob_attr
*cur
;
805 if (rpc_uci_lookup(ptr
) || !ptr
->s
)
813 uci_delete(cursor
, ptr
);
815 else if (blobmsg_type(opt
) == BLOBMSG_TYPE_ARRAY
)
817 blobmsg_for_each_attr(cur
, opt
, rem
)
819 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
823 ptr
->option
= blobmsg_data(cur
);
825 if (rpc_uci_lookup(ptr
) || !ptr
->o
)
828 uci_delete(cursor
, ptr
);
831 else if (blobmsg_type(opt
) == BLOBMSG_TYPE_STRING
)
834 ptr
->option
= blobmsg_data(opt
);
836 if (rpc_uci_lookup(ptr
) || !ptr
->o
)
839 uci_delete(cursor
, ptr
);
844 rpc_uci_delete(struct ubus_context
*ctx
, struct ubus_object
*obj
,
845 struct ubus_request_data
*req
, const char *method
,
846 struct blob_attr
*msg
)
848 struct blob_attr
*tb
[__RPC_D_MAX
];
849 struct uci_package
*p
= NULL
;
850 struct uci_element
*e
, *tmp
;
851 struct uci_ptr ptr
= { 0 };
853 blobmsg_parse(rpc_uci_delete_policy
, __RPC_D_MAX
, tb
,
854 blob_data(msg
), blob_len(msg
));
856 if (!tb
[RPC_D_CONFIG
] ||
857 (!tb
[RPC_D_SECTION
] && !tb
[RPC_D_TYPE
] && !tb
[RPC_D_MATCH
]))
858 return UBUS_STATUS_INVALID_ARGUMENT
;
860 if (!rpc_uci_write_access(tb
[RPC_D_SESSION
], tb
[RPC_D_CONFIG
]))
861 return UBUS_STATUS_PERMISSION_DENIED
;
863 ptr
.package
= blobmsg_data(tb
[RPC_D_CONFIG
]);
865 if (uci_load(cursor
, ptr
.package
, &p
))
866 return rpc_uci_status();
868 if (tb
[RPC_D_SECTION
])
870 ptr
.section
= blobmsg_data(tb
[RPC_D_SECTION
]);
872 if (tb
[RPC_D_OPTIONS
])
873 rpc_uci_merge_delete(tb
[RPC_D_OPTIONS
], &ptr
);
875 rpc_uci_merge_delete(tb
[RPC_D_OPTION
], &ptr
);
879 uci_foreach_element_safe(&p
->sections
, tmp
, e
)
881 if (!rpc_uci_match_section(uci_to_section(e
),
882 tb
[RPC_D_TYPE
], tb
[RPC_D_MATCH
]))
886 ptr
.section
= e
->name
;
888 if (tb
[RPC_D_OPTIONS
])
889 rpc_uci_merge_delete(tb
[RPC_D_OPTIONS
], &ptr
);
891 rpc_uci_merge_delete(tb
[RPC_D_OPTION
], &ptr
);
896 uci_unload(cursor
, p
);
898 return rpc_uci_status();
902 rpc_uci_rename(struct ubus_context
*ctx
, struct ubus_object
*obj
,
903 struct ubus_request_data
*req
, const char *method
,
904 struct blob_attr
*msg
)
906 struct blob_attr
*tb
[__RPC_R_MAX
];
907 struct uci_package
*p
= NULL
;
908 struct uci_ptr ptr
= { 0 };
910 blobmsg_parse(rpc_uci_rename_policy
, __RPC_R_MAX
, tb
,
911 blob_data(msg
), blob_len(msg
));
913 if (!tb
[RPC_R_CONFIG
] || !tb
[RPC_R_SECTION
] || !tb
[RPC_R_NAME
])
914 return UBUS_STATUS_INVALID_ARGUMENT
;
916 if (!rpc_uci_write_access(tb
[RPC_R_SESSION
], tb
[RPC_R_CONFIG
]))
917 return UBUS_STATUS_PERMISSION_DENIED
;
919 ptr
.package
= blobmsg_data(tb
[RPC_R_CONFIG
]);
920 ptr
.section
= blobmsg_data(tb
[RPC_R_SECTION
]);
921 ptr
.value
= blobmsg_data(tb
[RPC_R_NAME
]);
923 if (tb
[RPC_R_OPTION
])
924 ptr
.option
= blobmsg_data(tb
[RPC_R_OPTION
]);
926 if (uci_load(cursor
, ptr
.package
, &p
))
927 return rpc_uci_status();
929 if (uci_lookup_ptr(cursor
, &ptr
, NULL
, true))
932 if ((ptr
.option
&& !ptr
.o
) || !ptr
.s
)
934 cursor
->err
= UCI_ERR_NOTFOUND
;
938 if (uci_rename(cursor
, &ptr
))
944 uci_unload(cursor
, p
);
946 return rpc_uci_status();
950 rpc_uci_order(struct ubus_context
*ctx
, struct ubus_object
*obj
,
951 struct ubus_request_data
*req
, const char *method
,
952 struct blob_attr
*msg
)
954 struct blob_attr
*tb
[__RPC_O_MAX
];
955 struct blob_attr
*cur
;
956 struct uci_package
*p
= NULL
;
957 struct uci_ptr ptr
= { 0 };
960 blobmsg_parse(rpc_uci_order_policy
, __RPC_O_MAX
, tb
,
961 blob_data(msg
), blob_len(msg
));
963 if (!tb
[RPC_O_CONFIG
] || !tb
[RPC_O_SECTIONS
])
964 return UBUS_STATUS_INVALID_ARGUMENT
;
966 if (!rpc_uci_write_access(tb
[RPC_O_SESSION
], tb
[RPC_O_CONFIG
]))
967 return UBUS_STATUS_PERMISSION_DENIED
;
969 ptr
.package
= blobmsg_data(tb
[RPC_O_CONFIG
]);
971 if (uci_load(cursor
, ptr
.package
, &p
))
972 return rpc_uci_status();
974 blobmsg_for_each_attr(cur
, tb
[RPC_O_SECTIONS
], rem
)
976 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
980 ptr
.section
= blobmsg_data(cur
);
982 if (uci_lookup_ptr(cursor
, &ptr
, NULL
, true) || !ptr
.s
)
985 uci_reorder_section(cursor
, ptr
.s
, i
++);
989 uci_unload(cursor
, p
);
991 return rpc_uci_status();
995 rpc_uci_dump_change(struct uci_delta
*d
)
998 const char *types
[] = {
999 [UCI_CMD_REORDER
] = "order",
1000 [UCI_CMD_REMOVE
] = "remove",
1001 [UCI_CMD_RENAME
] = "rename",
1002 [UCI_CMD_ADD
] = "add",
1003 [UCI_CMD_LIST_ADD
] = "list-add",
1004 [UCI_CMD_LIST_DEL
] = "list-del",
1005 [UCI_CMD_CHANGE
] = "set",
1011 c
= blobmsg_open_array(&buf
, NULL
);
1013 blobmsg_add_string(&buf
, NULL
, types
[d
->cmd
]);
1014 blobmsg_add_string(&buf
, NULL
, d
->section
);
1017 blobmsg_add_string(&buf
, NULL
, d
->e
.name
);
1021 if (d
->cmd
== UCI_CMD_REORDER
)
1022 blobmsg_add_u32(&buf
, NULL
, strtoul(d
->value
, NULL
, 10));
1024 blobmsg_add_string(&buf
, NULL
, d
->value
);
1027 blobmsg_close_array(&buf
, c
);
1031 rpc_uci_changes(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1032 struct ubus_request_data
*req
, const char *method
,
1033 struct blob_attr
*msg
)
1035 struct blob_attr
*tb
[__RPC_C_MAX
];
1036 struct uci_package
*p
= NULL
;
1037 struct uci_element
*e
;
1042 blobmsg_parse(rpc_uci_config_policy
, __RPC_C_MAX
, tb
,
1043 blob_data(msg
), blob_len(msg
));
1045 if (tb
[RPC_C_CONFIG
])
1047 if (!rpc_uci_read_access(tb
[RPC_C_SESSION
], tb
[RPC_C_CONFIG
]))
1048 return UBUS_STATUS_PERMISSION_DENIED
;
1050 if (uci_load(cursor
, blobmsg_data(tb
[RPC_C_CONFIG
]), &p
))
1051 return rpc_uci_status();
1053 blob_buf_init(&buf
, 0);
1054 c
= blobmsg_open_array(&buf
, "changes");
1056 uci_foreach_element(&p
->saved_delta
, e
)
1057 rpc_uci_dump_change(uci_to_delta(e
));
1059 blobmsg_close_array(&buf
, c
);
1061 uci_unload(cursor
, p
);
1063 ubus_send_reply(ctx
, req
, buf
.head
);
1065 return rpc_uci_status();
1068 rpc_uci_set_savedir(tb
[RPC_C_SESSION
]);
1070 if (uci_list_configs(cursor
, &configs
))
1071 return rpc_uci_status();
1073 blob_buf_init(&buf
, 0);
1075 c
= blobmsg_open_table(&buf
, "changes");
1077 for (i
= 0; configs
[i
]; i
++)
1079 if (tb
[RPC_C_SESSION
] &&
1080 !rpc_session_access(blobmsg_data(tb
[RPC_C_SESSION
]), "uci",
1081 configs
[i
], "read"))
1084 if (uci_load(cursor
, configs
[i
], &p
))
1087 if (!uci_list_empty(&p
->saved_delta
))
1089 d
= blobmsg_open_array(&buf
, configs
[i
]);
1091 uci_foreach_element(&p
->saved_delta
, e
)
1092 rpc_uci_dump_change(uci_to_delta(e
));
1094 blobmsg_close_array(&buf
, d
);
1097 uci_unload(cursor
, p
);
1100 blobmsg_close_table(&buf
, c
);
1102 ubus_send_reply(ctx
, req
, buf
.head
);
1108 rpc_uci_trigger_event(struct ubus_context
*ctx
, const char *config
)
1110 char *pkg
= strdup(config
);
1111 static struct blob_buf b
;
1114 if (!ubus_lookup_id(ctx
, "service", &id
)) {
1117 blob_buf_init(&b
, 0);
1118 blobmsg_add_string(&b
, "type", "config.change");
1119 c
= blobmsg_open_table(&b
, "data");
1120 blobmsg_add_string(&b
, "package", pkg
);
1121 blobmsg_close_table(&b
, c
);
1122 ubus_invoke(ctx
, id
, "event", b
.head
, NULL
, 0, 1000);
1128 rpc_uci_revert_commit(struct ubus_context
*ctx
, struct blob_attr
*msg
, bool commit
)
1130 struct blob_attr
*tb
[__RPC_C_MAX
];
1131 struct uci_package
*p
= NULL
;
1132 struct uci_ptr ptr
= { 0 };
1135 return UBUS_STATUS_PERMISSION_DENIED
;
1137 blobmsg_parse(rpc_uci_config_policy
, __RPC_C_MAX
, tb
,
1138 blob_data(msg
), blob_len(msg
));
1140 if (!tb
[RPC_C_CONFIG
])
1141 return UBUS_STATUS_INVALID_ARGUMENT
;
1143 if (!rpc_uci_write_access(tb
[RPC_C_SESSION
], tb
[RPC_C_CONFIG
]))
1144 return UBUS_STATUS_PERMISSION_DENIED
;
1146 ptr
.package
= blobmsg_data(tb
[RPC_C_CONFIG
]);
1150 if (!uci_load(cursor
, ptr
.package
, &p
))
1152 uci_commit(cursor
, &p
, false);
1153 uci_unload(cursor
, p
);
1154 rpc_uci_trigger_event(ctx
, blobmsg_get_string(tb
[RPC_C_CONFIG
]));
1159 if (!uci_lookup_ptr(cursor
, &ptr
, NULL
, true) && ptr
.p
)
1161 uci_revert(cursor
, &ptr
);
1162 uci_unload(cursor
, ptr
.p
);
1166 return rpc_uci_status();
1170 rpc_uci_revert(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1171 struct ubus_request_data
*req
, const char *method
,
1172 struct blob_attr
*msg
)
1174 return rpc_uci_revert_commit(ctx
, msg
, false);
1178 rpc_uci_commit(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1179 struct ubus_request_data
*req
, const char *method
,
1180 struct blob_attr
*msg
)
1182 return rpc_uci_revert_commit(ctx
, msg
, true);
1186 rpc_uci_configs(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1187 struct ubus_request_data
*req
, const char *method
,
1188 struct blob_attr
*msg
)
1194 if (uci_list_configs(cursor
, &configs
))
1197 blob_buf_init(&buf
, 0);
1199 c
= blobmsg_open_array(&buf
, "configs");
1201 for (i
= 0; configs
[i
]; i
++)
1202 blobmsg_add_string(&buf
, NULL
, configs
[i
]);
1204 blobmsg_close_array(&buf
, c
);
1206 ubus_send_reply(ctx
, req
, buf
.head
);
1209 return rpc_uci_status();
1214 * Remove given delta save directory (if any).
1217 rpc_uci_purge_dir(const char *path
)
1222 char file
[PATH_MAX
];
1224 if (stat(path
, &s
) || !S_ISDIR(s
.st_mode
))
1227 if ((d
= opendir(path
)) != NULL
)
1229 while ((e
= readdir(d
)) != NULL
)
1231 snprintf(file
, sizeof(file
) - 1, "%s/%s", path
, e
->d_name
);
1233 if (stat(file
, &s
) || !S_ISREG(s
.st_mode
))
1246 rpc_uci_apply_config(struct ubus_context
*ctx
, char *config
)
1248 struct uci_package
*p
= NULL
;
1250 if (!uci_load(cursor
, config
, &p
)) {
1251 uci_commit(cursor
, &p
, false);
1252 uci_unload(cursor
, p
);
1254 rpc_uci_trigger_event(ctx
, config
);
1260 rpc_uci_copy_file(const char *src
, const char *target
, const char *file
)
1265 snprintf(tmp
, sizeof(tmp
), "%s%s", src
, file
);
1266 in
= fopen(tmp
, "rb");
1267 snprintf(tmp
, sizeof(tmp
), "%s%s", target
, file
);
1268 out
= fopen(tmp
, "wb+");
1271 int len
= fread(tmp
, 1, sizeof(tmp
), in
);
1274 fwrite(tmp
, 1, len
, out
);
1283 rpc_uci_do_rollback(struct ubus_context
*ctx
, const char *sid
, glob_t
*gl
)
1289 snprintf(tmp
, sizeof(tmp
), RPC_UCI_SAVEDIR_PREFIX
"%s/", sid
);
1293 for (i
= 0; i
< gl
->gl_pathc
; i
++) {
1294 char *config
= basename(gl
->gl_pathv
[i
]);
1299 rpc_uci_copy_file(RPC_SNAPSHOT_FILES
, RPC_UCI_DIR
, config
);
1300 rpc_uci_apply_config(ctx
, config
);
1302 rpc_uci_copy_file(RPC_SNAPSHOT_DELTA
, tmp
, config
);
1305 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES
);
1306 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA
);
1308 uloop_timeout_cancel(&apply_timer
);
1309 memset(apply_sid
, 0, sizeof(apply_sid
));
1314 rpc_uci_apply_timeout(struct uloop_timeout
*t
)
1319 snprintf(tmp
, sizeof(tmp
), "%s/*", RPC_SNAPSHOT_FILES
);
1320 if (glob(tmp
, GLOB_PERIOD
, NULL
, &gl
) < 0)
1323 rpc_uci_do_rollback(apply_ctx
, NULL
, &gl
);
1327 rpc_uci_apply_access(const char *sid
, glob_t
*gl
)
1332 if (gl
->gl_pathc
< 3)
1333 return UBUS_STATUS_NO_DATA
;
1335 for (i
= 0; i
< gl
->gl_pathc
; i
++) {
1336 char *config
= basename(gl
->gl_pathv
[i
]);
1340 if (stat(gl
->gl_pathv
[i
], &s
) || !s
.st_size
)
1342 if (!rpc_session_access(sid
, "uci", config
, "write"))
1343 return UBUS_STATUS_PERMISSION_DENIED
;
1348 return UBUS_STATUS_NO_DATA
;
1354 rpc_uci_apply(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1355 struct ubus_request_data
*req
, const char *method
,
1356 struct blob_attr
*msg
)
1358 struct blob_attr
*tb
[__RPC_T_MAX
];
1359 int timeout
= RPC_APPLY_TIMEOUT
;
1361 bool rollback
= false;
1366 blobmsg_parse(rpc_uci_apply_policy
, __RPC_T_MAX
, tb
,
1367 blob_data(msg
), blob_len(msg
));
1369 if (tb
[RPC_T_ROLLBACK
])
1370 rollback
= blobmsg_get_bool(tb
[RPC_T_ROLLBACK
]);
1372 if (apply_sid
[0] && rollback
)
1373 return UBUS_STATUS_PERMISSION_DENIED
;
1375 if (!tb
[RPC_T_SESSION
])
1376 return UBUS_STATUS_INVALID_ARGUMENT
;
1378 sid
= blobmsg_data(tb
[RPC_T_SESSION
]);
1380 if (tb
[RPC_T_TIMEOUT
])
1381 timeout
= blobmsg_get_u32(tb
[RPC_T_TIMEOUT
]);
1383 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES
);
1384 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA
);
1386 if (!apply_sid
[0]) {
1387 mkdir(RPC_SNAPSHOT_FILES
, 0700);
1388 mkdir(RPC_SNAPSHOT_DELTA
, 0700);
1390 snprintf(tmp
, sizeof(tmp
), RPC_UCI_SAVEDIR_PREFIX
"%s/*", sid
);
1391 if (glob(tmp
, GLOB_PERIOD
, NULL
, &gl
) < 0)
1392 return UBUS_STATUS_NOT_FOUND
;
1394 snprintf(tmp
, sizeof(tmp
), RPC_UCI_SAVEDIR_PREFIX
"%s/", sid
);
1396 ret
= rpc_uci_apply_access(sid
, &gl
);
1402 /* copy SID early because rpc_uci_apply_config() will clobber buf */
1404 strncpy(apply_sid
, sid
, RPC_SID_LEN
);
1406 for (i
= 0; i
< gl
.gl_pathc
; i
++) {
1407 char *config
= basename(gl
.gl_pathv
[i
]);
1413 if (stat(gl
.gl_pathv
[i
], &s
) || !s
.st_size
)
1416 rpc_uci_copy_file(RPC_UCI_DIR
, RPC_SNAPSHOT_FILES
, config
);
1417 rpc_uci_copy_file(tmp
, RPC_SNAPSHOT_DELTA
, config
);
1418 rpc_uci_apply_config(ctx
, config
);
1424 apply_timer
.cb
= rpc_uci_apply_timeout
;
1425 uloop_timeout_set(&apply_timer
, timeout
* 1000);
1434 rpc_uci_confirm(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1435 struct ubus_request_data
*req
, const char *method
,
1436 struct blob_attr
*msg
)
1438 struct blob_attr
*tb
[__RPC_B_MAX
];
1441 blobmsg_parse(rpc_uci_rollback_policy
, __RPC_B_MAX
, tb
,
1442 blob_data(msg
), blob_len(msg
));
1444 if (!tb
[RPC_B_SESSION
])
1445 return UBUS_STATUS_INVALID_ARGUMENT
;
1447 sid
= blobmsg_data(tb
[RPC_B_SESSION
]);
1450 return UBUS_STATUS_NO_DATA
;
1452 if (strcmp(apply_sid
, sid
))
1453 return UBUS_STATUS_PERMISSION_DENIED
;
1455 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES
);
1456 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA
);
1458 uloop_timeout_cancel(&apply_timer
);
1459 memset(apply_sid
, 0, sizeof(apply_sid
));
1466 rpc_uci_rollback(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1467 struct ubus_request_data
*req
, const char *method
,
1468 struct blob_attr
*msg
)
1470 struct blob_attr
*tb
[__RPC_B_MAX
];
1475 blobmsg_parse(rpc_uci_rollback_policy
, __RPC_B_MAX
, tb
,
1476 blob_data(msg
), blob_len(msg
));
1479 return UBUS_STATUS_NO_DATA
;
1481 if (!tb
[RPC_B_SESSION
])
1482 return UBUS_STATUS_INVALID_ARGUMENT
;
1484 sid
= blobmsg_data(tb
[RPC_B_SESSION
]);
1486 if (strcmp(apply_sid
, sid
))
1487 return UBUS_STATUS_PERMISSION_DENIED
;
1489 snprintf(tmp
, sizeof(tmp
), "%s/*", RPC_SNAPSHOT_FILES
);
1490 if (glob(tmp
, GLOB_PERIOD
, NULL
, &gl
) < 0)
1491 return UBUS_STATUS_NOT_FOUND
;
1493 rpc_uci_do_rollback(ctx
, sid
, &gl
);
1501 rpc_uci_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1502 struct ubus_request_data
*req
, const char *method
,
1503 struct blob_attr
*msg
)
1505 char * const cmd
[2] = { "/sbin/reload_config", NULL
};
1508 /* wait for the RPC call to complete */
1510 return execv(cmd
[0], cmd
);
1517 * Session destroy callback to purge associated delta directory.
1520 rpc_uci_purge_savedir_cb(struct rpc_session
*ses
, void *priv
)
1522 char path
[PATH_MAX
];
1524 snprintf(path
, sizeof(path
) - 1, RPC_UCI_SAVEDIR_PREFIX
"%s", ses
->id
);
1525 rpc_uci_purge_dir(path
);
1529 * Removes all delta directories which match the RPC_UCI_SAVEDIR_PREFIX.
1530 * This is used to clean up garbage when starting rpcd.
1532 void rpc_uci_purge_savedirs(void)
1537 if (!glob(RPC_UCI_SAVEDIR_PREFIX
"*", 0, NULL
, &gl
))
1539 for (i
= 0; i
< gl
.gl_pathc
; i
++)
1540 rpc_uci_purge_dir(gl
.gl_pathv
[i
]);
1546 int rpc_uci_api_init(struct ubus_context
*ctx
)
1548 static const struct ubus_method uci_methods
[] = {
1549 { .name
= "configs", .handler
= rpc_uci_configs
},
1550 UBUS_METHOD("get", rpc_uci_get
, rpc_uci_get_policy
),
1551 UBUS_METHOD("state", rpc_uci_state
, rpc_uci_get_policy
),
1552 UBUS_METHOD("add", rpc_uci_add
, rpc_uci_add_policy
),
1553 UBUS_METHOD("set", rpc_uci_set
, rpc_uci_set_policy
),
1554 UBUS_METHOD("delete", rpc_uci_delete
, rpc_uci_delete_policy
),
1555 UBUS_METHOD("rename", rpc_uci_rename
, rpc_uci_rename_policy
),
1556 UBUS_METHOD("order", rpc_uci_order
, rpc_uci_order_policy
),
1557 UBUS_METHOD("changes", rpc_uci_changes
, rpc_uci_config_policy
),
1558 UBUS_METHOD("revert", rpc_uci_revert
, rpc_uci_config_policy
),
1559 UBUS_METHOD("commit", rpc_uci_commit
, rpc_uci_config_policy
),
1560 UBUS_METHOD("apply", rpc_uci_apply
, rpc_uci_apply_policy
),
1561 UBUS_METHOD("confirm", rpc_uci_confirm
, rpc_uci_rollback_policy
),
1562 UBUS_METHOD("rollback", rpc_uci_rollback
, rpc_uci_rollback_policy
),
1563 UBUS_METHOD_NOARG("reload_config", rpc_uci_reload
),
1566 static struct ubus_object_type uci_type
=
1567 UBUS_OBJECT_TYPE("luci-rpc-uci", uci_methods
);
1569 static struct ubus_object obj
= {
1572 .methods
= uci_methods
,
1573 .n_methods
= ARRAY_SIZE(uci_methods
),
1576 static struct rpc_session_cb cb
= {
1577 .cb
= rpc_uci_purge_savedir_cb
1580 cursor
= uci_alloc_context();
1583 return UBUS_STATUS_UNKNOWN_ERROR
;
1585 rpc_session_destroy_cb(&cb
);
1587 return ubus_add_object(ctx
, &obj
);