7a367e7810fb934046224325e4bc863cdf4e9378
[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 char *mystringcopyx(const char *s) {
22     char *t;
23     t = stringcopy(s, 0);
24     if (!t)
25         debugx(1, DBG_ERR, "malloc failed");
26     return t;
27 }
28
29 /* returns NULL on error, where to continue parsing if token and ok. E.g. "" will return token with empty string */
30 char *strtokenquote(char *s, char **token, char *del, char *quote, char *comment) {
31     char *t = s, *q, *r;
32
33     if (!t || !token || !del)
34         return NULL;
35     while (*t && strchr(del, *t))
36         t++;
37     if (!*t || (comment && strchr(comment, *t))) {
38         *token = NULL;
39         return t + 1; /* needs to be non-NULL, but value doesn't matter */
40     }
41     if (quote && (q = strchr(quote, *t))) {
42         t++;
43         r = t;
44         while (*t && *t != *q)
45             t++;
46         if (!*t || (t[1] && !strchr(del, t[1])))
47             return NULL;
48         *t = '\0';
49         *token = r;
50         return t + 1;
51     }
52     *token = t;
53     t++;
54     while (*t && !strchr(del, *t))
55         t++;
56     *t = '\0';
57     return t + 1;
58 }
59
60 FILE *pushgconffile(struct gconffile **cf, const char *path) {
61     int i;
62     struct gconffile *newcf;
63     FILE *f;
64
65     f = fopen(path, "r");
66     if (!f) {
67         debug(DBG_INFO, "could not read config file %s", path);
68         return NULL;
69     }
70     debug(DBG_DBG, "opened config file %s", path);
71     if (!*cf) {
72         newcf = malloc(sizeof(struct gconffile) * 2);
73         if (!newcf)
74             debugx(1, DBG_ERR, "malloc failed");
75         newcf[1].file = NULL;
76         newcf[1].path = NULL;
77     } else {
78         for (i = 0; (*cf)[i].path; i++);
79         newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
80         if (!newcf)
81             debugx(1, DBG_ERR, "malloc failed");
82         memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
83     }
84     newcf[0].file = f;
85     newcf[0].path = mystringcopyx(path);
86     *cf = newcf;
87     return f;
88 }
89
90 FILE *pushgconffiles(struct gconffile **cf, const char *cfgpath) {
91     int i;
92     FILE *f;
93     glob_t globbuf;
94     char *path, *curfile = NULL, *dir;
95     
96     /* if cfgpath is relative, make it relative to current config */
97     if (*cfgpath == '/')
98         path = (char *)cfgpath;
99     else {
100         /* dirname may modify its argument */
101         curfile = mystringcopyx((*cf)->path);
102         dir = dirname(curfile);
103         path = malloc(strlen(dir) + strlen(cfgpath) + 2);
104         if (!path)
105             debugx(1, DBG_ERR, "malloc failed");
106         strcpy(path, dir);
107         path[strlen(dir)] = '/';
108         strcpy(path + strlen(dir) + 1, cfgpath);
109     }
110     memset(&globbuf, 0, sizeof(glob_t));
111     if (glob(path, 0, NULL, &globbuf)) {
112         debug(DBG_INFO, "could not glob %s", path);
113         f = NULL;
114     } else {
115         for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
116             f = pushgconffile(cf, globbuf.gl_pathv[i]);
117             if (!f)
118                 break;
119         }    
120         globfree(&globbuf);
121     }
122     if (curfile) {
123         free(curfile);
124         free(path);
125     }
126     return f;
127 }
128
129 FILE *popgconffile(struct gconffile **cf) {
130     int i;
131
132     if (!*cf)
133         return NULL;
134     for (i = 0; (*cf)[i].path; i++);
135     if (i && (*cf)[0].file) {
136         fclose((*cf)[0].file);
137         debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
138         free((*cf)[0].path);
139     }
140     if (i < 2) {
141         free(*cf);
142         *cf = NULL;
143         return NULL;
144     }
145     memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
146     return (*cf)[0].file;
147 }
148
149 struct gconffile *openconfigfile(const char *file) {
150     struct gconffile *cf = NULL;
151
152     if (!pushgconffile(&cf, file))
153         debugx(1, DBG_ERR, "could not read config file %s\n%s", file, strerror(errno));
154     debug(DBG_DBG, "reading config file %s", file);
155     return cf;
156 }
157
158 /* Parses config with following syntax:
159  * One of these:
160  * option-name value
161  * option-name = value
162  * Or:
163  * option-name value {
164  *     option-name [=] value
165  *     ...
166  * }
167  */
168
169 void getconfigline(struct gconffile **cf, char *block, char **opt, char **val, int *conftype) {
170     char line[1024];
171     char *tokens[3], *s;
172     int tcount;
173     
174     *opt = NULL;
175     *val = NULL;
176     *conftype = 0;
177     
178     if (!cf || !*cf || !(*cf)->file)
179         return;
180
181     for (;;) {
182         if (!fgets(line, 1024, (*cf)->file)) {
183             if (popgconffile(cf))
184                 continue;
185             return;
186         }
187         s = line;
188         for (tcount = 0; tcount < 3; tcount++) {
189             s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
190             if (!s)
191                 debugx(1, DBG_ERR, "Syntax error in line starting with: %s", line);
192             if (!tokens[tcount])
193                 break;
194         }
195         if (!tcount || **tokens == '#')
196             continue;
197
198         if (**tokens == '}') {
199             if (block)
200                 return;
201             debugx(1, DBG_ERR, "configuration error, found } with no matching {");
202         }
203         break;
204     }
205     
206     switch (tcount) {
207     case 2:
208         *opt = mystringcopyx(tokens[0]);
209         *val = mystringcopyx(tokens[1]);
210         *conftype = CONF_STR;
211         break;
212     case 3:
213         if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
214             *opt = mystringcopyx(tokens[0]);
215             *val = mystringcopyx(tokens[2]);
216             *conftype = CONF_STR;
217             break;
218         }
219         if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
220             *opt = mystringcopyx(tokens[0]);
221             *val = mystringcopyx(tokens[1]);
222             *conftype = CONF_CBK;
223             break;
224         }
225         /* fall through */
226     default:
227         if (block)
228             debugx(1, DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
229         debugx(1, DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
230     }
231
232     if (!**val)
233         debugx(1, DBG_ERR, "configuration error, option %s needs a non-empty value", *opt);
234     return;
235 }
236
237 void getgenericconfig(struct gconffile **cf, char *block, ...) {
238     va_list ap;
239     char *opt = NULL, *val, *word, *optval, **str = NULL, ***mstr = NULL;
240     uint8_t *bln;
241     int type = 0, conftype = 0, n;
242     void (*cbk)(struct gconffile **, char *, char *, char *) = NULL;
243
244     for (;;) {
245         free(opt);
246         getconfigline(cf, block, &opt, &val, &conftype);
247         if (!opt)
248             return;
249
250         if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
251             if (!pushgconffiles(cf, val))
252                 debugx(1, DBG_ERR, "failed to include config file %s", val);
253             free(val);
254             continue;
255         }
256             
257         va_start(ap, block);
258         while ((word = va_arg(ap, char *))) {
259             type = va_arg(ap, int);
260             switch (type) {
261             case CONF_STR:
262                 str = va_arg(ap, char **);
263                 if (!str)
264                     debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
265                 break;
266             case CONF_MSTR:
267                 mstr = va_arg(ap, char ***);
268                 if (!mstr)
269                     debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
270                 break;
271             case CONF_BLN:
272                 bln = va_arg(ap, uint8_t *);
273                 if (!bln)
274                     debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
275                 break;
276             case CONF_CBK:
277                 cbk = va_arg(ap, void (*)(struct gconffile **, char *, char *, char *));
278                 break;
279             default:
280                 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
281             }
282             if (!strcasecmp(opt, word))
283                 break;
284         }
285         va_end(ap);
286         
287         if (!word) {
288             if (block)
289                 debugx(1, DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
290             debugx(1, DBG_ERR, "configuration error, unknown option %s", opt);
291         }
292
293         if (((type == CONF_STR || type == CONF_MSTR || type == CONF_BLN) && conftype != CONF_STR) ||
294             (type == CONF_CBK && conftype != CONF_CBK)) {
295             if (block)
296                 debugx(1, DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
297             debugx(1, DBG_ERR, "configuration error, wrong syntax for option %s", opt);
298         }
299
300         switch (type) {
301         case CONF_STR:
302             if (*str)
303                 debugx(1, DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
304             *str = val;
305             break;
306         case CONF_MSTR:
307             if (*mstr)
308                 for (n = 0; (*mstr)[n]; n++);
309             else
310                 n = 0;
311             *mstr = realloc(*mstr, sizeof(char *) * (n + 2));
312             if (!*mstr)
313                 debugx(1, DBG_ERR, "malloc failed");
314             (*mstr)[n] = val;
315             (*mstr)[n + 1] = NULL;
316             break;
317         case CONF_BLN:
318             if (!strcasecmp(val, "on"))
319                 *bln = 1;
320             else if (!strcasecmp(val, "off"))
321                 *bln = 0;
322             else if (block)
323                 debugx(1, DBG_ERR, "configuration error in block %s, value for option %s must be on or off, not %s", block, opt, val);
324             else
325                 debugx(1, DBG_ERR, "configuration error, value for option %s must be on or off, not %s", opt, val);
326             break;
327         case CONF_CBK:
328             optval = malloc(strlen(opt) + strlen(val) + 2);
329             if (!optval)
330                 debugx(1, DBG_ERR, "malloc failed");
331             sprintf(optval, "%s %s", opt, val);
332             cbk(cf, optval, opt, val);
333             free(val);
334             free(optval);
335             continue;
336         default:
337             debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
338         }
339         if (block)
340             debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
341         else 
342             debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);
343         if (type == CONF_BLN)
344             free(val);
345     }
346 }