doc: Added manpage for htextauth.
[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 274 replstr(&resp->ver, req->ver);
1c3e0167
FT
275
276 if(!getheader(resp, "server"))
277 headappheader(resp, "Server", sprintf3("ashd/%s", VERSION));
5fc1bf9f
FT
278
279 if(!strcmp(req->ver, "HTTP/1.0")) {
280 writeresp(in, resp);
281 fprintf(in, "\r\n");
18fb436d
FT
282 if(!strcasecmp(req->method, "head")) {
283 if(!hasheader(req, "connection", "keep-alive"))
284 break;
285 } else if((hd = getheader(resp, "content-length")) != NULL) {
5226f7c5
FT
286 dlen = atoo(hd);
287 if(passdata(out, in, dlen) != dlen)
5fc1bf9f
FT
288 break;
289 if(!hasheader(req, "connection", "keep-alive"))
9d87a119 290 break;
5fc1bf9f
FT
291 } else {
292 passdata(out, in, -1);
293 break;
9d87a119 294 }
5fc1bf9f
FT
295 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
296 break;
297 } else if(!strcmp(req->ver, "HTTP/1.1")) {
18fb436d
FT
298 if(!strcasecmp(req->method, "head")) {
299 writeresp(in, resp);
300 fprintf(in, "\r\n");
301 } else if((hd = getheader(resp, "content-length")) != NULL) {
5fc1bf9f
FT
302 writeresp(in, resp);
303 fprintf(in, "\r\n");
5226f7c5
FT
304 dlen = atoo(hd);
305 if(passdata(out, in, dlen) != dlen)
5fc1bf9f
FT
306 break;
307 } else if(!getheader(resp, "transfer-encoding")) {
308 headappheader(resp, "Transfer-Encoding", "chunked");
309 writeresp(in, resp);
310 fprintf(in, "\r\n");
311 if(passchunks(out, in))
312 break;
313 } else {
314 writeresp(in, resp);
315 fprintf(in, "\r\n");
316 passdata(out, in, -1);
9d87a119 317 break;
5fc1bf9f
FT
318 }
319 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
9d87a119 320 break;
5fc1bf9f
FT
321 } else {
322 break;
9d87a119 323 }
5fc1bf9f
FT
324
325 fclose(out);
326 out = NULL;
9d87a119 327 freehthead(req);
9d87a119 328 freehthead(resp);
5fc1bf9f 329 req = resp = NULL;
66987955
FT
330 }
331
5fc1bf9f
FT
332 if(out != NULL)
333 fclose(out);
9d87a119
FT
334 if(req != NULL)
335 freehthead(req);
336 if(resp != NULL)
337 freehthead(resp);
5fc1bf9f 338 fclose(in);
66987955
FT
339}
340
32e24c19
FT
341static void plexwatch(struct muth *muth, va_list args)
342{
343 vavar(int, fd);
344 char *buf;
345 int ret;
346
347 while(1) {
348 block(fd, EV_READ, 0);
349 buf = smalloc(65536);
350 ret = recv(fd, buf, 65536, 0);
351 if(ret < 0) {
352 flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
353 exit(1);
354 } else if(ret == 0) {
355 exit(0);
356 }
357 /* Maybe I'd like to implement some protocol in this direction
358 * some day... */
359 free(buf);
360 }
361}
362
8774c31b
FT
363static void usage(FILE *out)
364{
43c58ba2 365 fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] [-p PIDFILE] PORTSPEC... -- ROOT [ARGS...]\n");
8774c31b 366 fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
6ca53b2e 367 fprintf(out, "\tavailable handlers are `plain' and `ssl'.\n");
8774c31b
FT
368}
369
370static void addport(char *spec)
371{
372 char *nm, *p, *p2, *n;
373 struct charvbuf pars, vals;
374
375 bufinit(pars);
376 bufinit(vals);
377 if((p = strchr(spec, ':')) == NULL) {
378 nm = spec;
379 } else {
380 nm = spec;
381 *(p++) = 0;
382 do {
383 if((n = strchr(p, ',')) != NULL)
384 *(n++) = 0;
385 if((p2 = strchr(p, '=')) != NULL)
386 *(p2++) = 0;
387 if(!*p) {
388 usage(stderr);
389 exit(1);
390 }
391 bufadd(pars, p);
392 if(p2)
393 bufadd(vals, p2);
394 else
395 bufadd(vals, "");
396 } while((p = n) != NULL);
397 }
398
399 /* XXX: It would be nice to decentralize this, but, meh... */
400 if(!strcmp(nm, "plain")) {
401 handleplain(pars.d, pars.b, vals.b);
6ca53b2e
FT
402#ifdef HAVE_GNUTLS
403 } else if(!strcmp(nm, "ssl")) {
404 handlegnussl(pars.d, pars.b, vals.b);
405#endif
8774c31b
FT
406 } else {
407 flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
408 exit(1);
409 }
410
411 buffree(pars);
412 buffree(vals);
413}
414
f0bbedf7
FT
415int main(int argc, char **argv)
416{
8774c31b
FT
417 int c;
418 int i, s1;
f0cbd8d7
FT
419 int daemonize, logsys;
420 char *root;
43c58ba2 421 FILE *pidout;
f0cbd8d7 422 struct passwd *pwent;
f4cdf919 423
f0cbd8d7
FT
424 daemonize = logsys = 0;
425 root = NULL;
426 pwent = NULL;
43c58ba2 427 while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
8774c31b
FT
428 switch(c) {
429 case 'h':
430 usage(stdout);
431 exit(0);
f0cbd8d7
FT
432 case 'f':
433 daemonize = 1;
434 break;
435 case 'S':
436 logsys = 1;
437 break;
438 case 'u':
439 if((pwent = getpwnam(optarg)) == NULL) {
440 flog(LOG_ERR, "could not find user %s", optarg);
441 exit(1);
442 }
443 break;
444 case 'r':
445 root = optarg;
446 break;
43c58ba2
FT
447 case 'p':
448 pidfile = optarg;
449 break;
8774c31b
FT
450 default:
451 usage(stderr);
452 exit(1);
453 }
454 }
8774c31b
FT
455 s1 = 0;
456 for(i = optind; i < argc; i++) {
457 if(!strcmp(argv[i], "--"))
458 break;
459 s1 = 1;
460 addport(argv[i]);
9d87a119 461 }
8774c31b
FT
462 if(!s1 || (i == argc)) {
463 usage(stderr);
464 exit(1);
f4cdf919 465 }
6a7a868e 466 if((plex = stdmkchild(argv + ++i, NULL, NULL)) < 0) {
8774c31b
FT
467 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
468 return(1);
f4cdf919 469 }
32e24c19 470 mustart(plexwatch, plex);
43c58ba2
FT
471 pidout = NULL;
472 if(pidfile != NULL) {
473 if((pidout = fopen(pidfile, "w")) == NULL) {
474 flog(LOG_ERR, "could not open %s for writing: %s", pidfile, strerror(errno));
475 return(1);
476 }
477 }
f0cbd8d7
FT
478 if(logsys)
479 opensyslog();
480 if(root) {
481 if(chroot(root)) {
482 flog(LOG_ERR, "could not chroot to %s: %s", root, strerror(errno));
483 exit(1);
484 }
485 }
486 if(pwent) {
487 if(setgid(pwent->pw_gid)) {
488 flog(LOG_ERR, "could not switch group to %i: %s", (int)pwent->pw_gid, strerror(errno));
489 exit(1);
490 }
491 if(setuid(pwent->pw_uid)) {
492 flog(LOG_ERR, "could not switch user to %i: %s", (int)pwent->pw_uid, strerror(errno));
493 exit(1);
494 }
495 }
15fa3fe8 496 signal(SIGPIPE, SIG_IGN);
f0cbd8d7
FT
497 if(daemonize) {
498 daemon(0, 0);
499 }
ee036f74 500 if(pidout != NULL) {
43c58ba2 501 fprintf(pidout, "%i\n", getpid());
ee036f74
FT
502 fclose(pidout);
503 }
f4cdf919
FT
504 ioloop();
505 return(0);
f0bbedf7 506}