Home | History | Annotate | Line # | Download | only in httpd
dir-index-bozo.c revision 1.28
      1 /*	$NetBSD: dir-index-bozo.c,v 1.28 2018/11/22 08:54:08 mrg Exp $	*/
      2 
      3 /*	$eterna: dir-index-bozo.c,v 1.20 2011/11/18 09:21:15 mrg Exp $	*/
      4 
      5 /*
      6  * Copyright (c) 1997-2018 Matthew R. Green
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer and
     16  *    dedication in the documentation and/or other materials provided
     17  *    with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  */
     32 
     33 /* this code implements directory index generation for bozohttpd */
     34 
     35 #ifndef NO_DIRINDEX_SUPPORT
     36 
     37 #include <sys/param.h>
     38 
     39 #include <dirent.h>
     40 #include <errno.h>
     41 #include <string.h>
     42 #include <stdlib.h>
     43 #include <time.h>
     44 #include <assert.h>
     45 
     46 #include "bozohttpd.h"
     47 
     48 static void
     49 directory_hr(bozohttpd_t *httpd)
     50 {
     51 
     52 	bozo_printf(httpd,
     53 		"<hr noshade align=\"left\" width=\"80%%\">\r\n\r\n");
     54 }
     55 
     56 /*
     57  * output a directory index.  return 1 if it actually did something..
     58  */
     59 int
     60 bozo_dir_index(bozo_httpreq_t *request, const char *dirpath, int isindex)
     61 {
     62 	bozohttpd_t *httpd = request->hr_httpd;
     63 	struct stat sb;
     64 	struct dirent **de, **deo;
     65 	struct tm *tm;
     66 	DIR *dp;
     67 	char buf[MAXPATHLEN];
     68 	char spacebuf[48];
     69 	char *file = NULL, *printname = NULL;
     70 	int l, k, j, i;
     71 
     72 	if (!isindex || !httpd->dir_indexing)
     73 		return 0;
     74 
     75 	if (strlen(dirpath) <= strlen(httpd->index_html))
     76 		dirpath = ".";
     77 	else {
     78 		file = bozostrdup(httpd, request, dirpath);
     79 
     80 		file[strlen(file) - strlen(httpd->index_html)] = '\0';
     81 		dirpath = file;
     82 	}
     83 	debug((httpd, DEBUG_FAT, "bozo_dir_index: dirpath '%s'", dirpath));
     84 	if (stat(dirpath, &sb) < 0 ||
     85 	    (dp = opendir(dirpath)) == NULL) {
     86 		if (errno == EPERM)
     87 			bozo_http_error(httpd, 403, request,
     88 					"no permission to open directory");
     89 		else if (errno == ENOENT)
     90 			bozo_http_error(httpd, 404, request, "no file");
     91 		else
     92 			bozo_http_error(httpd, 500, request, "open directory");
     93 		goto done;
     94 		/* NOTREACHED */
     95 	}
     96 
     97 	bozo_printf(httpd, "%s 200 OK\r\n", request->hr_proto);
     98 
     99 	if (request->hr_proto != httpd->consts.http_09) {
    100 		bozo_print_header(request, NULL, "text/html", "");
    101 		bozo_printf(httpd, "\r\n");
    102 	}
    103 	bozo_flush(httpd, stdout);
    104 
    105 	if (request->hr_method == HTTP_HEAD) {
    106 		closedir(dp);
    107 		goto done;
    108 	}
    109 
    110 #ifndef NO_USER_SUPPORT
    111 	if (request->hr_user) {
    112 		bozoasprintf(httpd, &printname, "~%s/%s",
    113 			     request->hr_user, request->hr_file);
    114 	} else
    115 		printname = bozostrdup(httpd, request, request->hr_file);
    116 #else
    117 	printname = bozostrdup(httpd, request, request->hr_file);
    118 #endif /* !NO_USER_SUPPORT */
    119 
    120 	bozo_printf(httpd,
    121 		"<html><head><title>Index of %s</title></head>\r\n",
    122 		printname);
    123 	bozo_printf(httpd, "<body><h1>Index of %s</h1>\r\n",
    124 		printname);
    125 	bozo_printf(httpd, "<pre>\r\n");
    126 #define NAMELEN 40
    127 #define LMODLEN 19
    128 	bozo_printf(httpd, "Name                                     "
    129 	    "Last modified          "
    130 	    "Size\n");
    131 	bozo_printf(httpd, "</pre>");
    132 	directory_hr(httpd);
    133 	bozo_printf(httpd, "<pre>");
    134 
    135 	for (j = k = scandir(dirpath, &de, NULL, alphasort), deo = de;
    136 	    j--; de++) {
    137 		int nostat = 0;
    138 		char *name = (*de)->d_name;
    139 		char *urlname, *htmlname;
    140 
    141 		if (strcmp(name, ".") == 0 ||
    142 		    (strcmp(name, "..") != 0 &&
    143 		     httpd->hide_dots && name[0] == '.'))
    144 			continue;
    145 
    146 		snprintf(buf, sizeof buf, "%s/%s", dirpath, name);
    147 		if (stat(buf, &sb))
    148 			nostat = 1;
    149 
    150 		l = 0;
    151 
    152 		urlname = bozo_escape_rfc3986(httpd, name, 0);
    153 		htmlname = bozo_escape_html(httpd, name);
    154 		if (htmlname == NULL)
    155 			htmlname = name;
    156 		if (strcmp(name, "..") == 0) {
    157 			bozo_printf(httpd, "<a href=\"../\">");
    158 			l += bozo_printf(httpd, "Parent Directory");
    159 		} else if (!nostat && S_ISDIR(sb.st_mode)) {
    160 			bozo_printf(httpd, "<a href=\"%s/\">", urlname);
    161 			l += bozo_printf(httpd, "%s/", htmlname);
    162 		} else if (strchr(name, ':') != NULL) {
    163 			/* RFC 3986 4.2 */
    164 			bozo_printf(httpd, "<a href=\"./%s\">", urlname);
    165 			l += bozo_printf(httpd, "%s", htmlname);
    166 		} else {
    167 			bozo_printf(httpd, "<a href=\"%s\">", urlname);
    168 			l += bozo_printf(httpd, "%s", htmlname);
    169 		}
    170 		if (htmlname != name)
    171 			free(htmlname);
    172 		bozo_printf(httpd, "</a>");
    173 
    174 		/* NAMELEN spaces */
    175 		/*LINTED*/
    176 		assert(/*CONSTCOND*/sizeof(spacebuf) > NAMELEN);
    177 		i = (l < NAMELEN) ? (NAMELEN - l) : 0;
    178 		i++;
    179 		memset(spacebuf, ' ', (size_t)i);
    180 		spacebuf[i] = '\0';
    181 		bozo_printf(httpd, "%s", spacebuf);
    182 		l += i;
    183 
    184 		if (nostat)
    185 			bozo_printf(httpd, "?                         ?");
    186 		else {
    187 			unsigned long long len;
    188 
    189 			len = ((unsigned long long)sb.st_size + 1023) / 1024;
    190 
    191 			tm = gmtime(&sb.st_mtime);
    192 			strftime(buf, sizeof buf, "%d-%b-%Y %R", tm);
    193 			l += bozo_printf(httpd, "%s", buf);
    194 
    195 			/* LMODLEN spaces */
    196 			/*LINTED*/
    197 			assert(/*CONSTCOND*/sizeof(spacebuf) > LMODLEN);
    198 			i = (l < (LMODLEN+NAMELEN+1)) ?
    199 				((LMODLEN+NAMELEN+1) - l) : 0;
    200 			i++;
    201 			memset(spacebuf, ' ', (size_t)i);
    202 			spacebuf[i] = '\0';
    203 			bozo_printf(httpd, "%s", spacebuf);
    204 
    205 			bozo_printf(httpd, "%12llukB", len);
    206 		}
    207 		bozo_printf(httpd, "\r\n");
    208 	}
    209 
    210 	closedir(dp);
    211 	while (k--)
    212         	free(deo[k]);
    213 	free(deo);
    214 	bozo_printf(httpd, "</pre>");
    215 	directory_hr(httpd);
    216 	bozo_printf(httpd, "</body></html>\r\n\r\n");
    217 	bozo_flush(httpd, stdout);
    218 
    219 done:
    220 	free(file);
    221 	free(printname);
    222 	return 1;
    223 }
    224 #endif /* NO_DIRINDEX_SUPPORT */
    225 
    226