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/>.
41 #define PAT_BASENAME 0
42 #define PAT_PATHNAME 1
50 struct config *next, *prev;
53 struct child *children;
54 struct pattern *patterns;
71 static struct config *cflist;
72 static struct config *gconfig, *lconfig;
75 static void freerule(struct rule *rule)
77 freeca(rule->patterns);
81 static void freepattern(struct pattern *pat)
85 for(rule = pat->rules; *rule; rule++)
87 if(pat->childnm != NULL)
93 static void freeconfig(struct config *cf)
95 struct child *ch, *nch;
96 struct pattern *pat, *npat;
99 cf->prev->next = cf->next;
101 cf->next->prev = cf->prev;
106 for(ch = cf->children; ch != NULL; ch = nch) {
110 for(pat = cf->patterns; pat != NULL; pat = npat) {
118 static struct child *getchild(struct config *cf, char *name)
122 for(ch = cf->children; ch; ch = ch->next) {
123 if(!strcmp(ch->name, name))
129 static struct rule *newrule(struct pattern *pat)
134 for(i = 0; pat->rules[i]; i++);
135 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
136 rule = pat->rules[i] = szmalloc(sizeof(*rule));
137 pat->rules[i + 1] = NULL;
141 static struct pattern *newpattern(void)
146 pat->rules = szmalloc(sizeof(*pat->rules));
150 static char **cadup(char **w)
156 ret = smalloc(sizeof(*ret) * (l + 1));
157 for(i = 0; i < l; i++)
158 ret[i] = sstrdup(w[i]);
163 static struct pattern *parsepattern(struct cfstate *s)
169 if(!strcmp(s->argv[0], "match")) {
176 if((s->argc > 1) && !strcmp(s->argv[1], "directory"))
181 if(!strcmp(s->argv[0], "filename")) {
183 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno);
187 rule->type = PAT_BASENAME;
188 rule->patterns = cadup(s->argv + 1);
189 } else if(!strcmp(s->argv[0], "pathname")) {
191 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno);
195 rule->type = PAT_PATHNAME;
196 rule->patterns = cadup(s->argv + 1);
197 } else if(!strcmp(s->argv[0], "all")) {
198 newrule(pat)->type = PAT_ALL;
199 } else if(!strcmp(s->argv[0], "default")) {
200 newrule(pat)->type = PAT_DEFAULT;
201 } else if(!strcmp(s->argv[0], "handler")) {
203 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
206 if(pat->childnm != NULL)
208 pat->childnm = sstrdup(s->argv[1]);
209 } else if(!strcmp(s->argv[0], "fork")) {
210 pat->fchild = cadup(s->argv + 1);
211 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
214 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
218 if(pat->rules[0] == NULL) {
219 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
223 if((pat->childnm == NULL) && (pat->fchild == NULL)) {
224 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
231 static struct config *emptyconfig(void)
239 static struct config *readconfig(char *file)
247 if((in = fopen(file, "r")) == NULL) {
248 flog(LOG_WARNING, "%s: %s", file, strerror(errno));
251 s = mkcfparser(in, file);
256 if((child = parsechild(s)) != NULL) {
257 child->next = cf->children;
258 cf->children = child;
259 } else if((pat = parsepattern(s)) != NULL) {
260 pat->next = cf->patterns;
262 } else if(!strcmp(s->argv[0], "index-file")) {
266 cf->index = cadup(s->argv + 1);
267 } else if(!strcmp(s->argv[0], "eof")) {
270 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
279 static struct config *getconfig(char *path)
286 fn = sprintf3("%s/.htrc", path);
287 for(cf = cflist; cf != NULL; cf = cf->next) {
288 if(!strcmp(cf->path, path)) {
289 if(now - cf->lastck > 5) {
290 if(stat(fn, &sb) || (sb.st_mtime != cf->mtime)) {
299 if(access(fn, R_OK) || stat(fn, &sb)) {
303 if((cf = readconfig(fn)) == NULL)
307 cf->path = sstrdup(path);
318 static struct config **getconfigs(char *file)
320 static struct config **ret = NULL;
333 if((p = strrchr(tmp, '/')) == NULL)
336 if((cf = getconfig(tmp)) != NULL)
340 if((cf = getconfig(".")) != NULL)
343 bufadd(buf, lconfig);
345 bufadd(buf, gconfig);
350 static struct child *findchild(char *file, char *name)
356 cfs = getconfigs(file);
357 for(i = 0; cfs[i] != NULL; i++) {
358 if((ch = getchild(cfs[i], name)) != NULL)
364 static struct pattern *findmatch(char *file, int trydefault, int dir)
372 if((bn = strrchr(file, '/')) != NULL)
376 cfs = getconfigs(file);
377 for(c = 0; cfs[c] != NULL; c++) {
378 for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
379 if(!dir && (pat->type == PT_DIR))
381 if(dir && (pat->type != PT_DIR))
383 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
384 if(rule->type == PAT_BASENAME) {
385 for(o = 0; rule->patterns[o] != NULL; o++) {
386 if(!fnmatch(rule->patterns[o], bn, 0))
389 if(rule->patterns[o] == NULL)
391 } else if(rule->type == PAT_PATHNAME) {
392 for(o = 0; rule->patterns[o] != NULL; o++) {
393 if(!fnmatch(rule->patterns[o], file, FNM_PATHNAME))
396 if(rule->patterns[o] == NULL)
398 } else if(rule->type == PAT_ALL) {
399 } else if(rule->type == PAT_DEFAULT) {
409 return(findmatch(file, 1, dir));
413 static void handle(struct hthead *req, int fd, char *path, struct pattern *pat)
417 headappheader(req, "X-Ash-File", path);
419 stdforkserve(pat->fchild, req, fd);
421 if((ch = findchild(path, pat->childnm)) == NULL) {
422 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
423 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
426 if(childhandle(ch, req, fd))
427 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
431 static void handlefile(struct hthead *req, int fd, char *path)
435 if((pat = findmatch(path, 0, 0)) == NULL) {
436 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
439 handle(req, fd, path, pat);
442 static char *findfile(char *path, char *name, struct stat *sb)
451 if((dir = opendir(path)) == NULL)
454 while((dent = readdir(dir)) != NULL) {
455 /* Ignore backup files.
456 * XXX: There is probably a better and more extensible way to
458 if(dent->d_name[strlen(dent->d_name) - 1] == '~')
460 if((p = strchr(dent->d_name, '.')) == NULL)
462 if(p - dent->d_name != strlen(name))
464 if(strncmp(dent->d_name, name, strlen(name)))
466 fp = sprintf3("%s/%s", path, dent->d_name);
469 if(!S_ISREG(sb->st_mode))
478 static void handledir(struct hthead *req, int fd, char *path)
483 char *inm, *ipath, *cpath;
486 cpath = sprintf2("%s/", path);
487 cfs = getconfigs(cpath);
488 for(i = 0; cfs[i] != NULL; i++) {
489 if(cfs[i]->index != NULL) {
490 for(o = 0; cfs[i]->index[o] != NULL; o++) {
491 inm = cfs[i]->index[o];
492 ipath = sprintf2("%s/%s", path, inm);
493 if(!stat(ipath, &sb) && S_ISREG(sb.st_mode)) {
494 handlefile(req, fd, ipath);
500 if(!strchr(inm, '.') && ((ipath = findfile(path, inm, NULL)) != NULL)) {
501 handlefile(req, fd, ipath);
509 if((pat = findmatch(cpath, 0, 1)) != NULL) {
510 handle(req, fd, cpath, pat);
513 simpleerror(fd, 403, "Not Authorized", "Will not send listings for this directory.");
519 static int checkpath(struct hthead *req, int fd, char *path, char *rest);
521 static int checkentry(struct hthead *req, int fd, char *path, char *rest, char *el)
528 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
531 if(!stat(sprintf3("%s/%s", path, el), &sb)) {
532 if(S_ISDIR(sb.st_mode)) {
534 stdredir(req, fd, 301, sprintf3("%s/", el));
537 newpath = sprintf2("%s/%s", path, el);
538 rv = checkpath(req, fd, newpath, rest + 1);
541 } else if(S_ISREG(sb.st_mode)) {
542 newpath = sprintf2("%s/%s", path, el);
544 handlefile(req, fd, newpath);
548 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
551 if(!strchr(el, '.') && ((newpath = findfile(path, el, NULL)) != NULL)) {
553 handlefile(req, fd, newpath);
560 static int checkpath(struct hthead *req, int fd, char *path, char *rest)
569 if(!strncmp(path, "./", 2))
571 cf = getconfig(path);
573 if((p = strchr(rest, '/')) == NULL) {
574 el = unquoteurl(rest);
577 char buf[p - rest + 1];
578 memcpy(buf, rest, p - rest);
580 el = unquoteurl(buf);
584 simpleerror(fd, 400, "Bad Request", "The requested URL contains an invalid escape sequence.");
588 if(strchr(el, '/') || (!*el && *rest)) {
589 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
595 handledir(req, fd, path);
598 rv = checkentry(req, fd, path, rest, el);
606 static void serve(struct hthead *req, int fd)
609 if(!checkpath(req, fd, ".", req->rest))
610 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
613 static void usage(FILE *out)
615 fprintf(out, "usage: dirplex [-hN] [-c CONFIG] DIR\n");
618 int main(int argc, char **argv)
622 char *gcf, *lcf, *clcf;
628 while((c = getopt(argc, argv, "hNc:")) >= 0) {
644 if(argc - optind < 1) {
649 if((gcf = findstdconf("ashd/dirplex.rc")) != NULL) {
650 gconfig = readconfig(gcf);
655 if(strchr(lcf, '/') == NULL) {
656 if((clcf = findstdconf(sprintf3("ashd/%s", lcf))) == NULL) {
657 flog(LOG_ERR, "could not find requested configuration `%s'", lcf);
660 if((lconfig = readconfig(clcf)) == NULL)
664 if((lconfig = readconfig(lcf)) == NULL)
668 if(chdir(argv[optind])) {
669 flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno));
672 signal(SIGCHLD, SIG_IGN);
674 if((fd = recvreq(0, &req)) < 0) {
676 flog(LOG_ERR, "recvreq: %s", strerror(errno));