X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fdirplex%2Fdirplex.c;h=ee7f650fb3d1ea1705dca2bebfbc919ace0bebcb;hb=HEAD;hp=f6002f3361d752173b06f41e75ce9a3e75840fa5;hpb=f2f009c95982947f3cd99567a17bbb65120fa741;p=ashd.git diff --git a/src/dirplex/dirplex.c b/src/dirplex/dirplex.c index f6002f3..ee7f650 100644 --- a/src/dirplex/dirplex.c +++ b/src/dirplex/dirplex.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -107,26 +108,51 @@ static void handle404(struct hthead *req, int fd, char *path) { struct child *ch; struct config *ccf; - char *tmp; + struct pattern *pat; - tmp = sstrdup(path); - ch = findchild(tmp, ".notfound", &ccf); - if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL)) - childerror(req, fd); - free(tmp); + 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); + } } static void handlefile(struct hthead *req, int fd, char *path) { struct pattern *pat; - if((pat = findmatch(path, 0, 0)) == NULL) { + 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; @@ -151,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)) { + free(fp); continue; - if(!S_ISREG(sb->st_mode)) + } + if(!checkaccess(path, dent->d_name)) { + free(fp); continue; - ret = sstrdup(fp); + } + ret = fp; break; } closedir(dir); @@ -194,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; } @@ -204,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 == '.') { - handle404(req, fd, sprintf3("%s/", path)); - 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)) { @@ -247,9 +279,11 @@ static int checkentry(struct hthead *req, int fd, char *path, char *rest, char * static int checkdir(struct hthead *req, int fd, char *path, char *rest) { - char *cpath; + 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, "."))) { @@ -268,10 +302,26 @@ static int checkdir(struct hthead *req, int fd, char *path, char *rest) 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) +static int checkpath(struct hthead *req, int fd, char *path, char *rest, int final) { char *p, *el; int rv; @@ -300,8 +350,7 @@ static int checkpath(struct hthead *req, int fd, char *path, char *rest) goto out; } if(strchr(el, '/') || (!*el && *rest)) { - handle404(req, fd, sprintf3("%s/", path)); - rv = 1; + rv = 0; goto out; } if(!*el) { @@ -310,9 +359,13 @@ static int checkpath(struct hthead *req, int fd, char *path, char *rest) 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); @@ -321,8 +374,7 @@ out: static void serve(struct hthead *req, int fd) { now = time(NULL); - if(!checkpath(req, fd, ".", req->rest)) - handle404(req, fd, "."); + checkpath(req, fd, ".", req->rest, 1); } static void chldhandler(int sig)