8a47f46b7248fa069f6aa347ed1daf9e1023aa04
[radsecproxy.git] / gconfig.c
1 /* Copyright (c) 2006-2010, UNINETT AS.
2  * Copyright (c) 2010, UNINETT AS, NORDUnet A/S.
3  * Copyright (c) 2010-2012, NORDUnet A/S. */
4 /* See LICENSE for licensing information. */
5
6 #include <string.h>
7 #include <stdarg.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <limits.h>
11 #include <glob.h>
12 #include <sys/types.h>
13 #include <ctype.h>
14 #include <libgen.h>
15 #include <errno.h>
16 #include "debug.h"
17 #include "util.h"
18 #include "gconfig.h"
19
20 /* returns NULL on error, where to continue parsing if token and ok. E.g. "" will return token with empty string */
21 char *strtokenquote(char *s, char **token, char *del, char *quote, char *comment) {
22     char *t = s, *q, *r;
23
24     if (!t || !token || !del)
25         return NULL;
26     while (*t && strchr(del, *t))
27         t++;
28     if (!*t || (comment && strchr(comment, *t))) {
29         *token = NULL;
30         return t + 1; /* needs to be non-NULL, but value doesn't matter */
31     }
32     if (quote && (q = strchr(quote, *t))) {
33         t++;
34         r = t;
35         while (*t && *t != *q)
36             t++;
37         if (!*t || (t[1] && !strchr(del, t[1])))
38             return NULL;
39         *t = '\0';
40         *token = r;
41         return t + 1;
42     }
43     *token = t;
44     t++;
45     while (*t && !strchr(del, *t))
46         t++;
47     *t = '\0';
48     return t + 1;
49 }
50
51 int pushgconfdata(struct gconffile **cf, const char *data) {
52     int i;
53     struct gconffile *newcf;
54
55     if (!*cf) {
56         newcf = malloc(sizeof(struct gconffile) * 2);
57         if (!newcf)
58             return 0;
59         memset(newcf, 0, sizeof(struct gconffile) * 2);
60     } else {
61         for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
62         newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
63         if (!newcf)
64             return 0;
65         memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
66         memset(newcf, 0, sizeof(struct gconffile));
67     }
68     newcf[0].data = data;
69     *cf = newcf;
70     return 1;
71 }
72
73 FILE *pushgconffile(struct gconffile **cf, FILE *file, const char *description) {
74     int i;
75     struct gconffile *newcf;
76     char *desc;
77
78     if (!file) {
79         debug(DBG_INFO, "could not read config from %s", description);
80         return NULL;
81     }
82     debug(DBG_DBG, "reading config from %s", description);
83
84     desc = stringcopy(description, 0);
85     if (!desc)
86         goto errmalloc;
87
88     if (!*cf) {
89         newcf = malloc(sizeof(struct gconffile) * 2);
90         if (!newcf)
91             goto errmalloc;
92         memset(newcf, 0, sizeof(struct gconffile) * 2);
93     } else {
94         for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
95         newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
96         if (!newcf)
97             goto errmalloc;
98         memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
99         memset(newcf, 0, sizeof(struct gconffile));
100     }
101     newcf[0].file = file;
102     newcf[0].path = desc;
103     *cf = newcf;
104     return file;
105
106 errmalloc:
107     free(desc);
108     fclose(file);
109     debug(DBG_ERR, "malloc failed");
110     return NULL;
111 }
112
113 FILE *pushgconfpath(struct gconffile **cf, const char *path) {
114     FILE *f;
115
116     f = fopen(path, "r");
117     return pushgconffile(cf, f, path);
118 }
119
120 FILE *pushgconfpaths(struct gconffile **cf, const char *cfgpath) {
121     int i;
122     FILE *f = NULL;
123     glob_t globbuf;
124     char *path, *curfile = NULL, *dir;
125
126     /* if cfgpath is relative, make it relative to current config */
127     if (*cfgpath == '/')
128         path = (char *)cfgpath;
129     else {
130         /* dirname may modify its argument */
131         curfile = stringcopy((*cf)->path, 0);
132         if (!curfile) {
133             debug(DBG_ERR, "malloc failed");
134             goto exit;
135         }
136         dir = dirname(curfile);
137         path = malloc(strlen(dir) + strlen(cfgpath) + 2);
138         if (!path) {
139             debug(DBG_ERR, "malloc failed");
140             goto exit;
141         }
142         strcpy(path, dir);
143         path[strlen(dir)] = '/';
144         strcpy(path + strlen(dir) + 1, cfgpath);
145     }
146     memset(&globbuf, 0, sizeof(glob_t));
147     if (glob(path, 0, NULL, &globbuf)) {
148         debug(DBG_WARN, "could not glob %s", path);
149         goto exit;
150     }
151
152     for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
153         f = pushgconfpath(cf, globbuf.gl_pathv[i]);
154         if (!f)
155             break;
156     }
157     globfree(&globbuf);
158
159 exit:
160     if (curfile) {
161         free(curfile);
162         free(path);
163     }
164     return f;
165 }
166
167 int popgconf(struct gconffile **cf) {
168     int i;
169
170     if (!*cf)
171         return 0;
172     for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
173     if (i && (*cf)[0].file) {
174         fclose((*cf)[0].file);
175         if ((*cf)[0].path) {
176             debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
177             free((*cf)[0].path);
178         }
179     }
180     if (i < 2) {
181         free(*cf);
182         *cf = NULL;
183         return 0;
184     }
185     memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
186     return 1;
187 }
188
189 void freegconfmstr(char **mstr) {
190     int i;
191
192     if (mstr) {
193         for (i = 0; mstr[i]; i++)
194             free(mstr[i]);
195         free(mstr);
196     }
197 }
198
199 void freegconf(struct gconffile **cf) {
200     int i;
201
202     if (!*cf)
203         return;
204
205     for (i = 0; (*cf)[i].data || (*cf)[i].path; i++) {
206         if ((*cf)[i].file) {
207             fclose((*cf)[i].file);
208             if ((*cf)[i].path) {
209                 debug(DBG_DBG, "closing config file %s", (*cf)[i].path);
210                 free((*cf)[i].path);
211             }
212         }
213     }
214     free(*cf);
215     *cf = NULL;
216 }
217
218 struct gconffile *openconfigfile(const char *file) {
219     struct gconffile *cf = NULL;
220
221     if (!pushgconfpath(&cf, file)) {
222         debug(DBG_ERR, "could not read config file %s\n%s", file, strerror(errno));
223         return NULL;
224     }
225     debug(DBG_DBG, "reading config file %s", file);
226     return cf;
227 }
228
229 /* Parses config with following syntax:
230  * One of these:
231  * option-name value
232  * option-name = value
233  * Or:
234  * option-name value {
235  *     option-name [=] value
236  *     ...
237  * }
238  */
239
240 int getlinefromcf(struct gconffile *cf, char *line, const size_t size) {
241     size_t i, pos;
242
243     if (!cf)
244         return 0;
245
246     if (cf->file)
247         return fgets(line, size, cf->file) ? 1 : 0;
248     else if (cf->data) {
249         pos = cf->datapos;
250         if (!cf->data[pos])
251             return 0;
252         for (i = pos; cf->data[i] && cf->data[i] != '\n'; i++);
253         if (cf->data[i] == '\n')
254             i++;
255         if (i - pos > size - 1)
256             i = size - 1 + pos;
257         memcpy(line, cf->data + pos, i - pos);
258         line[i - pos] = '\0';
259         cf->datapos = i;
260         return 1;
261     }
262     return 0;
263 }
264
265 int getconfigline(struct gconffile **cf, char *block, char **opt, char **val, int *conftype) {
266     char line[1024];
267     char *tokens[3], *s;
268     int tcount;
269
270     *opt = NULL;
271     *val = NULL;
272     *conftype = 0;
273
274     if (!cf || !*cf || (!(*cf)->file && !(*cf)->data))
275         return 1;
276
277     for (;;) {
278         if (!getlinefromcf(*cf, line, 1024)) {
279             if (popgconf(cf))
280                 continue;
281             return 1;
282         }
283         s = line;
284         for (tcount = 0; tcount < 3; tcount++) {
285             s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
286             if (!s) {
287                 debug(DBG_ERR, "Syntax error in line starting with: %s", line);
288                 return 0;
289             }
290             if (!tokens[tcount])
291                 break;
292         }
293         if (!tcount || **tokens == '#')
294             continue;
295
296         if (**tokens == '}') {
297             if (block)
298                 return 1;
299             debug(DBG_ERR, "configuration error, found } with no matching {");
300             return 0;
301         }
302         break;
303     }
304
305     switch (tcount) {
306     case 2:
307         *opt = stringcopy(tokens[0], 0);
308         if (!*opt)
309             goto errmalloc;
310         *val = stringcopy(tokens[1], 0);
311         if (!*val)
312             goto errmalloc;
313         *conftype = CONF_STR;
314         break;
315     case 3:
316         if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
317             *opt = stringcopy(tokens[0], 0);
318             if (!*opt)
319                 goto errmalloc;
320             *val = stringcopy(tokens[2], 0);
321             if (!*val)
322                 goto errmalloc;
323             *conftype = CONF_STR;
324             break;
325         }
326         if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
327             *opt = stringcopy(tokens[0], 0);
328             if (!*opt)
329                 goto errmalloc;
330             *val = stringcopy(tokens[1], 0);
331             if (!*val)
332                 goto errmalloc;
333             *conftype = CONF_CBK;
334             break;
335         }
336         /* fall through */
337     default:
338         if (block)
339             debug(DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
340         else
341             debug(DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
342         return 0;
343     }
344
345     if (**val)
346         return 1;
347
348     debug(DBG_ERR, "configuration error, option %s needs a non-empty value", *opt);
349     goto errexit;
350
351 errmalloc:
352     debug(DBG_ERR, "malloc failed");
353 errexit:
354     free(*opt);
355     *opt = NULL;
356     free(*val);
357     *val = NULL;
358     return 0;
359 }
360
361 uint8_t hexdigit2int(char d) {
362     if (d >= '0' && d <= '9')
363         return d - '0';
364     if (d >= 'a' && d <= 'f')
365         return 10 + d - 'a';
366     if (d >= 'A' && d <= 'F')
367         return 10 + d - 'A';
368     return 0;
369 }
370
371 void unhex(char *s) {
372     char *t;
373     for (t = s; *t; s++) {
374         if (*t == '%' && isxdigit((int)t[1]) && isxdigit((int)t[2])) {
375             *s = 16 * hexdigit2int(t[1]) + hexdigit2int(t[2]);
376             t += 3;
377         } else
378             *s = *t++;
379     }
380     *s = '\0';
381 }
382
383 typedef int (*t_fptr)(struct gconffile **, void *, char *, char *, char *);
384
385 /* returns 1 if ok, 0 on error */
386 /* caller must free returned values also on error */
387 int getgenericconfig(struct gconffile **cf, char *block, ...) {
388     va_list ap;
389     char *opt = NULL, *val, *word, *optval, **str = NULL, ***mstr = NULL, **newmstr, *endptr;
390     uint8_t *bln = NULL;
391     long int *lint = NULL;
392     int type = 0, conftype = 0, n;
393     t_fptr cbk = NULL;
394     void *cbkarg = NULL;
395
396     for (;;) {
397         free(opt);
398         if (!getconfigline(cf, block, &opt, &val, &conftype))
399             return 0;
400         if (!opt)
401             return 1;
402
403         if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
404             if (!pushgconfpaths(cf, val)) {
405                 debug(DBG_ERR, "failed to include config file %s", val);
406                 goto errexit;
407             }
408             free(val);
409             continue;
410         }
411
412         va_start(ap, block);
413         while ((word = va_arg(ap, char *))) {
414             type = va_arg(ap, int);
415             switch (type) {
416             case CONF_STR:
417                 str = va_arg(ap, char **);
418                 if (!str)
419                     goto errparam;
420                 break;
421             case CONF_MSTR:
422                 mstr = va_arg(ap, char ***);
423                 if (!mstr)
424                     goto errparam;
425                 break;
426             case CONF_BLN:
427                 bln = va_arg(ap, uint8_t *);
428                 if (!bln)
429                     goto errparam;
430                 break;
431             case CONF_LINT:
432                 lint = va_arg(ap, long int *);
433                 if (!lint)
434                     goto errparam;
435                 break;
436             case CONF_CBK:
437                 cbk = va_arg(ap, t_fptr);
438                 if (!cbk)
439                     goto errparam;
440                 cbkarg = va_arg(ap, void *);
441                 break;
442             default:
443                 goto errparam;
444             }
445             if (!strcasecmp(opt, word))
446                 break;
447         }
448         va_end(ap);
449
450         if (!word) {
451             if (block)
452                 debug(DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
453             debug(DBG_ERR, "configuration error, unknown option %s", opt);
454             goto errexit;
455         }
456
457         if (((type == CONF_STR || type == CONF_MSTR || type == CONF_BLN || type == CONF_LINT) && conftype != CONF_STR) ||
458             (type == CONF_CBK && conftype != CONF_CBK)) {
459             if (block)
460                 debug(DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
461             debug(DBG_ERR, "configuration error, wrong syntax for option %s", opt);
462             goto errexit;
463         }
464
465         switch (type) {
466         case CONF_STR:
467             if (*str) {
468                 debug(DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
469                 goto errexit;
470             }
471             unhex(val);
472             *str = val;
473             break;
474         case CONF_MSTR:
475             if (*mstr)
476                 for (n = 0; (*mstr)[n]; n++);
477             else
478                 n = 0;
479             newmstr = realloc(*mstr, sizeof(char *) * (n + 2));
480             if (!newmstr) {
481                 debug(DBG_ERR, "malloc failed");
482                 goto errexit;
483             }
484             unhex(val);
485             newmstr[n] = val;
486             newmstr[n + 1] = NULL;
487             *mstr = newmstr;
488             break;
489         case CONF_BLN:
490             if (!strcasecmp(val, "on"))
491                 *bln = 1;
492             else if (!strcasecmp(val, "off"))
493                 *bln = 0;
494             else {
495                 if (block)
496                     debug(DBG_ERR, "configuration error in block %s, value for option %s must be on or off, not %s", block, opt, val);
497                 else
498                     debug(DBG_ERR, "configuration error, value for option %s must be on or off, not %s", opt, val);
499                 goto errexit;
500             }
501             break;
502         case CONF_LINT:
503             endptr = NULL;
504             *lint = strtol(val, &endptr, 0);
505             if (*lint == LONG_MIN || *lint == LONG_MAX || !endptr || endptr == val || *endptr != '\0') {
506                 if (block)
507                     debug(DBG_ERR, "configuration error in block %s, value for option %s must be an integer, not %s", block, opt, val);
508                 else
509                     debug(DBG_ERR, "configuration error, value for option %s must be an integer, not %s", opt, val);
510                 goto errexit;
511             }
512             break;
513         case CONF_CBK:
514             optval = malloc(strlen(opt) + strlen(val) + 2);
515             if (!optval) {
516                 debug(DBG_ERR, "malloc failed");
517                 goto errexit;
518             }
519             sprintf(optval, "%s %s", opt, val);
520             if (!cbk(cf, cbkarg, optval, opt, val)) {
521                 free(optval);
522                 goto errexit;
523             }
524             free(val);
525             free(optval);
526             continue;
527         default:
528             goto errparam;
529         }
530         if (block)
531             debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
532         else
533             debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);
534         if (type == CONF_BLN || type == CONF_LINT)
535             free(val);
536     }
537
538 errparam:
539     debug(DBG_ERR, "getgenericconfig: internal parameter error");
540 errexit:
541     free(opt);
542     free(val);
543     return 0;
544 }
545
546 /* Local Variables: */
547 /* c-file-style: "stroustrup" */
548 /* End: */