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