Given the following httpd(8) configuration:
server "example.com" {
...
location found "/htdocs*" {
root ""
directory auto index
}
...
}
Instead of showing the directory index, accessing http://example.com/htdocs
fails with status code 500. The diff below fixes this error along
with some cleanup.
Index: usr.sbin/httpd/server_http.c
===================================================================
RCS file: /cvs/src/usr.sbin/httpd/server_http.c,v
retrieving revision 1.142
diff -u -p -u -p -r1.142 server_http.c
--- usr.sbin/httpd/server_http.c 29 Oct 2020 12:30:52 -0000 1.142
+++ usr.sbin/httpd/server_http.c 6 Nov 2020 16:07:00 -0000
@@ -1320,11 +1320,8 @@ server_response(struct httpd *httpd, str
goto fail;
/* Now search for the location */
- if ((srv_conf = server_getlocation(clt,
- desc->http_path)) == NULL) {
- server_abort_http(clt, 500, desc->http_path);
- return (-1);
- }
+ if ((srv_conf = server_getlocation(clt, desc->http_path)) == NULL)
+ goto rooterr;
/* Optional rewrite */
if (srv_conf->flags & SRVFLAG_PATH_REWRITE) {
@@ -1361,10 +1358,8 @@ server_response(struct httpd *httpd, str
/* Now search for the updated location */
if ((srv_conf = server_getlocation(clt,
- desc->http_path_alias)) == NULL) {
- server_abort_http(clt, 500, desc->http_path_alias);
- return (-1);
- }
+ desc->http_path_alias)) == NULL)
+ goto rooterr;
}
if (clt->clt_toread > 0 && (size_t)clt->clt_toread >
@@ -1386,6 +1381,11 @@ server_response(struct httpd *httpd, str
fail:
server_abort_http(clt, 400, "bad request");
return (-1);
+
+ rooterr: /* server root inaccessible */
+ srv_conf = clt->clt_srv_conf;
+ server_abort_http(clt, 500, srv_conf->root);
+ return (-1);
}
const char *
@@ -1456,7 +1456,9 @@ server_locationaccesstest(struct server_
srv_conf->flags) == 0)
return (0);
- if ((rootfd = open(srv_conf->root, O_RDONLY)) == -1)
+ /* If the server root is not accessible, we have a problem */
+ if ((rootfd = open(*srv_conf->root != '\0' ? srv_conf->root : "/",
+ O_RDONLY)) == -1)
return (-1);
path = server_root_strip(path, srv_conf->strip) + 1;