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