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