xref: /netbsd-src/libexec/httpd/dir-index-bozo.c (revision 07b58cb10b1979a2ac1b95133b4d5cfab1b25ba7)
1*07b58cb1Smaya /*	$NetBSD: dir-index-bozo.c,v 1.37 2024/04/26 20:27:12 maya Exp $	*/
218c80b65Stls 
341f9e942Smrg /*	$eterna: dir-index-bozo.c,v 1.20 2011/11/18 09:21:15 mrg Exp $	*/
460dbe745Stls 
560dbe745Stls /*
61d41f43aSmrg  * Copyright (c) 1997-2022 Matthew R. Green
760dbe745Stls  * All rights reserved.
860dbe745Stls  *
960dbe745Stls  * Redistribution and use in source and binary forms, with or without
1060dbe745Stls  * modification, are permitted provided that the following conditions
1160dbe745Stls  * are met:
1260dbe745Stls  * 1. Redistributions of source code must retain the above copyright
1360dbe745Stls  *    notice, this list of conditions and the following disclaimer.
1460dbe745Stls  * 2. Redistributions in binary form must reproduce the above copyright
1560dbe745Stls  *    notice, this list of conditions and the following disclaimer and
1660dbe745Stls  *    dedication in the documentation and/or other materials provided
1760dbe745Stls  *    with the distribution.
1860dbe745Stls  *
1960dbe745Stls  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2060dbe745Stls  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2160dbe745Stls  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2260dbe745Stls  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2360dbe745Stls  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2460dbe745Stls  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2560dbe745Stls  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2660dbe745Stls  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2760dbe745Stls  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2860dbe745Stls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2960dbe745Stls  * SUCH DAMAGE.
3060dbe745Stls  *
3160dbe745Stls  */
3260dbe745Stls 
3360dbe745Stls /* this code implements directory index generation for bozohttpd */
3460dbe745Stls 
3560dbe745Stls #ifndef NO_DIRINDEX_SUPPORT
3660dbe745Stls 
3760dbe745Stls #include <sys/param.h>
3860dbe745Stls 
3960dbe745Stls #include <dirent.h>
4060dbe745Stls #include <errno.h>
41026e4ac0Sjmcneill #include <fcntl.h>
4260dbe745Stls #include <string.h>
4303387632Smrg #include <stdlib.h>
4460dbe745Stls #include <time.h>
45026e4ac0Sjmcneill #include <unistd.h>
4660dbe745Stls #include <assert.h>
4760dbe745Stls 
4860dbe745Stls #include "bozohttpd.h"
4960dbe745Stls 
5060dbe745Stls /*
5160dbe745Stls  * output a directory index.  return 1 if it actually did something..
5260dbe745Stls  */
5360dbe745Stls int
bozo_dir_index(bozo_httpreq_t * request,const char * dirpath,int isindex)544ff1396fSmrg bozo_dir_index(bozo_httpreq_t *request, const char *dirpath, int isindex)
5560dbe745Stls {
56ce206308Smrg 	bozohttpd_t *httpd = request->hr_httpd;
5760dbe745Stls 	struct stat sb;
58172b076eSdogcow 	struct dirent **de, **deo;
5960dbe745Stls 	DIR *dp;
6060dbe745Stls 	char buf[MAXPATHLEN];
619b91523eSmrg 	char *file = NULL, *printname = NULL, *p;
62026e4ac0Sjmcneill 	int k, j, fd;
63026e4ac0Sjmcneill 	ssize_t rlen;
6460dbe745Stls 
65ce206308Smrg 	if (!isindex || !httpd->dir_indexing)
6660dbe745Stls 		return 0;
6760dbe745Stls 
684ff1396fSmrg 	if (strlen(dirpath) <= strlen(httpd->index_html))
694ff1396fSmrg 		dirpath = ".";
7060dbe745Stls 	else {
71cff2d956Smrg 		file = bozostrdup(httpd, request, dirpath);
7260dbe745Stls 
73ce206308Smrg 		file[strlen(file) - strlen(httpd->index_html)] = '\0';
744ff1396fSmrg 		dirpath = file;
7560dbe745Stls 	}
764cfb2183Smrg 	debug((httpd, DEBUG_FAT, "bozo_dir_index: dirpath '%s'", dirpath));
774ff1396fSmrg 	if (stat(dirpath, &sb) < 0 ||
784ff1396fSmrg 	    (dp = opendir(dirpath)) == NULL) {
7960dbe745Stls 		if (errno == EPERM)
804cfb2183Smrg 			bozo_http_error(httpd, 403, request,
8160dbe745Stls 					"no permission to open directory");
8260dbe745Stls 		else if (errno == ENOENT)
834cfb2183Smrg 			bozo_http_error(httpd, 404, request, "no file");
8460dbe745Stls 		else
854cfb2183Smrg 			bozo_http_error(httpd, 500, request, "open directory");
8603387632Smrg 		goto done;
8760dbe745Stls 		/* NOTREACHED */
8860dbe745Stls 	}
8960dbe745Stls 
90ce206308Smrg 	bozo_printf(httpd, "%s 200 OK\r\n", request->hr_proto);
9160dbe745Stls 
92ce206308Smrg 	if (request->hr_proto != httpd->consts.http_09) {
93ce206308Smrg 		bozo_print_header(request, NULL, "text/html", "");
94ce206308Smrg 		bozo_printf(httpd, "\r\n");
9560dbe745Stls 	}
96ce206308Smrg 	bozo_flush(httpd, stdout);
9760dbe745Stls 
9860dbe745Stls 	if (request->hr_method == HTTP_HEAD) {
9960dbe745Stls 		closedir(dp);
10003387632Smrg 		goto done;
10160dbe745Stls 	}
10260dbe745Stls 
103c4fe1facSshm #ifndef NO_USER_SUPPORT
104c4fe1facSshm 	if (request->hr_user) {
105c2e98309Smrg 		bozoasprintf(httpd, &printname, "~%s/%s",
106c2e98309Smrg 			     request->hr_user, request->hr_file);
107c4fe1facSshm 	} else
108cff2d956Smrg 		printname = bozostrdup(httpd, request, request->hr_file);
109c4fe1facSshm #else
110cff2d956Smrg 	printname = bozostrdup(httpd, request, request->hr_file);
111c4fe1facSshm #endif /* !NO_USER_SUPPORT */
1129b91523eSmrg 	if ((p = strstr(printname, httpd->index_html)) != NULL) {
1139b91523eSmrg 		if (strcmp(printname, httpd->index_html) == 0)
1149b91523eSmrg 			strcpy(printname, "/");	/* is ``slashdir'' */
1159b91523eSmrg 		else
1169b91523eSmrg 			*p = '\0';		/* strip unwanted ``index_html'' */
1179b91523eSmrg 	}
1189b91523eSmrg 	if ((p = bozo_escape_html(httpd, printname)) != NULL) {
1199b91523eSmrg 		free(printname);
1209b91523eSmrg 		printname = p;
1219b91523eSmrg 	}
122c4fe1facSshm 
1239b91523eSmrg 	bozo_printf(httpd,
1249b91523eSmrg 		"<!DOCTYPE html>\r\n"
1259b91523eSmrg 		"<html><head><meta charset=\"utf-8\"/>\r\n"
126*07b58cb1Smaya 		"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\r\n"
1279b91523eSmrg 		"<style type=\"text/css\">\r\n"
1289b91523eSmrg 		"table {\r\n"
1299b91523eSmrg 		"\tborder-top: 1px solid black;\r\n"
1309b91523eSmrg 		"\tborder-bottom: 1px solid black;\r\n"
1319b91523eSmrg 		"}\r\n"
1329b91523eSmrg 		"th { background: aquamarine; }\r\n"
1339b91523eSmrg 		"tr:nth-child(even) { background: lavender; }\r\n"
1349b91523eSmrg 		"</style>\r\n");
13534ece249Smrg 	bozo_printf(httpd, "<title>Index of %s</title></head>\r\n",
136c4fe1facSshm 		printname);
137ce206308Smrg 	bozo_printf(httpd, "<body><h1>Index of %s</h1>\r\n",
138c4fe1facSshm 		printname);
1399b91523eSmrg 	bozo_printf(httpd,
1409b91523eSmrg 		"<table cols=3>\r\n<thead>\r\n"
1419b91523eSmrg 		"<tr><th>Name<th>Last modified<th align=right>Size\r\n"
1429b91523eSmrg 		"<tbody>\r\n");
14360dbe745Stls 
1449b91523eSmrg 	for (j = k = scandir(dirpath, &de, NULL, alphasort), deo = de;
145389848dbSmrg 	    j-- > 0; de++) {
14660dbe745Stls 		int nostat = 0;
147035f1ce1Sdogcow 		char *name = (*de)->d_name;
148a4b84ca0Smrg 		char *urlname, *htmlname;
14960dbe745Stls 
15060dbe745Stls 		if (strcmp(name, ".") == 0 ||
151ce206308Smrg 		    (strcmp(name, "..") != 0 &&
152ce206308Smrg 		     httpd->hide_dots && name[0] == '.'))
15360dbe745Stls 			continue;
15460dbe745Stls 
15500e064adSmrg 		if (bozo_check_special_files(request, name, false))
15600e064adSmrg 			continue;
15700e064adSmrg 
1584ff1396fSmrg 		snprintf(buf, sizeof buf, "%s/%s", dirpath, name);
15960dbe745Stls 		if (stat(buf, &sb))
16060dbe745Stls 			nostat = 1;
16160dbe745Stls 
162c4fe1facSshm 		urlname = bozo_escape_rfc3986(httpd, name, 0);
163a4b84ca0Smrg 		htmlname = bozo_escape_html(httpd, name);
164a4b84ca0Smrg 		if (htmlname == NULL)
165a4b84ca0Smrg 			htmlname = name;
1669b91523eSmrg 		bozo_printf(httpd, "<tr><td>");
16760dbe745Stls 		if (strcmp(name, "..") == 0) {
168ce206308Smrg 			bozo_printf(httpd, "<a href=\"../\">");
16934ece249Smrg 			bozo_printf(httpd, "Parent Directory");
170b4624ca9Smrg 		} else if (!nostat && S_ISDIR(sb.st_mode)) {
1711be97454Smrg 			bozo_printf(httpd, "<a href=\"%s/\">", urlname);
17234ece249Smrg 			bozo_printf(httpd, "%s/", htmlname);
173728c26c1Sreed 		} else if (strchr(name, ':') != NULL) {
174728c26c1Sreed 			/* RFC 3986 4.2 */
1751be97454Smrg 			bozo_printf(httpd, "<a href=\"./%s\">", urlname);
17634ece249Smrg 			bozo_printf(httpd, "%s", htmlname);
17760dbe745Stls 		} else {
1781be97454Smrg 			bozo_printf(httpd, "<a href=\"%s\">", urlname);
17934ece249Smrg 			bozo_printf(httpd, "%s", htmlname);
18060dbe745Stls 		}
181a4b84ca0Smrg 		if (htmlname != name)
182a4b84ca0Smrg 			free(htmlname);
183ce206308Smrg 		bozo_printf(httpd, "</a>");
18460dbe745Stls 
18560dbe745Stls 		if (nostat)
18634ece249Smrg 			bozo_printf(httpd, "<td>?<td>?\r\n");
18760dbe745Stls 		else {
188b4624ca9Smrg 			unsigned long long len;
189b4624ca9Smrg 
19034ece249Smrg 			strftime(buf, sizeof buf, "%d-%b-%Y %R", gmtime(&sb.st_mtime));
19134ece249Smrg 			bozo_printf(httpd, "<td>%s", buf);
19234ece249Smrg 
193b4624ca9Smrg 			len = ((unsigned long long)sb.st_size + 1023) / 1024;
19434ece249Smrg 			bozo_printf(httpd, "<td align=right>%llukB", len);
19560dbe745Stls 		}
196ce206308Smrg 		bozo_printf(httpd, "\r\n");
19760dbe745Stls 	}
19860dbe745Stls 
19960dbe745Stls 	closedir(dp);
200172b076eSdogcow 	while (k--)
201172b076eSdogcow         	free(deo[k]);
202172b076eSdogcow 	free(deo);
20334ece249Smrg 	bozo_printf(httpd, "</table>\r\n");
204026e4ac0Sjmcneill 	if (httpd->dir_readme != NULL) {
205026e4ac0Sjmcneill 		if (httpd->dir_readme[0] == '/')
206026e4ac0Sjmcneill 			snprintf(buf, sizeof buf, "%s", httpd->dir_readme);
207026e4ac0Sjmcneill 		else
208026e4ac0Sjmcneill 			snprintf(buf, sizeof buf, "%s/%s", dirpath, httpd->dir_readme);
209026e4ac0Sjmcneill 		fd = open(buf, O_RDONLY);
210026e4ac0Sjmcneill 		if (fd != -1) {
211026e4ac0Sjmcneill 			bozo_flush(httpd, stdout);
212026e4ac0Sjmcneill 			do {
213026e4ac0Sjmcneill 				rlen = read(fd, buf, sizeof buf);
214026e4ac0Sjmcneill 				if (rlen <= 0)
215026e4ac0Sjmcneill 					break;
216026e4ac0Sjmcneill 				bozo_write(httpd, STDOUT_FILENO, buf, rlen);
217026e4ac0Sjmcneill 			} while (1);
218026e4ac0Sjmcneill 			close(fd);
219026e4ac0Sjmcneill 		}
220026e4ac0Sjmcneill 	}
221ce206308Smrg 	bozo_printf(httpd, "</body></html>\r\n\r\n");
222ce206308Smrg 	bozo_flush(httpd, stdout);
22360dbe745Stls 
22403387632Smrg done:
22503387632Smrg 	free(file);
226c4fe1facSshm 	free(printname);
22760dbe745Stls 	return 1;
22860dbe745Stls }
22960dbe745Stls #endif /* NO_DIRINDEX_SUPPORT */
230