[core] set REDIRECT_STATUS to error_handler_saved_status (fixes #1828)
[lighttpd.git] / src / mod_dirlisting.c
blob8775736c2bc4da74639bf9a7dde90a8866dceaa8
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 <stdio.h>
20 #include <unistd.h>
21 #include <time.h>
23 /**
24 * this is a dirlisting for a lighttpd plugin
27 #ifdef HAVE_ATTR_ATTRIBUTES_H
28 #include <attr/attributes.h>
29 #endif
31 #ifdef HAVE_SYS_EXTATTR_H
32 #include <sys/extattr.h>
33 #endif
35 #include "version.h"
37 /* plugin config for all request/connections */
39 typedef struct {
40 #ifdef HAVE_PCRE_H
41 pcre *regex;
42 #endif
43 buffer *string;
44 } excludes;
46 typedef struct {
47 excludes **ptr;
49 size_t used;
50 size_t size;
51 } excludes_buffer;
53 typedef struct {
54 unsigned short dir_listing;
55 unsigned short hide_dot_files;
56 unsigned short show_readme;
57 unsigned short hide_readme_file;
58 unsigned short encode_readme;
59 unsigned short show_header;
60 unsigned short hide_header_file;
61 unsigned short encode_header;
62 unsigned short auto_layout;
64 excludes_buffer *excludes;
66 buffer *external_css;
67 buffer *encoding;
68 buffer *set_footer;
69 } plugin_config;
71 typedef struct {
72 PLUGIN_DATA;
74 buffer *tmp_buf;
75 buffer *content_charset;
77 plugin_config **config_storage;
79 plugin_config conf;
80 } plugin_data;
82 static excludes_buffer *excludes_buffer_init(void) {
83 excludes_buffer *exb;
85 exb = calloc(1, sizeof(*exb));
87 return exb;
90 static int excludes_buffer_append(excludes_buffer *exb, buffer *string) {
91 #ifdef HAVE_PCRE_H
92 size_t i;
93 const char *errptr;
94 int erroff;
96 if (!string) return -1;
98 if (exb->size == 0) {
99 exb->size = 4;
100 exb->used = 0;
102 exb->ptr = malloc(exb->size * sizeof(*exb->ptr));
104 for(i = 0; i < exb->size ; i++) {
105 exb->ptr[i] = calloc(1, sizeof(**exb->ptr));
107 } else if (exb->used == exb->size) {
108 exb->size += 4;
110 exb->ptr = realloc(exb->ptr, exb->size * sizeof(*exb->ptr));
112 for(i = exb->used; i < exb->size; i++) {
113 exb->ptr[i] = calloc(1, sizeof(**exb->ptr));
118 if (NULL == (exb->ptr[exb->used]->regex = pcre_compile(string->ptr, 0,
119 &errptr, &erroff, NULL))) {
120 return -1;
123 exb->ptr[exb->used]->string = buffer_init();
124 buffer_copy_buffer(exb->ptr[exb->used]->string, string);
126 exb->used++;
128 return 0;
129 #else
130 UNUSED(exb);
131 UNUSED(string);
133 return -1;
134 #endif
137 static void excludes_buffer_free(excludes_buffer *exb) {
138 #ifdef HAVE_PCRE_H
139 size_t i;
141 for (i = 0; i < exb->size; i++) {
142 if (exb->ptr[i]->regex) pcre_free(exb->ptr[i]->regex);
143 if (exb->ptr[i]->string) buffer_free(exb->ptr[i]->string);
144 free(exb->ptr[i]);
147 if (exb->ptr) free(exb->ptr);
148 #endif
150 free(exb);
153 /* init the plugin data */
154 INIT_FUNC(mod_dirlisting_init) {
155 plugin_data *p;
157 p = calloc(1, sizeof(*p));
159 p->tmp_buf = buffer_init();
160 p->content_charset = buffer_init();
162 return p;
165 /* detroy the plugin data */
166 FREE_FUNC(mod_dirlisting_free) {
167 plugin_data *p = p_d;
169 UNUSED(srv);
171 if (!p) return HANDLER_GO_ON;
173 if (p->config_storage) {
174 size_t i;
175 for (i = 0; i < srv->config_context->used; i++) {
176 plugin_config *s = p->config_storage[i];
178 if (!s) continue;
180 excludes_buffer_free(s->excludes);
181 buffer_free(s->external_css);
182 buffer_free(s->encoding);
183 buffer_free(s->set_footer);
185 free(s);
187 free(p->config_storage);
190 buffer_free(p->tmp_buf);
191 buffer_free(p->content_charset);
193 free(p);
195 return HANDLER_GO_ON;
198 /* handle plugin config and check values */
200 #define CONFIG_EXCLUDE "dir-listing.exclude"
201 #define CONFIG_ACTIVATE "dir-listing.activate"
202 #define CONFIG_HIDE_DOTFILES "dir-listing.hide-dotfiles"
203 #define CONFIG_EXTERNAL_CSS "dir-listing.external-css"
204 #define CONFIG_ENCODING "dir-listing.encoding"
205 #define CONFIG_SHOW_README "dir-listing.show-readme"
206 #define CONFIG_HIDE_README_FILE "dir-listing.hide-readme-file"
207 #define CONFIG_SHOW_HEADER "dir-listing.show-header"
208 #define CONFIG_HIDE_HEADER_FILE "dir-listing.hide-header-file"
209 #define CONFIG_DIR_LISTING "server.dir-listing"
210 #define CONFIG_SET_FOOTER "dir-listing.set-footer"
211 #define CONFIG_ENCODE_README "dir-listing.encode-readme"
212 #define CONFIG_ENCODE_HEADER "dir-listing.encode-header"
213 #define CONFIG_AUTO_LAYOUT "dir-listing.auto-layout"
216 SETDEFAULTS_FUNC(mod_dirlisting_set_defaults) {
217 plugin_data *p = p_d;
218 size_t i = 0;
220 config_values_t cv[] = {
221 { CONFIG_EXCLUDE, NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
222 { CONFIG_ACTIVATE, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
223 { CONFIG_HIDE_DOTFILES, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
224 { CONFIG_EXTERNAL_CSS, NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
225 { CONFIG_ENCODING, NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 4 */
226 { CONFIG_SHOW_README, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 5 */
227 { CONFIG_HIDE_README_FILE, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 6 */
228 { CONFIG_SHOW_HEADER, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 7 */
229 { CONFIG_HIDE_HEADER_FILE, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 8 */
230 { CONFIG_DIR_LISTING, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 9 */
231 { CONFIG_SET_FOOTER, NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 10 */
232 { CONFIG_ENCODE_README, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 11 */
233 { CONFIG_ENCODE_HEADER, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 12 */
234 { CONFIG_AUTO_LAYOUT, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 13 */
236 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
239 if (!p) return HANDLER_ERROR;
241 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
243 for (i = 0; i < srv->config_context->used; i++) {
244 data_config const* config = (data_config const*)srv->config_context->data[i];
245 plugin_config *s;
246 data_unset *du_excludes;
248 s = calloc(1, sizeof(plugin_config));
249 s->excludes = excludes_buffer_init();
250 s->dir_listing = 0;
251 s->external_css = buffer_init();
252 s->hide_dot_files = 1;
253 s->show_readme = 0;
254 s->hide_readme_file = 0;
255 s->show_header = 0;
256 s->hide_header_file = 0;
257 s->encode_readme = 1;
258 s->encode_header = 1;
259 s->auto_layout = 1;
261 s->encoding = buffer_init();
262 s->set_footer = buffer_init();
264 cv[0].destination = s->excludes;
265 cv[1].destination = &(s->dir_listing);
266 cv[2].destination = &(s->hide_dot_files);
267 cv[3].destination = s->external_css;
268 cv[4].destination = s->encoding;
269 cv[5].destination = &(s->show_readme);
270 cv[6].destination = &(s->hide_readme_file);
271 cv[7].destination = &(s->show_header);
272 cv[8].destination = &(s->hide_header_file);
273 cv[9].destination = &(s->dir_listing); /* old name */
274 cv[10].destination = s->set_footer;
275 cv[11].destination = &(s->encode_readme);
276 cv[12].destination = &(s->encode_header);
277 cv[13].destination = &(s->auto_layout);
279 p->config_storage[i] = s;
281 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
282 return HANDLER_ERROR;
285 if (NULL != (du_excludes = array_get_element(config->value, CONFIG_EXCLUDE))) {
286 array *excludes_list;
287 size_t j;
289 if (du_excludes->type != TYPE_ARRAY) {
290 log_error_write(srv, __FILE__, __LINE__, "sss",
291 "unexpected type for key: ", CONFIG_EXCLUDE, "array of strings");
292 return HANDLER_ERROR;
295 excludes_list = ((data_array*)du_excludes)->value;
297 #ifndef HAVE_PCRE_H
298 if (excludes_list->used > 0) {
299 log_error_write(srv, __FILE__, __LINE__, "sss",
300 "pcre support is missing for: ", CONFIG_EXCLUDE, ", please install libpcre and the headers");
301 return HANDLER_ERROR;
303 #else
304 for (j = 0; j < excludes_list->used; j++) {
305 data_unset *du_exclude = excludes_list->data[j];
307 if (du_exclude->type != TYPE_STRING) {
308 log_error_write(srv, __FILE__, __LINE__, "sssbs",
309 "unexpected type for key: ", CONFIG_EXCLUDE, "[",
310 du_exclude->key, "](string)");
311 return HANDLER_ERROR;
314 if (0 != excludes_buffer_append(s->excludes, ((data_string*)(du_exclude))->value)) {
315 log_error_write(srv, __FILE__, __LINE__, "sb",
316 "pcre-compile failed for", ((data_string*)(du_exclude))->value);
317 return HANDLER_ERROR;
320 #endif
324 return HANDLER_GO_ON;
327 #define PATCH(x) \
328 p->conf.x = s->x;
329 static int mod_dirlisting_patch_connection(server *srv, connection *con, plugin_data *p) {
330 size_t i, j;
331 plugin_config *s = p->config_storage[0];
333 PATCH(dir_listing);
334 PATCH(external_css);
335 PATCH(hide_dot_files);
336 PATCH(encoding);
337 PATCH(show_readme);
338 PATCH(hide_readme_file);
339 PATCH(show_header);
340 PATCH(hide_header_file);
341 PATCH(excludes);
342 PATCH(set_footer);
343 PATCH(encode_readme);
344 PATCH(encode_header);
345 PATCH(auto_layout);
347 /* skip the first, the global context */
348 for (i = 1; i < srv->config_context->used; i++) {
349 data_config *dc = (data_config *)srv->config_context->data[i];
350 s = p->config_storage[i];
352 /* condition didn't match */
353 if (!config_check_cond(srv, con, dc)) continue;
355 /* merge config */
356 for (j = 0; j < dc->value->used; j++) {
357 data_unset *du = dc->value->data[j];
359 if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_ACTIVATE)) ||
360 buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_DIR_LISTING))) {
361 PATCH(dir_listing);
362 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_HIDE_DOTFILES))) {
363 PATCH(hide_dot_files);
364 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_EXTERNAL_CSS))) {
365 PATCH(external_css);
366 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_ENCODING))) {
367 PATCH(encoding);
368 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_SHOW_README))) {
369 PATCH(show_readme);
370 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_HIDE_README_FILE))) {
371 PATCH(hide_readme_file);
372 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_SHOW_HEADER))) {
373 PATCH(show_header);
374 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_HIDE_HEADER_FILE))) {
375 PATCH(hide_header_file);
376 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_SET_FOOTER))) {
377 PATCH(set_footer);
378 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_EXCLUDE))) {
379 PATCH(excludes);
380 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_ENCODE_README))) {
381 PATCH(encode_readme);
382 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_ENCODE_HEADER))) {
383 PATCH(encode_header);
384 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_AUTO_LAYOUT))) {
385 PATCH(auto_layout);
390 return 0;
392 #undef PATCH
394 typedef struct {
395 size_t namelen;
396 time_t mtime;
397 off_t size;
398 } dirls_entry_t;
400 typedef struct {
401 dirls_entry_t **ent;
402 size_t used;
403 size_t size;
404 } dirls_list_t;
406 #define DIRLIST_ENT_NAME(ent) ((char*)(ent) + sizeof(dirls_entry_t))
407 #define DIRLIST_BLOB_SIZE 16
409 /* simple combsort algorithm */
410 static void http_dirls_sort(dirls_entry_t **ent, int num) {
411 int gap = num;
412 int i, j;
413 int swapped;
414 dirls_entry_t *tmp;
416 do {
417 gap = (gap * 10) / 13;
418 if (gap == 9 || gap == 10)
419 gap = 11;
420 if (gap < 1)
421 gap = 1;
422 swapped = 0;
424 for (i = 0; i < num - gap; i++) {
425 j = i + gap;
426 if (strcmp(DIRLIST_ENT_NAME(ent[i]), DIRLIST_ENT_NAME(ent[j])) > 0) {
427 tmp = ent[i];
428 ent[i] = ent[j];
429 ent[j] = tmp;
430 swapped = 1;
434 } while (gap > 1 || swapped);
437 /* buffer must be able to hold "999.9K"
438 * conversion is simple but not perfect
440 static int http_list_directory_sizefmt(char *buf, size_t bufsz, off_t size) {
441 const char unit[] = "KMGTPE"; /* Kilo, Mega, Tera, Peta, Exa */
442 const char *u = unit - 1; /* u will always increment at least once */
443 int remain;
444 size_t buflen;
446 if (size < 100)
447 size += 99;
448 if (size < 100)
449 size = 0;
451 while (1) {
452 remain = (int) size & 1023;
453 size >>= 10;
454 u++;
455 if ((size & (~0 ^ 1023)) == 0)
456 break;
459 remain /= 100;
460 if (remain > 9)
461 remain = 9;
462 if (size > 999) {
463 size = 0;
464 remain = 9;
465 u++;
468 li_itostrn(buf, bufsz, size);
469 buflen = strlen(buf);
470 if (buflen + 3 >= bufsz) return buflen;
471 buf[buflen+0] = '.';
472 buf[buflen+1] = remain + '0';
473 buf[buflen+2] = *u;
474 buf[buflen+3] = '\0';
476 return buflen + 3;
479 static void http_list_directory_header(server *srv, connection *con, plugin_data *p, buffer *out) {
480 UNUSED(srv);
482 if (p->conf.auto_layout) {
483 buffer_append_string_len(out, CONST_STR_LEN(
484 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
485 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n"
486 "<head>\n"
487 "<title>Index of "
489 buffer_append_string_encoded(out, CONST_BUF_LEN(con->uri.path), ENCODING_MINIMAL_XML);
490 buffer_append_string_len(out, CONST_STR_LEN("</title>\n"));
492 if (!buffer_string_is_empty(p->conf.external_css)) {
493 buffer_append_string_len(out, CONST_STR_LEN("<link rel=\"stylesheet\" type=\"text/css\" href=\""));
494 buffer_append_string_buffer(out, p->conf.external_css);
495 buffer_append_string_len(out, CONST_STR_LEN("\" />\n"));
496 } else {
497 buffer_append_string_len(out, CONST_STR_LEN(
498 "<style type=\"text/css\">\n"
499 "a, a:active {text-decoration: none; color: blue;}\n"
500 "a:visited {color: #48468F;}\n"
501 "a:hover, a:focus {text-decoration: underline; color: red;}\n"
502 "body {background-color: #F5F5F5;}\n"
503 "h2 {margin-bottom: 12px;}\n"
504 "table {margin-left: 12px;}\n"
505 "th, td {"
506 " font: 90% monospace;"
507 " text-align: left;"
508 "}\n"
509 "th {"
510 " font-weight: bold;"
511 " padding-right: 14px;"
512 " padding-bottom: 3px;"
513 "}\n"
514 "td {padding-right: 14px;}\n"
515 "td.s, th.s {text-align: right;}\n"
516 "div.list {"
517 " background-color: white;"
518 " border-top: 1px solid #646464;"
519 " border-bottom: 1px solid #646464;"
520 " padding-top: 10px;"
521 " padding-bottom: 14px;"
522 "}\n"
523 "div.foot {"
524 " font: 90% monospace;"
525 " color: #787878;"
526 " padding-top: 4px;"
527 "}\n"
528 "</style>\n"
532 buffer_append_string_len(out, CONST_STR_LEN("</head>\n<body>\n"));
535 /* HEADER.txt */
536 if (p->conf.show_header) {
537 stream s;
538 /* if we have a HEADER file, display it in <pre class="header"></pre> */
540 buffer_copy_buffer(p->tmp_buf, con->physical.path);
541 buffer_append_slash(p->tmp_buf);
542 buffer_append_string_len(p->tmp_buf, CONST_STR_LEN("HEADER.txt"));
544 if (-1 != stream_open(&s, p->tmp_buf)) {
545 if (p->conf.encode_header) {
546 buffer_append_string_len(out, CONST_STR_LEN("<pre class=\"header\">"));
547 buffer_append_string_encoded(out, s.start, s.size, ENCODING_MINIMAL_XML);
548 buffer_append_string_len(out, CONST_STR_LEN("</pre>"));
549 } else {
550 buffer_append_string_len(out, s.start, s.size);
553 stream_close(&s);
556 buffer_append_string_len(out, CONST_STR_LEN("<h2>Index of "));
557 buffer_append_string_encoded(out, CONST_BUF_LEN(con->uri.path), ENCODING_MINIMAL_XML);
558 buffer_append_string_len(out, CONST_STR_LEN(
559 "</h2>\n"
560 "<div class=\"list\">\n"
561 "<table summary=\"Directory Listing\" cellpadding=\"0\" cellspacing=\"0\">\n"
562 "<thead>"
563 "<tr>"
564 "<th class=\"n\">Name</th>"
565 "<th class=\"m\">Last Modified</th>"
566 "<th class=\"s\">Size</th>"
567 "<th class=\"t\">Type</th>"
568 "</tr>"
569 "</thead>\n"
570 "<tbody>\n"
571 "<tr class=\"d\">"
572 "<td class=\"n\"><a href=\"../\">Parent Directory</a>/</td>"
573 "<td class=\"m\">&nbsp;</td>"
574 "<td class=\"s\">- &nbsp;</td>"
575 "<td class=\"t\">Directory</td>"
576 "</tr>\n"
580 static void http_list_directory_footer(server *srv, connection *con, plugin_data *p, buffer *out) {
581 UNUSED(srv);
583 buffer_append_string_len(out, CONST_STR_LEN(
584 "</tbody>\n"
585 "</table>\n"
586 "</div>\n"
589 if (p->conf.show_readme) {
590 stream s;
591 /* if we have a README file, display it in <pre class="readme"></pre> */
593 buffer_copy_buffer(p->tmp_buf, con->physical.path);
594 buffer_append_slash(p->tmp_buf);
595 buffer_append_string_len(p->tmp_buf, CONST_STR_LEN("README.txt"));
597 if (-1 != stream_open(&s, p->tmp_buf)) {
598 if (p->conf.encode_readme) {
599 buffer_append_string_len(out, CONST_STR_LEN("<pre class=\"readme\">"));
600 buffer_append_string_encoded(out, s.start, s.size, ENCODING_MINIMAL_XML);
601 buffer_append_string_len(out, CONST_STR_LEN("</pre>"));
602 } else {
603 buffer_append_string_len(out, s.start, s.size);
606 stream_close(&s);
609 if(p->conf.auto_layout) {
610 buffer_append_string_len(out, CONST_STR_LEN(
611 "<div class=\"foot\">"
614 if (!buffer_string_is_empty(p->conf.set_footer)) {
615 buffer_append_string_buffer(out, p->conf.set_footer);
616 } else if (buffer_is_empty(con->conf.server_tag)) {
617 buffer_append_string_len(out, CONST_STR_LEN(PACKAGE_DESC));
618 } else {
619 buffer_append_string_buffer(out, con->conf.server_tag);
622 buffer_append_string_len(out, CONST_STR_LEN(
623 "</div>\n"
624 "</body>\n"
625 "</html>\n"
630 static int http_list_directory(server *srv, connection *con, plugin_data *p, buffer *dir) {
631 DIR *dp;
632 buffer *out;
633 struct dirent *dent;
634 struct stat st;
635 char *path, *path_file;
636 size_t i;
637 int hide_dotfiles = p->conf.hide_dot_files;
638 dirls_list_t dirs, files, *list;
639 dirls_entry_t *tmp;
640 char sizebuf[sizeof("999.9K")];
641 char datebuf[sizeof("2005-Jan-01 22:23:24")];
642 size_t k;
643 const char *content_type;
644 long name_max;
645 #if defined(HAVE_XATTR) || defined(HAVE_EXTATTR)
646 char attrval[128];
647 int attrlen;
648 #endif
649 #ifdef HAVE_LOCALTIME_R
650 struct tm tm;
651 #endif
653 if (buffer_string_is_empty(dir)) return -1;
655 i = buffer_string_length(dir);
657 #ifdef HAVE_PATHCONF
658 if (0 >= (name_max = pathconf(dir->ptr, _PC_NAME_MAX))) {
659 /* some broken fs (fuse) return 0 instead of -1 */
660 #ifdef NAME_MAX
661 name_max = NAME_MAX;
662 #else
663 name_max = 255; /* stupid default */
664 #endif
666 #elif defined __WIN32
667 name_max = FILENAME_MAX;
668 #else
669 name_max = NAME_MAX;
670 #endif
672 path = malloc(buffer_string_length(dir) + name_max + 1);
673 force_assert(NULL != path);
674 strcpy(path, dir->ptr);
675 path_file = path + i;
677 if (NULL == (dp = opendir(path))) {
678 log_error_write(srv, __FILE__, __LINE__, "sbs",
679 "opendir failed:", dir, strerror(errno));
681 free(path);
682 return -1;
685 dirs.ent = (dirls_entry_t**) malloc(sizeof(dirls_entry_t*) * DIRLIST_BLOB_SIZE);
686 force_assert(dirs.ent);
687 dirs.size = DIRLIST_BLOB_SIZE;
688 dirs.used = 0;
689 files.ent = (dirls_entry_t**) malloc(sizeof(dirls_entry_t*) * DIRLIST_BLOB_SIZE);
690 force_assert(files.ent);
691 files.size = DIRLIST_BLOB_SIZE;
692 files.used = 0;
694 while ((dent = readdir(dp)) != NULL) {
695 unsigned short exclude_match = 0;
697 if (dent->d_name[0] == '.') {
698 if (hide_dotfiles)
699 continue;
700 if (dent->d_name[1] == '\0')
701 continue;
702 if (dent->d_name[1] == '.' && dent->d_name[2] == '\0')
703 continue;
706 if (p->conf.hide_readme_file) {
707 if (strcmp(dent->d_name, "README.txt") == 0)
708 continue;
710 if (p->conf.hide_header_file) {
711 if (strcmp(dent->d_name, "HEADER.txt") == 0)
712 continue;
715 /* compare d_name against excludes array
716 * elements, skipping any that match.
718 #ifdef HAVE_PCRE_H
719 for(i = 0; i < p->conf.excludes->used; i++) {
720 int n;
721 #define N 10
722 int ovec[N * 3];
723 pcre *regex = p->conf.excludes->ptr[i]->regex;
725 if ((n = pcre_exec(regex, NULL, dent->d_name,
726 strlen(dent->d_name), 0, 0, ovec, 3 * N)) < 0) {
727 if (n != PCRE_ERROR_NOMATCH) {
728 log_error_write(srv, __FILE__, __LINE__, "sd",
729 "execution error while matching:", n);
731 /* aborting would require a lot of manual cleanup here.
732 * skip instead (to not leak names that break pcre matching)
734 exclude_match = 1;
735 break;
738 else {
739 exclude_match = 1;
740 break;
744 if (exclude_match) {
745 continue;
747 #endif
749 i = strlen(dent->d_name);
751 /* NOTE: the manual says, d_name is never more than NAME_MAX
752 * so this should actually not be a buffer-overflow-risk
754 if (i > (size_t)name_max) continue;
756 memcpy(path_file, dent->d_name, i + 1);
757 if (stat(path, &st) != 0)
758 continue;
760 list = &files;
761 if (S_ISDIR(st.st_mode))
762 list = &dirs;
764 if (list->used == list->size) {
765 list->size += DIRLIST_BLOB_SIZE;
766 list->ent = (dirls_entry_t**) realloc(list->ent, sizeof(dirls_entry_t*) * list->size);
767 force_assert(list->ent);
770 tmp = (dirls_entry_t*) malloc(sizeof(dirls_entry_t) + 1 + i);
771 tmp->mtime = st.st_mtime;
772 tmp->size = st.st_size;
773 tmp->namelen = i;
774 memcpy(DIRLIST_ENT_NAME(tmp), dent->d_name, i + 1);
776 list->ent[list->used++] = tmp;
778 closedir(dp);
780 if (dirs.used) http_dirls_sort(dirs.ent, dirs.used);
782 if (files.used) http_dirls_sort(files.ent, files.used);
784 out = buffer_init();
785 buffer_copy_string_len(out, CONST_STR_LEN("<?xml version=\"1.0\" encoding=\""));
786 if (buffer_string_is_empty(p->conf.encoding)) {
787 buffer_append_string_len(out, CONST_STR_LEN("iso-8859-1"));
788 } else {
789 buffer_append_string_buffer(out, p->conf.encoding);
791 buffer_append_string_len(out, CONST_STR_LEN("\"?>\n"));
792 http_list_directory_header(srv, con, p, out);
794 /* directories */
795 for (i = 0; i < dirs.used; i++) {
796 tmp = dirs.ent[i];
798 #ifdef HAVE_LOCALTIME_R
799 localtime_r(&(tmp->mtime), &tm);
800 strftime(datebuf, sizeof(datebuf), "%Y-%b-%d %H:%M:%S", &tm);
801 #else
802 strftime(datebuf, sizeof(datebuf), "%Y-%b-%d %H:%M:%S", localtime(&(tmp->mtime)));
803 #endif
805 buffer_append_string_len(out, CONST_STR_LEN("<tr class=\"d\"><td class=\"n\"><a href=\""));
806 buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_REL_URI_PART);
807 buffer_append_string_len(out, CONST_STR_LEN("/\">"));
808 buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_MINIMAL_XML);
809 buffer_append_string_len(out, CONST_STR_LEN("</a>/</td><td class=\"m\">"));
810 buffer_append_string_len(out, datebuf, sizeof(datebuf) - 1);
811 buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"s\">- &nbsp;</td><td class=\"t\">Directory</td></tr>\n"));
813 free(tmp);
816 /* files */
817 for (i = 0; i < files.used; i++) {
818 tmp = files.ent[i];
820 content_type = NULL;
821 #if defined(HAVE_XATTR)
822 if (con->conf.use_xattr) {
823 memcpy(path_file, DIRLIST_ENT_NAME(tmp), tmp->namelen + 1);
824 attrlen = sizeof(attrval) - 1;
825 if (attr_get(path, srv->srvconf.xattr_name->ptr, attrval, &attrlen, 0) == 0) {
826 attrval[attrlen] = '\0';
827 content_type = attrval;
830 #elif defined(HAVE_EXTATTR)
831 if (con->conf.use_xattr) {
832 memcpy(path_file, DIRLIST_ENT_NAME(tmp), tmp->namelen + 1);
833 if(-1 != (attrlen = extattr_get_file(path, EXTATTR_NAMESPACE_USER, srv->srvconf.xattr_name->ptr, attrval, sizeof(attrval)-1))) {
834 attrval[attrlen] = '\0';
835 content_type = attrval;
838 #endif
840 if (content_type == NULL) {
841 content_type = "application/octet-stream";
842 for (k = 0; k < con->conf.mimetypes->used; k++) {
843 data_string *ds = (data_string *)con->conf.mimetypes->data[k];
844 size_t ct_len;
846 if (buffer_is_empty(ds->key))
847 continue;
849 ct_len = buffer_string_length(ds->key);
850 if (tmp->namelen < ct_len)
851 continue;
853 if (0 == strncasecmp(DIRLIST_ENT_NAME(tmp) + tmp->namelen - ct_len, ds->key->ptr, ct_len)) {
854 content_type = ds->value->ptr;
855 break;
860 #ifdef HAVE_LOCALTIME_R
861 localtime_r(&(tmp->mtime), &tm);
862 strftime(datebuf, sizeof(datebuf), "%Y-%b-%d %H:%M:%S", &tm);
863 #else
864 strftime(datebuf, sizeof(datebuf), "%Y-%b-%d %H:%M:%S", localtime(&(tmp->mtime)));
865 #endif
866 http_list_directory_sizefmt(sizebuf, sizeof(sizebuf), tmp->size);
868 buffer_append_string_len(out, CONST_STR_LEN("<tr><td class=\"n\"><a href=\""));
869 buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_REL_URI_PART);
870 buffer_append_string_len(out, CONST_STR_LEN("\">"));
871 buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_MINIMAL_XML);
872 buffer_append_string_len(out, CONST_STR_LEN("</a></td><td class=\"m\">"));
873 buffer_append_string_len(out, datebuf, sizeof(datebuf) - 1);
874 buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"s\">"));
875 buffer_append_string(out, sizebuf);
876 buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"t\">"));
877 buffer_append_string(out, content_type);
878 buffer_append_string_len(out, CONST_STR_LEN("</td></tr>\n"));
880 free(tmp);
883 free(files.ent);
884 free(dirs.ent);
885 free(path);
887 http_list_directory_footer(srv, con, p, out);
889 /* Insert possible charset to Content-Type */
890 if (buffer_string_is_empty(p->conf.encoding)) {
891 response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html"));
892 } else {
893 buffer_copy_string_len(p->content_charset, CONST_STR_LEN("text/html; charset="));
894 buffer_append_string_buffer(p->content_charset, p->conf.encoding);
895 response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(p->content_charset));
898 con->file_finished = 1;
899 chunkqueue_append_buffer(con->write_queue, out);
900 buffer_free(out);
902 return 0;
907 URIHANDLER_FUNC(mod_dirlisting_subrequest) {
908 plugin_data *p = p_d;
909 stat_cache_entry *sce = NULL;
911 UNUSED(srv);
913 /* we only handle GET, POST and HEAD */
914 switch(con->request.http_method) {
915 case HTTP_METHOD_GET:
916 case HTTP_METHOD_POST:
917 case HTTP_METHOD_HEAD:
918 break;
919 default:
920 return HANDLER_GO_ON;
923 if (con->mode != DIRECT) return HANDLER_GO_ON;
925 if (buffer_is_empty(con->physical.path)) return HANDLER_GO_ON;
926 if (buffer_is_empty(con->uri.path)) return HANDLER_GO_ON;
927 if (con->uri.path->ptr[buffer_string_length(con->uri.path) - 1] != '/') return HANDLER_GO_ON;
929 mod_dirlisting_patch_connection(srv, con, p);
931 if (!p->conf.dir_listing) return HANDLER_GO_ON;
933 if (con->conf.log_request_handling) {
934 log_error_write(srv, __FILE__, __LINE__, "s", "-- handling the request as Dir-Listing");
935 log_error_write(srv, __FILE__, __LINE__, "sb", "URI :", con->uri.path);
938 if (HANDLER_ERROR == stat_cache_get_entry(srv, con, con->physical.path, &sce)) {
939 log_error_write(srv, __FILE__, __LINE__, "SB", "stat_cache_get_entry failed: ", con->physical.path);
940 SEGFAULT();
943 if (!S_ISDIR(sce->st.st_mode)) return HANDLER_GO_ON;
945 if (http_list_directory(srv, con, p, con->physical.path)) {
946 /* dirlisting failed */
947 con->http_status = 403;
950 buffer_reset(con->physical.path);
952 /* not found */
953 return HANDLER_FINISHED;
956 /* this function is called at dlopen() time and inits the callbacks */
958 int mod_dirlisting_plugin_init(plugin *p);
959 int mod_dirlisting_plugin_init(plugin *p) {
960 p->version = LIGHTTPD_VERSION_ID;
961 p->name = buffer_init_string("dirlisting");
963 p->init = mod_dirlisting_init;
964 p->handle_subrequest_start = mod_dirlisting_subrequest;
965 p->set_defaults = mod_dirlisting_set_defaults;
966 p->cleanup = mod_dirlisting_free;
968 p->data = NULL;
970 return 0;