a133ff27606cab6969db9d00f38b36ec9000f457
[project/rpcd.git] / uci.c
1 /*
2 * rpcd - UBUS RPC server
3 *
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
5 *
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.
9 *
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.
17 */
18
19 #include <libgen.h>
20 #include <glob.h>
21
22 #include <libubox/blobmsg.h>
23 #include <libubox/blobmsg_json.h>
24
25 #include <rpcd/uci.h>
26 #include <rpcd/session.h>
27
28 static struct blob_buf buf;
29 static struct uci_context *cursor;
30 static struct uloop_timeout apply_timer;
31 static struct ubus_context *apply_ctx;
32 static bool apply_running;
33
34 enum {
35 RPC_G_CONFIG,
36 RPC_G_SECTION,
37 RPC_G_OPTION,
38 RPC_G_TYPE,
39 RPC_G_MATCH,
40 RPC_G_SESSION,
41 __RPC_G_MAX,
42 };
43
44 static const struct blobmsg_policy rpc_uci_get_policy[__RPC_G_MAX] = {
45 [RPC_G_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
46 [RPC_G_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
47 [RPC_G_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
48 [RPC_G_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
49 [RPC_G_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
50 [RPC_G_SESSION] = { .name = "ubus_rpc_session",
51 .type = BLOBMSG_TYPE_STRING },
52 };
53
54 enum {
55 RPC_A_CONFIG,
56 RPC_A_TYPE,
57 RPC_A_NAME,
58 RPC_A_VALUES,
59 RPC_A_SESSION,
60 __RPC_A_MAX,
61 };
62
63 static const struct blobmsg_policy rpc_uci_add_policy[__RPC_A_MAX] = {
64 [RPC_A_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
65 [RPC_A_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
66 [RPC_A_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
67 [RPC_A_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
68 [RPC_A_SESSION] = { .name = "ubus_rpc_session",
69 .type = BLOBMSG_TYPE_STRING },
70 };
71
72 enum {
73 RPC_S_CONFIG,
74 RPC_S_SECTION,
75 RPC_S_TYPE,
76 RPC_S_MATCH,
77 RPC_S_VALUES,
78 RPC_S_SESSION,
79 __RPC_S_MAX,
80 };
81
82 static const struct blobmsg_policy rpc_uci_set_policy[__RPC_S_MAX] = {
83 [RPC_S_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
84 [RPC_S_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
85 [RPC_S_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
86 [RPC_S_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
87 [RPC_S_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
88 [RPC_S_SESSION] = { .name = "ubus_rpc_session",
89 .type = BLOBMSG_TYPE_STRING },
90 };
91
92 enum {
93 RPC_D_CONFIG,
94 RPC_D_SECTION,
95 RPC_D_TYPE,
96 RPC_D_MATCH,
97 RPC_D_OPTION,
98 RPC_D_OPTIONS,
99 RPC_D_SESSION,
100 __RPC_D_MAX,
101 };
102
103 static const struct blobmsg_policy rpc_uci_delete_policy[__RPC_D_MAX] = {
104 [RPC_D_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
105 [RPC_D_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
106 [RPC_D_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
107 [RPC_D_MATCH] = { .name = "match", .type = BLOBMSG_TYPE_TABLE },
108 [RPC_D_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
109 [RPC_D_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_ARRAY },
110 [RPC_D_SESSION] = { .name = "ubus_rpc_session",
111 .type = BLOBMSG_TYPE_STRING },
112 };
113
114 enum {
115 RPC_R_CONFIG,
116 RPC_R_SECTION,
117 RPC_R_OPTION,
118 RPC_R_NAME,
119 RPC_R_SESSION,
120 __RPC_R_MAX,
121 };
122
123 static const struct blobmsg_policy rpc_uci_rename_policy[__RPC_R_MAX] = {
124 [RPC_R_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
125 [RPC_R_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
126 [RPC_R_OPTION] = { .name = "option", .type = BLOBMSG_TYPE_STRING },
127 [RPC_R_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
128 [RPC_R_SESSION] = { .name = "ubus_rpc_session",
129 .type = BLOBMSG_TYPE_STRING },
130 };
131
132 enum {
133 RPC_O_CONFIG,
134 RPC_O_SECTIONS,
135 RPC_O_SESSION,
136 __RPC_O_MAX,
137 };
138
139 static const struct blobmsg_policy rpc_uci_order_policy[__RPC_O_MAX] = {
140 [RPC_O_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
141 [RPC_O_SECTIONS] = { .name = "sections", .type = BLOBMSG_TYPE_ARRAY },
142 [RPC_O_SESSION] = { .name = "ubus_rpc_session",
143 .type = BLOBMSG_TYPE_STRING },
144 };
145
146 enum {
147 RPC_C_CONFIG,
148 RPC_C_SESSION,
149 __RPC_C_MAX,
150 };
151
152 static const struct blobmsg_policy rpc_uci_config_policy[__RPC_C_MAX] = {
153 [RPC_C_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
154 [RPC_C_SESSION] = { .name = "ubus_rpc_session",
155 .type = BLOBMSG_TYPE_STRING },
156 };
157
158 enum {
159 RPC_T_COMMIT,
160 RPC_T_TIMEOUT,
161 RPC_T_SESSION,
162 __RPC_T_MAX,
163 };
164
165 static const struct blobmsg_policy rpc_uci_apply_policy[__RPC_T_MAX] = {
166 [RPC_T_COMMIT] = { .name = "commit", .type = BLOBMSG_TYPE_BOOL },
167 [RPC_T_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
168 [RPC_T_SESSION] = { .name = "ubus_rpc_session",
169 .type = BLOBMSG_TYPE_STRING },
170 };
171
172 enum {
173 RPC_B_SESSION,
174 __RPC_B_MAX,
175 };
176
177 static const struct blobmsg_policy rpc_uci_rollback_policy[__RPC_B_MAX] = {
178 [RPC_B_SESSION] = { .name = "ubus_rpc_session",
179 .type = BLOBMSG_TYPE_STRING },
180 };
181
182 /*
183 * Turn uci error state into ubus return code
184 */
185 static int
186 rpc_uci_status(void)
187 {
188 switch (cursor->err)
189 {
190 case UCI_OK:
191 return UBUS_STATUS_OK;
192
193 case UCI_ERR_INVAL:
194 return UBUS_STATUS_INVALID_ARGUMENT;
195
196 case UCI_ERR_NOTFOUND:
197 return UBUS_STATUS_NOT_FOUND;
198
199 default:
200 return UBUS_STATUS_UNKNOWN_ERROR;
201 }
202 }
203
204 /*
205 * Setup per-session delta save directory. If the passed "sid" blob attribute
206 * pointer is NULL then the precedure was not invoked through the ubus-rpc so
207 * we do not perform session isolation and use the default save directory.
208 */
209 static void
210 rpc_uci_set_savedir(struct blob_attr *sid)
211 {
212 char path[PATH_MAX];
213
214 if (!sid)
215 {
216 uci_set_savedir(cursor, "/tmp/.uci");
217 return;
218 }
219
220 snprintf(path, sizeof(path) - 1,
221 RPC_UCI_SAVEDIR_PREFIX "%s", blobmsg_get_string(sid));
222
223 uci_set_savedir(cursor, path);
224 }
225
226 /*
227 * Test read access to given config. If the passed "sid" blob attribute pointer
228 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
229 * perform access control and always assume true.
230 */
231 static bool
232 rpc_uci_read_access(struct blob_attr *sid, struct blob_attr *config)
233 {
234 rpc_uci_set_savedir(sid);
235
236 if (!sid)
237 return true;
238
239 return rpc_session_access(blobmsg_data(sid), "uci",
240 blobmsg_data(config), "read");
241 }
242
243 /*
244 * Test write access to given config. If the passed "sid" blob attribute pointer
245 * is NULL then the precedure was not invoked through the ubus-rpc so we do not
246 * perform access control and always assume true.
247 */
248 static bool
249 rpc_uci_write_access(struct blob_attr *sid, struct blob_attr *config)
250 {
251 rpc_uci_set_savedir(sid);
252
253 if (!sid)
254 return true;
255
256 return rpc_session_access(blobmsg_data(sid), "uci",
257 blobmsg_data(config), "write");
258 }
259
260 /*
261 * Format applicable blob value as string and place a pointer to the string
262 * buffer in "p". Uses a static string buffer.
263 */
264 static bool
265 rpc_uci_format_blob(struct blob_attr *v, const char **p)
266 {
267 static char buf[21];
268
269 *p = NULL;
270
271 switch (blobmsg_type(v))
272 {
273 case BLOBMSG_TYPE_STRING:
274 if (blobmsg_data_len(v) > 1)
275 *p = blobmsg_data(v);
276 break;
277
278 case BLOBMSG_TYPE_INT64:
279 snprintf(buf, sizeof(buf), "%"PRIu64, blobmsg_get_u64(v));
280 *p = buf;
281 break;
282
283 case BLOBMSG_TYPE_INT32:
284 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u32(v));
285 *p = buf;
286 break;
287
288 case BLOBMSG_TYPE_INT16:
289 snprintf(buf, sizeof(buf), "%u", blobmsg_get_u16(v));
290 *p = buf;
291 break;
292
293 case BLOBMSG_TYPE_INT8:
294 snprintf(buf, sizeof(buf), "%u", !!blobmsg_get_u8(v));
295 *p = buf;
296 break;
297
298 default:
299 break;
300 }
301
302 return !!*p;
303 }
304
305 /*
306 * Lookup the given uci_ptr and enable extended lookup format if the .section
307 * value of the uci_ptr looks like extended syntax. Uses an internal copy
308 * of the given uci_ptr to perform the lookup as failing extended section
309 * lookup operations in libuci will zero our the uci_ptr struct.
310 * Copies the internal uci_ptr back to given the uci_ptr on success.
311 */
312 static int
313 rpc_uci_lookup(struct uci_ptr *ptr)
314 {
315 int rv;
316 struct uci_ptr lookup = *ptr;
317
318 if (!lookup.s && lookup.section && *lookup.section == '@')
319 lookup.flags |= UCI_LOOKUP_EXTENDED;
320
321 rv = uci_lookup_ptr(cursor, &lookup, NULL, true);
322
323 if (!rv)
324 *ptr = lookup;
325
326 return rv;
327 }
328
329 /*
330 * Checks whether the given uci_option object matches the given string value.
331 * 1) If the uci_option is of type list, check whether any of the list elements
332 * equals to the given string
333 * 2) If the uci_option is of type string, parse it into space separated tokens
334 * and check if any of the tokens equals to the given string.
335 * Returns true if a list element or token matched the given string.
336 */
337 static bool
338 rpc_uci_match_option(struct uci_option *o, const char *cmp)
339 {
340 struct uci_element *e;
341 char *s, *p;
342
343 if (o->type == UCI_TYPE_LIST)
344 {
345 uci_foreach_element(&o->v.list, e)
346 if (e->name && !strcmp(e->name, cmp))
347 return true;
348
349 return false;
350 }
351
352 if (!o->v.string)
353 return false;
354
355 s = strdup(o->v.string);
356
357 if (!s)
358 return false;
359
360 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
361 {
362 if (!strcmp(p, cmp))
363 {
364 free(s);
365 return true;
366 }
367 }
368
369 free(s);
370 return false;
371 }
372
373 /*
374 * Checks whether the given uci_section matches the type and value blob attrs.
375 * 1) Returns false if "type" is given and the section type does not match
376 * the value specified in the "type" string blob attribute, else continue.
377 * 2) Tests whether any key in the "matches" table blob attribute exists in
378 * the given uci_section and whether each value is contained in the
379 * corresponding uci option value (see rpc_uci_match_option()).
380 * 3) A missing or empty "matches" table blob attribute is always considered
381 * to be a match.
382 * Returns true if "type" matches or is NULL and "matches" matches or is NULL.
383 */
384 static bool
385 rpc_uci_match_section(struct uci_section *s,
386 struct blob_attr *type, struct blob_attr *matches)
387 {
388 struct uci_element *e;
389 struct blob_attr *cur;
390 const char *cmp;
391 bool match = false;
392 bool empty = true;
393 int rem;
394
395 if (type && strcmp(s->type, blobmsg_data(type)))
396 return false;
397
398 if (!matches)
399 return true;
400
401 blobmsg_for_each_attr(cur, matches, rem)
402 {
403 if (!rpc_uci_format_blob(cur, &cmp))
404 continue;
405
406 uci_foreach_element(&s->options, e)
407 {
408 if (strcmp(e->name, blobmsg_name(cur)))
409 continue;
410
411 if (!rpc_uci_match_option(uci_to_option(e), cmp))
412 return false;
413
414 match = true;
415 }
416
417 empty = false;
418 }
419
420 return (empty || match);
421 }
422
423 /*
424 * Dump the given uci_option value into the global blobmsg buffer and use
425 * given "name" as key.
426 * 1) If the uci_option is of type list, put a table into the blob buffer and
427 * add each list item as string to it.
428 * 2) If the uci_option is of type string, put its value directly into the blob
429 * buffer.
430 */
431 static void
432 rpc_uci_dump_option(struct uci_option *o, const char *name)
433 {
434 void *c;
435 struct uci_element *e;
436
437 switch (o->type)
438 {
439 case UCI_TYPE_STRING:
440 blobmsg_add_string(&buf, name, o->v.string);
441 break;
442
443 case UCI_TYPE_LIST:
444 c = blobmsg_open_array(&buf, name);
445
446 uci_foreach_element(&o->v.list, e)
447 blobmsg_add_string(&buf, NULL, e->name);
448
449 blobmsg_close_array(&buf, c);
450 break;
451
452 default:
453 break;
454 }
455 }
456
457 /*
458 * Dump the given uci_section object into the global blobmsg buffer and use
459 * given "name" as key.
460 * Puts a table into the blob buffer and puts each section option member value
461 * as value into the table using the option name as key.
462 * Adds three special keys ".anonymous", ".type" and ".name" which specify the
463 * corresponding section properties.
464 */
465 static void
466 rpc_uci_dump_section(struct uci_section *s, const char *name, int index)
467 {
468 void *c;
469 struct uci_option *o;
470 struct uci_element *e;
471
472 c = blobmsg_open_table(&buf, name);
473
474 blobmsg_add_u8(&buf, ".anonymous", s->anonymous);
475 blobmsg_add_string(&buf, ".type", s->type);
476 blobmsg_add_string(&buf, ".name", s->e.name);
477
478 if (index >= 0)
479 blobmsg_add_u32(&buf, ".index", index);
480
481 uci_foreach_element(&s->options, e)
482 {
483 o = uci_to_option(e);
484 rpc_uci_dump_option(o, o->e.name);
485 }
486
487 blobmsg_close_table(&buf, c);
488 }
489
490 /*
491 * Dump the given uci_package object into the global blobmsg buffer and use
492 * given "name" as key.
493 * Puts a table into the blob buffer and puts each package section member as
494 * value into the table using the section name as key.
495 * Only dumps sections matching the given "type" and "matches", see explaination
496 * of rpc_uci_match_section() for details.
497 */
498 static void
499 rpc_uci_dump_package(struct uci_package *p, const char *name,
500 struct blob_attr *type, struct blob_attr *matches)
501 {
502 void *c;
503 struct uci_element *e;
504 int i = -1;
505
506 c = blobmsg_open_table(&buf, name);
507
508 uci_foreach_element(&p->sections, e)
509 {
510 i++;
511
512 if (!rpc_uci_match_section(uci_to_section(e), type, matches))
513 continue;
514
515 rpc_uci_dump_section(uci_to_section(e), e->name, i);
516 }
517
518 blobmsg_close_table(&buf, c);
519 }
520
521
522 static int
523 rpc_uci_get(struct ubus_context *ctx, struct ubus_object *obj,
524 struct ubus_request_data *req, const char *method,
525 struct blob_attr *msg)
526 {
527 struct blob_attr *tb[__RPC_G_MAX];
528 struct uci_package *p = NULL;
529 struct uci_ptr ptr = { 0 };
530
531 blobmsg_parse(rpc_uci_get_policy, __RPC_G_MAX, tb,
532 blob_data(msg), blob_len(msg));
533
534 if (!tb[RPC_G_CONFIG])
535 return UBUS_STATUS_INVALID_ARGUMENT;
536
537 if (!rpc_uci_read_access(tb[RPC_G_SESSION], tb[RPC_G_CONFIG]))
538 return UBUS_STATUS_PERMISSION_DENIED;
539
540 ptr.package = blobmsg_data(tb[RPC_G_CONFIG]);
541
542 if (uci_load(cursor, ptr.package, &p))
543 return rpc_uci_status();
544
545 if (tb[RPC_G_SECTION])
546 {
547 ptr.section = blobmsg_data(tb[RPC_G_SECTION]);
548
549 if (tb[RPC_G_OPTION])
550 ptr.option = blobmsg_data(tb[RPC_G_OPTION]);
551 }
552
553 if (rpc_uci_lookup(&ptr) || !(ptr.flags & UCI_LOOKUP_COMPLETE))
554 goto out;
555
556 blob_buf_init(&buf, 0);
557
558 switch (ptr.last->type)
559 {
560 case UCI_TYPE_PACKAGE:
561 rpc_uci_dump_package(ptr.p, "values", tb[RPC_G_TYPE], tb[RPC_G_MATCH]);
562 break;
563
564 case UCI_TYPE_SECTION:
565 rpc_uci_dump_section(ptr.s, "values", -1);
566 break;
567
568 case UCI_TYPE_OPTION:
569 rpc_uci_dump_option(ptr.o, "value");
570 break;
571
572 default:
573 break;
574 }
575
576 ubus_send_reply(ctx, req, buf.head);
577
578 out:
579 uci_unload(cursor, p);
580
581 return rpc_uci_status();
582 }
583
584 static int
585 rpc_uci_add(struct ubus_context *ctx, struct ubus_object *obj,
586 struct ubus_request_data *req, const char *method,
587 struct blob_attr *msg)
588 {
589 struct blob_attr *tb[__RPC_A_MAX];
590 struct blob_attr *cur, *elem;
591 struct uci_package *p = NULL;
592 struct uci_section *s;
593 struct uci_ptr ptr = { 0 };
594 int rem, rem2;
595
596 blobmsg_parse(rpc_uci_add_policy, __RPC_A_MAX, tb,
597 blob_data(msg), blob_len(msg));
598
599 if (!tb[RPC_A_CONFIG] || !tb[RPC_A_TYPE])
600 return UBUS_STATUS_INVALID_ARGUMENT;
601
602 if (!rpc_uci_write_access(tb[RPC_A_SESSION], tb[RPC_A_CONFIG]))
603 return UBUS_STATUS_PERMISSION_DENIED;
604
605 ptr.package = blobmsg_data(tb[RPC_A_CONFIG]);
606
607 if (uci_load(cursor, ptr.package, &p))
608 return rpc_uci_status();
609
610 /* add named section */
611 if (tb[RPC_A_NAME])
612 {
613 ptr.section = blobmsg_data(tb[RPC_A_NAME]);
614 ptr.value = blobmsg_data(tb[RPC_A_TYPE]);
615 ptr.option = NULL;
616
617 if (rpc_uci_lookup(&ptr) || uci_set(cursor, &ptr))
618 goto out;
619 }
620
621 /* add anon section */
622 else
623 {
624 if (uci_add_section(cursor, p, blobmsg_data(tb[RPC_A_TYPE]), &s) || !s)
625 goto out;
626
627 ptr.section = s->e.name;
628 }
629
630 if (tb[RPC_A_VALUES])
631 {
632 blobmsg_for_each_attr(cur, tb[RPC_A_VALUES], rem)
633 {
634 ptr.o = NULL;
635 ptr.option = blobmsg_name(cur);
636
637 if (rpc_uci_lookup(&ptr) || !ptr.s)
638 continue;
639
640 switch (blobmsg_type(cur))
641 {
642 case BLOBMSG_TYPE_ARRAY:
643 blobmsg_for_each_attr(elem, cur, rem2)
644 if (rpc_uci_format_blob(elem, &ptr.value))
645 uci_add_list(cursor, &ptr);
646 break;
647
648 default:
649 if (rpc_uci_format_blob(cur, &ptr.value))
650 uci_set(cursor, &ptr);
651 break;
652 }
653 }
654 }
655
656 uci_save(cursor, p);
657
658 blob_buf_init(&buf, 0);
659 blobmsg_add_string(&buf, "section", ptr.section);
660 ubus_send_reply(ctx, req, buf.head);
661
662 out:
663 uci_unload(cursor, p);
664
665 return rpc_uci_status();
666 }
667
668 /*
669 * Turn value from a blob attribute into uci set operation
670 * 1) if the blob is of type array, delete existing option (if any) and
671 * emit uci add_list operations for each element
672 * 2) if the blob is not an array but an option of type list exists,
673 * delete existing list and emit uci set operation for the blob value
674 * 3) in all other cases only emit a set operation if there is no existing
675 * option of if the existing options value differs from the blob value
676 */
677 static void
678 rpc_uci_merge_set(struct blob_attr *opt, struct uci_ptr *ptr)
679 {
680 struct blob_attr *cur;
681 int rem;
682
683 ptr->o = NULL;
684 ptr->option = blobmsg_name(opt);
685
686 if (rpc_uci_lookup(ptr) || !ptr->s)
687 return;
688
689 if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
690 {
691 if (ptr->o)
692 uci_delete(cursor, ptr);
693
694 blobmsg_for_each_attr(cur, opt, rem)
695 if (rpc_uci_format_blob(cur, &ptr->value))
696 uci_add_list(cursor, ptr);
697 }
698 else if (ptr->o && ptr->o->type == UCI_TYPE_LIST)
699 {
700 uci_delete(cursor, ptr);
701
702 if (rpc_uci_format_blob(opt, &ptr->value))
703 uci_set(cursor, ptr);
704 }
705 else if (rpc_uci_format_blob(opt, &ptr->value))
706 {
707 if (!ptr->o || !ptr->o->v.string || strcmp(ptr->o->v.string, ptr->value))
708 uci_set(cursor, ptr);
709 }
710 }
711
712 static int
713 rpc_uci_set(struct ubus_context *ctx, struct ubus_object *obj,
714 struct ubus_request_data *req, const char *method,
715 struct blob_attr *msg)
716 {
717 struct blob_attr *tb[__RPC_S_MAX];
718 struct blob_attr *cur;
719 struct uci_package *p = NULL;
720 struct uci_element *e;
721 struct uci_ptr ptr = { 0 };
722 int rem;
723
724 blobmsg_parse(rpc_uci_set_policy, __RPC_S_MAX, tb,
725 blob_data(msg), blob_len(msg));
726
727 if (!tb[RPC_S_CONFIG] || !tb[RPC_S_VALUES] ||
728 (!tb[RPC_S_SECTION] && !tb[RPC_S_TYPE] && !tb[RPC_S_MATCH]))
729 return UBUS_STATUS_INVALID_ARGUMENT;
730
731 if (!rpc_uci_write_access(tb[RPC_S_SESSION], tb[RPC_S_CONFIG]))
732 return UBUS_STATUS_PERMISSION_DENIED;
733
734 ptr.package = blobmsg_data(tb[RPC_S_CONFIG]);
735
736 if (uci_load(cursor, ptr.package, &p))
737 return rpc_uci_status();
738
739 if (tb[RPC_S_SECTION])
740 {
741 ptr.section = blobmsg_data(tb[RPC_S_SECTION]);
742 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
743 rpc_uci_merge_set(cur, &ptr);
744 }
745 else
746 {
747 uci_foreach_element(&p->sections, e)
748 {
749 if (!rpc_uci_match_section(uci_to_section(e),
750 tb[RPC_S_TYPE], tb[RPC_S_MATCH]))
751 continue;
752
753 ptr.s = NULL;
754 ptr.section = e->name;
755
756 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
757 rpc_uci_merge_set(cur, &ptr);
758 }
759 }
760
761 uci_save(cursor, p);
762 uci_unload(cursor, p);
763
764 return rpc_uci_status();
765 }
766
767 /*
768 * Delete option or section from uci specified by given blob attribute pointer
769 * 1) if the blob is of type array, delete any option named after each element
770 * 2) if the blob is of type string, delete the option named after its value
771 * 3) if the blob is NULL, delete entire section
772 */
773 static void
774 rpc_uci_merge_delete(struct blob_attr *opt, struct uci_ptr *ptr)
775 {
776 struct blob_attr *cur;
777 int rem;
778
779 if (rpc_uci_lookup(ptr) || !ptr->s)
780 return;
781
782 if (!opt)
783 {
784 ptr->o = NULL;
785 ptr->option = NULL;
786
787 uci_delete(cursor, ptr);
788 }
789 else if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
790 {
791 blobmsg_for_each_attr(cur, opt, rem)
792 {
793 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
794 continue;
795
796 ptr->o = NULL;
797 ptr->option = blobmsg_data(cur);
798
799 if (rpc_uci_lookup(ptr) || !ptr->o)
800 continue;
801
802 uci_delete(cursor, ptr);
803 }
804 }
805 else if (blobmsg_type(opt) == BLOBMSG_TYPE_STRING)
806 {
807 ptr->o = NULL;
808 ptr->option = blobmsg_data(opt);
809
810 if (rpc_uci_lookup(ptr) || !ptr->o)
811 return;
812
813 uci_delete(cursor, ptr);
814 }
815 }
816
817 static int
818 rpc_uci_delete(struct ubus_context *ctx, struct ubus_object *obj,
819 struct ubus_request_data *req, const char *method,
820 struct blob_attr *msg)
821 {
822 struct blob_attr *tb[__RPC_D_MAX];
823 struct uci_package *p = NULL;
824 struct uci_element *e, *tmp;
825 struct uci_ptr ptr = { 0 };
826
827 blobmsg_parse(rpc_uci_delete_policy, __RPC_D_MAX, tb,
828 blob_data(msg), blob_len(msg));
829
830 if (!tb[RPC_D_CONFIG] ||
831 (!tb[RPC_D_SECTION] && !tb[RPC_D_TYPE] && !tb[RPC_D_MATCH]))
832 return UBUS_STATUS_INVALID_ARGUMENT;
833
834 if (!rpc_uci_write_access(tb[RPC_D_SESSION], tb[RPC_D_CONFIG]))
835 return UBUS_STATUS_PERMISSION_DENIED;
836
837 ptr.package = blobmsg_data(tb[RPC_D_CONFIG]);
838
839 if (uci_load(cursor, ptr.package, &p))
840 return rpc_uci_status();
841
842 if (tb[RPC_D_SECTION])
843 {
844 ptr.section = blobmsg_data(tb[RPC_D_SECTION]);
845
846 if (tb[RPC_D_OPTIONS])
847 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
848 else
849 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
850 }
851 else
852 {
853 uci_foreach_element_safe(&p->sections, tmp, e)
854 {
855 if (!rpc_uci_match_section(uci_to_section(e),
856 tb[RPC_D_TYPE], tb[RPC_D_MATCH]))
857 continue;
858
859 ptr.s = NULL;
860 ptr.section = e->name;
861
862 if (tb[RPC_D_OPTIONS])
863 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
864 else
865 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
866 }
867 }
868
869 uci_save(cursor, p);
870 uci_unload(cursor, p);
871
872 return rpc_uci_status();
873 }
874
875 static int
876 rpc_uci_rename(struct ubus_context *ctx, struct ubus_object *obj,
877 struct ubus_request_data *req, const char *method,
878 struct blob_attr *msg)
879 {
880 struct blob_attr *tb[__RPC_R_MAX];
881 struct uci_package *p = NULL;
882 struct uci_ptr ptr = { 0 };
883
884 blobmsg_parse(rpc_uci_rename_policy, __RPC_R_MAX, tb,
885 blob_data(msg), blob_len(msg));
886
887 if (!tb[RPC_R_CONFIG] || !tb[RPC_R_SECTION] || !tb[RPC_R_NAME])
888 return UBUS_STATUS_INVALID_ARGUMENT;
889
890 if (!rpc_uci_write_access(tb[RPC_R_SESSION], tb[RPC_R_CONFIG]))
891 return UBUS_STATUS_PERMISSION_DENIED;
892
893 ptr.package = blobmsg_data(tb[RPC_R_CONFIG]);
894 ptr.section = blobmsg_data(tb[RPC_R_SECTION]);
895 ptr.value = blobmsg_data(tb[RPC_R_NAME]);
896
897 if (tb[RPC_R_OPTION])
898 ptr.option = blobmsg_data(tb[RPC_R_OPTION]);
899
900 if (uci_load(cursor, ptr.package, &p))
901 return rpc_uci_status();
902
903 if (uci_lookup_ptr(cursor, &ptr, NULL, true))
904 goto out;
905
906 if ((ptr.option && !ptr.o) || !ptr.s)
907 {
908 cursor->err = UCI_ERR_NOTFOUND;
909 goto out;
910 }
911
912 if (uci_rename(cursor, &ptr))
913 goto out;
914
915 uci_save(cursor, p);
916
917 out:
918 uci_unload(cursor, p);
919
920 return rpc_uci_status();
921 }
922
923 static int
924 rpc_uci_order(struct ubus_context *ctx, struct ubus_object *obj,
925 struct ubus_request_data *req, const char *method,
926 struct blob_attr *msg)
927 {
928 struct blob_attr *tb[__RPC_O_MAX];
929 struct blob_attr *cur;
930 struct uci_package *p = NULL;
931 struct uci_ptr ptr = { 0 };
932 int rem, i = 1;
933
934 blobmsg_parse(rpc_uci_order_policy, __RPC_O_MAX, tb,
935 blob_data(msg), blob_len(msg));
936
937 if (!tb[RPC_O_CONFIG] || !tb[RPC_O_SECTIONS])
938 return UBUS_STATUS_INVALID_ARGUMENT;
939
940 if (!rpc_uci_write_access(tb[RPC_O_SESSION], tb[RPC_O_CONFIG]))
941 return UBUS_STATUS_PERMISSION_DENIED;
942
943 ptr.package = blobmsg_data(tb[RPC_O_CONFIG]);
944
945 if (uci_load(cursor, ptr.package, &p))
946 return rpc_uci_status();
947
948 blobmsg_for_each_attr(cur, tb[RPC_O_SECTIONS], rem)
949 {
950 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
951 continue;
952
953 ptr.s = NULL;
954 ptr.section = blobmsg_data(cur);
955
956 if (uci_lookup_ptr(cursor, &ptr, NULL, true) || !ptr.s)
957 continue;
958
959 uci_reorder_section(cursor, ptr.s, i++);
960 }
961
962 uci_save(cursor, p);
963 uci_unload(cursor, p);
964
965 return rpc_uci_status();
966 }
967
968 static void
969 rpc_uci_dump_change(struct uci_delta *d)
970 {
971 void *c;
972 const char *types[] = {
973 [UCI_CMD_REORDER] = "order",
974 [UCI_CMD_REMOVE] = "remove",
975 [UCI_CMD_RENAME] = "rename",
976 [UCI_CMD_ADD] = "add",
977 [UCI_CMD_LIST_ADD] = "list-add",
978 [UCI_CMD_LIST_DEL] = "list-del",
979 [UCI_CMD_CHANGE] = "set",
980 };
981
982 if (!d->section)
983 return;
984
985 c = blobmsg_open_array(&buf, NULL);
986
987 blobmsg_add_string(&buf, NULL, types[d->cmd]);
988 blobmsg_add_string(&buf, NULL, d->section);
989
990 if (d->e.name)
991 blobmsg_add_string(&buf, NULL, d->e.name);
992
993 if (d->value)
994 {
995 if (d->cmd == UCI_CMD_REORDER)
996 blobmsg_add_u32(&buf, NULL, strtoul(d->value, NULL, 10));
997 else
998 blobmsg_add_string(&buf, NULL, d->value);
999 }
1000
1001 blobmsg_close_array(&buf, c);
1002 }
1003
1004 static int
1005 rpc_uci_changes(struct ubus_context *ctx, struct ubus_object *obj,
1006 struct ubus_request_data *req, const char *method,
1007 struct blob_attr *msg)
1008 {
1009 struct blob_attr *tb[__RPC_C_MAX];
1010 struct uci_package *p = NULL;
1011 struct uci_element *e;
1012 void *c;
1013
1014 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
1015 blob_data(msg), blob_len(msg));
1016
1017 if (!tb[RPC_C_CONFIG])
1018 return UBUS_STATUS_INVALID_ARGUMENT;
1019
1020 if (!rpc_uci_read_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
1021 return UBUS_STATUS_PERMISSION_DENIED;
1022
1023 if (uci_load(cursor, blobmsg_data(tb[RPC_C_CONFIG]), &p))
1024 return rpc_uci_status();
1025
1026 blob_buf_init(&buf, 0);
1027 c = blobmsg_open_array(&buf, "changes");
1028
1029 uci_foreach_element(&p->saved_delta, e)
1030 rpc_uci_dump_change(uci_to_delta(e));
1031
1032 blobmsg_close_array(&buf, c);
1033
1034 ubus_send_reply(ctx, req, buf.head);
1035
1036 uci_unload(cursor, p);
1037
1038 return rpc_uci_status();
1039 }
1040
1041 static void
1042 rpc_uci_trigger_event(struct ubus_context *ctx, const char *config)
1043 {
1044 char *pkg = strdup(config);
1045 static struct blob_buf b;
1046 uint32_t id;
1047
1048 if (!ubus_lookup_id(ctx, "service", &id)) {
1049 void *c;
1050
1051 blob_buf_init(&b, 0);
1052 blobmsg_add_string(&b, "type", "config.change");
1053 c = blobmsg_open_table(&b, "data");
1054 blobmsg_add_string(&b, "package", pkg);
1055 blobmsg_close_table(&b, c);
1056 ubus_invoke(ctx, id, "event", b.head, NULL, 0, 1000);
1057 }
1058 free(pkg);
1059 }
1060
1061 static int
1062 rpc_uci_revert_commit(struct ubus_context *ctx, struct blob_attr *msg, bool commit)
1063 {
1064 struct blob_attr *tb[__RPC_C_MAX];
1065 struct uci_package *p = NULL;
1066 struct uci_ptr ptr = { 0 };
1067
1068 if (!apply_running)
1069 return UBUS_STATUS_PERMISSION_DENIED;
1070
1071 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
1072 blob_data(msg), blob_len(msg));
1073
1074 if (!tb[RPC_C_CONFIG])
1075 return UBUS_STATUS_INVALID_ARGUMENT;
1076
1077 if (!rpc_uci_write_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
1078 return UBUS_STATUS_PERMISSION_DENIED;
1079
1080 ptr.package = blobmsg_data(tb[RPC_C_CONFIG]);
1081
1082 if (commit)
1083 {
1084 if (!uci_load(cursor, ptr.package, &p))
1085 {
1086 uci_commit(cursor, &p, false);
1087 uci_unload(cursor, p);
1088 }
1089 rpc_uci_trigger_event(ctx, blobmsg_get_string(tb[RPC_C_CONFIG]));
1090 }
1091 else
1092 {
1093 if (!uci_lookup_ptr(cursor, &ptr, NULL, true) && ptr.p)
1094 uci_revert(cursor, &ptr);
1095 }
1096
1097 return rpc_uci_status();
1098 }
1099
1100 static int
1101 rpc_uci_revert(struct ubus_context *ctx, struct ubus_object *obj,
1102 struct ubus_request_data *req, const char *method,
1103 struct blob_attr *msg)
1104 {
1105 return rpc_uci_revert_commit(ctx, msg, false);
1106 }
1107
1108 static int
1109 rpc_uci_commit(struct ubus_context *ctx, struct ubus_object *obj,
1110 struct ubus_request_data *req, const char *method,
1111 struct blob_attr *msg)
1112 {
1113 return rpc_uci_revert_commit(ctx, msg, true);
1114 }
1115
1116 static int
1117 rpc_uci_configs(struct ubus_context *ctx, struct ubus_object *obj,
1118 struct ubus_request_data *req, const char *method,
1119 struct blob_attr *msg)
1120 {
1121 char **configs;
1122 void *c;
1123 int i;
1124
1125 if (uci_list_configs(cursor, &configs))
1126 goto out;
1127
1128 blob_buf_init(&buf, 0);
1129
1130 c = blobmsg_open_array(&buf, "configs");
1131
1132 for (i = 0; configs[i]; i++)
1133 blobmsg_add_string(&buf, NULL, configs[i]);
1134
1135 blobmsg_close_array(&buf, c);
1136
1137 ubus_send_reply(ctx, req, buf.head);
1138
1139 out:
1140 return rpc_uci_status();
1141 }
1142
1143
1144 /*
1145 * Remove given delta save directory (if any).
1146 */
1147 static void
1148 rpc_uci_purge_dir(const char *path)
1149 {
1150 DIR *d;
1151 struct stat s;
1152 struct dirent *e;
1153 char file[PATH_MAX];
1154
1155 if (stat(path, &s) || !S_ISDIR(s.st_mode))
1156 return;
1157
1158 if ((d = opendir(path)) != NULL)
1159 {
1160 while ((e = readdir(d)) != NULL)
1161 {
1162 snprintf(file, sizeof(file) - 1, "%s/%s", path, e->d_name);
1163
1164 if (stat(file, &s) || !S_ISREG(s.st_mode))
1165 continue;
1166
1167 unlink(file);
1168 }
1169
1170 closedir(d);
1171
1172 rmdir(path);
1173 }
1174 }
1175
1176 static int
1177 rpc_uci_apply_config(struct ubus_context *ctx, char *config)
1178 {
1179 struct uci_package *p = NULL;
1180 struct uci_ptr ptr = { 0 };
1181
1182 ptr.package = config;
1183
1184 if (!uci_load(cursor, ptr.package, &p)) {
1185 uci_commit(cursor, &p, false);
1186 uci_unload(cursor, p);
1187 }
1188 rpc_uci_trigger_event(ctx, config);
1189
1190 return 0;
1191 }
1192
1193 static void
1194 rpc_uci_copy_file(const char *src, const char *target, const char *file)
1195 {
1196 char tmp[256];
1197 FILE *in, *out;
1198
1199 snprintf(tmp, sizeof(tmp), "%s%s", src, file);
1200 in = fopen(tmp, "rb");
1201 snprintf(tmp, sizeof(tmp), "%s%s", target, file);
1202 out = fopen(tmp, "wb+");
1203 if (in && out)
1204 while (!feof(in)) {
1205 int len = fread(tmp, 1, sizeof(tmp), in);
1206
1207 if(len > 0)
1208 fwrite(tmp, 1, len, out);
1209 }
1210 if(in)
1211 fclose(in);
1212 if(out)
1213 fclose(out);
1214 }
1215
1216 static void
1217 rpc_uci_do_rollback(struct ubus_context *ctx, const char *sid, glob_t *gl)
1218 {
1219 int i;
1220 char tmp[PATH_MAX];
1221
1222 if (sid) {
1223 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
1224 mkdir(tmp, 0700);
1225 }
1226
1227 for (i = 0; i < gl->gl_pathc; i++) {
1228 char *config = basename(gl->gl_pathv[i]);
1229
1230 if (*config == '.')
1231 continue;
1232
1233 rpc_uci_copy_file(RPC_SNAPSHOT_FILES, RPC_UCI_DIR, config);
1234 rpc_uci_apply_config(ctx, config);
1235 if (sid)
1236 rpc_uci_copy_file(RPC_SNAPSHOT_DELTA, tmp, config);
1237 }
1238
1239 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1240 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1241
1242 uloop_timeout_cancel(&apply_timer);
1243 apply_running = false;
1244 apply_ctx = NULL;
1245 }
1246
1247 static void
1248 rpc_uci_apply_timeout(struct uloop_timeout *t)
1249 {
1250 glob_t gl;
1251 char tmp[PATH_MAX];
1252
1253 snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
1254 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1255 return;
1256
1257 rpc_uci_do_rollback(apply_ctx, NULL, &gl);
1258 }
1259
1260 static int
1261 rpc_uci_apply_access(const char *sid, glob_t *gl)
1262 {
1263 struct stat s;
1264 int i, c = 0;
1265
1266 if (gl->gl_pathc < 3)
1267 return UBUS_STATUS_NO_DATA;
1268
1269 for (i = 0; i < gl->gl_pathc; i++) {
1270 char *config = basename(gl->gl_pathv[i]);
1271
1272 if (*config == '.')
1273 continue;
1274 if (stat(gl->gl_pathv[i], &s) || !s.st_size)
1275 continue;
1276 if (!rpc_session_access(sid, "uci", config, "write"))
1277 return UBUS_STATUS_PERMISSION_DENIED;
1278 c++;
1279 }
1280
1281 if (!c)
1282 return UBUS_STATUS_NO_DATA;
1283
1284 return 0;
1285 }
1286
1287 static int
1288 rpc_uci_apply(struct ubus_context *ctx, struct ubus_object *obj,
1289 struct ubus_request_data *req, const char *method,
1290 struct blob_attr *msg)
1291 {
1292 struct blob_attr *tb[__RPC_T_MAX];
1293 int timeout = RPC_APPLY_TIMEOUT;
1294 char tmp[PATH_MAX];
1295 bool commit = false;
1296 int ret, i;
1297 char *sid;
1298 glob_t gl;
1299
1300 blobmsg_parse(rpc_uci_apply_policy, __RPC_T_MAX, tb,
1301 blob_data(msg), blob_len(msg));
1302
1303 if (tb[RPC_T_COMMIT])
1304 commit = blobmsg_get_bool(tb[RPC_T_COMMIT]);
1305
1306 if (apply_running && !commit)
1307 return UBUS_STATUS_PERMISSION_DENIED;
1308
1309 if (!tb[RPC_T_SESSION])
1310 return UBUS_STATUS_INVALID_ARGUMENT;
1311
1312 sid = blobmsg_data(tb[RPC_T_SESSION]);
1313
1314 if (tb[RPC_T_TIMEOUT])
1315 timeout = blobmsg_get_u32(tb[RPC_T_TIMEOUT]);
1316
1317 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1318 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1319
1320 if (!apply_running) {
1321 mkdir(RPC_SNAPSHOT_FILES, 0700);
1322 mkdir(RPC_SNAPSHOT_DELTA, 0700);
1323
1324 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/*", sid);
1325 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1326 return -1;
1327
1328 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
1329
1330 ret = rpc_uci_apply_access(sid, &gl);
1331 if (ret) {
1332 globfree(&gl);
1333 return ret;
1334 }
1335
1336 for (i = 0; i < gl.gl_pathc; i++) {
1337 char *config = basename(gl.gl_pathv[i]);
1338 struct stat s;
1339
1340 if (*config == '.')
1341 continue;
1342
1343 if (stat(gl.gl_pathv[i], &s) || !s.st_size)
1344 continue;
1345
1346 rpc_uci_copy_file(RPC_UCI_DIR, RPC_SNAPSHOT_FILES, config);
1347 rpc_uci_copy_file(tmp, RPC_SNAPSHOT_DELTA, config);
1348 rpc_uci_apply_config(ctx, config);
1349 }
1350
1351 globfree(&gl);
1352
1353 apply_running = true;
1354 apply_timer.cb = rpc_uci_apply_timeout;
1355 uloop_timeout_set(&apply_timer, timeout * 1000);
1356 apply_ctx = ctx;
1357 }
1358
1359 if (apply_running && commit) {
1360 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1361 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1362
1363 uloop_timeout_cancel(&apply_timer);
1364 apply_running = false;
1365 apply_ctx = NULL;
1366 }
1367
1368 return 0;
1369 }
1370
1371 static int
1372 rpc_uci_rollback(struct ubus_context *ctx, struct ubus_object *obj,
1373 struct ubus_request_data *req, const char *method,
1374 struct blob_attr *msg)
1375 {
1376 struct blob_attr *tb[__RPC_B_MAX];
1377 char tmp[PATH_MAX];
1378 glob_t gl;
1379 char *sid;
1380 int ret;
1381
1382 blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
1383 blob_data(msg), blob_len(msg));
1384
1385 if (!apply_running)
1386 return UBUS_STATUS_PERMISSION_DENIED;
1387
1388 if (!tb[RPC_B_SESSION])
1389 return UBUS_STATUS_INVALID_ARGUMENT;
1390
1391 sid = blobmsg_data(tb[RPC_B_SESSION]);
1392
1393 snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
1394 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1395 return -1;
1396
1397 ret = rpc_uci_apply_access(sid, &gl);
1398 if (ret) {
1399 globfree(&gl);
1400 return ret;
1401 }
1402
1403 rpc_uci_do_rollback(ctx, sid, &gl);
1404
1405 globfree(&gl);
1406
1407 return 0;
1408 }
1409
1410
1411 /*
1412 * Session destroy callback to purge associated delta directory.
1413 */
1414 static void
1415 rpc_uci_purge_savedir_cb(struct rpc_session *ses, void *priv)
1416 {
1417 char path[PATH_MAX];
1418
1419 snprintf(path, sizeof(path) - 1, RPC_UCI_SAVEDIR_PREFIX "%s", ses->id);
1420 rpc_uci_purge_dir(path);
1421 }
1422
1423 /*
1424 * Removes all delta directories which match the RPC_UCI_SAVEDIR_PREFIX.
1425 * This is used to clean up garbage when starting rpcd.
1426 */
1427 void rpc_uci_purge_savedirs(void)
1428 {
1429 int i;
1430 glob_t gl;
1431
1432 if (!glob(RPC_UCI_SAVEDIR_PREFIX "*", 0, NULL, &gl))
1433 {
1434 for (i = 0; i < gl.gl_pathc; i++)
1435 rpc_uci_purge_dir(gl.gl_pathv[i]);
1436
1437 globfree(&gl);
1438 }
1439 }
1440
1441 int rpc_uci_api_init(struct ubus_context *ctx)
1442 {
1443 static const struct ubus_method uci_methods[] = {
1444 { .name = "configs", .handler = rpc_uci_configs },
1445 UBUS_METHOD("get", rpc_uci_get, rpc_uci_get_policy),
1446 UBUS_METHOD("add", rpc_uci_add, rpc_uci_add_policy),
1447 UBUS_METHOD("set", rpc_uci_set, rpc_uci_set_policy),
1448 UBUS_METHOD("delete", rpc_uci_delete, rpc_uci_delete_policy),
1449 UBUS_METHOD("rename", rpc_uci_rename, rpc_uci_rename_policy),
1450 UBUS_METHOD("order", rpc_uci_order, rpc_uci_order_policy),
1451 UBUS_METHOD("changes", rpc_uci_changes, rpc_uci_config_policy),
1452 UBUS_METHOD("revert", rpc_uci_revert, rpc_uci_config_policy),
1453 UBUS_METHOD("commit", rpc_uci_commit, rpc_uci_config_policy),
1454 UBUS_METHOD("apply", rpc_uci_apply, rpc_uci_apply_policy),
1455 UBUS_METHOD("rollback", rpc_uci_rollback, rpc_uci_rollback_policy),
1456 };
1457
1458 static struct ubus_object_type uci_type =
1459 UBUS_OBJECT_TYPE("luci-rpc-uci", uci_methods);
1460
1461 static struct ubus_object obj = {
1462 .name = "uci",
1463 .type = &uci_type,
1464 .methods = uci_methods,
1465 .n_methods = ARRAY_SIZE(uci_methods),
1466 };
1467
1468 static struct rpc_session_cb cb = {
1469 .cb = rpc_uci_purge_savedir_cb
1470 };
1471
1472 cursor = uci_alloc_context();
1473
1474 if (!cursor)
1475 return UBUS_STATUS_UNKNOWN_ERROR;
1476
1477 rpc_session_destroy_cb(&cb);
1478
1479 return ubus_add_object(ctx, &obj);
1480 }