cli: check against multiple -f invocations to avoid leaks (patch by Stanislav Fomichev)
[project/uci.git] / cli.c
1 /*
2  * cli - Command Line Interface 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 General Public License version 2
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 #include <strings.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include "uci.h"
19
20 #define MAX_ARGS        4 /* max command line arguments for batch mode */
21
22 static const char *delimiter = " ";
23 static const char *appname;
24 static enum {
25         CLI_FLAG_MERGE =    (1 << 0),
26         CLI_FLAG_QUIET =    (1 << 1),
27         CLI_FLAG_NOCOMMIT = (1 << 2),
28         CLI_FLAG_BATCH =    (1 << 3),
29         CLI_FLAG_SHOW_EXT = (1 << 4),
30         CLI_FLAG_NOPLUGINS= (1 << 5),
31 } flags;
32
33 static FILE *input;
34
35 static struct uci_context *ctx;
36 enum {
37         /* section cmds */
38         CMD_GET,
39         CMD_SET,
40         CMD_ADD_LIST,
41         CMD_DEL,
42         CMD_RENAME,
43         CMD_REVERT,
44         CMD_REORDER,
45         /* package cmds */
46         CMD_SHOW,
47         CMD_CHANGES,
48         CMD_EXPORT,
49         CMD_COMMIT,
50         /* other cmds */
51         CMD_ADD,
52         CMD_IMPORT,
53         CMD_HELP,
54 };
55
56 struct uci_type_list {
57         unsigned int idx;
58         const char *name;
59         struct uci_type_list *next;
60 };
61
62 static struct uci_type_list *type_list = NULL;
63 static char *typestr = NULL;
64 static const char *cur_section_ref = NULL;
65
66 static int uci_cmd(int argc, char **argv);
67
68 static void
69 uci_reset_typelist(void)
70 {
71         struct uci_type_list *type;
72         while (type_list != NULL) {
73                         type = type_list;
74                         type_list = type_list->next;
75                         free(type);
76         }
77         if (typestr) {
78                 free(typestr);
79                 typestr = NULL;
80         }
81         cur_section_ref = NULL;
82 }
83
84 static char *
85 uci_lookup_section_ref(struct uci_section *s)
86 {
87         struct uci_type_list *ti = type_list;
88         int maxlen;
89
90         if (!s->anonymous || !(flags & CLI_FLAG_SHOW_EXT))
91                 return s->e.name;
92
93         /* look up in section type list */
94         while (ti) {
95                 if (strcmp(ti->name, s->type) == 0)
96                         break;
97                 ti = ti->next;
98         }
99         if (!ti) {
100                 ti = malloc(sizeof(struct uci_type_list));
101                 if (!ti)
102                         return NULL;
103                 memset(ti, 0, sizeof(struct uci_type_list));
104                 ti->next = type_list;
105                 type_list = ti;
106                 ti->name = s->type;
107         }
108
109         maxlen = strlen(s->type) + 1 + 2 + 10;
110         if (!typestr) {
111                 typestr = malloc(maxlen);
112         } else {
113                 typestr = realloc(typestr, maxlen);
114         }
115
116         if (typestr)
117                 sprintf(typestr, "@%s[%d]", ti->name, ti->idx);
118
119         ti->idx++;
120
121         return typestr;
122 }
123
124 static void uci_usage(void)
125 {
126         fprintf(stderr,
127                 "Usage: %s [<options>] <command> [<arguments>]\n\n"
128                 "Commands:\n"
129                 "\tbatch\n"
130                 "\texport     [<config>]\n"
131                 "\timport     [<config>]\n"
132                 "\tchanges    [<config>]\n"
133                 "\tcommit     [<config>]\n"
134                 "\tadd        <config> <section-type>\n"
135                 "\tadd_list   <config>.<section>.<option>=<string>\n"
136                 "\tshow       [<config>[.<section>[.<option>]]]\n"
137                 "\tget        <config>.<section>[.<option>]\n"
138                 "\tset        <config>.<section>[.<option>]=<value>\n"
139                 "\tdelete     <config>[.<section[.<option>]]\n"
140                 "\trename     <config>.<section>[.<option>]=<name>\n"
141                 "\trevert     <config>[.<section>[.<option>]]\n"
142                 "\treorder    <config>.<section>=<position>\n"
143                 "\n"
144                 "Options:\n"
145                 "\t-c <path>  set the search path for config files (default: /etc/config)\n"
146                 "\t-d <str>   set the delimiter for list values in uci show\n"
147                 "\t-f <file>  use <file> as input instead of stdin\n"
148                 "\t-L         do not load any plugins\n"
149                 "\t-m         when importing, merge data into an existing package\n"
150                 "\t-n         name unnamed sections on export (default)\n"
151                 "\t-N         don't name unnamed sections\n"
152                 "\t-p <path>  add a search path for config change files\n"
153                 "\t-P <path>  add a search path for config change files and use as default\n"
154                 "\t-q         quiet mode (don't print error messages)\n"
155                 "\t-s         force strict mode (stop on parser errors, default)\n"
156                 "\t-S         disable strict mode\n"
157                 "\t-X         do not use extended syntax on 'show'\n"
158                 "\n",
159                 appname
160         );
161 }
162
163 static void cli_perror(void)
164 {
165         if (flags & CLI_FLAG_QUIET)
166                 return;
167
168         uci_perror(ctx, appname);
169 }
170
171 static void uci_show_value(struct uci_option *o)
172 {
173         struct uci_element *e;
174         bool sep = false;
175
176         switch(o->type) {
177         case UCI_TYPE_STRING:
178                 printf("%s\n", o->v.string);
179                 break;
180         case UCI_TYPE_LIST:
181                 uci_foreach_element(&o->v.list, e) {
182                         printf("%s%s", (sep ? delimiter : ""), e->name);
183                         sep = true;
184                 }
185                 printf("\n");
186                 break;
187         default:
188                 printf("<unknown>\n");
189                 break;
190         }
191 }
192
193 static void uci_show_option(struct uci_option *o)
194 {
195         printf("%s.%s.%s=",
196                 o->section->package->e.name,
197                 (cur_section_ref ? cur_section_ref : o->section->e.name),
198                 o->e.name);
199         uci_show_value(o);
200 }
201
202 static void uci_show_section(struct uci_section *s)
203 {
204         struct uci_element *e;
205         const char *cname;
206         const char *sname;
207
208         cname = s->package->e.name;
209         sname = (cur_section_ref ? cur_section_ref : s->e.name);
210         printf("%s.%s=%s\n", cname, sname, s->type);
211         uci_foreach_element(&s->options, e) {
212                 uci_show_option(uci_to_option(e));
213         }
214 }
215
216 static void uci_show_package(struct uci_package *p)
217 {
218         struct uci_element *e;
219
220         uci_reset_typelist();
221         uci_foreach_element( &p->sections, e) {
222                 struct uci_section *s = uci_to_section(e);
223                 cur_section_ref = uci_lookup_section_ref(s);
224                 uci_show_section(s);
225         }
226         uci_reset_typelist();
227 }
228
229 static void uci_show_changes(struct uci_package *p)
230 {
231         struct uci_element *e;
232
233         uci_foreach_element(&p->saved_delta, e) {
234                 struct uci_delta *h = uci_to_delta(e);
235                 char *prefix = "";
236                 char *op = "=";
237
238                 switch(h->cmd) {
239                 case UCI_CMD_REMOVE:
240                         prefix = "-";
241                         break;
242                 case UCI_CMD_LIST_ADD:
243                         op = "+=";
244                         break;
245                 default:
246                         break;
247                 }
248                 printf("%s%s.%s", prefix, p->e.name, h->section);
249                 if (e->name)
250                         printf(".%s", e->name);
251                 if (h->cmd != UCI_CMD_REMOVE)
252                         printf("%s%s", op, h->value);
253                 printf("\n");
254         }
255 }
256
257 static int package_cmd(int cmd, char *tuple)
258 {
259         struct uci_element *e = NULL;
260         struct uci_ptr ptr;
261
262         if (uci_lookup_ptr(ctx, &ptr, tuple, true) != UCI_OK) {
263                 cli_perror();
264                 return 1;
265         }
266
267         e = ptr.last;
268         switch(cmd) {
269         case CMD_CHANGES:
270                 uci_show_changes(ptr.p);
271                 break;
272         case CMD_COMMIT:
273                 if (flags & CLI_FLAG_NOCOMMIT)
274                         return 0;
275                 if (uci_commit(ctx, &ptr.p, false) != UCI_OK)
276                         cli_perror();
277                 break;
278         case CMD_EXPORT:
279                 uci_export(ctx, stdout, ptr.p, true);
280                 break;
281         case CMD_SHOW:
282                 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
283                         ctx->err = UCI_ERR_NOTFOUND;
284                         cli_perror();
285                         return 1;
286                 }
287                 switch(e->type) {
288                         case UCI_TYPE_PACKAGE:
289                                 uci_show_package(ptr.p);
290                                 break;
291                         case UCI_TYPE_SECTION:
292                                 uci_show_section(ptr.s);
293                                 break;
294                         case UCI_TYPE_OPTION:
295                                 uci_show_option(ptr.o);
296                                 break;
297                         default:
298                                 /* should not happen */
299                                 return 1;
300                 }
301                 break;
302         }
303
304         uci_unload(ctx, ptr.p);
305         return 0;
306 }
307
308 static int uci_do_import(int argc, char **argv)
309 {
310         struct uci_package *package = NULL;
311         char *name = NULL;
312         int ret = UCI_OK;
313         bool merge = false;
314
315         if (argc > 2)
316                 return 255;
317
318         if (argc == 2)
319                 name = argv[1];
320         else if (flags & CLI_FLAG_MERGE)
321                 /* need a package to merge */
322                 return 255;
323
324         if (flags & CLI_FLAG_MERGE) {
325                 if (uci_load(ctx, name, &package) != UCI_OK)
326                         package = NULL;
327                 else
328                         merge = true;
329         }
330         ret = uci_import(ctx, input, name, &package, (name != NULL));
331         if (ret == UCI_OK) {
332                 if (merge) {
333                         ret = uci_save(ctx, package);
334                 } else {
335                         struct uci_element *e;
336                         /* loop through all config sections and overwrite existing data */
337                         uci_foreach_element(&ctx->root, e) {
338                                 struct uci_package *p = uci_to_package(e);
339                                 ret = uci_commit(ctx, &p, true);
340                         }
341                 }
342         }
343
344         if (ret != UCI_OK) {
345                 cli_perror();
346                 return 1;
347         }
348
349         return 0;
350 }
351
352 static int uci_do_package_cmd(int cmd, int argc, char **argv)
353 {
354         char **configs = NULL;
355         char **p;
356
357         if (argc > 2)
358                 return 255;
359
360         if (argc == 2)
361                 return package_cmd(cmd, argv[1]);
362
363         if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
364                 cli_perror();
365                 return 1;
366         }
367
368         for (p = configs; *p; p++) {
369                 package_cmd(cmd, *p);
370         }
371
372         return 0;
373 }
374
375 static int uci_do_add(int argc, char **argv)
376 {
377         struct uci_package *p = NULL;
378         struct uci_section *s = NULL;
379         int ret;
380
381         if (argc != 3)
382                 return 255;
383
384         ret = uci_load(ctx, argv[1], &p);
385         if (ret != UCI_OK)
386                 goto done;
387
388         ret = uci_add_section(ctx, p, argv[2], &s);
389         if (ret != UCI_OK)
390                 goto done;
391
392         ret = uci_save(ctx, p);
393
394 done:
395         if (ret != UCI_OK)
396                 cli_perror();
397         else if (s)
398                 fprintf(stdout, "%s\n", s->e.name);
399
400         return ret;
401 }
402
403 static int uci_do_section_cmd(int cmd, int argc, char **argv)
404 {
405         struct uci_element *e;
406         struct uci_ptr ptr;
407         int ret = UCI_OK;
408
409         if (argc != 2)
410                 return 255;
411
412         if (uci_lookup_ptr(ctx, &ptr, argv[1], true) != UCI_OK) {
413                 cli_perror();
414                 return 1;
415         }
416
417         if (ptr.value && (cmd != CMD_SET) && (cmd != CMD_ADD_LIST) && (cmd != CMD_RENAME) && (cmd != CMD_REORDER))
418                 return 1;
419
420         e = ptr.last;
421         switch(cmd) {
422         case CMD_GET:
423                 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
424                         ctx->err = UCI_ERR_NOTFOUND;
425                         cli_perror();
426                         return 1;
427                 }
428                 switch(e->type) {
429                 case UCI_TYPE_SECTION:
430                         printf("%s\n", ptr.s->type);
431                         break;
432                 case UCI_TYPE_OPTION:
433                         uci_show_value(ptr.o);
434                         break;
435                 default:
436                         break;
437                 }
438                 /* throw the value to stdout */
439                 break;
440         case CMD_RENAME:
441                 ret = uci_rename(ctx, &ptr);
442                 break;
443         case CMD_REVERT:
444                 ret = uci_revert(ctx, &ptr);
445                 break;
446         case CMD_SET:
447                 ret = uci_set(ctx, &ptr);
448                 break;
449         case CMD_ADD_LIST:
450                 ret = uci_add_list(ctx, &ptr);
451                 break;
452         case CMD_REORDER:
453                 if (!ptr.s || !ptr.value) {
454                         ctx->err = UCI_ERR_NOTFOUND;
455                         cli_perror();
456                         return 1;
457                 }
458                 ret = uci_reorder_section(ctx, ptr.s, strtoul(ptr.value, NULL, 10));
459                 break;
460         case CMD_DEL:
461                 ret = uci_delete(ctx, &ptr);
462                 break;
463         }
464
465         /* no save necessary for get */
466         if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
467                 return 0;
468
469         /* save changes, but don't commit them yet */
470         if (ret == UCI_OK)
471                 ret = uci_save(ctx, ptr.p);
472
473         if (ret != UCI_OK) {
474                 cli_perror();
475                 return 1;
476         }
477
478         return 0;
479 }
480
481 static int uci_batch_cmd(void)
482 {
483         char *argv[MAX_ARGS + 2];
484         char *str = NULL;
485         int ret = 0;
486         int i, j;
487
488         for(i = 0; i <= MAX_ARGS; i++) {
489                 if (i == MAX_ARGS) {
490                         fprintf(stderr, "Too many arguments\n");
491                         return 1;
492                 }
493                 argv[i] = NULL;
494                 if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
495                         cli_perror();
496                         i = 0;
497                         break;
498                 }
499                 if (!argv[i][0])
500                         break;
501                 argv[i] = strdup(argv[i]);
502                 if (!argv[i]) {
503                         perror("uci");
504                         return 1;
505                 }
506         }
507         argv[i] = NULL;
508
509         if (i > 0) {
510                 if (!strcasecmp(argv[0], "exit"))
511                         return 254;
512                 ret = uci_cmd(i, argv);
513         } else
514                 return 0;
515
516         for (j = 0; j < i; j++) {
517                 if (argv[j])
518                         free(argv[j]);
519         }
520
521         return ret;
522 }
523
524 static int uci_batch(void)
525 {
526         int ret = 0;
527
528         flags |= CLI_FLAG_BATCH;
529         while (!feof(input)) {
530                 struct uci_element *e, *tmp;
531
532                 ret = uci_batch_cmd();
533                 if (ret == 254)
534                         return 0;
535                 else if (ret == 255)
536                         fprintf(stderr, "Unknown command\n");
537
538                 /* clean up */
539                 uci_foreach_element_safe(&ctx->root, tmp, e) {
540                         uci_unload(ctx, uci_to_package(e));
541                 }
542         }
543         flags &= ~CLI_FLAG_BATCH;
544
545         return 0;
546 }
547
548 static int uci_cmd(int argc, char **argv)
549 {
550         int cmd = 0;
551
552         if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
553                 return uci_batch();
554         else if (!strcasecmp(argv[0], "show"))
555                 cmd = CMD_SHOW;
556         else if (!strcasecmp(argv[0], "changes"))
557                 cmd = CMD_CHANGES;
558         else if (!strcasecmp(argv[0], "export"))
559                 cmd = CMD_EXPORT;
560         else if (!strcasecmp(argv[0], "commit"))
561                 cmd = CMD_COMMIT;
562         else if (!strcasecmp(argv[0], "get"))
563                 cmd = CMD_GET;
564         else if (!strcasecmp(argv[0], "set"))
565                 cmd = CMD_SET;
566         else if (!strcasecmp(argv[0], "ren") ||
567                  !strcasecmp(argv[0], "rename"))
568                 cmd = CMD_RENAME;
569         else if (!strcasecmp(argv[0], "revert"))
570                 cmd = CMD_REVERT;
571         else if (!strcasecmp(argv[0], "reorder"))
572                 cmd = CMD_REORDER;
573         else if (!strcasecmp(argv[0], "del") ||
574                  !strcasecmp(argv[0], "delete"))
575                 cmd = CMD_DEL;
576         else if (!strcasecmp(argv[0], "import"))
577                 cmd = CMD_IMPORT;
578         else if (!strcasecmp(argv[0], "help"))
579                 cmd = CMD_HELP;
580         else if (!strcasecmp(argv[0], "add"))
581                 cmd = CMD_ADD;
582         else if (!strcasecmp(argv[0], "add_list"))
583                 cmd = CMD_ADD_LIST;
584         else
585                 cmd = -1;
586
587         switch(cmd) {
588                 case CMD_ADD_LIST:
589                 case CMD_GET:
590                 case CMD_SET:
591                 case CMD_DEL:
592                 case CMD_RENAME:
593                 case CMD_REVERT:
594                 case CMD_REORDER:
595                         return uci_do_section_cmd(cmd, argc, argv);
596                 case CMD_SHOW:
597                 case CMD_EXPORT:
598                 case CMD_COMMIT:
599                 case CMD_CHANGES:
600                         return uci_do_package_cmd(cmd, argc, argv);
601                 case CMD_IMPORT:
602                         return uci_do_import(argc, argv);
603                 case CMD_ADD:
604                         return uci_do_add(argc, argv);
605                 case CMD_HELP:
606                         uci_usage();
607                         return 0;
608                 default:
609                         return 255;
610         }
611 }
612
613 int main(int argc, char **argv)
614 {
615         int ret;
616         int c;
617
618         flags = CLI_FLAG_SHOW_EXT;
619         appname = argv[0];
620         input = stdin;
621         ctx = uci_alloc_context();
622         if (!ctx) {
623                 fprintf(stderr, "Out of memory\n");
624                 return 1;
625         }
626
627         while((c = getopt(argc, argv, "c:d:f:LmnNp:P:sSqX")) != -1) {
628                 switch(c) {
629                         case 'c':
630                                 uci_set_confdir(ctx, optarg);
631                                 break;
632                         case 'd':
633                                 delimiter = optarg;
634                                 break;
635                         case 'f':
636                                 if (input != stdin) {
637                                         perror("uci");
638                                         return 1;
639                                 }
640
641                                 input = fopen(optarg, "r");
642                                 if (!input) {
643                                         perror("uci");
644                                         return 1;
645                                 }
646                                 break;
647                         case 'L':
648                                 flags |= CLI_FLAG_NOPLUGINS;
649                                 break;
650                         case 'm':
651                                 flags |= CLI_FLAG_MERGE;
652                                 break;
653                         case 's':
654                                 ctx->flags |= UCI_FLAG_STRICT;
655                                 break;
656                         case 'S':
657                                 ctx->flags &= ~UCI_FLAG_STRICT;
658                                 ctx->flags |= UCI_FLAG_PERROR;
659                                 break;
660                         case 'n':
661                                 ctx->flags |= UCI_FLAG_EXPORT_NAME;
662                                 break;
663                         case 'N':
664                                 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
665                                 break;
666                         case 'p':
667                                 uci_add_delta_path(ctx, optarg);
668                                 break;
669                         case 'P':
670                                 uci_add_delta_path(ctx, ctx->savedir);
671                                 uci_set_savedir(ctx, optarg);
672                                 flags |= CLI_FLAG_NOCOMMIT;
673                                 break;
674                         case 'q':
675                                 flags |= CLI_FLAG_QUIET;
676                                 break;
677                         case 'X':
678                                 flags &= ~CLI_FLAG_SHOW_EXT;
679                                 break;
680                         default:
681                                 uci_usage();
682                                 return 0;
683                 }
684         }
685         if (optind > 1)
686                 argv[optind - 1] = argv[0];
687         argv += optind - 1;
688         argc -= optind - 1;
689
690         if (argc < 2) {
691                 uci_usage();
692                 return 0;
693         }
694
695         if (!(flags & CLI_FLAG_NOPLUGINS))
696                 uci_load_plugins(ctx, NULL);
697
698         ret = uci_cmd(argc - 1, argv + 1);
699         if (input != stdin)
700                 fclose(input);
701
702         if (ret == 255)
703                 uci_usage();
704
705         uci_free_context(ctx);
706
707         return ret;
708 }