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