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