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