create history files with the proper mode
[project/uci.git] / file.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 /*
16 * This file contains the code for parsing uci config files
17 */
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <stdbool.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <ctype.h>
26
27 #define LINEBUF 32
28 #define LINEBUF_MAX 4096
29
30 static void uci_parse_error(struct uci_context *ctx, char *pos, char *reason)
31 {
32 struct uci_parse_context *pctx = ctx->pctx;
33
34 pctx->reason = reason;
35 pctx->byte = pos - pctx->buf;
36 UCI_THROW(ctx, UCI_ERR_PARSE);
37 }
38
39 /*
40 * Fetch a new line from the input stream and resize buffer if necessary
41 */
42 static void uci_getln(struct uci_context *ctx, int offset)
43 {
44 struct uci_parse_context *pctx = ctx->pctx;
45 char *p;
46 int ofs;
47
48 if (pctx->buf == NULL) {
49 pctx->buf = uci_malloc(ctx, LINEBUF);
50 pctx->bufsz = LINEBUF;
51 }
52
53 ofs = offset;
54 do {
55 p = &pctx->buf[ofs];
56 p[ofs] = 0;
57
58 p = fgets(p, pctx->bufsz - ofs, pctx->file);
59 if (!p || !*p)
60 return;
61
62 ofs += strlen(p);
63 if (pctx->buf[ofs - 1] == '\n') {
64 pctx->line++;
65 pctx->buf[ofs - 1] = 0;
66 return;
67 }
68
69 if (pctx->bufsz > LINEBUF_MAX/2)
70 uci_parse_error(ctx, p, "line too long");
71
72 pctx->bufsz *= 2;
73 pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
74 } while (1);
75 }
76
77 /*
78 * Clean up all extra memory used by the parser and exporter
79 */
80 static void uci_file_cleanup(struct uci_context *ctx)
81 {
82 struct uci_parse_context *pctx;
83
84 if (ctx->buf) {
85 free(ctx->buf);
86 ctx->buf = NULL;
87 ctx->bufsz = 0;
88 }
89
90 pctx = ctx->pctx;
91 if (!pctx)
92 return;
93
94 ctx->pctx = NULL;
95 if (pctx->package)
96 uci_free_package(pctx->package);
97
98 if (pctx->buf)
99 free(pctx->buf);
100
101 free(pctx);
102 }
103
104 /*
105 * parse a character escaped by '\'
106 * returns true if the escaped character is to be parsed
107 * returns false if the escaped character is to be ignored
108 */
109 static inline bool parse_backslash(struct uci_context *ctx, char **str)
110 {
111 /* skip backslash */
112 *str += 1;
113
114 /* undecoded backslash at the end of line, fetch the next line */
115 if (!**str) {
116 *str += 1;
117 uci_getln(ctx, *str - ctx->pctx->buf);
118 return false;
119 }
120
121 /* FIXME: decode escaped char, necessary? */
122 return true;
123 }
124
125 /*
126 * move the string pointer forward until a non-whitespace character or
127 * EOL is reached
128 */
129 static void skip_whitespace(struct uci_context *ctx, char **str)
130 {
131 restart:
132 while (**str && isspace(**str))
133 *str += 1;
134
135 if (**str == '\\') {
136 if (!parse_backslash(ctx, str))
137 goto restart;
138 }
139 }
140
141 static inline void addc(char **dest, char **src)
142 {
143 **dest = **src;
144 *dest += 1;
145 *src += 1;
146 }
147
148 /*
149 * parse a double quoted string argument from the command line
150 */
151 static void parse_double_quote(struct uci_context *ctx, char **str, char **target)
152 {
153 char c;
154
155 /* skip quote character */
156 *str += 1;
157
158 while ((c = **str)) {
159 switch(c) {
160 case '"':
161 **target = 0;
162 *str += 1;
163 return;
164 case '\\':
165 if (!parse_backslash(ctx, str))
166 continue;
167 /* fall through */
168 default:
169 addc(target, str);
170 break;
171 }
172 }
173 uci_parse_error(ctx, *str, "unterminated \"");
174 }
175
176 /*
177 * parse a single quoted string argument from the command line
178 */
179 static void parse_single_quote(struct uci_context *ctx, char **str, char **target)
180 {
181 char c;
182 /* skip quote character */
183 *str += 1;
184
185 while ((c = **str)) {
186 switch(c) {
187 case '\'':
188 **target = 0;
189 *str += 1;
190 return;
191 default:
192 addc(target, str);
193 }
194 }
195 uci_parse_error(ctx, *str, "unterminated '");
196 }
197
198 /*
199 * parse a string from the command line and detect the quoting style
200 */
201 static void parse_str(struct uci_context *ctx, char **str, char **target)
202 {
203 do {
204 switch(**str) {
205 case '\'':
206 parse_single_quote(ctx, str, target);
207 break;
208 case '"':
209 parse_double_quote(ctx, str, target);
210 break;
211 case 0:
212 goto done;
213 case '\\':
214 if (!parse_backslash(ctx, str))
215 continue;
216 /* fall through */
217 default:
218 addc(target, str);
219 break;
220 }
221 } while (**str && !isspace(**str));
222 done:
223
224 /*
225 * if the string was unquoted and we've stopped at a whitespace
226 * character, skip to the next one, because the whitespace will
227 * be overwritten by a null byte here
228 */
229 if (**str)
230 *str += 1;
231
232 /* terminate the parsed string */
233 **target = 0;
234 }
235
236 /*
237 * extract the next argument from the command line
238 */
239 static char *next_arg(struct uci_context *ctx, char **str, bool required, bool name)
240 {
241 char *val;
242 char *ptr;
243
244 val = ptr = *str;
245 skip_whitespace(ctx, str);
246 parse_str(ctx, str, &ptr);
247 if (required && !*val)
248 uci_parse_error(ctx, *str, "insufficient arguments");
249 if (name && !uci_validate_name(val))
250 uci_parse_error(ctx, val, "invalid character in field");
251
252 return val;
253 }
254
255 /*
256 * verify that the end of the line or command is reached.
257 * throw an error if extra arguments are given on the command line
258 */
259 static void assert_eol(struct uci_context *ctx, char **str)
260 {
261 char *tmp;
262
263 tmp = next_arg(ctx, str, false, false);
264 if (tmp && *tmp)
265 uci_parse_error(ctx, *str, "too many arguments");
266 }
267
268 /*
269 * switch to a different config, either triggered by uci_load, or by a
270 * 'package <...>' statement in the import file
271 */
272 static void uci_switch_config(struct uci_context *ctx)
273 {
274 struct uci_parse_context *pctx;
275 struct uci_element *e;
276 const char *name;
277
278 pctx = ctx->pctx;
279 name = pctx->name;
280
281 /* add the last config to main config file list */
282 if (pctx->package) {
283 uci_list_add(&ctx->root, &pctx->package->e.list);
284
285 pctx->package = NULL;
286 pctx->section = NULL;
287 }
288
289 if (!name)
290 return;
291
292 /*
293 * if an older config under the same name exists, unload it
294 * ignore errors here, e.g. if the config was not found
295 */
296 e = uci_lookup_list(ctx, &ctx->root, name);
297 if (e)
298 UCI_THROW(ctx, UCI_ERR_DUPLICATE);
299 pctx->package = uci_alloc_package(ctx, name);
300 }
301
302 /*
303 * parse the 'package' uci command (next config package)
304 */
305 static void uci_parse_package(struct uci_context *ctx, char **str, bool single)
306 {
307 char *name = NULL;
308
309 /* command string null-terminated by strtok */
310 *str += strlen(*str) + 1;
311
312 name = next_arg(ctx, str, true, true);
313 assert_eol(ctx, str);
314 if (single)
315 return;
316
317 ctx->pctx->name = name;
318 uci_switch_config(ctx);
319 }
320
321 /*
322 * parse the 'config' uci command (open a section)
323 */
324 static void uci_parse_config(struct uci_context *ctx, char **str)
325 {
326 char *name = NULL;
327 char *type = NULL;
328
329 if (!ctx->pctx->package) {
330 if (!ctx->pctx->name)
331 uci_parse_error(ctx, *str, "attempting to import a file without a package name");
332
333 uci_switch_config(ctx);
334 }
335
336 /* command string null-terminated by strtok */
337 *str += strlen(*str) + 1;
338
339 type = next_arg(ctx, str, true, true);
340 name = next_arg(ctx, str, false, true);
341 assert_eol(ctx, str);
342 ctx->pctx->section = uci_alloc_section(ctx->pctx->package, type, name);
343 }
344
345 /*
346 * parse the 'option' uci command (open a value)
347 */
348 static void uci_parse_option(struct uci_context *ctx, char **str)
349 {
350 char *name = NULL;
351 char *value = NULL;
352
353 if (!ctx->pctx->section)
354 uci_parse_error(ctx, *str, "option command found before the first section");
355
356 /* command string null-terminated by strtok */
357 *str += strlen(*str) + 1;
358
359 name = next_arg(ctx, str, true, true);
360 value = next_arg(ctx, str, true, false);
361 assert_eol(ctx, str);
362 uci_alloc_option(ctx->pctx->section, name, value);
363 }
364
365
366 /*
367 * parse a complete input line, split up combined commands by ';'
368 */
369 static void uci_parse_line(struct uci_context *ctx, bool single)
370 {
371 struct uci_parse_context *pctx = ctx->pctx;
372 char *word, *brk = NULL;
373
374 for (word = strtok_r(pctx->buf, ";", &brk);
375 word;
376 word = strtok_r(NULL, ";", &brk)) {
377
378 char *pbrk = NULL;
379 word = strtok_r(word, " \t", &pbrk);
380
381 switch(word[0]) {
382 case 'p':
383 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
384 uci_parse_package(ctx, &word, single);
385 break;
386 case 'c':
387 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
388 uci_parse_config(ctx, &word);
389 break;
390 case 'o':
391 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
392 uci_parse_option(ctx, &word);
393 break;
394 default:
395 uci_parse_error(ctx, word, "unterminated command");
396 break;
397 }
398 }
399 }
400
401 /* max number of characters that escaping adds to the string */
402 #define UCI_QUOTE_ESCAPE "'\\''"
403
404 /*
405 * escape an uci string for export
406 */
407 static char *uci_escape(struct uci_context *ctx, char *str)
408 {
409 char *s, *p;
410 int pos = 0;
411
412 if (!ctx->buf) {
413 ctx->bufsz = LINEBUF;
414 ctx->buf = malloc(LINEBUF);
415 }
416
417 s = str;
418 p = strchr(str, '\'');
419 if (!p)
420 return str;
421
422 do {
423 int len = p - s;
424 if (len > 0) {
425 if (p + sizeof(UCI_QUOTE_ESCAPE) - str >= ctx->bufsz) {
426 ctx->bufsz *= 2;
427 ctx->buf = realloc(ctx->buf, ctx->bufsz);
428 if (!ctx->buf)
429 UCI_THROW(ctx, UCI_ERR_MEM);
430 }
431 memcpy(&ctx->buf[pos], s, len);
432 pos += len;
433 }
434 strcpy(&ctx->buf[pos], UCI_QUOTE_ESCAPE);
435 pos += sizeof(UCI_QUOTE_ESCAPE);
436 s = p + 1;
437 } while ((p = strchr(s, '\'')));
438
439 return ctx->buf;
440 }
441
442
443 /*
444 * export a single config package to a file stream
445 */
446 static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
447 {
448 struct uci_context *ctx = p->ctx;
449 struct uci_element *s, *o;
450
451 if (header)
452 fprintf(stream, "package '%s'\n", uci_escape(ctx, p->e.name));
453 uci_foreach_element(&p->sections, s) {
454 struct uci_section *sec = uci_to_section(s);
455 fprintf(stream, "\nconfig '%s'", uci_escape(ctx, sec->type));
456 fprintf(stream, " '%s'\n", uci_escape(ctx, sec->e.name));
457 uci_foreach_element(&sec->options, o) {
458 struct uci_option *opt = uci_to_option(o);
459 fprintf(stream, "\toption '%s'", uci_escape(ctx, opt->e.name));
460 fprintf(stream, " '%s'\n", uci_escape(ctx, opt->value));
461 }
462 }
463 fprintf(stream, "\n");
464 }
465
466 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
467 {
468 struct uci_element *e;
469
470 UCI_HANDLE_ERR(ctx);
471 UCI_ASSERT(ctx, stream != NULL);
472
473 if (package)
474 uci_export_package(package, stream, header);
475 else {
476 uci_foreach_element(&ctx->root, e) {
477 uci_export_package(uci_to_package(e), stream, header);
478 }
479 }
480
481 return 0;
482 }
483
484 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
485 {
486 struct uci_parse_context *pctx;
487
488 /* make sure no memory from previous parse attempts is leaked */
489 uci_file_cleanup(ctx);
490
491 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
492 ctx->pctx = pctx;
493 pctx->file = stream;
494
495 /*
496 * If 'name' was supplied, assume that the supplied stream does not contain
497 * the appropriate 'package <name>' string to specify the config name
498 * NB: the config file can still override the package name
499 */
500 if (name)
501 pctx->name = name;
502
503 while (!feof(pctx->file)) {
504 uci_getln(ctx, 0);
505
506 UCI_TRAP_SAVE(ctx, error);
507 if (pctx->buf[0])
508 uci_parse_line(ctx, single);
509 UCI_TRAP_RESTORE(ctx);
510 continue;
511 error:
512 if (ctx->flags & UCI_FLAG_PERROR)
513 uci_perror(ctx, NULL);
514 if ((ctx->errno != UCI_ERR_PARSE) ||
515 (ctx->flags & UCI_FLAG_STRICT))
516 UCI_THROW(ctx, ctx->errno);
517 }
518
519 if (package)
520 *package = pctx->package;
521
522 pctx->name = NULL;
523 uci_switch_config(ctx);
524
525 /* no error happened, we can get rid of the parser context now */
526 uci_file_cleanup(ctx);
527
528 return 0;
529 }
530
531 /*
532 * open a stream and go to the right position
533 *
534 * note: when opening for write and seeking to the beginning of
535 * the stream, truncate the file
536 */
537 static FILE *uci_open_stream(struct uci_context *ctx, const char *filename, int pos, bool write)
538 {
539 struct stat statbuf;
540 FILE *file = NULL;
541 int fd, ret;
542
543 if (!write && ((stat(filename, &statbuf) < 0) ||
544 ((statbuf.st_mode & S_IFMT) != S_IFREG))) {
545 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
546 }
547
548 fd = open(filename, (write ? O_RDWR | O_CREAT : O_RDONLY), UCI_FILEMODE);
549 if (fd <= 0)
550 goto error;
551
552 if (flock(fd, (write ? LOCK_EX : LOCK_SH)) < 0)
553 goto error;
554
555 if (write && (pos == SEEK_SET))
556 ret = ftruncate(fd, 0);
557 else
558 ret = lseek(fd, 0, pos);
559
560 if (ret < 0)
561 goto error;
562
563 file = fdopen(fd, (write ? "w" : "r"));
564 if (file)
565 goto done;
566
567 error:
568 UCI_THROW(ctx, UCI_ERR_IO);
569 done:
570 return file;
571 }
572
573 static void uci_close_stream(FILE *stream)
574 {
575 int fd;
576
577 if (!stream)
578 return;
579
580 fd = fileno(stream);
581 flock(fd, LOCK_UN);
582 fclose(stream);
583 }
584
585 static void uci_parse_history_line(struct uci_context *ctx, struct uci_package *p, char *buf)
586 {
587 bool delete = false;
588 char *package = NULL;
589 char *section = NULL;
590 char *option = NULL;
591 char *value = NULL;
592
593 if (buf[0] == '-') {
594 delete = true;
595 buf++;
596 }
597
598 UCI_INTERNAL(uci_parse_tuple, ctx, buf, &package, &section, &option, &value);
599 if (!package || !section || (!delete && !value))
600 goto error;
601 if (strcmp(package, p->e.name) != 0)
602 goto error;
603 if (!uci_validate_name(section))
604 goto error;
605 if (option && !uci_validate_name(option))
606 goto error;
607
608 if (delete)
609 UCI_INTERNAL(uci_del, ctx, p, section, option);
610 else
611 UCI_INTERNAL(uci_set, ctx, p, section, option, value);
612
613 return;
614 error:
615 UCI_THROW(ctx, UCI_ERR_PARSE);
616 }
617
618 static void uci_parse_history(struct uci_context *ctx, FILE *stream, struct uci_package *p)
619 {
620 struct uci_parse_context *pctx;
621
622 /* make sure no memory from previous parse attempts is leaked */
623 uci_file_cleanup(ctx);
624
625 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
626 ctx->pctx = pctx;
627 pctx->file = stream;
628
629 rewind(stream);
630 while (!feof(pctx->file)) {
631 uci_getln(ctx, 0);
632 if (!pctx->buf[0])
633 continue;
634 uci_parse_history_line(ctx, p, pctx->buf);
635 }
636
637 /* no error happened, we can get rid of the parser context now */
638 uci_file_cleanup(ctx);
639 }
640
641 static void uci_load_history(struct uci_context *ctx, struct uci_package *p)
642 {
643 char *filename = NULL;
644 FILE *f = NULL;
645
646 if (!p->confdir)
647 return;
648 if ((asprintf(&filename, "%s/%s", UCI_SAVEDIR, p->e.name) < 0) || !filename)
649 UCI_THROW(ctx, UCI_ERR_MEM);
650
651 UCI_TRAP_SAVE(ctx, done);
652 f = uci_open_stream(ctx, filename, SEEK_SET, false);
653 uci_parse_history(ctx, f, p);
654 UCI_TRAP_RESTORE(ctx);
655 done:
656 if (filename)
657 free(filename);
658 uci_close_stream(f);
659 ctx->errno = 0;
660 }
661
662 int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
663 {
664 char *filename;
665 bool confdir;
666 FILE *file = NULL;
667
668 UCI_HANDLE_ERR(ctx);
669 UCI_ASSERT(ctx, name != NULL);
670
671 switch (name[0]) {
672 case '.':
673 /* relative path outside of /etc/config */
674 if (name[1] != '/')
675 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
676 /* fall through */
677 case '/':
678 /* absolute path outside of /etc/config */
679 filename = uci_strdup(ctx, name);
680 name = strrchr(name, '/') + 1;
681 confdir = false;
682 break;
683 default:
684 /* config in /etc/config */
685 if (strchr(name, '/'))
686 UCI_THROW(ctx, UCI_ERR_INVAL);
687 filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2);
688 sprintf(filename, UCI_CONFDIR "/%s", name);
689 confdir = true;
690 break;
691 }
692
693 file = uci_open_stream(ctx, filename, SEEK_SET, false);
694 ctx->errno = 0;
695 UCI_TRAP_SAVE(ctx, done);
696 uci_import(ctx, file, name, package, true);
697 UCI_TRAP_RESTORE(ctx);
698
699 if (*package) {
700 (*package)->path = filename;
701 (*package)->confdir = confdir;
702 uci_load_history(ctx, *package);
703 }
704
705 done:
706 uci_close_stream(file);
707 return ctx->errno;
708 }
709
710 int uci_save(struct uci_context *ctx, struct uci_package *p)
711 {
712 FILE *f = NULL;
713 char *filename = NULL;
714 struct uci_element *e, *tmp;
715
716 UCI_HANDLE_ERR(ctx);
717 UCI_ASSERT(ctx, p != NULL);
718
719 /*
720 * if the config file was outside of the /etc/config path,
721 * don't save the history to a file, update the real file
722 * directly
723 */
724 if (!p->confdir)
725 return uci_commit(ctx, p);
726
727 if (uci_list_empty(&p->history))
728 return 0;
729
730 if ((asprintf(&filename, "%s/%s", UCI_SAVEDIR, p->e.name) < 0) || !filename)
731 UCI_THROW(ctx, UCI_ERR_MEM);
732
733 ctx->errno = 0;
734 UCI_TRAP_SAVE(ctx, done);
735 f = uci_open_stream(ctx, filename, SEEK_END, true);
736 UCI_TRAP_RESTORE(ctx);
737
738 uci_foreach_element_safe(&p->history, tmp, e) {
739 struct uci_history *h = uci_to_history(e);
740
741 if (h->cmd == UCI_CMD_REMOVE)
742 fprintf(f, "-");
743
744 fprintf(f, "%s.%s", p->e.name, h->section);
745 if (e->name)
746 fprintf(f, ".%s", e->name);
747
748 if (h->cmd == UCI_CMD_REMOVE)
749 fprintf(f, "\n");
750 else
751 fprintf(f, "=%s\n", h->value);
752 uci_free_history(h);
753 }
754
755 done:
756 uci_close_stream(f);
757 if (filename)
758 free(filename);
759 if (ctx->errno)
760 UCI_THROW(ctx, ctx->errno);
761
762 return 0;
763 }
764
765 int uci_commit(struct uci_context *ctx, struct uci_package *p)
766 {
767 FILE *f = NULL;
768
769 UCI_HANDLE_ERR(ctx);
770 UCI_ASSERT(ctx, p != NULL);
771 UCI_ASSERT(ctx, p->path != NULL);
772
773 f = uci_open_stream(ctx, p->path, SEEK_SET, true);
774
775 UCI_TRAP_SAVE(ctx, done);
776 uci_export(ctx, f, p, false);
777 UCI_TRAP_RESTORE(ctx);
778
779 done:
780 uci_close_stream(f);
781 if (ctx->errno)
782 UCI_THROW(ctx, ctx->errno);
783
784 return 0;
785 }
786
787
788 /*
789 * This function returns the filename by returning the string
790 * after the last '/' character. By checking for a non-'\0'
791 * character afterwards, directories are ignored (glob marks
792 * those with a trailing '/'
793 */
794 static inline char *get_filename(char *path)
795 {
796 char *p;
797
798 p = strrchr(path, '/');
799 p++;
800 if (!*p)
801 return NULL;
802 return p;
803 }
804
805 int uci_list_configs(struct uci_context *ctx, char ***list)
806 {
807 char **configs;
808 glob_t globbuf;
809 int size, i;
810 char *buf;
811
812 UCI_HANDLE_ERR(ctx);
813
814 if (glob(UCI_CONFDIR "/*", GLOB_MARK, NULL, &globbuf) != 0)
815 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
816
817 size = sizeof(char *) * (globbuf.gl_pathc + 1);
818 for(i = 0; i < globbuf.gl_pathc; i++) {
819 char *p;
820
821 p = get_filename(globbuf.gl_pathv[i]);
822 if (!p)
823 continue;
824
825 size += strlen(p) + 1;
826 }
827
828 configs = uci_malloc(ctx, size);
829 buf = (char *) &configs[globbuf.gl_pathc + 1];
830 for(i = 0; i < globbuf.gl_pathc; i++) {
831 char *p;
832
833 p = get_filename(globbuf.gl_pathv[i]);
834 if (!p)
835 continue;
836
837 configs[i] = buf;
838 strcpy(buf, p);
839 buf += strlen(buf) + 1;
840 }
841 *list = configs;
842
843 return 0;
844 }
845