htparser: Handle absolute-URI requests.
[ashd.git] / src / htparser.c
CommitLineData
f0bbedf7
FT
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>
d341283f 22#include <fcntl.h>
f4cdf919 23#include <string.h>
f4cdf919 24#include <sys/socket.h>
f0cbd8d7 25#include <pwd.h>
15fa3fe8 26#include <sys/signal.h>
f4cdf919 27#include <errno.h>
f0bbedf7
FT
28
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
32#include <utils.h>
f4cdf919 33#include <mt.h>
83723896 34#include <mtio.h>
f4cdf919 35#include <log.h>
66987955 36#include <req.h>
9d87a119 37#include <proc.h>
f4cdf919 38
8774c31b 39#include "htparser.h"
f4cdf919 40
8774c31b 41static int plex;
43c58ba2 42static char *pidfile = NULL;
d341283f 43static int daemonize, usesyslog;
f4cdf919 44
df431d1d
FT
45static void trimx(struct hthead *req)
46{
47 int i;
48
49 i = 0;
50 while(i < req->noheaders) {
51 if(!strncasecmp(req->headers[i][0], "x-ash-", 6)) {
52 free(req->headers[i][0]);
53 free(req->headers[i][1]);
54 free(req->headers[i]);
55 memmove(req->headers + i, req->headers + i + 1, sizeof(*req->headers) * (--req->noheaders - i));
56 } else {
57 i++;
58 }
59 }
60}
61
5fc1bf9f 62static struct hthead *parsereq(FILE *in)
66987955 63{
5fc1bf9f
FT
64 struct hthead *req;
65 struct charbuf method, url, ver;
66 int c;
66987955 67
5fc1bf9f
FT
68 req = NULL;
69 bufinit(method);
70 bufinit(url);
71 bufinit(ver);
72 while(1) {
73 c = getc(in);
74 if(c == ' ') {
75 break;
76 } else if((c == EOF) || (c < 32) || (c >= 128)) {
77 goto fail;
78 } else {
79 bufadd(method, c);
90b0ba0f
FT
80 if(method.d >= 128)
81 goto fail;
66987955 82 }
c9955b14 83 }
c9955b14 84 while(1) {
5fc1bf9f
FT
85 c = getc(in);
86 if(c == ' ') {
c9955b14 87 break;
5fc1bf9f
FT
88 } else if((c == EOF) || (c < 32)) {
89 goto fail;
90 } else {
91 bufadd(url, c);
90b0ba0f
FT
92 if(url.d >= 65536)
93 goto fail;
66987955
FT
94 }
95 }
66987955 96 while(1) {
5fc1bf9f
FT
97 c = getc(in);
98 if(c == 10) {
66987955 99 break;
5fc1bf9f
FT
100 } else if(c == 13) {
101 } else if((c == EOF) || (c < 32) || (c >= 128)) {
c9955b14 102 goto fail;
5fc1bf9f
FT
103 } else {
104 bufadd(ver, c);
90b0ba0f
FT
105 if(ver.d >= 128)
106 goto fail;
5fc1bf9f 107 }
66987955 108 }
5fc1bf9f
FT
109 bufadd(method, 0);
110 bufadd(url, 0);
111 bufadd(ver, 0);
112 req = mkreq(method.b, url.b, ver.b);
113 if(parseheaders(req, in))
114 goto fail;
df431d1d 115 trimx(req);
5fc1bf9f 116 goto out;
c9955b14
FT
117
118fail:
5fc1bf9f
FT
119 if(req != NULL) {
120 freehthead(req);
121 req = NULL;
122 }
123out:
124 buffree(method);
125 buffree(url);
126 buffree(ver);
127 return(req);
66987955
FT
128}
129
5fc1bf9f 130static struct hthead *parseresp(FILE *in)
9d87a119 131{
5fc1bf9f 132 struct hthead *req;
9d87a119 133 int code;
5fc1bf9f
FT
134 struct charbuf ver, msg;
135 int c;
9d87a119 136
5fc1bf9f
FT
137 req = NULL;
138 bufinit(ver);
139 bufinit(msg);
140 code = 0;
9d87a119 141 while(1) {
5fc1bf9f
FT
142 c = getc(in);
143 if(c == ' ') {
9d87a119 144 break;
5fc1bf9f 145 } else if((c == EOF) || (c < 32) || (c >= 128)) {
9d87a119 146 goto fail;
5fc1bf9f
FT
147 } else {
148 bufadd(ver, c);
e2b404ab
FT
149 if(ver.d >= 128)
150 goto fail;
5fc1bf9f
FT
151 }
152 }
153 while(1) {
154 c = getc(in);
155 if(c == ' ') {
156 break;
157 } else if((c == EOF) || (c < '0') || (c > '9')) {
9d87a119 158 goto fail;
5fc1bf9f
FT
159 } else {
160 code = (code * 10) + (c - '0');
e2b404ab
FT
161 if(code >= 10000)
162 goto fail;
5fc1bf9f
FT
163 }
164 }
165 while(1) {
166 c = getc(in);
167 if(c == 10) {
168 break;
169 } else if(c == 13) {
170 } else if((c == EOF) || (c < 32)) {
9d87a119 171 goto fail;
5fc1bf9f
FT
172 } else {
173 bufadd(msg, c);
e2b404ab
FT
174 if(msg.d >= 512)
175 goto fail;
5fc1bf9f 176 }
9d87a119 177 }
5fc1bf9f
FT
178 bufadd(msg, 0);
179 bufadd(ver, 0);
180 req = mkresp(code, msg.b, ver.b);
181 if(parseheaders(req, in))
182 goto fail;
183 goto out;
9d87a119
FT
184
185fail:
5fc1bf9f
FT
186 if(req != NULL) {
187 freehthead(req);
188 req = NULL;
189 }
190out:
191 buffree(msg);
192 buffree(ver);
193 return(req);
9d87a119
FT
194}
195
5fc1bf9f 196static off_t passdata(FILE *in, FILE *out, off_t max)
9d87a119 197{
5fc1bf9f
FT
198 size_t read;
199 off_t total;
200 char buf[8192];
201
202 total = 0;
f9255ddd 203 while(!feof(in) && ((max < 0) || (total < max))) {
5fc1bf9f
FT
204 read = sizeof(buf);
205 if(max >= 0)
a701d7b7 206 read = min(max - total, read);
5fc1bf9f
FT
207 read = fread(buf, 1, read, in);
208 if(ferror(in))
209 return(-1);
210 if(fwrite(buf, 1, read, out) != read)
211 return(-1);
212 total += read;
9d87a119 213 }
5fc1bf9f
FT
214 return(total);
215}
216
217static int passchunks(FILE *in, FILE *out)
218{
219 char buf[8192];
220 size_t read;
221
222 do {
223 read = fread(buf, 1, sizeof(buf), in);
224 if(ferror(in))
225 return(-1);
f9255ddd 226 fprintf(out, "%zx\r\n", read);
5fc1bf9f
FT
227 if(fwrite(buf, 1, read, out) != read)
228 return(-1);
229 fprintf(out, "\r\n");
230 } while(read > 0);
231 return(0);
232}
233
234static int hasheader(struct hthead *head, char *name, char *val)
235{
236 char *hd;
237
238 if((hd = getheader(head, name)) == NULL)
239 return(0);
240 return(!strcasecmp(hd, val));
9d87a119
FT
241}
242
64a9096a
FT
243static int canonreq(struct hthead *req)
244{
245 char *p, *p2, *r;
246 int n;
247
248 if(req->url[0] == '/') {
249 replrest(req, req->url + 1);
250 if((p = strchr(req->rest, '?')) != NULL)
251 *p = 0;
252 return(1);
253 }
254 if((p = strstr(req->url, "://")) != NULL) {
255 n = p - req->url;
256 if(((n == 4) && !strncasecmp(req->url, "http", 4)) ||
257 ((n == 5) && !strncasecmp(req->url, "https", 5))) {
258 if(getheader(req, "host"))
259 return(0);
260 p += 3;
261 if((p2 = strchr(p, '/')) == NULL) {
262 headappheader(req, "Host", p);
263 free(req->url);
264 req->url = sstrdup("/");
265 } else {
266 r = sstrdup(p2);
267 *(p2++) = 0;
268 headappheader(req, "Host", p);
269 free(req->url);
270 req->url = r;
271 }
272 replrest(req, req->url + 1);
273 if((p = strchr(req->rest, '?')) != NULL)
274 *p = 0;
275 return(1);
276 }
277 }
278 return(0);
279}
280
8774c31b 281void serve(FILE *in, struct conn *conn)
66987955 282{
af34331c 283 int pfds[2];
8774c31b 284 FILE *out;
9d87a119 285 struct hthead *req, *resp;
64a9096a 286 char *hd;
5fc1bf9f 287 off_t dlen;
66987955 288
5fc1bf9f 289 out = NULL;
3c296bd4 290 req = resp = NULL;
66987955 291 while(1) {
5fc1bf9f
FT
292 if((req = parsereq(in)) == NULL)
293 break;
64a9096a
FT
294 if(!canonreq(req))
295 break;
9d87a119 296
8774c31b
FT
297 if((conn->initreq != NULL) && conn->initreq(conn, req))
298 break;
299
46c3d430 300 if(block(plex, EV_WRITE, 60) <= 0)
5fc1bf9f 301 break;
af34331c 302 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
5fc1bf9f 303 break;
af34331c 304 if(sendreq(plex, req, pfds[0]))
5fc1bf9f 305 break;
af34331c 306 close(pfds[0]);
5fc1bf9f 307 out = mtstdopen(pfds[1], 1, 600, "r+");
a0327573 308
a0327573
FT
309 if((hd = getheader(req, "content-length")) != NULL) {
310 dlen = atoo(hd);
a06a2fbd 311 if(dlen > 0) {
5fc1bf9f
FT
312 if(passdata(in, out, dlen) != dlen)
313 break;
a06a2fbd 314 }
a0327573 315 }
5fc1bf9f
FT
316 if(fflush(out))
317 break;
d93d9a05 318 /* Make sure to send EOF */
5fc1bf9f 319 shutdown(pfds[1], SHUT_WR);
9d87a119 320
f9255ddd
FT
321 if((resp = parseresp(out)) == NULL)
322 break;
5fc1bf9f 323 replstr(&resp->ver, req->ver);
1c3e0167
FT
324
325 if(!getheader(resp, "server"))
326 headappheader(resp, "Server", sprintf3("ashd/%s", VERSION));
5fc1bf9f
FT
327
328 if(!strcmp(req->ver, "HTTP/1.0")) {
329 writeresp(in, resp);
330 fprintf(in, "\r\n");
18fb436d
FT
331 if(!strcasecmp(req->method, "head")) {
332 if(!hasheader(req, "connection", "keep-alive"))
333 break;
334 } else if((hd = getheader(resp, "content-length")) != NULL) {
5226f7c5
FT
335 dlen = atoo(hd);
336 if(passdata(out, in, dlen) != dlen)
5fc1bf9f
FT
337 break;
338 if(!hasheader(req, "connection", "keep-alive"))
9d87a119 339 break;
5fc1bf9f
FT
340 } else {
341 passdata(out, in, -1);
342 break;
9d87a119 343 }
5fc1bf9f
FT
344 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
345 break;
346 } else if(!strcmp(req->ver, "HTTP/1.1")) {
18fb436d
FT
347 if(!strcasecmp(req->method, "head")) {
348 writeresp(in, resp);
349 fprintf(in, "\r\n");
350 } else if((hd = getheader(resp, "content-length")) != NULL) {
5fc1bf9f
FT
351 writeresp(in, resp);
352 fprintf(in, "\r\n");
5226f7c5
FT
353 dlen = atoo(hd);
354 if(passdata(out, in, dlen) != dlen)
5fc1bf9f
FT
355 break;
356 } else if(!getheader(resp, "transfer-encoding")) {
357 headappheader(resp, "Transfer-Encoding", "chunked");
358 writeresp(in, resp);
359 fprintf(in, "\r\n");
360 if(passchunks(out, in))
361 break;
362 } else {
363 writeresp(in, resp);
364 fprintf(in, "\r\n");
365 passdata(out, in, -1);
9d87a119 366 break;
5fc1bf9f
FT
367 }
368 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
9d87a119 369 break;
5fc1bf9f
FT
370 } else {
371 break;
9d87a119 372 }
5fc1bf9f
FT
373
374 fclose(out);
375 out = NULL;
9d87a119 376 freehthead(req);
9d87a119 377 freehthead(resp);
5fc1bf9f 378 req = resp = NULL;
66987955
FT
379 }
380
5fc1bf9f
FT
381 if(out != NULL)
382 fclose(out);
9d87a119
FT
383 if(req != NULL)
384 freehthead(req);
385 if(resp != NULL)
386 freehthead(resp);
5fc1bf9f 387 fclose(in);
66987955
FT
388}
389
32e24c19
FT
390static void plexwatch(struct muth *muth, va_list args)
391{
392 vavar(int, fd);
393 char *buf;
394 int ret;
395
396 while(1) {
397 block(fd, EV_READ, 0);
398 buf = smalloc(65536);
399 ret = recv(fd, buf, 65536, 0);
400 if(ret < 0) {
401 flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
402 exit(1);
403 } else if(ret == 0) {
404 exit(0);
405 }
406 /* Maybe I'd like to implement some protocol in this direction
407 * some day... */
408 free(buf);
409 }
410}
411
d341283f
FT
412static void initroot(void *uu)
413{
414 int fd;
415
416 if(daemonize) {
417 setsid();
418 chdir("/");
419 if((fd = open("/dev/null", O_RDWR)) >= 0) {
420 dup2(fd, 0);
421 dup2(fd, 1);
422 dup2(fd, 2);
423 close(fd);
424 }
425 }
426 if(usesyslog)
427 putenv("ASHD_USESYSLOG=1");
428 else
429 unsetenv("ASHD_USESYSLOG");
430}
431
8774c31b
FT
432static void usage(FILE *out)
433{
43c58ba2 434 fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] [-p PIDFILE] PORTSPEC... -- ROOT [ARGS...]\n");
8774c31b 435 fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
6ca53b2e 436 fprintf(out, "\tavailable handlers are `plain' and `ssl'.\n");
8774c31b
FT
437}
438
439static void addport(char *spec)
440{
441 char *nm, *p, *p2, *n;
442 struct charvbuf pars, vals;
443
444 bufinit(pars);
445 bufinit(vals);
446 if((p = strchr(spec, ':')) == NULL) {
447 nm = spec;
448 } else {
449 nm = spec;
450 *(p++) = 0;
451 do {
452 if((n = strchr(p, ',')) != NULL)
453 *(n++) = 0;
454 if((p2 = strchr(p, '=')) != NULL)
455 *(p2++) = 0;
456 if(!*p) {
457 usage(stderr);
458 exit(1);
459 }
460 bufadd(pars, p);
461 if(p2)
462 bufadd(vals, p2);
463 else
464 bufadd(vals, "");
465 } while((p = n) != NULL);
466 }
467
468 /* XXX: It would be nice to decentralize this, but, meh... */
469 if(!strcmp(nm, "plain")) {
470 handleplain(pars.d, pars.b, vals.b);
6ca53b2e
FT
471#ifdef HAVE_GNUTLS
472 } else if(!strcmp(nm, "ssl")) {
473 handlegnussl(pars.d, pars.b, vals.b);
474#endif
8774c31b
FT
475 } else {
476 flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
477 exit(1);
478 }
479
480 buffree(pars);
481 buffree(vals);
482}
483
f0bbedf7
FT
484int main(int argc, char **argv)
485{
8774c31b
FT
486 int c;
487 int i, s1;
f0cbd8d7 488 char *root;
43c58ba2 489 FILE *pidout;
f0cbd8d7 490 struct passwd *pwent;
f4cdf919 491
d341283f 492 daemonize = usesyslog = 0;
f0cbd8d7
FT
493 root = NULL;
494 pwent = NULL;
43c58ba2 495 while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
8774c31b
FT
496 switch(c) {
497 case 'h':
498 usage(stdout);
499 exit(0);
f0cbd8d7
FT
500 case 'f':
501 daemonize = 1;
502 break;
503 case 'S':
d341283f 504 usesyslog = 1;
f0cbd8d7
FT
505 break;
506 case 'u':
507 if((pwent = getpwnam(optarg)) == NULL) {
508 flog(LOG_ERR, "could not find user %s", optarg);
509 exit(1);
510 }
511 break;
512 case 'r':
513 root = optarg;
514 break;
43c58ba2
FT
515 case 'p':
516 pidfile = optarg;
517 break;
8774c31b
FT
518 default:
519 usage(stderr);
520 exit(1);
521 }
522 }
8774c31b
FT
523 s1 = 0;
524 for(i = optind; i < argc; i++) {
525 if(!strcmp(argv[i], "--"))
526 break;
527 s1 = 1;
528 addport(argv[i]);
9d87a119 529 }
8774c31b
FT
530 if(!s1 || (i == argc)) {
531 usage(stderr);
532 exit(1);
f4cdf919 533 }
d341283f 534 if((plex = stdmkchild(argv + ++i, initroot, NULL)) < 0) {
8774c31b
FT
535 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
536 return(1);
f4cdf919 537 }
32e24c19 538 mustart(plexwatch, plex);
43c58ba2
FT
539 pidout = NULL;
540 if(pidfile != NULL) {
541 if((pidout = fopen(pidfile, "w")) == NULL) {
542 flog(LOG_ERR, "could not open %s for writing: %s", pidfile, strerror(errno));
543 return(1);
544 }
545 }
d341283f 546 if(usesyslog)
f0cbd8d7
FT
547 opensyslog();
548 if(root) {
0370bd82 549 if(chdir(root) || chroot(root)) {
f0cbd8d7
FT
550 flog(LOG_ERR, "could not chroot to %s: %s", root, strerror(errno));
551 exit(1);
552 }
553 }
554 if(pwent) {
555 if(setgid(pwent->pw_gid)) {
556 flog(LOG_ERR, "could not switch group to %i: %s", (int)pwent->pw_gid, strerror(errno));
557 exit(1);
558 }
559 if(setuid(pwent->pw_uid)) {
560 flog(LOG_ERR, "could not switch user to %i: %s", (int)pwent->pw_uid, strerror(errno));
561 exit(1);
562 }
563 }
15fa3fe8 564 signal(SIGPIPE, SIG_IGN);
f0cbd8d7
FT
565 if(daemonize) {
566 daemon(0, 0);
567 }
ee036f74 568 if(pidout != NULL) {
43c58ba2 569 fprintf(pidout, "%i\n", getpid());
ee036f74
FT
570 fclose(pidout);
571 }
f4cdf919
FT
572 ioloop();
573 return(0);
f0bbedf7 574}