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