support for checking certificate policy oids
[radsecproxy.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_INFO, "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 /* returns 1 if ok, 0 on error */
387 /* caller must free returned values also on error */
388 int getgenericconfig(struct gconffile **cf, char *block, ...) {
389     va_list ap;
390     char *opt = NULL, *val, *word, *optval, **str = NULL, ***mstr = NULL, **newmstr, *endptr;
391     uint8_t *bln = NULL;
392     long int *lint = NULL;
393     int type = 0, conftype = 0, n;
394     int (*cbk)(struct gconffile **, void *, char *, char *, char *) = NULL;
395     void *cbkarg = NULL;
396
397     for (;;) {
398         free(opt);
399         if (!getconfigline(cf, block, &opt, &val, &conftype))
400             return 0;
401         if (!opt)
402             return 1;
403
404         if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
405             if (!pushgconfpaths(cf, val)) {
406                 debug(DBG_ERR, "failed to include config file %s", val);
407                 goto errexit;
408             }
409             free(val);
410             continue;
411         }
412             
413         va_start(ap, block);
414         while ((word = va_arg(ap, char *))) {
415             type = va_arg(ap, int);
416             switch (type) {
417             case CONF_STR:
418                 str = va_arg(ap, char **);
419                 if (!str)
420                     goto errparam;
421                 break;
422             case CONF_MSTR:
423                 mstr = va_arg(ap, char ***);
424                 if (!mstr)
425                     goto errparam;
426                 break;
427             case CONF_BLN:
428                 bln = va_arg(ap, uint8_t *);
429                 if (!bln)
430                     goto errparam;
431                 break;
432             case CONF_LINT:
433                 lint = va_arg(ap, long int *);
434                 if (!lint)
435                     goto errparam;
436                 break;
437             case CONF_CBK:
438                 cbk = va_arg(ap, int (*)(struct gconffile **, void *, char *, char *, char *));
439                 if (!cbk)
440                     goto errparam;
441                 cbkarg = va_arg(ap, void *);
442                 break;
443             default:
444                 goto errparam;
445             }
446             if (!strcasecmp(opt, word))
447                 break;
448         }
449         va_end(ap);
450         
451         if (!word) {
452             if (block)
453                 debug(DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
454             debug(DBG_ERR, "configuration error, unknown option %s", opt);
455             goto errexit;
456         }
457
458         if (((type == CONF_STR || type == CONF_MSTR || type == CONF_BLN || type == CONF_LINT) && conftype != CONF_STR) ||
459             (type == CONF_CBK && conftype != CONF_CBK)) {
460             if (block)
461                 debug(DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
462             debug(DBG_ERR, "configuration error, wrong syntax for option %s", opt);
463             goto errexit;
464         }
465
466         switch (type) {
467         case CONF_STR:
468             if (*str) {
469                 debug(DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
470                 goto errexit;
471             }
472             unhex(val);
473             *str = val;
474             break;
475         case CONF_MSTR:
476             if (*mstr)
477                 for (n = 0; (*mstr)[n]; n++);
478             else
479                 n = 0;
480             newmstr = realloc(*mstr, sizeof(char *) * (n + 2));
481             if (!newmstr) {
482                 debug(DBG_ERR, "malloc failed");
483                 goto errexit;
484             }
485             unhex(val);
486             newmstr[n] = val;
487             newmstr[n + 1] = NULL;
488             *mstr = newmstr;
489             break;
490         case CONF_BLN:
491             if (!strcasecmp(val, "on"))
492                 *bln = 1;
493             else if (!strcasecmp(val, "off"))
494                 *bln = 0;
495             else {
496                 if (block)
497                     debug(DBG_ERR, "configuration error in block %s, value for option %s must be on or off, not %s", block, opt, val);
498                 else
499                     debug(DBG_ERR, "configuration error, value for option %s must be on or off, not %s", opt, val);
500                 goto errexit;
501             }
502             break;
503         case CONF_LINT:
504             endptr = NULL;
505             *lint = strtol(val, &endptr, 0);
506             if (*lint == LONG_MIN || *lint == LONG_MAX || !endptr || endptr == val || *endptr != '\0') {
507                 if (block)
508                     debug(DBG_ERR, "configuration error in block %s, value for option %s must be an integer, not %s", block, opt, val);
509                 else
510                     debug(DBG_ERR, "configuration error, value for option %s must be an integer, not %s", opt, val);
511                 goto errexit;
512             }
513             break;
514         case CONF_CBK:
515             optval = malloc(strlen(opt) + strlen(val) + 2);
516             if (!optval) {
517                 debug(DBG_ERR, "malloc failed");
518                 goto errexit;
519             }
520             sprintf(optval, "%s %s", opt, val);
521             if (!cbk(cf, cbkarg, optval, opt, val)) {
522                 free(optval);
523                 goto errexit;
524             }
525             free(val);
526             free(optval);
527             continue;
528         default:
529             goto errparam;
530         }
531         if (block)
532             debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
533         else 
534             debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);
535         if (type == CONF_BLN || type == CONF_LINT)
536             free(val);
537     }
538
539  errparam:
540     debug(DBG_ERR, "getgenericconfig: internal parameter error");
541  errexit:
542     free(opt);
543     free(val);
544     return 0;
545 }