dirplex: Added a capture option to ignore captures of the root directory.
[ashd.git] / src / dirplex / conf.c
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
37 static struct config *cflist;
38 struct config *gconfig, *lconfig;
39
40 static void freerule(struct rule *rule)
41 {
42     freeca(rule->patterns);
43     free(rule);
44 }
45
46 static 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
65 static 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
92 struct 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
103 static 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
115 static 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
124 static 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
137 static 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     sl = s->lno;
154     while(1) {
155         getcfline(s);
156         if(!strcmp(s->argv[0], "filename")) {
157             if(s->argc < 2) {
158                 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno);
159                 continue;
160             }
161             rule = newrule(pat);
162             rule->type = PAT_BASENAME;
163             rule->patterns = cadup(s->argv + 1);
164         } else if(!strcmp(s->argv[0], "pathname")) {
165             if(s->argc < 2) {
166                 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno);
167                 continue;
168             }
169             rule = newrule(pat);
170             rule->type = PAT_PATHNAME;
171             rule->patterns = cadup(s->argv + 1);
172         } else if(!strcmp(s->argv[0], "all")) {
173             newrule(pat)->type = PAT_ALL;
174         } else if(!strcmp(s->argv[0], "default")) {
175             newrule(pat)->type = PAT_DEFAULT;
176         } else if(!strcmp(s->argv[0], "local")) {
177             newrule(pat)->type = PAT_LOCAL;
178         } else if(!strcmp(s->argv[0], "handler")) {
179             if(s->argc < 2) {
180                 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
181                 continue;
182             }
183             if(pat->childnm != NULL)
184                 free(pat->childnm);
185             pat->childnm = sstrdup(s->argv[1]);
186         } else if(!strcmp(s->argv[0], "fork")) {
187             pat->fchild = cadup(s->argv + 1);
188         } else if(!strcmp(s->argv[0], "set") || !strcmp(s->argv[0], "xset")) {
189             if(s->argc < 3) {
190                 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `%s' directive", s->file, s->lno, s->argv[0]);
191                 continue;
192             }
193             omalloc(head);
194             if(!strcmp(s->argv[0], "xset"))
195                 head->name = sprintf2("X-Ash-%s", s->argv[1]);
196             else
197                 head->name = sstrdup(s->argv[1]);
198             head->value = sstrdup(s->argv[2]);
199             head->next = pat->headers;
200             pat->headers = head;
201         } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
202             break;
203         } else {
204             flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
205         }
206     }
207     
208     if(pat->rules[0] == NULL) {
209         flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
210         freepattern(pat);
211         return(NULL);
212     }
213     if((pat->childnm == NULL) && (pat->fchild == NULL)) {
214         flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
215         freepattern(pat);
216         return(NULL);
217     }
218     return(pat);
219 }
220
221 static struct config *emptyconfig(void)
222 {
223     struct config *cf;
224     
225     omalloc(cf);
226     return(cf);
227 }
228
229 struct config *readconfig(char *file)
230 {
231     struct cfstate *s;
232     FILE *in;
233     struct config *cf;
234     struct child *child;
235     struct pattern *pat;
236     
237     if((in = fopen(file, "r")) == NULL) {
238         flog(LOG_WARNING, "%s: %s", file, strerror(errno));
239         return(NULL);
240     }
241     s = mkcfparser(in, file);
242     cf = emptyconfig();
243     
244     while(1) {
245         getcfline(s);
246         if((child = parsechild(s)) != NULL) {
247             child->next = cf->children;
248             cf->children = child;
249         } else if((pat = parsepattern(s)) != NULL) {
250             pat->next = cf->patterns;
251             cf->patterns = pat;
252         } else if(!strcmp(s->argv[0], "index-file")) {
253             freeca(cf->index);
254             cf->index = NULL;
255             if(s->argc > 1)
256                 cf->index = cadup(s->argv + 1);
257         } else if(!strcmp(s->argv[0], "capture")) {
258             if(s->argc < 2) {
259                 flog(LOG_WARNING, "%s:%i: missing argument to capture declaration", s->file, s->lno);
260                 continue;
261             }
262             if(cf->capture != NULL)
263                 free(cf->capture);
264             cf->capture = sstrdup(s->argv[1]);
265             cf->caproot = 1;
266             if((s->argc > 2) && strchr(s->argv[2], 'R'))
267                 cf->caproot = 0;
268         } else if(!strcmp(s->argv[0], "eof")) {
269             break;
270         } else {
271             flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
272         }
273     }
274     
275     freecfparser(s);
276     fclose(in);
277     return(cf);
278 }
279
280 struct config *getconfig(char *path)
281 {
282     struct config *cf, *ocf;
283     struct stat sb;
284     char *fn;
285     time_t mtime;
286     
287     fn = sprintf3("%s/.htrc", path);
288     for(cf = cflist; cf != NULL; cf = cf->next) {
289         if(!strcmp(cf->path, path)) {
290             if(now - cf->lastck > 5) {
291                 cf->lastck = now;
292                 if(stat(fn, &sb) || (sb.st_mtime != cf->mtime))
293                     break;
294             }
295             return(cf);
296         }
297     }
298     ocf = cf;
299     if(access(fn, R_OK) || stat(fn, &sb)) {
300         cf = emptyconfig();
301         mtime = 0;
302     } else {
303         if((cf = readconfig(fn)) == NULL)
304             return(NULL);
305         mtime = sb.st_mtime;
306     }
307     if(ocf != NULL) {
308         mergechildren(cf->children, ocf->children);
309         freeconfig(ocf);
310     }
311     cf->path = sstrdup(path);
312     cf->mtime = mtime;
313     cf->lastck = now;
314     cf->next = cflist;
315     cf->prev = NULL;
316     if(cflist != NULL)
317         cflist->prev = cf;
318     cflist = cf;
319     return(cf);
320 }
321
322 struct config **getconfigs(char *file)
323 {
324     static struct config **ret = NULL;
325     struct {
326         struct config **b;
327         size_t s, d;
328     } buf;
329     struct config *cf;
330     char *tmp, *p;
331     
332     if(ret != NULL)
333         free(ret);
334     bufinit(buf);
335     if(!strncmp(file, "./", 2))
336         file += 2;
337     tmp = sstrdup(file);
338     while(1) {
339         if((p = strrchr(tmp, '/')) == NULL)
340             break;
341         *p = 0;
342         if((cf = getconfig(tmp)) != NULL)
343             bufadd(buf, cf);
344     }
345     free(tmp);
346     if((cf = getconfig(".")) != NULL)
347         bufadd(buf, cf);
348     if(lconfig != NULL)
349         bufadd(buf, lconfig);
350     if(gconfig != NULL)
351         bufadd(buf, gconfig);
352     bufadd(buf, NULL);
353     return(ret = buf.b);
354 }
355
356 struct child *findchild(char *file, char *name, struct config **cf)
357 {
358     int i;
359     struct config **cfs;
360     struct child *ch;
361     
362     if(cf != NULL)
363         *cf = NULL;
364     cfs = getconfigs(file);
365     for(i = 0; cfs[i] != NULL; i++) {
366         if((ch = getchild(cfs[i], name)) != NULL) {
367             if(cf != NULL)
368                 *cf = cfs[i];
369             return(ch);
370         }
371     }
372     if(!strcmp(name, ".notfound"))
373         return(notfound);
374     return(NULL);
375 }
376
377 struct pattern *findmatch(char *file, int trydefault, int dir)
378 {
379     int i, o, c;
380     char *bn, *ln;
381     struct config **cfs;
382     struct pattern *pat;
383     struct rule *rule;
384     size_t pl;
385     
386     if((bn = strrchr(file, '/')) != NULL)
387         bn++;
388     else
389         bn = file;
390     cfs = getconfigs(file);
391     for(c = 0; cfs[c] != NULL; c++) {
392         if(cfs[c]->path == NULL) {
393             ln = file;
394         } else {
395             pl = strlen(cfs[c]->path);
396             if((strlen(file) > pl) && !strncmp(file, cfs[c]->path, pl) && (file[pl] == '/'))
397                 ln = file + pl + 1;
398             else
399                 ln = file;      /* This should only happen in the base directory. */
400         }
401         for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
402             if(!dir && (pat->type == PT_DIR))
403                 continue;
404             if(dir && (pat->type != PT_DIR))
405                 continue;
406             for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
407                 if(rule->type == PAT_BASENAME) {
408                     for(o = 0; rule->patterns[o] != NULL; o++) {
409                         if(!fnmatch(rule->patterns[o], bn, 0))
410                             break;
411                     }
412                     if(rule->patterns[o] == NULL)
413                         break;
414                 } else if(rule->type == PAT_PATHNAME) {
415                     for(o = 0; rule->patterns[o] != NULL; o++) {
416                         if(!fnmatch(rule->patterns[o], ln, FNM_PATHNAME))
417                             break;
418                     }
419                     if(rule->patterns[o] == NULL)
420                         break;
421                 } else if(rule->type == PAT_ALL) {
422                 } else if(rule->type == PAT_DEFAULT) {
423                     if(!trydefault)
424                         break;
425                 } else if(rule->type == PAT_LOCAL) {
426                     if(strchr(ln, '/'))
427                         break;
428                 }
429             }
430             if(!rule)
431                 return(pat);
432         }
433     }
434     if(!trydefault)
435         return(findmatch(file, 1, dir));
436     return(NULL);
437 }
438
439 static int donotfound(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
440 {
441     simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
442     return(0);
443 }
444
445 static struct chandler i_notfound = {
446     .handle = donotfound,
447 };
448
449 static struct child s_notfound = {
450     .name = ".notfound",
451     .iface = &i_notfound,
452 };
453 struct child *notfound = &s_notfound;