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