python: Changed wmain calling convention.
[ashd.git] / src / htparser.c
... / ...
CommitLineData
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>
22#include <fcntl.h>
23#include <string.h>
24#include <sys/socket.h>
25#include <pwd.h>
26#include <sys/signal.h>
27#include <errno.h>
28
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
32#include <utils.h>
33#include <mt.h>
34#include <mtio.h>
35#include <log.h>
36#include <req.h>
37#include <proc.h>
38
39#include "htparser.h"
40
41static int plex;
42static char *pidfile = NULL;
43static int daemonize, usesyslog;
44
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
62static struct hthead *parsereq(FILE *in)
63{
64 struct hthead *req;
65 struct charbuf method, url, ver;
66 int c;
67
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);
80 }
81 }
82 while(1) {
83 c = getc(in);
84 if(c == ' ') {
85 break;
86 } else if((c == EOF) || (c < 32)) {
87 goto fail;
88 } else {
89 bufadd(url, c);
90 }
91 }
92 while(1) {
93 c = getc(in);
94 if(c == 10) {
95 break;
96 } else if(c == 13) {
97 } else if((c == EOF) || (c < 32) || (c >= 128)) {
98 goto fail;
99 } else {
100 bufadd(ver, c);
101 }
102 }
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;
109 trimx(req);
110 goto out;
111
112fail:
113 if(req != NULL) {
114 freehthead(req);
115 req = NULL;
116 }
117out:
118 buffree(method);
119 buffree(url);
120 buffree(ver);
121 return(req);
122}
123
124static struct hthead *parseresp(FILE *in)
125{
126 struct hthead *req;
127 int code;
128 struct charbuf ver, msg;
129 int c;
130
131 req = NULL;
132 bufinit(ver);
133 bufinit(msg);
134 code = 0;
135 while(1) {
136 c = getc(in);
137 if(c == ' ') {
138 break;
139 } else if((c == EOF) || (c < 32) || (c >= 128)) {
140 goto fail;
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')) {
150 goto fail;
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)) {
161 goto fail;
162 } else {
163 bufadd(msg, c);
164 }
165 }
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;
172
173fail:
174 if(req != NULL) {
175 freehthead(req);
176 req = NULL;
177 }
178out:
179 buffree(msg);
180 buffree(ver);
181 return(req);
182}
183
184static off_t passdata(FILE *in, FILE *out, off_t max)
185{
186 size_t read;
187 off_t total;
188 char buf[8192];
189
190 total = 0;
191 while(!feof(in) && ((max < 0) || (total < max))) {
192 read = sizeof(buf);
193 if(max >= 0)
194 read = min(max - total, read);
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;
201 }
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);
214 fprintf(out, "%zx\r\n", read);
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));
229}
230
231void serve(FILE *in, struct conn *conn)
232{
233 int pfds[2];
234 FILE *out;
235 struct hthead *req, *resp;
236 char *hd, *p;
237 off_t dlen;
238
239 out = NULL;
240 req = resp = NULL;
241 while(1) {
242 if((req = parsereq(in)) == NULL)
243 break;
244 replrest(req, req->url);
245 if(req->rest[0] == '/')
246 replrest(req, req->rest + 1);
247 if((p = strchr(req->rest, '?')) != NULL)
248 *p = 0;
249
250 if((conn->initreq != NULL) && conn->initreq(conn, req))
251 break;
252
253 if(block(plex, EV_WRITE, 60) <= 0)
254 break;
255 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
256 break;
257 if(sendreq(plex, req, pfds[0]))
258 break;
259 close(pfds[0]);
260 out = mtstdopen(pfds[1], 1, 600, "r+");
261
262 if((hd = getheader(req, "content-length")) != NULL) {
263 dlen = atoo(hd);
264 if(dlen > 0) {
265 if(passdata(in, out, dlen) != dlen)
266 break;
267 }
268 }
269 if(fflush(out))
270 break;
271 /* Make sure to send EOF */
272 shutdown(pfds[1], SHUT_WR);
273
274 if((resp = parseresp(out)) == NULL)
275 break;
276 replstr(&resp->ver, req->ver);
277
278 if(!getheader(resp, "server"))
279 headappheader(resp, "Server", sprintf3("ashd/%s", VERSION));
280
281 if(!strcmp(req->ver, "HTTP/1.0")) {
282 writeresp(in, resp);
283 fprintf(in, "\r\n");
284 if(!strcasecmp(req->method, "head")) {
285 if(!hasheader(req, "connection", "keep-alive"))
286 break;
287 } else if((hd = getheader(resp, "content-length")) != NULL) {
288 dlen = atoo(hd);
289 if(passdata(out, in, dlen) != dlen)
290 break;
291 if(!hasheader(req, "connection", "keep-alive"))
292 break;
293 } else {
294 passdata(out, in, -1);
295 break;
296 }
297 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
298 break;
299 } else if(!strcmp(req->ver, "HTTP/1.1")) {
300 if(!strcasecmp(req->method, "head")) {
301 writeresp(in, resp);
302 fprintf(in, "\r\n");
303 } else if((hd = getheader(resp, "content-length")) != NULL) {
304 writeresp(in, resp);
305 fprintf(in, "\r\n");
306 dlen = atoo(hd);
307 if(passdata(out, in, dlen) != dlen)
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);
319 break;
320 }
321 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
322 break;
323 } else {
324 break;
325 }
326
327 fclose(out);
328 out = NULL;
329 freehthead(req);
330 freehthead(resp);
331 req = resp = NULL;
332 }
333
334 if(out != NULL)
335 fclose(out);
336 if(req != NULL)
337 freehthead(req);
338 if(resp != NULL)
339 freehthead(resp);
340 fclose(in);
341}
342
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
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
385static void usage(FILE *out)
386{
387 fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] [-p PIDFILE] PORTSPEC... -- ROOT [ARGS...]\n");
388 fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
389 fprintf(out, "\tavailable handlers are `plain' and `ssl'.\n");
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);
424#ifdef HAVE_GNUTLS
425 } else if(!strcmp(nm, "ssl")) {
426 handlegnussl(pars.d, pars.b, vals.b);
427#endif
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
437int main(int argc, char **argv)
438{
439 int c;
440 int i, s1;
441 char *root;
442 FILE *pidout;
443 struct passwd *pwent;
444
445 daemonize = usesyslog = 0;
446 root = NULL;
447 pwent = NULL;
448 while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
449 switch(c) {
450 case 'h':
451 usage(stdout);
452 exit(0);
453 case 'f':
454 daemonize = 1;
455 break;
456 case 'S':
457 usesyslog = 1;
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;
468 case 'p':
469 pidfile = optarg;
470 break;
471 default:
472 usage(stderr);
473 exit(1);
474 }
475 }
476 s1 = 0;
477 for(i = optind; i < argc; i++) {
478 if(!strcmp(argv[i], "--"))
479 break;
480 s1 = 1;
481 addport(argv[i]);
482 }
483 if(!s1 || (i == argc)) {
484 usage(stderr);
485 exit(1);
486 }
487 if((plex = stdmkchild(argv + ++i, initroot, NULL)) < 0) {
488 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
489 return(1);
490 }
491 mustart(plexwatch, plex);
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 }
499 if(usesyslog)
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 }
517 signal(SIGPIPE, SIG_IGN);
518 if(daemonize) {
519 daemon(0, 0);
520 }
521 if(pidout != NULL) {
522 fprintf(pidout, "%i\n", getpid());
523 fclose(pidout);
524 }
525 ioloop();
526 return(0);
527}