Comply better with the CGI specification by unquoting PATH_INFO.
[ashd.git] / src / dirplex / conf.c
CommitLineData
992ce9ef
FT
1/*
2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <stdlib.h>
20#include <stdio.h>
992ce9ef 21#include <unistd.h>
600a1ce7 22#include <string.h>
992ce9ef 23#include <errno.h>
992ce9ef 24#include <fnmatch.h>
600a1ce7 25#include <sys/stat.h>
992ce9ef
FT
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30#include <utils.h>
992ce9ef 31#include <log.h>
06c1a718 32#include <cf.h>
992ce9ef 33
600a1ce7 34#include "dirplex.h"
992ce9ef 35
0fc6fd13 36static struct config *cflist;
600a1ce7 37struct config *gconfig, *lconfig;
992ce9ef 38
1eba4f97
FT
39static void freerule(struct rule *rule)
40{
41 freeca(rule->patterns);
42 free(rule);
43}
44
992ce9ef
FT
45static void freepattern(struct pattern *pat)
46{
47 struct rule **rule;
48
1eba4f97
FT
49 for(rule = pat->rules; *rule; rule++)
50 freerule(*rule);
992ce9ef
FT
51 if(pat->childnm != NULL)
52 free(pat->childnm);
624f637d 53 freeca(pat->fchild);
992ce9ef
FT
54 free(pat);
55}
56
57static void freeconfig(struct config *cf)
58{
59 struct child *ch, *nch;
60 struct pattern *pat, *npat;
61
62 if(cf->prev != NULL)
63 cf->prev->next = cf->next;
64 if(cf->next != NULL)
65 cf->next->prev = cf->prev;
66 if(cf == cflist)
67 cflist = cf->next;
0fc6fd13
FT
68 if(cf->path != NULL)
69 free(cf->path);
992ce9ef
FT
70 for(ch = cf->children; ch != NULL; ch = nch) {
71 nch = ch->next;
72 freechild(ch);
73 }
74 for(pat = cf->patterns; pat != NULL; pat = npat) {
75 npat = pat->next;
76 freepattern(pat);
77 }
6373daf9 78 freeca(cf->index);
300d73d9
FT
79 if(cf->capture != NULL)
80 free(cf->capture);
992ce9ef
FT
81 free(cf);
82}
83
600a1ce7 84struct child *getchild(struct config *cf, char *name)
992ce9ef
FT
85{
86 struct child *ch;
87
88 for(ch = cf->children; ch; ch = ch->next) {
89 if(!strcmp(ch->name, name))
90 break;
91 }
92 return(ch);
93}
94
95static struct rule *newrule(struct pattern *pat)
96{
97 int i;
98 struct rule *rule;
99
100 for(i = 0; pat->rules[i]; i++);
101 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
cceb3611 102 rule = pat->rules[i] = szmalloc(sizeof(*rule));
992ce9ef
FT
103 pat->rules[i + 1] = NULL;
104 return(rule);
105}
106
107static struct pattern *newpattern(void)
108{
109 struct pattern *pat;
110
111 omalloc(pat);
112 pat->rules = szmalloc(sizeof(*pat->rules));
113 return(pat);
114}
115
1eba4f97
FT
116static char **cadup(char **w)
117{
118 char **ret;
119 int i, l;
120
121 l = calen(w);
122 ret = smalloc(sizeof(*ret) * (l + 1));
123 for(i = 0; i < l; i++)
124 ret[i] = sstrdup(w[i]);
125 ret[i] = NULL;
126 return(ret);
127}
128
06c1a718
FT
129static struct pattern *parsepattern(struct cfstate *s)
130{
131 struct pattern *pat;
132 struct rule *rule;
1eba4f97 133 int sl;
06c1a718
FT
134
135 if(!strcmp(s->argv[0], "match")) {
136 s->expstart = 1;
137 pat = newpattern();
138 } else {
139 return(NULL);
140 }
141
8232cebe
FT
142 if((s->argc > 1) && !strcmp(s->argv[1], "directory"))
143 pat->type = PT_DIR;
06c1a718
FT
144 sl = s->lno;
145 while(1) {
146 getcfline(s);
147 if(!strcmp(s->argv[0], "filename")) {
148 if(s->argc < 2) {
149 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno);
150 continue;
151 }
152 rule = newrule(pat);
153 rule->type = PAT_BASENAME;
1eba4f97 154 rule->patterns = cadup(s->argv + 1);
06c1a718
FT
155 } else if(!strcmp(s->argv[0], "pathname")) {
156 if(s->argc < 2) {
157 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno);
158 continue;
159 }
160 rule = newrule(pat);
161 rule->type = PAT_PATHNAME;
1eba4f97 162 rule->patterns = cadup(s->argv + 1);
06c1a718
FT
163 } else if(!strcmp(s->argv[0], "all")) {
164 newrule(pat)->type = PAT_ALL;
165 } else if(!strcmp(s->argv[0], "default")) {
166 newrule(pat)->type = PAT_DEFAULT;
7711283c
FT
167 } else if(!strcmp(s->argv[0], "local")) {
168 newrule(pat)->type = PAT_LOCAL;
06c1a718
FT
169 } else if(!strcmp(s->argv[0], "handler")) {
170 if(s->argc < 2) {
171 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
172 continue;
173 }
174 if(pat->childnm != NULL)
175 free(pat->childnm);
176 pat->childnm = sstrdup(s->argv[1]);
624f637d 177 } else if(!strcmp(s->argv[0], "fork")) {
1eba4f97 178 pat->fchild = cadup(s->argv + 1);
06c1a718
FT
179 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
180 break;
181 } else {
182 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
183 }
184 }
185
186 if(pat->rules[0] == NULL) {
187 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
188 freepattern(pat);
189 return(NULL);
190 }
624f637d 191 if((pat->childnm == NULL) && (pat->fchild == NULL)) {
06c1a718
FT
192 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
193 freepattern(pat);
194 return(NULL);
195 }
196 return(pat);
197}
198
17e55136
FT
199static struct config *emptyconfig(void)
200{
201 struct config *cf;
202
203 omalloc(cf);
204 return(cf);
205}
206
600a1ce7 207struct config *readconfig(char *file)
992ce9ef 208{
06c1a718
FT
209 struct cfstate *s;
210 FILE *in;
992ce9ef 211 struct config *cf;
992ce9ef
FT
212 struct child *child;
213 struct pattern *pat;
992ce9ef 214
17e55136
FT
215 if((in = fopen(file, "r")) == NULL) {
216 flog(LOG_WARNING, "%s: %s", file, strerror(errno));
992ce9ef 217 return(NULL);
06c1a718 218 }
17e55136
FT
219 s = mkcfparser(in, file);
220 cf = emptyconfig();
06c1a718
FT
221
222 while(1) {
223 getcfline(s);
224 if((child = parsechild(s)) != NULL) {
225 child->next = cf->children;
226 cf->children = child;
227 } else if((pat = parsepattern(s)) != NULL) {
228 pat->next = cf->patterns;
229 cf->patterns = pat;
6373daf9
FT
230 } else if(!strcmp(s->argv[0], "index-file")) {
231 freeca(cf->index);
232 cf->index = NULL;
233 if(s->argc > 1)
234 cf->index = cadup(s->argv + 1);
300d73d9
FT
235 } else if(!strcmp(s->argv[0], "capture")) {
236 if(s->argc < 2) {
237 flog(LOG_WARNING, "%s:%i: missing argument to capture declaration", s->file, s->lno);
238 continue;
239 }
240 if(cf->capture != NULL)
241 free(cf->capture);
242 cf->capture = sstrdup(s->argv[1]);
06c1a718
FT
243 } else if(!strcmp(s->argv[0], "eof")) {
244 break;
245 } else {
246 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
992ce9ef 247 }
06c1a718 248 }
992ce9ef 249
06c1a718
FT
250 freecfparser(s);
251 fclose(in);
992ce9ef
FT
252 return(cf);
253}
254
a756e349
FT
255static void mergechildren(struct config *dst, struct config *src)
256{
257 struct child *ch1, *ch2;
258
259 for(ch1 = dst->children; ch1 != NULL; ch1 = ch1->next) {
260 for(ch2 = src->children; ch2 != NULL; ch2 = ch2->next) {
261 if(!strcmp(ch1->name, ch2->name)) {
262 ch1->fd = ch2->fd;
263 ch2->fd = -1;
264 break;
265 }
266 }
267 }
268}
269
600a1ce7 270struct config *getconfig(char *path)
992ce9ef 271{
a756e349 272 struct config *cf, *ocf;
992ce9ef 273 struct stat sb;
17e55136
FT
274 char *fn;
275 time_t mtime;
992ce9ef 276
17e55136 277 fn = sprintf3("%s/.htrc", path);
992ce9ef
FT
278 for(cf = cflist; cf != NULL; cf = cf->next) {
279 if(!strcmp(cf->path, path)) {
17e55136 280 if(now - cf->lastck > 5) {
a756e349 281 if(stat(fn, &sb) || (sb.st_mtime != cf->mtime))
17e55136 282 break;
992ce9ef 283 }
17e55136 284 cf->lastck = now;
992ce9ef
FT
285 return(cf);
286 }
287 }
a756e349 288 ocf = cf;
17e55136
FT
289 if(access(fn, R_OK) || stat(fn, &sb)) {
290 cf = emptyconfig();
291 mtime = 0;
292 } else {
293 if((cf = readconfig(fn)) == NULL)
294 return(NULL);
295 mtime = sb.st_mtime;
992ce9ef 296 }
a756e349
FT
297 if(ocf != NULL) {
298 mergechildren(cf, ocf);
299 freeconfig(ocf);
300 }
17e55136
FT
301 cf->path = sstrdup(path);
302 cf->mtime = mtime;
303 cf->lastck = now;
304 cf->next = cflist;
d245c327
FT
305 cf->prev = NULL;
306 if(cflist != NULL)
307 cflist->prev = cf;
17e55136 308 cflist = cf;
992ce9ef
FT
309 return(cf);
310}
311
600a1ce7 312struct config **getconfigs(char *file)
992ce9ef 313{
8dce6d19
FT
314 static struct config **ret = NULL;
315 struct {
316 struct config **b;
317 size_t s, d;
318 } buf;
992ce9ef 319 struct config *cf;
8dce6d19
FT
320 char *tmp, *p;
321
322 if(ret != NULL)
323 free(ret);
324 bufinit(buf);
325 tmp = sstrdup(file);
992ce9ef 326 while(1) {
8dce6d19 327 if((p = strrchr(tmp, '/')) == NULL)
992ce9ef 328 break;
8dce6d19
FT
329 *p = 0;
330 if((cf = getconfig(tmp)) != NULL)
331 bufadd(buf, cf);
332 }
333 free(tmp);
334 if((cf = getconfig(".")) != NULL)
335 bufadd(buf, cf);
0fc6fd13
FT
336 if(lconfig != NULL)
337 bufadd(buf, lconfig);
338 if(gconfig != NULL)
339 bufadd(buf, gconfig);
8dce6d19
FT
340 bufadd(buf, NULL);
341 return(ret = buf.b);
342}
343
da75c835 344struct child *findchild(char *file, char *name, struct config **cf)
8dce6d19
FT
345{
346 int i;
347 struct config **cfs;
348 struct child *ch;
349
350 cfs = getconfigs(file);
351 for(i = 0; cfs[i] != NULL; i++) {
da75c835
FT
352 if((ch = getchild(cfs[i], name)) != NULL) {
353 if(cf != NULL)
354 *cf = cfs[i];
355 return(ch);
356 }
992ce9ef 357 }
da75c835 358 return(NULL);
992ce9ef
FT
359}
360
600a1ce7 361struct pattern *findmatch(char *file, int trydefault, int dir)
992ce9ef 362{
1eba4f97 363 int i, o, c;
8dce6d19
FT
364 char *bn;
365 struct config **cfs;
992ce9ef
FT
366 struct pattern *pat;
367 struct rule *rule;
7711283c 368 size_t pl;
992ce9ef
FT
369
370 if((bn = strrchr(file, '/')) != NULL)
371 bn++;
372 else
373 bn = file;
8dce6d19
FT
374 cfs = getconfigs(file);
375 for(c = 0; cfs[c] != NULL; c++) {
376 for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
8232cebe
FT
377 if(!dir && (pat->type == PT_DIR))
378 continue;
379 if(dir && (pat->type != PT_DIR))
380 continue;
992ce9ef
FT
381 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
382 if(rule->type == PAT_BASENAME) {
1eba4f97
FT
383 for(o = 0; rule->patterns[o] != NULL; o++) {
384 if(!fnmatch(rule->patterns[o], bn, 0))
385 break;
386 }
387 if(rule->patterns[o] == NULL)
992ce9ef
FT
388 break;
389 } else if(rule->type == PAT_PATHNAME) {
1eba4f97
FT
390 for(o = 0; rule->patterns[o] != NULL; o++) {
391 if(!fnmatch(rule->patterns[o], file, FNM_PATHNAME))
392 break;
393 }
394 if(rule->patterns[o] == NULL)
992ce9ef
FT
395 break;
396 } else if(rule->type == PAT_ALL) {
397 } else if(rule->type == PAT_DEFAULT) {
398 if(!trydefault)
399 break;
7711283c
FT
400 } else if(rule->type == PAT_LOCAL) {
401 if(cfs[c]->path == NULL)
402 break;
403 pl = strlen(cfs[c]->path);
404 if(!((strlen(file) > pl) && !strncmp(file, cfs[c]->path, pl) && (file[pl] == '/') && !strchr(file + pl + 1, '/')))
405 break;
992ce9ef
FT
406 }
407 }
408 if(!rule)
8dce6d19 409 return(pat);
992ce9ef
FT
410 }
411 }
8232cebe
FT
412 if(!trydefault)
413 return(findmatch(file, 1, dir));
8dce6d19 414 return(NULL);
992ce9ef 415}