X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fdirplex%2Fdirplex.c;h=ee7f650fb3d1ea1705dca2bebfbc919ace0bebcb;hb=HEAD;hp=c31c6cda7a62c175a87b1eb339db28148068edef;hpb=6a7a868e28e79f5502cc4b37578c263f640439c8;p=ashd.git diff --git a/src/dirplex/dirplex.c b/src/dirplex/dirplex.c index c31c6cd..ee7f650 100644 --- a/src/dirplex/dirplex.c +++ b/src/dirplex/dirplex.c @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include #ifdef HAVE_CONFIG_H @@ -41,21 +43,81 @@ time_t now; +static void chinit(void *idata) +{ + char *twd = idata; + + if(twd != NULL) { + /* This should never be able to fail other than for critical + * I/O errors or some such, since the path has already been + * traversed. */ + if(chdir(twd)) + exit(127); + } +} + +static void childerror(struct hthead *req, int fd) +{ + if(errno == EAGAIN) + simpleerror(fd, 500, "Server Error", "The request handler is overloaded."); + else + simpleerror(fd, 500, "Server Error", "The request handler crashed."); +} + static void handle(struct hthead *req, int fd, char *path, struct pattern *pat) { struct child *ch; + struct config *ccf; + struct headmod *head; + char *twd; - headappheader(req, "X-Ash-File", path); + for(head = pat->headers; head != NULL; head = head->next) { + headrmheader(req, head->name); + headappheader(req, head->name, head->value); + } + if(!strncmp(path, "./", 2) && path[2]) + path += 2; if(pat->fchild) { + headappheader(req, "X-Ash-File", path); stdforkserve(pat->fchild, req, fd, NULL, NULL); } else { - if((ch = findchild(path, pat->childnm)) == NULL) { + if((ch = findchild(path, pat->childnm, &ccf)) == NULL) { flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm); simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm); return; } - if(childhandle(ch, req, fd, NULL, NULL)) - simpleerror(fd, 500, "Server Error", "The request handler crashed."); + if((twd = ccf?ccf->path:NULL) != NULL) { + if(!strcmp(twd, ".")) { + twd = NULL; + } else if(strncmp(path, twd, strlen(twd)) || (path[strlen(twd)] != '/')) { + /* Should be an impossible case under the current (and + * foreseeable) scheme. */ + simpleerror(fd, 500, "Server Error", "An internal server error occurred."); + return; + } else { + path = path + strlen(twd) + 1; + } + } + headappheader(req, "X-Ash-File", path); + if(childhandle(ch, req, fd, chinit, twd)) + childerror(req, fd); + } +} + +static void handle404(struct hthead *req, int fd, char *path) +{ + struct child *ch; + struct config *ccf; + struct pattern *pat; + + char tmp[strlen(path) + 1]; + strcpy(tmp, path); + if((pat = findmatch(tmp, 0, PT_NOTFOUND)) != NULL) { + handle(req, fd, tmp, pat); + } else { + ch = findchild(tmp, ".notfound", &ccf); + if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL)) + childerror(req, fd); } } @@ -63,13 +125,34 @@ static void handlefile(struct hthead *req, int fd, char *path) { struct pattern *pat; - if((pat = findmatch(path, 0, 0)) == NULL) { - simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource."); + if((pat = findmatch(path, 0, PT_FILE)) == NULL) { + handle404(req, fd, path); return; } handle(req, fd, path, pat); } +static int checkaccess(char *path, char *name) +{ + int i, o; + struct config **cfs; + + if(*name == '.') { + cfs = getconfigs(sprintf3("%s/", path)); + for(i = 0; cfs[i] != NULL; i++) { + if(cfs[i]->dotallow != NULL) { + for(o = 0; cfs[i]->dotallow[o] != NULL; o++) { + if(!fnmatch(cfs[i]->dotallow[o], name, 0)) + return(1); + } + break; + } + } + return(0); + } + return(1); +} + static char *findfile(char *path, char *name, struct stat *sb) { DIR *dir; @@ -94,12 +177,20 @@ static char *findfile(char *path, char *name, struct stat *sb) continue; if(strncmp(dent->d_name, name, strlen(name))) continue; - fp = sprintf3("%s/%s", path, dent->d_name); - if(stat(fp, sb)) + fp = sprintf2("%s/%s", path, dent->d_name); + if(stat(fp, sb)) { + free(fp); continue; - if(!S_ISREG(sb->st_mode)) + } + if(!S_ISREG(sb->st_mode)) { + free(fp); + continue; + } + if(!checkaccess(path, dent->d_name)) { + free(fp); continue; - ret = sstrdup(fp); + } + ret = fp; break; } closedir(dir); @@ -137,7 +228,7 @@ static void handledir(struct hthead *req, int fd, char *path) break; } } - if((pat = findmatch(cpath, 0, 1)) != NULL) { + if((pat = findmatch(cpath, 0, PT_DIR)) != NULL) { handle(req, fd, cpath, pat); goto out; } @@ -147,26 +238,24 @@ out: free(cpath); } -static int checkpath(struct hthead *req, int fd, char *path, char *rest); +static int checkpath(struct hthead *req, int fd, char *path, char *rest, int final); -static int checkentry(struct hthead *req, int fd, char *path, char *rest, char *el) +static int checkentry(struct hthead *req, int fd, char *path, char *rest, char *el, int final) { struct stat sb; char *newpath; int rv; - if(!el == '.') { - simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource."); - return(1); - } if(!stat(sprintf3("%s/%s", path, el), &sb)) { + if(!checkaccess(path, el)) + return(0); if(S_ISDIR(sb.st_mode)) { if(!*rest) { stdredir(req, fd, 301, sprintf3("%s/", el)); return(1); } newpath = sprintf2("%s/%s", path, el); - rv = checkpath(req, fd, newpath, rest + 1); + rv = checkpath(req, fd, newpath, rest + 1, final); free(newpath); return(rv); } else if(S_ISREG(sb.st_mode)) { @@ -176,7 +265,7 @@ static int checkentry(struct hthead *req, int fd, char *path, char *rest, char * free(newpath); return(1); } - simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource."); + handle404(req, fd, sprintf3("%s/", path)); return(1); } if(!strchr(el, '.') && ((newpath = findfile(path, el, NULL)) != NULL)) { @@ -188,9 +277,52 @@ static int checkentry(struct hthead *req, int fd, char *path, char *rest, char * return(0); } -static int checkpath(struct hthead *req, int fd, char *path, char *rest) +static int checkdir(struct hthead *req, int fd, char *path, char *rest) +{ + char *cpath, *newpath; + struct config *cf, *ccf; + struct child *ch; + struct stat sb; + int rv; + + cf = getconfig(path); + if((cf->capture != NULL) && (cf->caproot || !cf->path || strcmp(cf->path, "."))) { + cpath = sprintf2("%s/", path); + if((ch = findchild(cpath, cf->capture, &ccf)) == NULL) { + free(cpath); + flog(LOG_ERR, "child %s requested for capture, but was not declared", cf->capture); + simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", cf->capture); + return(1); + } + free(cpath); + if(*rest == '/') + rest++; + replrest(req, rest); + if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL)) + childerror(req, fd); + return(1); + } + if(cf->reparse != NULL) { + newpath = (cf->reparse[0] == '/')?sstrdup(cf->reparse):sprintf2("%s/%s", path, cf->reparse); + rv = stat(newpath, &sb); + if(!rv && S_ISDIR(sb.st_mode)) { + rv = checkpath(req, fd, newpath, rest, !cf->parsecomb); + } else if(!rv && S_ISREG(sb.st_mode)) { + replrest(req, rest); + handlefile(req, fd, newpath); + rv = 1; + } else { + rv = !cf->parsecomb; + } + free(newpath); + if(rv) + return(rv); + } + return(0); +} + +static int checkpath(struct hthead *req, int fd, char *path, char *rest, int final) { - struct config *cf; char *p, *el; int rv; @@ -199,7 +331,8 @@ static int checkpath(struct hthead *req, int fd, char *path, char *rest) if(!strncmp(path, "./", 2)) path += 2; - cf = getconfig(path); + if(checkdir(req, fd, path, rest)) + return(1); if((p = strchr(rest, '/')) == NULL) { el = unquoteurl(rest); @@ -217,18 +350,22 @@ static int checkpath(struct hthead *req, int fd, char *path, char *rest) goto out; } if(strchr(el, '/') || (!*el && *rest)) { - simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource."); - rv = 1; + rv = 0; goto out; } if(!*el) { replrest(req, rest); handledir(req, fd, path); - return(1); + rv = 1; + goto out; } - rv = checkentry(req, fd, path, rest, el); + rv = checkentry(req, fd, path, rest, el, final); out: + if(final && !rv) { + handle404(req, fd, sprintf3("%s/", path)); + rv = 1; + } if(el != NULL) free(el); return(rv); @@ -237,8 +374,22 @@ out: static void serve(struct hthead *req, int fd) { now = time(NULL); - if(!checkpath(req, fd, ".", req->rest)) - simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource."); + checkpath(req, fd, ".", req->rest, 1); +} + +static void chldhandler(int sig) +{ + pid_t pid; + int st; + + while((pid = waitpid(-1, &st, WNOHANG)) > 0) { + if(WCOREDUMP(st)) + flog(LOG_WARNING, "child process %i dumped core", pid); + } +} + +static void sighandler(int sig) +{ } static void usage(FILE *out) @@ -300,7 +451,8 @@ int main(int argc, char **argv) flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno)); exit(1); } - signal(SIGCHLD, SIG_IGN); + signal(SIGCHLD, chldhandler); + signal(SIGPIPE, sighandler); while(1) { if((fd = recvreq(0, &req)) < 0) { if(errno != 0)