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