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