accesslog: Pass file descriptors through response.
[ashd.git] / src / htparser.c
... / ...
CommitLineData
1/*
2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <stdlib.h>
20#include <unistd.h>
21#include <stdio.h>
22#include <fcntl.h>
23#include <string.h>
24#include <sys/socket.h>
25#include <pwd.h>
26#include <sys/signal.h>
27#include <errno.h>
28
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
32#include <utils.h>
33#include <mt.h>
34#include <mtio.h>
35#include <log.h>
36#include <req.h>
37#include <proc.h>
38#include <bufio.h>
39
40#include "htparser.h"
41
42static int plex;
43static char *pidfile = NULL;
44static int daemonize, usesyslog;
45struct mtbuf listeners;
46
47static void trimx(struct hthead *req)
48{
49 int i;
50
51 i = 0;
52 while(i < req->noheaders) {
53 if(!strncasecmp(req->headers[i][0], "x-ash-", 6)) {
54 free(req->headers[i][0]);
55 free(req->headers[i][1]);
56 free(req->headers[i]);
57 memmove(req->headers + i, req->headers + i + 1, sizeof(*req->headers) * (--req->noheaders - i));
58 } else {
59 i++;
60 }
61 }
62}
63
64static struct hthead *parsereq(struct bufio *in)
65{
66 struct hthead *req;
67 struct charbuf method, url, ver;
68 int c;
69
70 req = NULL;
71 bufinit(method);
72 bufinit(url);
73 bufinit(ver);
74 while(1) {
75 c = biogetc(in);
76 if(c == ' ') {
77 break;
78 } else if((c == EOF) || (c < 32) || (c >= 128)) {
79 goto fail;
80 } else {
81 bufadd(method, c);
82 if(method.d >= 128)
83 goto fail;
84 }
85 }
86 while(1) {
87 c = biogetc(in);
88 if(c == ' ') {
89 break;
90 } else if((c == EOF) || (c < 32)) {
91 goto fail;
92 } else {
93 bufadd(url, c);
94 if(url.d >= 65536)
95 goto fail;
96 }
97 }
98 while(1) {
99 c = biogetc(in);
100 if(c == 10) {
101 break;
102 } else if(c == 13) {
103 } else if((c == EOF) || (c < 32) || (c >= 128)) {
104 goto fail;
105 } else {
106 bufadd(ver, c);
107 if(ver.d >= 128)
108 goto fail;
109 }
110 }
111 bufadd(method, 0);
112 bufadd(url, 0);
113 bufadd(ver, 0);
114 req = mkreq(method.b, url.b, ver.b);
115 if(parseheadersb(req, in))
116 goto fail;
117 trimx(req);
118 goto out;
119
120fail:
121 if(req != NULL) {
122 freehthead(req);
123 req = NULL;
124 }
125out:
126 buffree(method);
127 buffree(url);
128 buffree(ver);
129 return(req);
130}
131
132static off_t passdata(struct bufio *in, struct bufio *out, off_t max)
133{
134 ssize_t read;
135 off_t total;
136
137 total = 0;
138 while(!bioeof(in) && ((max < 0) || (total < max))) {
139 if((read = biordata(in)) > 0) {
140 if(max >= 0)
141 read = min(max - total, read);
142 if((read = biowritesome(out, in->rbuf.b + in->rh, read)) < 0)
143 return(-1);
144 in->rh += read;
145 total += read;
146 }
147 if(biorspace(in) && ((max < 0) || (biordata(in) < max - total)) && (biofillsome(in) < 0))
148 return(-1);
149 }
150 return(total);
151}
152
153static int recvchunks(struct bufio *in, struct bufio *out)
154{
155 size_t read, chlen;
156 int c, r;
157
158 while(1) {
159 chlen = 0;
160 r = 0;
161 while(1) {
162 c = biogetc(in);
163 if(c == 10) {
164 if(!r)
165 return(-1);
166 break;
167 } else if(c == 13) {
168 } else if((c >= '0') && (c <= '9')) {
169 chlen = (chlen << 4) + (c - '0');
170 r = 1;
171 } else if((c >= 'A') && (c <= 'F')) {
172 chlen = (chlen << 4) + (c + 10 - 'A');
173 r = 1;
174 } else if((c >= 'a') && (c <= 'f')) {
175 chlen = (chlen << 4) + (c + 10 - 'a');
176 r = 1;
177 } else {
178 /* XXX: Technically, there may be chunk extensions to
179 * be read, but since that will likely never actually
180 * happen in practice, I can just as well add support
181 * for that if it actually does become relevant. */
182 return(-1);
183 }
184 }
185 if(chlen == 0)
186 break;
187 while(chlen > 0) {
188 if((read = biordata(in)) > 0) {
189 if((read = biowritesome(out, in->rbuf.b + in->rh, min(read, chlen))) < 0)
190 return(-1);
191 in->rh += read;
192 chlen -= read;
193 }
194 if(biorspace(in) && (biordata(in) < chlen) && (biofillsome(in) <= 0))
195 return(-1);
196 }
197 if((biogetc(in) != 13) || (biogetc(in) != 10))
198 return(-1);
199 }
200 /* XXX: Technically, there may be trailers to be read, but that's
201 * just about as likely as chunk extensions. */
202 if((biogetc(in) != 13) || (biogetc(in) != 10))
203 return(-1);
204 return(0);
205}
206
207static int passchunks(struct bufio *in, struct bufio *out)
208{
209 size_t read;
210
211 while(!bioeof(in)) {
212 if((read = biordata(in)) > 0) {
213 bioprintf(out, "%zx\r\n", read);
214 if(biowrite(out, in->rbuf.b + in->rh, read) != read)
215 return(-1);
216 in->rh += read;
217 bioprintf(out, "\r\n");
218 if(bioflush(out) < 0)
219 return(-1);
220 }
221 if(biorspace(in) && (biofillsome(in) < 0))
222 return(-1);
223 }
224 bioprintf(out, "0\r\n\r\n");
225 return(0);
226}
227
228static int hasheader(struct hthead *head, char *name, char *val)
229{
230 char *hd;
231
232 if((hd = getheader(head, name)) == NULL)
233 return(0);
234 return(!strcasecmp(hd, val));
235}
236
237static int canonreq(struct hthead *req)
238{
239 char *p, *p2, *r;
240 int n;
241
242 if(req->url[0] == '/') {
243 replrest(req, req->url + 1);
244 if((p = strchr(req->rest, '?')) != NULL)
245 *p = 0;
246 return(1);
247 }
248 if((p = strstr(req->url, "://")) != NULL) {
249 n = p - req->url;
250 if(((n == 4) && !strncasecmp(req->url, "http", 4)) ||
251 ((n == 5) && !strncasecmp(req->url, "https", 5))) {
252 if(getheader(req, "host"))
253 return(0);
254 p += 3;
255 if((p2 = strchr(p, '/')) == NULL) {
256 headappheader(req, "Host", p);
257 free(req->url);
258 req->url = sstrdup("/");
259 } else {
260 r = sstrdup(p2);
261 *(p2++) = 0;
262 headappheader(req, "Host", p);
263 free(req->url);
264 req->url = r;
265 }
266 replrest(req, req->url + 1);
267 if((p = strchr(req->rest, '?')) != NULL)
268 *p = 0;
269 return(1);
270 }
271 }
272 return(0);
273}
274
275static int http10keep(struct hthead *req, struct hthead *resp)
276{
277 int fc;
278
279 fc = hasheader(resp, "connection", "close");
280 headrmheader(resp, "connection");
281 if(!fc && hasheader(req, "connection", "keep-alive")) {
282 headappheader(resp, "Connection", "Keep-Alive");
283 return(1);
284 } else {
285 return(0);
286 }
287}
288
289static char *connid(void)
290{
291 static struct charbuf cur;
292 int i;
293 char *ret;
294
295 for(i = 0; i < cur.d; i++) {
296 if((++cur.b[i]) > 'Z')
297 cur.b[i] = 'A';
298 else
299 goto done;
300 }
301 bufadd(cur, 'A');
302done:
303 ret = memcpy(smalloc(cur.d + 1), cur.b, cur.d);
304 ret[cur.d] = 0;
305 return(ret);
306}
307
308static void passduplex(struct bufio *a, int afd, struct bufio *b, int bfd)
309{
310 struct selected pfd[4], sel;
311 struct bufio *sio;
312 int n, ev;
313
314 while(!bioeof(a) && !bioeof(b)) {
315 biocopybuf(b, a);
316 biocopybuf(a, b);
317 n = 0;
318 if(!a->eof) {
319 ev = 0;
320 if(biorspace(a))
321 ev |= EV_READ;
322 if(biowdata(a))
323 ev |= EV_WRITE;
324 if(ev)
325 pfd[n++] = (struct selected){.fd = afd, .ev = ev};
326 }
327 if(!b->eof) {
328 ev = 0;
329 if(!b->eof && biorspace(b))
330 ev |= EV_READ;
331 if(biowdata(b))
332 ev |= EV_WRITE;
333 if(ev)
334 pfd[n++] = (struct selected){.fd = bfd, .ev = ev};
335 }
336 sel = mblock(600, n, pfd);
337 if(sel.fd == afd)
338 sio = a;
339 else if(sel.fd == bfd)
340 sio = b;
341 else
342 break;
343 if((sel.ev & EV_READ) && (biofillsome(sio) < 0))
344 break;
345 if((sel.ev & EV_WRITE) && (bioflushsome(sio) < 0))
346 break;
347 }
348}
349
350void serve(struct bufio *in, int infd, struct conn *conn)
351{
352 int pfds[2];
353 struct bufio *out, *dout;
354 struct stdiofd *outi;
355 struct hthead *req, *resp;
356 char *hd, *id;
357 off_t dlen;
358 int keep, duplex;
359
360 id = connid();
361 out = NULL;
362 req = resp = NULL;
363 while(plex >= 0) {
364 bioflush(in);
365 if((req = parsereq(in)) == NULL)
366 break;
367 if(!canonreq(req))
368 break;
369
370 headappheader(req, "X-Ash-Connection-ID", id);
371 if((conn->initreq != NULL) && conn->initreq(conn, req))
372 break;
373
374 if((plex < 0) || block(plex, EV_WRITE, 60) <= 0)
375 break;
376 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
377 break;
378 if(sendreq(plex, req, pfds[0]))
379 break;
380 close(pfds[0]);
381 out = mtbioopen(pfds[1], 1, 600, "r+", &outi);
382
383 if(getheader(req, "content-type") != NULL) {
384 if((hd = getheader(req, "content-length")) != NULL) {
385 dlen = atoo(hd);
386 if(dlen > 0) {
387 if(passdata(in, out, dlen) != dlen)
388 break;
389 }
390 } else if(((hd = getheader(req, "transfer-encoding")) != NULL) && !strcasecmp(hd, "chunked")) {
391 if(recvchunks(in, out))
392 break;
393 } else {
394 /* Ignore rather than abort, to be kinder to broken clients. */
395 headrmheader(req, "content-type");
396 }
397 }
398 if(bioflush(out))
399 break;
400 /* Make sure to send EOF */
401 shutdown(pfds[1], SHUT_WR);
402
403 if((resp = parseresponseb(out)) == NULL)
404 break;
405 replstr(&resp->ver, req->ver);
406
407 if(!getheader(resp, "server"))
408 headappheader(resp, "Server", sprintf3("ashd/%s", VERSION));
409 duplex = hasheader(resp, "x-ash-switch", "duplex");
410 trimx(resp);
411
412 if(duplex) {
413 if(outi->rights < 0)
414 break;
415 writerespb(in, resp);
416 bioprintf(in, "\r\n");
417 dout = mtbioopen(outi->rights, 1, 600, "r+", NULL);
418 passduplex(in, infd, dout, outi->rights);
419 outi->rights = -1;
420 bioclose(dout);
421 break;
422 } else if(!strcasecmp(req->ver, "HTTP/1.0")) {
423 if(!strcasecmp(req->method, "head")) {
424 keep = http10keep(req, resp);
425 writerespb(in, resp);
426 bioprintf(in, "\r\n");
427 } else if((hd = getheader(resp, "content-length")) != NULL) {
428 keep = http10keep(req, resp);
429 dlen = atoo(hd);
430 writerespb(in, resp);
431 bioprintf(in, "\r\n");
432 if(passdata(out, in, dlen) != dlen)
433 break;
434 } else {
435 headrmheader(resp, "connection");
436 writerespb(in, resp);
437 bioprintf(in, "\r\n");
438 passdata(out, in, -1);
439 break;
440 }
441 if(!keep)
442 break;
443 } else if(!strcasecmp(req->ver, "HTTP/1.1")) {
444 if(!strcasecmp(req->method, "head")) {
445 writerespb(in, resp);
446 bioprintf(in, "\r\n");
447 } else if((hd = getheader(resp, "content-length")) != NULL) {
448 writerespb(in, resp);
449 bioprintf(in, "\r\n");
450 dlen = atoo(hd);
451 if(passdata(out, in, dlen) != dlen)
452 break;
453 } else if(!getheader(resp, "transfer-encoding")) {
454 headappheader(resp, "Transfer-Encoding", "chunked");
455 writerespb(in, resp);
456 bioprintf(in, "\r\n");
457 if(passchunks(out, in))
458 break;
459 } else {
460 writerespb(in, resp);
461 bioprintf(in, "\r\n");
462 passdata(out, in, -1);
463 break;
464 }
465 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
466 break;
467 } else {
468 break;
469 }
470
471 bioclose(out);
472 out = NULL;
473 freehthead(req);
474 freehthead(resp);
475 req = resp = NULL;
476 }
477
478 if(out != NULL)
479 bioclose(out);
480 if(req != NULL)
481 freehthead(req);
482 if(resp != NULL)
483 freehthead(resp);
484 bioclose(in);
485 free(id);
486}
487
488static void plexwatch(struct muth *muth, va_list args)
489{
490 vavar(int, fd);
491 char *buf;
492 int i, s, ret;
493
494 s = 0;
495 while(1) {
496 if(block(fd, EV_READ, 0) == 0)
497 break;
498 buf = smalloc(65536);
499 ret = recv(fd, buf, 65536, 0);
500 if(ret < 0) {
501 flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
502 exit(1);
503 } else if(ret == 0) {
504 s = 1;
505 free(buf);
506 break;
507 }
508 /* Maybe I'd like to implement some protocol in this direction
509 * some day... */
510 free(buf);
511 }
512 shutdown(plex, SHUT_RDWR);
513 for(i = 0; i < listeners.d; i++) {
514 if(listeners.b[i] == muth)
515 bufdel(listeners, i);
516 }
517 if(s) {
518 flog(LOG_INFO, "root handler exited, so shutting down listening...");
519 while(listeners.d > 0)
520 resume(listeners.b[0], 0);
521 }
522}
523
524static void initroot(void *uu)
525{
526 int fd;
527
528 setsid();
529 if(daemonize) {
530 chdir("/");
531 if((fd = open("/dev/null", O_RDWR)) >= 0) {
532 dup2(fd, 0);
533 dup2(fd, 1);
534 dup2(fd, 2);
535 close(fd);
536 }
537 }
538 if(usesyslog)
539 putenv("ASHD_USESYSLOG=1");
540 else
541 unsetenv("ASHD_USESYSLOG");
542}
543
544static void usage(FILE *out)
545{
546 fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] [-p PIDFILE] PORTSPEC... -- ROOT [ARGS...]\n");
547 fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
548 fprintf(out, "\tavailable handlers are `plain' and `ssl'.\n");
549}
550
551static void addport(char *spec)
552{
553 char *nm, *p, *p2, *n;
554 struct charvbuf pars, vals;
555
556 bufinit(pars);
557 bufinit(vals);
558 if((p = strchr(spec, ':')) == NULL) {
559 nm = spec;
560 } else {
561 nm = spec;
562 *(p++) = 0;
563 do {
564 if((n = strchr(p, ',')) != NULL)
565 *(n++) = 0;
566 if((p2 = strchr(p, '=')) != NULL)
567 *(p2++) = 0;
568 if(!*p) {
569 usage(stderr);
570 exit(1);
571 }
572 bufadd(pars, p);
573 if(p2)
574 bufadd(vals, p2);
575 else
576 bufadd(vals, "");
577 } while((p = n) != NULL);
578 }
579
580 /* XXX: It would be nice to decentralize this, but, meh... */
581 if(!strcmp(nm, "plain")) {
582 handleplain(pars.d, pars.b, vals.b);
583#ifdef HAVE_GNUTLS
584 } else if(!strcmp(nm, "ssl")) {
585 handlegnussl(pars.d, pars.b, vals.b);
586#endif
587 } else {
588 flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
589 exit(1);
590 }
591
592 buffree(pars);
593 buffree(vals);
594}
595
596static void sighandler(int sig)
597{
598 exitioloop(1);
599}
600
601int main(int argc, char **argv)
602{
603 int c, d;
604 int i, s1;
605 char *root;
606 FILE *pidout;
607 struct passwd *pwent;
608
609 daemonize = usesyslog = 0;
610 root = NULL;
611 pwent = NULL;
612 while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
613 switch(c) {
614 case 'h':
615 usage(stdout);
616 exit(0);
617 case 'f':
618 daemonize = 1;
619 break;
620 case 'S':
621 usesyslog = 1;
622 break;
623 case 'u':
624 if((pwent = getpwnam(optarg)) == NULL) {
625 flog(LOG_ERR, "could not find user %s", optarg);
626 exit(1);
627 }
628 break;
629 case 'r':
630 root = optarg;
631 break;
632 case 'p':
633 pidfile = optarg;
634 break;
635 default:
636 usage(stderr);
637 exit(1);
638 }
639 }
640 s1 = 0;
641 for(i = optind; i < argc; i++) {
642 if(!strcmp(argv[i], "--"))
643 break;
644 s1 = 1;
645 addport(argv[i]);
646 }
647 if(!s1 || (i == argc)) {
648 usage(stderr);
649 exit(1);
650 }
651 if((plex = stdmkchild(argv + ++i, initroot, NULL)) < 0) {
652 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
653 return(1);
654 }
655 bufadd(listeners, mustart(plexwatch, plex));
656 pidout = NULL;
657 if(pidfile != NULL) {
658 if((pidout = fopen(pidfile, "w")) == NULL) {
659 flog(LOG_ERR, "could not open %s for writing: %s", pidfile, strerror(errno));
660 return(1);
661 }
662 }
663 if(usesyslog)
664 opensyslog();
665 if(root) {
666 if(chdir(root) || chroot(root)) {
667 flog(LOG_ERR, "could not chroot to %s: %s", root, strerror(errno));
668 exit(1);
669 }
670 }
671 if(pwent) {
672 if(setgid(pwent->pw_gid)) {
673 flog(LOG_ERR, "could not switch group to %i: %s", (int)pwent->pw_gid, strerror(errno));
674 exit(1);
675 }
676 if(setuid(pwent->pw_uid)) {
677 flog(LOG_ERR, "could not switch user to %i: %s", (int)pwent->pw_uid, strerror(errno));
678 exit(1);
679 }
680 }
681 signal(SIGPIPE, SIG_IGN);
682 signal(SIGCHLD, SIG_IGN);
683 signal(SIGINT, sighandler);
684 signal(SIGTERM, sighandler);
685 if(daemonize) {
686 daemon(0, 0);
687 }
688 if(pidout != NULL) {
689 fprintf(pidout, "%i\n", getpid());
690 fclose(pidout);
691 }
692 d = 0;
693 while(!d) {
694 switch(ioloop()) {
695 case 0:
696 d = 1;
697 break;
698 case 1:
699 if(listeners.d > 0) {
700 while(listeners.d > 0)
701 resume(listeners.b[0], 0);
702 flog(LOG_INFO, "no longer listening");
703 } else {
704 d = 1;
705 }
706 break;
707 }
708 }
709 return(0);
710}