commit 9526d3d35917770ca5a021d745bcf50d382875b2
parent fc79d9a4d2cd906e09d9f046e9f1cc91e35b0f7e
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Wed,  8 Mar 2023 20:54:44 +0100
fix a regression from 980a398da8acca65a13936ff0792c39f3dcb0ede
Open directory listings did not prefix the entries with / correctly when using
a chroot.
Signed-off-by: Christoph Lohmann <20h@r-36.net>
Diffstat:
1 file changed, 17 insertions(+), 5 deletions(-)
diff --git a/handlr.c b/handlr.c
@@ -21,6 +21,16 @@
 #include "ind.h"
 #include "arg.h"
 
+char *
+make_base_path(char *path, char *base)
+{
+	if (!(base[0] == '/' && base[1] == '\0') &&
+	    strlen(path) > strlen(base))
+		return path + strlen(base);
+	else
+		return path;
+}
+
 void
 handledir(int sock, char *path, char *port, char *base, char *args,
 		char *sear, char *ohost, char *chost, char *bhost, int istls)
@@ -37,15 +47,16 @@ handledir(int sock, char *path, char *port, char *base, char *args,
 
 	pa = xstrdup(path);
 	e = pa + strlen(pa) - 1;
-	if (e[0] == '/')
+	if (e > pa && e[0] == '/')
 		*e = '\0';
 
 	par = xstrdup(pa);
-	b = strrchr(par + strlen(base), '/');
+
+	b = strrchr(make_base_path(par, base), '/');
 	if (b != NULL) {
 		*b = '\0';
 		dprintf(sock, "1..\t%s\t%s\t%s\r\n",
-			par + strlen(base), ohost, port);
+			make_base_path(par, base), ohost, port);
 	}
 	free(par);
 
@@ -62,11 +73,12 @@ handledir(int sock, char *path, char *port, char *base, char *args,
 			}
 
 			type = gettype(dirent[i]->d_name);
-			file = smprintf("%s/%s", pa,
+			file = smprintf("%s%s%s", pa,
+					pa[0] == '/' && pa[1] == '\0' ? "" : "/",
 					dirent[i]->d_name);
 			if (stat(file, &st) >= 0 && S_ISDIR(st.st_mode))
 				type = gettype("index.gph");
-			e = file + strlen(base);
+			e = make_base_path(file, base);
 			ret = dprintf(sock,
 					"%c%-50.50s %10s %16s\t%s\t%s\t%s\r\n",
 					*type->type,