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.
19 #include <libubox/blobmsg.h>
20 #include <libubox/blobmsg_json.h>
23 #include <rpcd/session.h>
25 static struct blob_buf buf
;
26 static struct uci_context
*cursor
;
38 static const struct blobmsg_policy rpc_uci_get_policy
[__RPC_G_MAX
] = {
39 [RPC_G_CONFIG
] = { .name
= "config", .type
= BLOBMSG_TYPE_STRING
},
40 [RPC_G_SECTION
] = { .name
= "section", .type
= BLOBMSG_TYPE_STRING
},
41 [RPC_G_OPTION
] = { .name
= "option", .type
= BLOBMSG_TYPE_STRING
},
42 [RPC_G_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
43 [RPC_G_MATCH
] = { .name
= "match", .type
= BLOBMSG_TYPE_TABLE
},
44 [RPC_G_SESSION
] = { .name
= "ubus_rpc_session",
45 .type
= BLOBMSG_TYPE_STRING
},
57 static const struct blobmsg_policy rpc_uci_add_policy
[__RPC_A_MAX
] = {
58 [RPC_A_CONFIG
] = { .name
= "config", .type
= BLOBMSG_TYPE_STRING
},
59 [RPC_A_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
60 [RPC_A_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
61 [RPC_A_VALUES
] = { .name
= "values", .type
= BLOBMSG_TYPE_TABLE
},
62 [RPC_A_SESSION
] = { .name
= "ubus_rpc_session",
63 .type
= BLOBMSG_TYPE_STRING
},
76 static const struct blobmsg_policy rpc_uci_set_policy
[__RPC_S_MAX
] = {
77 [RPC_S_CONFIG
] = { .name
= "config", .type
= BLOBMSG_TYPE_STRING
},
78 [RPC_S_SECTION
] = { .name
= "section", .type
= BLOBMSG_TYPE_STRING
},
79 [RPC_S_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
80 [RPC_S_MATCH
] = { .name
= "match", .type
= BLOBMSG_TYPE_TABLE
},
81 [RPC_S_VALUES
] = { .name
= "values", .type
= BLOBMSG_TYPE_TABLE
},
82 [RPC_S_SESSION
] = { .name
= "ubus_rpc_session",
83 .type
= BLOBMSG_TYPE_STRING
},
97 static const struct blobmsg_policy rpc_uci_delete_policy
[__RPC_D_MAX
] = {
98 [RPC_D_CONFIG
] = { .name
= "config", .type
= BLOBMSG_TYPE_STRING
},
99 [RPC_D_SECTION
] = { .name
= "section", .type
= BLOBMSG_TYPE_STRING
},
100 [RPC_D_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
101 [RPC_D_MATCH
] = { .name
= "match", .type
= BLOBMSG_TYPE_TABLE
},
102 [RPC_D_OPTION
] = { .name
= "option", .type
= BLOBMSG_TYPE_STRING
},
103 [RPC_D_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_ARRAY
},
104 [RPC_D_SESSION
] = { .name
= "ubus_rpc_session",
105 .type
= BLOBMSG_TYPE_STRING
},
117 static const struct blobmsg_policy rpc_uci_rename_policy
[__RPC_R_MAX
] = {
118 [RPC_R_CONFIG
] = { .name
= "config", .type
= BLOBMSG_TYPE_STRING
},
119 [RPC_R_SECTION
] = { .name
= "section", .type
= BLOBMSG_TYPE_STRING
},
120 [RPC_R_OPTION
] = { .name
= "option", .type
= BLOBMSG_TYPE_STRING
},
121 [RPC_R_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
122 [RPC_R_SESSION
] = { .name
= "ubus_rpc_session",
123 .type
= BLOBMSG_TYPE_STRING
},
133 static const struct blobmsg_policy rpc_uci_order_policy
[__RPC_O_MAX
] = {
134 [RPC_O_CONFIG
] = { .name
= "config", .type
= BLOBMSG_TYPE_STRING
},
135 [RPC_O_SECTIONS
] = { .name
= "sections", .type
= BLOBMSG_TYPE_ARRAY
},
136 [RPC_O_SESSION
] = { .name
= "ubus_rpc_session",
137 .type
= BLOBMSG_TYPE_STRING
},
146 static const struct blobmsg_policy rpc_uci_config_policy
[__RPC_C_MAX
] = {
147 [RPC_C_CONFIG
] = { .name
= "config", .type
= BLOBMSG_TYPE_STRING
},
148 [RPC_C_SESSION
] = { .name
= "ubus_rpc_session",
149 .type
= BLOBMSG_TYPE_STRING
},
153 * Turn uci error state into ubus return code
161 return UBUS_STATUS_OK
;
164 return UBUS_STATUS_INVALID_ARGUMENT
;
166 case UCI_ERR_NOTFOUND
:
167 return UBUS_STATUS_NOT_FOUND
;
170 return UBUS_STATUS_UNKNOWN_ERROR
;
175 * Setup per-session delta save directory. If the passed "sid" blob attribute
176 * pointer is NULL then the precedure was not invoked through the ubus-rpc so
177 * we do not perform session isolation and use the default save directory.
180 rpc_uci_set_savedir(struct blob_attr
*sid
)
186 uci_set_savedir(cursor
, "/tmp/.uci");
190 snprintf(path
, sizeof(path
) - 1,
191 RPC_UCI_SAVEDIR_PREFIX
"%s", blobmsg_get_string(sid
));
193 uci_set_savedir(cursor
, path
);
197 * Test read access to given config. If the passed "sid" blob attribute pointer
198 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
199 * perform access control and always assume true.
202 rpc_uci_read_access(struct blob_attr
*sid
, struct blob_attr
*config
)
204 rpc_uci_set_savedir(sid
);
209 return rpc_session_access(blobmsg_data(sid
), "uci",
210 blobmsg_data(config
), "read");
214 * Test write access to given config. If the passed "sid" blob attribute pointer
215 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
216 * perform access control and always assume true.
219 rpc_uci_write_access(struct blob_attr
*sid
, struct blob_attr
*config
)
221 rpc_uci_set_savedir(sid
);
226 return rpc_session_access(blobmsg_data(sid
), "uci",
227 blobmsg_data(config
), "write");
231 * Format applicable blob value as string and place a pointer to the string
232 * buffer in "p". Uses a static string buffer.
235 rpc_uci_format_blob(struct blob_attr
*v
, const char **p
)
241 switch (blobmsg_type(v
))
243 case BLOBMSG_TYPE_STRING
:
244 if (blobmsg_data_len(v
) > 1)
245 *p
= blobmsg_data(v
);
248 case BLOBMSG_TYPE_INT64
:
249 snprintf(buf
, sizeof(buf
), "%"PRIu64
, blobmsg_get_u64(v
));
253 case BLOBMSG_TYPE_INT32
:
254 snprintf(buf
, sizeof(buf
), "%u", blobmsg_get_u32(v
));
258 case BLOBMSG_TYPE_INT16
:
259 snprintf(buf
, sizeof(buf
), "%u", blobmsg_get_u16(v
));
263 case BLOBMSG_TYPE_INT8
:
264 snprintf(buf
, sizeof(buf
), "%u", !!blobmsg_get_u8(v
));
276 * Lookup the given uci_ptr and enable extended lookup format if the .section
277 * value of the uci_ptr looks like extended syntax. Uses an internal copy
278 * of the given uci_ptr to perform the lookup as failing extended section
279 * lookup operations in libuci will zero our the uci_ptr struct.
280 * Copies the internal uci_ptr back to given the uci_ptr on success.
283 rpc_uci_lookup(struct uci_ptr
*ptr
)
286 struct uci_ptr lookup
= *ptr
;
288 if (!lookup
.s
&& lookup
.section
&& *lookup
.section
== '@')
289 lookup
.flags
|= UCI_LOOKUP_EXTENDED
;
291 rv
= uci_lookup_ptr(cursor
, &lookup
, NULL
, true);
300 * Checks whether the given uci_option object matches the given string value.
301 * 1) If the uci_option is of type list, check whether any of the list elements
302 * equals to the given string
303 * 2) If the uci_option is of type string, parse it into space separated tokens
304 * and check if any of the tokens equals to the given string.
305 * Returns true if a list element or token matched the given string.
308 rpc_uci_match_option(struct uci_option
*o
, const char *cmp
)
310 struct uci_element
*e
;
313 if (o
->type
== UCI_TYPE_LIST
)
315 uci_foreach_element(&o
->v
.list
, e
)
316 if (e
->name
&& !strcmp(e
->name
, cmp
))
325 s
= strdup(o
->v
.string
);
330 for (p
= strtok(s
, " \t"); p
; p
= strtok(NULL
, " \t"))
344 * Checks whether the given uci_section matches the type and value blob attrs.
345 * 1) Returns false if "type" is given and the section type does not match
346 * the value specified in the "type" string blob attribute, else continue.
347 * 2) Tests whether any key in the "matches" table blob attribute exists in
348 * the given uci_section and whether each value is contained in the
349 * corresponding uci option value (see rpc_uci_match_option()).
350 * 3) A missing or empty "matches" table blob attribute is always considered
352 * Returns true if "type" matches or is NULL and "matches" matches or is NULL.
355 rpc_uci_match_section(struct uci_section
*s
,
356 struct blob_attr
*type
, struct blob_attr
*matches
)
358 struct uci_element
*e
;
359 struct blob_attr
*cur
;
365 if (type
&& strcmp(s
->type
, blobmsg_data(type
)))
371 blobmsg_for_each_attr(cur
, matches
, rem
)
373 if (!rpc_uci_format_blob(cur
, &cmp
))
376 uci_foreach_element(&s
->options
, e
)
378 if (strcmp(e
->name
, blobmsg_name(cur
)))
381 if (!rpc_uci_match_option(uci_to_option(e
), cmp
))
390 return (empty
|| match
);
394 * Dump the given uci_option value into the global blobmsg buffer and use
395 * given "name" as key.
396 * 1) If the uci_option is of type list, put a table into the blob buffer and
397 * add each list item as string to it.
398 * 2) If the uci_option is of type string, put its value directly into the blob
402 rpc_uci_dump_option(struct uci_option
*o
, const char *name
)
405 struct uci_element
*e
;
409 case UCI_TYPE_STRING
:
410 blobmsg_add_string(&buf
, name
, o
->v
.string
);
414 c
= blobmsg_open_array(&buf
, name
);
416 uci_foreach_element(&o
->v
.list
, e
)
417 blobmsg_add_string(&buf
, NULL
, e
->name
);
419 blobmsg_close_array(&buf
, c
);
428 * Dump the given uci_section object into the global blobmsg buffer and use
429 * given "name" as key.
430 * Puts a table into the blob buffer and puts each section option member value
431 * as value into the table using the option name as key.
432 * Adds three special keys ".anonymous", ".type" and ".name" which specify the
433 * corresponding section properties.
436 rpc_uci_dump_section(struct uci_section
*s
, const char *name
, int index
)
439 struct uci_option
*o
;
440 struct uci_element
*e
;
442 c
= blobmsg_open_table(&buf
, name
);
444 blobmsg_add_u8(&buf
, ".anonymous", s
->anonymous
);
445 blobmsg_add_string(&buf
, ".type", s
->type
);
446 blobmsg_add_string(&buf
, ".name", s
->e
.name
);
449 blobmsg_add_u32(&buf
, ".index", index
);
451 uci_foreach_element(&s
->options
, e
)
453 o
= uci_to_option(e
);
454 rpc_uci_dump_option(o
, o
->e
.name
);
457 blobmsg_close_table(&buf
, c
);
461 * Dump the given uci_package object into the global blobmsg buffer and use
462 * given "name" as key.
463 * Puts a table into the blob buffer and puts each package section member as
464 * value into the table using the section name as key.
465 * Only dumps sections matching the given "type" and "matches", see explaination
466 * of rpc_uci_match_section() for details.
469 rpc_uci_dump_package(struct uci_package
*p
, const char *name
,
470 struct blob_attr
*type
, struct blob_attr
*matches
)
473 struct uci_element
*e
;
476 c
= blobmsg_open_table(&buf
, name
);
478 uci_foreach_element(&p
->sections
, e
)
482 if (!rpc_uci_match_section(uci_to_section(e
), type
, matches
))
485 rpc_uci_dump_section(uci_to_section(e
), e
->name
, i
);
488 blobmsg_close_table(&buf
, c
);
493 rpc_uci_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
494 struct ubus_request_data
*req
, const char *method
,
495 struct blob_attr
*msg
)
497 struct blob_attr
*tb
[__RPC_G_MAX
];
498 struct uci_package
*p
= NULL
;
499 struct uci_ptr ptr
= { 0 };
501 blobmsg_parse(rpc_uci_get_policy
, __RPC_G_MAX
, tb
,
502 blob_data(msg
), blob_len(msg
));
504 if (!tb
[RPC_G_CONFIG
])
505 return UBUS_STATUS_INVALID_ARGUMENT
;
507 if (!rpc_uci_read_access(tb
[RPC_G_SESSION
], tb
[RPC_G_CONFIG
]))
508 return UBUS_STATUS_PERMISSION_DENIED
;
510 ptr
.package
= blobmsg_data(tb
[RPC_G_CONFIG
]);
511 uci_load(cursor
, ptr
.package
, &p
);
516 if (tb
[RPC_G_SECTION
])
518 ptr
.section
= blobmsg_data(tb
[RPC_G_SECTION
]);
520 if (tb
[RPC_G_OPTION
])
521 ptr
.option
= blobmsg_data(tb
[RPC_G_OPTION
]);
524 if (rpc_uci_lookup(&ptr
) || !(ptr
.flags
& UCI_LOOKUP_COMPLETE
))
527 blob_buf_init(&buf
, 0);
529 switch (ptr
.last
->type
)
531 case UCI_TYPE_PACKAGE
:
532 rpc_uci_dump_package(ptr
.p
, "values", tb
[RPC_G_TYPE
], tb
[RPC_G_MATCH
]);
535 case UCI_TYPE_SECTION
:
536 rpc_uci_dump_section(ptr
.s
, "values", -1);
539 case UCI_TYPE_OPTION
:
540 rpc_uci_dump_option(ptr
.o
, "value");
547 ubus_send_reply(ctx
, req
, buf
.head
);
551 uci_unload(cursor
, p
);
553 return rpc_uci_status();
557 rpc_uci_add(struct ubus_context
*ctx
, struct ubus_object
*obj
,
558 struct ubus_request_data
*req
, const char *method
,
559 struct blob_attr
*msg
)
561 struct blob_attr
*tb
[__RPC_A_MAX
];
562 struct blob_attr
*cur
, *elem
;
563 struct uci_package
*p
= NULL
;
564 struct uci_section
*s
;
565 struct uci_ptr ptr
= { 0 };
568 blobmsg_parse(rpc_uci_add_policy
, __RPC_A_MAX
, tb
,
569 blob_data(msg
), blob_len(msg
));
571 if (!tb
[RPC_A_CONFIG
] || !tb
[RPC_A_TYPE
])
572 return UBUS_STATUS_INVALID_ARGUMENT
;
574 if (!rpc_uci_write_access(tb
[RPC_A_SESSION
], tb
[RPC_A_CONFIG
]))
575 return UBUS_STATUS_PERMISSION_DENIED
;
577 ptr
.package
= blobmsg_data(tb
[RPC_A_CONFIG
]);
579 uci_load(cursor
, ptr
.package
, &p
);
584 /* add named section */
587 ptr
.section
= blobmsg_data(tb
[RPC_A_NAME
]);
588 ptr
.value
= blobmsg_data(tb
[RPC_A_TYPE
]);
591 if (rpc_uci_lookup(&ptr
) || uci_set(cursor
, &ptr
))
595 /* add anon section */
598 if (uci_add_section(cursor
, p
, blobmsg_data(tb
[RPC_A_TYPE
]), &s
) || !s
)
601 ptr
.section
= s
->e
.name
;
604 if (tb
[RPC_A_VALUES
])
606 blobmsg_for_each_attr(cur
, tb
[RPC_A_VALUES
], rem
)
609 ptr
.option
= blobmsg_name(cur
);
611 if (rpc_uci_lookup(&ptr
) || !ptr
.s
)
614 switch (blobmsg_type(cur
))
616 case BLOBMSG_TYPE_ARRAY
:
617 blobmsg_for_each_attr(elem
, cur
, rem2
)
618 if (rpc_uci_format_blob(elem
, &ptr
.value
))
619 uci_add_list(cursor
, &ptr
);
623 if (rpc_uci_format_blob(cur
, &ptr
.value
))
624 uci_set(cursor
, &ptr
);
632 blob_buf_init(&buf
, 0);
633 blobmsg_add_string(&buf
, "section", ptr
.section
);
634 ubus_send_reply(ctx
, req
, buf
.head
);
638 uci_unload(cursor
, p
);
640 return rpc_uci_status();
644 * Turn value from a blob attribute into uci set operation
645 * 1) if the blob is of type array, delete existing option (if any) and
646 * emit uci add_list operations for each element
647 * 2) if the blob is not an array but an option of type list exists,
648 * delete existing list and emit uci set operation for the blob value
649 * 3) in all other cases only emit a set operation if there is no existing
650 * option of if the existing options value differs from the blob value
653 rpc_uci_merge_set(struct blob_attr
*opt
, struct uci_ptr
*ptr
)
655 struct blob_attr
*cur
;
659 ptr
->option
= blobmsg_name(opt
);
661 if (rpc_uci_lookup(ptr
) || !ptr
->s
)
664 if (blobmsg_type(opt
) == BLOBMSG_TYPE_ARRAY
)
667 uci_delete(cursor
, ptr
);
669 blobmsg_for_each_attr(cur
, opt
, rem
)
670 if (rpc_uci_format_blob(cur
, &ptr
->value
))
671 uci_add_list(cursor
, ptr
);
673 else if (ptr
->o
&& ptr
->o
->type
== UCI_TYPE_LIST
)
675 uci_delete(cursor
, ptr
);
677 if (rpc_uci_format_blob(opt
, &ptr
->value
))
678 uci_set(cursor
, ptr
);
680 else if (rpc_uci_format_blob(opt
, &ptr
->value
))
682 if (!ptr
->o
|| !ptr
->o
->v
.string
|| strcmp(ptr
->o
->v
.string
, ptr
->value
))
683 uci_set(cursor
, ptr
);
688 rpc_uci_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
689 struct ubus_request_data
*req
, const char *method
,
690 struct blob_attr
*msg
)
692 struct blob_attr
*tb
[__RPC_S_MAX
];
693 struct blob_attr
*cur
;
694 struct uci_package
*p
= NULL
;
695 struct uci_element
*e
;
696 struct uci_ptr ptr
= { 0 };
699 blobmsg_parse(rpc_uci_set_policy
, __RPC_S_MAX
, tb
,
700 blob_data(msg
), blob_len(msg
));
702 if (!tb
[RPC_S_CONFIG
] || !tb
[RPC_S_VALUES
] ||
703 (!tb
[RPC_S_SECTION
] && !tb
[RPC_S_TYPE
] && !tb
[RPC_S_MATCH
]))
704 return UBUS_STATUS_INVALID_ARGUMENT
;
706 if (!rpc_uci_write_access(tb
[RPC_S_SESSION
], tb
[RPC_S_CONFIG
]))
707 return UBUS_STATUS_PERMISSION_DENIED
;
709 ptr
.package
= blobmsg_data(tb
[RPC_S_CONFIG
]);
710 uci_load(cursor
, ptr
.package
, &p
);
715 if (tb
[RPC_S_SECTION
])
717 ptr
.section
= blobmsg_data(tb
[RPC_S_SECTION
]);
718 blobmsg_for_each_attr(cur
, tb
[RPC_S_VALUES
], rem
)
719 rpc_uci_merge_set(cur
, &ptr
);
723 uci_foreach_element(&p
->sections
, e
)
725 if (!rpc_uci_match_section(uci_to_section(e
),
726 tb
[RPC_S_TYPE
], tb
[RPC_S_MATCH
]))
730 ptr
.section
= e
->name
;
732 blobmsg_for_each_attr(cur
, tb
[RPC_S_VALUES
], rem
)
733 rpc_uci_merge_set(cur
, &ptr
);
741 uci_unload(cursor
, p
);
743 return rpc_uci_status();
747 * Delete option or section from uci specified by given blob attribute pointer
748 * 1) if the blob is of type array, delete any option named after each element
749 * 2) if the blob is of type string, delete the option named after its value
750 * 3) if the blob is NULL, delete entire section
753 rpc_uci_merge_delete(struct blob_attr
*opt
, struct uci_ptr
*ptr
)
755 struct blob_attr
*cur
;
758 if (rpc_uci_lookup(ptr
) || !ptr
->s
)
766 uci_delete(cursor
, ptr
);
768 else if (blobmsg_type(opt
) == BLOBMSG_TYPE_ARRAY
)
770 blobmsg_for_each_attr(cur
, opt
, rem
)
772 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
776 ptr
->option
= blobmsg_data(cur
);
778 if (rpc_uci_lookup(ptr
) || !ptr
->o
)
781 uci_delete(cursor
, ptr
);
784 else if (blobmsg_type(opt
) == BLOBMSG_TYPE_STRING
)
787 ptr
->option
= blobmsg_data(opt
);
789 if (rpc_uci_lookup(ptr
) || !ptr
->o
)
792 uci_delete(cursor
, ptr
);
797 rpc_uci_delete(struct ubus_context
*ctx
, struct ubus_object
*obj
,
798 struct ubus_request_data
*req
, const char *method
,
799 struct blob_attr
*msg
)
801 struct blob_attr
*tb
[__RPC_D_MAX
];
802 struct uci_package
*p
= NULL
;
803 struct uci_element
*e
, *tmp
;
804 struct uci_ptr ptr
= { 0 };
806 blobmsg_parse(rpc_uci_delete_policy
, __RPC_D_MAX
, tb
,
807 blob_data(msg
), blob_len(msg
));
809 if (!tb
[RPC_D_CONFIG
] ||
810 (!tb
[RPC_D_SECTION
] && !tb
[RPC_D_TYPE
] && !tb
[RPC_D_MATCH
]))
811 return UBUS_STATUS_INVALID_ARGUMENT
;
813 if (!rpc_uci_write_access(tb
[RPC_D_SESSION
], tb
[RPC_D_CONFIG
]))
814 return UBUS_STATUS_PERMISSION_DENIED
;
816 ptr
.package
= blobmsg_data(tb
[RPC_D_CONFIG
]);
817 uci_load(cursor
, ptr
.package
, &p
);
822 if (tb
[RPC_D_SECTION
])
824 ptr
.section
= blobmsg_data(tb
[RPC_D_SECTION
]);
826 if (tb
[RPC_D_OPTIONS
])
827 rpc_uci_merge_delete(tb
[RPC_D_OPTIONS
], &ptr
);
829 rpc_uci_merge_delete(tb
[RPC_D_OPTION
], &ptr
);
833 uci_foreach_element_safe(&p
->sections
, tmp
, e
)
835 if (!rpc_uci_match_section(uci_to_section(e
),
836 tb
[RPC_D_TYPE
], tb
[RPC_D_MATCH
]))
840 ptr
.section
= e
->name
;
842 if (tb
[RPC_D_OPTIONS
])
843 rpc_uci_merge_delete(tb
[RPC_D_OPTIONS
], &ptr
);
845 rpc_uci_merge_delete(tb
[RPC_D_OPTION
], &ptr
);
853 uci_unload(cursor
, p
);
855 return rpc_uci_status();
859 rpc_uci_rename(struct ubus_context
*ctx
, struct ubus_object
*obj
,
860 struct ubus_request_data
*req
, const char *method
,
861 struct blob_attr
*msg
)
863 struct blob_attr
*tb
[__RPC_R_MAX
];
864 struct uci_package
*p
= NULL
;
865 struct uci_ptr ptr
= { 0 };
867 blobmsg_parse(rpc_uci_rename_policy
, __RPC_R_MAX
, tb
,
868 blob_data(msg
), blob_len(msg
));
870 if (!tb
[RPC_R_CONFIG
] || !tb
[RPC_R_SECTION
] || !tb
[RPC_R_NAME
])
871 return UBUS_STATUS_INVALID_ARGUMENT
;
873 if (!rpc_uci_write_access(tb
[RPC_R_SESSION
], tb
[RPC_R_CONFIG
]))
874 return UBUS_STATUS_PERMISSION_DENIED
;
876 ptr
.package
= blobmsg_data(tb
[RPC_R_CONFIG
]);
877 ptr
.section
= blobmsg_data(tb
[RPC_R_SECTION
]);
878 ptr
.value
= blobmsg_data(tb
[RPC_R_NAME
]);
880 if (tb
[RPC_R_OPTION
])
881 ptr
.option
= blobmsg_data(tb
[RPC_R_OPTION
]);
883 uci_load(cursor
, ptr
.package
, &p
);
888 if (uci_lookup_ptr(cursor
, &ptr
, NULL
, true))
891 if ((ptr
.option
&& !ptr
.o
) || !ptr
.s
)
893 cursor
->err
= UCI_ERR_NOTFOUND
;
897 if (uci_rename(cursor
, &ptr
))
904 uci_unload(cursor
, p
);
906 return rpc_uci_status();
910 rpc_uci_order(struct ubus_context
*ctx
, struct ubus_object
*obj
,
911 struct ubus_request_data
*req
, const char *method
,
912 struct blob_attr
*msg
)
914 struct blob_attr
*tb
[__RPC_O_MAX
];
915 struct blob_attr
*cur
;
916 struct uci_package
*p
= NULL
;
917 struct uci_ptr ptr
= { 0 };
920 blobmsg_parse(rpc_uci_order_policy
, __RPC_O_MAX
, tb
,
921 blob_data(msg
), blob_len(msg
));
923 if (!tb
[RPC_O_CONFIG
] || !tb
[RPC_O_SECTIONS
])
924 return UBUS_STATUS_INVALID_ARGUMENT
;
926 if (!rpc_uci_write_access(tb
[RPC_O_SESSION
], tb
[RPC_O_CONFIG
]))
927 return UBUS_STATUS_PERMISSION_DENIED
;
929 ptr
.package
= blobmsg_data(tb
[RPC_O_CONFIG
]);
931 uci_load(cursor
, ptr
.package
, &p
);
936 blobmsg_for_each_attr(cur
, tb
[RPC_O_SECTIONS
], rem
)
938 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
942 ptr
.section
= blobmsg_data(cur
);
944 if (uci_lookup_ptr(cursor
, &ptr
, NULL
, true) || !ptr
.s
)
947 uci_reorder_section(cursor
, ptr
.s
, i
++);
954 uci_unload(cursor
, p
);
956 return rpc_uci_status();
960 rpc_uci_dump_change(struct uci_delta
*d
)
963 const char *types
[] = {
964 [UCI_CMD_REORDER
] = "order",
965 [UCI_CMD_REMOVE
] = "remove",
966 [UCI_CMD_RENAME
] = "rename",
967 [UCI_CMD_ADD
] = "add",
968 [UCI_CMD_LIST_ADD
] = "list-add",
969 [UCI_CMD_LIST_DEL
] = "list-del",
970 [UCI_CMD_CHANGE
] = "set",
976 c
= blobmsg_open_array(&buf
, NULL
);
978 blobmsg_add_string(&buf
, NULL
, types
[d
->cmd
]);
979 blobmsg_add_string(&buf
, NULL
, d
->section
);
982 blobmsg_add_string(&buf
, NULL
, d
->e
.name
);
986 if (d
->cmd
== UCI_CMD_REORDER
)
987 blobmsg_add_u32(&buf
, NULL
, strtoul(d
->value
, NULL
, 10));
989 blobmsg_add_string(&buf
, NULL
, d
->value
);
992 blobmsg_close_array(&buf
, c
);
996 rpc_uci_changes(struct ubus_context
*ctx
, struct ubus_object
*obj
,
997 struct ubus_request_data
*req
, const char *method
,
998 struct blob_attr
*msg
)
1000 struct blob_attr
*tb
[__RPC_C_MAX
];
1001 struct uci_package
*p
= NULL
;
1002 struct uci_element
*e
;
1005 blobmsg_parse(rpc_uci_config_policy
, __RPC_C_MAX
, tb
,
1006 blob_data(msg
), blob_len(msg
));
1008 if (!tb
[RPC_C_CONFIG
])
1009 return UBUS_STATUS_INVALID_ARGUMENT
;
1011 if (!rpc_uci_read_access(tb
[RPC_C_SESSION
], tb
[RPC_C_CONFIG
]))
1012 return UBUS_STATUS_PERMISSION_DENIED
;
1014 uci_load(cursor
, blobmsg_data(tb
[RPC_C_CONFIG
]), &p
);
1019 blob_buf_init(&buf
, 0);
1020 c
= blobmsg_open_array(&buf
, "changes");
1022 uci_foreach_element(&p
->saved_delta
, e
)
1023 rpc_uci_dump_change(uci_to_delta(e
));
1025 blobmsg_close_array(&buf
, c
);
1027 ubus_send_reply(ctx
, req
, buf
.head
);
1031 uci_unload(cursor
, p
);
1033 return rpc_uci_status();
1037 rpc_uci_trigger_event(struct ubus_context
*ctx
, const char *config
)
1039 char *pkg
= strdup(config
);
1040 static struct blob_buf b
;
1043 if (!ubus_lookup_id(ctx
, "service", &id
)) {
1046 blob_buf_init(&b
, 0);
1047 blobmsg_add_string(&b
, "type", "config.change");
1048 c
= blobmsg_open_table(&b
, "data");
1049 blobmsg_add_string(&b
, "package", pkg
);
1050 blobmsg_close_table(&b
, c
);
1051 ubus_invoke(ctx
, id
, "event", b
.head
, NULL
, 0, 1000);
1057 rpc_uci_revert_commit(struct ubus_context
*ctx
, struct blob_attr
*msg
, bool commit
)
1059 struct blob_attr
*tb
[__RPC_C_MAX
];
1060 struct uci_package
*p
= NULL
;
1061 struct uci_ptr ptr
= { 0 };
1063 blobmsg_parse(rpc_uci_config_policy
, __RPC_C_MAX
, tb
,
1064 blob_data(msg
), blob_len(msg
));
1066 if (!tb
[RPC_C_CONFIG
])
1067 return UBUS_STATUS_INVALID_ARGUMENT
;
1069 if (!rpc_uci_write_access(tb
[RPC_C_SESSION
], tb
[RPC_C_CONFIG
]))
1070 return UBUS_STATUS_PERMISSION_DENIED
;
1072 ptr
.package
= blobmsg_data(tb
[RPC_C_CONFIG
]);
1076 uci_load(cursor
, ptr
.package
, &p
);
1080 uci_commit(cursor
, &p
, false);
1081 uci_unload(cursor
, p
);
1083 rpc_uci_trigger_event(ctx
, blobmsg_get_string(tb
[RPC_C_CONFIG
]));
1087 if (!uci_lookup_ptr(cursor
, &ptr
, NULL
, true) && ptr
.p
)
1088 uci_revert(cursor
, &ptr
);
1091 return rpc_uci_status();
1095 rpc_uci_revert(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1096 struct ubus_request_data
*req
, const char *method
,
1097 struct blob_attr
*msg
)
1099 return rpc_uci_revert_commit(ctx
, msg
, false);
1103 rpc_uci_commit(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1104 struct ubus_request_data
*req
, const char *method
,
1105 struct blob_attr
*msg
)
1107 return rpc_uci_revert_commit(ctx
, msg
, true);
1111 rpc_uci_configs(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1112 struct ubus_request_data
*req
, const char *method
,
1113 struct blob_attr
*msg
)
1119 if (uci_list_configs(cursor
, &configs
))
1122 blob_buf_init(&buf
, 0);
1124 c
= blobmsg_open_array(&buf
, "configs");
1126 for (i
= 0; configs
[i
]; i
++)
1127 blobmsg_add_string(&buf
, NULL
, configs
[i
]);
1129 blobmsg_close_array(&buf
, c
);
1131 ubus_send_reply(ctx
, req
, buf
.head
);
1134 return rpc_uci_status();
1139 * Remove given delta save directory (if any).
1142 rpc_uci_purge_dir(const char *path
)
1147 char file
[PATH_MAX
];
1149 if (stat(path
, &s
) || !S_ISDIR(s
.st_mode
))
1152 if ((d
= opendir(path
)) != NULL
)
1154 while ((e
= readdir(d
)) != NULL
)
1156 snprintf(file
, sizeof(file
) - 1, "%s/%s", path
, e
->d_name
);
1158 if (stat(file
, &s
) || !S_ISREG(s
.st_mode
))
1171 * Session destroy callback to purge associated delta directory.
1174 rpc_uci_purge_savedir_cb(struct rpc_session
*ses
, void *priv
)
1176 char path
[PATH_MAX
];
1178 snprintf(path
, sizeof(path
) - 1, RPC_UCI_SAVEDIR_PREFIX
"%s", ses
->id
);
1179 rpc_uci_purge_dir(path
);
1183 * Removes all delta directories which match the RPC_UCI_SAVEDIR_PREFIX.
1184 * This is used to clean up garbage when starting rpcd.
1186 void rpc_uci_purge_savedirs(void)
1191 if (!glob(RPC_UCI_SAVEDIR_PREFIX
"*", 0, NULL
, &gl
))
1193 for (i
= 0; i
< gl
.gl_pathc
; i
++)
1194 rpc_uci_purge_dir(gl
.gl_pathv
[i
]);
1200 int rpc_uci_api_init(struct ubus_context
*ctx
)
1202 static const struct ubus_method uci_methods
[] = {
1203 { .name
= "configs", .handler
= rpc_uci_configs
},
1204 UBUS_METHOD("get", rpc_uci_get
, rpc_uci_get_policy
),
1205 UBUS_METHOD("add", rpc_uci_add
, rpc_uci_add_policy
),
1206 UBUS_METHOD("set", rpc_uci_set
, rpc_uci_set_policy
),
1207 UBUS_METHOD("delete", rpc_uci_delete
, rpc_uci_delete_policy
),
1208 UBUS_METHOD("rename", rpc_uci_rename
, rpc_uci_rename_policy
),
1209 UBUS_METHOD("order", rpc_uci_order
, rpc_uci_order_policy
),
1210 UBUS_METHOD("changes", rpc_uci_changes
, rpc_uci_config_policy
),
1211 UBUS_METHOD("revert", rpc_uci_revert
, rpc_uci_config_policy
),
1212 UBUS_METHOD("commit", rpc_uci_commit
, rpc_uci_config_policy
),
1215 static struct ubus_object_type uci_type
=
1216 UBUS_OBJECT_TYPE("luci-rpc-uci", uci_methods
);
1218 static struct ubus_object obj
= {
1221 .methods
= uci_methods
,
1222 .n_methods
= ARRAY_SIZE(uci_methods
),
1225 static struct rpc_session_cb cb
= {
1226 .cb
= rpc_uci_purge_savedir_cb
1229 cursor
= uci_alloc_context();
1232 return UBUS_STATUS_UNKNOWN_ERROR
;
1234 rpc_session_destroy_cb(&cb
);
1236 return ubus_add_object(ctx
, &obj
);