fix errors detected by Coverity Scan
[lighttpd.git] / src / mod_dirlisting.c
blobb65dbbaca5a763759b5cd50a5d00dcd3d740b3eb
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);
507 close(fd);
509 if (encode) {
510 buffer_append_string_len(out, CONST_STR_LEN("</pre>"));
514 static void http_list_directory_header(server *srv, connection *con, plugin_data *p, buffer *out) {
515 UNUSED(srv);
517 if (p->conf.auto_layout) {
518 buffer_append_string_len(out, CONST_STR_LEN(
519 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
520 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n"
521 "<head>\n"
522 "<title>Index of "
524 buffer_append_string_encoded(out, CONST_BUF_LEN(con->uri.path), ENCODING_MINIMAL_XML);
525 buffer_append_string_len(out, CONST_STR_LEN("</title>\n"));
527 if (!buffer_string_is_empty(p->conf.external_css)) {
528 buffer_append_string_len(out, CONST_STR_LEN("<link rel=\"stylesheet\" type=\"text/css\" href=\""));
529 buffer_append_string_buffer(out, p->conf.external_css);
530 buffer_append_string_len(out, CONST_STR_LEN("\" />\n"));
531 } else {
532 buffer_append_string_len(out, CONST_STR_LEN(
533 "<style type=\"text/css\">\n"
534 "a, a:active {text-decoration: none; color: blue;}\n"
535 "a:visited {color: #48468F;}\n"
536 "a:hover, a:focus {text-decoration: underline; color: red;}\n"
537 "body {background-color: #F5F5F5;}\n"
538 "h2 {margin-bottom: 12px;}\n"
539 "table {margin-left: 12px;}\n"
540 "th, td {"
541 " font: 90% monospace;"
542 " text-align: left;"
543 "}\n"
544 "th {"
545 " font-weight: bold;"
546 " padding-right: 14px;"
547 " padding-bottom: 3px;"
548 "}\n"
549 "td {padding-right: 14px;}\n"
550 "td.s, th.s {text-align: right;}\n"
551 "div.list {"
552 " background-color: white;"
553 " border-top: 1px solid #646464;"
554 " border-bottom: 1px solid #646464;"
555 " padding-top: 10px;"
556 " padding-bottom: 14px;"
557 "}\n"
558 "div.foot {"
559 " font: 90% monospace;"
560 " color: #787878;"
561 " padding-top: 4px;"
562 "}\n"
563 "</style>\n"
567 buffer_append_string_len(out, CONST_STR_LEN("</head>\n<body>\n"));
570 /* HEADER.txt */
571 if (p->conf.show_header) {
572 /* if we have a HEADER file, display it in <pre class="header"></pre> */
574 buffer_copy_buffer(p->tmp_buf, con->physical.path);
575 buffer_append_slash(p->tmp_buf);
576 buffer_append_string_len(p->tmp_buf, CONST_STR_LEN("HEADER.txt"));
578 http_list_directory_include_file(out, p->tmp_buf, "header", p->conf.encode_header);
581 buffer_append_string_len(out, CONST_STR_LEN("<h2>Index of "));
582 buffer_append_string_encoded(out, CONST_BUF_LEN(con->uri.path), ENCODING_MINIMAL_XML);
583 buffer_append_string_len(out, CONST_STR_LEN(
584 "</h2>\n"
585 "<div class=\"list\">\n"
586 "<table summary=\"Directory Listing\" cellpadding=\"0\" cellspacing=\"0\">\n"
587 "<thead>"
588 "<tr>"
589 "<th class=\"n\">Name</th>"
590 "<th class=\"m\">Last Modified</th>"
591 "<th class=\"s\">Size</th>"
592 "<th class=\"t\">Type</th>"
593 "</tr>"
594 "</thead>\n"
595 "<tbody>\n"
596 "<tr class=\"d\">"
597 "<td class=\"n\"><a href=\"../\">Parent Directory</a>/</td>"
598 "<td class=\"m\">&nbsp;</td>"
599 "<td class=\"s\">- &nbsp;</td>"
600 "<td class=\"t\">Directory</td>"
601 "</tr>\n"
605 static void http_list_directory_footer(server *srv, connection *con, plugin_data *p, buffer *out) {
606 UNUSED(srv);
608 buffer_append_string_len(out, CONST_STR_LEN(
609 "</tbody>\n"
610 "</table>\n"
611 "</div>\n"
614 if (p->conf.show_readme) {
615 /* if we have a README file, display it in <pre class="readme"></pre> */
617 buffer_copy_buffer(p->tmp_buf, con->physical.path);
618 buffer_append_slash(p->tmp_buf);
619 buffer_append_string_len(p->tmp_buf, CONST_STR_LEN("README.txt"));
621 http_list_directory_include_file(out, p->tmp_buf, "readme", p->conf.encode_readme);
624 if(p->conf.auto_layout) {
625 buffer_append_string_len(out, CONST_STR_LEN(
626 "<div class=\"foot\">"
629 if (!buffer_string_is_empty(p->conf.set_footer)) {
630 buffer_append_string_buffer(out, p->conf.set_footer);
631 } else if (buffer_is_empty(con->conf.server_tag)) {
632 buffer_append_string_len(out, CONST_STR_LEN(PACKAGE_DESC));
633 } else {
634 buffer_append_string_buffer(out, con->conf.server_tag);
637 buffer_append_string_len(out, CONST_STR_LEN(
638 "</div>\n"
639 "</body>\n"
640 "</html>\n"
645 static int http_list_directory(server *srv, connection *con, plugin_data *p, buffer *dir) {
646 DIR *dp;
647 buffer *out;
648 struct dirent *dent;
649 struct stat st;
650 char *path, *path_file;
651 size_t i;
652 int hide_dotfiles = p->conf.hide_dot_files;
653 dirls_list_t dirs, files, *list;
654 dirls_entry_t *tmp;
655 char sizebuf[sizeof("999.9K")];
656 char datebuf[sizeof("2005-Jan-01 22:23:24")];
657 size_t k;
658 const char *content_type;
659 long name_max;
660 #if defined(HAVE_XATTR) || defined(HAVE_EXTATTR)
661 char attrval[128];
662 int attrlen;
663 #endif
664 #ifdef HAVE_LOCALTIME_R
665 struct tm tm;
666 #endif
668 if (buffer_string_is_empty(dir)) return -1;
670 i = buffer_string_length(dir);
672 #ifdef HAVE_PATHCONF
673 if (0 >= (name_max = pathconf(dir->ptr, _PC_NAME_MAX))) {
674 /* some broken fs (fuse) return 0 instead of -1 */
675 #ifdef NAME_MAX
676 name_max = NAME_MAX;
677 #else
678 name_max = 255; /* stupid default */
679 #endif
681 #elif defined __WIN32
682 name_max = FILENAME_MAX;
683 #else
684 name_max = NAME_MAX;
685 #endif
687 path = malloc(i + name_max + 1);
688 force_assert(NULL != path);
689 memcpy(path, dir->ptr, i+1);
690 path_file = path + i;
692 if (NULL == (dp = opendir(path))) {
693 log_error_write(srv, __FILE__, __LINE__, "sbs",
694 "opendir failed:", dir, strerror(errno));
696 free(path);
697 return -1;
700 dirs.ent = (dirls_entry_t**) malloc(sizeof(dirls_entry_t*) * DIRLIST_BLOB_SIZE);
701 force_assert(dirs.ent);
702 dirs.size = DIRLIST_BLOB_SIZE;
703 dirs.used = 0;
704 files.ent = (dirls_entry_t**) malloc(sizeof(dirls_entry_t*) * DIRLIST_BLOB_SIZE);
705 force_assert(files.ent);
706 files.size = DIRLIST_BLOB_SIZE;
707 files.used = 0;
709 while ((dent = readdir(dp)) != NULL) {
710 unsigned short exclude_match = 0;
712 if (dent->d_name[0] == '.') {
713 if (hide_dotfiles)
714 continue;
715 if (dent->d_name[1] == '\0')
716 continue;
717 if (dent->d_name[1] == '.' && dent->d_name[2] == '\0')
718 continue;
721 if (p->conf.hide_readme_file) {
722 if (strcmp(dent->d_name, "README.txt") == 0)
723 continue;
725 if (p->conf.hide_header_file) {
726 if (strcmp(dent->d_name, "HEADER.txt") == 0)
727 continue;
730 /* compare d_name against excludes array
731 * elements, skipping any that match.
733 #ifdef HAVE_PCRE_H
734 for(i = 0; i < p->conf.excludes->used; i++) {
735 int n;
736 #define N 10
737 int ovec[N * 3];
738 pcre *regex = p->conf.excludes->ptr[i]->regex;
740 if ((n = pcre_exec(regex, NULL, dent->d_name,
741 strlen(dent->d_name), 0, 0, ovec, 3 * N)) < 0) {
742 if (n != PCRE_ERROR_NOMATCH) {
743 log_error_write(srv, __FILE__, __LINE__, "sd",
744 "execution error while matching:", n);
746 /* aborting would require a lot of manual cleanup here.
747 * skip instead (to not leak names that break pcre matching)
749 exclude_match = 1;
750 break;
753 else {
754 exclude_match = 1;
755 break;
759 if (exclude_match) {
760 continue;
762 #endif
764 i = strlen(dent->d_name);
766 /* NOTE: the manual says, d_name is never more than NAME_MAX
767 * so this should actually not be a buffer-overflow-risk
769 if (i > (size_t)name_max) continue;
771 memcpy(path_file, dent->d_name, i + 1);
772 if (stat(path, &st) != 0)
773 continue;
775 list = &files;
776 if (S_ISDIR(st.st_mode))
777 list = &dirs;
779 if (list->used == list->size) {
780 list->size += DIRLIST_BLOB_SIZE;
781 list->ent = (dirls_entry_t**) realloc(list->ent, sizeof(dirls_entry_t*) * list->size);
782 force_assert(list->ent);
785 tmp = (dirls_entry_t*) malloc(sizeof(dirls_entry_t) + 1 + i);
786 tmp->mtime = st.st_mtime;
787 tmp->size = st.st_size;
788 tmp->namelen = i;
789 memcpy(DIRLIST_ENT_NAME(tmp), dent->d_name, i + 1);
791 list->ent[list->used++] = tmp;
793 closedir(dp);
795 if (dirs.used) http_dirls_sort(dirs.ent, dirs.used);
797 if (files.used) http_dirls_sort(files.ent, files.used);
799 out = buffer_init();
800 buffer_copy_string_len(out, CONST_STR_LEN("<?xml version=\"1.0\" encoding=\""));
801 if (buffer_string_is_empty(p->conf.encoding)) {
802 buffer_append_string_len(out, CONST_STR_LEN("iso-8859-1"));
803 } else {
804 buffer_append_string_buffer(out, p->conf.encoding);
806 buffer_append_string_len(out, CONST_STR_LEN("\"?>\n"));
807 http_list_directory_header(srv, con, p, out);
809 /* directories */
810 for (i = 0; i < dirs.used; i++) {
811 tmp = dirs.ent[i];
813 #ifdef HAVE_LOCALTIME_R
814 localtime_r(&(tmp->mtime), &tm);
815 strftime(datebuf, sizeof(datebuf), "%Y-%b-%d %H:%M:%S", &tm);
816 #else
817 strftime(datebuf, sizeof(datebuf), "%Y-%b-%d %H:%M:%S", localtime(&(tmp->mtime)));
818 #endif
820 buffer_append_string_len(out, CONST_STR_LEN("<tr class=\"d\"><td class=\"n\"><a href=\""));
821 buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_REL_URI_PART);
822 buffer_append_string_len(out, CONST_STR_LEN("/\">"));
823 buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_MINIMAL_XML);
824 buffer_append_string_len(out, CONST_STR_LEN("</a>/</td><td class=\"m\">"));
825 buffer_append_string_len(out, datebuf, sizeof(datebuf) - 1);
826 buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"s\">- &nbsp;</td><td class=\"t\">Directory</td></tr>\n"));
828 free(tmp);
831 /* files */
832 for (i = 0; i < files.used; i++) {
833 tmp = files.ent[i];
835 content_type = NULL;
836 #if defined(HAVE_XATTR)
837 if (con->conf.use_xattr) {
838 memcpy(path_file, DIRLIST_ENT_NAME(tmp), tmp->namelen + 1);
839 attrlen = sizeof(attrval) - 1;
840 if (attr_get(path, srv->srvconf.xattr_name->ptr, attrval, &attrlen, 0) == 0) {
841 attrval[attrlen] = '\0';
842 content_type = attrval;
845 #elif defined(HAVE_EXTATTR)
846 if (con->conf.use_xattr) {
847 memcpy(path_file, DIRLIST_ENT_NAME(tmp), tmp->namelen + 1);
848 if(-1 != (attrlen = extattr_get_file(path, EXTATTR_NAMESPACE_USER, srv->srvconf.xattr_name->ptr, attrval, sizeof(attrval)-1))) {
849 attrval[attrlen] = '\0';
850 content_type = attrval;
853 #endif
855 if (content_type == NULL) {
856 content_type = "application/octet-stream";
857 for (k = 0; k < con->conf.mimetypes->used; k++) {
858 data_string *ds = (data_string *)con->conf.mimetypes->data[k];
859 size_t ct_len;
861 if (buffer_is_empty(ds->key))
862 continue;
864 ct_len = buffer_string_length(ds->key);
865 if (tmp->namelen < ct_len)
866 continue;
868 if (0 == strncasecmp(DIRLIST_ENT_NAME(tmp) + tmp->namelen - ct_len, ds->key->ptr, ct_len)) {
869 content_type = ds->value->ptr;
870 break;
875 #ifdef HAVE_LOCALTIME_R
876 localtime_r(&(tmp->mtime), &tm);
877 strftime(datebuf, sizeof(datebuf), "%Y-%b-%d %H:%M:%S", &tm);
878 #else
879 strftime(datebuf, sizeof(datebuf), "%Y-%b-%d %H:%M:%S", localtime(&(tmp->mtime)));
880 #endif
881 http_list_directory_sizefmt(sizebuf, sizeof(sizebuf), tmp->size);
883 buffer_append_string_len(out, CONST_STR_LEN("<tr><td class=\"n\"><a href=\""));
884 buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_REL_URI_PART);
885 buffer_append_string_len(out, CONST_STR_LEN("\">"));
886 buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_MINIMAL_XML);
887 buffer_append_string_len(out, CONST_STR_LEN("</a></td><td class=\"m\">"));
888 buffer_append_string_len(out, datebuf, sizeof(datebuf) - 1);
889 buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"s\">"));
890 buffer_append_string(out, sizebuf);
891 buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"t\">"));
892 buffer_append_string(out, content_type);
893 buffer_append_string_len(out, CONST_STR_LEN("</td></tr>\n"));
895 free(tmp);
898 free(files.ent);
899 free(dirs.ent);
900 free(path);
902 http_list_directory_footer(srv, con, p, out);
904 /* Insert possible charset to Content-Type */
905 if (buffer_string_is_empty(p->conf.encoding)) {
906 response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html"));
907 } else {
908 buffer_copy_string_len(p->content_charset, CONST_STR_LEN("text/html; charset="));
909 buffer_append_string_buffer(p->content_charset, p->conf.encoding);
910 response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(p->content_charset));
913 con->file_finished = 1;
914 chunkqueue_append_buffer(con->write_queue, out);
915 buffer_free(out);
917 return 0;
922 URIHANDLER_FUNC(mod_dirlisting_subrequest) {
923 plugin_data *p = p_d;
924 stat_cache_entry *sce = NULL;
926 UNUSED(srv);
928 /* we only handle GET, POST and HEAD */
929 switch(con->request.http_method) {
930 case HTTP_METHOD_GET:
931 case HTTP_METHOD_POST:
932 case HTTP_METHOD_HEAD:
933 break;
934 default:
935 return HANDLER_GO_ON;
938 if (con->mode != DIRECT) return HANDLER_GO_ON;
940 if (buffer_is_empty(con->physical.path)) return HANDLER_GO_ON;
941 if (buffer_is_empty(con->uri.path)) return HANDLER_GO_ON;
942 if (con->uri.path->ptr[buffer_string_length(con->uri.path) - 1] != '/') return HANDLER_GO_ON;
944 mod_dirlisting_patch_connection(srv, con, p);
946 if (!p->conf.dir_listing) return HANDLER_GO_ON;
948 if (con->conf.log_request_handling) {
949 log_error_write(srv, __FILE__, __LINE__, "s", "-- handling the request as Dir-Listing");
950 log_error_write(srv, __FILE__, __LINE__, "sb", "URI :", con->uri.path);
953 if (HANDLER_ERROR == stat_cache_get_entry(srv, con, con->physical.path, &sce)) {
954 log_error_write(srv, __FILE__, __LINE__, "SB", "stat_cache_get_entry failed: ", con->physical.path);
955 SEGFAULT();
958 if (!S_ISDIR(sce->st.st_mode)) return HANDLER_GO_ON;
960 if (http_list_directory(srv, con, p, con->physical.path)) {
961 /* dirlisting failed */
962 con->http_status = 403;
965 buffer_reset(con->physical.path);
967 /* not found */
968 return HANDLER_FINISHED;
971 /* this function is called at dlopen() time and inits the callbacks */
973 int mod_dirlisting_plugin_init(plugin *p);
974 int mod_dirlisting_plugin_init(plugin *p) {
975 p->version = LIGHTTPD_VERSION_ID;
976 p->name = buffer_init_string("dirlisting");
978 p->init = mod_dirlisting_init;
979 p->handle_subrequest_start = mod_dirlisting_subrequest;
980 p->set_defaults = mod_dirlisting_set_defaults;
981 p->cleanup = mod_dirlisting_free;
983 p->data = NULL;
985 return 0;