b08f6ef54be1f9f3e47b4544ef2e0f34d86b73ac
[sgt/agedu] / httpd.c
1 /*
2 * httpd.c: implementation of httpd.h.
3 */
4
5 #define _GNU_SOURCE
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <assert.h>
12 #include <unistd.h>
13 #include <pwd.h>
14 #include <ctype.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <arpa/inet.h>
20 #include <netinet/in.h>
21 #include <syslog.h>
22
23 #include "malloc.h"
24 #include "html.h"
25 #include "httpd.h"
26
27 /* --- Logic driving what the web server's responses are. --- */
28
29 enum { /* connctx states */
30 READING_REQ_LINE,
31 READING_HEADERS,
32 DONE
33 };
34
35 struct connctx {
36 const void *t;
37 char *data;
38 int datalen, datasize;
39 char *method, *url, *headers, *auth;
40 int state;
41 };
42
43 /*
44 * Called when a new connection arrives on a listening socket.
45 * Returns a connctx for the new connection.
46 */
47 struct connctx *new_connection(const void *t)
48 {
49 struct connctx *cctx = snew(struct connctx);
50 cctx->t = t;
51 cctx->data = NULL;
52 cctx->datalen = cctx->datasize = 0;
53 cctx->state = READING_REQ_LINE;
54 cctx->method = cctx->url = cctx->headers = cctx->auth = NULL;
55 return cctx;
56 }
57
58 void free_connection(struct connctx *cctx)
59 {
60 sfree(cctx->data);
61 sfree(cctx);
62 }
63
64 static char *http_error(char *code, char *errmsg, char *extraheader,
65 char *errtext, ...)
66 {
67 return dupfmt("HTTP/1.1 %s %s\r\n"
68 "Date: %D\r\n"
69 "Server: agedu\r\n"
70 "Connection: close\r\n"
71 "%s"
72 "Content-Type: text/html; charset=US-ASCII\r\n"
73 "\r\n"
74 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
75 "<HTML><HEAD>\r\n"
76 "<TITLE>%s %s</TITLE>\r\n"
77 "</HEAD><BODY>\r\n"
78 "<H1>%s %s</H1>\r\n"
79 "<P>%s</P>\r\n"
80 "</BODY></HTML>\r\n", code, errmsg,
81 extraheader ? extraheader : "",
82 code, errmsg, code, errmsg, errtext);
83 }
84
85 static char *http_success(char *mimetype, int stuff_cr, char *document)
86 {
87 return dupfmt("HTTP/1.1 200 OK\r\n"
88 "Date: %D\r\n"
89 "Expires: %D\r\n"
90 "Server: agedu\r\n"
91 "Connection: close\r\n"
92 "Content-Type: %s\r\n"
93 "\r\n"
94 "%S", mimetype, stuff_cr, document);
95 }
96
97 /*
98 * Called when data comes in on a connection.
99 *
100 * If this function returns NULL, the platform code continues
101 * reading from the socket. Otherwise, it returns some dynamically
102 * allocated data which the platform code will then write to the
103 * socket before closing it.
104 */
105 char *got_data(struct connctx *ctx, char *data, int length,
106 int magic_access, const char *auth_string,
107 const struct html_config *cfg)
108 {
109 char *line, *p, *q, *r, *z1, *z2, c1, c2;
110 int auth_provided = 0, auth_correct = 0;
111 unsigned long index;
112 char *document, *ret;
113
114 /*
115 * Add the data we've just received to our buffer.
116 */
117 if (ctx->datasize < ctx->datalen + length) {
118 ctx->datasize = (ctx->datalen + length) * 3 / 2 + 4096;
119 ctx->data = sresize(ctx->data, ctx->datasize, char);
120 }
121 memcpy(ctx->data + ctx->datalen, data, length);
122 ctx->datalen += length;
123
124 /*
125 * Gradually process the HTTP request as we receive it.
126 */
127 if (ctx->state == READING_REQ_LINE) {
128 /*
129 * We're waiting for the first line of the input, which
130 * contains the main HTTP request. See if we've got it
131 * yet.
132 */
133
134 line = ctx->data;
135 /*
136 * RFC 2616 section 4.1: `In the interest of robustness,
137 * [...] if the server is reading the protocol stream at
138 * the beginning of a message and receives a CRLF first,
139 * it should ignore the CRLF.'
140 */
141 while (line - ctx->data < ctx->datalen &&
142 (*line == '\r' || *line == '\n'))
143 line++;
144 q = line;
145 while (q - ctx->data < ctx->datalen && *q != '\n')
146 q++;
147 if (q - ctx->data >= ctx->datalen)
148 return NULL; /* not got request line yet */
149
150 /*
151 * We've got the first line of the request. Zero-terminate
152 * and parse it into method, URL and optional HTTP
153 * version.
154 */
155 *q = '\0';
156 ctx->headers = q+1;
157 if (q > line && q[-1] == '\r')
158 *--q = '\0';
159 z1 = z2 = q;
160 c1 = c2 = *q;
161 p = line;
162 while (*p && !isspace((unsigned char)*p)) p++;
163 if (*p) {
164 z1 = p++;
165 c1 = *z1;
166 *z1 = '\0';
167 }
168 while (*p && isspace((unsigned char)*p)) p++;
169 q = p;
170 while (*q && !isspace((unsigned char)*q)) q++;
171 z2 = q++;
172 c2 = *z2;
173 *z2 = '\0';
174 while (*q && isspace((unsigned char)*q)) q++;
175
176 /*
177 * Now `line' points at the method name; p points at the
178 * URL, if any; q points at the HTTP version, if any.
179 */
180
181 /*
182 * There should _be_ a URL, on any request type at all.
183 */
184 if (!*p) {
185 char *ret, *text;
186 /* Restore the request to the way we received it. */
187 *z2 = c2;
188 *z1 = c1;
189 text = dupfmt("<code>agedu</code> received the HTTP request"
190 " \"<code>%h</code>\", which contains no URL.",
191 line);
192 ret = http_error("400", "Bad request", NULL, text);
193 sfree(text);
194 return ret;
195 }
196
197 ctx->method = line;
198 ctx->url = p;
199
200 /*
201 * If there was an HTTP version, we might need to see
202 * headers. Otherwise, the request is done.
203 */
204 if (*q) {
205 ctx->state = READING_HEADERS;
206 } else {
207 ctx->state = DONE;
208 }
209 }
210
211 if (ctx->state == READING_HEADERS) {
212 /*
213 * While we're receiving the HTTP request headers, all we
214 * do is to keep scanning to see if we find two newlines
215 * next to each other.
216 */
217 q = ctx->data + ctx->datalen;
218 for (p = ctx->headers; p < q; p++) {
219 if (*p == '\n' &&
220 ((p+1 < q && p[1] == '\n') ||
221 (p+2 < q && p[1] == '\r' && p[2] == '\n'))) {
222 p[1] = '\0';
223 ctx->state = DONE;
224 break;
225 }
226 }
227 }
228
229 if (ctx->state == DONE) {
230 /*
231 * Now we have the entire HTTP request. Decide what to do
232 * with it.
233 */
234 if (auth_string) {
235 /*
236 * Search the request headers for Authorization.
237 */
238 q = ctx->data + ctx->datalen;
239 for (p = ctx->headers; p < q; p++) {
240 const char *hdr = "Authorization:";
241 int i;
242 for (i = 0; hdr[i]; i++) {
243 if (p >= q || tolower((unsigned char)*p) !=
244 tolower((unsigned char)hdr[i]))
245 break;
246 p++;
247 }
248 if (!hdr[i])
249 break; /* found our header */
250 p = memchr(p, '\n', q - p);
251 if (!p)
252 p = q;
253 }
254 if (p < q) {
255 auth_provided = 1;
256 while (p < q && isspace((unsigned char)*p))
257 p++;
258 r = p;
259 while (p < q && !isspace((unsigned char)*p))
260 p++;
261 if (p < q) {
262 *p++ = '\0';
263 if (!strcasecmp(r, "Basic")) {
264 while (p < q && isspace((unsigned char)*p))
265 p++;
266 r = p;
267 while (p < q && !isspace((unsigned char)*p))
268 p++;
269 if (p < q) {
270 *p++ = '\0';
271 if (!strcmp(r, auth_string))
272 auth_correct = 1;
273 }
274 }
275 }
276 }
277 }
278
279 if (!magic_access && !auth_correct) {
280 if (auth_string && !auth_provided) {
281 ret = http_error("401", "Unauthorized",
282 "WWW-Authenticate: Basic realm=\"agedu\"\r\n",
283 "Please authenticate to view these pages.");
284 } else {
285 ret = http_error("403", "Forbidden", NULL,
286 "This is a restricted-access set of pages.");
287 }
288 } else {
289 p = ctx->url;
290 p += strspn(p, "/?");
291 index = strtoul(p, NULL, 10);
292 document = html_query(ctx->t, index, cfg);
293 if (document) {
294 ret = http_success("text/html", 1, document);
295 sfree(document);
296 } else {
297 ret = http_error("404", "Not Found", NULL,
298 "Pathname index out of range.");
299 }
300 }
301 return ret;
302 } else
303 return NULL;
304 }
305
306 /* --- Platform support for running a web server. --- */
307
308 enum { FD_CLIENT, FD_LISTENER, FD_CONNECTION };
309
310 struct fd {
311 int fd;
312 int type;
313 int deleted;
314 char *wdata;
315 int wdatalen, wdatapos;
316 int magic_access;
317 struct connctx *cctx;
318 };
319
320 struct fd *fds = NULL;
321 int nfds = 0, fdsize = 0;
322
323 struct fd *new_fdstruct(int fd, int type)
324 {
325 struct fd *ret;
326
327 if (nfds >= fdsize) {
328 fdsize = nfds * 3 / 2 + 32;
329 fds = sresize(fds, fdsize, struct fd);
330 }
331
332 ret = &fds[nfds++];
333
334 ret->fd = fd;
335 ret->type = type;
336 ret->wdata = NULL;
337 ret->wdatalen = ret->wdatapos = 0;
338 ret->cctx = NULL;
339 ret->deleted = 0;
340 ret->magic_access = 0;
341
342 return ret;
343 }
344
345 int check_owning_uid(int fd, int flip)
346 {
347 struct sockaddr_in sock, peer;
348 socklen_t addrlen;
349 char linebuf[4096], matchbuf[80];
350 FILE *fp;
351
352 addrlen = sizeof(sock);
353 if (getsockname(fd, (struct sockaddr *)&sock, &addrlen)) {
354 fprintf(stderr, "getsockname: %s\n", strerror(errno));
355 exit(1);
356 }
357 addrlen = sizeof(peer);
358 if (getpeername(fd, (struct sockaddr *)&peer, &addrlen)) {
359 if (errno == ENOTCONN) {
360 peer.sin_addr.s_addr = htonl(0);
361 peer.sin_port = htons(0);
362 } else {
363 fprintf(stderr, "getpeername: %s\n", strerror(errno));
364 exit(1);
365 }
366 }
367
368 if (flip) {
369 struct sockaddr_in tmp = sock;
370 sock = peer;
371 peer = tmp;
372 }
373
374 sprintf(matchbuf, "%08X:%04X %08X:%04X",
375 peer.sin_addr.s_addr, ntohs(peer.sin_port),
376 sock.sin_addr.s_addr, ntohs(sock.sin_port));
377 fp = fopen("/proc/net/tcp", "r");
378 if (fp) {
379 while (fgets(linebuf, sizeof(linebuf), fp)) {
380 if (strlen(linebuf) >= 75 &&
381 !strncmp(linebuf+6, matchbuf, strlen(matchbuf))) {
382 return atoi(linebuf + 75);
383 }
384 }
385 }
386
387 return -1;
388 }
389
390 void check_magic_access(struct fd *fd)
391 {
392 if (check_owning_uid(fd->fd, 0) == getuid())
393 fd->magic_access = 1;
394 }
395
396 static void base64_encode_atom(unsigned char *data, int n, char *out)
397 {
398 static const char base64_chars[] =
399 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
400
401 unsigned word;
402
403 word = data[0] << 16;
404 if (n > 1)
405 word |= data[1] << 8;
406 if (n > 2)
407 word |= data[2];
408 out[0] = base64_chars[(word >> 18) & 0x3F];
409 out[1] = base64_chars[(word >> 12) & 0x3F];
410 if (n > 1)
411 out[2] = base64_chars[(word >> 6) & 0x3F];
412 else
413 out[2] = '=';
414 if (n > 2)
415 out[3] = base64_chars[word & 0x3F];
416 else
417 out[3] = '=';
418 }
419
420 void run_httpd(const void *t, int authmask, const struct html_config *incfg)
421 {
422 int fd;
423 int authtype;
424 char *authstring = NULL, authbuf[512];
425 unsigned long ipaddr;
426 struct fd *f;
427 struct sockaddr_in addr;
428 socklen_t addrlen;
429 struct html_config cfg = *incfg;
430
431 cfg.format = "%lu";
432
433 /*
434 * Establish the listening socket and retrieve its port
435 * number.
436 */
437 fd = socket(PF_INET, SOCK_STREAM, 0);
438 if (fd < 0) {
439 fprintf(stderr, "socket(PF_INET): %s\n", strerror(errno));
440 exit(1);
441 }
442 addr.sin_family = AF_INET;
443 srand(0L);
444 ipaddr = 0x7f000000;
445 ipaddr += (1 + rand() % 255) << 16;
446 ipaddr += (1 + rand() % 255) << 8;
447 ipaddr += (1 + rand() % 255);
448 addr.sin_addr.s_addr = htonl(ipaddr);
449 addr.sin_port = htons(0);
450 addrlen = sizeof(addr);
451 if (bind(fd, (struct sockaddr *)&addr, addrlen) < 0) {
452 fprintf(stderr, "bind: %s\n", strerror(errno));
453 exit(1);
454 }
455 if (listen(fd, 5) < 0) {
456 fprintf(stderr, "listen: %s\n", strerror(errno));
457 exit(1);
458 }
459 addrlen = sizeof(addr);
460 if (getsockname(fd, (struct sockaddr *)&addr, &addrlen)) {
461 fprintf(stderr, "getsockname: %s\n", strerror(errno));
462 exit(1);
463 }
464 if ((authmask & HTTPD_AUTH_MAGIC) &&
465 (check_owning_uid(fd, 1) == getuid())) {
466 authtype = HTTPD_AUTH_MAGIC;
467 printf("Using Linux /proc/net magic authentication\n");
468 } else if ((authmask & HTTPD_AUTH_BASIC)) {
469 char username[128], password[128], userpass[259];
470 const char *rname;
471 unsigned char passbuf[10];
472 int i, j, k, fd;
473
474 authtype = HTTPD_AUTH_BASIC;
475
476 sprintf(username, "agedu");
477 rname = "/dev/urandom";
478 fd = open(rname, O_RDONLY);
479 if (fd < 0) {
480 int err = errno;
481 rname = "/dev/random";
482 fd = open(rname, O_RDONLY);
483 if (fd < 0) {
484 int err2 = errno;
485 fprintf(stderr, "/dev/urandom: open: %s\n", strerror(err));
486 fprintf(stderr, "/dev/random: open: %s\n", strerror(err2));
487 exit(1);
488 }
489 }
490 for (i = 0; i < 10 ;) {
491 j = read(fd, passbuf + i, 10 - i);
492 if (j <= 0) {
493 fprintf(stderr, "%s: read: %s\n", rname,
494 j < 0 ? strerror(errno) : "unexpected EOF");
495 exit(1);
496 }
497 i += j;
498 }
499 close(fd);
500 for (i = 0; i < 16; i++) {
501 /* 32 characters out of the 36 alphanumerics gives me the
502 * latitude to discard i,l,o for being too numeric-looking,
503 * and w because it has two too many syllables and one too
504 * many presidential associations. */
505 static const char chars[32] = "0123456789abcdefghjkmnpqrstuvxyz";
506 int v = 0;
507
508 k = i / 8 * 5;
509 for (j = 0; j < 5; j++)
510 v |= ((passbuf[k+j] >> (i%8)) & 1) << j;
511
512 password[i] = chars[v];
513 }
514 password[i] = '\0';
515
516 printf("Using HTTP Basic authentication\nUsername: %s\nPassword: %s\n",
517 username, password);
518
519 k = sprintf(userpass, "%s:%s", username, password);
520 for (i = j = 0; i < k ;) {
521 int s = k-i < 3 ? k-i : 3;
522 base64_encode_atom((unsigned char *)(userpass+i), s, authbuf+j);
523 i += s;
524 j += 4;
525 }
526 authbuf[j] = '\0';
527 authstring = authbuf;
528 } else {
529 authtype = HTTPD_AUTH_NONE;
530 printf("Web server is unauthenticated\n");
531 }
532 printf("URL: http://%s:%d/\n",
533 inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
534
535 /*
536 * Now construct an fd structure to hold it.
537 */
538 f = new_fdstruct(fd, FD_LISTENER);
539
540 /*
541 * Read from standard input, and treat EOF as a notification
542 * to exit.
543 */
544 new_fdstruct(0, FD_CLIENT);
545
546 /*
547 * Now we're ready to run our main loop. Keep looping round on
548 * select.
549 */
550 while (1) {
551 fd_set rfds, wfds;
552 int i, j, maxfd, ret;
553
554 #define FD_SET_MAX(fd, set, max) \
555 do { FD_SET((fd),(set)); (max) = ((max)<=(fd)?(fd)+1:(max)); } while(0)
556
557 /*
558 * Loop round the fd list putting fds into our select
559 * sets. Also in this loop we remove any that were marked
560 * as deleted in the previous loop.
561 */
562 FD_ZERO(&rfds);
563 FD_ZERO(&wfds);
564 maxfd = 0;
565 for (i = j = 0; j < nfds; j++) {
566
567 if (fds[j].deleted) {
568 sfree(fds[j].wdata);
569 free_connection(fds[j].cctx);
570 continue;
571 }
572 fds[i] = fds[j];
573
574 switch (fds[i].type) {
575 case FD_CLIENT:
576 case FD_LISTENER:
577 FD_SET_MAX(fds[i].fd, &rfds, maxfd);
578 break;
579 case FD_CONNECTION:
580 /*
581 * Always read from a connection socket. Even
582 * after we've started writing, the peer might
583 * still be sending (e.g. because we shamefully
584 * jumped the gun before waiting for the end of
585 * the HTTP request) and so we should be prepared
586 * to read data and throw it away.
587 */
588 FD_SET_MAX(fds[i].fd, &rfds, maxfd);
589 /*
590 * Also attempt to write, if we have data to write.
591 */
592 if (fds[i].wdatapos < fds[i].wdatalen)
593 FD_SET_MAX(fds[i].fd, &wfds, maxfd);
594 break;
595 }
596
597 i++;
598 }
599 nfds = i;
600
601 ret = select(maxfd, &rfds, &wfds, NULL, NULL);
602 if (ret <= 0) {
603 if (ret < 0 && (errno != EINTR)) {
604 fprintf(stderr, "select: %s", strerror(errno));
605 exit(1);
606 }
607 continue;
608 }
609
610 for (i = 0; i < nfds; i++) {
611 switch (fds[i].type) {
612 case FD_CLIENT:
613 if (FD_ISSET(fds[i].fd, &rfds)) {
614 char buf[4096];
615 int ret = read(fds[i].fd, buf, sizeof(buf));
616 if (ret <= 0) {
617 if (ret < 0) {
618 fprintf(stderr, "standard input: read: %s\n",
619 strerror(errno));
620 exit(1);
621 }
622 return;
623 }
624 }
625 break;
626 case FD_LISTENER:
627 if (FD_ISSET(fds[i].fd, &rfds)) {
628 /*
629 * New connection has come in. Accept it.
630 */
631 struct fd *f;
632 struct sockaddr_in addr;
633 socklen_t addrlen = sizeof(addr);
634 int newfd = accept(fds[i].fd, (struct sockaddr *)&addr,
635 &addrlen);
636 if (newfd < 0)
637 break; /* not sure what happened there */
638
639 f = new_fdstruct(newfd, FD_CONNECTION);
640 f->cctx = new_connection(t);
641 if (authtype == HTTPD_AUTH_MAGIC)
642 check_magic_access(f);
643 }
644 break;
645 case FD_CONNECTION:
646 if (FD_ISSET(fds[i].fd, &rfds)) {
647 /*
648 * There's data to be read.
649 */
650 char readbuf[4096];
651 int ret;
652
653 ret = read(fds[i].fd, readbuf, sizeof(readbuf));
654 if (ret <= 0) {
655 /*
656 * This shouldn't happen in a sensible
657 * HTTP connection, so we abandon the
658 * connection if it does.
659 */
660 close(fds[i].fd);
661 fds[i].deleted = 1;
662 break;
663 } else {
664 if (!fds[i].wdata) {
665 /*
666 * If we haven't got an HTTP response
667 * yet, keep processing data in the
668 * hope of acquiring one.
669 */
670 fds[i].wdata = got_data
671 (fds[i].cctx, readbuf, ret,
672 (authtype == HTTPD_AUTH_NONE ||
673 fds[i].magic_access), authstring, &cfg);
674 if (fds[i].wdata) {
675 fds[i].wdatalen = strlen(fds[i].wdata);
676 fds[i].wdatapos = 0;
677 }
678 } else {
679 /*
680 * Otherwise, just drop our read data
681 * on the floor.
682 */
683 }
684 }
685 }
686 if (FD_ISSET(fds[i].fd, &wfds) &&
687 fds[i].wdatapos < fds[i].wdatalen) {
688 /*
689 * The socket is writable, and we have data to
690 * write. Write it.
691 */
692 int ret = write(fds[i].fd, fds[i].wdata + fds[i].wdatapos,
693 fds[i].wdatalen - fds[i].wdatapos);
694 if (ret <= 0) {
695 /*
696 * Shouldn't happen; abandon the connection.
697 */
698 close(fds[i].fd);
699 fds[i].deleted = 1;
700 break;
701 } else {
702 fds[i].wdatapos += ret;
703 if (fds[i].wdatapos == fds[i].wdatalen) {
704 shutdown(fds[i].fd, SHUT_WR);
705 }
706 }
707 }
708 break;
709 }
710 }
711
712 }
713 }