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 void handledir(struct hthead *req, int fd, char *path)
447 char *inm, *ipath, *p, *cpath;
452 cpath = sprintf2("%s/", path);
453 cfs = getconfigs(cpath);
454 for(i = 0; cfs[i] != NULL; i++) {
455 if(cfs[i]->index != NULL) {
456 for(o = 0; cfs[i]->index[o] != NULL; o++) {
457 inm = cfs[i]->index[o];
458 ipath = sprintf2("%s/%s", path, inm);
459 if(!stat(ipath, &sb) && S_ISREG(sb.st_mode)) {
460 handlefile(req, fd, ipath);
467 if(!strchr(inm, '.') && ((dir = opendir(path)) != NULL)) {
468 while((dent = readdir(dir)) != NULL) {
469 /* Ignore backup files.
470 * XXX: There is probably a better and more
471 * extensible way to do this. */
472 if(dent->d_name[strlen(dent->d_name) - 1] == '~')
474 if((p = strchr(dent->d_name, '.')) == NULL)
476 if(strncmp(dent->d_name, inm, strlen(inm)))
478 ipath = sprintf2("%s/%s", path, dent->d_name);
479 if(stat(ipath, &sb) || !S_ISREG(sb.st_mode)) {
489 handlefile(req, fd, ipath);
497 if((pat = findmatch(cpath, 0, 1)) != NULL) {
498 handle(req, fd, cpath, pat);
501 simpleerror(fd, 403, "Not Authorized", "Will not send listings for this directory.");
507 static int checkdir(struct hthead *req, int fd, char *path)
512 static void serve(struct hthead *req, int fd)
514 char *p, *p2, *path, *tmp, *buf, *p3, *nm;
524 if((p2 = strchr(p, '/')) == NULL) {
528 if((tmp = unquoteurl(p)) == NULL) {
529 simpleerror(fd, 400, "Bad Request", "The requested URL contains an invalid escape sequence.");
537 if(stat(path, &sb)) {
538 flog(LOG_WARNING, "failed to stat previously stated directory %s: %s", path, strerror(errno));
539 simpleerror(fd, 500, "Internal Server Error", "The server encountered an unexpected condition.");
544 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
549 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
556 * First, check the name verbatimely:
558 buf = sprintf3("%s/%s", path, p);
559 if(!stat(buf, &sb)) {
560 if(S_ISDIR(sb.st_mode)) {
562 if(!strcmp(path, "."))
565 path = sprintf2("%s/%s", path, p);
568 stdredir(req, fd, 301, sprintf3("%s/", p));
571 if(checkdir(req, fd, path))
575 if(S_ISREG(sb.st_mode)) {
577 path = sprintf2("%s/%s", path, p);
581 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
586 * Check the file extensionlessly:
588 if(!strchr(p, '.') && ((dir = opendir(path)) != NULL)) {
589 while((dent = readdir(dir)) != NULL) {
590 buf = sprintf3("%s/%s", path, dent->d_name);
591 /* Ignore backup files.
592 * XXX: There is probably a better and more
593 * extensible way to do this. */
594 if(dent->d_name[strlen(dent->d_name) - 1] == '~')
596 if((p3 = strchr(dent->d_name, '.')) != NULL)
598 if(strcmp(dent->d_name, p))
602 if(!S_ISREG(sb.st_mode))
614 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
626 if(!strncmp(path, "./", 2))
627 memmove(path, path + 2, strlen(path + 2) + 1);
628 if(S_ISDIR(sb.st_mode)) {
629 handledir(req, fd, path);
630 } else if(S_ISREG(sb.st_mode)) {
631 handlefile(req, fd, path);
633 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
639 /* No special handling, for now at least. */
644 static void usage(FILE *out)
646 fprintf(out, "usage: dirplex [-hN] [-c CONFIG] DIR\n");
649 int main(int argc, char **argv)
653 char *gcf, *lcf, *clcf;
659 while((c = getopt(argc, argv, "hNc:")) >= 0) {
675 if(argc - optind < 1) {
680 if((gcf = findstdconf("ashd/dirplex.rc")) != NULL) {
681 gconfig = readconfig(gcf);
686 if(strchr(lcf, '/') == NULL) {
687 if((clcf = findstdconf(sprintf3("ashd/%s", lcf))) == NULL) {
688 flog(LOG_ERR, "could not find requested configuration `%s'", lcf);
691 if((lconfig = readconfig(clcf)) == NULL)
695 if((lconfig = readconfig(lcf)) == NULL)
699 if(chdir(argv[optind])) {
700 flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno));
703 signal(SIGCHLD, SIG_IGN);
705 if((fd = recvreq(0, &req)) < 0) {
707 flog(LOG_ERR, "recvreq: %s", strerror(errno));