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