reset response headers, write_queue for error docs
[lighttpd.git] / src / mod_dirlisting.c
blobf8946207cce260260cccb6a9c2e9b0963a7e1367
1 #include "first.h"
3 #include "base.h"
4 #include "log.h"
5 #include "buffer.h"
7 #include "plugin.h"
9 #include "response.h"
10 #include "stat_cache.h"
11 #include "stream.h"
13 #include <ctype.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <dirent.h>
17 #include <assert.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <time.h>
24 /**
25 * this is a dirlisting for a lighttpd plugin
28 #ifdef HAVE_ATTR_ATTRIBUTES_H
29 #include <attr/attributes.h>
30 #endif
32 #ifdef HAVE_SYS_EXTATTR_H
33 #include <sys/extattr.h>
34 #endif
36 #include "version.h"
38 /* plugin config for all request/connections */
40 typedef struct {
41 #ifdef HAVE_PCRE_H
42 pcre *regex;
43 #endif
44 buffer *string;
45 } excludes;
47 typedef struct {
48 excludes **ptr;
50 size_t used;
51 size_t size;
52 } excludes_buffer;
54 typedef struct {
55 unsigned short dir_listing;
56 unsigned short hide_dot_files;
57 unsigned short show_readme;
58 unsigned short hide_readme_file;
59 unsigned short encode_readme;
60 unsigned short show_header;
61 unsigned short hide_header_file;
62 unsigned short encode_header;
63 unsigned short auto_layout;
65 excludes_buffer *excludes;
67 buffer *external_css;
68 buffer *encoding;
69 buffer *set_footer;
70 } plugin_config;
72 typedef struct {
73 PLUGIN_DATA;
75 buffer *tmp_buf;
76 buffer *content_charset;
78 plugin_config **config_storage;
80 plugin_config conf;
81 } plugin_data;
83 static excludes_buffer *excludes_buffer_init(void) {
84 excludes_buffer *exb;
86 exb = calloc(1, sizeof(*exb));
88 return exb;
91 static int excludes_buffer_append(excludes_buffer *exb, buffer *string) {
92 #ifdef HAVE_PCRE_H
93 size_t i;
94 const char *errptr;
95 int erroff;
97 if (!string) return -1;
99 if (exb->size == 0) {
100 exb->size = 4;
101 exb->used = 0;
103 exb->ptr = malloc(exb->size * sizeof(*exb->ptr));
105 for(i = 0; i < exb->size ; i++) {
106 exb->ptr[i] = calloc(1, sizeof(**exb->ptr));
108 } else if (exb->used == exb->size) {
109 exb->size += 4;
111 exb->ptr = realloc(exb->ptr, exb->size * sizeof(*exb->ptr));
113 for(i = exb->used; i < exb->size; i++) {
114 exb->ptr[i] = calloc(1, sizeof(**exb->ptr));
119 if (NULL == (exb->ptr[exb->used]->regex = pcre_compile(string->ptr, 0,
120 &errptr, &erroff, NULL))) {
121 return -1;
124 exb->ptr[exb->used]->string = buffer_init();
125 buffer_copy_buffer(exb->ptr[exb->used]->string, string);
127 exb->used++;
129 return 0;
130 #else
131 UNUSED(exb);
132 UNUSED(string);
134 return -1;
135 #endif
138 static void excludes_buffer_free(excludes_buffer *exb) {
139 #ifdef HAVE_PCRE_H
140 size_t i;
142 for (i = 0; i < exb->size; i++) {
143 if (exb->ptr[i]->regex) pcre_free(exb->ptr[i]->regex);
144 if (exb->ptr[i]->string) buffer_free(exb->ptr[i]->string);
145 free(exb->ptr[i]);
148 if (exb->ptr) free(exb->ptr);
149 #endif
151 free(exb);
154 /* init the plugin data */
155 INIT_FUNC(mod_dirlisting_init) {
156 plugin_data *p;
158 p = calloc(1, sizeof(*p));
160 p->tmp_buf = buffer_init();
161 p->content_charset = buffer_init();
163 return p;
166 /* detroy the plugin data */
167 FREE_FUNC(mod_dirlisting_free) {
168 plugin_data *p = p_d;
170 UNUSED(srv);
172 if (!p) return HANDLER_GO_ON;
174 if (p->config_storage) {
175 size_t i;
176 for (i = 0; i < srv->config_context->used; i++) {
177 plugin_config *s = p->config_storage[i];
179 if (!s) continue;
181 excludes_buffer_free(s->excludes);
182 buffer_free(s->external_css);
183 buffer_free(s->encoding);
184 buffer_free(s->set_footer);
186 free(s);
188 free(p->config_storage);
191 buffer_free(p->tmp_buf);
192 buffer_free(p->content_charset);
194 free(p);
196 return HANDLER_GO_ON;
199 /* handle plugin config and check values */
201 #define CONFIG_EXCLUDE "dir-listing.exclude"
202 #define CONFIG_ACTIVATE "dir-listing.activate"
203 #define CONFIG_HIDE_DOTFILES "dir-listing.hide-dotfiles"
204 #define CONFIG_EXTERNAL_CSS "dir-listing.external-css"
205 #define CONFIG_ENCODING "dir-listing.encoding"
206 #define CONFIG_SHOW_README "dir-listing.show-readme"
207 #define CONFIG_HIDE_README_FILE "dir-listing.hide-readme-file"
208 #define CONFIG_SHOW_HEADER "dir-listing.show-header"
209 #define CONFIG_HIDE_HEADER_FILE "dir-listing.hide-header-file"
210 #define CONFIG_DIR_LISTING "server.dir-listing"
211 #define CONFIG_SET_FOOTER "dir-listing.set-footer"
212 #define CONFIG_ENCODE_README "dir-listing.encode-readme"
213 #define CONFIG_ENCODE_HEADER "dir-listing.encode-header"
214 #define CONFIG_AUTO_LAYOUT "dir-listing.auto-layout"
217 SETDEFAULTS_FUNC(mod_dirlisting_set_defaults) {
218 plugin_data *p = p_d;
219 size_t i = 0;
221 config_values_t cv[] = {
222 { CONFIG_EXCLUDE, NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
223 { CONFIG_ACTIVATE, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
224 { CONFIG_HIDE_DOTFILES, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
225 { CONFIG_EXTERNAL_CSS, NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
226 { CONFIG_ENCODING, NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 4 */
227 { CONFIG_SHOW_README, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 5 */
228 { CONFIG_HIDE_README_FILE, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 6 */
229 { CONFIG_SHOW_HEADER, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 7 */
230 { CONFIG_HIDE_HEADER_FILE, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 8 */
231 { CONFIG_DIR_LISTING, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 9 */
232 { CONFIG_SET_FOOTER, NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 10 */
233 { CONFIG_ENCODE_README, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 11 */
234 { CONFIG_ENCODE_HEADER, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 12 */
235 { CONFIG_AUTO_LAYOUT, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 13 */
237 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
240 if (!p) return HANDLER_ERROR;
242 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
244 for (i = 0; i < srv->config_context->used; i++) {
245 data_config const* config = (data_config const*)srv->config_context->data[i];
246 plugin_config *s;
247 data_unset *du_excludes;
249 s = calloc(1, sizeof(plugin_config));
250 s->excludes = excludes_buffer_init();
251 s->dir_listing = 0;
252 s->external_css = buffer_init();
253 s->hide_dot_files = 1;
254 s->show_readme = 0;
255 s->hide_readme_file = 0;
256 s->show_header = 0;
257 s->hide_header_file = 0;
258 s->encode_readme = 1;
259 s->encode_header = 1;
260 s->auto_layout = 1;
262 s->encoding = buffer_init();
263 s->set_footer = buffer_init();
265 cv[0].destination = s->excludes;
266 cv[1].destination = &(s->dir_listing);
267 cv[2].destination = &(s->hide_dot_files);
268 cv[3].destination = s->external_css;
269 cv[4].destination = s->encoding;
270 cv[5].destination = &(s->show_readme);
271 cv[6].destination = &(s->hide_readme_file);
272 cv[7].destination = &(s->show_header);
273 cv[8].destination = &(s->hide_header_file);
274 cv[9].destination = &(s->dir_listing); /* old name */
275 cv[10].destination = s->set_footer;
276 cv[11].destination = &(s->encode_readme);
277 cv[12].destination = &(s->encode_header);
278 cv[13].destination = &(s->auto_layout);
280 p->config_storage[i] = s;
282 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
283 return HANDLER_ERROR;
286 if (NULL != (du_excludes = array_get_element(config->value, CONFIG_EXCLUDE))) {
287 array *excludes_list;
288 size_t j;
290 if (du_excludes->type != TYPE_ARRAY) {
291 log_error_write(srv, __FILE__, __LINE__, "sss",
292 "unexpected type for key: ", CONFIG_EXCLUDE, "array of strings");
293 return HANDLER_ERROR;
296 excludes_list = ((data_array*)du_excludes)->value;
298 #ifndef HAVE_PCRE_H
299 if (excludes_list->used > 0) {
300 log_error_write(srv, __FILE__, __LINE__, "sss",
301 "pcre support is missing for: ", CONFIG_EXCLUDE, ", please install libpcre and the headers");
302 return HANDLER_ERROR;
304 #else
305 for (j = 0; j < excludes_list->used; j++) {
306 data_unset *du_exclude = excludes_list->data[j];
308 if (du_exclude->type != TYPE_STRING) {
309 log_error_write(srv, __FILE__, __LINE__, "sssbs",
310 "unexpected type for key: ", CONFIG_EXCLUDE, "[",
311 du_exclude->key, "](string)");
312 return HANDLER_ERROR;
315 if (0 != excludes_buffer_append(s->excludes, ((data_string*)(du_exclude))->value)) {
316 log_error_write(srv, __FILE__, __LINE__, "sb",
317 "pcre-compile failed for", ((data_string*)(du_exclude))->value);
318 return HANDLER_ERROR;
321 #endif
325 return HANDLER_GO_ON;
328 #define PATCH(x) \
329 p->conf.x = s->x;
330 static int mod_dirlisting_patch_connection(server *srv, connection *con, plugin_data *p) {
331 size_t i, j;
332 plugin_config *s = p->config_storage[0];
334 PATCH(dir_listing);
335 PATCH(external_css);
336 PATCH(hide_dot_files);
337 PATCH(encoding);
338 PATCH(show_readme);
339 PATCH(hide_readme_file);
340 PATCH(show_header);
341 PATCH(hide_header_file);
342 PATCH(excludes);
343 PATCH(set_footer);
344 PATCH(encode_readme);
345 PATCH(encode_header);
346 PATCH(auto_layout);
348 /* skip the first, the global context */
349 for (i = 1; i < srv->config_context->used; i++) {
350 data_config *dc = (data_config *)srv->config_context->data[i];
351 s = p->config_storage[i];
353 /* condition didn't match */
354 if (!config_check_cond(srv, con, dc)) continue;
356 /* merge config */
357 for (j = 0; j < dc->value->used; j++) {
358 data_unset *du = dc->value->data[j];
360 if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_ACTIVATE)) ||
361 buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_DIR_LISTING))) {
362 PATCH(dir_listing);
363 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_HIDE_DOTFILES))) {
364 PATCH(hide_dot_files);
365 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_EXTERNAL_CSS))) {
366 PATCH(external_css);
367 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_ENCODING))) {
368 PATCH(encoding);
369 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_SHOW_README))) {
370 PATCH(show_readme);
371 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_HIDE_README_FILE))) {
372 PATCH(hide_readme_file);
373 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_SHOW_HEADER))) {
374 PATCH(show_header);
375 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_HIDE_HEADER_FILE))) {
376 PATCH(hide_header_file);
377 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_SET_FOOTER))) {
378 PATCH(set_footer);
379 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_EXCLUDE))) {
380 PATCH(excludes);
381 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_ENCODE_README))) {
382 PATCH(encode_readme);
383 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_ENCODE_HEADER))) {
384 PATCH(encode_header);
385 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_AUTO_LAYOUT))) {
386 PATCH(auto_layout);
391 return 0;
393 #undef PATCH
395 typedef struct {
396 size_t namelen;
397 time_t mtime;
398 off_t size;
399 } dirls_entry_t;
401 typedef struct {
402 dirls_entry_t **ent;
403 size_t used;
404 size_t size;
405 } dirls_list_t;
407 #define DIRLIST_ENT_NAME(ent) ((char*)(ent) + sizeof(dirls_entry_t))
408 #define DIRLIST_BLOB_SIZE 16
410 /* simple combsort algorithm */
411 static void http_dirls_sort(dirls_entry_t **ent, int num) {
412 int gap = num;
413 int i, j;
414 int swapped;
415 dirls_entry_t *tmp;
417 do {
418 gap = (gap * 10) / 13;
419 if (gap == 9 || gap == 10)
420 gap = 11;
421 if (gap < 1)
422 gap = 1;
423 swapped = 0;
425 for (i = 0; i < num - gap; i++) {
426 j = i + gap;
427 if (strcmp(DIRLIST_ENT_NAME(ent[i]), DIRLIST_ENT_NAME(ent[j])) > 0) {
428 tmp = ent[i];
429 ent[i] = ent[j];
430 ent[j] = tmp;
431 swapped = 1;
435 } while (gap > 1 || swapped);
438 /* buffer must be able to hold "999.9K"
439 * conversion is simple but not perfect
441 static int http_list_directory_sizefmt(char *buf, size_t bufsz, off_t size) {
442 const char unit[] = "KMGTPE"; /* Kilo, Mega, Tera, Peta, Exa */
443 const char *u = unit - 1; /* u will always increment at least once */
444 int remain;
445 size_t buflen;
447 if (size < 100)
448 size += 99;
449 if (size < 100)
450 size = 0;
452 while (1) {
453 remain = (int) size & 1023;
454 size >>= 10;
455 u++;
456 if ((size & (~0 ^ 1023)) == 0)
457 break;
460 remain /= 100;
461 if (remain > 9)
462 remain = 9;
463 if (size > 999) {
464 size = 0;
465 remain = 9;
466 u++;
469 li_itostrn(buf, bufsz, size);
470 buflen = strlen(buf);
471 if (buflen + 3 >= bufsz) return buflen;
472 buf[buflen+0] = '.';
473 buf[buflen+1] = remain + '0';
474 buf[buflen+2] = *u;
475 buf[buflen+3] = '\0';
477 return buflen + 3;
480 /* don't want to block when open()ing a fifo */
481 #if defined(O_NONBLOCK)
482 # define FIFO_NONBLOCK O_NONBLOCK
483 #else
484 # define FIFO_NONBLOCK 0
485 #endif
487 static void http_list_directory_include_file(buffer *out, buffer *path, const char *classname, int encode) {
488 int fd = open(path->ptr, O_RDONLY | FIFO_NONBLOCK);
489 ssize_t rd;
490 char buf[8192];
492 if (-1 == fd) return;
494 if (encode) {
495 buffer_append_string_len(out, CONST_STR_LEN("<pre class=\""));
496 buffer_append_string(out, classname);
497 buffer_append_string_len(out, CONST_STR_LEN("\">"));
500 while ((rd = read(fd, buf, sizeof(buf))) > 0) {
501 if (encode) {
502 buffer_append_string_encoded(out, buf, (size_t)rd, ENCODING_MINIMAL_XML);
503 } else {
504 buffer_append_string_len(out, buf, (size_t)rd);
508 if (encode) {
509 buffer_append_string_len(out, CONST_STR_LEN("</pre>"));
513 static void http_list_directory_header(server *srv, connection *con, plugin_data *p, buffer *out) {
514 UNUSED(srv);
516 if (p->conf.auto_layout) {
517 buffer_append_string_len(out, CONST_STR_LEN(
518 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
519 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n"
520 "<head>\n"
521 "<title>Index of "
523 buffer_append_string_encoded(out, CONST_BUF_LEN(con->uri.path), ENCODING_MINIMAL_XML);
524 buffer_append_string_len(out, CONST_STR_LEN("</title>\n"));
526 if (!buffer_string_is_empty(p->conf.external_css)) {
527 buffer_append_string_len(out, CONST_STR_LEN("<link rel=\"stylesheet\" type=\"text/css\" href=\""));
528 buffer_append_string_buffer(out, p->conf.external_css);
529 buffer_append_string_len(out, CONST_STR_LEN("\" />\n"));
530 } else {
531 buffer_append_string_len(out, CONST_STR_LEN(
532 "<style type=\"text/css\">\n"
533 "a, a:active {text-decoration: none; color: blue;}\n"
534 "a:visited {color: #48468F;}\n"
535 "a:hover, a:focus {text-decoration: underline; color: red;}\n"
536 "body {background-color: #F5F5F5;}\n"
537 "h2 {margin-bottom: 12px;}\n"
538 "table {margin-left: 12px;}\n"
539 "th, td {"
540 " font: 90% monospace;"
541 " text-align: left;"
542 "}\n"
543 "th {"
544 " font-weight: bold;"
545 " padding-right: 14px;"
546 " padding-bottom: 3px;"
547 "}\n"
548 "td {padding-right: 14px;}\n"
549 "td.s, th.s {text-align: right;}\n"
550 "div.list {"
551 " background-color: white;"
552 " border-top: 1px solid #646464;"
553 " border-bottom: 1px solid #646464;"
554 " padding-top: 10px;"
555 " padding-bottom: 14px;"
556 "}\n"
557 "div.foot {"
558 " font: 90% monospace;"
559 " color: #787878;"
560 " padding-top: 4px;"
561 "}\n"
562 "</style>\n"
566 buffer_append_string_len(out, CONST_STR_LEN("</head>\n<body>\n"));
569 /* HEADER.txt */
570 if (p->conf.show_header) {
571 /* if we have a HEADER file, display it in <pre class="header"></pre> */
573 buffer_copy_buffer(p->tmp_buf, con->physical.path);
574 buffer_append_slash(p->tmp_buf);
575 buffer_append_string_len(p->tmp_buf, CONST_STR_LEN("HEADER.txt"));
577 http_list_directory_include_file(out, p->tmp_buf, "header", p->conf.encode_header);
580 buffer_append_string_len(out, CONST_STR_LEN("<h2>Index of "));
581 buffer_append_string_encoded(out, CONST_BUF_LEN(con->uri.path), ENCODING_MINIMAL_XML);
582 buffer_append_string_len(out, CONST_STR_LEN(
583 "</h2>\n"
584 "<div class=\"list\">\n"
585 "<table summary=\"Directory Listing\" cellpadding=\"0\" cellspacing=\"0\">\n"
586 "<thead>"
587 "<tr>"
588 "<th class=\"n\">Name</th>"
589 "<th class=\"m\">Last Modified</th>"
590 "<th class=\"s\">Size</th>"
591 "<th class=\"t\">Type</th>"
592 "</tr>"
593 "</thead>\n"
594 "<tbody>\n"
595 "<tr class=\"d\">"
596 "<td class=\"n\"><a href=\"../\">Parent Directory</a>/</td>"
597 "<td class=\"m\">&nbsp;</td>"
598 "<td class=\"s\">- &nbsp;</td>"
599 "<td class=\"t\">Directory</td>"
600 "</tr>\n"
604 static void http_list_directory_footer(server *srv, connection *con, plugin_data *p, buffer *out) {
605 UNUSED(srv);
607 buffer_append_string_len(out, CONST_STR_LEN(
608 "</tbody>\n"
609 "</table>\n"
610 "</div>\n"
613 if (p->conf.show_readme) {
614 /* if we have a README file, display it in <pre class="readme"></pre> */
616 buffer_copy_buffer(p->tmp_buf, con->physical.path);
617 buffer_append_slash(p->tmp_buf);
618 buffer_append_string_len(p->tmp_buf, CONST_STR_LEN("README.txt"));
620 http_list_directory_include_file(out, p->tmp_buf, "readme", p->conf.encode_readme);
623 if(p->conf.auto_layout) {
624 buffer_append_string_len(out, CONST_STR_LEN(
625 "<div class=\"foot\">"
628 if (!buffer_string_is_empty(p->conf.set_footer)) {
629 buffer_append_string_buffer(out, p->conf.set_footer);
630 } else if (buffer_is_empty(con->conf.server_tag)) {
631 buffer_append_string_len(out, CONST_STR_LEN(PACKAGE_DESC));
632 } else {
633 buffer_append_string_buffer(out, con->conf.server_tag);
636 buffer_append_string_len(out, CONST_STR_LEN(
637 "</div>\n"
638 "</body>\n"
639 "</html>\n"
644 static int http_list_directory(server *srv, connection *con, plugin_data *p, buffer *dir) {
645 DIR *dp;
646 buffer *out;
647 struct dirent *dent;
648 struct stat st;
649 char *path, *path_file;
650 size_t i;
651 int hide_dotfiles = p->conf.hide_dot_files;
652 dirls_list_t dirs, files, *list;
653 dirls_entry_t *tmp;
654 char sizebuf[sizeof("999.9K")];
655 char datebuf[sizeof("2005-Jan-01 22:23:24")];
656 size_t k;
657 const char *content_type;
658 long name_max;
659 #if defined(HAVE_XATTR) || defined(HAVE_EXTATTR)
660 char attrval[128];
661 int attrlen;
662 #endif
663 #ifdef HAVE_LOCALTIME_R
664 struct tm tm;
665 #endif
667 if (buffer_string_is_empty(dir)) return -1;
669 i = buffer_string_length(dir);
671 #ifdef HAVE_PATHCONF
672 if (0 >= (name_max = pathconf(dir->ptr, _PC_NAME_MAX))) {
673 /* some broken fs (fuse) return 0 instead of -1 */
674 #ifdef NAME_MAX
675 name_max = NAME_MAX;
676 #else
677 name_max = 255; /* stupid default */
678 #endif
680 #elif defined __WIN32
681 name_max = FILENAME_MAX;
682 #else
683 name_max = NAME_MAX;
684 #endif
686 path = malloc(buffer_string_length(dir) + name_max + 1);
687 force_assert(NULL != path);
688 strcpy(path, dir->ptr);
689 path_file = path + i;
691 if (NULL == (dp = opendir(path))) {
692 log_error_write(srv, __FILE__, __LINE__, "sbs",
693 "opendir failed:", dir, strerror(errno));
695 free(path);
696 return -1;
699 dirs.ent = (dirls_entry_t**) malloc(sizeof(dirls_entry_t*) * DIRLIST_BLOB_SIZE);
700 force_assert(dirs.ent);
701 dirs.size = DIRLIST_BLOB_SIZE;
702 dirs.used = 0;
703 files.ent = (dirls_entry_t**) malloc(sizeof(dirls_entry_t*) * DIRLIST_BLOB_SIZE);
704 force_assert(files.ent);
705 files.size = DIRLIST_BLOB_SIZE;
706 files.used = 0;
708 while ((dent = readdir(dp)) != NULL) {
709 unsigned short exclude_match = 0;
711 if (dent->d_name[0] == '.') {
712 if (hide_dotfiles)
713 continue;
714 if (dent->d_name[1] == '\0')
715 continue;
716 if (dent->d_name[1] == '.' && dent->d_name[2] == '\0')
717 continue;
720 if (p->conf.hide_readme_file) {
721 if (strcmp(dent->d_name, "README.txt") == 0)
722 continue;
724 if (p->conf.hide_header_file) {
725 if (strcmp(dent->d_name, "HEADER.txt") == 0)
726 continue;
729 /* compare d_name against excludes array
730 * elements, skipping any that match.
732 #ifdef HAVE_PCRE_H
733 for(i = 0; i < p->conf.excludes->used; i++) {
734 int n;
735 #define N 10
736 int ovec[N * 3];
737 pcre *regex = p->conf.excludes->ptr[i]->regex;
739 if ((n = pcre_exec(regex, NULL, dent->d_name,
740 strlen(dent->d_name), 0, 0, ovec, 3 * N)) < 0) {
741 if (n != PCRE_ERROR_NOMATCH) {
742 log_error_write(srv, __FILE__, __LINE__, "sd",
743 "execution error while matching:", n);
745 /* aborting would require a lot of manual cleanup here.
746 * skip instead (to not leak names that break pcre matching)
748 exclude_match = 1;
749 break;
752 else {
753 exclude_match = 1;
754 break;
758 if (exclude_match) {
759 continue;
761 #endif
763 i = strlen(dent->d_name);
765 /* NOTE: the manual says, d_name is never more than NAME_MAX
766 * so this should actually not be a buffer-overflow-risk
768 if (i > (size_t)name_max) continue;
770 memcpy(path_file, dent->d_name, i + 1);
771 if (stat(path, &st) != 0)
772 continue;
774 list = &files;
775 if (S_ISDIR(st.st_mode))
776 list = &dirs;
778 if (list->used == list->size) {
779 list->size += DIRLIST_BLOB_SIZE;
780 list->ent = (dirls_entry_t**) realloc(list->ent, sizeof(dirls_entry_t*) * list->size);
781 force_assert(list->ent);
784 tmp = (dirls_entry_t*) malloc(sizeof(dirls_entry_t) + 1 + i);
785 tmp->mtime = st.st_mtime;
786 tmp->size = st.st_size;
787 tmp->namelen = i;
788 memcpy(DIRLIST_ENT_NAME(tmp), dent->d_name, i + 1);
790 list->ent[list->used++] = tmp;
792 closedir(dp);
794 if (dirs.used) http_dirls_sort(dirs.ent, dirs.used);
796 if (files.used) http_dirls_sort(files.ent, files.used);
798 out = buffer_init();
799 buffer_copy_string_len(out, CONST_STR_LEN("<?xml version=\"1.0\" encoding=\""));
800 if (buffer_string_is_empty(p->conf.encoding)) {
801 buffer_append_string_len(out, CONST_STR_LEN("iso-8859-1"));
802 } else {
803 buffer_append_string_buffer(out, p->conf.encoding);
805 buffer_append_string_len(out, CONST_STR_LEN("\"?>\n"));
806 http_list_directory_header(srv, con, p, out);
808 /* directories */
809 for (i = 0; i < dirs.used; i++) {
810 tmp = dirs.ent[i];
812 #ifdef HAVE_LOCALTIME_R
813 localtime_r(&(tmp->mtime), &tm);
814 strftime(datebuf, sizeof(datebuf), "%Y-%b-%d %H:%M:%S", &tm);
815 #else
816 strftime(datebuf, sizeof(datebuf), "%Y-%b-%d %H:%M:%S", localtime(&(tmp->mtime)));
817 #endif
819 buffer_append_string_len(out, CONST_STR_LEN("<tr class=\"d\"><td class=\"n\"><a href=\""));
820 buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_REL_URI_PART);
821 buffer_append_string_len(out, CONST_STR_LEN("/\">"));
822 buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_MINIMAL_XML);
823 buffer_append_string_len(out, CONST_STR_LEN("</a>/</td><td class=\"m\">"));
824 buffer_append_string_len(out, datebuf, sizeof(datebuf) - 1);
825 buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"s\">- &nbsp;</td><td class=\"t\">Directory</td></tr>\n"));
827 free(tmp);
830 /* files */
831 for (i = 0; i < files.used; i++) {
832 tmp = files.ent[i];
834 content_type = NULL;
835 #if defined(HAVE_XATTR)
836 if (con->conf.use_xattr) {
837 memcpy(path_file, DIRLIST_ENT_NAME(tmp), tmp->namelen + 1);
838 attrlen = sizeof(attrval) - 1;
839 if (attr_get(path, srv->srvconf.xattr_name->ptr, attrval, &attrlen, 0) == 0) {
840 attrval[attrlen] = '\0';
841 content_type = attrval;
844 #elif defined(HAVE_EXTATTR)
845 if (con->conf.use_xattr) {
846 memcpy(path_file, DIRLIST_ENT_NAME(tmp), tmp->namelen + 1);
847 if(-1 != (attrlen = extattr_get_file(path, EXTATTR_NAMESPACE_USER, srv->srvconf.xattr_name->ptr, attrval, sizeof(attrval)-1))) {
848 attrval[attrlen] = '\0';
849 content_type = attrval;
852 #endif
854 if (content_type == NULL) {
855 content_type = "application/octet-stream";
856 for (k = 0; k < con->conf.mimetypes->used; k++) {
857 data_string *ds = (data_string *)con->conf.mimetypes->data[k];
858 size_t ct_len;
860 if (buffer_is_empty(ds->key))
861 continue;
863 ct_len = buffer_string_length(ds->key);
864 if (tmp->namelen < ct_len)
865 continue;
867 if (0 == strncasecmp(DIRLIST_ENT_NAME(tmp) + tmp->namelen - ct_len, ds->key->ptr, ct_len)) {
868 content_type = ds->value->ptr;
869 break;
874 #ifdef HAVE_LOCALTIME_R
875 localtime_r(&(tmp->mtime), &tm);
876 strftime(datebuf, sizeof(datebuf), "%Y-%b-%d %H:%M:%S", &tm);
877 #else
878 strftime(datebuf, sizeof(datebuf), "%Y-%b-%d %H:%M:%S", localtime(&(tmp->mtime)));
879 #endif
880 http_list_directory_sizefmt(sizebuf, sizeof(sizebuf), tmp->size);
882 buffer_append_string_len(out, CONST_STR_LEN("<tr><td class=\"n\"><a href=\""));
883 buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_REL_URI_PART);
884 buffer_append_string_len(out, CONST_STR_LEN("\">"));
885 buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_MINIMAL_XML);
886 buffer_append_string_len(out, CONST_STR_LEN("</a></td><td class=\"m\">"));
887 buffer_append_string_len(out, datebuf, sizeof(datebuf) - 1);
888 buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"s\">"));
889 buffer_append_string(out, sizebuf);
890 buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"t\">"));
891 buffer_append_string(out, content_type);
892 buffer_append_string_len(out, CONST_STR_LEN("</td></tr>\n"));
894 free(tmp);
897 free(files.ent);
898 free(dirs.ent);
899 free(path);
901 http_list_directory_footer(srv, con, p, out);
903 /* Insert possible charset to Content-Type */
904 if (buffer_string_is_empty(p->conf.encoding)) {
905 response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html"));
906 } else {
907 buffer_copy_string_len(p->content_charset, CONST_STR_LEN("text/html; charset="));
908 buffer_append_string_buffer(p->content_charset, p->conf.encoding);
909 response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(p->content_charset));
912 con->file_finished = 1;
913 chunkqueue_append_buffer(con->write_queue, out);
914 buffer_free(out);
916 return 0;
921 URIHANDLER_FUNC(mod_dirlisting_subrequest) {
922 plugin_data *p = p_d;
923 stat_cache_entry *sce = NULL;
925 UNUSED(srv);
927 /* we only handle GET, POST and HEAD */
928 switch(con->request.http_method) {
929 case HTTP_METHOD_GET:
930 case HTTP_METHOD_POST:
931 case HTTP_METHOD_HEAD:
932 break;
933 default:
934 return HANDLER_GO_ON;
937 if (con->mode != DIRECT) return HANDLER_GO_ON;
939 if (buffer_is_empty(con->physical.path)) return HANDLER_GO_ON;
940 if (buffer_is_empty(con->uri.path)) return HANDLER_GO_ON;
941 if (con->uri.path->ptr[buffer_string_length(con->uri.path) - 1] != '/') return HANDLER_GO_ON;
943 mod_dirlisting_patch_connection(srv, con, p);
945 if (!p->conf.dir_listing) return HANDLER_GO_ON;
947 if (con->conf.log_request_handling) {
948 log_error_write(srv, __FILE__, __LINE__, "s", "-- handling the request as Dir-Listing");
949 log_error_write(srv, __FILE__, __LINE__, "sb", "URI :", con->uri.path);
952 if (HANDLER_ERROR == stat_cache_get_entry(srv, con, con->physical.path, &sce)) {
953 log_error_write(srv, __FILE__, __LINE__, "SB", "stat_cache_get_entry failed: ", con->physical.path);
954 SEGFAULT();
957 if (!S_ISDIR(sce->st.st_mode)) return HANDLER_GO_ON;
959 if (http_list_directory(srv, con, p, con->physical.path)) {
960 /* dirlisting failed */
961 con->http_status = 403;
964 buffer_reset(con->physical.path);
966 /* not found */
967 return HANDLER_FINISHED;
970 /* this function is called at dlopen() time and inits the callbacks */
972 int mod_dirlisting_plugin_init(plugin *p);
973 int mod_dirlisting_plugin_init(plugin *p) {
974 p->version = LIGHTTPD_VERSION_ID;
975 p->name = buffer_init_string("dirlisting");
977 p->init = mod_dirlisting_init;
978 p->handle_subrequest_start = mod_dirlisting_subrequest;
979 p->set_defaults = mod_dirlisting_set_defaults;
980 p->cleanup = mod_dirlisting_free;
982 p->data = NULL;
984 return 0;