httpd_indexcgi.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 *location;
  205. location = getenv("REQUEST_URI");
  206. if (!location)
  207. return 1;
  208. /* drop URL arguments if any */
  209. strchrnul(location, '?')[0] = '\0';
  210. if (location[0] != '/'
  211. || strstr(location, "//")
  212. || strstr(location, "/../")
  213. || strcmp(strrchr(location, '/'), "/..") == 0
  214. ) {
  215. return 1;
  216. }
  217. if (chdir("..")
  218. || (location[1] && chdir(location + 1))
  219. ) {
  220. return 1;
  221. }
  222. dirp = opendir(".");
  223. if (!dirp)
  224. return 1;
  225. dir_list = NULL;
  226. dir_list_count = 0;
  227. while (1) {
  228. struct dirent *dp;
  229. struct stat sb;
  230. dp = readdir(dirp);
  231. if (!dp)
  232. break;
  233. if (dp->d_name[0] == '.' && !dp->d_name[1])
  234. continue;
  235. if (stat(dp->d_name, &sb) != 0)
  236. continue;
  237. dir_list = realloc(dir_list, (dir_list_count + 1) * sizeof(dir_list[0]));
  238. dir_list[dir_list_count].dl_name = strdup(dp->d_name);
  239. dir_list[dir_list_count].dl_mode = sb.st_mode;
  240. dir_list[dir_list_count].dl_size = sb.st_size;
  241. dir_list[dir_list_count].dl_mtime = sb.st_mtime;
  242. dir_list_count++;
  243. }
  244. closedir(dirp);
  245. qsort(dir_list, dir_list_count, sizeof(dir_list[0]), (void*)compare_dl);
  246. fmt_str(
  247. "" /* Additional headers (currently none) */
  248. "\r\n" /* Mandatory empty line after headers */
  249. "<html><head><title>Index of ");
  250. /* Guard against directories with &, > etc */
  251. fmt_html(location);
  252. fmt_str(
  253. "</title>\n"
  254. STYLE_STR
  255. "</head>" "\n"
  256. "<body>" "\n"
  257. "<h1>Index of ");
  258. fmt_html(location);
  259. fmt_str(
  260. "</h1>" "\n"
  261. "<table>" "\n"
  262. "<col class=nm><col class=sz><col class=dt>" "\n"
  263. "<tr class=hdr><th class=cnt>Name<th class=sz>Size<th class=dt>Last modified" "\n");
  264. odd = 0;
  265. count_dirs = 0;
  266. count_files = 0;
  267. size_total = 0;
  268. cdir = dir_list;
  269. while (dir_list_count--) {
  270. struct tm *ptm;
  271. if (S_ISDIR(cdir->dl_mode)) {
  272. count_dirs++;
  273. } else if (S_ISREG(cdir->dl_mode)) {
  274. count_files++;
  275. size_total += cdir->dl_size;
  276. } else
  277. goto next;
  278. fmt_str("<tr class=");
  279. *dst++ = (odd ? 'o' : 'e');
  280. fmt_str("><td class=nm><a href='");
  281. fmt_url(cdir->dl_name); /* %20 etc */
  282. if (S_ISDIR(cdir->dl_mode))
  283. *dst++ = '/';
  284. fmt_str("'>");
  285. fmt_html(cdir->dl_name); /* &lt; etc */
  286. if (S_ISDIR(cdir->dl_mode))
  287. *dst++ = '/';
  288. fmt_str("</a><td class=sz>");
  289. if (S_ISREG(cdir->dl_mode))
  290. fmt_ull(cdir->dl_size);
  291. fmt_str("<td class=dt>");
  292. ptm = gmtime(&cdir->dl_mtime);
  293. fmt_04u(1900 + ptm->tm_year); *dst++ = '-';
  294. fmt_02u(ptm->tm_mon + 1); *dst++ = '-';
  295. fmt_02u(ptm->tm_mday); *dst++ = ' ';
  296. fmt_02u(ptm->tm_hour); *dst++ = ':';
  297. fmt_02u(ptm->tm_min); *dst++ = ':';
  298. fmt_02u(ptm->tm_sec);
  299. *dst++ = '\n';
  300. odd = 1 - odd;
  301. next:
  302. cdir++;
  303. }
  304. fmt_str("<tr class=foot><th class=cnt>Files: ");
  305. fmt_ull(count_files);
  306. /* count_dirs - 1: we don't want to count ".." */
  307. fmt_str(", directories: ");
  308. fmt_ull(count_dirs - 1);
  309. fmt_str("<th class=sz>");
  310. fmt_ull(size_total);
  311. fmt_str("<th class=dt>\n");
  312. /* "</table></body></html>" - why bother? */
  313. guarantee(BUFFER_SIZE * 2); /* flush */
  314. return 0;
  315. }