removed some harmless compiler warnings
[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 = NULL;
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     else {
114         for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
115             f = pushgconffile(cf, globbuf.gl_pathv[i]);
116             if (!f)
117                 break;
118         }    
119         globfree(&globbuf);
120     }
121     if (curfile) {
122         free(curfile);
123         free(path);
124     }
125     return f;
126 }
127
128 FILE *popgconffile(struct gconffile **cf) {
129     int i;
130
131     if (!*cf)
132         return NULL;
133     for (i = 0; (*cf)[i].path; i++);
134     if (i && (*cf)[0].file) {
135         fclose((*cf)[0].file);
136         debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
137         free((*cf)[0].path);
138     }
139     if (i < 2) {
140         free(*cf);
141         *cf = NULL;
142         return NULL;
143     }
144     memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
145     return (*cf)[0].file;
146 }
147
148 struct gconffile *openconfigfile(const char *file) {
149     struct gconffile *cf = NULL;
150
151     if (!pushgconffile(&cf, file))
152         debugx(1, DBG_ERR, "could not read config file %s\n%s", file, strerror(errno));
153     debug(DBG_DBG, "reading config file %s", file);
154     return cf;
155 }
156
157 /* Parses config with following syntax:
158  * One of these:
159  * option-name value
160  * option-name = value
161  * Or:
162  * option-name value {
163  *     option-name [=] value
164  *     ...
165  * }
166  */
167
168 void getconfigline(struct gconffile **cf, char *block, char **opt, char **val, int *conftype) {
169     char line[1024];
170     char *tokens[3], *s;
171     int tcount;
172     
173     *opt = NULL;
174     *val = NULL;
175     *conftype = 0;
176     
177     if (!cf || !*cf || !(*cf)->file)
178         return;
179
180     for (;;) {
181         if (!fgets(line, 1024, (*cf)->file)) {
182             if (popgconffile(cf))
183                 continue;
184             return;
185         }
186         s = line;
187         for (tcount = 0; tcount < 3; tcount++) {
188             s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
189             if (!s)
190                 debugx(1, DBG_ERR, "Syntax error in line starting with: %s", line);
191             if (!tokens[tcount])
192                 break;
193         }
194         if (!tcount || **tokens == '#')
195             continue;
196
197         if (**tokens == '}') {
198             if (block)
199                 return;
200             debugx(1, DBG_ERR, "configuration error, found } with no matching {");
201         }
202         break;
203     }
204     
205     switch (tcount) {
206     case 2:
207         *opt = mystringcopyx(tokens[0]);
208         *val = mystringcopyx(tokens[1]);
209         *conftype = CONF_STR;
210         break;
211     case 3:
212         if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
213             *opt = mystringcopyx(tokens[0]);
214             *val = mystringcopyx(tokens[2]);
215             *conftype = CONF_STR;
216             break;
217         }
218         if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
219             *opt = mystringcopyx(tokens[0]);
220             *val = mystringcopyx(tokens[1]);
221             *conftype = CONF_CBK;
222             break;
223         }
224         /* fall through */
225     default:
226         if (block)
227             debugx(1, DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
228         debugx(1, DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
229     }
230
231     if (!**val)
232         debugx(1, DBG_ERR, "configuration error, option %s needs a non-empty value", *opt);
233     return;
234 }
235
236 void getgenericconfig(struct gconffile **cf, char *block, ...) {
237     va_list ap;
238     char *opt = NULL, *val, *word, *optval, **str = NULL, ***mstr = NULL;
239     uint8_t *bln = NULL;
240     int type = 0, conftype = 0, n;
241     void (*cbk)(struct gconffile **, char *, char *, char *) = NULL;
242
243     for (;;) {
244         free(opt);
245         getconfigline(cf, block, &opt, &val, &conftype);
246         if (!opt)
247             return;
248
249         if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
250             if (!pushgconffiles(cf, val))
251                 debugx(1, DBG_ERR, "failed to include config file %s", val);
252             free(val);
253             continue;
254         }
255             
256         va_start(ap, block);
257         while ((word = va_arg(ap, char *))) {
258             type = va_arg(ap, int);
259             switch (type) {
260             case CONF_STR:
261                 str = va_arg(ap, char **);
262                 if (!str)
263                     debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
264                 break;
265             case CONF_MSTR:
266                 mstr = va_arg(ap, char ***);
267                 if (!mstr)
268                     debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
269                 break;
270             case CONF_BLN:
271                 bln = va_arg(ap, uint8_t *);
272                 if (!bln)
273                     debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
274                 break;
275             case CONF_CBK:
276                 cbk = va_arg(ap, void (*)(struct gconffile **, char *, char *, char *));
277                 break;
278             default:
279                 debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
280             }
281             if (!strcasecmp(opt, word))
282                 break;
283         }
284         va_end(ap);
285         
286         if (!word) {
287             if (block)
288                 debugx(1, DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
289             debugx(1, DBG_ERR, "configuration error, unknown option %s", opt);
290         }
291
292         if (((type == CONF_STR || type == CONF_MSTR || type == CONF_BLN) && conftype != CONF_STR) ||
293             (type == CONF_CBK && conftype != CONF_CBK)) {
294             if (block)
295                 debugx(1, DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
296             debugx(1, DBG_ERR, "configuration error, wrong syntax for option %s", opt);
297         }
298
299         switch (type) {
300         case CONF_STR:
301             if (*str)
302                 debugx(1, DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
303             *str = val;
304             break;
305         case CONF_MSTR:
306             if (*mstr)
307                 for (n = 0; (*mstr)[n]; n++);
308             else
309                 n = 0;
310             *mstr = realloc(*mstr, sizeof(char *) * (n + 2));
311             if (!*mstr)
312                 debugx(1, DBG_ERR, "malloc failed");
313             (*mstr)[n] = val;
314             (*mstr)[n + 1] = NULL;
315             break;
316         case CONF_BLN:
317             if (!strcasecmp(val, "on"))
318                 *bln = 1;
319             else if (!strcasecmp(val, "off"))
320                 *bln = 0;
321             else if (block)
322                 debugx(1, DBG_ERR, "configuration error in block %s, value for option %s must be on or off, not %s", block, opt, val);
323             else
324                 debugx(1, DBG_ERR, "configuration error, value for option %s must be on or off, not %s", opt, val);
325             break;
326         case CONF_CBK:
327             optval = malloc(strlen(opt) + strlen(val) + 2);
328             if (!optval)
329                 debugx(1, DBG_ERR, "malloc failed");
330             sprintf(optval, "%s %s", opt, val);
331             cbk(cf, optval, opt, val);
332             free(val);
333             free(optval);
334             continue;
335         default:
336             debugx(1, DBG_ERR, "getgenericconfig: internal parameter error");
337         }
338         if (block)
339             debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
340         else 
341             debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);
342         if (type == CONF_BLN)
343             free(val);
344     }
345 }