python: Added some code for easier testing of WSGI handlers.
[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>
b70b2d4f 33#include <resp.h>
992ce9ef 34
600a1ce7 35#include "dirplex.h"
992ce9ef 36
0fc6fd13 37static struct config *cflist;
600a1ce7 38struct config *gconfig, *lconfig;
992ce9ef 39
1eba4f97
FT
40static void freerule(struct rule *rule)
41{
42 freeca(rule->patterns);
43 free(rule);
44}
45
992ce9ef
FT
46static void freepattern(struct pattern *pat)
47{
48 struct rule **rule;
acc2d159 49 struct headmod *head;
992ce9ef 50
1eba4f97
FT
51 for(rule = pat->rules; *rule; rule++)
52 freerule(*rule);
acc2d159
FT
53 while((head = pat->headers) != NULL) {
54 pat->headers = head->next;
55 free(head->name);
56 free(head->value);
57 free(head);
58 }
992ce9ef
FT
59 if(pat->childnm != NULL)
60 free(pat->childnm);
624f637d 61 freeca(pat->fchild);
992ce9ef
FT
62 free(pat);
63}
64
65static void freeconfig(struct config *cf)
66{
67 struct child *ch, *nch;
68 struct pattern *pat, *npat;
69
70 if(cf->prev != NULL)
71 cf->prev->next = cf->next;
72 if(cf->next != NULL)
73 cf->next->prev = cf->prev;
74 if(cf == cflist)
75 cflist = cf->next;
0fc6fd13
FT
76 if(cf->path != NULL)
77 free(cf->path);
992ce9ef
FT
78 for(ch = cf->children; ch != NULL; ch = nch) {
79 nch = ch->next;
80 freechild(ch);
81 }
82 for(pat = cf->patterns; pat != NULL; pat = npat) {
83 npat = pat->next;
84 freepattern(pat);
85 }
6373daf9 86 freeca(cf->index);
300d73d9
FT
87 if(cf->capture != NULL)
88 free(cf->capture);
992ce9ef
FT
89 free(cf);
90}
91
600a1ce7 92struct child *getchild(struct config *cf, char *name)
992ce9ef
FT
93{
94 struct child *ch;
95
96 for(ch = cf->children; ch; ch = ch->next) {
97 if(!strcmp(ch->name, name))
98 break;
99 }
100 return(ch);
101}
102
103static struct rule *newrule(struct pattern *pat)
104{
105 int i;
106 struct rule *rule;
107
108 for(i = 0; pat->rules[i]; i++);
109 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
cceb3611 110 rule = pat->rules[i] = szmalloc(sizeof(*rule));
992ce9ef
FT
111 pat->rules[i + 1] = NULL;
112 return(rule);
113}
114
115static struct pattern *newpattern(void)
116{
117 struct pattern *pat;
118
119 omalloc(pat);
120 pat->rules = szmalloc(sizeof(*pat->rules));
121 return(pat);
122}
123
1eba4f97
FT
124static char **cadup(char **w)
125{
126 char **ret;
127 int i, l;
128
129 l = calen(w);
130 ret = smalloc(sizeof(*ret) * (l + 1));
131 for(i = 0; i < l; i++)
132 ret[i] = sstrdup(w[i]);
133 ret[i] = NULL;
134 return(ret);
135}
136
06c1a718
FT
137static struct pattern *parsepattern(struct cfstate *s)
138{
139 struct pattern *pat;
140 struct rule *rule;
acc2d159 141 struct headmod *head;
1eba4f97 142 int sl;
06c1a718
FT
143
144 if(!strcmp(s->argv[0], "match")) {
145 s->expstart = 1;
146 pat = newpattern();
147 } else {
148 return(NULL);
149 }
150
8232cebe
FT
151 if((s->argc > 1) && !strcmp(s->argv[1], "directory"))
152 pat->type = PT_DIR;
a0b6c27c
FT
153 else if((s->argc > 1) && !strcmp(s->argv[1], "notfound"))
154 pat->type = PT_NOTFOUND;
155 else
156 pat->type = PT_FILE;
06c1a718
FT
157 sl = s->lno;
158 while(1) {
159 getcfline(s);
160 if(!strcmp(s->argv[0], "filename")) {
161 if(s->argc < 2) {
162 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno);
163 continue;
164 }
165 rule = newrule(pat);
166 rule->type = PAT_BASENAME;
1eba4f97 167 rule->patterns = cadup(s->argv + 1);
06c1a718
FT
168 } else if(!strcmp(s->argv[0], "pathname")) {
169 if(s->argc < 2) {
170 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno);
171 continue;
172 }
173 rule = newrule(pat);
174 rule->type = PAT_PATHNAME;
1eba4f97 175 rule->patterns = cadup(s->argv + 1);
06c1a718
FT
176 } else if(!strcmp(s->argv[0], "all")) {
177 newrule(pat)->type = PAT_ALL;
178 } else if(!strcmp(s->argv[0], "default")) {
179 newrule(pat)->type = PAT_DEFAULT;
7711283c
FT
180 } else if(!strcmp(s->argv[0], "local")) {
181 newrule(pat)->type = PAT_LOCAL;
06c1a718
FT
182 } else if(!strcmp(s->argv[0], "handler")) {
183 if(s->argc < 2) {
184 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
185 continue;
186 }
187 if(pat->childnm != NULL)
188 free(pat->childnm);
189 pat->childnm = sstrdup(s->argv[1]);
624f637d 190 } else if(!strcmp(s->argv[0], "fork")) {
1eba4f97 191 pat->fchild = cadup(s->argv + 1);
8cc893f5 192 } else if(!strcmp(s->argv[0], "set") || !strcmp(s->argv[0], "xset")) {
acc2d159 193 if(s->argc < 3) {
8cc893f5 194 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `%s' directive", s->file, s->lno, s->argv[0]);
acc2d159
FT
195 continue;
196 }
197 omalloc(head);
8cc893f5
FT
198 if(!strcmp(s->argv[0], "xset"))
199 head->name = sprintf2("X-Ash-%s", s->argv[1]);
200 else
201 head->name = sstrdup(s->argv[1]);
acc2d159
FT
202 head->value = sstrdup(s->argv[2]);
203 head->next = pat->headers;
204 pat->headers = head;
06c1a718
FT
205 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
206 break;
207 } else {
208 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
209 }
210 }
211
212 if(pat->rules[0] == NULL) {
213 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
214 freepattern(pat);
215 return(NULL);
216 }
624f637d 217 if((pat->childnm == NULL) && (pat->fchild == NULL)) {
06c1a718
FT
218 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
219 freepattern(pat);
220 return(NULL);
221 }
222 return(pat);
223}
224
17e55136
FT
225static struct config *emptyconfig(void)
226{
227 struct config *cf;
228
229 omalloc(cf);
230 return(cf);
231}
232
600a1ce7 233struct config *readconfig(char *file)
992ce9ef 234{
06c1a718
FT
235 struct cfstate *s;
236 FILE *in;
992ce9ef 237 struct config *cf;
992ce9ef
FT
238 struct child *child;
239 struct pattern *pat;
992ce9ef 240
17e55136
FT
241 if((in = fopen(file, "r")) == NULL) {
242 flog(LOG_WARNING, "%s: %s", file, strerror(errno));
992ce9ef 243 return(NULL);
06c1a718 244 }
17e55136
FT
245 s = mkcfparser(in, file);
246 cf = emptyconfig();
06c1a718
FT
247
248 while(1) {
249 getcfline(s);
250 if((child = parsechild(s)) != NULL) {
251 child->next = cf->children;
252 cf->children = child;
253 } else if((pat = parsepattern(s)) != NULL) {
254 pat->next = cf->patterns;
255 cf->patterns = pat;
6373daf9
FT
256 } else if(!strcmp(s->argv[0], "index-file")) {
257 freeca(cf->index);
7bf1ad5a 258 cf->index = cadup(s->argv + 1);
300d73d9
FT
259 } else if(!strcmp(s->argv[0], "capture")) {
260 if(s->argc < 2) {
261 flog(LOG_WARNING, "%s:%i: missing argument to capture declaration", s->file, s->lno);
262 continue;
263 }
264 if(cf->capture != NULL)
265 free(cf->capture);
266 cf->capture = sstrdup(s->argv[1]);
257cd4a2
FT
267 cf->caproot = 0;
268 if((s->argc > 2) && strchr(s->argv[2], 'D'))
269 cf->caproot = 1;
06c1a718
FT
270 } else if(!strcmp(s->argv[0], "eof")) {
271 break;
272 } else {
273 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
992ce9ef 274 }
06c1a718 275 }
992ce9ef 276
06c1a718
FT
277 freecfparser(s);
278 fclose(in);
992ce9ef
FT
279 return(cf);
280}
281
600a1ce7 282struct config *getconfig(char *path)
992ce9ef 283{
a756e349 284 struct config *cf, *ocf;
992ce9ef 285 struct stat sb;
17e55136
FT
286 char *fn;
287 time_t mtime;
992ce9ef 288
17e55136 289 fn = sprintf3("%s/.htrc", path);
992ce9ef
FT
290 for(cf = cflist; cf != NULL; cf = cf->next) {
291 if(!strcmp(cf->path, path)) {
17e55136 292 if(now - cf->lastck > 5) {
0fb57c32 293 cf->lastck = now;
a756e349 294 if(stat(fn, &sb) || (sb.st_mtime != cf->mtime))
17e55136 295 break;
992ce9ef
FT
296 }
297 return(cf);
298 }
299 }
a756e349 300 ocf = cf;
17e55136
FT
301 if(access(fn, R_OK) || stat(fn, &sb)) {
302 cf = emptyconfig();
303 mtime = 0;
304 } else {
305 if((cf = readconfig(fn)) == NULL)
306 return(NULL);
307 mtime = sb.st_mtime;
992ce9ef 308 }
a756e349 309 if(ocf != NULL) {
1924fe8c 310 mergechildren(cf->children, ocf->children);
a756e349
FT
311 freeconfig(ocf);
312 }
17e55136
FT
313 cf->path = sstrdup(path);
314 cf->mtime = mtime;
315 cf->lastck = now;
316 cf->next = cflist;
d245c327
FT
317 cf->prev = NULL;
318 if(cflist != NULL)
319 cflist->prev = cf;
17e55136 320 cflist = cf;
992ce9ef
FT
321 return(cf);
322}
323
600a1ce7 324struct config **getconfigs(char *file)
992ce9ef 325{
8dce6d19
FT
326 static struct config **ret = NULL;
327 struct {
328 struct config **b;
329 size_t s, d;
330 } buf;
992ce9ef 331 struct config *cf;
8dce6d19
FT
332 char *tmp, *p;
333
334 if(ret != NULL)
335 free(ret);
336 bufinit(buf);
7db7f678
FT
337 if(!strncmp(file, "./", 2))
338 file += 2;
8dce6d19 339 tmp = sstrdup(file);
992ce9ef 340 while(1) {
8dce6d19 341 if((p = strrchr(tmp, '/')) == NULL)
992ce9ef 342 break;
8dce6d19
FT
343 *p = 0;
344 if((cf = getconfig(tmp)) != NULL)
345 bufadd(buf, cf);
346 }
347 free(tmp);
348 if((cf = getconfig(".")) != NULL)
349 bufadd(buf, cf);
0fc6fd13
FT
350 if(lconfig != NULL)
351 bufadd(buf, lconfig);
352 if(gconfig != NULL)
353 bufadd(buf, gconfig);
8dce6d19
FT
354 bufadd(buf, NULL);
355 return(ret = buf.b);
356}
357
da75c835 358struct child *findchild(char *file, char *name, struct config **cf)
8dce6d19
FT
359{
360 int i;
361 struct config **cfs;
362 struct child *ch;
363
b70b2d4f
FT
364 if(cf != NULL)
365 *cf = NULL;
8dce6d19
FT
366 cfs = getconfigs(file);
367 for(i = 0; cfs[i] != NULL; i++) {
da75c835
FT
368 if((ch = getchild(cfs[i], name)) != NULL) {
369 if(cf != NULL)
370 *cf = cfs[i];
371 return(ch);
372 }
992ce9ef 373 }
b70b2d4f
FT
374 if(!strcmp(name, ".notfound"))
375 return(notfound);
da75c835 376 return(NULL);
992ce9ef
FT
377}
378
a0b6c27c 379struct pattern *findmatch(char *file, int trydefault, int type)
992ce9ef 380{
1eba4f97 381 int i, o, c;
edcd094e 382 char *bn, *ln;
8dce6d19 383 struct config **cfs;
992ce9ef
FT
384 struct pattern *pat;
385 struct rule *rule;
7711283c 386 size_t pl;
992ce9ef
FT
387
388 if((bn = strrchr(file, '/')) != NULL)
389 bn++;
390 else
391 bn = file;
8dce6d19
FT
392 cfs = getconfigs(file);
393 for(c = 0; cfs[c] != NULL; c++) {
edcd094e
FT
394 if(cfs[c]->path == NULL) {
395 ln = file;
396 } else {
397 pl = strlen(cfs[c]->path);
398 if((strlen(file) > pl) && !strncmp(file, cfs[c]->path, pl) && (file[pl] == '/'))
399 ln = file + pl + 1;
400 else
401 ln = file; /* This should only happen in the base directory. */
402 }
8dce6d19 403 for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
a0b6c27c 404 if(pat->type != type)
8232cebe 405 continue;
992ce9ef
FT
406 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
407 if(rule->type == PAT_BASENAME) {
1eba4f97
FT
408 for(o = 0; rule->patterns[o] != NULL; o++) {
409 if(!fnmatch(rule->patterns[o], bn, 0))
410 break;
411 }
412 if(rule->patterns[o] == NULL)
992ce9ef
FT
413 break;
414 } else if(rule->type == PAT_PATHNAME) {
1eba4f97 415 for(o = 0; rule->patterns[o] != NULL; o++) {
edcd094e 416 if(!fnmatch(rule->patterns[o], ln, FNM_PATHNAME))
1eba4f97
FT
417 break;
418 }
419 if(rule->patterns[o] == NULL)
992ce9ef
FT
420 break;
421 } else if(rule->type == PAT_ALL) {
422 } else if(rule->type == PAT_DEFAULT) {
423 if(!trydefault)
424 break;
7711283c 425 } else if(rule->type == PAT_LOCAL) {
edcd094e 426 if(strchr(ln, '/'))
7711283c 427 break;
992ce9ef
FT
428 }
429 }
430 if(!rule)
8dce6d19 431 return(pat);
992ce9ef
FT
432 }
433 }
8232cebe 434 if(!trydefault)
a0b6c27c 435 return(findmatch(file, 1, type));
8dce6d19 436 return(NULL);
992ce9ef 437}
b70b2d4f
FT
438
439static int donotfound(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
440{
441 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
442 return(0);
443}
444
445static struct chandler i_notfound = {
446 .handle = donotfound,
447};
448
449static struct child s_notfound = {
450 .name = ".notfound",
451 .iface = &i_notfound,
452};
453struct child *notfound = &s_notfound;