httpd_indexcgi.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (c) 2007 Denys Vlasenko <vda.linux@googlemail.com>
  3. *
  4. * Licensed under GPLv2, see file LICENSE in this source tree.
  5. */
  6. /*
  7. * This program is a CGI application. It outputs directory index page.
  8. * Put it into cgi-bin/index.cgi and chmod 0755.
  9. */
  10. /* Build a-la
  11. i486-linux-uclibc-gcc \
  12. -static -static-libgcc \
  13. -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 \
  14. -Wall -Wshadow -Wwrite-strings -Wundef -Wstrict-prototypes -Werror \
  15. -Wold-style-definition -Wdeclaration-after-statement -Wno-pointer-sign \
  16. -Wmissing-prototypes -Wmissing-declarations \
  17. -Os -fno-builtin-strlen -finline-limit=0 -fomit-frame-pointer \
  18. -ffunction-sections -fdata-sections -fno-guess-branch-probability \
  19. -funsigned-char \
  20. -falign-functions=1 -falign-jumps=1 -falign-labels=1 -falign-loops=1 \
  21. -march=i386 -mpreferred-stack-boundary=2 \
  22. -Wl,-Map -Wl,link.map -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
  23. httpd_indexcgi.c -o index.cgi
  24. */
  25. /* We don't use printf, as it pulls in >12 kb of code from uclibc (i386). */
  26. /* Currently malloc machinery is the biggest part of libc we pull in. */
  27. /* We have only one realloc and one strdup, any idea how to do without? */
  28. /* Size (i386, static uclibc, approximate):
  29. * text data bss dec hex filename
  30. * 13036 44 3052 16132 3f04 index.cgi
  31. * 2576 4 2048 4628 1214 index.cgi.o
  32. */
  33. #define _GNU_SOURCE 1 /* for strchrnul */
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <errno.h>
  37. #include <stdint.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include <stdio.h>
  42. #include <dirent.h>
  43. #include <time.h>
  44. /* Appearance of the table is controlled by style sheet *ONLY*,
  45. * formatting code uses <TAG class=CLASS> to apply style
  46. * to elements. Edit stylesheet to your liking and recompile. */
  47. #define STYLE_STR \
  48. "<style>" "\n"\
  49. "table {" "\n"\
  50. "width:100%;" "\n"\
  51. "background-color:#fff5ee;" "\n"\
  52. "border-width:1px;" /* 1px 1px 1px 1px; */ "\n"\
  53. "border-spacing:2px;" "\n"\
  54. "border-style:solid;" /* solid solid solid solid; */ "\n"\
  55. "border-color:black;" /* black black black black; */ "\n"\
  56. "border-collapse:collapse;" "\n"\
  57. "}" "\n"\
  58. "th {" "\n"\
  59. "border-width:1px;" /* 1px 1px 1px 1px; */ "\n"\
  60. "padding:1px;" /* 1px 1px 1px 1px; */ "\n"\
  61. "border-style:solid;" /* solid solid solid solid; */ "\n"\
  62. "border-color:black;" /* black black black black; */ "\n"\
  63. "}" "\n"\
  64. "td {" "\n"\
  65. /* top right bottom left */ \
  66. "border-width:0px 1px 0px 1px;" "\n"\
  67. "padding:1px;" /* 1px 1px 1px 1px; */ "\n"\
  68. "border-style:solid;" /* solid solid solid solid; */ "\n"\
  69. "border-color:black;" /* black black black black; */ "\n"\
  70. "white-space:nowrap;" "\n"\
  71. "}" "\n"\
  72. "tr.hdr { background-color:#eee5de; }" "\n"\
  73. "tr.o { background-color:#ffffff; }" "\n"\
  74. /* tr.e { ... } - for even rows (currently none) */ \
  75. "tr.foot { background-color:#eee5de; }" "\n"\
  76. "th.cnt { text-align:left; }" "\n"\
  77. "th.sz { text-align:right; }" "\n"\
  78. "th.dt { text-align:right; }" "\n"\
  79. "td.sz { text-align:right; }" "\n"\
  80. "td.dt { text-align:right; }" "\n"\
  81. "col.nm { width:98%; }" "\n"\
  82. "col.sz { width:1%; }" "\n"\
  83. "col.dt { width:1%; }" "\n"\
  84. "</style>" "\n"\
  85. typedef struct dir_list_t {
  86. char *dl_name;
  87. mode_t dl_mode;
  88. off_t dl_size;
  89. time_t dl_mtime;
  90. } dir_list_t;
  91. static int compare_dl(dir_list_t *a, dir_list_t *b)
  92. {
  93. /* ".." is 'less than' any other dir entry */
  94. if (strcmp(a->dl_name, "..") == 0) {
  95. return -1;
  96. }
  97. if (strcmp(b->dl_name, "..") == 0) {
  98. return 1;
  99. }
  100. if (S_ISDIR(a->dl_mode) != S_ISDIR(b->dl_mode)) {
  101. /* 1 if b is a dir (and thus a is 'after' b, a > b),
  102. * else -1 (a < b) */
  103. return (S_ISDIR(b->dl_mode) != 0) ? 1 : -1;
  104. }
  105. return strcmp(a->dl_name, b->dl_name);
  106. }
  107. static char buffer[2*1024 > sizeof(STYLE_STR) ? 2*1024 : sizeof(STYLE_STR)];
  108. static char *dst = buffer;
  109. enum {
  110. BUFFER_SIZE = sizeof(buffer),
  111. HEADROOM = 64,
  112. };
  113. /* After this call, you have at least size + HEADROOM bytes available
  114. * ahead of dst */
  115. static void guarantee(int size)
  116. {
  117. if (buffer + (BUFFER_SIZE-HEADROOM) - dst >= size)
  118. return;
  119. write(STDOUT_FILENO, buffer, dst - buffer);
  120. dst = buffer;
  121. }
  122. /* NB: formatters do not store terminating NUL! */
  123. /* HEADROOM bytes are available after dst after this call */
  124. static void fmt_str(/*char *dst,*/ const char *src)
  125. {
  126. unsigned len = strlen(src);
  127. guarantee(len);
  128. memcpy(dst, src, len);
  129. dst += len;
  130. }
  131. /* HEADROOM bytes after dst are available after this call */
  132. static void fmt_url(/*char *dst,*/ const char *name)
  133. {
  134. while (*name) {
  135. unsigned c = *name++;
  136. guarantee(3);
  137. *dst = c;
  138. if ((c - '0') > 9 /* not a digit */
  139. && ((c|0x20) - 'a') > ('z' - 'a') /* not A-Z or a-z */
  140. && !strchr("._-+@", c)
  141. ) {
  142. *dst++ = '%';
  143. *dst++ = "0123456789ABCDEF"[c >> 4];
  144. *dst = "0123456789ABCDEF"[c & 0xf];
  145. }
  146. dst++;
  147. }
  148. }
  149. /* HEADROOM bytes are available after dst after this call */
  150. static void fmt_html(/*char *dst,*/ const char *name)
  151. {
  152. while (*name) {
  153. char c = *name++;
  154. if (c == '<')
  155. fmt_str("&lt;");
  156. else if (c == '>')
  157. fmt_str("&gt;");
  158. else if (c == '&') {
  159. fmt_str("&amp;");
  160. } else {
  161. guarantee(1);
  162. *dst++ = c;
  163. continue;
  164. }
  165. }
  166. }
  167. /* HEADROOM bytes are available after dst after this call */
  168. static void fmt_ull(/*char *dst,*/ unsigned long long n)
  169. {
  170. char buf[sizeof(n)*3 + 2];
  171. char *p;
  172. p = buf + sizeof(buf) - 1;
  173. *p = '\0';
  174. do {
  175. *--p = (n % 10) + '0';
  176. n /= 10;
  177. } while (n);
  178. fmt_str(/*dst,*/ p);
  179. }
  180. /* Does not call guarantee - eats into headroom instead */
  181. static void fmt_02u(/*char *dst,*/ unsigned n)
  182. {
  183. /* n %= 100; - not needed, callers don't pass big n */
  184. dst[0] = (n / 10) + '0';
  185. dst[1] = (n % 10) + '0';
  186. dst += 2;
  187. }
  188. /* Does not call guarantee - eats into headroom instead */
  189. static void fmt_04u(/*char *dst,*/ unsigned n)
  190. {
  191. /* n %= 10000; - not needed, callers don't pass big n */
  192. fmt_02u(n / 100);
  193. fmt_02u(n % 100);
  194. }
  195. int main(int argc, char *argv[])
  196. {
  197. dir_list_t *dir_list;
  198. dir_list_t *cdir;
  199. unsigned dir_list_count;
  200. unsigned count_dirs;
  201. unsigned count_files;
  202. unsigned long long size_total;
  203. int odd;
  204. DIR *dirp;
  205. char *location;
  206. location = getenv("REQUEST_URI");
  207. if (!location)
  208. return 1;
  209. /* drop URL arguments if any */
  210. strchrnul(location, '?')[0] = '\0';
  211. if (location[0] != '/'
  212. || strstr(location, "//")
  213. || strstr(location, "/../")
  214. || strcmp(strrchr(location, '/'), "/..") == 0
  215. ) {
  216. return 1;
  217. }
  218. if (chdir("..")
  219. || (location[1] && chdir(location + 1))
  220. ) {
  221. return 1;
  222. }
  223. dirp = opendir(".");
  224. if (!dirp)
  225. return 1;
  226. dir_list = NULL;
  227. dir_list_count = 0;
  228. while (1) {
  229. struct dirent *dp;
  230. struct stat sb;
  231. dp = readdir(dirp);
  232. if (!dp)
  233. break;
  234. if (dp->d_name[0] == '.' && !dp->d_name[1])
  235. continue;
  236. if (stat(dp->d_name, &sb) != 0)
  237. continue;
  238. dir_list = realloc(dir_list, (dir_list_count + 1) * sizeof(dir_list[0]));
  239. dir_list[dir_list_count].dl_name = strdup(dp->d_name);
  240. dir_list[dir_list_count].dl_mode = sb.st_mode;
  241. dir_list[dir_list_count].dl_size = sb.st_size;
  242. dir_list[dir_list_count].dl_mtime = sb.st_mtime;
  243. dir_list_count++;
  244. }
  245. closedir(dirp);
  246. qsort(dir_list, dir_list_count, sizeof(dir_list[0]), (void*)compare_dl);
  247. fmt_str(
  248. "" /* Additional headers (currently none) */
  249. "\r\n" /* Mandatory empty line after headers */
  250. "<html><head><title>Index of ");
  251. /* Guard against directories with &, > etc */
  252. fmt_html(location);
  253. fmt_str(
  254. "</title>\n"
  255. STYLE_STR
  256. "</head>" "\n"
  257. "<body>" "\n"
  258. "<h1>Index of ");
  259. fmt_html(location);
  260. fmt_str(
  261. "</h1>" "\n"
  262. "<table>" "\n"
  263. "<col class=nm><col class=sz><col class=dt>" "\n"
  264. "<tr class=hdr><th class=cnt>Name<th class=sz>Size<th class=dt>Last modified" "\n");
  265. odd = 0;
  266. count_dirs = 0;
  267. count_files = 0;
  268. size_total = 0;
  269. cdir = dir_list;
  270. while (dir_list_count--) {
  271. struct tm *ptm;
  272. if (S_ISDIR(cdir->dl_mode)) {
  273. count_dirs++;
  274. } else if (S_ISREG(cdir->dl_mode)) {
  275. count_files++;
  276. size_total += cdir->dl_size;
  277. } else
  278. goto next;
  279. fmt_str("<tr class=");
  280. *dst++ = (odd ? 'o' : 'e');
  281. fmt_str("><td class=nm><a href='");
  282. fmt_url(cdir->dl_name); /* %20 etc */
  283. if (S_ISDIR(cdir->dl_mode))
  284. *dst++ = '/';
  285. fmt_str("'>");
  286. fmt_html(cdir->dl_name); /* &lt; etc */
  287. if (S_ISDIR(cdir->dl_mode))
  288. *dst++ = '/';
  289. fmt_str("</a><td class=sz>");
  290. if (S_ISREG(cdir->dl_mode))
  291. fmt_ull(cdir->dl_size);
  292. fmt_str("<td class=dt>");
  293. ptm = gmtime(&cdir->dl_mtime);
  294. fmt_04u(1900 + ptm->tm_year); *dst++ = '-';
  295. fmt_02u(ptm->tm_mon + 1); *dst++ = '-';
  296. fmt_02u(ptm->tm_mday); *dst++ = ' ';
  297. fmt_02u(ptm->tm_hour); *dst++ = ':';
  298. fmt_02u(ptm->tm_min); *dst++ = ':';
  299. fmt_02u(ptm->tm_sec);
  300. *dst++ = '\n';
  301. odd = 1 - odd;
  302. next:
  303. cdir++;
  304. }
  305. fmt_str("<tr class=foot><th class=cnt>Files: ");
  306. fmt_ull(count_files);
  307. /* count_dirs - 1: we don't want to count ".." */
  308. fmt_str(", directories: ");
  309. fmt_ull(count_dirs - 1);
  310. fmt_str("<th class=sz>");
  311. fmt_ull(size_total);
  312. fmt_str("<th class=dt>\n");
  313. /* "</table></body></html>" - why bother? */
  314. guarantee(BUFFER_SIZE * 2); /* flush */
  315. return 0;
  316. }