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