httpd_indexcgi.c 9.6 KB

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