cli: check against multiple -f invocations to avoid leaks (patch by Stanislav Fomichev)
[project/uci.git] / delta.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 Lesser General Public License for more details.
13  */
14
15 /*
16  * This file contains the code for handling uci config delta files
17  */
18
19 #define _GNU_SOURCE
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/file.h>
23 #include <stdbool.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #include "uci.h"
32 #include "uci_internal.h"
33
34 /* record a change that was done to a package */
35 void
36 uci_add_delta(struct uci_context *ctx, struct uci_list *list, int cmd, const char *section, const char *option, const char *value)
37 {
38         struct uci_delta *h;
39         int size = strlen(section) + 1;
40         char *ptr;
41
42         if (value)
43                 size += strlen(value) + 1;
44
45         h = uci_alloc_element(ctx, delta, option, size);
46         ptr = uci_dataptr(h);
47         h->cmd = cmd;
48         h->section = strcpy(ptr, section);
49         if (value) {
50                 ptr += strlen(ptr) + 1;
51                 h->value = strcpy(ptr, value);
52         }
53         uci_list_add(list, &h->e.list);
54 }
55
56 void
57 uci_free_delta(struct uci_delta *h)
58 {
59         if (!h)
60                 return;
61         if ((h->section != NULL) &&
62                 (h->section != uci_dataptr(h))) {
63                 free(h->section);
64                 free(h->value);
65         }
66         uci_free_element(&h->e);
67 }
68
69
70 int uci_set_savedir(struct uci_context *ctx, const char *dir)
71 {
72         char *sdir;
73
74         UCI_HANDLE_ERR(ctx);
75         UCI_ASSERT(ctx, dir != NULL);
76
77         sdir = uci_strdup(ctx, dir);
78         if (ctx->savedir != uci_savedir)
79                 free(ctx->savedir);
80         ctx->savedir = sdir;
81         return 0;
82 }
83
84 int uci_add_delta_path(struct uci_context *ctx, const char *dir)
85 {
86         struct uci_element *e;
87
88         UCI_HANDLE_ERR(ctx);
89         UCI_ASSERT(ctx, dir != NULL);
90         e = uci_alloc_generic(ctx, UCI_TYPE_PATH, dir, sizeof(struct uci_element));
91         uci_list_add(&ctx->delta_path, &e->list);
92
93         return 0;
94 }
95
96 static inline int uci_parse_delta_tuple(struct uci_context *ctx, char **buf, struct uci_ptr *ptr)
97 {
98         int c = UCI_CMD_CHANGE;
99
100         switch(**buf) {
101         case '^':
102                 c = UCI_CMD_REORDER;
103                 break;
104         case '-':
105                 c = UCI_CMD_REMOVE;
106                 break;
107         case '@':
108                 c = UCI_CMD_RENAME;
109                 break;
110         case '+':
111                 /* UCI_CMD_ADD is used for anonymous sections or list values */
112                 c = UCI_CMD_ADD;
113                 break;
114         case '|':
115                 c = UCI_CMD_LIST_ADD;
116                 break;
117         }
118
119         if (c != UCI_CMD_CHANGE)
120                 *buf += 1;
121
122         UCI_INTERNAL(uci_parse_ptr, ctx, ptr, *buf);
123
124         if (!ptr->section)
125                 goto error;
126         if (ptr->flags & UCI_LOOKUP_EXTENDED)
127                 goto error;
128
129         switch(c) {
130         case UCI_CMD_REORDER:
131                 if (!ptr->value || ptr->option)
132                         goto error;
133                 break;
134         case UCI_CMD_RENAME:
135                 if (!ptr->value || !uci_validate_name(ptr->value))
136                         goto error;
137                 break;
138         case UCI_CMD_LIST_ADD:
139                 if (!ptr->option)
140                         goto error;
141         }
142
143         return c;
144
145 error:
146         UCI_THROW(ctx, UCI_ERR_INVAL);
147         return 0;
148 }
149
150 static void uci_parse_delta_line(struct uci_context *ctx, struct uci_package *p, char *buf)
151 {
152         struct uci_element *e = NULL;
153         struct uci_ptr ptr;
154         int cmd;
155
156         cmd = uci_parse_delta_tuple(ctx, &buf, &ptr);
157         if (strcmp(ptr.package, p->e.name) != 0)
158                 goto error;
159
160         if (ctx->flags & UCI_FLAG_SAVED_DELTA)
161                 uci_add_delta(ctx, &p->saved_delta, cmd, ptr.section, ptr.option, ptr.value);
162
163         switch(cmd) {
164         case UCI_CMD_REORDER:
165                 uci_expand_ptr(ctx, &ptr, true);
166                 if (!ptr.s)
167                         UCI_THROW(ctx, UCI_ERR_NOTFOUND);
168                 UCI_INTERNAL(uci_reorder_section, ctx, ptr.s, strtoul(ptr.value, NULL, 10));
169                 break;
170         case UCI_CMD_RENAME:
171                 UCI_INTERNAL(uci_rename, ctx, &ptr);
172                 break;
173         case UCI_CMD_REMOVE:
174                 UCI_INTERNAL(uci_delete, ctx, &ptr);
175                 break;
176         case UCI_CMD_LIST_ADD:
177                 UCI_INTERNAL(uci_add_list, ctx, &ptr);
178                 break;
179         case UCI_CMD_ADD:
180         case UCI_CMD_CHANGE:
181                 UCI_INTERNAL(uci_set, ctx, &ptr);
182                 e = ptr.last;
183                 if (!ptr.option && e && (cmd == UCI_CMD_ADD))
184                         uci_to_section(e)->anonymous = true;
185                 break;
186         }
187         return;
188 error:
189         UCI_THROW(ctx, UCI_ERR_PARSE);
190 }
191
192 /* returns the number of changes that were successfully parsed */
193 static int uci_parse_delta(struct uci_context *ctx, FILE *stream, struct uci_package *p)
194 {
195         struct uci_parse_context *pctx;
196         int changes = 0;
197
198         /* make sure no memory from previous parse attempts is leaked */
199         uci_cleanup(ctx);
200
201         pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
202         ctx->pctx = pctx;
203         pctx->file = stream;
204
205         while (!feof(pctx->file)) {
206                 uci_getln(ctx, 0);
207                 if (!pctx->buf[0])
208                         continue;
209
210                 /*
211                  * ignore parse errors in single lines, we want to preserve as much
212                  * delta as possible
213                  */
214                 UCI_TRAP_SAVE(ctx, error);
215                 uci_parse_delta_line(ctx, p, pctx->buf);
216                 UCI_TRAP_RESTORE(ctx);
217                 changes++;
218 error:
219                 continue;
220         }
221
222         /* no error happened, we can get rid of the parser context now */
223         uci_cleanup(ctx);
224         return changes;
225 }
226
227 /* returns the number of changes that were successfully parsed */
228 static int uci_load_delta_file(struct uci_context *ctx, struct uci_package *p, char *filename, FILE **f, bool flush)
229 {
230         FILE *stream = NULL;
231         int changes = 0;
232
233         UCI_TRAP_SAVE(ctx, done);
234         stream = uci_open_stream(ctx, filename, SEEK_SET, flush, false);
235         if (p)
236                 changes = uci_parse_delta(ctx, stream, p);
237         UCI_TRAP_RESTORE(ctx);
238 done:
239         if (f)
240                 *f = stream;
241         else if (stream)
242                 uci_close_stream(stream);
243         return changes;
244 }
245
246 /* returns the number of changes that were successfully parsed */
247 __private int uci_load_delta(struct uci_context *ctx, struct uci_package *p, bool flush)
248 {
249         struct uci_element *e;
250         char *filename = NULL;
251         FILE *f = NULL;
252         int changes = 0;
253
254         if (!p->has_delta)
255                 return 0;
256
257         uci_foreach_element(&ctx->delta_path, e) {
258                 if ((asprintf(&filename, "%s/%s", e->name, p->e.name) < 0) || !filename)
259                         UCI_THROW(ctx, UCI_ERR_MEM);
260
261                 uci_load_delta_file(ctx, p, filename, NULL, false);
262                 free(filename);
263         }
264
265         if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
266                 UCI_THROW(ctx, UCI_ERR_MEM);
267
268         changes = uci_load_delta_file(ctx, p, filename, &f, flush);
269         if (flush && f && (changes > 0)) {
270                 rewind(f);
271                 if (ftruncate(fileno(f), 0) < 0) {
272                         uci_close_stream(f);
273                         UCI_THROW(ctx, UCI_ERR_IO);
274                 }
275         }
276         if (filename)
277                 free(filename);
278         uci_close_stream(f);
279         ctx->err = 0;
280         return changes;
281 }
282
283 static void uci_filter_delta(struct uci_context *ctx, const char *name, const char *section, const char *option)
284 {
285         struct uci_parse_context *pctx;
286         struct uci_element *e, *tmp;
287         struct uci_list list;
288         char *filename = NULL;
289         struct uci_ptr ptr;
290         FILE *f = NULL;
291
292         uci_list_init(&list);
293         uci_alloc_parse_context(ctx);
294         pctx = ctx->pctx;
295
296         if ((asprintf(&filename, "%s/%s", ctx->savedir, name) < 0) || !filename)
297                 UCI_THROW(ctx, UCI_ERR_MEM);
298
299         UCI_TRAP_SAVE(ctx, done);
300         f = uci_open_stream(ctx, filename, SEEK_SET, true, false);
301         pctx->file = f;
302         while (!feof(f)) {
303                 struct uci_element *e;
304                 char *buf;
305
306                 uci_getln(ctx, 0);
307                 buf = pctx->buf;
308                 if (!buf[0])
309                         continue;
310
311                 /* NB: need to allocate the element before the call to 
312                  * uci_parse_delta_tuple, otherwise the original string 
313                  * gets modified before it is saved */
314                 e = uci_alloc_generic(ctx, UCI_TYPE_DELTA, pctx->buf, sizeof(struct uci_element));
315                 uci_list_add(&list, &e->list);
316
317                 uci_parse_delta_tuple(ctx, &buf, &ptr);
318                 if (section) {
319                         if (!ptr.section || (strcmp(section, ptr.section) != 0))
320                                 continue;
321                 }
322                 if (option) {
323                         if (!ptr.option || (strcmp(option, ptr.option) != 0))
324                                 continue;
325                 }
326                 /* match, drop this element again */
327                 uci_free_element(e);
328         }
329
330         /* rebuild the delta file */
331         rewind(f);
332         if (ftruncate(fileno(f), 0) < 0)
333                 UCI_THROW(ctx, UCI_ERR_IO);
334         uci_foreach_element_safe(&list, tmp, e) {
335                 fprintf(f, "%s\n", e->name);
336                 uci_free_element(e);
337         }
338         UCI_TRAP_RESTORE(ctx);
339
340 done:
341         if (filename)
342                 free(filename);
343         uci_close_stream(pctx->file);
344         uci_foreach_element_safe(&list, tmp, e) {
345                 uci_free_element(e);
346         }
347         uci_cleanup(ctx);
348 }
349
350 int uci_revert(struct uci_context *ctx, struct uci_ptr *ptr)
351 {
352         char *package = NULL;
353         char *section = NULL;
354         char *option = NULL;
355
356         UCI_HANDLE_ERR(ctx);
357         uci_expand_ptr(ctx, ptr, false);
358         UCI_ASSERT(ctx, ptr->p->has_delta);
359
360         /* 
361          * - flush unwritten changes
362          * - save the package name
363          * - unload the package
364          * - filter the delta
365          * - reload the package
366          */
367         UCI_TRAP_SAVE(ctx, error);
368         UCI_INTERNAL(uci_save, ctx, ptr->p);
369
370         /* NB: need to clone package, section and option names, 
371          * as they may get freed on uci_free_package() */
372         package = uci_strdup(ctx, ptr->p->e.name);
373         if (ptr->section)
374                 section = uci_strdup(ctx, ptr->section);
375         if (ptr->option)
376                 option = uci_strdup(ctx, ptr->option);
377
378         uci_free_package(&ptr->p);
379         uci_filter_delta(ctx, package, section, option);
380
381         UCI_INTERNAL(uci_load, ctx, package, &ptr->p);
382         UCI_TRAP_RESTORE(ctx);
383         ctx->err = 0;
384
385 error:
386         if (package)
387                 free(package);
388         if (section)
389                 free(section);
390         if (option)
391                 free(option);
392         if (ctx->err)
393                 UCI_THROW(ctx, ctx->err);
394         return 0;
395 }
396
397 int uci_save(struct uci_context *ctx, struct uci_package *p)
398 {
399         FILE *f = NULL;
400         char *filename = NULL;
401         struct uci_element *e, *tmp;
402         struct stat statbuf;
403
404         UCI_HANDLE_ERR(ctx);
405         UCI_ASSERT(ctx, p != NULL);
406
407         /* 
408          * if the config file was outside of the /etc/config path,
409          * don't save the delta to a file, update the real file
410          * directly.
411          * does not modify the uci_package pointer
412          */
413         if (!p->has_delta)
414                 return uci_commit(ctx, &p, false);
415
416         if (uci_list_empty(&p->delta))
417                 return 0;
418
419         if (stat(ctx->savedir, &statbuf) < 0)
420                 mkdir(ctx->savedir, UCI_DIRMODE);
421         else if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
422                 UCI_THROW(ctx, UCI_ERR_IO);
423
424         if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
425                 UCI_THROW(ctx, UCI_ERR_MEM);
426
427         uci_foreach_element(&ctx->hooks, tmp) {
428                 struct uci_hook *hook = uci_to_hook(tmp);
429
430                 if (!hook->ops->set)
431                         continue;
432
433                 uci_foreach_element(&p->delta, e) {
434                         hook->ops->set(hook->ops, p, uci_to_delta(e));
435                 }
436         }
437
438         ctx->err = 0;
439         UCI_TRAP_SAVE(ctx, done);
440         f = uci_open_stream(ctx, filename, SEEK_END, true, true);
441         UCI_TRAP_RESTORE(ctx);
442
443         uci_foreach_element_safe(&p->delta, tmp, e) {
444                 struct uci_delta *h = uci_to_delta(e);
445                 char *prefix = "";
446
447                 switch(h->cmd) {
448                 case UCI_CMD_REMOVE:
449                         prefix = "-";
450                         break;
451                 case UCI_CMD_RENAME:
452                         prefix = "@";
453                         break;
454                 case UCI_CMD_ADD:
455                         prefix = "+";
456                         break;
457                 case UCI_CMD_REORDER:
458                         prefix = "^";
459                         break;
460                 case UCI_CMD_LIST_ADD:
461                         prefix = "|";
462                         break;
463                 default:
464                         break;
465                 }
466
467                 fprintf(f, "%s%s.%s", prefix, p->e.name, h->section);
468                 if (e->name)
469                         fprintf(f, ".%s", e->name);
470
471                 if (h->cmd == UCI_CMD_REMOVE)
472                         fprintf(f, "\n");
473                 else
474                         fprintf(f, "=%s\n", h->value);
475                 uci_free_delta(h);
476         }
477
478 done:
479         uci_close_stream(f);
480         if (filename)
481                 free(filename);
482         if (ctx->err)
483                 UCI_THROW(ctx, ctx->err);
484
485         return 0;
486 }
487
488