dir-index-bozo.c revision 1.10
1/*	$NetBSD: dir-index-bozo.c,v 1.10 2010/08/16 18:32:44 dogcow Exp $	*/
2
3/*	$eterna: dir-index-bozo.c,v 1.17 2010/05/10 02:51:28 mrg Exp $	*/
4
5/*
6 * Copyright (c) 1997-2010 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
48static void
49directory_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 */
59int
60bozo_dir_index(bozo_httpreq_t *request, const char *dirname, 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;
70	int l, k, j, i;
71
72	if (!isindex || !httpd->dir_indexing)
73		return 0;
74
75	if (strlen(dirname) <= strlen(httpd->index_html))
76		dirname = ".";
77	else {
78		file = bozostrdup(httpd, dirname);
79
80		file[strlen(file) - strlen(httpd->index_html)] = '\0';
81		dirname = file;
82	}
83	debug((httpd, DEBUG_FAT, "bozo_dir_index: dirname ``%s''", dirname));
84	if (stat(dirname, &sb) < 0 ||
85	    (dp = opendir(dirname)) == NULL) {
86		if (errno == EPERM)
87			(void)bozo_http_error(httpd, 403, request,
88			    "no permission to open directory");
89		else if (errno == ENOENT)
90			(void)bozo_http_error(httpd, 404, request, "no file");
91		else
92			(void)bozo_http_error(httpd, 500, request,
93					"open directory");
94		goto done;
95		/* NOTREACHED */
96	}
97
98	bozo_printf(httpd, "%s 200 OK\r\n", request->hr_proto);
99
100	if (request->hr_proto != httpd->consts.http_09) {
101		bozo_print_header(request, NULL, "text/html", "");
102		bozo_printf(httpd, "\r\n");
103	}
104	bozo_flush(httpd, stdout);
105
106	if (request->hr_method == HTTP_HEAD) {
107		closedir(dp);
108		goto done;
109	}
110
111	bozo_printf(httpd,
112		"<html><head><title>Index of %s</title></head>\r\n",
113		request->hr_file);
114	bozo_printf(httpd, "<body><h1>Index of %s</h1>\r\n",
115		request->hr_file);
116	bozo_printf(httpd, "<pre>\r\n");
117#define NAMELEN 40
118#define LMODLEN 19
119	bozo_printf(httpd, "Name                                     "
120	    "Last modified          "
121	    "Size\n");
122	bozo_printf(httpd, "</pre>");
123	directory_hr(httpd);
124	bozo_printf(httpd, "<pre>");
125
126	for (j = k = scandir(dirname, &de, NULL, alphasort), deo = de;
127	    j--; de++) {
128		int nostat = 0;
129		char *name = (*de)->d_name;
130
131		if (strcmp(name, ".") == 0 ||
132		    (strcmp(name, "..") != 0 &&
133		     httpd->hide_dots && name[0] == '.'))
134			continue;
135
136		snprintf(buf, sizeof buf, "%s/%s", dirname, name);
137		if (stat(buf, &sb))
138			nostat = 1;
139
140		l = 0;
141
142		if (strcmp(name, "..") == 0) {
143			bozo_printf(httpd, "<a href=\"../\">");
144			l += bozo_printf(httpd, "Parent Directory");
145		} else if (S_ISDIR(sb.st_mode)) {
146			bozo_printf(httpd, "<a href=\"%s/\">", name);
147			l += bozo_printf(httpd, "%s/", name);
148		} else {
149			bozo_printf(httpd, "<a href=\"%s\">", name);
150			l += bozo_printf(httpd, "%s", name);
151		}
152		bozo_printf(httpd, "</a>");
153
154		/* NAMELEN spaces */
155		/*LINTED*/
156		assert(/*CONSTCOND*/sizeof(spacebuf) > NAMELEN);
157		i = (l < NAMELEN) ? (NAMELEN - l) : 0;
158		i++;
159		memset(spacebuf, ' ', (size_t)i);
160		spacebuf[i] = '\0';
161		bozo_printf(httpd, spacebuf);
162		l += i;
163
164		if (nostat)
165			bozo_printf(httpd, "?                         ?");
166		else {
167			tm = gmtime(&sb.st_mtime);
168			strftime(buf, sizeof buf, "%d-%b-%Y %R", tm);
169			l += bozo_printf(httpd, "%s", buf);
170
171			/* LMODLEN spaces */
172			/*LINTED*/
173			assert(/*CONSTCOND*/sizeof(spacebuf) > LMODLEN);
174			i = (l < (LMODLEN+NAMELEN+1)) ?
175				((LMODLEN+NAMELEN+1) - l) : 0;
176			i++;
177			memset(spacebuf, ' ', (size_t)i);
178			spacebuf[i] = '\0';
179			bozo_printf(httpd, spacebuf);
180
181			bozo_printf(httpd, "%7ukB",
182			    ((unsigned)((unsigned)(sb.st_size) >> 10)));
183		}
184		bozo_printf(httpd, "\r\n");
185	}
186
187	closedir(dp);
188	while (k--)
189        	free(deo[k]);
190	free(deo);
191	bozo_printf(httpd, "</pre>");
192	directory_hr(httpd);
193	bozo_printf(httpd, "</body></html>\r\n\r\n");
194	bozo_flush(httpd, stdout);
195
196done:
197	if (file)
198		free(file);
199	return 1;
200}
201#endif /* NO_DIRINDEX_SUPPORT */
202
203