fix wrong buffer allocation
[project/uci.git] / list.c
1 /*
2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 /* initialize a list head/item */
16 static inline void uci_list_init(struct uci_list *ptr)
17 {
18 ptr->prev = ptr;
19 ptr->next = ptr;
20 }
21
22 /* inserts a new list entry after a given entry */
23 static inline void uci_list_insert(struct uci_list *list, struct uci_list *ptr)
24 {
25 list->next->prev = ptr;
26 ptr->prev = list;
27 ptr->next = list->next;
28 list->next = ptr;
29 }
30
31 /* inserts a new list entry at the tail of the list */
32 static inline void uci_list_add(struct uci_list *head, struct uci_list *ptr)
33 {
34 /* NB: head->prev points at the tail */
35 uci_list_insert(head->prev, ptr);
36 }
37
38 static inline void uci_list_del(struct uci_list *ptr)
39 {
40 struct uci_list *next, *prev;
41
42 next = ptr->next;
43 prev = ptr->prev;
44
45 prev->next = next;
46 next->prev = prev;
47
48 uci_list_init(ptr);
49 }
50
51 /*
52 * uci_alloc_generic allocates a new uci_element with payload
53 * payload is appended to the struct to save memory and reduce fragmentation
54 */
55 static struct uci_element *
56 uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
57 {
58 struct uci_element *e;
59 int datalen = size;
60 void *ptr;
61
62 ptr = uci_malloc(ctx, datalen);
63 e = (struct uci_element *) ptr;
64 e->type = type;
65 if (name) {
66 UCI_TRAP_SAVE(ctx, error);
67 e->name = uci_strdup(ctx, name);
68 UCI_TRAP_RESTORE(ctx);
69 }
70 uci_list_init(&e->list);
71 goto done;
72
73 error:
74 free(ptr);
75 UCI_THROW(ctx, ctx->err);
76
77 done:
78 return e;
79 }
80
81 static void
82 uci_free_element(struct uci_element *e)
83 {
84 if (e->name)
85 free(e->name);
86 if (!uci_list_empty(&e->list))
87 uci_list_del(&e->list);
88 free(e);
89 }
90
91 static struct uci_option *
92 uci_alloc_option(struct uci_section *s, const char *name, const char *value)
93 {
94 struct uci_package *p = s->package;
95 struct uci_context *ctx = p->ctx;
96 struct uci_option *o;
97
98 o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
99 o->type = UCI_TYPE_STRING;
100 o->v.string = uci_dataptr(o);
101 o->section = s;
102 strcpy(o->v.string, value);
103 uci_list_add(&s->options, &o->e.list);
104
105 return o;
106 }
107
108 static inline void
109 uci_free_option(struct uci_option *o)
110 {
111 struct uci_element *e, *tmp;
112
113 switch(o->type) {
114 case UCI_TYPE_STRING:
115 if ((o->v.string != uci_dataptr(o)) &&
116 (o->v.string != NULL))
117 free(o->v.string);
118 break;
119 case UCI_TYPE_LIST:
120 uci_foreach_element_safe(&o->v.list, tmp, e) {
121 uci_free_element(e);
122 }
123 break;
124 default:
125 break;
126 }
127 uci_free_element(&o->e);
128 }
129
130 static struct uci_option *
131 uci_alloc_list(struct uci_section *s, const char *name)
132 {
133 struct uci_package *p = s->package;
134 struct uci_context *ctx = p->ctx;
135 struct uci_option *o;
136
137 o = uci_alloc_element(ctx, option, name, 0);
138 o->type = UCI_TYPE_LIST;
139 o->section = s;
140 uci_list_init(&o->v.list);
141 uci_list_add(&s->options, &o->e.list);
142
143 return o;
144 }
145
146 /* fix up an unnamed section, e.g. after adding options to it */
147 static void uci_fixup_section(struct uci_context *ctx, struct uci_section *s)
148 {
149 unsigned int hash = ~0;
150 struct uci_element *e;
151 char buf[16];
152
153 if (!s || s->e.name)
154 return;
155
156 /*
157 * Generate a name for unnamed sections. This is used as reference
158 * when locating or updating the section from apps/scripts.
159 * To make multiple concurrent versions somewhat safe for updating,
160 * the name is generated from a hash of its type and name/value
161 * pairs of its option, and it is prefixed by a counter value.
162 * If the order of the unnamed sections changes for some reason,
163 * updates to them will be rejected.
164 */
165 hash = djbhash(hash, s->type);
166 uci_foreach_element(&s->options, e) {
167 struct uci_option *o;
168 hash = djbhash(hash, e->name);
169 o = uci_to_option(e);
170 switch(o->type) {
171 case UCI_TYPE_STRING:
172 hash = djbhash(hash, o->v.string);
173 break;
174 default:
175 break;
176 }
177 }
178 sprintf(buf, "cfg%02x%04x", ++s->package->n_section, hash % (1 << 16));
179 s->e.name = uci_strdup(ctx, buf);
180 }
181
182 static struct uci_section *
183 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
184 {
185 struct uci_context *ctx = p->ctx;
186 struct uci_section *s;
187
188 if (name && !name[0])
189 name = NULL;
190
191 s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
192 uci_list_init(&s->options);
193 s->type = uci_dataptr(s);
194 s->package = p;
195 strcpy(s->type, type);
196 if (name == NULL)
197 s->anonymous = true;
198 p->n_section++;
199
200 uci_list_add(&p->sections, &s->e.list);
201
202 return s;
203 }
204
205 static void
206 uci_free_section(struct uci_section *s)
207 {
208 struct uci_element *o, *tmp;
209
210 uci_foreach_element_safe(&s->options, tmp, o) {
211 uci_free_option(uci_to_option(o));
212 }
213 if ((s->type != uci_dataptr(s)) &&
214 (s->type != NULL))
215 free(s->type);
216 uci_free_element(&s->e);
217 }
218
219 __plugin struct uci_package *
220 uci_alloc_package(struct uci_context *ctx, const char *name)
221 {
222 struct uci_package *p;
223
224 p = uci_alloc_element(ctx, package, name, 0);
225 p->ctx = ctx;
226 uci_list_init(&p->sections);
227 uci_list_init(&p->history);
228 uci_list_init(&p->saved_history);
229 return p;
230 }
231
232 static void
233 uci_free_package(struct uci_package **package)
234 {
235 struct uci_element *e, *tmp;
236 struct uci_package *p = *package;
237
238 if(!p)
239 return;
240
241 if (p->path)
242 free(p->path);
243 uci_foreach_element_safe(&p->sections, tmp, e) {
244 uci_free_section(uci_to_section(e));
245 }
246 uci_foreach_element_safe(&p->history, tmp, e) {
247 uci_free_history(uci_to_history(e));
248 }
249 uci_foreach_element_safe(&p->saved_history, tmp, e) {
250 uci_free_history(uci_to_history(e));
251 }
252 uci_free_element(&p->e);
253 *package = NULL;
254 }
255
256 static struct uci_element *uci_lookup_list(struct uci_list *list, const char *name)
257 {
258 struct uci_element *e;
259
260 uci_foreach_element(list, e) {
261 if (!strcmp(e->name, name))
262 return e;
263 }
264 return NULL;
265 }
266
267 int uci_lookup_ext(struct uci_context *ctx, struct uci_element **res, char *ptr)
268 {
269 struct uci_package *p = NULL;
270 struct uci_element *e;
271 struct uci_section *s;
272 char *package = NULL;
273 char *section = NULL;
274 char *option = NULL;
275 char *idxstr, *t;
276 int idx, c;
277
278 UCI_HANDLE_ERR(ctx);
279 UCI_ASSERT(ctx, res != NULL);
280 UCI_ASSERT(ctx, ptr != NULL);
281
282 UCI_INTERNAL(uci_parse_tuple, ctx, ptr, &package, &section, &option, NULL);
283
284 /* look up the package first */
285 e = uci_lookup_list(&ctx->root, package);
286 if (!e) {
287 UCI_INTERNAL(uci_load, ctx, package, &p);
288 if (!p)
289 goto notfound;
290 e = &p->e;
291 } else {
292 p = uci_to_package(e);
293 }
294
295 if (!section)
296 goto done;
297
298 /* if the section name validates as a regular name, pass through
299 * to the regular uci_lookup function call */
300 if (!*section || uci_validate_name(section)) {
301 UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
302 goto done;
303 }
304
305 /* name did not validate, that means we have an extended lookup call
306 * parse it here. for now only the section index syntax is supported */
307 if (section[0] != '@')
308 goto error;
309
310 section++;
311
312 /* parse the section index part */
313 idxstr = strchr(section, '[');
314 if (!idxstr)
315 goto error;
316 *idxstr = 0;
317 idxstr++;
318
319 t = strchr(idxstr, ']');
320 if (!t)
321 goto error;
322 if (t[1] != 0)
323 goto error;
324 *t = 0;
325
326 t = NULL;
327 idx = strtol(idxstr, &t, 10);
328 if (t && *t)
329 goto error;
330
331 if (!*section)
332 section = NULL;
333 if (section && !uci_validate_str(section, false))
334 goto error;
335
336 /* if the given index is negative, it specifies the section number from
337 * the end of the list */
338 if (idx < 0) {
339 c = 0;
340 uci_foreach_element(&p->sections, e) {
341 s = uci_to_section(e);
342 if (section && (strcmp(s->type, section) != 0))
343 continue;
344
345 c++;
346 }
347 idx += c;
348 }
349
350 c = 0;
351 uci_foreach_element(&p->sections, e) {
352 s = uci_to_section(e);
353 if (section && (strcmp(s->type, section) != 0))
354 continue;
355
356 if (idx == c)
357 goto found;
358 c++;
359 }
360 goto notfound;
361
362 found:
363 if (option)
364 e = uci_lookup_list(&s->options, option);
365 done:
366 *res = e;
367 return 0;
368
369 notfound:
370 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
371 error:
372 UCI_THROW(ctx, UCI_ERR_INVAL);
373 return 0;
374 }
375
376 int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *p, const char *section, const char *option)
377 {
378 struct uci_element *e;
379 struct uci_section *s;
380
381 UCI_HANDLE_ERR(ctx);
382 UCI_ASSERT(ctx, res != NULL);
383 UCI_ASSERT(ctx, p != NULL);
384 UCI_ASSERT(ctx, section && uci_validate_name(section));
385 if (option)
386 UCI_ASSERT(ctx, uci_validate_name(option));
387
388 e = uci_lookup_list(&p->sections, section);
389 if (!e)
390 goto notfound;
391
392 if (option) {
393 s = uci_to_section(e);
394 e = uci_lookup_list(&s->options, option);
395 if (!e)
396 goto notfound;
397 }
398
399 *res = e;
400 return 0;
401
402 notfound:
403 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
404 return 0;
405 }
406
407 int uci_del_element(struct uci_context *ctx, struct uci_element *e)
408 {
409 /* NB: UCI_INTERNAL use means without history tracking */
410 bool internal = ctx->internal;
411 struct uci_package *p = NULL;
412 struct uci_section *s = NULL;
413 struct uci_option *o = NULL;
414 struct uci_element *i, *tmp;
415 char *option = NULL;
416
417 UCI_HANDLE_ERR(ctx);
418 UCI_ASSERT(ctx, e != NULL);
419
420 switch(e->type) {
421 case UCI_TYPE_SECTION:
422 s = uci_to_section(e);
423 uci_foreach_element_safe(&s->options, tmp, i) {
424 uci_del_element(ctx, i);
425 }
426 break;
427 case UCI_TYPE_OPTION:
428 o = uci_to_option(e);
429 s = o->section;
430 p = s->package;
431 option = e->name;
432 break;
433 default:
434 UCI_THROW(ctx, UCI_ERR_INVAL);
435 break;
436 }
437
438 p = s->package;
439 if (!internal && p->has_history)
440 uci_add_history(ctx, &p->history, UCI_CMD_REMOVE, s->e.name, option, NULL);
441
442 switch(e->type) {
443 case UCI_TYPE_SECTION:
444 uci_free_section(s);
445 break;
446 case UCI_TYPE_OPTION:
447 uci_free_option(o);
448 break;
449 default:
450 break;
451 }
452
453 return 0;
454 }
455
456 int uci_add_element_list(struct uci_context *ctx, struct uci_option *o, const char *value)
457 {
458 struct uci_element *e;
459 struct uci_package *p;
460 struct uci_section *s;
461 bool internal = ctx->internal;
462
463 UCI_HANDLE_ERR(ctx);
464 UCI_ASSERT(ctx, (o != NULL) && (o->type == UCI_TYPE_LIST) && uci_validate_text(value));
465
466 s = o->section;
467 p = s->package;
468
469 if (!internal && p->has_history)
470 uci_add_history(ctx, &p->history, UCI_CMD_LIST_ADD, s->e.name, o->e.name, value);
471
472 e = uci_alloc_generic(ctx, UCI_TYPE_ITEM, value, sizeof(struct uci_option));
473 uci_list_add(&o->v.list, &e->list);
474
475 return 0;
476 }
477
478 int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, const char *value)
479 {
480 /* NB: UCI_INTERNAL use means without history tracking */
481 bool internal = ctx->internal;
482 struct uci_list *list;
483 struct uci_element *e;
484 struct uci_package *p;
485 struct uci_section *s;
486 struct uci_option *o;
487 char *section;
488 char *option;
489 char *str;
490 int size = 0;
491
492 UCI_HANDLE_ERR(ctx);
493 UCI_ASSERT(ctx, (element != NULL) && (*element != NULL));
494
495 /* what the 'value' of an element means depends on the type
496 * for a section, the 'value' means its type
497 * for an option, the 'value' means its value string
498 * when changing the value, shrink the element to its actual size
499 * (it may have been allocated with a bigger size, to include
500 * its buffer)
501 * then duplicate the string passed on the command line and
502 * insert it into the structure.
503 */
504 e = *element;
505 list = e->list.prev;
506
507 switch(e->type) {
508 case UCI_TYPE_SECTION:
509 UCI_ASSERT(ctx, uci_validate_str(value, false));
510 size = sizeof(struct uci_section);
511 s = uci_to_section(e);
512 section = e->name;
513 option = NULL;
514 /* matches the currently set value */
515 if (!strcmp(value, s->type))
516 return 0;
517 break;
518
519 case UCI_TYPE_OPTION:
520 UCI_ASSERT(ctx, value != NULL);
521 o = uci_to_option(e);
522 s = o->section;
523 section = s->e.name;
524 option = o->e.name;
525 switch(o->type) {
526 case UCI_TYPE_STRING:
527 size = sizeof(struct uci_option);
528 /* matches the currently set value */
529 if (!strcmp(value, o->v.string))
530 return 0;
531 break;
532 default:
533 /* default action for non-string datatypes is to delete
534 * the existing entry, then re-create it as a string */
535 break;
536 }
537 break;
538
539 default:
540 UCI_THROW(ctx, UCI_ERR_INVAL);
541 return 0;
542 }
543 p = s->package;
544 if (!internal && p->has_history)
545 uci_add_history(ctx, &p->history, UCI_CMD_CHANGE, section, option, value);
546
547 if ((e->type == UCI_TYPE_OPTION) && (size == 0)) {
548 o = uci_alloc_option(s, option, value);
549 UCI_INTERNAL(uci_del_element, ctx, e);
550 *element = &o->e;
551 goto done;
552 }
553
554 uci_list_del(&e->list);
555 e = uci_realloc(ctx, e, size);
556 str = uci_strdup(ctx, value);
557 uci_list_insert(list, &e->list);
558 *element = e;
559
560 switch(e->type) {
561 case UCI_TYPE_SECTION:
562 uci_to_section(e)->type = str;
563 break;
564 case UCI_TYPE_OPTION:
565 uci_to_option(e)->v.string = str;
566 break;
567 default:
568 break;
569 }
570
571 done:
572 return 0;
573 }
574
575 int uci_rename(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *name)
576 {
577 /* NB: UCI_INTERNAL use means without history tracking */
578 bool internal = ctx->internal;
579 struct uci_element *e;
580
581 UCI_HANDLE_ERR(ctx);
582
583 /* NB: p, section, option validated by uci_lookup */
584 UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
585
586 if (!internal && p->has_history)
587 uci_add_history(ctx, &p->history, UCI_CMD_RENAME, section, option, name);
588
589 name = uci_strdup(ctx, name);
590 if (e->name)
591 free(e->name);
592 e->name = name;
593
594 return 0;
595 }
596
597 int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
598 {
599 bool internal = ctx->internal;
600 struct uci_section *s;
601
602 UCI_HANDLE_ERR(ctx);
603 UCI_ASSERT(ctx, p != NULL);
604 s = uci_alloc_section(p, type, NULL);
605 uci_fixup_section(ctx, s);
606 *res = s;
607 if (!internal && p->has_history)
608 uci_add_history(ctx, &p->history, UCI_CMD_ADD, s->e.name, NULL, type);
609
610 return 0;
611 }
612
613 int uci_delete(struct uci_context *ctx, struct uci_package *p, const char *section, const char *option)
614 {
615 /* NB: pass on internal flag to uci_del_element */
616 bool internal = ctx->internal;
617 struct uci_element *e;
618
619 UCI_HANDLE_ERR(ctx);
620
621 /* NB: p, section, option validated by uci_lookup */
622 UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
623
624 ctx->internal = internal;
625 return uci_del_element(ctx, e);
626 }
627
628 int uci_add_list(struct uci_context *ctx, struct uci_package *p, const char *section, const char *option, const char *value, struct uci_option **result)
629 {
630 /* NB: UCI_INTERNAL use means without history tracking */
631 bool internal = ctx->internal;
632 struct uci_element *e;
633 struct uci_section *s;
634 struct uci_option *o;
635 struct uci_option *prev = NULL;
636 const char *value2 = NULL;
637
638 UCI_HANDLE_ERR(ctx);
639 UCI_ASSERT(ctx, p && section && option && value && uci_validate_text(value));
640
641 /* look up the section first */
642 UCI_INTERNAL(uci_lookup, ctx, &e, p, section, NULL);
643 s = uci_to_section(e);
644
645 e = uci_lookup_list(&s->options, option);
646 if (e) {
647 o = uci_to_option(e);
648 switch (o->type) {
649 case UCI_TYPE_STRING:
650 /* we already have a string value, let's convert that to a list */
651 prev = o;
652 value2 = value;
653 value = o->v.string;
654 break;
655 case UCI_TYPE_LIST:
656 if (result)
657 *result = o;
658
659 ctx->internal = internal;
660 return uci_add_element_list(ctx, o, value);
661 default:
662 UCI_THROW(ctx, UCI_ERR_INVAL);
663 break;
664 }
665 }
666
667 o = uci_alloc_list(s, option);
668 if (result)
669 *result = o;
670 if (prev) {
671 UCI_INTERNAL(uci_add_element_list, ctx, o, value);
672 uci_free_option(prev);
673 value = value2;
674 }
675
676 ctx->internal = internal;
677 return uci_add_element_list(ctx, o, value);
678 }
679
680 int uci_set(struct uci_context *ctx, struct uci_package *p, const char *section, const char *option, const char *value, struct uci_element **result)
681 {
682 /* NB: UCI_INTERNAL use means without history tracking */
683 bool internal = ctx->internal;
684 struct uci_element *e = NULL;
685 struct uci_section *s = NULL;
686 struct uci_option *o = NULL;
687
688 UCI_HANDLE_ERR(ctx);
689 UCI_ASSERT(ctx, p != NULL);
690 UCI_ASSERT(ctx, uci_validate_name(section));
691 if (option) {
692 UCI_ASSERT(ctx, uci_validate_name(option));
693 UCI_ASSERT(ctx, value != NULL);
694 } else {
695 UCI_ASSERT(ctx, uci_validate_str(value, false));
696 }
697
698 /*
699 * look up the package, section and option (if set)
700 * if the section/option is to be modified and it is not found
701 * create a new element in the appropriate list
702 */
703 e = uci_lookup_list(&p->sections, section);
704 if (!e)
705 goto notfound;
706
707 s = uci_to_section(e);
708 if (ctx->pctx && ctx->pctx->merge)
709 ctx->pctx->section = s;
710
711 if (option) {
712 e = uci_lookup_list(&s->options, option);
713 if (!e)
714 goto notfound;
715 o = uci_to_option(e);
716 }
717
718 /*
719 * no unknown element was supplied, assume that we can just update
720 * an existing entry
721 */
722 if (o)
723 e = &o->e;
724 else
725 e = &s->e;
726 if (result)
727 *result = e;
728 else
729 result = &e;
730
731 ctx->internal = internal;
732 return uci_set_element_value(ctx, result, value);
733
734 notfound:
735 /*
736 * the entry that we need to update was not found,
737 * check if the search failed prematurely.
738 * this can happen if the package was not found, or if
739 * an option was supplied, but the section wasn't found
740 */
741 if (!p || (!s && option))
742 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
743
744 /* now add the missing entry */
745 if (!internal && p->has_history)
746 uci_add_history(ctx, &p->history, UCI_CMD_CHANGE, section, option, value);
747 if (s) {
748 o = uci_alloc_option(s, option, value);
749 if (result)
750 *result = &o->e;
751 } else {
752 s = uci_alloc_section(p, value, section);
753 if (result)
754 *result = &s->e;
755 if (ctx->pctx && ctx->pctx->merge)
756 ctx->pctx->section = s;
757 }
758
759 return 0;
760 }
761
762 int uci_unload(struct uci_context *ctx, struct uci_package *p)
763 {
764 UCI_HANDLE_ERR(ctx);
765 UCI_ASSERT(ctx, p != NULL);
766
767 uci_free_package(&p);
768 return 0;
769 }
770