1 #include "first.h" 2 3 #include "base.h" 4 #include "log.h" 5 #include "buffer.h" 6 #include "fdevent.h" 7 #include "http_header.h" 8 9 #include "plugin.h" 10 11 #include "stat_cache.h" 12 13 #include <stdlib.h> 14 #include <string.h> 15 #include <dirent.h> 16 #include <fcntl.h> 17 #include <unistd.h> 18 #include <time.h> 19 20 #ifdef HAVE_PCRE_H 21 #include <pcre.h> 22 #endif 23 24 /** 25 * this is a dirlisting for a lighttpd plugin 26 */ 27 28 typedef struct { 29 char dir_listing; 30 char hide_dot_files; 31 char hide_readme_file; 32 char encode_readme; 33 char hide_header_file; 34 char encode_header; 35 char auto_layout; 36 37 #ifdef HAVE_PCRE_H 38 pcre **excludes; 39 #else 40 void *excludes; 41 #endif 42 43 const buffer *show_readme; 44 const buffer *show_header; 45 const buffer *external_css; 46 const buffer *external_js; 47 const buffer *encoding; 48 const buffer *set_footer; 49 } plugin_config; 50 51 typedef struct { 52 PLUGIN_DATA; 53 plugin_config defaults; 54 plugin_config conf; 55 56 buffer tmp_buf; 57 } plugin_data; 58 59 #ifdef HAVE_PCRE_H 60 61 static pcre ** mod_dirlisting_parse_excludes(server *srv, const array *a) { 62 pcre **regexes = calloc(a->used + 1, sizeof(pcre *)); 63 force_assert(regexes); 64 for (uint32_t j = 0; j < a->used; ++j) { 65 const data_string *ds = (const data_string *)a->data[j]; 66 const char *errptr; 67 int erroff; 68 regexes[j] = pcre_compile(ds->value.ptr, 0, &errptr, &erroff, NULL); 69 if (NULL == regexes[j]) { 70 log_error(srv->errh, __FILE__, __LINE__, 71 "pcre_compile failed for: %s", ds->value.ptr); 72 for (pcre **regex = regexes; *regex; ++regex) pcre_free(*regex); 73 free(regexes); 74 return NULL; 75 } 76 } 77 return regexes; 78 } 79 80 static int mod_dirlisting_exclude(log_error_st *errh, pcre **regex, const char *name, size_t len) { 81 for(; *regex; ++regex) { 82 #define N 10 83 int ovec[N * 3]; 84 int n; 85 if ((n = pcre_exec(*regex, NULL, name, len, 0, 0, ovec, 3 * N)) < 0) { 86 if (n == PCRE_ERROR_NOMATCH) continue; 87 88 log_error(errh, __FILE__, __LINE__, 89 "execution error while matching: %d", n); 90 /* aborting would require a lot of manual cleanup here. 91 * skip instead (to not leak names that break pcre matching) 92 */ 93 } 94 return 1; 95 #undef N 96 } 97 return 0; /* no match */ 98 } 99 100 #else 101 102 #define mod_dirlisting_exclude(a, b, c, d) 0 103 104 #endif 105 106 107 INIT_FUNC(mod_dirlisting_init) { 108 return calloc(1, sizeof(plugin_data)); 109 } 110 111 FREE_FUNC(mod_dirlisting_free) { 112 plugin_data * const p = p_d; 113 free(p->tmp_buf.ptr); 114 if (NULL == p->cvlist) return; 115 /* (init i to 0 if global context; to 1 to skip empty global context) */ 116 for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { 117 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 118 for (; -1 != cpv->k_id; ++cpv) { 119 switch (cpv->k_id) { 120 #ifdef HAVE_PCRE_H 121 case 2: /* dir-listing.exclude */ 122 if (cpv->vtype != T_CONFIG_LOCAL) continue; 123 for (pcre **regex = cpv->v.v; *regex; ++regex) 124 pcre_free(*regex); 125 free(cpv->v.v); 126 break; 127 #endif 128 default: 129 break; 130 } 131 } 132 } 133 } 134 135 static void mod_dirlisting_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) { 136 switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ 137 case 0: /* dir-listing.activate */ 138 case 1: /* server.dir-listing *//*(historical)*/ 139 pconf->dir_listing = (char)cpv->v.u; 140 break; 141 case 2: /* dir-listing.exclude */ 142 if (cpv->vtype == T_CONFIG_LOCAL) 143 pconf->excludes = cpv->v.v; 144 break; 145 case 3: /* dir-listing.hide-dotfiles */ 146 pconf->hide_dot_files = (char)cpv->v.u; 147 break; 148 case 4: /* dir-listing.external-css */ 149 pconf->external_css = cpv->v.b; 150 break; 151 case 5: /* dir-listing.external-js */ 152 pconf->external_js = cpv->v.b; 153 break; 154 case 6: /* dir-listing.encoding */ 155 pconf->encoding = cpv->v.b; 156 break; 157 case 7: /* dir-listing.show-readme */ 158 pconf->show_readme = cpv->v.b; 159 break; 160 case 8: /* dir-listing.hide-readme-file */ 161 pconf->hide_readme_file = (char)cpv->v.u; 162 break; 163 case 9: /* dir-listing.show-header */ 164 pconf->show_header = cpv->v.b; 165 break; 166 case 10:/* dir-listing.hide-header-file */ 167 pconf->hide_header_file = (char)cpv->v.u; 168 break; 169 case 11:/* dir-listing.set-footer */ 170 pconf->set_footer = cpv->v.b; 171 break; 172 case 12:/* dir-listing.encode-readme */ 173 pconf->encode_readme = (char)cpv->v.u; 174 break; 175 case 13:/* dir-listing.encode-header */ 176 pconf->encode_header = (char)cpv->v.u; 177 break; 178 case 14:/* dir-listing.auto-layout */ 179 pconf->auto_layout = (char)cpv->v.u; 180 break; 181 default:/* should not happen */ 182 return; 183 } 184 } 185 186 static void mod_dirlisting_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) { 187 do { 188 mod_dirlisting_merge_config_cpv(pconf, cpv); 189 } while ((++cpv)->k_id != -1); 190 } 191 192 static void mod_dirlisting_patch_config(request_st * const r, plugin_data * const p) { 193 memcpy(&p->conf, &p->defaults, sizeof(plugin_config)); 194 for (int i = 1, used = p->nconfig; i < used; ++i) { 195 if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id)) 196 mod_dirlisting_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]); 197 } 198 } 199 200 SETDEFAULTS_FUNC(mod_dirlisting_set_defaults) { 201 static const config_plugin_keys_t cpk[] = { 202 { CONST_STR_LEN("dir-listing.activate"), 203 T_CONFIG_BOOL, 204 T_CONFIG_SCOPE_CONNECTION } 205 ,{ CONST_STR_LEN("server.dir-listing"), 206 T_CONFIG_BOOL, 207 T_CONFIG_SCOPE_CONNECTION } 208 ,{ CONST_STR_LEN("dir-listing.exclude"), 209 T_CONFIG_ARRAY_VLIST, 210 T_CONFIG_SCOPE_CONNECTION } 211 ,{ CONST_STR_LEN("dir-listing.hide-dot-files"), 212 T_CONFIG_BOOL, 213 T_CONFIG_SCOPE_CONNECTION } 214 ,{ CONST_STR_LEN("dir-listing.external-css"), 215 T_CONFIG_STRING, 216 T_CONFIG_SCOPE_CONNECTION } 217 ,{ CONST_STR_LEN("dir-listing.external-js"), 218 T_CONFIG_STRING, 219 T_CONFIG_SCOPE_CONNECTION } 220 ,{ CONST_STR_LEN("dir-listing.encoding"), 221 T_CONFIG_STRING, 222 T_CONFIG_SCOPE_CONNECTION } 223 ,{ CONST_STR_LEN("dir-listing.show-readme"), 224 T_CONFIG_STRING, 225 T_CONFIG_SCOPE_CONNECTION } 226 ,{ CONST_STR_LEN("dir-listing.hide-readme-file"), 227 T_CONFIG_BOOL, 228 T_CONFIG_SCOPE_CONNECTION } 229 ,{ CONST_STR_LEN("dir-listing.show-header"), 230 T_CONFIG_STRING, 231 T_CONFIG_SCOPE_CONNECTION } 232 ,{ CONST_STR_LEN("dir-listing.hide-header-file"), 233 T_CONFIG_BOOL, 234 T_CONFIG_SCOPE_CONNECTION } 235 ,{ CONST_STR_LEN("dir-listing.set-footer"), 236 T_CONFIG_STRING, 237 T_CONFIG_SCOPE_CONNECTION } 238 ,{ CONST_STR_LEN("dir-listing.encode-readme"), 239 T_CONFIG_BOOL, 240 T_CONFIG_SCOPE_CONNECTION } 241 ,{ CONST_STR_LEN("dir-listing.encode-header"), 242 T_CONFIG_BOOL, 243 T_CONFIG_SCOPE_CONNECTION } 244 ,{ CONST_STR_LEN("dir-listing.auto-layout"), 245 T_CONFIG_BOOL, 246 T_CONFIG_SCOPE_CONNECTION } 247 ,{ NULL, 0, 248 T_CONFIG_UNSET, 249 T_CONFIG_SCOPE_UNSET } 250 }; 251 252 plugin_data * const p = p_d; 253 if (!config_plugin_values_init(srv, p, cpk, "mod_dirlisting")) 254 return HANDLER_ERROR; 255 256 /* process and validate config directives 257 * (init i to 0 if global context; to 1 to skip empty global context) */ 258 for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 259 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 260 for (; -1 != cpv->k_id; ++cpv) { 261 switch (cpv->k_id) { 262 case 0: /* dir-listing.activate */ 263 case 1: /* server.dir-listing *//*(historical)*/ 264 break; 265 case 2: /* dir-listing.exclude */ 266 #ifndef HAVE_PCRE_H 267 if (cpv->v.a->used > 0) { 268 log_error(srv->errh, __FILE__, __LINE__, 269 "pcre support is missing for: %s, " 270 "please install libpcre and the headers", 271 cpk[cpv->k_id].k); 272 return HANDLER_ERROR; 273 } 274 #else 275 cpv->v.v = mod_dirlisting_parse_excludes(srv, cpv->v.a); 276 if (NULL == cpv->v.v) { 277 log_error(srv->errh, __FILE__, __LINE__, 278 "unexpected value for %s", cpk[cpv->k_id].k); 279 return HANDLER_ERROR; 280 } 281 cpv->vtype = T_CONFIG_LOCAL; 282 #endif 283 break; 284 case 3: /* dir-listing.hide-dotfiles */ 285 case 4: /* dir-listing.external-css */ 286 case 5: /* dir-listing.external-js */ 287 case 6: /* dir-listing.encoding */ 288 case 7: /* dir-listing.show-readme */ 289 if (!buffer_string_is_empty(cpv->v.b)) { 290 buffer *b; 291 *(const buffer **)&b = cpv->v.b; 292 if (buffer_is_equal_string(b, CONST_STR_LEN("enable"))) 293 buffer_copy_string_len(b, CONST_STR_LEN("README.txt")); 294 else if (buffer_is_equal_string(b,CONST_STR_LEN("disable"))) 295 buffer_clear(b); 296 } 297 break; 298 case 8: /* dir-listing.hide-readme-file */ 299 break; 300 case 9: /* dir-listing.show-header */ 301 if (!buffer_string_is_empty(cpv->v.b)) { 302 buffer *b; 303 *(const buffer **)&b = cpv->v.b; 304 if (buffer_is_equal_string(b, CONST_STR_LEN("enable"))) 305 buffer_copy_string_len(b, CONST_STR_LEN("HEADER.txt")); 306 else if (buffer_is_equal_string(b,CONST_STR_LEN("disable"))) 307 buffer_clear(b); 308 } 309 break; 310 case 10:/* dir-listing.hide-header-file */ 311 case 11:/* dir-listing.set-footer */ 312 case 12:/* dir-listing.encode-readme */ 313 case 13:/* dir-listing.encode-header */ 314 case 14:/* dir-listing.auto-layout */ 315 break; 316 default:/* should not happen */ 317 break; 318 } 319 } 320 } 321 322 p->defaults.dir_listing = 0; 323 p->defaults.hide_dot_files = 1; 324 p->defaults.hide_readme_file = 0; 325 p->defaults.hide_header_file = 0; 326 p->defaults.encode_readme = 1; 327 p->defaults.encode_header = 1; 328 p->defaults.auto_layout = 1; 329 330 /* initialize p->defaults from global config context */ 331 if (p->nconfig > 0 && p->cvlist->v.u2[1]) { 332 const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; 333 if (-1 != cpv->k_id) 334 mod_dirlisting_merge_config(&p->defaults, cpv); 335 } 336 337 return HANDLER_GO_ON; 338 } 339 340 typedef struct { 341 uint32_t namelen; 342 time_t mtime; 343 off_t size; 344 } dirls_entry_t; 345 346 typedef struct { 347 dirls_entry_t **ent; 348 uint32_t used; 349 uint32_t size; 350 } dirls_list_t; 351 352 #define DIRLIST_ENT_NAME(ent) ((char*)(ent) + sizeof(dirls_entry_t)) 353 #define DIRLIST_BLOB_SIZE 16 354 355 /* simple combsort algorithm */ 356 static void http_dirls_sort(dirls_entry_t **ent, int num) { 357 int gap = num; 358 int i, j; 359 int swapped; 360 dirls_entry_t *tmp; 361 362 do { 363 gap = (gap * 10) / 13; 364 if (gap == 9 || gap == 10) 365 gap = 11; 366 if (gap < 1) 367 gap = 1; 368 swapped = 0; 369 370 for (i = 0; i < num - gap; i++) { 371 j = i + gap; 372 if (strcmp(DIRLIST_ENT_NAME(ent[i]), DIRLIST_ENT_NAME(ent[j])) > 0) { 373 tmp = ent[i]; 374 ent[i] = ent[j]; 375 ent[j] = tmp; 376 swapped = 1; 377 } 378 } 379 380 } while (gap > 1 || swapped); 381 } 382 383 /* buffer must be able to hold "999.9K" 384 * conversion is simple but not perfect 385 */ 386 static int http_list_directory_sizefmt(char *buf, size_t bufsz, off_t size) { 387 const char unit[] = " KMGTPE"; /* Kilo, Mega, Giga, Tera, Peta, Exa */ 388 const char *u = unit; /* u will always increment at least once */ 389 int remain; 390 size_t buflen; 391 392 if (size < 100) 393 size += 99; 394 if (size < 100) 395 size = 0; 396 397 while (1) { 398 remain = (int) size & 1023; 399 size >>= 10; 400 u++; 401 if ((size & (~0 ^ 1023)) == 0) 402 break; 403 } 404 405 remain /= 100; 406 if (remain > 9) 407 remain = 9; 408 if (size > 999) { 409 size = 0; 410 remain = 9; 411 u++; 412 } 413 414 buflen = li_itostrn(buf, bufsz, size); 415 if (buflen + 3 >= bufsz) return buflen; 416 buf[buflen+0] = '.'; 417 buf[buflen+1] = remain + '0'; 418 buf[buflen+2] = *u; 419 buf[buflen+3] = '\0'; 420 421 return buflen + 3; 422 } 423 424 static void http_list_directory_include_file(buffer *out, int symlinks, const buffer *path, const char *classname, int encode) { 425 int fd = fdevent_open_cloexec(path->ptr, symlinks, O_RDONLY, 0); 426 ssize_t rd; 427 char buf[8192]; 428 429 if (-1 == fd) return; 430 431 if (encode) { 432 buffer_append_string_len(out, CONST_STR_LEN("<pre class=\"")); 433 buffer_append_string(out, classname); 434 buffer_append_string_len(out, CONST_STR_LEN("\">")); 435 } 436 437 while ((rd = read(fd, buf, sizeof(buf))) > 0) { 438 if (encode) { 439 buffer_append_string_encoded(out, buf, (size_t)rd, ENCODING_MINIMAL_XML); 440 } else { 441 buffer_append_string_len(out, buf, (size_t)rd); 442 } 443 } 444 close(fd); 445 446 if (encode) { 447 buffer_append_string_len(out, CONST_STR_LEN("</pre>")); 448 } 449 } 450 451 /* portions copied from mod_status 452 * modified and specialized for stable dirlist sorting by name */ 453 static const char js_simple_table_resort[] = \ 454 "var click_column;\n" \ 455 "var name_column = 0;\n" \ 456 "var date_column = 1;\n" \ 457 "var size_column = 2;\n" \ 458 "var type_column = 3;\n" \ 459 "var prev_span = null;\n" \ 460 "\n" \ 461 "if (typeof(String.prototype.localeCompare) === 'undefined') {\n" \ 462 " String.prototype.localeCompare = function(str, locale, options) {\n" \ 463 " return ((this == str) ? 0 : ((this > str) ? 1 : -1));\n" \ 464 " };\n" \ 465 "}\n" \ 466 "\n" \ 467 "if (typeof(String.prototype.toLocaleUpperCase) === 'undefined') {\n" \ 468 " String.prototype.toLocaleUpperCase = function() {\n" \ 469 " return this.toUpperCase();\n" \ 470 " };\n" \ 471 "}\n" \ 472 "\n" \ 473 "function get_inner_text(el) {\n" \ 474 " if((typeof el == 'string')||(typeof el == 'undefined'))\n" \ 475 " return el;\n" \ 476 " if(el.innerText)\n" \ 477 " return el.innerText;\n" \ 478 " else {\n" \ 479 " var str = \"\";\n" \ 480 " var cs = el.childNodes;\n" \ 481 " var l = cs.length;\n" \ 482 " for (i=0;i<l;i++) {\n" \ 483 " if (cs[i].nodeType==1) str += get_inner_text(cs[i]);\n" \ 484 " else if (cs[i].nodeType==3) str += cs[i].nodeValue;\n" \ 485 " }\n" \ 486 " }\n" \ 487 " return str;\n" \ 488 "}\n" \ 489 "\n" \ 490 "function isdigit(c) {\n" \ 491 " return (c >= '0' && c <= '9');\n" \ 492 "}\n" \ 493 "\n" \ 494 "function unit_multiplier(unit) {\n" \ 495 " return (unit=='K') ? 1000\n" \ 496 " : (unit=='M') ? 1000000\n" \ 497 " : (unit=='G') ? 1000000000\n" \ 498 " : (unit=='T') ? 1000000000000\n" \ 499 " : (unit=='P') ? 1000000000000000\n" \ 500 " : (unit=='E') ? 1000000000000000000 : 1;\n" \ 501 "}\n" \ 502 "\n" \ 503 "var li_date_regex=/(\\d{4})-(\\w{3})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})/;\n" \ 504 "\n" \ 505 "var li_mon = ['Jan','Feb','Mar','Apr','May','Jun',\n" \ 506 " 'Jul','Aug','Sep','Oct','Nov','Dec'];\n" \ 507 "\n" \ 508 "function li_mon_num(mon) {\n" \ 509 " var i; for (i = 0; i < 12 && mon != li_mon[i]; ++i); return i;\n" \ 510 "}\n" \ 511 "\n" \ 512 "function li_date_cmp(s1, s2) {\n" \ 513 " var dp1 = li_date_regex.exec(s1)\n" \ 514 " var dp2 = li_date_regex.exec(s2)\n" \ 515 " for (var i = 1; i < 7; ++i) {\n" \ 516 " var cmp = (2 != i)\n" \ 517 " ? parseInt(dp1[i]) - parseInt(dp2[i])\n" \ 518 " : li_mon_num(dp1[2]) - li_mon_num(dp2[2]);\n" \ 519 " if (0 != cmp) return cmp;\n" \ 520 " }\n" \ 521 " return 0;\n" \ 522 "}\n" \ 523 "\n" \ 524 "function sortfn_then_by_name(a,b,sort_column) {\n" \ 525 " if (sort_column == name_column || sort_column == type_column) {\n" \ 526 " var ad = (a.cells[type_column].innerHTML === 'Directory');\n" \ 527 " var bd = (b.cells[type_column].innerHTML === 'Directory');\n" \ 528 " if (ad != bd) return (ad ? -1 : 1);\n" \ 529 " }\n" \ 530 " var at = get_inner_text(a.cells[sort_column]);\n" \ 531 " var bt = get_inner_text(b.cells[sort_column]);\n" \ 532 " var cmp;\n" \ 533 " if (sort_column == name_column) {\n" \ 534 " if (at == '..') return -1;\n" \ 535 " if (bt == '..') return 1;\n" \ 536 " }\n" \ 537 " if (a.cells[sort_column].className == 'int') {\n" \ 538 " cmp = parseInt(at)-parseInt(bt);\n" \ 539 " } else if (sort_column == date_column) {\n" \ 540 " var ad = isdigit(at.substr(0,1));\n" \ 541 " var bd = isdigit(bt.substr(0,1));\n" \ 542 " if (ad != bd) return (!ad ? -1 : 1);\n" \ 543 " cmp = li_date_cmp(at,bt);\n" \ 544 " } else if (sort_column == size_column) {\n" \ 545 " var ai = parseInt(at, 10) * unit_multiplier(at.substr(-1,1));\n" \ 546 " var bi = parseInt(bt, 10) * unit_multiplier(bt.substr(-1,1));\n" \ 547 " if (at.substr(0,1) == '-') ai = -1;\n" \ 548 " if (bt.substr(0,1) == '-') bi = -1;\n" \ 549 " cmp = ai - bi;\n" \ 550 " } else {\n" \ 551 " cmp = at.toLocaleUpperCase().localeCompare(bt.toLocaleUpperCase());\n" \ 552 " if (0 != cmp) return cmp;\n" \ 553 " cmp = at.localeCompare(bt);\n" \ 554 " }\n" \ 555 " if (0 != cmp || sort_column == name_column) return cmp;\n" \ 556 " return sortfn_then_by_name(a,b,name_column);\n" \ 557 "}\n" \ 558 "\n" \ 559 "function sortfn(a,b) {\n" \ 560 " return sortfn_then_by_name(a,b,click_column);\n" \ 561 "}\n" \ 562 "\n" \ 563 "function resort(lnk) {\n" \ 564 " var span = lnk.childNodes[1];\n" \ 565 " var table = lnk.parentNode.parentNode.parentNode.parentNode;\n" \ 566 " var rows = new Array();\n" \ 567 " for (j=1;j<table.rows.length;j++)\n" \ 568 " rows[j-1] = table.rows[j];\n" \ 569 " click_column = lnk.parentNode.cellIndex;\n" \ 570 " rows.sort(sortfn);\n" \ 571 "\n" \ 572 " if (prev_span != null) prev_span.innerHTML = '';\n" \ 573 " if (span.getAttribute('sortdir')=='down') {\n" \ 574 " span.innerHTML = '↑';\n" \ 575 " span.setAttribute('sortdir','up');\n" \ 576 " rows.reverse();\n" \ 577 " } else {\n" \ 578 " span.innerHTML = '↓';\n" \ 579 " span.setAttribute('sortdir','down');\n" \ 580 " }\n" \ 581 " for (i=0;i<rows.length;i++)\n" \ 582 " table.tBodies[0].appendChild(rows[i]);\n" \ 583 " prev_span = span;\n" \ 584 "}\n"; 585 586 /* portions copied from mod_dirlist (lighttpd2) */ 587 static const char js_simple_table_init_sort[] = \ 588 "\n" \ 589 "function init_sort(init_sort_column, ascending) {\n" \ 590 " var tables = document.getElementsByTagName(\"table\");\n" \ 591 " for (var i = 0; i < tables.length; i++) {\n" \ 592 " var table = tables[i];\n" \ 593 " //var c = table.getAttribute(\"class\")\n" \ 594 " //if (-1 != c.split(\" \").indexOf(\"sort\")) {\n" \ 595 " var row = table.rows[0].cells;\n" \ 596 " for (var j = 0; j < row.length; j++) {\n" \ 597 " var n = row[j];\n" \ 598 " if (n.childNodes.length == 1 && n.childNodes[0].nodeType == 3) {\n" \ 599 " var link = document.createElement(\"a\");\n" \ 600 " var title = n.childNodes[0].nodeValue.replace(/:$/, \"\");\n" \ 601 " link.appendChild(document.createTextNode(title));\n" \ 602 " link.setAttribute(\"href\", \"#\");\n" \ 603 " link.setAttribute(\"class\", \"sortheader\");\n" \ 604 " link.setAttribute(\"onclick\", \"resort(this);return false;\");\n" \ 605 " var arrow = document.createElement(\"span\");\n" \ 606 " arrow.setAttribute(\"class\", \"sortarrow\");\n" \ 607 " arrow.appendChild(document.createTextNode(\":\"));\n" \ 608 " link.appendChild(arrow)\n" \ 609 " n.replaceChild(link, n.firstChild);\n" \ 610 " }\n" \ 611 " }\n" \ 612 " var lnk = row[init_sort_column].firstChild;\n" \ 613 " if (ascending) {\n" \ 614 " var span = lnk.childNodes[1];\n" \ 615 " span.setAttribute('sortdir','down');\n" \ 616 " }\n" \ 617 " resort(lnk);\n" \ 618 " //}\n" \ 619 " }\n" \ 620 "}\n"; 621 622 static void http_dirlist_append_js_table_resort (buffer * const b, const request_st * const r) { 623 char col = '0'; 624 char ascending = '0'; 625 if (!buffer_string_is_empty(&r->uri.query)) { 626 const char *qs = r->uri.query.ptr; 627 do { 628 if (qs[0] == 'C' && qs[1] == '=') { 629 switch (qs[2]) { 630 case 'N': col = '0'; break; 631 case 'M': col = '1'; break; 632 case 'S': col = '2'; break; 633 case 'T': 634 case 'D': col = '3'; break; 635 default: break; 636 } 637 } 638 else if (qs[0] == 'O' && qs[1] == '=') { 639 switch (qs[2]) { 640 case 'A': ascending = '1'; break; 641 case 'D': ascending = '0'; break; 642 default: break; 643 } 644 } 645 } while ((qs = strchr(qs, '&')) && *++qs); 646 } 647 648 buffer_append_string_len(b, CONST_STR_LEN("\n<script type=\"text/javascript\">\n// <!--\n\n")); 649 buffer_append_string_len(b, js_simple_table_resort, sizeof(js_simple_table_resort)-1); 650 buffer_append_string_len(b, js_simple_table_init_sort, sizeof(js_simple_table_init_sort)-1); 651 buffer_append_string_len(b, CONST_STR_LEN("\ninit_sort(")); 652 buffer_append_string_len(b, &col, 1); 653 buffer_append_string_len(b, CONST_STR_LEN(", ")); 654 buffer_append_string_len(b, &ascending, 1); 655 buffer_append_string_len(b, CONST_STR_LEN(");\n\n// -->\n</script>\n\n")); 656 } 657 658 static void http_list_directory_header(const request_st * const r, plugin_data * const p, buffer * const out) { 659 660 if (p->conf.auto_layout) { 661 buffer_append_string_len(out, CONST_STR_LEN( 662 "<!DOCTYPE html>\n" 663 "<html>\n" 664 "<head>\n" 665 )); 666 if (!buffer_string_is_empty(p->conf.encoding)) { 667 buffer_append_string_len(out, CONST_STR_LEN("<meta charset=\"")); 668 buffer_append_string_buffer(out, p->conf.encoding); 669 buffer_append_string_len(out, CONST_STR_LEN("\">\n")); 670 } 671 buffer_append_string_len(out, CONST_STR_LEN("<title>Index of ")); 672 buffer_append_string_encoded(out, CONST_BUF_LEN(&r->uri.path), ENCODING_MINIMAL_XML); 673 buffer_append_string_len(out, CONST_STR_LEN("</title>\n")); 674 675 if (!buffer_string_is_empty(p->conf.external_css)) { 676 buffer_append_string_len(out, CONST_STR_LEN("<meta name=\"viewport\" content=\"initial-scale=1\">")); 677 buffer_append_string_len(out, CONST_STR_LEN("<link rel=\"stylesheet\" type=\"text/css\" href=\"")); 678 buffer_append_string_buffer(out, p->conf.external_css); 679 buffer_append_string_len(out, CONST_STR_LEN("\">\n")); 680 } else { 681 buffer_append_string_len(out, CONST_STR_LEN( 682 "<style type=\"text/css\">\n" 683 "a, a:active {text-decoration: none; color: blue;}\n" 684 "a:visited {color: #48468F;}\n" 685 "a:hover, a:focus {text-decoration: underline; color: red;}\n" 686 "body {background-color: #F5F5F5;}\n" 687 "h2 {margin-bottom: 12px;}\n" 688 "table {margin-left: 12px;}\n" 689 "th, td {" 690 " font: 90% monospace;" 691 " text-align: left;" 692 "}\n" 693 "th {" 694 " font-weight: bold;" 695 " padding-right: 14px;" 696 " padding-bottom: 3px;" 697 "}\n" 698 "td {padding-right: 14px;}\n" 699 "td.s, th.s {text-align: right;}\n" 700 "div.list {" 701 " background-color: white;" 702 " border-top: 1px solid #646464;" 703 " border-bottom: 1px solid #646464;" 704 " padding-top: 10px;" 705 " padding-bottom: 14px;" 706 "}\n" 707 "div.foot {" 708 " font: 90% monospace;" 709 " color: #787878;" 710 " padding-top: 4px;" 711 "}\n" 712 "</style>\n" 713 )); 714 } 715 716 buffer_append_string_len(out, CONST_STR_LEN("</head>\n<body>\n")); 717 } 718 719 if (!buffer_string_is_empty(p->conf.show_header)) { 720 /* if we have a HEADER file, display it in <pre class="header"></pre> */ 721 722 const buffer *hb = p->conf.show_header; 723 if (hb->ptr[0] != '/') { 724 hb = &p->tmp_buf; 725 buffer_copy_buffer(&p->tmp_buf, &r->physical.path); 726 buffer_append_path_len(&p->tmp_buf, CONST_BUF_LEN(p->conf.show_header)); 727 } 728 729 http_list_directory_include_file(out, r->conf.follow_symlink, hb, "header", p->conf.encode_header); 730 } 731 732 buffer_append_string_len(out, CONST_STR_LEN("<h2>Index of ")); 733 buffer_append_string_encoded(out, CONST_BUF_LEN(&r->uri.path), ENCODING_MINIMAL_XML); 734 buffer_append_string_len(out, CONST_STR_LEN( 735 "</h2>\n" 736 "<div class=\"list\">\n" 737 "<table summary=\"Directory Listing\" cellpadding=\"0\" cellspacing=\"0\">\n" 738 "<thead>" 739 "<tr>" 740 "<th class=\"n\">Name</th>" 741 "<th class=\"m\">Last Modified</th>" 742 "<th class=\"s\">Size</th>" 743 "<th class=\"t\">Type</th>" 744 "</tr>" 745 "</thead>\n" 746 "<tbody>\n" 747 )); 748 if (!buffer_is_equal_string(&r->uri.path, CONST_STR_LEN("/"))) { 749 buffer_append_string_len(out, CONST_STR_LEN( 750 "<tr class=\"d\">" 751 "<td class=\"n\"><a href=\"../\">..</a>/</td>" 752 "<td class=\"m\"> </td>" 753 "<td class=\"s\">- </td>" 754 "<td class=\"t\">Directory</td>" 755 "</tr>\n" 756 )); 757 } 758 } 759 760 static void http_list_directory_footer(const request_st * const r, plugin_data * const p, buffer * const out) { 761 762 buffer_append_string_len(out, CONST_STR_LEN( 763 "</tbody>\n" 764 "</table>\n" 765 "</div>\n" 766 )); 767 768 if (!buffer_string_is_empty(p->conf.show_readme)) { 769 /* if we have a README file, display it in <pre class="readme"></pre> */ 770 771 const buffer *rb = p->conf.show_readme; 772 if (rb->ptr[0] != '/') { 773 rb = &p->tmp_buf; 774 buffer_copy_buffer(&p->tmp_buf, &r->physical.path); 775 buffer_append_path_len(&p->tmp_buf, CONST_BUF_LEN(p->conf.show_readme)); 776 } 777 778 http_list_directory_include_file(out, r->conf.follow_symlink, rb, "readme", p->conf.encode_readme); 779 } 780 781 if(p->conf.auto_layout) { 782 783 buffer_append_string_len(out, CONST_STR_LEN( 784 "<div class=\"foot\">" 785 )); 786 787 if (!buffer_string_is_empty(p->conf.set_footer)) { 788 buffer_append_string_buffer(out, p->conf.set_footer); 789 } else { 790 buffer_append_string_buffer(out, r->conf.server_tag); 791 } 792 793 buffer_append_string_len(out, CONST_STR_LEN( 794 "</div>\n" 795 )); 796 797 if (!buffer_string_is_empty(p->conf.external_js)) { 798 buffer_append_string_len(out, CONST_STR_LEN("<script type=\"text/javascript\" src=\"")); 799 buffer_append_string_buffer(out, p->conf.external_js); 800 buffer_append_string_len(out, CONST_STR_LEN("\"></script>\n")); 801 } else if (buffer_is_empty(p->conf.external_js)) { 802 http_dirlist_append_js_table_resort(out, r); 803 } 804 805 buffer_append_string_len(out, CONST_STR_LEN( 806 "</body>\n" 807 "</html>\n" 808 )); 809 } 810 } 811 812 static int http_list_directory(request_st * const r, plugin_data * const p, buffer * const dir) { 813 DIR *dp; 814 buffer *out; 815 struct dirent *dent; 816 struct stat st; 817 char *path, *path_file; 818 size_t i; 819 int hide_dotfiles = p->conf.hide_dot_files; 820 dirls_list_t dirs, files, *list; 821 dirls_entry_t *tmp; 822 char sizebuf[sizeof("999.9K")]; 823 char datebuf[sizeof("2005-Jan-01 22:23:24")]; 824 const buffer *content_type; 825 long name_max; 826 log_error_st * const errh = r->conf.errh; 827 #ifdef HAVE_LOCALTIME_R 828 struct tm tm; 829 #endif 830 831 if (buffer_string_is_empty(dir)) return -1; 832 833 i = buffer_string_length(dir); 834 835 #ifdef HAVE_PATHCONF 836 if (0 >= (name_max = pathconf(dir->ptr, _PC_NAME_MAX))) { 837 /* some broken fs (fuse) return 0 instead of -1 */ 838 #ifdef NAME_MAX 839 name_max = NAME_MAX; 840 #else 841 name_max = 255; /* stupid default */ 842 #endif 843 } 844 #elif defined __WIN32 845 name_max = FILENAME_MAX; 846 #else 847 name_max = NAME_MAX; 848 #endif 849 850 path = malloc(i + name_max + 1); 851 force_assert(NULL != path); 852 memcpy(path, dir->ptr, i+1); 853 path_file = path + i; 854 855 if (NULL == (dp = opendir(path))) { 856 log_error(errh, __FILE__, __LINE__, 857 "opendir failed: %s", dir->ptr); 858 859 free(path); 860 return -1; 861 } 862 863 dirs.ent = (dirls_entry_t**) malloc(sizeof(dirls_entry_t*) * DIRLIST_BLOB_SIZE); 864 force_assert(dirs.ent); 865 dirs.size = DIRLIST_BLOB_SIZE; 866 dirs.used = 0; 867 files.ent = (dirls_entry_t**) malloc(sizeof(dirls_entry_t*) * DIRLIST_BLOB_SIZE); 868 force_assert(files.ent); 869 files.size = DIRLIST_BLOB_SIZE; 870 files.used = 0; 871 872 while ((dent = readdir(dp)) != NULL) { 873 if (dent->d_name[0] == '.') { 874 if (hide_dotfiles) 875 continue; 876 if (dent->d_name[1] == '\0') 877 continue; 878 if (dent->d_name[1] == '.' && dent->d_name[2] == '\0') 879 continue; 880 } 881 882 if (p->conf.hide_readme_file && !buffer_string_is_empty(p->conf.show_readme)) { 883 if (strcmp(dent->d_name, p->conf.show_readme->ptr) == 0) 884 continue; 885 } 886 if (p->conf.hide_header_file && !buffer_string_is_empty(p->conf.show_header)) { 887 if (strcmp(dent->d_name, p->conf.show_header->ptr) == 0) 888 continue; 889 } 890 891 i = strlen(dent->d_name); 892 893 /* compare d_name against excludes array 894 * elements, skipping any that match. 895 */ 896 if (p->conf.excludes 897 && mod_dirlisting_exclude(errh, p->conf.excludes, dent->d_name, i)) 898 continue; 899 900 /* NOTE: the manual says, d_name is never more than NAME_MAX 901 * so this should actually not be a buffer-overflow-risk 902 */ 903 if (i > (size_t)name_max) continue; 904 905 memcpy(path_file, dent->d_name, i + 1); 906 if (stat(path, &st) != 0) 907 continue; 908 909 list = &files; 910 if (S_ISDIR(st.st_mode)) 911 list = &dirs; 912 913 if (list->used == list->size) { 914 list->size += DIRLIST_BLOB_SIZE; 915 list->ent = (dirls_entry_t**) realloc(list->ent, sizeof(dirls_entry_t*) * list->size); 916 force_assert(list->ent); 917 } 918 919 tmp = (dirls_entry_t*) malloc(sizeof(dirls_entry_t) + 1 + i); 920 tmp->mtime = st.st_mtime; 921 tmp->size = st.st_size; 922 tmp->namelen = (uint32_t)i; 923 memcpy(DIRLIST_ENT_NAME(tmp), dent->d_name, i + 1); 924 925 list->ent[list->used++] = tmp; 926 } 927 closedir(dp); 928 929 if (dirs.used) http_dirls_sort(dirs.ent, dirs.used); 930 931 if (files.used) http_dirls_sort(files.ent, files.used); 932 933 out = chunkqueue_append_buffer_open(r->write_queue); 934 http_list_directory_header(r, p, out); 935 936 /* directories */ 937 for (i = 0; i < dirs.used; i++) { 938 tmp = dirs.ent[i]; 939 940 #ifdef HAVE_LOCALTIME_R 941 localtime_r(&(tmp->mtime), &tm); 942 strftime(datebuf, sizeof(datebuf), "%Y-%b-%d %H:%M:%S", &tm); 943 #else 944 strftime(datebuf, sizeof(datebuf), "%Y-%b-%d %H:%M:%S", localtime(&(tmp->mtime))); 945 #endif 946 947 buffer_append_string_len(out, CONST_STR_LEN("<tr class=\"d\"><td class=\"n\"><a href=\"")); 948 buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_REL_URI_PART); 949 buffer_append_string_len(out, CONST_STR_LEN("/\">")); 950 buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_MINIMAL_XML); 951 buffer_append_string_len(out, CONST_STR_LEN("</a>/</td><td class=\"m\">")); 952 buffer_append_string_len(out, datebuf, sizeof(datebuf) - 1); 953 buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"s\">- </td><td class=\"t\">Directory</td></tr>\n")); 954 955 free(tmp); 956 } 957 958 /* files */ 959 const array * const mimetypes = r->conf.mimetypes; 960 for (i = 0; i < files.used; i++) { 961 tmp = files.ent[i]; 962 963 #if defined(HAVE_XATTR) || defined(HAVE_EXTATTR) /*(pass full path)*/ 964 content_type = NULL; 965 if (r->conf.use_xattr) { 966 memcpy(path_file, DIRLIST_ENT_NAME(tmp), tmp->namelen + 1); 967 content_type = stat_cache_mimetype_by_xattr(path); 968 } 969 if (NULL == content_type) 970 #endif 971 content_type = stat_cache_mimetype_by_ext(mimetypes, DIRLIST_ENT_NAME(tmp), tmp->namelen); 972 if (NULL == content_type) { 973 static const buffer octet_stream = 974 { "application/octet-stream", 975 sizeof("application/octet-stream"), 0 }; 976 content_type = &octet_stream; 977 } 978 979 #ifdef HAVE_LOCALTIME_R 980 localtime_r(&(tmp->mtime), &tm); 981 strftime(datebuf, sizeof(datebuf), "%Y-%b-%d %H:%M:%S", &tm); 982 #else 983 strftime(datebuf, sizeof(datebuf), "%Y-%b-%d %H:%M:%S", localtime(&(tmp->mtime))); 984 #endif 985 http_list_directory_sizefmt(sizebuf, sizeof(sizebuf), tmp->size); 986 987 buffer_append_string_len(out, CONST_STR_LEN("<tr><td class=\"n\"><a href=\"")); 988 buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_REL_URI_PART); 989 buffer_append_string_len(out, CONST_STR_LEN("\">")); 990 buffer_append_string_encoded(out, DIRLIST_ENT_NAME(tmp), tmp->namelen, ENCODING_MINIMAL_XML); 991 buffer_append_string_len(out, CONST_STR_LEN("</a></td><td class=\"m\">")); 992 buffer_append_string_len(out, datebuf, sizeof(datebuf) - 1); 993 buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"s\">")); 994 buffer_append_string(out, sizebuf); 995 buffer_append_string_len(out, CONST_STR_LEN("</td><td class=\"t\">")); 996 buffer_append_string_buffer(out, content_type); 997 buffer_append_string_len(out, CONST_STR_LEN("</td></tr>\n")); 998 999 free(tmp); 1000 } 1001 1002 free(files.ent); 1003 free(dirs.ent); 1004 free(path); 1005 1006 http_list_directory_footer(r, p, out); 1007 1008 /* Insert possible charset to Content-Type */ 1009 if (buffer_string_is_empty(p->conf.encoding)) { 1010 http_header_response_set(r, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html")); 1011 } else { 1012 buffer_copy_string_len(&p->tmp_buf, CONST_STR_LEN("text/html; charset=")); 1013 buffer_append_string_buffer(&p->tmp_buf, p->conf.encoding); 1014 http_header_response_set(r, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(&p->tmp_buf)); 1015 } 1016 1017 chunkqueue_append_buffer_commit(r->write_queue); 1018 r->resp_body_finished = 1; 1019 1020 return 0; 1021 } 1022 1023 1024 1025 URIHANDLER_FUNC(mod_dirlisting_subrequest) { 1026 plugin_data *p = p_d; 1027 stat_cache_entry *sce = NULL; 1028 1029 if (NULL != r->handler_module) return HANDLER_GO_ON; 1030 if (buffer_string_is_empty(&r->uri.path)) return HANDLER_GO_ON; 1031 if (r->uri.path.ptr[buffer_string_length(&r->uri.path) - 1] != '/') return HANDLER_GO_ON; 1032 if (!http_method_get_or_head(r->http_method)) return HANDLER_GO_ON; 1033 if (buffer_is_empty(&r->physical.path)) return HANDLER_GO_ON; 1034 1035 mod_dirlisting_patch_config(r, p); 1036 1037 if (!p->conf.dir_listing) return HANDLER_GO_ON; 1038 1039 if (r->conf.log_request_handling) { 1040 log_error(r->conf.errh, __FILE__, __LINE__, 1041 "-- handling the request as Dir-Listing"); 1042 log_error(r->conf.errh, __FILE__, __LINE__, 1043 "URI : %s", r->uri.path.ptr); 1044 } 1045 1046 sce = stat_cache_get_entry(&r->physical.path); 1047 if (NULL == sce) { 1048 log_error(r->conf.errh, __FILE__, __LINE__, 1049 "stat_cache_get_entry failed: %s", r->physical.path.ptr); 1050 r->http_status = 500; 1051 return HANDLER_FINISHED; 1052 } 1053 1054 if (!S_ISDIR(sce->st.st_mode)) return HANDLER_GO_ON; 1055 1056 if (http_list_directory(r, p, &r->physical.path)) { 1057 /* dirlisting failed */ 1058 r->http_status = 403; 1059 } 1060 1061 buffer_reset(&r->physical.path); 1062 1063 /* not found */ 1064 return HANDLER_FINISHED; 1065 } 1066 1067 1068 int mod_dirlisting_plugin_init(plugin *p); 1069 int mod_dirlisting_plugin_init(plugin *p) { 1070 p->version = LIGHTTPD_VERSION_ID; 1071 p->name = "dirlisting"; 1072 1073 p->init = mod_dirlisting_init; 1074 p->handle_subrequest_start = mod_dirlisting_subrequest; 1075 p->set_defaults = mod_dirlisting_set_defaults; 1076 p->cleanup = mod_dirlisting_free; 1077 1078 return 0; 1079 } 1080