htparser: Implemented resource limits for response headers as well.
[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
8774c31b 243void serve(FILE *in, struct conn *conn)
66987955 244{
af34331c 245 int pfds[2];
8774c31b 246 FILE *out;
9d87a119 247 struct hthead *req, *resp;
5fc1bf9f
FT
248 char *hd, *p;
249 off_t dlen;
66987955 250
5fc1bf9f 251 out = NULL;
3c296bd4 252 req = resp = NULL;
66987955 253 while(1) {
5fc1bf9f
FT
254 if((req = parsereq(in)) == NULL)
255 break;
256 replrest(req, req->url);
9e9eca79
FT
257 if(req->rest[0] == '/')
258 replrest(req, req->rest + 1);
edad3c6a
FT
259 if((p = strchr(req->rest, '?')) != NULL)
260 *p = 0;
9d87a119 261
8774c31b
FT
262 if((conn->initreq != NULL) && conn->initreq(conn, req))
263 break;
264
46c3d430 265 if(block(plex, EV_WRITE, 60) <= 0)
5fc1bf9f 266 break;
af34331c 267 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
5fc1bf9f 268 break;
af34331c 269 if(sendreq(plex, req, pfds[0]))
5fc1bf9f 270 break;
af34331c 271 close(pfds[0]);
5fc1bf9f 272 out = mtstdopen(pfds[1], 1, 600, "r+");
a0327573 273
a0327573
FT
274 if((hd = getheader(req, "content-length")) != NULL) {
275 dlen = atoo(hd);
a06a2fbd 276 if(dlen > 0) {
5fc1bf9f
FT
277 if(passdata(in, out, dlen) != dlen)
278 break;
a06a2fbd 279 }
a0327573 280 }
5fc1bf9f
FT
281 if(fflush(out))
282 break;
d93d9a05 283 /* Make sure to send EOF */
5fc1bf9f 284 shutdown(pfds[1], SHUT_WR);
9d87a119 285
f9255ddd
FT
286 if((resp = parseresp(out)) == NULL)
287 break;
5fc1bf9f 288 replstr(&resp->ver, req->ver);
1c3e0167
FT
289
290 if(!getheader(resp, "server"))
291 headappheader(resp, "Server", sprintf3("ashd/%s", VERSION));
5fc1bf9f
FT
292
293 if(!strcmp(req->ver, "HTTP/1.0")) {
294 writeresp(in, resp);
295 fprintf(in, "\r\n");
18fb436d
FT
296 if(!strcasecmp(req->method, "head")) {
297 if(!hasheader(req, "connection", "keep-alive"))
298 break;
299 } else if((hd = getheader(resp, "content-length")) != NULL) {
5226f7c5
FT
300 dlen = atoo(hd);
301 if(passdata(out, in, dlen) != dlen)
5fc1bf9f
FT
302 break;
303 if(!hasheader(req, "connection", "keep-alive"))
9d87a119 304 break;
5fc1bf9f
FT
305 } else {
306 passdata(out, in, -1);
307 break;
9d87a119 308 }
5fc1bf9f
FT
309 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
310 break;
311 } else if(!strcmp(req->ver, "HTTP/1.1")) {
18fb436d
FT
312 if(!strcasecmp(req->method, "head")) {
313 writeresp(in, resp);
314 fprintf(in, "\r\n");
315 } else if((hd = getheader(resp, "content-length")) != NULL) {
5fc1bf9f
FT
316 writeresp(in, resp);
317 fprintf(in, "\r\n");
5226f7c5
FT
318 dlen = atoo(hd);
319 if(passdata(out, in, dlen) != dlen)
5fc1bf9f
FT
320 break;
321 } else if(!getheader(resp, "transfer-encoding")) {
322 headappheader(resp, "Transfer-Encoding", "chunked");
323 writeresp(in, resp);
324 fprintf(in, "\r\n");
325 if(passchunks(out, in))
326 break;
327 } else {
328 writeresp(in, resp);
329 fprintf(in, "\r\n");
330 passdata(out, in, -1);
9d87a119 331 break;
5fc1bf9f
FT
332 }
333 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
9d87a119 334 break;
5fc1bf9f
FT
335 } else {
336 break;
9d87a119 337 }
5fc1bf9f
FT
338
339 fclose(out);
340 out = NULL;
9d87a119 341 freehthead(req);
9d87a119 342 freehthead(resp);
5fc1bf9f 343 req = resp = NULL;
66987955
FT
344 }
345
5fc1bf9f
FT
346 if(out != NULL)
347 fclose(out);
9d87a119
FT
348 if(req != NULL)
349 freehthead(req);
350 if(resp != NULL)
351 freehthead(resp);
5fc1bf9f 352 fclose(in);
66987955
FT
353}
354
32e24c19
FT
355static void plexwatch(struct muth *muth, va_list args)
356{
357 vavar(int, fd);
358 char *buf;
359 int ret;
360
361 while(1) {
362 block(fd, EV_READ, 0);
363 buf = smalloc(65536);
364 ret = recv(fd, buf, 65536, 0);
365 if(ret < 0) {
366 flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
367 exit(1);
368 } else if(ret == 0) {
369 exit(0);
370 }
371 /* Maybe I'd like to implement some protocol in this direction
372 * some day... */
373 free(buf);
374 }
375}
376
d341283f
FT
377static void initroot(void *uu)
378{
379 int fd;
380
381 if(daemonize) {
382 setsid();
383 chdir("/");
384 if((fd = open("/dev/null", O_RDWR)) >= 0) {
385 dup2(fd, 0);
386 dup2(fd, 1);
387 dup2(fd, 2);
388 close(fd);
389 }
390 }
391 if(usesyslog)
392 putenv("ASHD_USESYSLOG=1");
393 else
394 unsetenv("ASHD_USESYSLOG");
395}
396
8774c31b
FT
397static void usage(FILE *out)
398{
43c58ba2 399 fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] [-p PIDFILE] PORTSPEC... -- ROOT [ARGS...]\n");
8774c31b 400 fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
6ca53b2e 401 fprintf(out, "\tavailable handlers are `plain' and `ssl'.\n");
8774c31b
FT
402}
403
404static void addport(char *spec)
405{
406 char *nm, *p, *p2, *n;
407 struct charvbuf pars, vals;
408
409 bufinit(pars);
410 bufinit(vals);
411 if((p = strchr(spec, ':')) == NULL) {
412 nm = spec;
413 } else {
414 nm = spec;
415 *(p++) = 0;
416 do {
417 if((n = strchr(p, ',')) != NULL)
418 *(n++) = 0;
419 if((p2 = strchr(p, '=')) != NULL)
420 *(p2++) = 0;
421 if(!*p) {
422 usage(stderr);
423 exit(1);
424 }
425 bufadd(pars, p);
426 if(p2)
427 bufadd(vals, p2);
428 else
429 bufadd(vals, "");
430 } while((p = n) != NULL);
431 }
432
433 /* XXX: It would be nice to decentralize this, but, meh... */
434 if(!strcmp(nm, "plain")) {
435 handleplain(pars.d, pars.b, vals.b);
6ca53b2e
FT
436#ifdef HAVE_GNUTLS
437 } else if(!strcmp(nm, "ssl")) {
438 handlegnussl(pars.d, pars.b, vals.b);
439#endif
8774c31b
FT
440 } else {
441 flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
442 exit(1);
443 }
444
445 buffree(pars);
446 buffree(vals);
447}
448
f0bbedf7
FT
449int main(int argc, char **argv)
450{
8774c31b
FT
451 int c;
452 int i, s1;
f0cbd8d7 453 char *root;
43c58ba2 454 FILE *pidout;
f0cbd8d7 455 struct passwd *pwent;
f4cdf919 456
d341283f 457 daemonize = usesyslog = 0;
f0cbd8d7
FT
458 root = NULL;
459 pwent = NULL;
43c58ba2 460 while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
8774c31b
FT
461 switch(c) {
462 case 'h':
463 usage(stdout);
464 exit(0);
f0cbd8d7
FT
465 case 'f':
466 daemonize = 1;
467 break;
468 case 'S':
d341283f 469 usesyslog = 1;
f0cbd8d7
FT
470 break;
471 case 'u':
472 if((pwent = getpwnam(optarg)) == NULL) {
473 flog(LOG_ERR, "could not find user %s", optarg);
474 exit(1);
475 }
476 break;
477 case 'r':
478 root = optarg;
479 break;
43c58ba2
FT
480 case 'p':
481 pidfile = optarg;
482 break;
8774c31b
FT
483 default:
484 usage(stderr);
485 exit(1);
486 }
487 }
8774c31b
FT
488 s1 = 0;
489 for(i = optind; i < argc; i++) {
490 if(!strcmp(argv[i], "--"))
491 break;
492 s1 = 1;
493 addport(argv[i]);
9d87a119 494 }
8774c31b
FT
495 if(!s1 || (i == argc)) {
496 usage(stderr);
497 exit(1);
f4cdf919 498 }
d341283f 499 if((plex = stdmkchild(argv + ++i, initroot, NULL)) < 0) {
8774c31b
FT
500 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
501 return(1);
f4cdf919 502 }
32e24c19 503 mustart(plexwatch, plex);
43c58ba2
FT
504 pidout = NULL;
505 if(pidfile != NULL) {
506 if((pidout = fopen(pidfile, "w")) == NULL) {
507 flog(LOG_ERR, "could not open %s for writing: %s", pidfile, strerror(errno));
508 return(1);
509 }
510 }
d341283f 511 if(usesyslog)
f0cbd8d7
FT
512 opensyslog();
513 if(root) {
0370bd82 514 if(chdir(root) || chroot(root)) {
f0cbd8d7
FT
515 flog(LOG_ERR, "could not chroot to %s: %s", root, strerror(errno));
516 exit(1);
517 }
518 }
519 if(pwent) {
520 if(setgid(pwent->pw_gid)) {
521 flog(LOG_ERR, "could not switch group to %i: %s", (int)pwent->pw_gid, strerror(errno));
522 exit(1);
523 }
524 if(setuid(pwent->pw_uid)) {
525 flog(LOG_ERR, "could not switch user to %i: %s", (int)pwent->pw_uid, strerror(errno));
526 exit(1);
527 }
528 }
15fa3fe8 529 signal(SIGPIPE, SIG_IGN);
f0cbd8d7
FT
530 if(daemonize) {
531 daemon(0, 0);
532 }
ee036f74 533 if(pidout != NULL) {
43c58ba2 534 fprintf(pidout, "%i\n", getpid());
ee036f74
FT
535 fclose(pidout);
536 }
f4cdf919
FT
537 ioloop();
538 return(0);
f0bbedf7 539}