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