httpd_indexcgi.c 9.4 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. #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>" \
  49. "table {" \
  50. "width:100%;" \
  51. "background-color:#fff5ee;" \
  52. "border-width:1px;" /* 1px 1px 1px 1px; */ \
  53. "border-spacing:2px;" \
  54. "border-style:solid;" /* solid solid solid solid; */ \
  55. "border-color:black;" /* black black black black; */ \
  56. "border-collapse:collapse" \
  57. "}" \
  58. "th {" \
  59. "border-width:1px;" /* 1px 1px 1px 1px; */ \
  60. "padding:1px;" /* 1px 1px 1px 1px; */ \
  61. "border-style:solid;" /* solid solid solid solid; */ \
  62. "border-color:black" /* black black black black; */ \
  63. "}" \
  64. "td {" \
  65. /* top right bottom left */ \
  66. "border-width:0 1px 0 1px;" \
  67. "padding:1px;" /* 1px 1px 1px 1px; */ \
  68. "border-style:solid;" /* solid solid solid solid; */ \
  69. "border-color:black;" /* black black black black; */ \
  70. "white-space:nowrap" \
  71. "}" \
  72. "tr:nth-child(odd) { background-color:#ffffff }" \
  73. "tr.hdr { background-color:#eee5de }" \
  74. "tr.foot { background-color:#eee5de }" \
  75. "th.cnt { text-align:left }" \
  76. "th.sz { text-align:right }" \
  77. "th.dt { text-align:right }" \
  78. "td.sz { text-align:right }" \
  79. "td.dt { text-align:right }" \
  80. "col.nm { width:98% }" \
  81. "col.sz { width:1% }" \
  82. "col.dt { width:1% }" \
  83. "</style>" \
  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. DIR *dirp;
  203. char *location;
  204. location = getenv("REQUEST_URI");
  205. if (!location)
  206. return 1;
  207. /* drop URL arguments if any */
  208. strchrnul(location, '?')[0] = '\0';
  209. if (location[0] != '/'
  210. || strstr(location, "//")
  211. || strstr(location, "/../")
  212. || strcmp(strrchr(location, '/'), "/..") == 0
  213. ) {
  214. return 1;
  215. }
  216. if (chdir("..")
  217. || (location[1] && chdir(location + 1))
  218. ) {
  219. return 1;
  220. }
  221. dirp = opendir(".");
  222. if (!dirp)
  223. return 1;
  224. dir_list = NULL;
  225. dir_list_count = 0;
  226. while (1) {
  227. struct dirent *dp;
  228. struct stat sb;
  229. dp = readdir(dirp);
  230. if (!dp)
  231. break;
  232. if (dp->d_name[0] == '.' && !dp->d_name[1])
  233. continue;
  234. if (stat(dp->d_name, &sb) != 0)
  235. continue;
  236. dir_list = realloc(dir_list, (dir_list_count + 1) * sizeof(dir_list[0]));
  237. dir_list[dir_list_count].dl_name = strdup(dp->d_name);
  238. dir_list[dir_list_count].dl_mode = sb.st_mode;
  239. dir_list[dir_list_count].dl_size = sb.st_size;
  240. dir_list[dir_list_count].dl_mtime = sb.st_mtime;
  241. dir_list_count++;
  242. }
  243. closedir(dirp);
  244. qsort(dir_list, dir_list_count, sizeof(dir_list[0]), (void*)compare_dl);
  245. fmt_str(
  246. "" /* Additional headers (currently none) */
  247. "\r\n" /* Mandatory empty line after headers */
  248. "<html><head><title>Index of ");
  249. /* Guard against directories with &, > etc */
  250. fmt_html(location);
  251. fmt_str(
  252. "</title>\n"
  253. STYLE_STR
  254. "</head>" "\n"
  255. "<body>" "\n"
  256. "<h1>Index of ");
  257. fmt_html(location);
  258. fmt_str(
  259. "</h1>" "\n"
  260. "<table>" "\n"
  261. "<col class=nm><col class=sz><col class=dt>" "\n"
  262. "<tr class=hdr><th class=cnt>Name<th class=sz>Size<th class=dt>Last modified" "\n");
  263. count_dirs = 0;
  264. count_files = 0;
  265. size_total = 0;
  266. cdir = dir_list;
  267. while (dir_list_count--) {
  268. struct tm *ptm;
  269. if (S_ISDIR(cdir->dl_mode)) {
  270. count_dirs++;
  271. } else if (S_ISREG(cdir->dl_mode)) {
  272. count_files++;
  273. size_total += cdir->dl_size;
  274. } else
  275. goto next;
  276. fmt_str("<tr><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. ptm = gmtime(&cdir->dl_mtime);
  289. fmt_04u(1900 + ptm->tm_year); *dst++ = '-';
  290. fmt_02u(ptm->tm_mon + 1); *dst++ = '-';
  291. fmt_02u(ptm->tm_mday); *dst++ = ' ';
  292. fmt_02u(ptm->tm_hour); *dst++ = ':';
  293. fmt_02u(ptm->tm_min); *dst++ = ':';
  294. fmt_02u(ptm->tm_sec);
  295. *dst++ = '\n';
  296. next:
  297. cdir++;
  298. }
  299. fmt_str("<tr class=foot><th class=cnt>Files: ");
  300. fmt_ull(count_files);
  301. /* count_dirs - 1: we don't want to count ".." */
  302. fmt_str(", directories: ");
  303. fmt_ull(count_dirs - 1);
  304. fmt_str("<th class=sz>");
  305. fmt_ull(size_total);
  306. fmt_str("<th class=dt>\n");
  307. /* "</table></body></html>" - why bother? */
  308. guarantee(BUFFER_SIZE * 2); /* flush */
  309. return 0;
  310. }