X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fdirplex.c;h=01d87157319005e62bf3a75682995eb2ba4e9b80;hb=6373daf9d9fdd9e24d2c657abf0625337f9b3a24;hp=7216d9523019f7fb850c6d482a36fccbbb2e543f;hpb=624f637dd0eb621353222dac371ab9672ffe62b1;p=ashd.git diff --git a/src/dirplex.c b/src/dirplex.c index 7216d95..01d8715 100644 --- a/src/dirplex.c +++ b/src/dirplex.c @@ -49,11 +49,12 @@ struct config { time_t mtime, lastck; struct child *children; struct pattern *patterns; + char **index; }; struct rule { int type; - char *pattern; + char **patterns; }; struct pattern { @@ -67,15 +68,18 @@ static struct config *cflist; static struct config *gconfig, *lconfig; static time_t now; +static void freerule(struct rule *rule) +{ + freeca(rule->patterns); + free(rule); +} + static void freepattern(struct pattern *pat) { struct rule **rule; - for(rule = pat->rules; *rule; rule++) { - if((*rule)->pattern != NULL) - free((*rule)->pattern); - free(*rule); - } + for(rule = pat->rules; *rule; rule++) + freerule(*rule); if(pat->childnm != NULL) free(pat->childnm); freeca(pat->fchild); @@ -103,6 +107,7 @@ static void freeconfig(struct config *cf) npat = pat->next; freepattern(pat); } + freeca(cf->index); free(cf); } @@ -138,11 +143,24 @@ static struct pattern *newpattern(void) return(pat); } +static char **cadup(char **w) +{ + char **ret; + int i, l; + + l = calen(w); + ret = smalloc(sizeof(*ret) * (l + 1)); + for(i = 0; i < l; i++) + ret[i] = sstrdup(w[i]); + ret[i] = NULL; + return(ret); +} + static struct pattern *parsepattern(struct cfstate *s) { struct pattern *pat; struct rule *rule; - int sl, i; + int sl; if(!strcmp(s->argv[0], "match")) { s->expstart = 1; @@ -161,7 +179,7 @@ static struct pattern *parsepattern(struct cfstate *s) } rule = newrule(pat); rule->type = PAT_BASENAME; - rule->pattern = sstrdup(s->argv[1]); + rule->patterns = cadup(s->argv + 1); } else if(!strcmp(s->argv[0], "pathname")) { if(s->argc < 2) { flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno); @@ -169,7 +187,7 @@ static struct pattern *parsepattern(struct cfstate *s) } rule = newrule(pat); rule->type = PAT_PATHNAME; - rule->pattern = sstrdup(s->argv[1]); + rule->patterns = cadup(s->argv + 1); } else if(!strcmp(s->argv[0], "all")) { newrule(pat)->type = PAT_ALL; } else if(!strcmp(s->argv[0], "default")) { @@ -183,10 +201,7 @@ static struct pattern *parsepattern(struct cfstate *s) free(pat->childnm); pat->childnm = sstrdup(s->argv[1]); } else if(!strcmp(s->argv[0], "fork")) { - pat->fchild = smalloc(sizeof(*pat->fchild) * s->argc); - for(i = 0; i < s->argc - 1; i++) - pat->fchild[i] = sstrdup(s->argv[i + 1]); - pat->fchild[i] = 0; + pat->fchild = cadup(s->argv + 1); } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) { break; } else { @@ -238,6 +253,11 @@ static struct config *readconfig(char *file) } else if((pat = parsepattern(s)) != NULL) { pat->next = cf->patterns; cf->patterns = pat; + } else if(!strcmp(s->argv[0], "index-file")) { + freeca(cf->index); + cf->index = NULL; + if(s->argc > 1) + cf->index = cadup(s->argv + 1); } else if(!strcmp(s->argv[0], "eof")) { break; } else { @@ -334,7 +354,7 @@ static struct child *findchild(char *file, char *name) static struct pattern *findmatch(char *file, int trydefault) { - int i, c; + int i, o, c; char *bn; struct config **cfs; struct pattern *pat; @@ -349,10 +369,18 @@ static struct pattern *findmatch(char *file, int trydefault) for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) { for(i = 0; (rule = pat->rules[i]) != NULL; i++) { if(rule->type == PAT_BASENAME) { - if(fnmatch(rule->pattern, bn, 0)) + for(o = 0; rule->patterns[o] != NULL; o++) { + if(!fnmatch(rule->patterns[o], bn, 0)) + break; + } + if(rule->patterns[o] == NULL) break; } else if(rule->type == PAT_PATHNAME) { - if(fnmatch(rule->pattern, file, FNM_PATHNAME)) + for(o = 0; rule->patterns[o] != NULL; o++) { + if(!fnmatch(rule->patterns[o], file, FNM_PATHNAME)) + break; + } + if(rule->patterns[o] == NULL) break; } else if(rule->type == PAT_ALL) { } else if(rule->type == PAT_DEFAULT) { @@ -392,8 +420,54 @@ static void handlefile(struct hthead *req, int fd, char *path) static void handledir(struct hthead *req, int fd, char *path) { - /* XXX: Todo */ - simpleerror(fd, 403, "Not Authorized", "Will not send directory listings or indices yet."); + struct config **cfs; + int i, o; + struct stat sb; + char *inm, *ipath, *p; + DIR *dir; + struct dirent *dent; + + cfs = getconfigs(sprintf3("%s/", path)); + for(i = 0; cfs[i] != NULL; i++) { + if(cfs[i]->index != NULL) { + for(o = 0; cfs[i]->index[o] != NULL; o++) { + inm = cfs[i]->index[o]; + ipath = sprintf2("%s/%s", path, inm); + if(!stat(ipath, &sb) && S_ISREG(sb.st_mode)) { + handlefile(req, fd, ipath); + free(ipath); + return; + } + free(ipath); + + ipath = NULL; + if(!strchr(inm, '.') && ((dir = opendir(path)) != NULL)) { + while((dent = readdir(dir)) != NULL) { + if((p = strchr(dent->d_name, '.')) == NULL) + continue; + if(strncmp(dent->d_name, inm, p - dent->d_name)) + continue; + ipath = sprintf2("%s/%s", path, dent->d_name); + if(stat(ipath, &sb) || !S_ISREG(sb.st_mode)) { + free(ipath); + ipath = NULL; + continue; + } + break; + } + closedir(dir); + } + if(ipath != NULL) { + handlefile(req, fd, ipath); + free(ipath); + return; + } + } + break; + } + } + /* XXX: Directory listings */ + simpleerror(fd, 403, "Not Authorized", "Will not send listings for this directory."); } static int checkdir(struct hthead *req, int fd, char *path)