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