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