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) {
82 struct child *getchild(struct config *cf, char *name)
86 for(ch = cf->children; ch; ch = ch->next) {
87 if(!strcmp(ch->name, name))
93 static struct rule *newrule(struct pattern *pat)
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;
105 static struct pattern *newpattern(void)
110 pat->rules = szmalloc(sizeof(*pat->rules));
114 static char **cadup(char **w)
120 ret = smalloc(sizeof(*ret) * (l + 1));
121 for(i = 0; i < l; i++)
122 ret[i] = sstrdup(w[i]);
127 static struct pattern *parsepattern(struct cfstate *s)
133 if(!strcmp(s->argv[0], "match")) {
140 if((s->argc > 1) && !strcmp(s->argv[1], "directory"))
145 if(!strcmp(s->argv[0], "filename")) {
147 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno);
151 rule->type = PAT_BASENAME;
152 rule->patterns = cadup(s->argv + 1);
153 } else if(!strcmp(s->argv[0], "pathname")) {
155 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno);
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")) {
167 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
170 if(pat->childnm != NULL)
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")) {
178 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
182 if(pat->rules[0] == NULL) {
183 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
187 if((pat->childnm == NULL) && (pat->fchild == NULL)) {
188 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
195 static struct config *emptyconfig(void)
203 struct config *readconfig(char *file)
211 if((in = fopen(file, "r")) == NULL) {
212 flog(LOG_WARNING, "%s: %s", file, strerror(errno));
215 s = mkcfparser(in, file);
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;
226 } else if(!strcmp(s->argv[0], "index-file")) {
230 cf->index = cadup(s->argv + 1);
231 } else if(!strcmp(s->argv[0], "eof")) {
234 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
243 struct config *getconfig(char *path)
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)) {
263 if(access(fn, R_OK) || stat(fn, &sb)) {
267 if((cf = readconfig(fn)) == NULL)
271 cf->path = sstrdup(path);
282 struct config **getconfigs(char *file)
284 static struct config **ret = NULL;
297 if((p = strrchr(tmp, '/')) == NULL)
300 if((cf = getconfig(tmp)) != NULL)
304 if((cf = getconfig(".")) != NULL)
307 bufadd(buf, lconfig);
309 bufadd(buf, gconfig);
314 struct child *findchild(char *file, char *name)
320 cfs = getconfigs(file);
321 for(i = 0; cfs[i] != NULL; i++) {
322 if((ch = getchild(cfs[i], name)) != NULL)
328 struct pattern *findmatch(char *file, int trydefault, int dir)
336 if((bn = strrchr(file, '/')) != NULL)
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))
345 if(dir && (pat->type != PT_DIR))
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))
353 if(rule->patterns[o] == NULL)
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))
360 if(rule->patterns[o] == NULL)
362 } else if(rule->type == PAT_ALL) {
363 } else if(rule->type == PAT_DEFAULT) {
373 return(findmatch(file, 1, dir));