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