etc: Add environment option to run init.d/ashd silently.
[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);
7aad7863 87 freeca(cf->dotallow);
300d73d9
FT
88 if(cf->capture != NULL)
89 free(cf->capture);
539c7b9f
FT
90 if(cf->reparse != NULL)
91 free(cf->reparse);
992ce9ef
FT
92 free(cf);
93}
94
600a1ce7 95struct child *getchild(struct config *cf, char *name)
992ce9ef
FT
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));
cceb3611 113 rule = pat->rules[i] = szmalloc(sizeof(*rule));
992ce9ef
FT
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
1eba4f97
FT
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
06c1a718
FT
140static struct pattern *parsepattern(struct cfstate *s)
141{
142 struct pattern *pat;
143 struct rule *rule;
acc2d159 144 struct headmod *head;
1eba4f97 145 int sl;
06c1a718
FT
146
147 if(!strcmp(s->argv[0], "match")) {
148 s->expstart = 1;
149 pat = newpattern();
150 } else {
151 return(NULL);
152 }
153
8232cebe
FT
154 if((s->argc > 1) && !strcmp(s->argv[1], "directory"))
155 pat->type = PT_DIR;
a0b6c27c
FT
156 else if((s->argc > 1) && !strcmp(s->argv[1], "notfound"))
157 pat->type = PT_NOTFOUND;
158 else
159 pat->type = PT_FILE;
06c1a718
FT
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;
1eba4f97 170 rule->patterns = cadup(s->argv + 1);
06c1a718
FT
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;
1eba4f97 178 rule->patterns = cadup(s->argv + 1);
06c1a718
FT
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;
7711283c
FT
183 } else if(!strcmp(s->argv[0], "local")) {
184 newrule(pat)->type = PAT_LOCAL;
06c1a718
FT
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]);
624f637d 193 } else if(!strcmp(s->argv[0], "fork")) {
1eba4f97 194 pat->fchild = cadup(s->argv + 1);
8cc893f5 195 } else if(!strcmp(s->argv[0], "set") || !strcmp(s->argv[0], "xset")) {
acc2d159 196 if(s->argc < 3) {
8cc893f5 197 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `%s' directive", s->file, s->lno, s->argv[0]);
acc2d159
FT
198 continue;
199 }
200 omalloc(head);
8cc893f5
FT
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]);
acc2d159
FT
205 head->value = sstrdup(s->argv[2]);
206 head->next = pat->headers;
207 pat->headers = head;
06c1a718
FT
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 }
624f637d 220 if((pat->childnm == NULL) && (pat->fchild == NULL)) {
06c1a718
FT
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
17e55136
FT
228static struct config *emptyconfig(void)
229{
230 struct config *cf;
231
232 omalloc(cf);
233 return(cf);
234}
235
600a1ce7 236struct config *readconfig(char *file)
992ce9ef 237{
06c1a718
FT
238 struct cfstate *s;
239 FILE *in;
992ce9ef 240 struct config *cf;
992ce9ef
FT
241 struct child *child;
242 struct pattern *pat;
992ce9ef 243
17e55136
FT
244 if((in = fopen(file, "r")) == NULL) {
245 flog(LOG_WARNING, "%s: %s", file, strerror(errno));
992ce9ef 246 return(NULL);
06c1a718 247 }
17e55136
FT
248 s = mkcfparser(in, file);
249 cf = emptyconfig();
06c1a718
FT
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;
6373daf9
FT
259 } else if(!strcmp(s->argv[0], "index-file")) {
260 freeca(cf->index);
7bf1ad5a 261 cf->index = cadup(s->argv + 1);
7aad7863
FT
262 } else if(!strcmp(s->argv[0], "dot-allow")) {
263 freeca(cf->dotallow);
264 cf->dotallow = cadup(s->argv + 1);
300d73d9
FT
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]);
257cd4a2
FT
273 cf->caproot = 0;
274 if((s->argc > 2) && strchr(s->argv[2], 'D'))
275 cf->caproot = 1;
539c7b9f
FT
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;
06c1a718
FT
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]);
992ce9ef 291 }
06c1a718 292 }
992ce9ef 293
06c1a718
FT
294 freecfparser(s);
295 fclose(in);
992ce9ef
FT
296 return(cf);
297}
298
600a1ce7 299struct config *getconfig(char *path)
992ce9ef 300{
a756e349 301 struct config *cf, *ocf;
992ce9ef 302 struct stat sb;
17e55136
FT
303 char *fn;
304 time_t mtime;
992ce9ef 305
17e55136 306 fn = sprintf3("%s/.htrc", path);
992ce9ef
FT
307 for(cf = cflist; cf != NULL; cf = cf->next) {
308 if(!strcmp(cf->path, path)) {
17e55136 309 if(now - cf->lastck > 5) {
0fb57c32 310 cf->lastck = now;
a756e349 311 if(stat(fn, &sb) || (sb.st_mtime != cf->mtime))
17e55136 312 break;
992ce9ef
FT
313 }
314 return(cf);
315 }
316 }
a756e349 317 ocf = cf;
17e55136
FT
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;
992ce9ef 325 }
a756e349 326 if(ocf != NULL) {
1924fe8c 327 mergechildren(cf->children, ocf->children);
a756e349
FT
328 freeconfig(ocf);
329 }
17e55136
FT
330 cf->path = sstrdup(path);
331 cf->mtime = mtime;
332 cf->lastck = now;
333 cf->next = cflist;
d245c327
FT
334 cf->prev = NULL;
335 if(cflist != NULL)
336 cflist->prev = cf;
17e55136 337 cflist = cf;
992ce9ef
FT
338 return(cf);
339}
340
600a1ce7 341struct config **getconfigs(char *file)
992ce9ef 342{
8dce6d19
FT
343 static struct config **ret = NULL;
344 struct {
345 struct config **b;
346 size_t s, d;
347 } buf;
992ce9ef 348 struct config *cf;
8dce6d19
FT
349 char *tmp, *p;
350
351 if(ret != NULL)
352 free(ret);
353 bufinit(buf);
7db7f678
FT
354 if(!strncmp(file, "./", 2))
355 file += 2;
8dce6d19 356 tmp = sstrdup(file);
992ce9ef 357 while(1) {
8dce6d19 358 if((p = strrchr(tmp, '/')) == NULL)
992ce9ef 359 break;
8dce6d19
FT
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);
0fc6fd13
FT
367 if(lconfig != NULL)
368 bufadd(buf, lconfig);
369 if(gconfig != NULL)
370 bufadd(buf, gconfig);
8dce6d19
FT
371 bufadd(buf, NULL);
372 return(ret = buf.b);
373}
374
da75c835 375struct child *findchild(char *file, char *name, struct config **cf)
8dce6d19
FT
376{
377 int i;
378 struct config **cfs;
379 struct child *ch;
380
b70b2d4f
FT
381 if(cf != NULL)
382 *cf = NULL;
8dce6d19
FT
383 cfs = getconfigs(file);
384 for(i = 0; cfs[i] != NULL; i++) {
da75c835
FT
385 if((ch = getchild(cfs[i], name)) != NULL) {
386 if(cf != NULL)
387 *cf = cfs[i];
388 return(ch);
389 }
992ce9ef 390 }
b70b2d4f
FT
391 if(!strcmp(name, ".notfound"))
392 return(notfound);
da75c835 393 return(NULL);
992ce9ef
FT
394}
395
a0b6c27c 396struct pattern *findmatch(char *file, int trydefault, int type)
992ce9ef 397{
1eba4f97 398 int i, o, c;
edcd094e 399 char *bn, *ln;
8dce6d19 400 struct config **cfs;
992ce9ef
FT
401 struct pattern *pat;
402 struct rule *rule;
7711283c 403 size_t pl;
992ce9ef
FT
404
405 if((bn = strrchr(file, '/')) != NULL)
406 bn++;
407 else
408 bn = file;
8dce6d19
FT
409 cfs = getconfigs(file);
410 for(c = 0; cfs[c] != NULL; c++) {
edcd094e
FT
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 }
8dce6d19 420 for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
a0b6c27c 421 if(pat->type != type)
8232cebe 422 continue;
992ce9ef
FT
423 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
424 if(rule->type == PAT_BASENAME) {
1eba4f97
FT
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)
992ce9ef
FT
430 break;
431 } else if(rule->type == PAT_PATHNAME) {
1eba4f97 432 for(o = 0; rule->patterns[o] != NULL; o++) {
edcd094e 433 if(!fnmatch(rule->patterns[o], ln, FNM_PATHNAME))
1eba4f97
FT
434 break;
435 }
436 if(rule->patterns[o] == NULL)
992ce9ef
FT
437 break;
438 } else if(rule->type == PAT_ALL) {
439 } else if(rule->type == PAT_DEFAULT) {
440 if(!trydefault)
441 break;
7711283c 442 } else if(rule->type == PAT_LOCAL) {
edcd094e 443 if(strchr(ln, '/'))
7711283c 444 break;
992ce9ef
FT
445 }
446 }
447 if(!rule)
8dce6d19 448 return(pat);
992ce9ef
FT
449 }
450 }
8232cebe 451 if(!trydefault)
a0b6c27c 452 return(findmatch(file, 1, type));
8dce6d19 453 return(NULL);
992ce9ef 454}
b70b2d4f
FT
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;