webls.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ctype.h>
  4. #include <bio.h>
  5. #include <regexp.h>
  6. #include <fcall.h>
  7. #include "httpd.h"
  8. #include "httpsrv.h"
  9. static Hio *hout;
  10. static Hio houtb;
  11. static HConnect *connect;
  12. static int vermaj, gidwidth, uidwidth, lenwidth, devwidth;
  13. static Biobuf *aio, *dio;
  14. static void
  15. doctype(void)
  16. {
  17. hprint(hout, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n");
  18. hprint(hout, " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
  19. }
  20. void
  21. error(char *title, char *fmt, ...)
  22. {
  23. va_list arg;
  24. char buf[1024], *out;
  25. va_start(arg, fmt);
  26. out = vseprint(buf, buf+sizeof(buf), fmt, arg);
  27. va_end(arg);
  28. *out = 0;
  29. hprint(hout, "%s 404 %s\r\n", hversion, title);
  30. hprint(hout, "Date: %D\r\n", time(nil));
  31. hprint(hout, "Server: Plan9\r\n");
  32. hprint(hout, "Content-type: text/html\r\n");
  33. hprint(hout, "\r\n");
  34. doctype();
  35. hprint(hout, "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
  36. hprint(hout, "<head><title>%s</title></head>\n", title);
  37. hprint(hout, "<body>\n");
  38. hprint(hout, "<h1>%s</h1>\n", title);
  39. hprint(hout, "%s\n", buf);
  40. hprint(hout, "</body>\n");
  41. hprint(hout, "</html>\n");
  42. hflush(hout);
  43. writelog(connect, "Reply: 404\nReason: %s\n", title);
  44. exits(nil);
  45. }
  46. /*
  47. * Are we actually allowed to look in here?
  48. *
  49. * Rules:
  50. * 1) If neither allowed nor denied files exist, access is granted.
  51. * 2) If allowed exists and denied does not, dir *must* be in allowed
  52. * for access to be granted, otherwise, access is denied.
  53. * 3) If denied exists and allowed does not, dir *must not* be in
  54. * denied for access to be granted, otherwise, access is enied.
  55. * 4) If both exist, okay if either (a) file is not in denied, or
  56. * (b) in denied and in allowed. Otherwise, access is denied.
  57. */
  58. static Reprog *
  59. getre(Biobuf *buf)
  60. {
  61. Reprog *re;
  62. char *p, *t;
  63. if (buf == nil)
  64. return(nil);
  65. for ( ; ; free(p)) {
  66. p = Brdstr(buf, '\n', 0);
  67. if (p == nil)
  68. return(nil);
  69. t = strchr(p, '#');
  70. if (t != nil)
  71. *t = '\0';
  72. t = p + strlen(p);
  73. while (--t > p && isspace(*t))
  74. *t = '\0';
  75. if (strlen(p) == 0)
  76. continue;
  77. re = regcomp(p);
  78. if (re == nil)
  79. continue;
  80. free(p);
  81. return(re);
  82. }
  83. }
  84. static int
  85. allowed(char *dir)
  86. {
  87. Reprog *re;
  88. int okay;
  89. Resub match;
  90. if (strcmp(dir, "..") == 0 || strncmp(dir, "../", 3) == 0)
  91. return(0);
  92. if (aio == nil && dio == nil)
  93. return(1);
  94. if (aio != nil)
  95. Bseek(aio, 0, 0);
  96. if (dio != nil)
  97. Bseek(dio, 0, 0);
  98. okay = (dio != nil);
  99. while (okay && (re = getre(dio)) != nil) {
  100. memset(&match, 0, sizeof(match));
  101. okay = (regexec(re, dir, &match, 1) != 1);
  102. free(re);
  103. }
  104. if (aio == nil)
  105. return(okay);
  106. while (!okay && (re = getre(aio)) != nil) {
  107. memset(&match, 0, sizeof(match));
  108. okay = (regexec(re, dir, &match, 1) == 1);
  109. free(re);
  110. }
  111. return(okay);
  112. }
  113. /*
  114. * Comparison routine for sorting the directory.
  115. */
  116. static int
  117. compar(Dir *a, Dir *b)
  118. {
  119. return(strcmp(a->name, b->name));
  120. }
  121. /*
  122. * These is for formating; how wide are variable-length
  123. * fields?
  124. */
  125. static void
  126. maxwidths(Dir *dp, long n)
  127. {
  128. long i;
  129. char scratch[64];
  130. for (i = 0; i < n; i++) {
  131. if (snprint(scratch, sizeof scratch, "%ud", dp[i].dev) > devwidth)
  132. devwidth = strlen(scratch);
  133. if (strlen(dp[i].uid) > uidwidth)
  134. uidwidth = strlen(dp[i].uid);
  135. if (strlen(dp[i].gid) > gidwidth)
  136. gidwidth = strlen(dp[i].gid);
  137. if (snprint(scratch, sizeof scratch, "%lld", dp[i].length) > lenwidth)
  138. lenwidth = strlen(scratch);
  139. }
  140. }
  141. /*
  142. * Do an actual directory listing.
  143. * asciitime is lifted directly out of ls.
  144. */
  145. char *
  146. asciitime(long l)
  147. {
  148. ulong clk;
  149. static char buf[32];
  150. char *t;
  151. clk = time(nil);
  152. t = ctime(l);
  153. /* 6 months in the past or a day in the future */
  154. if(l<clk-180L*24*60*60 || clk+24L*60*60<l){
  155. memmove(buf, t+4, 7); /* month and day */
  156. memmove(buf+7, t+23, 5); /* year */
  157. }else
  158. memmove(buf, t+4, 12); /* skip day of week */
  159. buf[12] = 0;
  160. return buf;
  161. }
  162. static void
  163. dols(char *dir)
  164. {
  165. Dir *d;
  166. char *f, *p;
  167. long i, n;
  168. int fd;
  169. cleanname(dir);
  170. if (!allowed(dir)) {
  171. error("Permission denied", "<p>Cannot list directory %s: Access prohibited</p>", dir);
  172. return;
  173. }
  174. fd = open(dir, OREAD);
  175. if (fd < 0) {
  176. error("Cannot read directory", "<p>Cannot read directory %s: %r</p>", dir);
  177. return;
  178. }
  179. if (vermaj) {
  180. hokheaders(connect);
  181. hprint(hout, "Content-type: text/html\r\n");
  182. hprint(hout, "\r\n");
  183. }
  184. doctype();
  185. hprint(hout, "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
  186. hprint(hout, "<head><title>Index of %s</title></head>\n", dir);
  187. hprint(hout, "<body>\n");
  188. hprint(hout, "<h1>Index of %s</h1>\n", dir);
  189. n = dirreadall(fd, &d);
  190. close(fd);
  191. maxwidths(d, n);
  192. qsort(d, n, sizeof(Dir), (int (*)(void *, void *))compar);
  193. hprint(hout, "<pre>\n");
  194. for (i = 0; i < n; i++) {
  195. f = smprint("%s/%s", dir, d[i].name);
  196. cleanname(f);
  197. if (d[i].mode & DMDIR) {
  198. p = smprint("/magic/webls?dir=%H", f);
  199. free(f);
  200. f = p;
  201. }
  202. hprint(hout, "%M %C %*ud %-*s %-*s %*lld %s <a href=\"%s\">%s</a>\n",
  203. d[i].mode, d[i].type,
  204. devwidth, d[i].dev,
  205. uidwidth, d[i].uid,
  206. gidwidth, d[i].gid,
  207. lenwidth, d[i].length,
  208. asciitime(d[i].mtime), f, d[i].name);
  209. free(f);
  210. }
  211. f = smprint("%s/..", dir);
  212. cleanname(f);
  213. if (allowed(f))
  214. hprint(hout, "\nGo to <a href=\"/magic/webls?dir=%H\">parent</a> directory\n", f);
  215. else
  216. hprint(hout, "\nEnd of directory listing\n");
  217. free(f);
  218. hprint(hout, "</pre>\n</body>\n</html>\n");
  219. hflush(hout);
  220. free(d);
  221. }
  222. /*
  223. * Handle unpacking the request in the URI and
  224. * invoking the actual handler.
  225. */
  226. static void
  227. dosearch(char *search)
  228. {
  229. if (strncmp(search, "dir=", 4) == 0){
  230. search = hurlunesc(connect, search+4);
  231. dols(search);
  232. return;
  233. }
  234. /*
  235. * Otherwise, we've gotten an illegal request.
  236. * spit out a non-apologetic error.
  237. */
  238. search = hurlunesc(connect, search);
  239. error("Bad directory listing request",
  240. "<p>Illegal formatted directory listing request:</p>\n"
  241. "<p>%H</p>", search);
  242. }
  243. void
  244. main(int argc, char **argv)
  245. {
  246. fmtinstall('H', httpfmt);
  247. fmtinstall('U', hurlfmt);
  248. fmtinstall('M', dirmodefmt);
  249. aio = Bopen("/sys/lib/webls.allowed", OREAD);
  250. dio = Bopen("/sys/lib/webls.denied", OREAD);
  251. if(argc == 2){
  252. hinit(&houtb, 1, Hwrite);
  253. hout = &houtb;
  254. dols(argv[1]);
  255. exits(nil);
  256. }
  257. close(2);
  258. connect = init(argc, argv);
  259. hout = &connect->hout;
  260. vermaj = connect->req.vermaj;
  261. if(hparseheaders(connect, HSTIMEOUT) < 0)
  262. exits("failed");
  263. if(strcmp(connect->req.meth, "GET") != 0 && strcmp(connect->req.meth, "HEAD") != 0){
  264. hunallowed(connect, "GET, HEAD");
  265. exits("not allowed");
  266. }
  267. if(connect->head.expectother || connect->head.expectcont){
  268. hfail(connect, HExpectFail, nil);
  269. exits("failed");
  270. }
  271. bind("/usr/web", "/", MREPL);
  272. if(connect->req.search != nil)
  273. dosearch(connect->req.search);
  274. else
  275. error("Bad argument", "<p>Need a search argument</p>");
  276. hflush(hout);
  277. writelog(connect, "200 webls %ld %ld\n", hout->seek, hout->seek);
  278. exits(nil);
  279. }