dirplex: Moved to its own directory and split off configuration code.
[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 struct config *getconfig(char *path)
244 {
245     struct config *cf;
246     struct stat sb;
247     char *fn;
248     time_t mtime;
249     
250     fn = sprintf3("%s/.htrc", path);
251     for(cf = cflist; cf != NULL; cf = cf->next) {
252         if(!strcmp(cf->path, path)) {
253             if(now - cf->lastck > 5) {
254                 if(stat(fn, &sb) || (sb.st_mtime != cf->mtime)) {
255                     freeconfig(cf);
256                     break;
257                 }
258             }
259             cf->lastck = now;
260             return(cf);
261         }
262     }
263     if(access(fn, R_OK) || stat(fn, &sb)) {
264         cf = emptyconfig();
265         mtime = 0;
266     } else {
267         if((cf = readconfig(fn)) == NULL)
268             return(NULL);
269         mtime = sb.st_mtime;
270     }
271     cf->path = sstrdup(path);
272     cf->mtime = mtime;
273     cf->lastck = now;
274     cf->next = cflist;
275     cf->prev = NULL;
276     if(cflist != NULL)
277         cflist->prev = cf;
278     cflist = cf;
279     return(cf);
280 }
281
282 struct config **getconfigs(char *file)
283 {
284     static struct config **ret = NULL;
285     struct {
286         struct config **b;
287         size_t s, d;
288     } buf;
289     struct config *cf;
290     char *tmp, *p;
291     
292     if(ret != NULL)
293         free(ret);
294     bufinit(buf);
295     tmp = sstrdup(file);
296     while(1) {
297         if((p = strrchr(tmp, '/')) == NULL)
298             break;
299         *p = 0;
300         if((cf = getconfig(tmp)) != NULL)
301             bufadd(buf, cf);
302     }
303     free(tmp);
304     if((cf = getconfig(".")) != NULL)
305         bufadd(buf, cf);
306     if(lconfig != NULL)
307         bufadd(buf, lconfig);
308     if(gconfig != NULL)
309         bufadd(buf, gconfig);
310     bufadd(buf, NULL);
311     return(ret = buf.b);
312 }
313
314 struct child *findchild(char *file, char *name)
315 {
316     int i;
317     struct config **cfs;
318     struct child *ch;
319     
320     cfs = getconfigs(file);
321     for(i = 0; cfs[i] != NULL; i++) {
322         if((ch = getchild(cfs[i], name)) != NULL)
323             break;
324     }
325     return(ch);
326 }
327
328 struct pattern *findmatch(char *file, int trydefault, int dir)
329 {
330     int i, o, c;
331     char *bn;
332     struct config **cfs;
333     struct pattern *pat;
334     struct rule *rule;
335     
336     if((bn = strrchr(file, '/')) != NULL)
337         bn++;
338     else
339         bn = file;
340     cfs = getconfigs(file);
341     for(c = 0; cfs[c] != NULL; c++) {
342         for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
343             if(!dir && (pat->type == PT_DIR))
344                 continue;
345             if(dir && (pat->type != PT_DIR))
346                 continue;
347             for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
348                 if(rule->type == PAT_BASENAME) {
349                     for(o = 0; rule->patterns[o] != NULL; o++) {
350                         if(!fnmatch(rule->patterns[o], bn, 0))
351                             break;
352                     }
353                     if(rule->patterns[o] == NULL)
354                         break;
355                 } else if(rule->type == PAT_PATHNAME) {
356                     for(o = 0; rule->patterns[o] != NULL; o++) {
357                         if(!fnmatch(rule->patterns[o], file, FNM_PATHNAME))
358                             break;
359                     }
360                     if(rule->patterns[o] == NULL)
361                         break;
362                 } else if(rule->type == PAT_ALL) {
363                 } else if(rule->type == PAT_DEFAULT) {
364                     if(!trydefault)
365                         break;
366                 }
367             }
368             if(!rule)
369                 return(pat);
370         }
371     }
372     if(!trydefault)
373         return(findmatch(file, 1, dir));
374     return(NULL);
375 }