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