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