dirplex: Add directive to allow selected dot-files and -directories.
authorFredrik Tolf <fredrik@dolda2000.com>
Thu, 27 Jul 2023 14:38:53 +0000 (16:38 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Thu, 27 Jul 2023 14:38:53 +0000 (16:38 +0200)
src/dirplex/conf.c
src/dirplex/dirplex.c
src/dirplex/dirplex.h

index 9ae4ca7..0bfa6f4 100644 (file)
@@ -84,6 +84,7 @@ static void freeconfig(struct config *cf)
        freepattern(pat);
     }
     freeca(cf->index);
+    freeca(cf->dotallow);
     if(cf->capture != NULL)
        free(cf->capture);
     if(cf->reparse != NULL)
@@ -258,6 +259,9 @@ struct config *readconfig(char *file)
        } else if(!strcmp(s->argv[0], "index-file")) {
            freeca(cf->index);
            cf->index = cadup(s->argv + 1);
+       } else if(!strcmp(s->argv[0], "dot-allow")) {
+           freeca(cf->dotallow);
+           cf->dotallow = cadup(s->argv + 1);
        } else if(!strcmp(s->argv[0], "capture")) {
            if(s->argc < 2) {
                flog(LOG_WARNING, "%s:%i: missing argument to capture declaration", s->file, s->lno);
index 73bb9cc..ee7f650 100644 (file)
@@ -25,6 +25,7 @@
 #include <ctype.h>
 #include <dirent.h>
 #include <time.h>
+#include <fnmatch.h>
 #include <sys/wait.h>
 #include <sys/signal.h>
 
@@ -131,6 +132,27 @@ static void handlefile(struct hthead *req, int fd, char *path)
     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;
@@ -155,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);
@@ -216,9 +246,9 @@ static int checkentry(struct hthead *req, int fd, char *path, char *rest, char *
     char *newpath;
     int rv;
     
-    if(*el == '.')
-       return(0);
     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));
index ffe12a6..754327d 100644 (file)
@@ -17,7 +17,7 @@ struct config {
     time_t mtime, lastck;
     struct child *children;
     struct pattern *patterns;
-    char **index;
+    char **index, **dotallow;
     char *capture, *reparse;
     int caproot, parsecomb;
 };