webls.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. char *bbuf;
  64. int n;
  65. if (buf == nil)
  66. return(nil);
  67. for ( ; ; free(p)) {
  68. p = Brdstr(buf, '\n', 0);
  69. if (p == nil)
  70. return(nil);
  71. t = strchr(p, '#');
  72. if (t != nil)
  73. *t = '\0';
  74. t = p + strlen(p);
  75. while (--t > p && isspace(*t))
  76. *t = '\0';
  77. n = strlen(p);
  78. if (n == 0)
  79. continue;
  80. /* root the regular expresssion */
  81. bbuf = malloc(n+2);
  82. if(bbuf == nil)
  83. sysfatal("out of memory");
  84. bbuf[0] = '^';
  85. strcpy(bbuf+1, p);
  86. re = regcomp(bbuf);
  87. free(bbuf);
  88. if (re == nil)
  89. continue;
  90. free(p);
  91. return(re);
  92. }
  93. }
  94. static int
  95. allowed(char *dir)
  96. {
  97. Reprog *re;
  98. int okay;
  99. Resub match;
  100. if (strcmp(dir, "..") == 0 || strncmp(dir, "../", 3) == 0)
  101. return(0);
  102. if (aio == nil)
  103. return(0);
  104. if (aio != nil)
  105. Bseek(aio, 0, 0);
  106. if (dio != nil)
  107. Bseek(dio, 0, 0);
  108. /* if no deny list, assume everything is denied */
  109. okay = (dio != nil);
  110. /* go through denials till we find a match */
  111. while (okay && (re = getre(dio)) != nil) {
  112. memset(&match, 0, sizeof(match));
  113. okay = (regexec(re, dir, &match, 1) != 1);
  114. free(re);
  115. }
  116. /* go through accepts till we have a match */
  117. if (aio == nil)
  118. return(okay);
  119. while (!okay && (re = getre(aio)) != nil) {
  120. memset(&match, 0, sizeof(match));
  121. okay = (regexec(re, dir, &match, 1) == 1);
  122. free(re);
  123. }
  124. return(okay);
  125. }
  126. /*
  127. * Comparison routine for sorting the directory.
  128. */
  129. static int
  130. compar(Dir *a, Dir *b)
  131. {
  132. return(strcmp(a->name, b->name));
  133. }
  134. /*
  135. * These is for formating; how wide are variable-length
  136. * fields?
  137. */
  138. static void
  139. maxwidths(Dir *dp, long n)
  140. {
  141. long i;
  142. char scratch[64];
  143. for (i = 0; i < n; i++) {
  144. if (snprint(scratch, sizeof scratch, "%ud", dp[i].dev) > devwidth)
  145. devwidth = strlen(scratch);
  146. if (strlen(dp[i].uid) > uidwidth)
  147. uidwidth = strlen(dp[i].uid);
  148. if (strlen(dp[i].gid) > gidwidth)
  149. gidwidth = strlen(dp[i].gid);
  150. if (snprint(scratch, sizeof scratch, "%lld", dp[i].length) > lenwidth)
  151. lenwidth = strlen(scratch);
  152. }
  153. }
  154. /*
  155. * Do an actual directory listing.
  156. * asciitime is lifted directly out of ls.
  157. */
  158. char *
  159. asciitime(long l)
  160. {
  161. ulong clk;
  162. static char buf[32];
  163. char *t;
  164. clk = time(nil);
  165. t = ctime(l);
  166. /* 6 months in the past or a day in the future */
  167. if(l<clk-180L*24*60*60 || clk+24L*60*60<l){
  168. memmove(buf, t+4, 7); /* month and day */
  169. memmove(buf+7, t+23, 5); /* year */
  170. }else
  171. memmove(buf, t+4, 12); /* skip day of week */
  172. buf[12] = 0;
  173. return buf;
  174. }
  175. static void
  176. dols(char *dir)
  177. {
  178. Dir *d;
  179. char *f, *p,*nm;
  180. long i, n;
  181. int fd;
  182. cleanname(dir); // expands "" to "."; ``dir+1'' access below depends on that
  183. if (!allowed(dir)) {
  184. error("Permission denied", "<p>Cannot list directory %s: Access prohibited</p>", dir);
  185. return;
  186. }
  187. fd = open(dir, OREAD);
  188. if (fd < 0) {
  189. error("Cannot read directory", "<p>Cannot read directory %s: %r</p>", dir);
  190. return;
  191. }
  192. if (vermaj) {
  193. hokheaders(connect);
  194. hprint(hout, "Content-type: text/html\r\n");
  195. hprint(hout, "\r\n");
  196. }
  197. doctype();
  198. hprint(hout, "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
  199. hprint(hout, "<head><title>Index of %s</title></head>\n", dir);
  200. hprint(hout, "<body>\n");
  201. hprint(hout, "<h1>Index of ");
  202. nm = dir;
  203. while((p = strchr(nm, '/')) != nil){
  204. *p = '\0';
  205. f = (*dir == '\0') ? "/" : dir;
  206. if (!(*dir == '\0' && *(dir+1) == '\0') && allowed(f))
  207. hprint(hout, "<a href=\"/magic/webls?dir=%H\">%s/</a>", f, nm);
  208. else
  209. hprint(hout, "%s/", nm);
  210. *p = '/';
  211. nm = p+1;
  212. }
  213. hprint(hout, "%s</h1>\n", nm);
  214. n = dirreadall(fd, &d);
  215. close(fd);
  216. maxwidths(d, n);
  217. qsort(d, n, sizeof(Dir), (int (*)(void *, void *))compar);
  218. hprint(hout, "<pre>\n");
  219. for (i = 0; i < n; i++) {
  220. f = smprint("%s/%s", dir, d[i].name);
  221. cleanname(f);
  222. if (d[i].mode & DMDIR) {
  223. p = smprint("/magic/webls?dir=%H", f);
  224. free(f);
  225. f = p;
  226. }
  227. hprint(hout, "%M %C %*ud %-*s %-*s %*lld %s <a href=\"%s\">%s</a>\n",
  228. d[i].mode, d[i].type,
  229. devwidth, d[i].dev,
  230. uidwidth, d[i].uid,
  231. gidwidth, d[i].gid,
  232. lenwidth, d[i].length,
  233. asciitime(d[i].mtime), f, d[i].name);
  234. free(f);
  235. }
  236. f = smprint("%s/..", dir);
  237. cleanname(f);
  238. if (strcmp(f, dir) != 0 && allowed(f))
  239. hprint(hout, "\nGo to <a href=\"/magic/webls?dir=%H\">parent</a> directory\n", f);
  240. else
  241. hprint(hout, "\nEnd of directory listing\n");
  242. free(f);
  243. hprint(hout, "</pre>\n</body>\n</html>\n");
  244. hflush(hout);
  245. free(d);
  246. }
  247. /*
  248. * Handle unpacking the request in the URI and
  249. * invoking the actual handler.
  250. */
  251. static void
  252. dosearch(char *search)
  253. {
  254. if (strncmp(search, "dir=", 4) == 0){
  255. search = hurlunesc(connect, search+4);
  256. dols(search);
  257. return;
  258. }
  259. /*
  260. * Otherwise, we've gotten an illegal request.
  261. * spit out a non-apologetic error.
  262. */
  263. search = hurlunesc(connect, search);
  264. error("Bad directory listing request",
  265. "<p>Illegal formatted directory listing request:</p>\n"
  266. "<p>%H</p>", search);
  267. }
  268. void
  269. main(int argc, char **argv)
  270. {
  271. fmtinstall('H', httpfmt);
  272. fmtinstall('U', hurlfmt);
  273. fmtinstall('M', dirmodefmt);
  274. aio = Bopen("/sys/lib/webls.allowed", OREAD);
  275. dio = Bopen("/sys/lib/webls.denied", OREAD);
  276. if(argc == 2){
  277. hinit(&houtb, 1, Hwrite);
  278. hout = &houtb;
  279. dols(argv[1]);
  280. exits(nil);
  281. }
  282. close(2);
  283. connect = init(argc, argv);
  284. hout = &connect->hout;
  285. vermaj = connect->req.vermaj;
  286. if(hparseheaders(connect, HSTIMEOUT) < 0)
  287. exits("failed");
  288. if(strcmp(connect->req.meth, "GET") != 0 && strcmp(connect->req.meth, "HEAD") != 0){
  289. hunallowed(connect, "GET, HEAD");
  290. exits("not allowed");
  291. }
  292. if(connect->head.expectother || connect->head.expectcont){
  293. hfail(connect, HExpectFail, nil);
  294. exits("failed");
  295. }
  296. bind("/usr/web", "/", MREPL);
  297. if(connect->req.search != nil)
  298. dosearch(connect->req.search);
  299. else
  300. error("Bad argument", "<p>Need a search argument</p>");
  301. hflush(hout);
  302. writelog(connect, "200 webls %ld %ld\n", hout->seek, hout->seek);
  303. exits(nil);
  304. }