session: use correct policy for rpc_handle_login()
[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 uci_load(cursor, ptr.package, &p);
542
543 if (!p)
544 goto out;
545
546 if (tb[RPC_G_SECTION])
547 {
548 ptr.section = blobmsg_data(tb[RPC_G_SECTION]);
549
550 if (tb[RPC_G_OPTION])
551 ptr.option = blobmsg_data(tb[RPC_G_OPTION]);
552 }
553
554 if (rpc_uci_lookup(&ptr) || !(ptr.flags & UCI_LOOKUP_COMPLETE))
555 goto out;
556
557 blob_buf_init(&buf, 0);
558
559 switch (ptr.last->type)
560 {
561 case UCI_TYPE_PACKAGE:
562 rpc_uci_dump_package(ptr.p, "values", tb[RPC_G_TYPE], tb[RPC_G_MATCH]);
563 break;
564
565 case UCI_TYPE_SECTION:
566 rpc_uci_dump_section(ptr.s, "values", -1);
567 break;
568
569 case UCI_TYPE_OPTION:
570 rpc_uci_dump_option(ptr.o, "value");
571 break;
572
573 default:
574 break;
575 }
576
577 ubus_send_reply(ctx, req, buf.head);
578
579 out:
580 if (p)
581 uci_unload(cursor, p);
582
583 return rpc_uci_status();
584 }
585
586 static int
587 rpc_uci_add(struct ubus_context *ctx, struct ubus_object *obj,
588 struct ubus_request_data *req, const char *method,
589 struct blob_attr *msg)
590 {
591 struct blob_attr *tb[__RPC_A_MAX];
592 struct blob_attr *cur, *elem;
593 struct uci_package *p = NULL;
594 struct uci_section *s;
595 struct uci_ptr ptr = { 0 };
596 int rem, rem2;
597
598 blobmsg_parse(rpc_uci_add_policy, __RPC_A_MAX, tb,
599 blob_data(msg), blob_len(msg));
600
601 if (!tb[RPC_A_CONFIG] || !tb[RPC_A_TYPE])
602 return UBUS_STATUS_INVALID_ARGUMENT;
603
604 if (!rpc_uci_write_access(tb[RPC_A_SESSION], tb[RPC_A_CONFIG]))
605 return UBUS_STATUS_PERMISSION_DENIED;
606
607 ptr.package = blobmsg_data(tb[RPC_A_CONFIG]);
608
609 uci_load(cursor, ptr.package, &p);
610
611 if (!p)
612 goto out;
613
614 /* add named section */
615 if (tb[RPC_A_NAME])
616 {
617 ptr.section = blobmsg_data(tb[RPC_A_NAME]);
618 ptr.value = blobmsg_data(tb[RPC_A_TYPE]);
619 ptr.option = NULL;
620
621 if (rpc_uci_lookup(&ptr) || uci_set(cursor, &ptr))
622 goto out;
623 }
624
625 /* add anon section */
626 else
627 {
628 if (uci_add_section(cursor, p, blobmsg_data(tb[RPC_A_TYPE]), &s) || !s)
629 goto out;
630
631 ptr.section = s->e.name;
632 }
633
634 if (tb[RPC_A_VALUES])
635 {
636 blobmsg_for_each_attr(cur, tb[RPC_A_VALUES], rem)
637 {
638 ptr.o = NULL;
639 ptr.option = blobmsg_name(cur);
640
641 if (rpc_uci_lookup(&ptr) || !ptr.s)
642 continue;
643
644 switch (blobmsg_type(cur))
645 {
646 case BLOBMSG_TYPE_ARRAY:
647 blobmsg_for_each_attr(elem, cur, rem2)
648 if (rpc_uci_format_blob(elem, &ptr.value))
649 uci_add_list(cursor, &ptr);
650 break;
651
652 default:
653 if (rpc_uci_format_blob(cur, &ptr.value))
654 uci_set(cursor, &ptr);
655 break;
656 }
657 }
658 }
659
660 uci_save(cursor, p);
661
662 blob_buf_init(&buf, 0);
663 blobmsg_add_string(&buf, "section", ptr.section);
664 ubus_send_reply(ctx, req, buf.head);
665
666 out:
667 if (p)
668 uci_unload(cursor, p);
669
670 return rpc_uci_status();
671 }
672
673 /*
674 * Turn value from a blob attribute into uci set operation
675 * 1) if the blob is of type array, delete existing option (if any) and
676 * emit uci add_list operations for each element
677 * 2) if the blob is not an array but an option of type list exists,
678 * delete existing list and emit uci set operation for the blob value
679 * 3) in all other cases only emit a set operation if there is no existing
680 * option of if the existing options value differs from the blob value
681 */
682 static void
683 rpc_uci_merge_set(struct blob_attr *opt, struct uci_ptr *ptr)
684 {
685 struct blob_attr *cur;
686 int rem;
687
688 ptr->o = NULL;
689 ptr->option = blobmsg_name(opt);
690
691 if (rpc_uci_lookup(ptr) || !ptr->s)
692 return;
693
694 if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
695 {
696 if (ptr->o)
697 uci_delete(cursor, ptr);
698
699 blobmsg_for_each_attr(cur, opt, rem)
700 if (rpc_uci_format_blob(cur, &ptr->value))
701 uci_add_list(cursor, ptr);
702 }
703 else if (ptr->o && ptr->o->type == UCI_TYPE_LIST)
704 {
705 uci_delete(cursor, ptr);
706
707 if (rpc_uci_format_blob(opt, &ptr->value))
708 uci_set(cursor, ptr);
709 }
710 else if (rpc_uci_format_blob(opt, &ptr->value))
711 {
712 if (!ptr->o || !ptr->o->v.string || strcmp(ptr->o->v.string, ptr->value))
713 uci_set(cursor, ptr);
714 }
715 }
716
717 static int
718 rpc_uci_set(struct ubus_context *ctx, struct ubus_object *obj,
719 struct ubus_request_data *req, const char *method,
720 struct blob_attr *msg)
721 {
722 struct blob_attr *tb[__RPC_S_MAX];
723 struct blob_attr *cur;
724 struct uci_package *p = NULL;
725 struct uci_element *e;
726 struct uci_ptr ptr = { 0 };
727 int rem;
728
729 blobmsg_parse(rpc_uci_set_policy, __RPC_S_MAX, tb,
730 blob_data(msg), blob_len(msg));
731
732 if (!tb[RPC_S_CONFIG] || !tb[RPC_S_VALUES] ||
733 (!tb[RPC_S_SECTION] && !tb[RPC_S_TYPE] && !tb[RPC_S_MATCH]))
734 return UBUS_STATUS_INVALID_ARGUMENT;
735
736 if (!rpc_uci_write_access(tb[RPC_S_SESSION], tb[RPC_S_CONFIG]))
737 return UBUS_STATUS_PERMISSION_DENIED;
738
739 ptr.package = blobmsg_data(tb[RPC_S_CONFIG]);
740 uci_load(cursor, ptr.package, &p);
741
742 if (!p)
743 goto out;
744
745 if (tb[RPC_S_SECTION])
746 {
747 ptr.section = blobmsg_data(tb[RPC_S_SECTION]);
748 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
749 rpc_uci_merge_set(cur, &ptr);
750 }
751 else
752 {
753 uci_foreach_element(&p->sections, e)
754 {
755 if (!rpc_uci_match_section(uci_to_section(e),
756 tb[RPC_S_TYPE], tb[RPC_S_MATCH]))
757 continue;
758
759 ptr.s = NULL;
760 ptr.section = e->name;
761
762 blobmsg_for_each_attr(cur, tb[RPC_S_VALUES], rem)
763 rpc_uci_merge_set(cur, &ptr);
764 }
765 }
766
767 uci_save(cursor, p);
768
769 out:
770 if (p)
771 uci_unload(cursor, p);
772
773 return rpc_uci_status();
774 }
775
776 /*
777 * Delete option or section from uci specified by given blob attribute pointer
778 * 1) if the blob is of type array, delete any option named after each element
779 * 2) if the blob is of type string, delete the option named after its value
780 * 3) if the blob is NULL, delete entire section
781 */
782 static void
783 rpc_uci_merge_delete(struct blob_attr *opt, struct uci_ptr *ptr)
784 {
785 struct blob_attr *cur;
786 int rem;
787
788 if (rpc_uci_lookup(ptr) || !ptr->s)
789 return;
790
791 if (!opt)
792 {
793 ptr->o = NULL;
794 ptr->option = NULL;
795
796 uci_delete(cursor, ptr);
797 }
798 else if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
799 {
800 blobmsg_for_each_attr(cur, opt, rem)
801 {
802 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
803 continue;
804
805 ptr->o = NULL;
806 ptr->option = blobmsg_data(cur);
807
808 if (rpc_uci_lookup(ptr) || !ptr->o)
809 continue;
810
811 uci_delete(cursor, ptr);
812 }
813 }
814 else if (blobmsg_type(opt) == BLOBMSG_TYPE_STRING)
815 {
816 ptr->o = NULL;
817 ptr->option = blobmsg_data(opt);
818
819 if (rpc_uci_lookup(ptr) || !ptr->o)
820 return;
821
822 uci_delete(cursor, ptr);
823 }
824 }
825
826 static int
827 rpc_uci_delete(struct ubus_context *ctx, struct ubus_object *obj,
828 struct ubus_request_data *req, const char *method,
829 struct blob_attr *msg)
830 {
831 struct blob_attr *tb[__RPC_D_MAX];
832 struct uci_package *p = NULL;
833 struct uci_element *e, *tmp;
834 struct uci_ptr ptr = { 0 };
835
836 blobmsg_parse(rpc_uci_delete_policy, __RPC_D_MAX, tb,
837 blob_data(msg), blob_len(msg));
838
839 if (!tb[RPC_D_CONFIG] ||
840 (!tb[RPC_D_SECTION] && !tb[RPC_D_TYPE] && !tb[RPC_D_MATCH]))
841 return UBUS_STATUS_INVALID_ARGUMENT;
842
843 if (!rpc_uci_write_access(tb[RPC_D_SESSION], tb[RPC_D_CONFIG]))
844 return UBUS_STATUS_PERMISSION_DENIED;
845
846 ptr.package = blobmsg_data(tb[RPC_D_CONFIG]);
847 uci_load(cursor, ptr.package, &p);
848
849 if (!p)
850 goto out;
851
852 if (tb[RPC_D_SECTION])
853 {
854 ptr.section = blobmsg_data(tb[RPC_D_SECTION]);
855
856 if (tb[RPC_D_OPTIONS])
857 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
858 else
859 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
860 }
861 else
862 {
863 uci_foreach_element_safe(&p->sections, tmp, e)
864 {
865 if (!rpc_uci_match_section(uci_to_section(e),
866 tb[RPC_D_TYPE], tb[RPC_D_MATCH]))
867 continue;
868
869 ptr.s = NULL;
870 ptr.section = e->name;
871
872 if (tb[RPC_D_OPTIONS])
873 rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
874 else
875 rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
876 }
877 }
878
879 uci_save(cursor, p);
880
881 out:
882 if (p)
883 uci_unload(cursor, p);
884
885 return rpc_uci_status();
886 }
887
888 static int
889 rpc_uci_rename(struct ubus_context *ctx, struct ubus_object *obj,
890 struct ubus_request_data *req, const char *method,
891 struct blob_attr *msg)
892 {
893 struct blob_attr *tb[__RPC_R_MAX];
894 struct uci_package *p = NULL;
895 struct uci_ptr ptr = { 0 };
896
897 blobmsg_parse(rpc_uci_rename_policy, __RPC_R_MAX, tb,
898 blob_data(msg), blob_len(msg));
899
900 if (!tb[RPC_R_CONFIG] || !tb[RPC_R_SECTION] || !tb[RPC_R_NAME])
901 return UBUS_STATUS_INVALID_ARGUMENT;
902
903 if (!rpc_uci_write_access(tb[RPC_R_SESSION], tb[RPC_R_CONFIG]))
904 return UBUS_STATUS_PERMISSION_DENIED;
905
906 ptr.package = blobmsg_data(tb[RPC_R_CONFIG]);
907 ptr.section = blobmsg_data(tb[RPC_R_SECTION]);
908 ptr.value = blobmsg_data(tb[RPC_R_NAME]);
909
910 if (tb[RPC_R_OPTION])
911 ptr.option = blobmsg_data(tb[RPC_R_OPTION]);
912
913 uci_load(cursor, ptr.package, &p);
914
915 if (!p)
916 goto out;
917
918 if (uci_lookup_ptr(cursor, &ptr, NULL, true))
919 goto out;
920
921 if ((ptr.option && !ptr.o) || !ptr.s)
922 {
923 cursor->err = UCI_ERR_NOTFOUND;
924 goto out;
925 }
926
927 if (uci_rename(cursor, &ptr))
928 goto out;
929
930 uci_save(cursor, p);
931
932 out:
933 if (p)
934 uci_unload(cursor, p);
935
936 return rpc_uci_status();
937 }
938
939 static int
940 rpc_uci_order(struct ubus_context *ctx, struct ubus_object *obj,
941 struct ubus_request_data *req, const char *method,
942 struct blob_attr *msg)
943 {
944 struct blob_attr *tb[__RPC_O_MAX];
945 struct blob_attr *cur;
946 struct uci_package *p = NULL;
947 struct uci_ptr ptr = { 0 };
948 int rem, i = 1;
949
950 blobmsg_parse(rpc_uci_order_policy, __RPC_O_MAX, tb,
951 blob_data(msg), blob_len(msg));
952
953 if (!tb[RPC_O_CONFIG] || !tb[RPC_O_SECTIONS])
954 return UBUS_STATUS_INVALID_ARGUMENT;
955
956 if (!rpc_uci_write_access(tb[RPC_O_SESSION], tb[RPC_O_CONFIG]))
957 return UBUS_STATUS_PERMISSION_DENIED;
958
959 ptr.package = blobmsg_data(tb[RPC_O_CONFIG]);
960
961 uci_load(cursor, ptr.package, &p);
962
963 if (!p)
964 goto out;
965
966 blobmsg_for_each_attr(cur, tb[RPC_O_SECTIONS], rem)
967 {
968 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
969 continue;
970
971 ptr.s = NULL;
972 ptr.section = blobmsg_data(cur);
973
974 if (uci_lookup_ptr(cursor, &ptr, NULL, true) || !ptr.s)
975 continue;
976
977 uci_reorder_section(cursor, ptr.s, i++);
978 }
979
980 uci_save(cursor, p);
981
982 out:
983 if (p)
984 uci_unload(cursor, p);
985
986 return rpc_uci_status();
987 }
988
989 static void
990 rpc_uci_dump_change(struct uci_delta *d)
991 {
992 void *c;
993 const char *types[] = {
994 [UCI_CMD_REORDER] = "order",
995 [UCI_CMD_REMOVE] = "remove",
996 [UCI_CMD_RENAME] = "rename",
997 [UCI_CMD_ADD] = "add",
998 [UCI_CMD_LIST_ADD] = "list-add",
999 [UCI_CMD_LIST_DEL] = "list-del",
1000 [UCI_CMD_CHANGE] = "set",
1001 };
1002
1003 if (!d->section)
1004 return;
1005
1006 c = blobmsg_open_array(&buf, NULL);
1007
1008 blobmsg_add_string(&buf, NULL, types[d->cmd]);
1009 blobmsg_add_string(&buf, NULL, d->section);
1010
1011 if (d->e.name)
1012 blobmsg_add_string(&buf, NULL, d->e.name);
1013
1014 if (d->value)
1015 {
1016 if (d->cmd == UCI_CMD_REORDER)
1017 blobmsg_add_u32(&buf, NULL, strtoul(d->value, NULL, 10));
1018 else
1019 blobmsg_add_string(&buf, NULL, d->value);
1020 }
1021
1022 blobmsg_close_array(&buf, c);
1023 }
1024
1025 static int
1026 rpc_uci_changes(struct ubus_context *ctx, struct ubus_object *obj,
1027 struct ubus_request_data *req, const char *method,
1028 struct blob_attr *msg)
1029 {
1030 struct blob_attr *tb[__RPC_C_MAX];
1031 struct uci_package *p = NULL;
1032 struct uci_element *e;
1033 void *c;
1034
1035 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
1036 blob_data(msg), blob_len(msg));
1037
1038 if (!tb[RPC_C_CONFIG])
1039 return UBUS_STATUS_INVALID_ARGUMENT;
1040
1041 if (!rpc_uci_read_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
1042 return UBUS_STATUS_PERMISSION_DENIED;
1043
1044 uci_load(cursor, blobmsg_data(tb[RPC_C_CONFIG]), &p);
1045
1046 if (!p)
1047 goto out;
1048
1049 blob_buf_init(&buf, 0);
1050 c = blobmsg_open_array(&buf, "changes");
1051
1052 uci_foreach_element(&p->saved_delta, e)
1053 rpc_uci_dump_change(uci_to_delta(e));
1054
1055 blobmsg_close_array(&buf, c);
1056
1057 ubus_send_reply(ctx, req, buf.head);
1058
1059 out:
1060 if (p)
1061 uci_unload(cursor, p);
1062
1063 return rpc_uci_status();
1064 }
1065
1066 static void
1067 rpc_uci_trigger_event(struct ubus_context *ctx, const char *config)
1068 {
1069 char *pkg = strdup(config);
1070 static struct blob_buf b;
1071 uint32_t id;
1072
1073 if (!ubus_lookup_id(ctx, "service", &id)) {
1074 void *c;
1075
1076 blob_buf_init(&b, 0);
1077 blobmsg_add_string(&b, "type", "config.change");
1078 c = blobmsg_open_table(&b, "data");
1079 blobmsg_add_string(&b, "package", pkg);
1080 blobmsg_close_table(&b, c);
1081 ubus_invoke(ctx, id, "event", b.head, NULL, 0, 1000);
1082 }
1083 free(pkg);
1084 }
1085
1086 static int
1087 rpc_uci_revert_commit(struct ubus_context *ctx, struct blob_attr *msg, bool commit)
1088 {
1089 struct blob_attr *tb[__RPC_C_MAX];
1090 struct uci_package *p = NULL;
1091 struct uci_ptr ptr = { 0 };
1092
1093 if (!apply_running)
1094 return UBUS_STATUS_PERMISSION_DENIED;
1095
1096 blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
1097 blob_data(msg), blob_len(msg));
1098
1099 if (!tb[RPC_C_CONFIG])
1100 return UBUS_STATUS_INVALID_ARGUMENT;
1101
1102 if (!rpc_uci_write_access(tb[RPC_C_SESSION], tb[RPC_C_CONFIG]))
1103 return UBUS_STATUS_PERMISSION_DENIED;
1104
1105 ptr.package = blobmsg_data(tb[RPC_C_CONFIG]);
1106
1107 if (commit)
1108 {
1109 uci_load(cursor, ptr.package, &p);
1110
1111 if (p)
1112 {
1113 uci_commit(cursor, &p, false);
1114 uci_unload(cursor, p);
1115 }
1116 rpc_uci_trigger_event(ctx, blobmsg_get_string(tb[RPC_C_CONFIG]));
1117 }
1118 else
1119 {
1120 if (!uci_lookup_ptr(cursor, &ptr, NULL, true) && ptr.p)
1121 uci_revert(cursor, &ptr);
1122 }
1123
1124 return rpc_uci_status();
1125 }
1126
1127 static int
1128 rpc_uci_revert(struct ubus_context *ctx, struct ubus_object *obj,
1129 struct ubus_request_data *req, const char *method,
1130 struct blob_attr *msg)
1131 {
1132 return rpc_uci_revert_commit(ctx, msg, false);
1133 }
1134
1135 static int
1136 rpc_uci_commit(struct ubus_context *ctx, struct ubus_object *obj,
1137 struct ubus_request_data *req, const char *method,
1138 struct blob_attr *msg)
1139 {
1140 return rpc_uci_revert_commit(ctx, msg, true);
1141 }
1142
1143 static int
1144 rpc_uci_configs(struct ubus_context *ctx, struct ubus_object *obj,
1145 struct ubus_request_data *req, const char *method,
1146 struct blob_attr *msg)
1147 {
1148 char **configs;
1149 void *c;
1150 int i;
1151
1152 if (uci_list_configs(cursor, &configs))
1153 goto out;
1154
1155 blob_buf_init(&buf, 0);
1156
1157 c = blobmsg_open_array(&buf, "configs");
1158
1159 for (i = 0; configs[i]; i++)
1160 blobmsg_add_string(&buf, NULL, configs[i]);
1161
1162 blobmsg_close_array(&buf, c);
1163
1164 ubus_send_reply(ctx, req, buf.head);
1165
1166 out:
1167 return rpc_uci_status();
1168 }
1169
1170
1171 /*
1172 * Remove given delta save directory (if any).
1173 */
1174 static void
1175 rpc_uci_purge_dir(const char *path)
1176 {
1177 DIR *d;
1178 struct stat s;
1179 struct dirent *e;
1180 char file[PATH_MAX];
1181
1182 if (stat(path, &s) || !S_ISDIR(s.st_mode))
1183 return;
1184
1185 if ((d = opendir(path)) != NULL)
1186 {
1187 while ((e = readdir(d)) != NULL)
1188 {
1189 snprintf(file, sizeof(file) - 1, "%s/%s", path, e->d_name);
1190
1191 if (stat(file, &s) || !S_ISREG(s.st_mode))
1192 continue;
1193
1194 unlink(file);
1195 }
1196
1197 closedir(d);
1198
1199 rmdir(path);
1200 }
1201 }
1202
1203 static int
1204 rpc_uci_apply_config(struct ubus_context *ctx, char *config)
1205 {
1206 struct uci_package *p = NULL;
1207 struct uci_ptr ptr = { 0 };
1208
1209 ptr.package = config;
1210 uci_load(cursor, ptr.package, &p);
1211
1212 if (p) {
1213 uci_commit(cursor, &p, false);
1214 uci_unload(cursor, p);
1215 }
1216 rpc_uci_trigger_event(ctx, config);
1217
1218 return 0;
1219 }
1220
1221 static void
1222 rpc_uci_copy_file(const char *src, const char *target, const char *file)
1223 {
1224 char tmp[256];
1225 FILE *in, *out;
1226
1227 snprintf(tmp, sizeof(tmp), "%s%s", src, file);
1228 in = fopen(tmp, "rb");
1229 snprintf(tmp, sizeof(tmp), "%s%s", target, file);
1230 out = fopen(tmp, "wb+");
1231 if (in && out)
1232 while (!feof(in)) {
1233 int len = fread(tmp, 1, sizeof(tmp), in);
1234
1235 if(len > 0)
1236 fwrite(tmp, 1, len, out);
1237 }
1238 if(in)
1239 fclose(in);
1240 if(out)
1241 fclose(out);
1242 }
1243
1244 static void
1245 rpc_uci_do_rollback(struct ubus_context *ctx, const char *sid, glob_t *gl)
1246 {
1247 int i;
1248 char tmp[PATH_MAX];
1249
1250 if (sid) {
1251 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
1252 mkdir(tmp, 0700);
1253 }
1254
1255 for (i = 0; i < gl->gl_pathc; i++) {
1256 char *config = basename(gl->gl_pathv[i]);
1257
1258 if (*config == '.')
1259 continue;
1260
1261 rpc_uci_copy_file(RPC_SNAPSHOT_FILES, RPC_UCI_DIR, config);
1262 rpc_uci_apply_config(ctx, config);
1263 if (sid)
1264 rpc_uci_copy_file(RPC_SNAPSHOT_DELTA, tmp, config);
1265 }
1266
1267 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1268 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1269
1270 uloop_timeout_cancel(&apply_timer);
1271 apply_running = false;
1272 apply_ctx = NULL;
1273 }
1274
1275 static void
1276 rpc_uci_apply_timeout(struct uloop_timeout *t)
1277 {
1278 glob_t gl;
1279 char tmp[PATH_MAX];
1280
1281 snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
1282 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1283 return;
1284
1285 rpc_uci_do_rollback(apply_ctx, NULL, &gl);
1286 }
1287
1288 static int
1289 rpc_uci_apply_access(const char *sid, glob_t *gl)
1290 {
1291 struct stat s;
1292 int i, c = 0;
1293
1294 if (gl->gl_pathc < 3)
1295 return UBUS_STATUS_NO_DATA;
1296
1297 for (i = 0; i < gl->gl_pathc; i++) {
1298 char *config = basename(gl->gl_pathv[i]);
1299
1300 if (*config == '.')
1301 continue;
1302 if (stat(gl->gl_pathv[i], &s) || !s.st_size)
1303 continue;
1304 if (!rpc_session_access(sid, "uci", config, "write"))
1305 return UBUS_STATUS_PERMISSION_DENIED;
1306 c++;
1307 }
1308
1309 if (!c)
1310 return UBUS_STATUS_NO_DATA;
1311
1312 return 0;
1313 }
1314
1315 static int
1316 rpc_uci_apply(struct ubus_context *ctx, struct ubus_object *obj,
1317 struct ubus_request_data *req, const char *method,
1318 struct blob_attr *msg)
1319 {
1320 struct blob_attr *tb[__RPC_T_MAX];
1321 int timeout = RPC_APPLY_TIMEOUT;
1322 char tmp[PATH_MAX];
1323 bool commit = false;
1324 int ret, i;
1325 char *sid;
1326 glob_t gl;
1327
1328 blobmsg_parse(rpc_uci_apply_policy, __RPC_T_MAX, tb,
1329 blob_data(msg), blob_len(msg));
1330
1331 if (tb[RPC_T_COMMIT])
1332 commit = blobmsg_get_bool(tb[RPC_T_COMMIT]);
1333
1334 if (apply_running && !commit)
1335 return UBUS_STATUS_PERMISSION_DENIED;
1336
1337 if (!tb[RPC_T_SESSION])
1338 return UBUS_STATUS_INVALID_ARGUMENT;
1339
1340 sid = blobmsg_data(tb[RPC_T_SESSION]);
1341
1342 if (tb[RPC_T_TIMEOUT])
1343 timeout = blobmsg_get_u32(tb[RPC_T_TIMEOUT]);
1344
1345 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1346 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1347
1348 if (!apply_running) {
1349 mkdir(RPC_SNAPSHOT_FILES, 0700);
1350 mkdir(RPC_SNAPSHOT_DELTA, 0700);
1351
1352 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/*", sid);
1353 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1354 return -1;
1355
1356 snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
1357
1358 ret = rpc_uci_apply_access(sid, &gl);
1359 if (ret) {
1360 globfree(&gl);
1361 return ret;
1362 }
1363
1364 for (i = 0; i < gl.gl_pathc; i++) {
1365 char *config = basename(gl.gl_pathv[i]);
1366 struct stat s;
1367
1368 if (*config == '.')
1369 continue;
1370
1371 if (stat(gl.gl_pathv[i], &s) || !s.st_size)
1372 continue;
1373
1374 rpc_uci_copy_file(RPC_UCI_DIR, RPC_SNAPSHOT_FILES, config);
1375 rpc_uci_copy_file(tmp, RPC_SNAPSHOT_DELTA, config);
1376 rpc_uci_apply_config(ctx, config);
1377 }
1378
1379 globfree(&gl);
1380
1381 apply_running = true;
1382 apply_timer.cb = rpc_uci_apply_timeout;
1383 uloop_timeout_set(&apply_timer, timeout * 1000);
1384 apply_ctx = ctx;
1385 }
1386
1387 if (apply_running && commit) {
1388 rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
1389 rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
1390
1391 uloop_timeout_cancel(&apply_timer);
1392 apply_running = false;
1393 apply_ctx = NULL;
1394 }
1395
1396 return 0;
1397 }
1398
1399 static int
1400 rpc_uci_rollback(struct ubus_context *ctx, struct ubus_object *obj,
1401 struct ubus_request_data *req, const char *method,
1402 struct blob_attr *msg)
1403 {
1404 struct blob_attr *tb[__RPC_B_MAX];
1405 char tmp[PATH_MAX];
1406 glob_t gl;
1407 char *sid;
1408 int ret;
1409
1410 blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
1411 blob_data(msg), blob_len(msg));
1412
1413 if (!apply_running)
1414 return UBUS_STATUS_PERMISSION_DENIED;
1415
1416 if (!tb[RPC_B_SESSION])
1417 return UBUS_STATUS_INVALID_ARGUMENT;
1418
1419 sid = blobmsg_data(tb[RPC_B_SESSION]);
1420
1421 snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
1422 if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
1423 return -1;
1424
1425 ret = rpc_uci_apply_access(sid, &gl);
1426 if (ret) {
1427 globfree(&gl);
1428 return ret;
1429 }
1430
1431 rpc_uci_do_rollback(ctx, sid, &gl);
1432
1433 globfree(&gl);
1434
1435 return 0;
1436 }
1437
1438
1439 /*
1440 * Session destroy callback to purge associated delta directory.
1441 */
1442 static void
1443 rpc_uci_purge_savedir_cb(struct rpc_session *ses, void *priv)
1444 {
1445 char path[PATH_MAX];
1446
1447 snprintf(path, sizeof(path) - 1, RPC_UCI_SAVEDIR_PREFIX "%s", ses->id);
1448 rpc_uci_purge_dir(path);
1449 }
1450
1451 /*
1452 * Removes all delta directories which match the RPC_UCI_SAVEDIR_PREFIX.
1453 * This is used to clean up garbage when starting rpcd.
1454 */
1455 void rpc_uci_purge_savedirs(void)
1456 {
1457 int i;
1458 glob_t gl;
1459
1460 if (!glob(RPC_UCI_SAVEDIR_PREFIX "*", 0, NULL, &gl))
1461 {
1462 for (i = 0; i < gl.gl_pathc; i++)
1463 rpc_uci_purge_dir(gl.gl_pathv[i]);
1464
1465 globfree(&gl);
1466 }
1467 }
1468
1469 int rpc_uci_api_init(struct ubus_context *ctx)
1470 {
1471 static const struct ubus_method uci_methods[] = {
1472 { .name = "configs", .handler = rpc_uci_configs },
1473 UBUS_METHOD("get", rpc_uci_get, rpc_uci_get_policy),
1474 UBUS_METHOD("add", rpc_uci_add, rpc_uci_add_policy),
1475 UBUS_METHOD("set", rpc_uci_set, rpc_uci_set_policy),
1476 UBUS_METHOD("delete", rpc_uci_delete, rpc_uci_delete_policy),
1477 UBUS_METHOD("rename", rpc_uci_rename, rpc_uci_rename_policy),
1478 UBUS_METHOD("order", rpc_uci_order, rpc_uci_order_policy),
1479 UBUS_METHOD("changes", rpc_uci_changes, rpc_uci_config_policy),
1480 UBUS_METHOD("revert", rpc_uci_revert, rpc_uci_config_policy),
1481 UBUS_METHOD("commit", rpc_uci_commit, rpc_uci_config_policy),
1482 UBUS_METHOD("apply", rpc_uci_apply, rpc_uci_apply_policy),
1483 UBUS_METHOD("rollback", rpc_uci_rollback, rpc_uci_rollback_policy),
1484 };
1485
1486 static struct ubus_object_type uci_type =
1487 UBUS_OBJECT_TYPE("luci-rpc-uci", uci_methods);
1488
1489 static struct ubus_object obj = {
1490 .name = "uci",
1491 .type = &uci_type,
1492 .methods = uci_methods,
1493 .n_methods = ARRAY_SIZE(uci_methods),
1494 };
1495
1496 static struct rpc_session_cb cb = {
1497 .cb = rpc_uci_purge_savedir_cb
1498 };
1499
1500 cursor = uci_alloc_context();
1501
1502 if (!cursor)
1503 return UBUS_STATUS_UNKNOWN_ERROR;
1504
1505 rpc_session_destroy_cb(&cb);
1506
1507 return ubus_add_object(ctx, &obj);
1508 }