2 * rpcd - UBUS RPC server
4 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <libubox/blobmsg.h>
23 #include <libubox/blobmsg_json.h>
26 #include <rpcd/exec.h>
27 #include <rpcd/session.h>
29 static struct blob_buf buf
;
30 static struct uci_context
*cursor
;
31 static struct uloop_timeout apply_timer
;
32 static struct ubus_context
*apply_ctx
;
34 char apply_sid
[RPC_SID_LEN
+ 1];
46 static const struct blobmsg_policy rpc_uci_get_policy
[__RPC_G_MAX
] = {
47 [RPC_G_CONFIG
] = { .name
= "config", .type
= BLOBMSG_TYPE_STRING
},
48 [RPC_G_SECTION
] = { .name
= "section", .type
= BLOBMSG_TYPE_STRING
},
49 [RPC_G_OPTION
] = { .name
= "option", .type
= BLOBMSG_TYPE_STRING
},
50 [RPC_G_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
51 [RPC_G_MATCH
] = { .name
= "match", .type
= BLOBMSG_TYPE_TABLE
},
52 [RPC_G_SESSION
] = { .name
= "ubus_rpc_session",
53 .type
= BLOBMSG_TYPE_STRING
},
65 static const struct blobmsg_policy rpc_uci_add_policy
[__RPC_A_MAX
] = {
66 [RPC_A_CONFIG
] = { .name
= "config", .type
= BLOBMSG_TYPE_STRING
},
67 [RPC_A_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
68 [RPC_A_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
69 [RPC_A_VALUES
] = { .name
= "values", .type
= BLOBMSG_TYPE_TABLE
},
70 [RPC_A_SESSION
] = { .name
= "ubus_rpc_session",
71 .type
= BLOBMSG_TYPE_STRING
},
84 static const struct blobmsg_policy rpc_uci_set_policy
[__RPC_S_MAX
] = {
85 [RPC_S_CONFIG
] = { .name
= "config", .type
= BLOBMSG_TYPE_STRING
},
86 [RPC_S_SECTION
] = { .name
= "section", .type
= BLOBMSG_TYPE_STRING
},
87 [RPC_S_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
88 [RPC_S_MATCH
] = { .name
= "match", .type
= BLOBMSG_TYPE_TABLE
},
89 [RPC_S_VALUES
] = { .name
= "values", .type
= BLOBMSG_TYPE_TABLE
},
90 [RPC_S_SESSION
] = { .name
= "ubus_rpc_session",
91 .type
= BLOBMSG_TYPE_STRING
},
105 static const struct blobmsg_policy rpc_uci_delete_policy
[__RPC_D_MAX
] = {
106 [RPC_D_CONFIG
] = { .name
= "config", .type
= BLOBMSG_TYPE_STRING
},
107 [RPC_D_SECTION
] = { .name
= "section", .type
= BLOBMSG_TYPE_STRING
},
108 [RPC_D_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
109 [RPC_D_MATCH
] = { .name
= "match", .type
= BLOBMSG_TYPE_TABLE
},
110 [RPC_D_OPTION
] = { .name
= "option", .type
= BLOBMSG_TYPE_STRING
},
111 [RPC_D_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_ARRAY
},
112 [RPC_D_SESSION
] = { .name
= "ubus_rpc_session",
113 .type
= BLOBMSG_TYPE_STRING
},
125 static const struct blobmsg_policy rpc_uci_rename_policy
[__RPC_R_MAX
] = {
126 [RPC_R_CONFIG
] = { .name
= "config", .type
= BLOBMSG_TYPE_STRING
},
127 [RPC_R_SECTION
] = { .name
= "section", .type
= BLOBMSG_TYPE_STRING
},
128 [RPC_R_OPTION
] = { .name
= "option", .type
= BLOBMSG_TYPE_STRING
},
129 [RPC_R_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
130 [RPC_R_SESSION
] = { .name
= "ubus_rpc_session",
131 .type
= BLOBMSG_TYPE_STRING
},
141 static const struct blobmsg_policy rpc_uci_order_policy
[__RPC_O_MAX
] = {
142 [RPC_O_CONFIG
] = { .name
= "config", .type
= BLOBMSG_TYPE_STRING
},
143 [RPC_O_SECTIONS
] = { .name
= "sections", .type
= BLOBMSG_TYPE_ARRAY
},
144 [RPC_O_SESSION
] = { .name
= "ubus_rpc_session",
145 .type
= BLOBMSG_TYPE_STRING
},
154 static const struct blobmsg_policy rpc_uci_config_policy
[__RPC_C_MAX
] = {
155 [RPC_C_CONFIG
] = { .name
= "config", .type
= BLOBMSG_TYPE_STRING
},
156 [RPC_C_SESSION
] = { .name
= "ubus_rpc_session",
157 .type
= BLOBMSG_TYPE_STRING
},
167 static const struct blobmsg_policy rpc_uci_apply_policy
[__RPC_T_MAX
] = {
168 [RPC_T_ROLLBACK
] = { .name
= "rollback", .type
= BLOBMSG_TYPE_BOOL
},
169 [RPC_T_TIMEOUT
] = { .name
= "timeout", .type
= BLOBMSG_TYPE_INT32
},
170 [RPC_T_SESSION
] = { .name
= "ubus_rpc_session",
171 .type
= BLOBMSG_TYPE_STRING
},
179 static const struct blobmsg_policy rpc_uci_rollback_policy
[__RPC_B_MAX
] = {
180 [RPC_B_SESSION
] = { .name
= "ubus_rpc_session",
181 .type
= BLOBMSG_TYPE_STRING
},
185 * Validate a uci name
188 rpc_uci_verify_str(const char *name
, bool extended
, bool type
)
196 if (extended
&& *name
!= '@')
199 for (c
= name
+ extended
; *c
; c
++)
200 if (!isalnum(*c
) && *c
!= '_' && ((!type
&& !extended
) || *c
!= '-'))
209 return (e
> c
&& *e
== ']' && *(e
+1) == 0);
216 * Check that string is a valid, shell compatible uci name
218 static bool rpc_uci_verify_name(const char *name
) {
219 return rpc_uci_verify_str(name
, false, false);
223 * Check that string is a valid section type name
225 static bool rpc_uci_verify_type(const char *type
) {
226 return rpc_uci_verify_str(type
, false, true);
230 * Check that the string is a valid section id, optionally in extended
233 static bool rpc_uci_verify_section(const char *section
) {
234 return rpc_uci_verify_str(section
, true, false);
239 * Turn uci error state into ubus return code
247 return UBUS_STATUS_OK
;
250 return UBUS_STATUS_INVALID_ARGUMENT
;
252 case UCI_ERR_NOTFOUND
:
253 return UBUS_STATUS_NOT_FOUND
;
256 return UBUS_STATUS_UNKNOWN_ERROR
;
261 * Clear all save directories from the uci cursor and append the given path
262 * as new save directory.
265 rpc_uci_replace_savedir(const char *path
)
267 struct uci_element
*e
, *tmp
;
269 uci_foreach_element_safe(&cursor
->delta_path
, tmp
, e
) {
276 cursor
->delta_path
.prev
= &cursor
->delta_path
;
277 cursor
->delta_path
.next
= &cursor
->delta_path
;
280 uci_set_savedir(cursor
, path
);
284 * Setup per-session delta save directory. If the passed "sid" blob attribute
285 * pointer is NULL then the precedure was not invoked through the ubus-rpc so
286 * we do not perform session isolation and use the default save directory.
289 rpc_uci_set_savedir(struct blob_attr
*sid
)
295 rpc_uci_replace_savedir("/tmp/.uci");
299 snprintf(path
, sizeof(path
) - 1,
300 RPC_UCI_SAVEDIR_PREFIX
"%s", blobmsg_get_string(sid
));
302 rpc_uci_replace_savedir(path
);
306 * Test read access to given config. If the passed "sid" blob attribute pointer
307 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
308 * perform access control and always assume true.
311 rpc_uci_read_access(struct blob_attr
*sid
, struct blob_attr
*config
)
313 rpc_uci_set_savedir(sid
);
318 return rpc_session_access(blobmsg_data(sid
), "uci",
319 blobmsg_data(config
), "read");
323 * Test write access to given config. If the passed "sid" blob attribute pointer
324 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
325 * perform access control and always assume true.
328 rpc_uci_write_access(struct blob_attr
*sid
, struct blob_attr
*config
)
330 rpc_uci_set_savedir(sid
);
335 return rpc_session_access(blobmsg_data(sid
), "uci",
336 blobmsg_data(config
), "write");
340 * Format applicable blob value as string and place a pointer to the string
341 * buffer in "p". Uses a static string buffer.
344 rpc_uci_format_blob(struct blob_attr
*v
, const char **p
)
350 switch (blobmsg_type(v
))
352 case BLOBMSG_TYPE_STRING
:
353 *p
= blobmsg_data(v
);
356 case BLOBMSG_TYPE_INT64
:
357 snprintf(buf
, sizeof(buf
), "%"PRIu64
, blobmsg_get_u64(v
));
361 case BLOBMSG_TYPE_INT32
:
362 snprintf(buf
, sizeof(buf
), "%u", blobmsg_get_u32(v
));
366 case BLOBMSG_TYPE_INT16
:
367 snprintf(buf
, sizeof(buf
), "%u", blobmsg_get_u16(v
));
371 case BLOBMSG_TYPE_INT8
:
372 snprintf(buf
, sizeof(buf
), "%u", !!blobmsg_get_u8(v
));
384 * Lookup the given uci_ptr and enable extended lookup format if the .section
385 * value of the uci_ptr looks like extended syntax. Uses an internal copy
386 * of the given uci_ptr to perform the lookup as failing extended section
387 * lookup operations in libuci will zero our the uci_ptr struct.
388 * Copies the internal uci_ptr back to given the uci_ptr on success.
391 rpc_uci_lookup(struct uci_ptr
*ptr
)
394 struct uci_ptr lookup
= *ptr
;
396 if (!lookup
.s
&& lookup
.section
&& *lookup
.section
== '@')
397 lookup
.flags
|= UCI_LOOKUP_EXTENDED
;
399 rv
= uci_lookup_ptr(cursor
, &lookup
, NULL
, true);
408 * Checks whether the given uci_option object matches the given string value.
409 * 1) If the uci_option is of type list, check whether any of the list elements
410 * equals to the given string
411 * 2) If the uci_option is of type string, parse it into space separated tokens
412 * and check if any of the tokens equals to the given string.
413 * Returns true if a list element or token matched the given string.
416 rpc_uci_match_option(struct uci_option
*o
, const char *cmp
)
418 struct uci_element
*e
;
421 if (o
->type
== UCI_TYPE_LIST
)
423 uci_foreach_element(&o
->v
.list
, e
)
424 if (e
->name
&& !strcmp(e
->name
, cmp
))
433 s
= strdup(o
->v
.string
);
438 for (p
= strtok(s
, " \t"); p
; p
= strtok(NULL
, " \t"))
452 * Checks whether the given uci_section matches the type and value blob attrs.
453 * 1) Returns false if "type" is given and the section type does not match
454 * the value specified in the "type" string blob attribute, else continue.
455 * 2) Tests whether any key in the "matches" table blob attribute exists in
456 * the given uci_section and whether each value is contained in the
457 * corresponding uci option value (see rpc_uci_match_option()).
458 * 3) A missing or empty "matches" table blob attribute is always considered
460 * Returns true if "type" matches or is NULL and "matches" matches or is NULL.
463 rpc_uci_match_section(struct uci_section
*s
,
464 struct blob_attr
*type
, struct blob_attr
*matches
)
466 struct uci_element
*e
;
467 struct blob_attr
*cur
;
473 if (type
&& strcmp(s
->type
, blobmsg_data(type
)))
479 blobmsg_for_each_attr(cur
, matches
, rem
)
481 if (!rpc_uci_format_blob(cur
, &cmp
))
484 uci_foreach_element(&s
->options
, e
)
486 if (strcmp(e
->name
, blobmsg_name(cur
)))
489 if (!rpc_uci_match_option(uci_to_option(e
), cmp
))
498 return (empty
|| match
);
502 * Dump the given uci_option value into the global blobmsg buffer and use
503 * given "name" as key.
504 * 1) If the uci_option is of type list, put a table into the blob buffer and
505 * add each list item as string to it.
506 * 2) If the uci_option is of type string, put its value directly into the blob
510 rpc_uci_dump_option(struct uci_option
*o
, const char *name
)
513 struct uci_element
*e
;
517 case UCI_TYPE_STRING
:
518 blobmsg_add_string(&buf
, name
, o
->v
.string
);
522 c
= blobmsg_open_array(&buf
, name
);
524 uci_foreach_element(&o
->v
.list
, e
)
525 blobmsg_add_string(&buf
, NULL
, e
->name
);
527 blobmsg_close_array(&buf
, c
);
536 * Dump the given uci_section object into the global blobmsg buffer and use
537 * given "name" as key.
538 * Puts a table into the blob buffer and puts each section option member value
539 * as value into the table using the option name as key.
540 * Adds three special keys ".anonymous", ".type" and ".name" which specify the
541 * corresponding section properties.
544 rpc_uci_dump_section(struct uci_section
*s
, const char *name
, int index
)
547 struct uci_option
*o
;
548 struct uci_element
*e
;
550 c
= blobmsg_open_table(&buf
, name
);
552 blobmsg_add_u8(&buf
, ".anonymous", s
->anonymous
);
553 blobmsg_add_string(&buf
, ".type", s
->type
);
554 blobmsg_add_string(&buf
, ".name", s
->e
.name
);
557 blobmsg_add_u32(&buf
, ".index", index
);
559 uci_foreach_element(&s
->options
, e
)
561 o
= uci_to_option(e
);
562 rpc_uci_dump_option(o
, o
->e
.name
);
565 blobmsg_close_table(&buf
, c
);
569 * Dump the given uci_package object into the global blobmsg buffer and use
570 * given "name" as key.
571 * Puts a table into the blob buffer and puts each package section member as
572 * value into the table using the section name as key.
573 * Only dumps sections matching the given "type" and "matches", see explaination
574 * of rpc_uci_match_section() for details.
577 rpc_uci_dump_package(struct uci_package
*p
, const char *name
,
578 struct blob_attr
*type
, struct blob_attr
*matches
)
581 struct uci_element
*e
;
584 c
= blobmsg_open_table(&buf
, name
);
586 uci_foreach_element(&p
->sections
, e
)
590 if (!rpc_uci_match_section(uci_to_section(e
), type
, matches
))
593 rpc_uci_dump_section(uci_to_section(e
), e
->name
, i
);
596 blobmsg_close_table(&buf
, c
);
601 rpc_uci_getcommon(struct ubus_context
*ctx
, struct ubus_request_data
*req
,
602 struct blob_attr
*msg
, bool use_state
)
604 struct blob_attr
*tb
[__RPC_G_MAX
];
605 struct uci_package
*p
= NULL
;
606 struct uci_ptr ptr
= { 0 };
608 blobmsg_parse(rpc_uci_get_policy
, __RPC_G_MAX
, tb
,
609 blob_data(msg
), blob_len(msg
));
611 if (!tb
[RPC_G_CONFIG
])
612 return UBUS_STATUS_INVALID_ARGUMENT
;
614 if (!rpc_uci_read_access(tb
[RPC_G_SESSION
], tb
[RPC_G_CONFIG
]))
615 return UBUS_STATUS_PERMISSION_DENIED
;
617 ptr
.package
= blobmsg_data(tb
[RPC_G_CONFIG
]);
620 uci_set_savedir(cursor
, "/var/state");
622 if (uci_load(cursor
, ptr
.package
, &p
))
623 return rpc_uci_status();
625 if (tb
[RPC_G_SECTION
])
627 ptr
.section
= blobmsg_data(tb
[RPC_G_SECTION
]);
629 if (tb
[RPC_G_OPTION
])
630 ptr
.option
= blobmsg_data(tb
[RPC_G_OPTION
]);
633 if (rpc_uci_lookup(&ptr
) || !(ptr
.flags
& UCI_LOOKUP_COMPLETE
))
636 blob_buf_init(&buf
, 0);
638 switch (ptr
.last
->type
)
640 case UCI_TYPE_PACKAGE
:
641 rpc_uci_dump_package(ptr
.p
, "values", tb
[RPC_G_TYPE
], tb
[RPC_G_MATCH
]);
644 case UCI_TYPE_SECTION
:
645 rpc_uci_dump_section(ptr
.s
, "values", -1);
648 case UCI_TYPE_OPTION
:
649 rpc_uci_dump_option(ptr
.o
, "value");
656 ubus_send_reply(ctx
, req
, buf
.head
);
659 uci_unload(cursor
, p
);
661 return rpc_uci_status();
665 rpc_uci_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
666 struct ubus_request_data
*req
, const char *method
,
667 struct blob_attr
*msg
)
669 return rpc_uci_getcommon(ctx
, req
, msg
, false);
673 rpc_uci_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
674 struct ubus_request_data
*req
, const char *method
,
675 struct blob_attr
*msg
)
677 return rpc_uci_getcommon(ctx
, req
, msg
, true);
681 rpc_uci_add(struct ubus_context
*ctx
, struct ubus_object
*obj
,
682 struct ubus_request_data
*req
, const char *method
,
683 struct blob_attr
*msg
)
685 struct blob_attr
*tb
[__RPC_A_MAX
];
686 struct blob_attr
*cur
, *elem
;
687 struct uci_package
*p
= NULL
;
688 struct uci_section
*s
;
689 struct uci_ptr ptr
= { 0 };
690 int rem
, rem2
, err
= 0;
692 blobmsg_parse(rpc_uci_add_policy
, __RPC_A_MAX
, tb
,
693 blob_data(msg
), blob_len(msg
));
695 if (!tb
[RPC_A_CONFIG
] || !tb
[RPC_A_TYPE
])
696 return UBUS_STATUS_INVALID_ARGUMENT
;
698 if (!rpc_uci_write_access(tb
[RPC_A_SESSION
], tb
[RPC_A_CONFIG
]))
699 return UBUS_STATUS_PERMISSION_DENIED
;
701 if (!rpc_uci_verify_type(blobmsg_data(tb
[RPC_A_TYPE
])))
702 return UBUS_STATUS_INVALID_ARGUMENT
;
704 if (tb
[RPC_A_NAME
] &&
705 !rpc_uci_verify_name(blobmsg_data(tb
[RPC_A_NAME
])))
706 return UBUS_STATUS_INVALID_ARGUMENT
;
708 ptr
.package
= blobmsg_data(tb
[RPC_A_CONFIG
]);
710 if (uci_load(cursor
, ptr
.package
, &p
))
711 return rpc_uci_status();
713 /* add named section */
716 ptr
.section
= blobmsg_data(tb
[RPC_A_NAME
]);
717 ptr
.value
= blobmsg_data(tb
[RPC_A_TYPE
]);
720 if (rpc_uci_lookup(&ptr
) || uci_set(cursor
, &ptr
))
724 /* add anon section */
727 if (uci_add_section(cursor
, p
, blobmsg_data(tb
[RPC_A_TYPE
]), &s
) || !s
)
730 ptr
.section
= s
->e
.name
;
733 if (tb
[RPC_A_VALUES
])
735 blobmsg_for_each_attr(cur
, tb
[RPC_A_VALUES
], rem
)
739 ptr
.option
= blobmsg_name(cur
);
741 if (!rpc_uci_verify_name(ptr
.option
))
744 err
= UBUS_STATUS_INVALID_ARGUMENT
;
749 if (rpc_uci_lookup(&ptr
) || !ptr
.s
)
752 err
= UBUS_STATUS_NOT_FOUND
;
757 switch (blobmsg_type(cur
))
759 case BLOBMSG_TYPE_ARRAY
:
760 blobmsg_for_each_attr(elem
, cur
, rem2
)
762 if (!rpc_uci_format_blob(elem
, &ptr
.value
))
765 err
= UBUS_STATUS_INVALID_ARGUMENT
;
770 uci_add_list(cursor
, &ptr
);
776 if (!rpc_uci_format_blob(cur
, &ptr
.value
))
779 err
= UBUS_STATUS_INVALID_ARGUMENT
;
783 uci_set(cursor
, &ptr
);
795 blob_buf_init(&buf
, 0);
796 blobmsg_add_string(&buf
, "section", ptr
.section
);
797 ubus_send_reply(ctx
, req
, buf
.head
);
801 uci_unload(cursor
, p
);
803 return err
? err
: rpc_uci_status();
807 * Turn value from a blob attribute into uci set operation
808 * 1) if the blob is of type array, delete existing option (if any) and
809 * emit uci add_list operations for each element
810 * 2) if the blob is not an array but an option of type list exists,
811 * delete existing list and emit uci set operation for the blob value
812 * 3) in all other cases only emit a set operation if there is no existing
813 * option of if the existing options value differs from the blob value
816 rpc_uci_merge_set(struct blob_attr
*opt
, struct uci_ptr
*ptr
)
818 struct blob_attr
*cur
;
823 ptr
->option
= blobmsg_name(opt
);
826 if (!rpc_uci_verify_name(ptr
->option
))
827 return UBUS_STATUS_INVALID_ARGUMENT
;
829 if (rpc_uci_lookup(ptr
) || !ptr
->s
)
830 return UBUS_STATUS_NOT_FOUND
;
832 if (blobmsg_type(opt
) == BLOBMSG_TYPE_ARRAY
)
835 uci_delete(cursor
, ptr
);
839 rv
= UBUS_STATUS_INVALID_ARGUMENT
;
841 blobmsg_for_each_attr(cur
, opt
, rem
)
843 if (!rpc_uci_format_blob(cur
, &ptr
->value
))
846 uci_add_list(cursor
, ptr
);
852 else if (ptr
->o
&& ptr
->o
->type
== UCI_TYPE_LIST
)
854 uci_delete(cursor
, ptr
);
857 if (!rpc_uci_format_blob(opt
, &ptr
->value
))
858 return UBUS_STATUS_INVALID_ARGUMENT
;
860 uci_set(cursor
, ptr
);
864 if (!rpc_uci_format_blob(opt
, &ptr
->value
))
865 return UBUS_STATUS_INVALID_ARGUMENT
;
867 if (!ptr
->o
|| !ptr
->o
->v
.string
|| strcmp(ptr
->o
->v
.string
, ptr
->value
))
868 uci_set(cursor
, ptr
);
875 rpc_uci_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
876 struct ubus_request_data
*req
, const char *method
,
877 struct blob_attr
*msg
)
879 struct blob_attr
*tb
[__RPC_S_MAX
];
880 struct blob_attr
*cur
;
881 struct uci_package
*p
= NULL
;
882 struct uci_element
*e
;
883 struct uci_ptr ptr
= { 0 };
884 int rem
, rv
, err
= 0;
886 blobmsg_parse(rpc_uci_set_policy
, __RPC_S_MAX
, tb
,
887 blob_data(msg
), blob_len(msg
));
889 if (!tb
[RPC_S_CONFIG
] || !tb
[RPC_S_VALUES
] ||
890 (!tb
[RPC_S_SECTION
] && !tb
[RPC_S_TYPE
] && !tb
[RPC_S_MATCH
]))
891 return UBUS_STATUS_INVALID_ARGUMENT
;
893 if (!rpc_uci_write_access(tb
[RPC_S_SESSION
], tb
[RPC_S_CONFIG
]))
894 return UBUS_STATUS_PERMISSION_DENIED
;
896 if (tb
[RPC_S_SECTION
] &&
897 !rpc_uci_verify_section(blobmsg_data(tb
[RPC_S_SECTION
])))
898 return UBUS_STATUS_INVALID_ARGUMENT
;
900 ptr
.package
= blobmsg_data(tb
[RPC_S_CONFIG
]);
902 if (uci_load(cursor
, ptr
.package
, &p
))
903 return rpc_uci_status();
905 if (tb
[RPC_S_SECTION
])
907 ptr
.section
= blobmsg_data(tb
[RPC_S_SECTION
]);
908 blobmsg_for_each_attr(cur
, tb
[RPC_S_VALUES
], rem
)
910 rv
= rpc_uci_merge_set(cur
, &ptr
);
918 uci_foreach_element(&p
->sections
, e
)
920 if (!rpc_uci_match_section(uci_to_section(e
),
921 tb
[RPC_S_TYPE
], tb
[RPC_S_MATCH
]))
925 ptr
.section
= e
->name
;
927 blobmsg_for_each_attr(cur
, tb
[RPC_S_VALUES
], rem
)
929 rv
= rpc_uci_merge_set(cur
, &ptr
);
938 err
= UBUS_STATUS_NOT_FOUND
;
943 uci_unload(cursor
, p
);
945 return err
? err
: rpc_uci_status();
949 * Delete option or section from uci specified by given blob attribute pointer
950 * 1) if the blob is of type array, delete any option named after each element
951 * 2) if the blob is of type string, delete the option named after its value
952 * 3) if the blob is NULL, delete entire section
955 rpc_uci_merge_delete(struct blob_attr
*opt
, struct uci_ptr
*ptr
)
957 struct blob_attr
*cur
;
960 if (rpc_uci_lookup(ptr
) || !ptr
->s
)
961 return UBUS_STATUS_NOT_FOUND
;
968 uci_delete(cursor
, ptr
);
971 else if (blobmsg_type(opt
) == BLOBMSG_TYPE_ARRAY
)
973 rv
= UBUS_STATUS_NOT_FOUND
;
975 blobmsg_for_each_attr(cur
, opt
, rem
)
977 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
981 ptr
->option
= blobmsg_data(cur
);
983 if (rpc_uci_lookup(ptr
) || !ptr
->o
)
986 uci_delete(cursor
, ptr
);
993 else if (blobmsg_type(opt
) == BLOBMSG_TYPE_STRING
)
996 ptr
->option
= blobmsg_data(opt
);
998 if (rpc_uci_lookup(ptr
) || !ptr
->o
)
999 return UBUS_STATUS_NOT_FOUND
;
1001 uci_delete(cursor
, ptr
);
1005 return UBUS_STATUS_INVALID_ARGUMENT
;
1009 rpc_uci_delete(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1010 struct ubus_request_data
*req
, const char *method
,
1011 struct blob_attr
*msg
)
1013 struct blob_attr
*tb
[__RPC_D_MAX
];
1014 struct uci_package
*p
= NULL
;
1015 struct uci_element
*e
, *tmp
;
1016 struct uci_ptr ptr
= { 0 };
1019 blobmsg_parse(rpc_uci_delete_policy
, __RPC_D_MAX
, tb
,
1020 blob_data(msg
), blob_len(msg
));
1022 if (!tb
[RPC_D_CONFIG
] ||
1023 (!tb
[RPC_D_SECTION
] && !tb
[RPC_D_TYPE
] && !tb
[RPC_D_MATCH
]))
1024 return UBUS_STATUS_INVALID_ARGUMENT
;
1026 if (!rpc_uci_write_access(tb
[RPC_D_SESSION
], tb
[RPC_D_CONFIG
]))
1027 return UBUS_STATUS_PERMISSION_DENIED
;
1029 if (tb
[RPC_D_TYPE
] &&
1030 !rpc_uci_verify_type(blobmsg_data(tb
[RPC_D_TYPE
])))
1031 return UBUS_STATUS_INVALID_ARGUMENT
;
1033 if (tb
[RPC_D_SECTION
] &&
1034 !rpc_uci_verify_section(blobmsg_data(tb
[RPC_D_SECTION
])))
1035 return UBUS_STATUS_INVALID_ARGUMENT
;
1037 ptr
.package
= blobmsg_data(tb
[RPC_D_CONFIG
]);
1039 if (uci_load(cursor
, ptr
.package
, &p
))
1040 return rpc_uci_status();
1042 if (tb
[RPC_D_SECTION
])
1044 ptr
.section
= blobmsg_data(tb
[RPC_D_SECTION
]);
1046 if (tb
[RPC_D_OPTIONS
])
1047 err
= rpc_uci_merge_delete(tb
[RPC_D_OPTIONS
], &ptr
);
1049 err
= rpc_uci_merge_delete(tb
[RPC_D_OPTION
], &ptr
);
1053 uci_foreach_element_safe(&p
->sections
, tmp
, e
)
1055 if (!rpc_uci_match_section(uci_to_section(e
),
1056 tb
[RPC_D_TYPE
], tb
[RPC_D_MATCH
]))
1060 ptr
.section
= e
->name
;
1062 if (tb
[RPC_D_OPTIONS
])
1063 err
= rpc_uci_merge_delete(tb
[RPC_D_OPTIONS
], &ptr
);
1065 err
= rpc_uci_merge_delete(tb
[RPC_D_OPTION
], &ptr
);
1068 if (!err
&& !ptr
.section
)
1069 err
= UBUS_STATUS_NOT_FOUND
;
1073 uci_save(cursor
, p
);
1075 uci_unload(cursor
, p
);
1077 return err
? err
: rpc_uci_status();
1081 rpc_uci_rename(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1082 struct ubus_request_data
*req
, const char *method
,
1083 struct blob_attr
*msg
)
1085 struct blob_attr
*tb
[__RPC_R_MAX
];
1086 struct uci_package
*p
= NULL
;
1087 struct uci_ptr ptr
= { 0 };
1089 blobmsg_parse(rpc_uci_rename_policy
, __RPC_R_MAX
, tb
,
1090 blob_data(msg
), blob_len(msg
));
1092 if (!tb
[RPC_R_CONFIG
] || !tb
[RPC_R_SECTION
] || !tb
[RPC_R_NAME
])
1093 return UBUS_STATUS_INVALID_ARGUMENT
;
1095 if (!rpc_uci_write_access(tb
[RPC_R_SESSION
], tb
[RPC_R_CONFIG
]))
1096 return UBUS_STATUS_PERMISSION_DENIED
;
1098 ptr
.package
= blobmsg_data(tb
[RPC_R_CONFIG
]);
1099 ptr
.section
= blobmsg_data(tb
[RPC_R_SECTION
]);
1100 ptr
.value
= blobmsg_data(tb
[RPC_R_NAME
]);
1102 if (!rpc_uci_verify_name(ptr
.value
))
1103 return UBUS_STATUS_INVALID_ARGUMENT
;
1105 if (tb
[RPC_R_OPTION
])
1106 ptr
.option
= blobmsg_data(tb
[RPC_R_OPTION
]);
1108 if (uci_load(cursor
, ptr
.package
, &p
))
1109 return rpc_uci_status();
1111 if (uci_lookup_ptr(cursor
, &ptr
, NULL
, true))
1114 if ((ptr
.option
&& !ptr
.o
) || !ptr
.s
)
1116 cursor
->err
= UCI_ERR_NOTFOUND
;
1120 if (uci_rename(cursor
, &ptr
))
1123 uci_save(cursor
, p
);
1126 uci_unload(cursor
, p
);
1128 return rpc_uci_status();
1132 rpc_uci_order(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1133 struct ubus_request_data
*req
, const char *method
,
1134 struct blob_attr
*msg
)
1136 struct blob_attr
*tb
[__RPC_O_MAX
];
1137 struct blob_attr
*cur
;
1138 struct uci_package
*p
= NULL
;
1139 struct uci_ptr ptr
= { 0 };
1140 int rem
, i
= 0, err
= 0;
1142 blobmsg_parse(rpc_uci_order_policy
, __RPC_O_MAX
, tb
,
1143 blob_data(msg
), blob_len(msg
));
1145 if (!tb
[RPC_O_CONFIG
] || !tb
[RPC_O_SECTIONS
])
1146 return UBUS_STATUS_INVALID_ARGUMENT
;
1148 if (!rpc_uci_write_access(tb
[RPC_O_SESSION
], tb
[RPC_O_CONFIG
]))
1149 return UBUS_STATUS_PERMISSION_DENIED
;
1151 ptr
.package
= blobmsg_data(tb
[RPC_O_CONFIG
]);
1153 if (uci_load(cursor
, ptr
.package
, &p
))
1154 return rpc_uci_status();
1156 blobmsg_for_each_attr(cur
, tb
[RPC_O_SECTIONS
], rem
)
1158 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
1161 err
= UBUS_STATUS_INVALID_ARGUMENT
;
1167 ptr
.section
= blobmsg_data(cur
);
1169 if (uci_lookup_ptr(cursor
, &ptr
, NULL
, true) || !ptr
.s
)
1172 err
= UBUS_STATUS_NOT_FOUND
;
1177 uci_reorder_section(cursor
, ptr
.s
, i
++);
1181 uci_save(cursor
, p
);
1183 uci_unload(cursor
, p
);
1185 return err
? err
: rpc_uci_status();
1189 rpc_uci_dump_change(struct uci_delta
*d
)
1192 const char *types
[] = {
1193 [UCI_CMD_REORDER
] = "order",
1194 [UCI_CMD_REMOVE
] = "remove",
1195 [UCI_CMD_RENAME
] = "rename",
1196 [UCI_CMD_ADD
] = "add",
1197 [UCI_CMD_LIST_ADD
] = "list-add",
1198 [UCI_CMD_LIST_DEL
] = "list-del",
1199 [UCI_CMD_CHANGE
] = "set",
1205 c
= blobmsg_open_array(&buf
, NULL
);
1207 blobmsg_add_string(&buf
, NULL
, types
[d
->cmd
]);
1208 blobmsg_add_string(&buf
, NULL
, d
->section
);
1211 blobmsg_add_string(&buf
, NULL
, d
->e
.name
);
1215 if (d
->cmd
== UCI_CMD_REORDER
)
1216 blobmsg_add_u32(&buf
, NULL
, strtoul(d
->value
, NULL
, 10));
1218 blobmsg_add_string(&buf
, NULL
, d
->value
);
1221 blobmsg_close_array(&buf
, c
);
1225 rpc_uci_changes(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1226 struct ubus_request_data
*req
, const char *method
,
1227 struct blob_attr
*msg
)
1229 struct blob_attr
*tb
[__RPC_C_MAX
];
1230 struct uci_package
*p
= NULL
;
1231 struct uci_element
*e
;
1236 blobmsg_parse(rpc_uci_config_policy
, __RPC_C_MAX
, tb
,
1237 blob_data(msg
), blob_len(msg
));
1239 if (tb
[RPC_C_CONFIG
])
1241 if (!rpc_uci_read_access(tb
[RPC_C_SESSION
], tb
[RPC_C_CONFIG
]))
1242 return UBUS_STATUS_PERMISSION_DENIED
;
1244 if (uci_load(cursor
, blobmsg_data(tb
[RPC_C_CONFIG
]), &p
))
1245 return rpc_uci_status();
1247 blob_buf_init(&buf
, 0);
1248 c
= blobmsg_open_array(&buf
, "changes");
1250 uci_foreach_element(&p
->saved_delta
, e
)
1251 rpc_uci_dump_change(uci_to_delta(e
));
1253 blobmsg_close_array(&buf
, c
);
1255 uci_unload(cursor
, p
);
1257 ubus_send_reply(ctx
, req
, buf
.head
);
1259 return rpc_uci_status();
1262 rpc_uci_set_savedir(tb
[RPC_C_SESSION
]);
1264 if (uci_list_configs(cursor
, &configs
))
1265 return rpc_uci_status();
1267 blob_buf_init(&buf
, 0);
1269 c
= blobmsg_open_table(&buf
, "changes");
1271 for (i
= 0; configs
[i
]; i
++)
1273 if (tb
[RPC_C_SESSION
] &&
1274 !rpc_session_access(blobmsg_data(tb
[RPC_C_SESSION
]), "uci",
1275 configs
[i
], "read"))
1278 if (uci_load(cursor
, configs
[i
], &p
))
1281 if (!uci_list_empty(&p
->saved_delta
))
1283 d
= blobmsg_open_array(&buf
, configs
[i
]);
1285 uci_foreach_element(&p
->saved_delta
, e
)
1286 rpc_uci_dump_change(uci_to_delta(e
));
1288 blobmsg_close_array(&buf
, d
);
1291 uci_unload(cursor
, p
);
1296 blobmsg_close_table(&buf
, c
);
1298 ubus_send_reply(ctx
, req
, buf
.head
);
1304 rpc_uci_trigger_event(struct ubus_context
*ctx
, const char *config
)
1306 char *pkg
= strdup(config
);
1307 static struct blob_buf b
;
1310 if (!ubus_lookup_id(ctx
, "service", &id
)) {
1313 blob_buf_init(&b
, 0);
1314 blobmsg_add_string(&b
, "type", "config.change");
1315 c
= blobmsg_open_table(&b
, "data");
1316 blobmsg_add_string(&b
, "package", pkg
);
1317 blobmsg_close_table(&b
, c
);
1318 ubus_invoke(ctx
, id
, "event", b
.head
, NULL
, 0, 1000);
1324 rpc_uci_revert_commit(struct ubus_context
*ctx
, struct blob_attr
*msg
, bool commit
)
1326 struct blob_attr
*tb
[__RPC_C_MAX
];
1327 struct uci_package
*p
= NULL
;
1328 struct uci_ptr ptr
= { 0 };
1331 return UBUS_STATUS_PERMISSION_DENIED
;
1333 blobmsg_parse(rpc_uci_config_policy
, __RPC_C_MAX
, tb
,
1334 blob_data(msg
), blob_len(msg
));
1336 if (!tb
[RPC_C_CONFIG
])
1337 return UBUS_STATUS_INVALID_ARGUMENT
;
1339 if (!rpc_uci_write_access(tb
[RPC_C_SESSION
], tb
[RPC_C_CONFIG
]))
1340 return UBUS_STATUS_PERMISSION_DENIED
;
1342 ptr
.package
= blobmsg_data(tb
[RPC_C_CONFIG
]);
1346 if (!uci_load(cursor
, ptr
.package
, &p
))
1348 uci_commit(cursor
, &p
, false);
1349 uci_unload(cursor
, p
);
1350 rpc_uci_trigger_event(ctx
, blobmsg_get_string(tb
[RPC_C_CONFIG
]));
1355 if (!uci_lookup_ptr(cursor
, &ptr
, NULL
, true) && ptr
.p
)
1357 uci_revert(cursor
, &ptr
);
1358 uci_unload(cursor
, ptr
.p
);
1362 return rpc_uci_status();
1366 rpc_uci_revert(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1367 struct ubus_request_data
*req
, const char *method
,
1368 struct blob_attr
*msg
)
1370 return rpc_uci_revert_commit(ctx
, msg
, false);
1374 rpc_uci_commit(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1375 struct ubus_request_data
*req
, const char *method
,
1376 struct blob_attr
*msg
)
1378 return rpc_uci_revert_commit(ctx
, msg
, true);
1382 rpc_uci_configs(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1383 struct ubus_request_data
*req
, const char *method
,
1384 struct blob_attr
*msg
)
1390 if (uci_list_configs(cursor
, &configs
))
1393 blob_buf_init(&buf
, 0);
1395 c
= blobmsg_open_array(&buf
, "configs");
1397 for (i
= 0; configs
[i
]; i
++)
1398 blobmsg_add_string(&buf
, NULL
, configs
[i
]);
1402 blobmsg_close_array(&buf
, c
);
1404 ubus_send_reply(ctx
, req
, buf
.head
);
1407 return rpc_uci_status();
1412 * Remove given delta save directory (if any).
1415 rpc_uci_purge_dir(const char *path
)
1420 char file
[PATH_MAX
];
1422 if (stat(path
, &s
) || !S_ISDIR(s
.st_mode
))
1425 if ((d
= opendir(path
)) != NULL
)
1427 while ((e
= readdir(d
)) != NULL
)
1429 snprintf(file
, sizeof(file
) - 1, "%s/%s", path
, e
->d_name
);
1431 if (stat(file
, &s
) || !S_ISREG(s
.st_mode
))
1444 rpc_uci_apply_config(struct ubus_context
*ctx
, char *config
)
1446 struct uci_package
*p
= NULL
;
1448 if (!uci_load(cursor
, config
, &p
)) {
1449 uci_commit(cursor
, &p
, false);
1450 uci_unload(cursor
, p
);
1452 rpc_uci_trigger_event(ctx
, config
);
1458 rpc_uci_copy_file(const char *src
, const char *target
, const char *file
)
1463 snprintf(tmp
, sizeof(tmp
), "%s%s", src
, file
);
1464 in
= fopen(tmp
, "rb");
1465 snprintf(tmp
, sizeof(tmp
), "%s%s", target
, file
);
1466 out
= fopen(tmp
, "wb+");
1469 int len
= fread(tmp
, 1, sizeof(tmp
), in
);
1472 fwrite(tmp
, 1, len
, out
);
1481 rpc_uci_apply_access(const char *sid
, glob_t
*gl
)
1486 if (gl
->gl_pathc
< 3)
1487 return UBUS_STATUS_NO_DATA
;
1489 for (i
= 0; i
< gl
->gl_pathc
; i
++) {
1490 char *config
= basename(gl
->gl_pathv
[i
]);
1494 if (stat(gl
->gl_pathv
[i
], &s
) || !s
.st_size
)
1496 if (!rpc_session_access(sid
, "uci", config
, "write"))
1497 return UBUS_STATUS_PERMISSION_DENIED
;
1502 return UBUS_STATUS_NO_DATA
;
1508 rpc_uci_do_rollback(struct ubus_context
*ctx
, glob_t
*gl
)
1513 /* Test apply permission to see if the initiator session still exists.
1514 * If it does, restore the delta files as well, else just restore the
1515 * main configuration files. */
1517 ? rpc_uci_apply_access(apply_sid
, gl
) : UBUS_STATUS_NOT_FOUND
;
1520 snprintf(tmp
, sizeof(tmp
), RPC_UCI_SAVEDIR_PREFIX
"%s/", apply_sid
);
1524 /* avoid merging unrelated uci changes when restoring old configs */
1525 rpc_uci_replace_savedir("/dev/null");
1527 for (i
= 0; i
< gl
->gl_pathc
; i
++) {
1528 char *config
= basename(gl
->gl_pathv
[i
]);
1533 rpc_uci_copy_file(RPC_SNAPSHOT_FILES
, RPC_UCI_DIR
, config
);
1534 rpc_uci_apply_config(ctx
, config
);
1539 rpc_uci_copy_file(RPC_SNAPSHOT_DELTA
, tmp
, config
);
1542 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES
);
1543 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA
);
1545 uloop_timeout_cancel(&apply_timer
);
1546 memset(apply_sid
, 0, sizeof(apply_sid
));
1551 rpc_uci_apply_timeout(struct uloop_timeout
*t
)
1556 snprintf(tmp
, sizeof(tmp
), "%s/*", RPC_SNAPSHOT_FILES
);
1557 if (glob(tmp
, GLOB_PERIOD
, NULL
, &gl
) < 0)
1560 rpc_uci_do_rollback(apply_ctx
, &gl
);
1566 rpc_uci_apply(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1567 struct ubus_request_data
*req
, const char *method
,
1568 struct blob_attr
*msg
)
1570 struct blob_attr
*tb
[__RPC_T_MAX
];
1571 int timeout
= RPC_APPLY_TIMEOUT
;
1573 bool rollback
= false;
1578 blobmsg_parse(rpc_uci_apply_policy
, __RPC_T_MAX
, tb
,
1579 blob_data(msg
), blob_len(msg
));
1581 if (tb
[RPC_T_ROLLBACK
])
1582 rollback
= blobmsg_get_bool(tb
[RPC_T_ROLLBACK
]);
1584 if (apply_sid
[0] && rollback
)
1585 return UBUS_STATUS_PERMISSION_DENIED
;
1587 if (!tb
[RPC_T_SESSION
])
1588 return UBUS_STATUS_INVALID_ARGUMENT
;
1590 sid
= blobmsg_data(tb
[RPC_T_SESSION
]);
1592 if (tb
[RPC_T_TIMEOUT
])
1593 timeout
= blobmsg_get_u32(tb
[RPC_T_TIMEOUT
]);
1595 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES
);
1596 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA
);
1598 if (!apply_sid
[0]) {
1599 rpc_uci_set_savedir(tb
[RPC_T_SESSION
]);
1601 mkdir(RPC_SNAPSHOT_FILES
, 0700);
1602 mkdir(RPC_SNAPSHOT_DELTA
, 0700);
1604 snprintf(tmp
, sizeof(tmp
), RPC_UCI_SAVEDIR_PREFIX
"%s/*", sid
);
1605 if (glob(tmp
, GLOB_PERIOD
, NULL
, &gl
) < 0)
1606 return UBUS_STATUS_NOT_FOUND
;
1608 snprintf(tmp
, sizeof(tmp
), RPC_UCI_SAVEDIR_PREFIX
"%s/", sid
);
1610 ret
= rpc_uci_apply_access(sid
, &gl
);
1616 /* copy SID early because rpc_uci_apply_config() will clobber buf */
1618 strncpy(apply_sid
, sid
, RPC_SID_LEN
);
1620 for (i
= 0; i
< gl
.gl_pathc
; i
++) {
1621 char *config
= basename(gl
.gl_pathv
[i
]);
1627 if (stat(gl
.gl_pathv
[i
], &s
) || !s
.st_size
)
1630 rpc_uci_copy_file(RPC_UCI_DIR
, RPC_SNAPSHOT_FILES
, config
);
1631 rpc_uci_copy_file(tmp
, RPC_SNAPSHOT_DELTA
, config
);
1632 rpc_uci_apply_config(ctx
, config
);
1638 apply_timer
.cb
= rpc_uci_apply_timeout
;
1639 uloop_timeout_set(&apply_timer
, timeout
* 1000);
1648 rpc_uci_confirm(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1649 struct ubus_request_data
*req
, const char *method
,
1650 struct blob_attr
*msg
)
1652 struct blob_attr
*tb
[__RPC_B_MAX
];
1655 blobmsg_parse(rpc_uci_rollback_policy
, __RPC_B_MAX
, tb
,
1656 blob_data(msg
), blob_len(msg
));
1658 if (!tb
[RPC_B_SESSION
])
1659 return UBUS_STATUS_INVALID_ARGUMENT
;
1661 sid
= blobmsg_data(tb
[RPC_B_SESSION
]);
1664 return UBUS_STATUS_NO_DATA
;
1666 if (strcmp(apply_sid
, sid
))
1667 return UBUS_STATUS_PERMISSION_DENIED
;
1669 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES
);
1670 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA
);
1672 uloop_timeout_cancel(&apply_timer
);
1673 memset(apply_sid
, 0, sizeof(apply_sid
));
1680 rpc_uci_rollback(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1681 struct ubus_request_data
*req
, const char *method
,
1682 struct blob_attr
*msg
)
1684 struct blob_attr
*tb
[__RPC_B_MAX
];
1689 blobmsg_parse(rpc_uci_rollback_policy
, __RPC_B_MAX
, tb
,
1690 blob_data(msg
), blob_len(msg
));
1693 return UBUS_STATUS_NO_DATA
;
1695 if (!tb
[RPC_B_SESSION
])
1696 return UBUS_STATUS_INVALID_ARGUMENT
;
1698 sid
= blobmsg_data(tb
[RPC_B_SESSION
]);
1700 if (strcmp(apply_sid
, sid
))
1701 return UBUS_STATUS_PERMISSION_DENIED
;
1703 snprintf(tmp
, sizeof(tmp
), "%s/*", RPC_SNAPSHOT_FILES
);
1704 if (glob(tmp
, GLOB_PERIOD
, NULL
, &gl
) < 0)
1705 return UBUS_STATUS_NOT_FOUND
;
1707 rpc_uci_do_rollback(ctx
, &gl
);
1715 rpc_uci_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1716 struct ubus_request_data
*req
, const char *method
,
1717 struct blob_attr
*msg
)
1719 char * const cmd
[2] = { "/sbin/reload_config", NULL
};
1722 /* wait for the RPC call to complete */
1724 return execv(cmd
[0], cmd
);
1731 * Session destroy callback to purge associated delta directory.
1734 rpc_uci_purge_savedir_cb(struct rpc_session
*ses
, void *priv
)
1736 char path
[PATH_MAX
];
1738 snprintf(path
, sizeof(path
) - 1, RPC_UCI_SAVEDIR_PREFIX
"%s", ses
->id
);
1739 rpc_uci_purge_dir(path
);
1743 * Removes all delta directories which match the RPC_UCI_SAVEDIR_PREFIX.
1744 * This is used to clean up garbage when starting rpcd.
1746 void rpc_uci_purge_savedirs(void)
1751 if (!glob(RPC_UCI_SAVEDIR_PREFIX
"*", 0, NULL
, &gl
))
1753 for (i
= 0; i
< gl
.gl_pathc
; i
++)
1754 rpc_uci_purge_dir(gl
.gl_pathv
[i
]);
1760 int rpc_uci_api_init(struct ubus_context
*ctx
)
1762 static const struct ubus_method uci_methods
[] = {
1763 { .name
= "configs", .handler
= rpc_uci_configs
},
1764 UBUS_METHOD("get", rpc_uci_get
, rpc_uci_get_policy
),
1765 UBUS_METHOD("state", rpc_uci_state
, rpc_uci_get_policy
),
1766 UBUS_METHOD("add", rpc_uci_add
, rpc_uci_add_policy
),
1767 UBUS_METHOD("set", rpc_uci_set
, rpc_uci_set_policy
),
1768 UBUS_METHOD("delete", rpc_uci_delete
, rpc_uci_delete_policy
),
1769 UBUS_METHOD("rename", rpc_uci_rename
, rpc_uci_rename_policy
),
1770 UBUS_METHOD("order", rpc_uci_order
, rpc_uci_order_policy
),
1771 UBUS_METHOD("changes", rpc_uci_changes
, rpc_uci_config_policy
),
1772 UBUS_METHOD("revert", rpc_uci_revert
, rpc_uci_config_policy
),
1773 UBUS_METHOD("commit", rpc_uci_commit
, rpc_uci_config_policy
),
1774 UBUS_METHOD("apply", rpc_uci_apply
, rpc_uci_apply_policy
),
1775 UBUS_METHOD("confirm", rpc_uci_confirm
, rpc_uci_rollback_policy
),
1776 UBUS_METHOD("rollback", rpc_uci_rollback
, rpc_uci_rollback_policy
),
1777 UBUS_METHOD_NOARG("reload_config", rpc_uci_reload
),
1780 static struct ubus_object_type uci_type
=
1781 UBUS_OBJECT_TYPE("rpcd-plugin-uci", uci_methods
);
1783 static struct ubus_object obj
= {
1786 .methods
= uci_methods
,
1787 .n_methods
= ARRAY_SIZE(uci_methods
),
1790 static struct rpc_session_cb cb
= {
1791 .cb
= rpc_uci_purge_savedir_cb
1794 cursor
= uci_alloc_context();
1797 return UBUS_STATUS_UNKNOWN_ERROR
;
1799 rpc_session_destroy_cb(&cb
);
1801 return ubus_add_object(ctx
, &obj
);