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