Updated changelog.
[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 if(cf->capture != NULL)
88 free(cf->capture);
89 free(cf);
90}
91
92struct child *getchild(struct config *cf, char *name)
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));
110 rule = pat->rules[i] = szmalloc(sizeof(*rule));
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
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
137static struct pattern *parsepattern(struct cfstate *s)
138{
139 struct pattern *pat;
140 struct rule *rule;
141 struct headmod *head;
142 int sl;
143
144 if(!strcmp(s->argv[0], "match")) {
145 s->expstart = 1;
146 pat = newpattern();
147 } else {
148 return(NULL);
149 }
150
151 if((s->argc > 1) && !strcmp(s->argv[1], "directory"))
152 pat->type = PT_DIR;
153 else if((s->argc > 1) && !strcmp(s->argv[1], "notfound"))
154 pat->type = PT_NOTFOUND;
155 else
156 pat->type = PT_FILE;
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;
167 rule->patterns = cadup(s->argv + 1);
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;
175 rule->patterns = cadup(s->argv + 1);
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;
180 } else if(!strcmp(s->argv[0], "local")) {
181 newrule(pat)->type = PAT_LOCAL;
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]);
190 } else if(!strcmp(s->argv[0], "fork")) {
191 pat->fchild = cadup(s->argv + 1);
192 } else if(!strcmp(s->argv[0], "set") || !strcmp(s->argv[0], "xset")) {
193 if(s->argc < 3) {
194 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `%s' directive", s->file, s->lno, s->argv[0]);
195 continue;
196 }
197 omalloc(head);
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]);
202 head->value = sstrdup(s->argv[2]);
203 head->next = pat->headers;
204 pat->headers = head;
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 }
217 if((pat->childnm == NULL) && (pat->fchild == NULL)) {
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
225static struct config *emptyconfig(void)
226{
227 struct config *cf;
228
229 omalloc(cf);
230 return(cf);
231}
232
233struct config *readconfig(char *file)
234{
235 struct cfstate *s;
236 FILE *in;
237 struct config *cf;
238 struct child *child;
239 struct pattern *pat;
240
241 if((in = fopen(file, "r")) == NULL) {
242 flog(LOG_WARNING, "%s: %s", file, strerror(errno));
243 return(NULL);
244 }
245 s = mkcfparser(in, file);
246 cf = emptyconfig();
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;
256 } else if(!strcmp(s->argv[0], "index-file")) {
257 freeca(cf->index);
258 cf->index = NULL;
259 if(s->argc > 1)
260 cf->index = cadup(s->argv + 1);
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]);
269 cf->caproot = 0;
270 if((s->argc > 2) && strchr(s->argv[2], 'D'))
271 cf->caproot = 1;
272 } else if(!strcmp(s->argv[0], "eof")) {
273 break;
274 } else {
275 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
276 }
277 }
278
279 freecfparser(s);
280 fclose(in);
281 return(cf);
282}
283
284struct config *getconfig(char *path)
285{
286 struct config *cf, *ocf;
287 struct stat sb;
288 char *fn;
289 time_t mtime;
290
291 fn = sprintf3("%s/.htrc", path);
292 for(cf = cflist; cf != NULL; cf = cf->next) {
293 if(!strcmp(cf->path, path)) {
294 if(now - cf->lastck > 5) {
295 cf->lastck = now;
296 if(stat(fn, &sb) || (sb.st_mtime != cf->mtime))
297 break;
298 }
299 return(cf);
300 }
301 }
302 ocf = cf;
303 if(access(fn, R_OK) || stat(fn, &sb)) {
304 cf = emptyconfig();
305 mtime = 0;
306 } else {
307 if((cf = readconfig(fn)) == NULL)
308 return(NULL);
309 mtime = sb.st_mtime;
310 }
311 if(ocf != NULL) {
312 mergechildren(cf->children, ocf->children);
313 freeconfig(ocf);
314 }
315 cf->path = sstrdup(path);
316 cf->mtime = mtime;
317 cf->lastck = now;
318 cf->next = cflist;
319 cf->prev = NULL;
320 if(cflist != NULL)
321 cflist->prev = cf;
322 cflist = cf;
323 return(cf);
324}
325
326struct config **getconfigs(char *file)
327{
328 static struct config **ret = NULL;
329 struct {
330 struct config **b;
331 size_t s, d;
332 } buf;
333 struct config *cf;
334 char *tmp, *p;
335
336 if(ret != NULL)
337 free(ret);
338 bufinit(buf);
339 if(!strncmp(file, "./", 2))
340 file += 2;
341 tmp = sstrdup(file);
342 while(1) {
343 if((p = strrchr(tmp, '/')) == NULL)
344 break;
345 *p = 0;
346 if((cf = getconfig(tmp)) != NULL)
347 bufadd(buf, cf);
348 }
349 free(tmp);
350 if((cf = getconfig(".")) != NULL)
351 bufadd(buf, cf);
352 if(lconfig != NULL)
353 bufadd(buf, lconfig);
354 if(gconfig != NULL)
355 bufadd(buf, gconfig);
356 bufadd(buf, NULL);
357 return(ret = buf.b);
358}
359
360struct child *findchild(char *file, char *name, struct config **cf)
361{
362 int i;
363 struct config **cfs;
364 struct child *ch;
365
366 if(cf != NULL)
367 *cf = NULL;
368 cfs = getconfigs(file);
369 for(i = 0; cfs[i] != NULL; i++) {
370 if((ch = getchild(cfs[i], name)) != NULL) {
371 if(cf != NULL)
372 *cf = cfs[i];
373 return(ch);
374 }
375 }
376 if(!strcmp(name, ".notfound"))
377 return(notfound);
378 return(NULL);
379}
380
381struct pattern *findmatch(char *file, int trydefault, int type)
382{
383 int i, o, c;
384 char *bn, *ln;
385 struct config **cfs;
386 struct pattern *pat;
387 struct rule *rule;
388 size_t pl;
389
390 if((bn = strrchr(file, '/')) != NULL)
391 bn++;
392 else
393 bn = file;
394 cfs = getconfigs(file);
395 for(c = 0; cfs[c] != NULL; c++) {
396 if(cfs[c]->path == NULL) {
397 ln = file;
398 } else {
399 pl = strlen(cfs[c]->path);
400 if((strlen(file) > pl) && !strncmp(file, cfs[c]->path, pl) && (file[pl] == '/'))
401 ln = file + pl + 1;
402 else
403 ln = file; /* This should only happen in the base directory. */
404 }
405 for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
406 if(pat->type != type)
407 continue;
408 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
409 if(rule->type == PAT_BASENAME) {
410 for(o = 0; rule->patterns[o] != NULL; o++) {
411 if(!fnmatch(rule->patterns[o], bn, 0))
412 break;
413 }
414 if(rule->patterns[o] == NULL)
415 break;
416 } else if(rule->type == PAT_PATHNAME) {
417 for(o = 0; rule->patterns[o] != NULL; o++) {
418 if(!fnmatch(rule->patterns[o], ln, FNM_PATHNAME))
419 break;
420 }
421 if(rule->patterns[o] == NULL)
422 break;
423 } else if(rule->type == PAT_ALL) {
424 } else if(rule->type == PAT_DEFAULT) {
425 if(!trydefault)
426 break;
427 } else if(rule->type == PAT_LOCAL) {
428 if(strchr(ln, '/'))
429 break;
430 }
431 }
432 if(!rule)
433 return(pat);
434 }
435 }
436 if(!trydefault)
437 return(findmatch(file, 1, type));
438 return(NULL);
439}
440
441static int donotfound(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
442{
443 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
444 return(0);
445}
446
447static struct chandler i_notfound = {
448 .handle = donotfound,
449};
450
451static struct child s_notfound = {
452 .name = ".notfound",
453 .iface = &i_notfound,
454};
455struct child *notfound = &s_notfound;