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