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