httpd_indexcgi.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <errno.h>
  36. #include <stdint.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <unistd.h>
  40. #include <stdio.h>
  41. #include <dirent.h>
  42. #include <time.h>
  43. /* Appearance of the table is controlled by style sheet *ONLY*,
  44. * formatting code uses <TAG class=CLASS> to apply style
  45. * to elements. Edit stylesheet to your liking and recompile. */
  46. #define STYLE_STR \
  47. "<style>" "\n"\
  48. "table {" "\n"\
  49. "width:100%;" "\n"\
  50. "background-color:#fff5ee;" "\n"\
  51. "border-width:1px;" /* 1px 1px 1px 1px; */ "\n"\
  52. "border-spacing:2px;" "\n"\
  53. "border-style:solid;" /* solid solid solid solid; */ "\n"\
  54. "border-color:black;" /* black black black black; */ "\n"\
  55. "border-collapse:collapse;" "\n"\
  56. "}" "\n"\
  57. "th {" "\n"\
  58. "border-width:1px;" /* 1px 1px 1px 1px; */ "\n"\
  59. "padding:1px;" /* 1px 1px 1px 1px; */ "\n"\
  60. "border-style:solid;" /* solid solid solid solid; */ "\n"\
  61. "border-color:black;" /* black black black black; */ "\n"\
  62. "}" "\n"\
  63. "td {" "\n"\
  64. /* top right bottom left */ \
  65. "border-width:0px 1px 0px 1px;" "\n"\
  66. "padding:1px;" /* 1px 1px 1px 1px; */ "\n"\
  67. "border-style:solid;" /* solid solid solid solid; */ "\n"\
  68. "border-color:black;" /* black black black black; */ "\n"\
  69. "white-space:nowrap;" "\n"\
  70. "}" "\n"\
  71. "tr.hdr { background-color:#eee5de; }" "\n"\
  72. "tr.o { background-color:#ffffff; }" "\n"\
  73. /* tr.e { ... } - for even rows (currently none) */ \
  74. "tr.foot { background-color:#eee5de; }" "\n"\
  75. "th.cnt { text-align:left; }" "\n"\
  76. "th.sz { text-align:right; }" "\n"\
  77. "th.dt { text-align:right; }" "\n"\
  78. "td.sz { text-align:right; }" "\n"\
  79. "td.dt { text-align:right; }" "\n"\
  80. "col.nm { width:98%; }" "\n"\
  81. "col.sz { width:1%; }" "\n"\
  82. "col.dt { width:1%; }" "\n"\
  83. "</style>" "\n"\
  84. typedef struct dir_list_t {
  85. char *dl_name;
  86. mode_t dl_mode;
  87. off_t dl_size;
  88. time_t dl_mtime;
  89. } dir_list_t;
  90. static int compare_dl(dir_list_t *a, dir_list_t *b)
  91. {
  92. /* ".." is 'less than' any other dir entry */
  93. if (strcmp(a->dl_name, "..") == 0) {
  94. return -1;
  95. }
  96. if (strcmp(b->dl_name, "..") == 0) {
  97. return 1;
  98. }
  99. if (S_ISDIR(a->dl_mode) != S_ISDIR(b->dl_mode)) {
  100. /* 1 if b is a dir (and thus a is 'after' b, a > b),
  101. * else -1 (a < b) */
  102. return (S_ISDIR(b->dl_mode) != 0) ? 1 : -1;
  103. }
  104. return strcmp(a->dl_name, b->dl_name);
  105. }
  106. static char buffer[2*1024 > sizeof(STYLE_STR) ? 2*1024 : sizeof(STYLE_STR)];
  107. static char *dst = buffer;
  108. enum {
  109. BUFFER_SIZE = sizeof(buffer),
  110. HEADROOM = 64,
  111. };
  112. /* After this call, you have at least size + HEADROOM bytes available
  113. * ahead of dst */
  114. static void guarantee(int size)
  115. {
  116. if (buffer + (BUFFER_SIZE-HEADROOM) - dst >= size)
  117. return;
  118. write(STDOUT_FILENO, buffer, dst - buffer);
  119. dst = buffer;
  120. }
  121. /* NB: formatters do not store terminating NUL! */
  122. /* HEADROOM bytes are available after dst after this call */
  123. static void fmt_str(/*char *dst,*/ const char *src)
  124. {
  125. unsigned len = strlen(src);
  126. guarantee(len);
  127. memcpy(dst, src, len);
  128. dst += len;
  129. }
  130. /* HEADROOM bytes after dst are available after this call */
  131. static void fmt_url(/*char *dst,*/ const char *name)
  132. {
  133. while (*name) {
  134. unsigned c = *name++;
  135. guarantee(3);
  136. *dst = c;
  137. if ((c - '0') > 9 /* not a digit */
  138. && ((c|0x20) - 'a') > ('z' - 'a') /* not A-Z or a-z */
  139. && !strchr("._-+@", c)
  140. ) {
  141. *dst++ = '%';
  142. *dst++ = "0123456789ABCDEF"[c >> 4];
  143. *dst = "0123456789ABCDEF"[c & 0xf];
  144. }
  145. dst++;
  146. }
  147. }
  148. /* HEADROOM bytes are available after dst after this call */
  149. static void fmt_html(/*char *dst,*/ const char *name)
  150. {
  151. while (*name) {
  152. char c = *name++;
  153. if (c == '<')
  154. fmt_str("&lt;");
  155. else if (c == '>')
  156. fmt_str("&gt;");
  157. else if (c == '&') {
  158. fmt_str("&amp;");
  159. } else {
  160. guarantee(1);
  161. *dst++ = c;
  162. continue;
  163. }
  164. }
  165. }
  166. /* HEADROOM bytes are available after dst after this call */
  167. static void fmt_ull(/*char *dst,*/ unsigned long long n)
  168. {
  169. char buf[sizeof(n)*3 + 2];
  170. char *p;
  171. p = buf + sizeof(buf) - 1;
  172. *p = '\0';
  173. do {
  174. *--p = (n % 10) + '0';
  175. n /= 10;
  176. } while (n);
  177. fmt_str(/*dst,*/ p);
  178. }
  179. /* Does not call guarantee - eats into headroom instead */
  180. static void fmt_02u(/*char *dst,*/ unsigned n)
  181. {
  182. /* n %= 100; - not needed, callers don't pass big n */
  183. dst[0] = (n / 10) + '0';
  184. dst[1] = (n % 10) + '0';
  185. dst += 2;
  186. }
  187. /* Does not call guarantee - eats into headroom instead */
  188. static void fmt_04u(/*char *dst,*/ unsigned n)
  189. {
  190. /* n %= 10000; - not needed, callers don't pass big n */
  191. fmt_02u(n / 100);
  192. fmt_02u(n % 100);
  193. }
  194. int main(int argc, char *argv[])
  195. {
  196. dir_list_t *dir_list;
  197. dir_list_t *cdir;
  198. unsigned dir_list_count;
  199. unsigned count_dirs;
  200. unsigned count_files;
  201. unsigned long long size_total;
  202. int odd;
  203. DIR *dirp;
  204. char *QUERY_STRING;
  205. QUERY_STRING = getenv("QUERY_STRING");
  206. if (!QUERY_STRING
  207. || QUERY_STRING[0] != '/'
  208. || strstr(QUERY_STRING, "//")
  209. || strstr(QUERY_STRING, "/../")
  210. || strcmp(strrchr(QUERY_STRING, '/'), "/..") == 0
  211. ) {
  212. return 1;
  213. }
  214. if (chdir("..")
  215. || (QUERY_STRING[1] && chdir(QUERY_STRING + 1))
  216. ) {
  217. return 1;
  218. }
  219. dirp = opendir(".");
  220. if (!dirp)
  221. return 1;
  222. dir_list = NULL;
  223. dir_list_count = 0;
  224. while (1) {
  225. struct dirent *dp;
  226. struct stat sb;
  227. dp = readdir(dirp);
  228. if (!dp)
  229. break;
  230. if (dp->d_name[0] == '.' && !dp->d_name[1])
  231. continue;
  232. if (stat(dp->d_name, &sb) != 0)
  233. continue;
  234. dir_list = realloc(dir_list, (dir_list_count + 1) * sizeof(dir_list[0]));
  235. dir_list[dir_list_count].dl_name = strdup(dp->d_name);
  236. dir_list[dir_list_count].dl_mode = sb.st_mode;
  237. dir_list[dir_list_count].dl_size = sb.st_size;
  238. dir_list[dir_list_count].dl_mtime = sb.st_mtime;
  239. dir_list_count++;
  240. }
  241. closedir(dirp);
  242. qsort(dir_list, dir_list_count, sizeof(dir_list[0]), (void*)compare_dl);
  243. fmt_str(
  244. "" /* Additional headers (currently none) */
  245. "\r\n" /* Mandatory empty line after headers */
  246. "<html><head><title>Index of ");
  247. /* Guard against directories with &, > etc */
  248. fmt_html(QUERY_STRING);
  249. fmt_str(
  250. "</title>\n"
  251. STYLE_STR
  252. "</head>" "\n"
  253. "<body>" "\n"
  254. "<h1>Index of ");
  255. fmt_html(QUERY_STRING);
  256. fmt_str(
  257. "</h1>" "\n"
  258. "<table>" "\n"
  259. "<col class=nm><col class=sz><col class=dt>" "\n"
  260. "<tr class=hdr><th class=cnt>Name<th class=sz>Size<th class=dt>Last modified" "\n");
  261. odd = 0;
  262. count_dirs = 0;
  263. count_files = 0;
  264. size_total = 0;
  265. cdir = dir_list;
  266. while (dir_list_count--) {
  267. struct tm *ptm;
  268. if (S_ISDIR(cdir->dl_mode)) {
  269. count_dirs++;
  270. } else if (S_ISREG(cdir->dl_mode)) {
  271. count_files++;
  272. size_total += cdir->dl_size;
  273. } else
  274. goto next;
  275. fmt_str("<tr class=");
  276. *dst++ = (odd ? 'o' : 'e');
  277. fmt_str("><td class=nm><a href='");
  278. fmt_url(cdir->dl_name); /* %20 etc */
  279. if (S_ISDIR(cdir->dl_mode))
  280. *dst++ = '/';
  281. fmt_str("'>");
  282. fmt_html(cdir->dl_name); /* &lt; etc */
  283. if (S_ISDIR(cdir->dl_mode))
  284. *dst++ = '/';
  285. fmt_str("</a><td class=sz>");
  286. if (S_ISREG(cdir->dl_mode))
  287. fmt_ull(cdir->dl_size);
  288. fmt_str("<td class=dt>");
  289. ptm = gmtime(&cdir->dl_mtime);
  290. fmt_04u(1900 + ptm->tm_year); *dst++ = '-';
  291. fmt_02u(ptm->tm_mon + 1); *dst++ = '-';
  292. fmt_02u(ptm->tm_mday); *dst++ = ' ';
  293. fmt_02u(ptm->tm_hour); *dst++ = ':';
  294. fmt_02u(ptm->tm_min); *dst++ = ':';
  295. fmt_02u(ptm->tm_sec);
  296. *dst++ = '\n';
  297. odd = 1 - odd;
  298. next:
  299. cdir++;
  300. }
  301. fmt_str("<tr class=foot><th class=cnt>Files: ");
  302. fmt_ull(count_files);
  303. /* count_dirs - 1: we don't want to count ".." */
  304. fmt_str(", directories: ");
  305. fmt_ull(count_dirs - 1);
  306. fmt_str("<th class=sz>");
  307. fmt_ull(size_total);
  308. fmt_str("<th class=dt>\n");
  309. /* "</table></body></html>" - why bother? */
  310. guarantee(BUFFER_SIZE * 2); /* flush */
  311. return 0;
  312. }