call*cgi, python: Added symmetrical {SERVER,REMOTE}_{ADDR,PORT} parameters.
[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 off_t passdata(FILE *in, FILE *out, off_t max)
9d87a119 131{
5fc1bf9f
FT
132 size_t read;
133 off_t total;
134 char buf[8192];
135
136 total = 0;
f9255ddd 137 while(!feof(in) && ((max < 0) || (total < max))) {
5fc1bf9f
FT
138 read = sizeof(buf);
139 if(max >= 0)
a701d7b7 140 read = min(max - total, read);
5fc1bf9f
FT
141 read = fread(buf, 1, read, in);
142 if(ferror(in))
143 return(-1);
144 if(fwrite(buf, 1, read, out) != read)
145 return(-1);
146 total += read;
9d87a119 147 }
5fc1bf9f
FT
148 return(total);
149}
150
8d19a9ec
FT
151static int recvchunks(FILE *in, FILE *out)
152{
153 char buf[8192];
154 size_t read, chlen;
155 int c, r;
156
157 while(1) {
158 chlen = 0;
159 r = 0;
160 while(1) {
161 c = getc(in);
162 if(c == 10) {
163 if(!r)
164 return(-1);
165 break;
166 } else if(c == 13) {
167 } else if((c >= '0') && (c <= '9')) {
168 chlen = (chlen << 4) + (c - '0');
169 r = 1;
170 } else if((c >= 'A') && (c <= 'F')) {
171 chlen = (chlen << 4) + (c + 10 - 'A');
172 r = 1;
173 } else if((c >= 'a') && (c <= 'f')) {
174 chlen = (chlen << 4) + (c + 10 - 'a');
175 r = 1;
176 } else {
177 /* XXX: Technically, there may be chunk extensions to
178 * be read, but since that will likely never actually
179 * happen in practice, I can just as well add support
180 * for that if it actually does become relevant. */
181 return(-1);
182 }
183 }
184 if(chlen == 0)
185 break;
186 while(chlen > 0) {
187 read = fread(buf, 1, min(sizeof(buf), chlen), in);
188 if(feof(in) || ferror(in))
189 return(-1);
190 if(fwrite(buf, 1, read, out) != read)
191 return(-1);
192 chlen -= read;
193 }
194 if((getc(in) != 13) || (getc(in) != 10))
195 return(-1);
196 }
197 /* XXX: Technically, there may be trailers to be read, but that's
198 * just about as likely as chunk extensions. */
199 if((getc(in) != 13) || (getc(in) != 10))
200 return(-1);
201 return(0);
202}
203
5fc1bf9f
FT
204static int passchunks(FILE *in, FILE *out)
205{
206 char buf[8192];
207 size_t read;
208
209 do {
210 read = fread(buf, 1, sizeof(buf), in);
211 if(ferror(in))
212 return(-1);
f9255ddd 213 fprintf(out, "%zx\r\n", read);
5fc1bf9f
FT
214 if(fwrite(buf, 1, read, out) != read)
215 return(-1);
216 fprintf(out, "\r\n");
217 } while(read > 0);
218 return(0);
219}
220
221static int hasheader(struct hthead *head, char *name, char *val)
222{
223 char *hd;
224
225 if((hd = getheader(head, name)) == NULL)
226 return(0);
227 return(!strcasecmp(hd, val));
9d87a119
FT
228}
229
64a9096a
FT
230static int canonreq(struct hthead *req)
231{
232 char *p, *p2, *r;
233 int n;
234
235 if(req->url[0] == '/') {
236 replrest(req, req->url + 1);
237 if((p = strchr(req->rest, '?')) != NULL)
238 *p = 0;
239 return(1);
240 }
241 if((p = strstr(req->url, "://")) != NULL) {
242 n = p - req->url;
243 if(((n == 4) && !strncasecmp(req->url, "http", 4)) ||
244 ((n == 5) && !strncasecmp(req->url, "https", 5))) {
245 if(getheader(req, "host"))
246 return(0);
247 p += 3;
248 if((p2 = strchr(p, '/')) == NULL) {
249 headappheader(req, "Host", p);
250 free(req->url);
251 req->url = sstrdup("/");
252 } else {
253 r = sstrdup(p2);
254 *(p2++) = 0;
255 headappheader(req, "Host", p);
256 free(req->url);
257 req->url = r;
258 }
259 replrest(req, req->url + 1);
260 if((p = strchr(req->rest, '?')) != NULL)
261 *p = 0;
262 return(1);
263 }
264 }
265 return(0);
266}
267
75bb20c8
FT
268static int http10keep(struct hthead *req, struct hthead *resp)
269{
270 int fc;
271
272 fc = hasheader(resp, "connection", "close");
273 headrmheader(resp, "connection");
274 if(!fc && hasheader(req, "connection", "keep-alive")) {
275 headappheader(resp, "Connection", "Keep-Alive");
276 return(1);
277 } else {
278 return(0);
279 }
280}
281
8774c31b 282void serve(FILE *in, struct conn *conn)
66987955 283{
af34331c 284 int pfds[2];
8774c31b 285 FILE *out;
9d87a119 286 struct hthead *req, *resp;
64a9096a 287 char *hd;
5fc1bf9f 288 off_t dlen;
75bb20c8 289 int keep;
66987955 290
5fc1bf9f 291 out = NULL;
3c296bd4 292 req = resp = NULL;
66987955 293 while(1) {
5fc1bf9f
FT
294 if((req = parsereq(in)) == NULL)
295 break;
64a9096a
FT
296 if(!canonreq(req))
297 break;
9d87a119 298
8774c31b
FT
299 if((conn->initreq != NULL) && conn->initreq(conn, req))
300 break;
301
46c3d430 302 if(block(plex, EV_WRITE, 60) <= 0)
5fc1bf9f 303 break;
af34331c 304 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
5fc1bf9f 305 break;
af34331c 306 if(sendreq(plex, req, pfds[0]))
5fc1bf9f 307 break;
af34331c 308 close(pfds[0]);
5fc1bf9f 309 out = mtstdopen(pfds[1], 1, 600, "r+");
a0327573 310
8d19a9ec
FT
311 if(getheader(req, "content-type") != NULL) {
312 if((hd = getheader(req, "content-length")) != NULL) {
313 dlen = atoo(hd);
314 if(dlen > 0) {
315 if(passdata(in, out, dlen) != dlen)
316 break;
317 }
318 } else if(((hd = getheader(req, "transfer-encoding")) != NULL) && !strcasecmp(hd, "chunked")) {
319 if(recvchunks(in, out))
5fc1bf9f 320 break;
8d19a9ec 321 } else {
2dbb9937
FT
322 /* Ignore rather than abort, to be kinder to broken clients. */
323 headrmheader(req, "content-type");
a06a2fbd 324 }
a0327573 325 }
5fc1bf9f
FT
326 if(fflush(out))
327 break;
d93d9a05 328 /* Make sure to send EOF */
5fc1bf9f 329 shutdown(pfds[1], SHUT_WR);
9d87a119 330
3ef78895 331 if((resp = parseresponse(out)) == NULL)
f9255ddd 332 break;
5fc1bf9f 333 replstr(&resp->ver, req->ver);
1c3e0167
FT
334
335 if(!getheader(resp, "server"))
336 headappheader(resp, "Server", sprintf3("ashd/%s", VERSION));
5fc1bf9f 337
cefb0f7a 338 if(!strcasecmp(req->ver, "HTTP/1.0")) {
18fb436d 339 if(!strcasecmp(req->method, "head")) {
75bb20c8
FT
340 keep = http10keep(req, resp);
341 writeresp(in, resp);
342 fprintf(in, "\r\n");
18fb436d 343 } else if((hd = getheader(resp, "content-length")) != NULL) {
75bb20c8 344 keep = http10keep(req, resp);
5226f7c5 345 dlen = atoo(hd);
75bb20c8
FT
346 writeresp(in, resp);
347 fprintf(in, "\r\n");
5226f7c5 348 if(passdata(out, in, dlen) != dlen)
5fc1bf9f 349 break;
5fc1bf9f 350 } else {
75bb20c8
FT
351 headrmheader(resp, "connection");
352 writeresp(in, resp);
353 fprintf(in, "\r\n");
5fc1bf9f
FT
354 passdata(out, in, -1);
355 break;
9d87a119 356 }
75bb20c8 357 if(!keep)
5fc1bf9f 358 break;
cefb0f7a 359 } else if(!strcasecmp(req->ver, "HTTP/1.1")) {
18fb436d
FT
360 if(!strcasecmp(req->method, "head")) {
361 writeresp(in, resp);
362 fprintf(in, "\r\n");
363 } else if((hd = getheader(resp, "content-length")) != NULL) {
5fc1bf9f
FT
364 writeresp(in, resp);
365 fprintf(in, "\r\n");
5226f7c5
FT
366 dlen = atoo(hd);
367 if(passdata(out, in, dlen) != dlen)
5fc1bf9f
FT
368 break;
369 } else if(!getheader(resp, "transfer-encoding")) {
370 headappheader(resp, "Transfer-Encoding", "chunked");
371 writeresp(in, resp);
372 fprintf(in, "\r\n");
373 if(passchunks(out, in))
374 break;
375 } else {
376 writeresp(in, resp);
377 fprintf(in, "\r\n");
378 passdata(out, in, -1);
9d87a119 379 break;
5fc1bf9f
FT
380 }
381 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
9d87a119 382 break;
5fc1bf9f
FT
383 } else {
384 break;
9d87a119 385 }
5fc1bf9f
FT
386
387 fclose(out);
388 out = NULL;
9d87a119 389 freehthead(req);
9d87a119 390 freehthead(resp);
5fc1bf9f 391 req = resp = NULL;
66987955
FT
392 }
393
5fc1bf9f
FT
394 if(out != NULL)
395 fclose(out);
9d87a119
FT
396 if(req != NULL)
397 freehthead(req);
398 if(resp != NULL)
399 freehthead(resp);
5fc1bf9f 400 fclose(in);
66987955
FT
401}
402
32e24c19
FT
403static void plexwatch(struct muth *muth, va_list args)
404{
405 vavar(int, fd);
406 char *buf;
407 int ret;
408
409 while(1) {
410 block(fd, EV_READ, 0);
411 buf = smalloc(65536);
412 ret = recv(fd, buf, 65536, 0);
413 if(ret < 0) {
414 flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
415 exit(1);
416 } else if(ret == 0) {
417 exit(0);
418 }
419 /* Maybe I'd like to implement some protocol in this direction
420 * some day... */
421 free(buf);
422 }
423}
424
d341283f
FT
425static void initroot(void *uu)
426{
427 int fd;
428
429 if(daemonize) {
430 setsid();
431 chdir("/");
432 if((fd = open("/dev/null", O_RDWR)) >= 0) {
433 dup2(fd, 0);
434 dup2(fd, 1);
435 dup2(fd, 2);
436 close(fd);
437 }
438 }
439 if(usesyslog)
440 putenv("ASHD_USESYSLOG=1");
441 else
442 unsetenv("ASHD_USESYSLOG");
443}
444
8774c31b
FT
445static void usage(FILE *out)
446{
43c58ba2 447 fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] [-p PIDFILE] PORTSPEC... -- ROOT [ARGS...]\n");
8774c31b 448 fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
6ca53b2e 449 fprintf(out, "\tavailable handlers are `plain' and `ssl'.\n");
8774c31b
FT
450}
451
452static void addport(char *spec)
453{
454 char *nm, *p, *p2, *n;
455 struct charvbuf pars, vals;
456
457 bufinit(pars);
458 bufinit(vals);
459 if((p = strchr(spec, ':')) == NULL) {
460 nm = spec;
461 } else {
462 nm = spec;
463 *(p++) = 0;
464 do {
465 if((n = strchr(p, ',')) != NULL)
466 *(n++) = 0;
467 if((p2 = strchr(p, '=')) != NULL)
468 *(p2++) = 0;
469 if(!*p) {
470 usage(stderr);
471 exit(1);
472 }
473 bufadd(pars, p);
474 if(p2)
475 bufadd(vals, p2);
476 else
477 bufadd(vals, "");
478 } while((p = n) != NULL);
479 }
480
481 /* XXX: It would be nice to decentralize this, but, meh... */
482 if(!strcmp(nm, "plain")) {
483 handleplain(pars.d, pars.b, vals.b);
6ca53b2e
FT
484#ifdef HAVE_GNUTLS
485 } else if(!strcmp(nm, "ssl")) {
486 handlegnussl(pars.d, pars.b, vals.b);
487#endif
8774c31b
FT
488 } else {
489 flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
490 exit(1);
491 }
492
493 buffree(pars);
494 buffree(vals);
495}
496
f0bbedf7
FT
497int main(int argc, char **argv)
498{
8774c31b
FT
499 int c;
500 int i, s1;
f0cbd8d7 501 char *root;
43c58ba2 502 FILE *pidout;
f0cbd8d7 503 struct passwd *pwent;
f4cdf919 504
d341283f 505 daemonize = usesyslog = 0;
f0cbd8d7
FT
506 root = NULL;
507 pwent = NULL;
43c58ba2 508 while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
8774c31b
FT
509 switch(c) {
510 case 'h':
511 usage(stdout);
512 exit(0);
f0cbd8d7
FT
513 case 'f':
514 daemonize = 1;
515 break;
516 case 'S':
d341283f 517 usesyslog = 1;
f0cbd8d7
FT
518 break;
519 case 'u':
520 if((pwent = getpwnam(optarg)) == NULL) {
521 flog(LOG_ERR, "could not find user %s", optarg);
522 exit(1);
523 }
524 break;
525 case 'r':
526 root = optarg;
527 break;
43c58ba2
FT
528 case 'p':
529 pidfile = optarg;
530 break;
8774c31b
FT
531 default:
532 usage(stderr);
533 exit(1);
534 }
535 }
8774c31b
FT
536 s1 = 0;
537 for(i = optind; i < argc; i++) {
538 if(!strcmp(argv[i], "--"))
539 break;
540 s1 = 1;
541 addport(argv[i]);
9d87a119 542 }
8774c31b
FT
543 if(!s1 || (i == argc)) {
544 usage(stderr);
545 exit(1);
f4cdf919 546 }
d341283f 547 if((plex = stdmkchild(argv + ++i, initroot, NULL)) < 0) {
8774c31b
FT
548 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
549 return(1);
f4cdf919 550 }
32e24c19 551 mustart(plexwatch, plex);
43c58ba2
FT
552 pidout = NULL;
553 if(pidfile != NULL) {
554 if((pidout = fopen(pidfile, "w")) == NULL) {
555 flog(LOG_ERR, "could not open %s for writing: %s", pidfile, strerror(errno));
556 return(1);
557 }
558 }
d341283f 559 if(usesyslog)
f0cbd8d7
FT
560 opensyslog();
561 if(root) {
0370bd82 562 if(chdir(root) || chroot(root)) {
f0cbd8d7
FT
563 flog(LOG_ERR, "could not chroot to %s: %s", root, strerror(errno));
564 exit(1);
565 }
566 }
567 if(pwent) {
568 if(setgid(pwent->pw_gid)) {
569 flog(LOG_ERR, "could not switch group to %i: %s", (int)pwent->pw_gid, strerror(errno));
570 exit(1);
571 }
572 if(setuid(pwent->pw_uid)) {
573 flog(LOG_ERR, "could not switch user to %i: %s", (int)pwent->pw_uid, strerror(errno));
574 exit(1);
575 }
576 }
15fa3fe8 577 signal(SIGPIPE, SIG_IGN);
f0cbd8d7
FT
578 if(daemonize) {
579 daemon(0, 0);
580 }
ee036f74 581 if(pidout != NULL) {
43c58ba2 582 fprintf(pidout, "%i\n", getpid());
ee036f74
FT
583 fclose(pidout);
584 }
f4cdf919
FT
585 ioloop();
586 return(0);
f0bbedf7 587}