dirplex: Start children in the directory in which they were declared.
[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
34 #include "dirplex.h"
35
36 static struct config *cflist;
37 struct config *gconfig, *lconfig;
38
39 static void freerule(struct rule *rule)
40 {
41     freeca(rule->patterns);
42     free(rule);
43 }
44
45 static void freepattern(struct pattern *pat)
46 {
47     struct rule **rule;
48     
49     for(rule = pat->rules; *rule; rule++)
50         freerule(*rule);
51     if(pat->childnm != NULL)
52         free(pat->childnm);
53     freeca(pat->fchild);
54     free(pat);
55 }
56
57 static void freeconfig(struct config *cf)
58 {
59     struct child *ch, *nch;
60     struct pattern *pat, *npat;
61     
62     if(cf->prev != NULL)
63         cf->prev->next = cf->next;
64     if(cf->next != NULL)
65         cf->next->prev = cf->prev;
66     if(cf == cflist)
67         cflist = cf->next;
68     if(cf->path != NULL)
69         free(cf->path);
70     for(ch = cf->children; ch != NULL; ch = nch) {
71         nch = ch->next;
72         freechild(ch);
73     }
74     for(pat = cf->patterns; pat != NULL; pat = npat) {
75         npat = pat->next;
76         freepattern(pat);
77     }
78     freeca(cf->index);
79     free(cf);
80 }
81
82 struct child *getchild(struct config *cf, char *name)
83 {
84     struct child *ch;
85     
86     for(ch = cf->children; ch; ch = ch->next) {
87         if(!strcmp(ch->name, name))
88             break;
89     }
90     return(ch);
91 }
92
93 static struct rule *newrule(struct pattern *pat)
94 {
95     int i;
96     struct rule *rule;
97     
98     for(i = 0; pat->rules[i]; i++);
99     pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
100     rule = pat->rules[i] = szmalloc(sizeof(*rule));
101     pat->rules[i + 1] = NULL;
102     return(rule);
103 }
104
105 static struct pattern *newpattern(void)
106 {
107     struct pattern *pat;
108     
109     omalloc(pat);
110     pat->rules = szmalloc(sizeof(*pat->rules));
111     return(pat);
112 }
113
114 static char **cadup(char **w)
115 {
116     char **ret;
117     int i, l;
118     
119     l = calen(w);
120     ret = smalloc(sizeof(*ret) * (l + 1));
121     for(i = 0; i < l; i++)
122         ret[i] = sstrdup(w[i]);
123     ret[i] = NULL;
124     return(ret);
125 }
126
127 static struct pattern *parsepattern(struct cfstate *s)
128 {
129     struct pattern *pat;
130     struct rule *rule;
131     int sl;
132
133     if(!strcmp(s->argv[0], "match")) {
134         s->expstart = 1;
135         pat = newpattern();
136     } else {
137         return(NULL);
138     }
139     
140     if((s->argc > 1) && !strcmp(s->argv[1], "directory"))
141         pat->type = PT_DIR;
142     sl = s->lno;
143     while(1) {
144         getcfline(s);
145         if(!strcmp(s->argv[0], "filename")) {
146             if(s->argc < 2) {
147                 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno);
148                 continue;
149             }
150             rule = newrule(pat);
151             rule->type = PAT_BASENAME;
152             rule->patterns = cadup(s->argv + 1);
153         } else if(!strcmp(s->argv[0], "pathname")) {
154             if(s->argc < 2) {
155                 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno);
156                 continue;
157             }
158             rule = newrule(pat);
159             rule->type = PAT_PATHNAME;
160             rule->patterns = cadup(s->argv + 1);
161         } else if(!strcmp(s->argv[0], "all")) {
162             newrule(pat)->type = PAT_ALL;
163         } else if(!strcmp(s->argv[0], "default")) {
164             newrule(pat)->type = PAT_DEFAULT;
165         } else if(!strcmp(s->argv[0], "handler")) {
166             if(s->argc < 2) {
167                 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
168                 continue;
169             }
170             if(pat->childnm != NULL)
171                 free(pat->childnm);
172             pat->childnm = sstrdup(s->argv[1]);
173         } else if(!strcmp(s->argv[0], "fork")) {
174             pat->fchild = cadup(s->argv + 1);
175         } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
176             break;
177         } else {
178             flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
179         }
180     }
181     
182     if(pat->rules[0] == NULL) {
183         flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
184         freepattern(pat);
185         return(NULL);
186     }
187     if((pat->childnm == NULL) && (pat->fchild == NULL)) {
188         flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
189         freepattern(pat);
190         return(NULL);
191     }
192     return(pat);
193 }
194
195 static struct config *emptyconfig(void)
196 {
197     struct config *cf;
198     
199     omalloc(cf);
200     return(cf);
201 }
202
203 struct config *readconfig(char *file)
204 {
205     struct cfstate *s;
206     FILE *in;
207     struct config *cf;
208     struct child *child;
209     struct pattern *pat;
210     
211     if((in = fopen(file, "r")) == NULL) {
212         flog(LOG_WARNING, "%s: %s", file, strerror(errno));
213         return(NULL);
214     }
215     s = mkcfparser(in, file);
216     cf = emptyconfig();
217     
218     while(1) {
219         getcfline(s);
220         if((child = parsechild(s)) != NULL) {
221             child->next = cf->children;
222             cf->children = child;
223         } else if((pat = parsepattern(s)) != NULL) {
224             pat->next = cf->patterns;
225             cf->patterns = pat;
226         } else if(!strcmp(s->argv[0], "index-file")) {
227             freeca(cf->index);
228             cf->index = NULL;
229             if(s->argc > 1)
230                 cf->index = cadup(s->argv + 1);
231         } else if(!strcmp(s->argv[0], "eof")) {
232             break;
233         } else {
234             flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
235         }
236     }
237     
238     freecfparser(s);
239     fclose(in);
240     return(cf);
241 }
242
243 static void mergechildren(struct config *dst, struct config *src)
244 {
245     struct child *ch1, *ch2;
246     
247     for(ch1 = dst->children; ch1 != NULL; ch1 = ch1->next) {
248         for(ch2 = src->children; ch2 != NULL; ch2 = ch2->next) {
249             if(!strcmp(ch1->name, ch2->name)) {
250                 ch1->fd = ch2->fd;
251                 ch2->fd = -1;
252                 break;
253             }
254         }
255     }
256 }
257
258 struct config *getconfig(char *path)
259 {
260     struct config *cf, *ocf;
261     struct stat sb;
262     char *fn;
263     time_t mtime;
264     
265     fn = sprintf3("%s/.htrc", path);
266     for(cf = cflist; cf != NULL; cf = cf->next) {
267         if(!strcmp(cf->path, path)) {
268             if(now - cf->lastck > 5) {
269                 if(stat(fn, &sb) || (sb.st_mtime != cf->mtime))
270                     break;
271             }
272             cf->lastck = now;
273             return(cf);
274         }
275     }
276     ocf = cf;
277     if(access(fn, R_OK) || stat(fn, &sb)) {
278         cf = emptyconfig();
279         mtime = 0;
280     } else {
281         if((cf = readconfig(fn)) == NULL)
282             return(NULL);
283         mtime = sb.st_mtime;
284     }
285     if(ocf != NULL) {
286         mergechildren(cf, ocf);
287         freeconfig(ocf);
288     }
289     cf->path = sstrdup(path);
290     cf->mtime = mtime;
291     cf->lastck = now;
292     cf->next = cflist;
293     cf->prev = NULL;
294     if(cflist != NULL)
295         cflist->prev = cf;
296     cflist = cf;
297     return(cf);
298 }
299
300 struct config **getconfigs(char *file)
301 {
302     static struct config **ret = NULL;
303     struct {
304         struct config **b;
305         size_t s, d;
306     } buf;
307     struct config *cf;
308     char *tmp, *p;
309     
310     if(ret != NULL)
311         free(ret);
312     bufinit(buf);
313     tmp = sstrdup(file);
314     while(1) {
315         if((p = strrchr(tmp, '/')) == NULL)
316             break;
317         *p = 0;
318         if((cf = getconfig(tmp)) != NULL)
319             bufadd(buf, cf);
320     }
321     free(tmp);
322     if((cf = getconfig(".")) != NULL)
323         bufadd(buf, cf);
324     if(lconfig != NULL)
325         bufadd(buf, lconfig);
326     if(gconfig != NULL)
327         bufadd(buf, gconfig);
328     bufadd(buf, NULL);
329     return(ret = buf.b);
330 }
331
332 struct child *findchild(char *file, char *name, struct config **cf)
333 {
334     int i;
335     struct config **cfs;
336     struct child *ch;
337     
338     cfs = getconfigs(file);
339     for(i = 0; cfs[i] != NULL; i++) {
340         if((ch = getchild(cfs[i], name)) != NULL) {
341             if(cf != NULL)
342                 *cf = cfs[i];
343             return(ch);
344         }
345     }
346     return(NULL);
347 }
348
349 struct pattern *findmatch(char *file, int trydefault, int dir)
350 {
351     int i, o, c;
352     char *bn;
353     struct config **cfs;
354     struct pattern *pat;
355     struct rule *rule;
356     
357     if((bn = strrchr(file, '/')) != NULL)
358         bn++;
359     else
360         bn = file;
361     cfs = getconfigs(file);
362     for(c = 0; cfs[c] != NULL; c++) {
363         for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
364             if(!dir && (pat->type == PT_DIR))
365                 continue;
366             if(dir && (pat->type != PT_DIR))
367                 continue;
368             for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
369                 if(rule->type == PAT_BASENAME) {
370                     for(o = 0; rule->patterns[o] != NULL; o++) {
371                         if(!fnmatch(rule->patterns[o], bn, 0))
372                             break;
373                     }
374                     if(rule->patterns[o] == NULL)
375                         break;
376                 } else if(rule->type == PAT_PATHNAME) {
377                     for(o = 0; rule->patterns[o] != NULL; o++) {
378                         if(!fnmatch(rule->patterns[o], file, FNM_PATHNAME))
379                             break;
380                     }
381                     if(rule->patterns[o] == NULL)
382                         break;
383                 } else if(rule->type == PAT_ALL) {
384                 } else if(rule->type == PAT_DEFAULT) {
385                     if(!trydefault)
386                         break;
387                 }
388             }
389             if(!rule)
390                 return(pat);
391         }
392     }
393     if(!trydefault)
394         return(findmatch(file, 1, dir));
395     return(NULL);
396 }