2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
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.
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.
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/>.
36 static struct config *cflist;
37 struct config *gconfig, *lconfig;
39 static void freerule(struct rule *rule)
41 freeca(rule->patterns);
45 static void freepattern(struct pattern *pat)
49 for(rule = pat->rules; *rule; rule++)
51 if(pat->childnm != NULL)
57 static void freeconfig(struct config *cf)
59 struct child *ch, *nch;
60 struct pattern *pat, *npat;
63 cf->prev->next = cf->next;
65 cf->next->prev = cf->prev;
70 for(ch = cf->children; ch != NULL; ch = nch) {
74 for(pat = cf->patterns; pat != NULL; pat = npat) {
79 if(cf->capture != NULL)
84 struct child *getchild(struct config *cf, char *name)
88 for(ch = cf->children; ch; ch = ch->next) {
89 if(!strcmp(ch->name, name))
95 static struct rule *newrule(struct pattern *pat)
100 for(i = 0; pat->rules[i]; i++);
101 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
102 rule = pat->rules[i] = szmalloc(sizeof(*rule));
103 pat->rules[i + 1] = NULL;
107 static struct pattern *newpattern(void)
112 pat->rules = szmalloc(sizeof(*pat->rules));
116 static char **cadup(char **w)
122 ret = smalloc(sizeof(*ret) * (l + 1));
123 for(i = 0; i < l; i++)
124 ret[i] = sstrdup(w[i]);
129 static struct pattern *parsepattern(struct cfstate *s)
135 if(!strcmp(s->argv[0], "match")) {
142 if((s->argc > 1) && !strcmp(s->argv[1], "directory"))
147 if(!strcmp(s->argv[0], "filename")) {
149 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno);
153 rule->type = PAT_BASENAME;
154 rule->patterns = cadup(s->argv + 1);
155 } else if(!strcmp(s->argv[0], "pathname")) {
157 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno);
161 rule->type = PAT_PATHNAME;
162 rule->patterns = cadup(s->argv + 1);
163 } else if(!strcmp(s->argv[0], "all")) {
164 newrule(pat)->type = PAT_ALL;
165 } else if(!strcmp(s->argv[0], "default")) {
166 newrule(pat)->type = PAT_DEFAULT;
167 } else if(!strcmp(s->argv[0], "local")) {
168 newrule(pat)->type = PAT_LOCAL;
169 } else if(!strcmp(s->argv[0], "handler")) {
171 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
174 if(pat->childnm != NULL)
176 pat->childnm = sstrdup(s->argv[1]);
177 } else if(!strcmp(s->argv[0], "fork")) {
178 pat->fchild = cadup(s->argv + 1);
179 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
182 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
186 if(pat->rules[0] == NULL) {
187 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
191 if((pat->childnm == NULL) && (pat->fchild == NULL)) {
192 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
199 static struct config *emptyconfig(void)
207 struct config *readconfig(char *file)
215 if((in = fopen(file, "r")) == NULL) {
216 flog(LOG_WARNING, "%s: %s", file, strerror(errno));
219 s = mkcfparser(in, file);
224 if((child = parsechild(s)) != NULL) {
225 child->next = cf->children;
226 cf->children = child;
227 } else if((pat = parsepattern(s)) != NULL) {
228 pat->next = cf->patterns;
230 } else if(!strcmp(s->argv[0], "index-file")) {
234 cf->index = cadup(s->argv + 1);
235 } else if(!strcmp(s->argv[0], "capture")) {
237 flog(LOG_WARNING, "%s:%i: missing argument to capture declaration", s->file, s->lno);
240 if(cf->capture != NULL)
242 cf->capture = sstrdup(s->argv[1]);
243 } else if(!strcmp(s->argv[0], "eof")) {
246 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
255 static void mergechildren(struct config *dst, struct config *src)
257 struct child *ch1, *ch2;
259 for(ch1 = dst->children; ch1 != NULL; ch1 = ch1->next) {
260 for(ch2 = src->children; ch2 != NULL; ch2 = ch2->next) {
261 if(!strcmp(ch1->name, ch2->name)) {
270 struct config *getconfig(char *path)
272 struct config *cf, *ocf;
277 fn = sprintf3("%s/.htrc", path);
278 for(cf = cflist; cf != NULL; cf = cf->next) {
279 if(!strcmp(cf->path, path)) {
280 if(now - cf->lastck > 5) {
281 if(stat(fn, &sb) || (sb.st_mtime != cf->mtime))
289 if(access(fn, R_OK) || stat(fn, &sb)) {
293 if((cf = readconfig(fn)) == NULL)
298 mergechildren(cf, ocf);
301 cf->path = sstrdup(path);
312 struct config **getconfigs(char *file)
314 static struct config **ret = NULL;
327 if((p = strrchr(tmp, '/')) == NULL)
330 if((cf = getconfig(tmp)) != NULL)
334 if((cf = getconfig(".")) != NULL)
337 bufadd(buf, lconfig);
339 bufadd(buf, gconfig);
344 struct child *findchild(char *file, char *name, struct config **cf)
350 cfs = getconfigs(file);
351 for(i = 0; cfs[i] != NULL; i++) {
352 if((ch = getchild(cfs[i], name)) != NULL) {
361 struct pattern *findmatch(char *file, int trydefault, int dir)
370 if((bn = strrchr(file, '/')) != NULL)
374 cfs = getconfigs(file);
375 for(c = 0; cfs[c] != NULL; c++) {
376 for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
377 if(!dir && (pat->type == PT_DIR))
379 if(dir && (pat->type != PT_DIR))
381 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
382 if(rule->type == PAT_BASENAME) {
383 for(o = 0; rule->patterns[o] != NULL; o++) {
384 if(!fnmatch(rule->patterns[o], bn, 0))
387 if(rule->patterns[o] == NULL)
389 } else if(rule->type == PAT_PATHNAME) {
390 for(o = 0; rule->patterns[o] != NULL; o++) {
391 if(!fnmatch(rule->patterns[o], file, FNM_PATHNAME))
394 if(rule->patterns[o] == NULL)
396 } else if(rule->type == PAT_ALL) {
397 } else if(rule->type == PAT_DEFAULT) {
400 } else if(rule->type == PAT_LOCAL) {
401 if(cfs[c]->path == NULL)
403 pl = strlen(cfs[c]->path);
404 if(!((strlen(file) > pl) && !strncmp(file, cfs[c]->path, pl) && (file[pl] == '/') && !strchr(file + pl + 1, '/')))
413 return(findmatch(file, 1, dir));