bozohttpd.c revision 1.106 1 /* $NetBSD: bozohttpd.c,v 1.106 2018/12/15 12:43:24 leot Exp $ */
2
3 /* $eterna: bozohttpd.c,v 1.178 2011/11/18 09:21:15 mrg Exp $ */
4
5 /*
6 * Copyright (c) 1997-2018 Matthew R. Green
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer and
16 * dedication in the documentation and/or other materials provided
17 * with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 */
32
33 /* this program is dedicated to the Great God of Processed Cheese */
34
35 /*
36 * bozohttpd.c: minimal httpd; provides only these features:
37 * - HTTP/0.9 (by virtue of ..)
38 * - HTTP/1.0
39 * - HTTP/1.1
40 * - CGI/1.1 this will only be provided for "system" scripts
41 * - automatic "missing trailing slash" redirections
42 * - configurable translation of /~user/ to ~user/public_html,
43 * however, this does not include cgi-bin support
44 * - access lists via libwrap via inetd/tcpd
45 * - virtual hosting
46 * - not that we do not even pretend to understand MIME, but
47 * rely only on the HTTP specification
48 * - ipv6 support
49 * - automatic `index.html' generation
50 * - configurable server name
51 * - directory index generation
52 * - daemon mode (lacks libwrap support)
53 * - .htpasswd support
54 */
55
56 /*
57 * requirements for minimal http/1.1 (at least, as documented in
58 * RFC 2616 (HTTP/1.1):
59 *
60 * - 14.11: content-encoding handling. [1]
61 *
62 * - 14.13: content-length handling. this is only a SHOULD header
63 * thus we could just not send it ever. [1]
64 *
65 * - 14.17: content-type handling. [1]
66 *
67 * - 14.28: if-unmodified-since handling. if-modified-since is
68 * done since, shouldn't be too hard for this one.
69 *
70 * [1] need to revisit to ensure proper behaviour
71 *
72 * and the following is a list of features that we do not need
73 * to have due to other limits, or are too lazy. there are more
74 * of these than are listed, but these are of particular note,
75 * and could perhaps be implemented.
76 *
77 * - 3.5/3.6: content/transfer codings. probably can ignore
78 * this? we "SHOULD"n't. but 4.4 says we should ignore a
79 * `content-length' header upon reciept of a `transfer-encoding'
80 * header.
81 *
82 * - 5.1.1: request methods. only MUST support GET and HEAD,
83 * but there are new ones besides POST that are currently
84 * supported: OPTIONS PUT DELETE TRACE and CONNECT, plus
85 * extensions not yet known?
86 *
87 * - 10.1: we can ignore informational status codes
88 *
89 * - 10.3.3/10.3.4/10.3.8: just use '302' codes always.
90 *
91 * - 14.1/14.2/14.3/14.27: we do not support Accept: headers.
92 * just ignore them and send the request anyway. they are
93 * only SHOULD.
94 *
95 * - 14.5/14.16/14.35: only support simple ranges: %d- and %d-%d
96 * would be nice to support more.
97 *
98 * - 14.9: we aren't a cache.
99 *
100 * - 14.15: content-md5 would be nice.
101 *
102 * - 14.24/14.26/14.27: if-match, if-none-match, if-range. be
103 * nice to support this.
104 *
105 * - 14.44: Vary: seems unneeded. ignore it for now.
106 */
107
108 #ifndef INDEX_HTML
109 #define INDEX_HTML "index.html"
110 #endif
111 #ifndef SERVER_SOFTWARE
112 #define SERVER_SOFTWARE "bozohttpd/20181125"
113 #endif
114 #ifndef PUBLIC_HTML
115 #define PUBLIC_HTML "public_html"
116 #endif
117
118 #ifndef USE_ARG
119 #define USE_ARG(x) /*LINTED*/(void)&(x)
120 #endif
121
122 /*
123 * And so it begins ..
124 */
125
126 #include <sys/param.h>
127 #include <sys/socket.h>
128 #include <sys/time.h>
129 #include <sys/mman.h>
130
131 #include <arpa/inet.h>
132
133 #include <ctype.h>
134 #include <dirent.h>
135 #include <errno.h>
136 #include <fcntl.h>
137 #include <netdb.h>
138 #include <pwd.h>
139 #include <grp.h>
140 #include <signal.h>
141 #include <stdarg.h>
142 #include <stdlib.h>
143 #include <stdbool.h>
144 #include <strings.h>
145 #include <string.h>
146 #include <syslog.h>
147 #include <time.h>
148 #include <unistd.h>
149
150 #include "bozohttpd.h"
151
152 #ifndef INITIAL_TIMEOUT
153 #define INITIAL_TIMEOUT "30" /* wait for 30 seconds initially */
154 #endif
155 #ifndef HEADER_WAIT_TIME
156 #define HEADER_WAIT_TIME "10" /* need more headers every 10 seconds */
157 #endif
158 #ifndef TOTAL_MAX_REQ_TIME
159 #define TOTAL_MAX_REQ_TIME "600" /* must have total request in 600 */
160 #endif /* seconds */
161
162 /* if monotonic time is not available try real time. */
163 #ifndef CLOCK_MONOTONIC
164 #define CLOCK_MONOTONIC CLOCK_REALTIME
165 #endif
166
167 /* variables and functions */
168 #ifndef LOG_FTP
169 #define LOG_FTP LOG_DAEMON
170 #endif
171
172 /*
173 * List of special file that we should never serve.
174 */
175 struct {
176 const char *file;
177 const char *name;
178 } specials[] = {
179 { DIRECT_ACCESS_FILE, "rejected direct access request" },
180 { REDIRECT_FILE, "rejected redirect request" },
181 { ABSREDIRECT_FILE, "rejected absredirect request" },
182 { REMAP_FILE, "rejected remap request" },
183 { AUTH_FILE, "rejected authfile request" },
184 { NULL, NULL },
185 };
186
187 volatile sig_atomic_t timeout_hit;
188
189 /*
190 * check there's enough space in the prefs and names arrays.
191 */
192 static int
193 size_arrays(bozoprefs_t *bozoprefs, size_t needed)
194 {
195 char **temp;
196
197 if (bozoprefs->size == 0) {
198 /* only get here first time around */
199 bozoprefs->name = calloc(sizeof(char *), needed);
200 if (bozoprefs->name == NULL)
201 return 0;
202 bozoprefs->value = calloc(sizeof(char *), needed);
203 if (bozoprefs->value == NULL) {
204 free(bozoprefs->name);
205 return 0;
206 }
207 bozoprefs->size = needed;
208 } else if (bozoprefs->count == bozoprefs->size) {
209 /* only uses 'needed' when filled array */
210 temp = realloc(bozoprefs->name, sizeof(char *) * needed);
211 if (temp == NULL)
212 return 0;
213 bozoprefs->name = temp;
214 temp = realloc(bozoprefs->value, sizeof(char *) * needed);
215 if (temp == NULL)
216 return 0;
217 bozoprefs->value = temp;
218 bozoprefs->size += needed;
219 }
220 return 1;
221 }
222
223 static ssize_t
224 findvar(bozoprefs_t *bozoprefs, const char *name)
225 {
226 size_t i;
227
228 for (i = 0; i < bozoprefs->count; i++)
229 if (strcmp(bozoprefs->name[i], name) == 0)
230 return (ssize_t)i;
231 return -1;
232 }
233
234 int
235 bozo_set_pref(bozohttpd_t *httpd, bozoprefs_t *bozoprefs,
236 const char *name, const char *value)
237 {
238 ssize_t i;
239
240 if ((i = findvar(bozoprefs, name)) < 0) {
241 /* add the element to the array */
242 if (!size_arrays(bozoprefs, bozoprefs->size + 15))
243 return 0;
244 i = bozoprefs->count++;
245 bozoprefs->name[i] = bozostrdup(httpd, NULL, name);
246 } else {
247 /* replace the element in the array */
248 if (bozoprefs->value[i]) {
249 free(bozoprefs->value[i]);
250 bozoprefs->value[i] = NULL;
251 }
252 }
253 bozoprefs->value[i] = bozostrdup(httpd, NULL, value);
254 return 1;
255 }
256
257 /*
258 * get a variable's value, or NULL
259 */
260 char *
261 bozo_get_pref(bozoprefs_t *bozoprefs, const char *name)
262 {
263 ssize_t i;
264
265 i = findvar(bozoprefs, name);
266 return i < 0 ? NULL : bozoprefs->value[i];
267 }
268
269 char *
270 bozo_http_date(char *date, size_t datelen)
271 {
272 struct tm *tm;
273 time_t now;
274
275 /* Sun, 06 Nov 1994 08:49:37 GMT */
276 now = time(NULL);
277 tm = gmtime(&now); /* HTTP/1.1 spec rev 06 sez GMT only */
278 strftime(date, datelen, "%a, %d %b %Y %H:%M:%S GMT", tm);
279 return date;
280 }
281
282 /*
283 * convert "in" into the three parts of a request (first line).
284 * we allocate into file and query, but return pointers into
285 * "in" for proto and method.
286 */
287 static void
288 parse_request(bozohttpd_t *httpd, char *in, char **method, char **file,
289 char **query, char **proto)
290 {
291 ssize_t len;
292 char *val;
293
294 USE_ARG(httpd);
295 debug((httpd, DEBUG_EXPLODING, "parse in: %s", in));
296 *method = *file = *query = *proto = NULL;
297
298 len = (ssize_t)strlen(in);
299 val = bozostrnsep(&in, " \t\n\r", &len);
300 if (len < 1 || val == NULL)
301 return;
302 *method = val;
303
304 while (*in == ' ' || *in == '\t')
305 in++;
306 val = bozostrnsep(&in, " \t\n\r", &len);
307 if (len < 1) {
308 if (len == 0)
309 *file = val;
310 else
311 *file = in;
312 } else {
313 *file = val;
314
315 *query = strchr(*file, '?');
316 if (*query)
317 *(*query)++ = '\0';
318
319 if (in) {
320 while (*in && (*in == ' ' || *in == '\t'))
321 in++;
322 if (*in)
323 *proto = in;
324 }
325 }
326
327 /* allocate private copies */
328 *file = bozostrdup(httpd, NULL, *file);
329 if (*query)
330 *query = bozostrdup(httpd, NULL, *query);
331
332 debug((httpd, DEBUG_FAT,
333 "url: method: \"%s\" file: \"%s\" query: \"%s\" proto: \"%s\"",
334 *method, *file, *query, *proto));
335 }
336
337 /*
338 * cleanup a bozo_httpreq_t after use
339 */
340 void
341 bozo_clean_request(bozo_httpreq_t *request)
342 {
343 struct bozoheaders *hdr, *ohdr = NULL;
344
345 if (request == NULL)
346 return;
347
348 /* If SSL enabled cleanup SSL structure. */
349 bozo_ssl_destroy(request->hr_httpd);
350
351 /* clean up request */
352 free(request->hr_remotehost);
353 free(request->hr_remoteaddr);
354 free(request->hr_serverport);
355 free(request->hr_virthostname);
356 free(request->hr_file);
357 free(request->hr_oldfile);
358 free(request->hr_query);
359 free(request->hr_host);
360 bozo_user_free(request->hr_user);
361 bozo_auth_cleanup(request);
362 for (hdr = SIMPLEQ_FIRST(&request->hr_headers); hdr;
363 hdr = SIMPLEQ_NEXT(hdr, h_next)) {
364 free(hdr->h_value);
365 free(hdr->h_header);
366 free(ohdr);
367 ohdr = hdr;
368 }
369 free(ohdr);
370 ohdr = NULL;
371 for (hdr = SIMPLEQ_FIRST(&request->hr_replheaders); hdr;
372 hdr = SIMPLEQ_NEXT(hdr, h_next)) {
373 free(hdr->h_value);
374 free(hdr->h_header);
375 free(ohdr);
376 ohdr = hdr;
377 }
378 free(ohdr);
379
380 free(request);
381 }
382
383 /*
384 * send a HTTP/1.1 408 response if we timeout.
385 */
386 /* ARGSUSED */
387 static void
388 alarmer(int sig)
389 {
390 timeout_hit = 1;
391 }
392
393
394 /*
395 * set a timeout for "initial", "header", or "request".
396 */
397 int
398 bozo_set_timeout(bozohttpd_t *httpd, bozoprefs_t *prefs,
399 const char *target, const char *val)
400 {
401 const char **cur, *timeouts[] = {
402 "initial timeout",
403 "header timeout",
404 "request timeout",
405 NULL,
406 };
407 /* adjust minlen if more timeouts appear with conflicting names */
408 const size_t minlen = 1;
409 size_t len = strlen(target);
410
411 for (cur = timeouts; len >= minlen && *cur; cur++) {
412 if (strncmp(target, *cur, len) == 0) {
413 bozo_set_pref(httpd, prefs, *cur, val);
414 return 0;
415 }
416 }
417 return 1;
418 }
419
420 /*
421 * a list of header quirks: currently, a list of headers that
422 * can't be folded into a single line.
423 */
424 const char *header_quirks[] = { "WWW-Authenticate", NULL };
425
426 /*
427 * add or merge this header (val: str) into the requests list
428 */
429 static bozoheaders_t *
430 addmerge_header(bozo_httpreq_t *request, struct qheaders *headers,
431 const char *val, const char *str, ssize_t len)
432 {
433 struct bozohttpd_t *httpd = request->hr_httpd;
434 struct bozoheaders *hdr = NULL;
435 const char **quirk;
436
437 USE_ARG(len);
438 for (quirk = header_quirks; *quirk; quirk++)
439 if (strcasecmp(*quirk, val) == 0)
440 break;
441
442 if (*quirk == NULL) {
443 /* do we exist already? */
444 SIMPLEQ_FOREACH(hdr, headers, h_next) {
445 if (strcasecmp(val, hdr->h_header) == 0)
446 break;
447 }
448 }
449
450 if (hdr) {
451 /* yup, merge it in */
452 char *nval;
453
454 bozoasprintf(httpd, &nval, "%s, %s", hdr->h_value, str);
455 free(hdr->h_value);
456 hdr->h_value = nval;
457 } else {
458 /* nope, create a new one */
459
460 hdr = bozomalloc(httpd, sizeof *hdr);
461 hdr->h_header = bozostrdup(httpd, request, val);
462 if (str && *str)
463 hdr->h_value = bozostrdup(httpd, request, str);
464 else
465 hdr->h_value = bozostrdup(httpd, request, " ");
466
467 SIMPLEQ_INSERT_TAIL(headers, hdr, h_next);
468 request->hr_nheaders++;
469 }
470
471 return hdr;
472 }
473
474 bozoheaders_t *
475 addmerge_reqheader(bozo_httpreq_t *request, const char *val, const char *str,
476 ssize_t len)
477 {
478
479 return addmerge_header(request, &request->hr_headers, val, str, len);
480 }
481
482 bozoheaders_t *
483 addmerge_replheader(bozo_httpreq_t *request, const char *val, const char *str,
484 ssize_t len)
485 {
486
487 return addmerge_header(request, &request->hr_replheaders,
488 val, str, len);
489 }
490
491 /*
492 * as the prototype string is not constant (eg, "HTTP/1.1" is equivalent
493 * to "HTTP/001.01"), we MUST parse this.
494 */
495 static int
496 process_proto(bozo_httpreq_t *request, const char *proto)
497 {
498 struct bozohttpd_t *httpd = request->hr_httpd;
499 char majorstr[16], *minorstr;
500 int majorint, minorint;
501
502 if (proto == NULL) {
503 got_proto_09:
504 request->hr_proto = httpd->consts.http_09;
505 debug((httpd, DEBUG_FAT, "request %s is http/0.9",
506 request->hr_file));
507 return 0;
508 }
509
510 if (strncasecmp(proto, "HTTP/", 5) != 0)
511 goto bad;
512 strncpy(majorstr, proto + 5, sizeof majorstr);
513 majorstr[sizeof(majorstr)-1] = 0;
514 minorstr = strchr(majorstr, '.');
515 if (minorstr == NULL)
516 goto bad;
517 *minorstr++ = 0;
518
519 majorint = atoi(majorstr);
520 minorint = atoi(minorstr);
521
522 switch (majorint) {
523 case 0:
524 if (minorint != 9)
525 break;
526 goto got_proto_09;
527 case 1:
528 if (minorint == 0)
529 request->hr_proto = httpd->consts.http_10;
530 else if (minorint == 1)
531 request->hr_proto = httpd->consts.http_11;
532 else
533 break;
534
535 debug((httpd, DEBUG_FAT, "request %s is %s",
536 request->hr_file, request->hr_proto));
537 SIMPLEQ_INIT(&request->hr_headers);
538 request->hr_nheaders = 0;
539 return 0;
540 }
541 bad:
542 return bozo_http_error(httpd, 404, NULL, "unknown prototype");
543 }
544
545 /*
546 * process each type of HTTP method, setting this HTTP requests
547 * method type.
548 */
549 static struct method_map {
550 const char *name;
551 int type;
552 } method_map[] = {
553 { "GET", HTTP_GET, },
554 { "POST", HTTP_POST, },
555 { "HEAD", HTTP_HEAD, },
556 #if 0 /* other non-required http/1.1 methods */
557 { "OPTIONS", HTTP_OPTIONS, },
558 { "PUT", HTTP_PUT, },
559 { "DELETE", HTTP_DELETE, },
560 { "TRACE", HTTP_TRACE, },
561 { "CONNECT", HTTP_CONNECT, },
562 #endif
563 { NULL, 0, },
564 };
565
566 static int
567 process_method(bozo_httpreq_t *request, const char *method)
568 {
569 struct bozohttpd_t *httpd = request->hr_httpd;
570 struct method_map *mmp;
571
572 if (request->hr_proto == httpd->consts.http_11)
573 request->hr_allow = "GET, HEAD, POST";
574
575 for (mmp = method_map; mmp->name; mmp++)
576 if (strcasecmp(method, mmp->name) == 0) {
577 request->hr_method = mmp->type;
578 request->hr_methodstr = mmp->name;
579 return 0;
580 }
581
582 return bozo_http_error(httpd, 404, request, "unknown method");
583 }
584
585 /* check header byte count */
586 static int
587 bozo_got_header_length(bozo_httpreq_t *request, size_t len)
588 {
589
590 if (len > BOZO_HEADERS_MAX_SIZE - request->hr_header_bytes)
591 return bozo_http_error(request->hr_httpd, 413, request,
592 "too many headers");
593
594 request->hr_header_bytes += len;
595
596 return 0;
597 }
598
599 /*
600 * This function reads a http request from stdin, returning a pointer to a
601 * bozo_httpreq_t structure, describing the request.
602 */
603 bozo_httpreq_t *
604 bozo_read_request(bozohttpd_t *httpd)
605 {
606 struct sigaction sa;
607 char *str, *val, *method, *file, *proto, *query;
608 char *host, *addr, *port;
609 char bufport[10];
610 char hbuf[NI_MAXHOST], abuf[NI_MAXHOST];
611 struct sockaddr_storage ss;
612 ssize_t len;
613 int line = 0;
614 socklen_t slen;
615 bozo_httpreq_t *request;
616 struct timespec ots, ts;
617
618 /*
619 * if we're in daemon mode, bozo_daemon_fork() will return here twice
620 * for each call. once in the child, returning 0, and once in the
621 * parent, returning 1. for each child, then we can setup SSL, and
622 * the parent can signal the caller there was no request to process
623 * and it will wait for another.
624 */
625 if (bozo_daemon_fork(httpd))
626 return NULL;
627 if (bozo_ssl_accept(httpd))
628 return NULL;
629
630 request = bozomalloc(httpd, sizeof(*request));
631 memset(request, 0, sizeof(*request));
632 request->hr_httpd = httpd;
633 request->hr_allow = request->hr_host = NULL;
634 request->hr_content_type = request->hr_content_length = NULL;
635 request->hr_range = NULL;
636 request->hr_last_byte_pos = -1;
637 request->hr_if_modified_since = NULL;
638 request->hr_virthostname = NULL;
639 request->hr_file = NULL;
640 request->hr_oldfile = NULL;
641 SIMPLEQ_INIT(&request->hr_replheaders);
642 bozo_auth_init(request);
643
644 slen = sizeof(ss);
645 if (getpeername(0, (struct sockaddr *)(void *)&ss, &slen) < 0)
646 host = addr = NULL;
647 else {
648 if (getnameinfo((struct sockaddr *)(void *)&ss, slen,
649 abuf, sizeof abuf, NULL, 0, NI_NUMERICHOST) == 0)
650 addr = abuf;
651 else
652 addr = NULL;
653 if (httpd->numeric == 0 &&
654 getnameinfo((struct sockaddr *)(void *)&ss, slen,
655 hbuf, sizeof hbuf, NULL, 0, 0) == 0)
656 host = hbuf;
657 else
658 host = NULL;
659 }
660 if (host != NULL)
661 request->hr_remotehost = bozostrdup(httpd, request, host);
662 if (addr != NULL)
663 request->hr_remoteaddr = bozostrdup(httpd, request, addr);
664 slen = sizeof(ss);
665
666 /*
667 * Override the bound port from the request value, so it works even
668 * if passed through a proxy that doesn't rewrite the port.
669 */
670 if (httpd->bindport) {
671 if (strcmp(httpd->bindport, "80") != 0)
672 port = httpd->bindport;
673 else
674 port = NULL;
675 } else {
676 if (getsockname(0, (struct sockaddr *)(void *)&ss, &slen) < 0)
677 port = NULL;
678 else {
679 if (getnameinfo((struct sockaddr *)(void *)&ss, slen,
680 NULL, 0, bufport, sizeof bufport,
681 NI_NUMERICSERV) == 0)
682 port = bufport;
683 else
684 port = NULL;
685 }
686 }
687 if (port != NULL)
688 request->hr_serverport = bozostrdup(httpd, request, port);
689
690 /*
691 * setup a timer to make sure the request is not hung
692 */
693 sa.sa_handler = alarmer;
694 sigemptyset(&sa.sa_mask);
695 sigaddset(&sa.sa_mask, SIGALRM);
696 sa.sa_flags = 0;
697 sigaction(SIGALRM, &sa, NULL);
698
699 if (clock_gettime(CLOCK_MONOTONIC, &ots) != 0) {
700 bozo_http_error(httpd, 500, NULL, "clock_gettime failed");
701 goto cleanup;
702 }
703
704 alarm(httpd->initial_timeout);
705 while ((str = bozodgetln(httpd, STDIN_FILENO, &len, bozo_read)) != NULL) {
706 alarm(0);
707
708 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
709 bozo_http_error(httpd, 500, NULL, "clock_gettime failed");
710 goto cleanup;
711 }
712 /*
713 * don't timeout if old tv_sec is not more than current
714 * tv_sec, or if current tv_sec is less than the request
715 * timeout (these shouldn't happen, but the first could
716 * if monotonic time is not available.)
717 *
718 * the other timeout and header size checks should ensure
719 * that even if time it set backwards or forwards a very
720 * long way, timeout will eventually happen, even if this
721 * one fails.
722 */
723 if (ts.tv_sec > ots.tv_sec &&
724 ts.tv_sec > httpd->request_timeout &&
725 ts.tv_sec - httpd->request_timeout > ots.tv_sec)
726 timeout_hit = 1;
727
728 if (timeout_hit) {
729 bozo_http_error(httpd, 408, NULL, "request timed out");
730 goto cleanup;
731 }
732 line++;
733
734 if (line == 1) {
735 if (len < 1) {
736 bozo_http_error(httpd, 404, NULL, "null method");
737 goto cleanup;
738 }
739 bozowarn(httpd,
740 "got request ``%s'' from host %s to port %s",
741 str,
742 host ? host : addr ? addr : "<local>",
743 port ? port : "<stdin>");
744
745 /* we allocate return space in file and query only */
746 parse_request(httpd, str, &method, &file, &query, &proto);
747 request->hr_file = file;
748 request->hr_query = query;
749 if (method == NULL) {
750 bozo_http_error(httpd, 404, NULL, "null method");
751 goto cleanup;
752 }
753 if (file == NULL) {
754 bozo_http_error(httpd, 404, NULL, "null file");
755 goto cleanup;
756 }
757
758 /*
759 * note that we parse the proto first, so that we
760 * can more properly parse the method and the url.
761 */
762
763 if (process_proto(request, proto) ||
764 process_method(request, method)) {
765 goto cleanup;
766 }
767
768 debug((httpd, DEBUG_FAT, "got file \"%s\" query \"%s\"",
769 request->hr_file,
770 request->hr_query ? request->hr_query : "<none>"));
771
772 /* http/0.9 has no header processing */
773 if (request->hr_proto == httpd->consts.http_09)
774 break;
775 } else { /* incoming headers */
776 bozoheaders_t *hdr;
777
778 if (*str == '\0')
779 break;
780
781 val = bozostrnsep(&str, ":", &len);
782 debug((httpd, DEBUG_EXPLODING, "read_req2: after "
783 "bozostrnsep: str `%s' val `%s'", str, val));
784 if (val == NULL || len == -1) {
785 bozo_http_error(httpd, 404, request, "no header");
786 goto cleanup;
787 }
788 while (*str == ' ' || *str == '\t')
789 len--, str++;
790 while (*val == ' ' || *val == '\t')
791 val++;
792
793 if (bozo_got_header_length(request, len))
794 goto cleanup;
795
796 if (bozo_auth_check_headers(request, val, str, len))
797 goto next_header;
798
799 hdr = addmerge_reqheader(request, val, str, len);
800
801 if (strcasecmp(hdr->h_header, "content-type") == 0)
802 request->hr_content_type = hdr->h_value;
803 else if (strcasecmp(hdr->h_header, "content-length") == 0)
804 request->hr_content_length = hdr->h_value;
805 else if (strcasecmp(hdr->h_header, "host") == 0) {
806 if (request->hr_host) {
807 /* RFC 7230 (HTTP/1.1): 5.4 */
808 bozo_http_error(httpd, 400, request,
809 "Only allow one Host: header");
810 goto cleanup;
811 }
812 request->hr_host = bozostrdup(httpd, request,
813 hdr->h_value);
814 }
815 /* RFC 2616 (HTTP/1.1): 14.20 */
816 else if (strcasecmp(hdr->h_header, "expect") == 0) {
817 bozo_http_error(httpd, 417, request,
818 "we don't support Expect:");
819 goto cleanup;
820 }
821 else if (strcasecmp(hdr->h_header, "referrer") == 0 ||
822 strcasecmp(hdr->h_header, "referer") == 0)
823 request->hr_referrer = hdr->h_value;
824 else if (strcasecmp(hdr->h_header, "range") == 0)
825 request->hr_range = hdr->h_value;
826 else if (strcasecmp(hdr->h_header,
827 "if-modified-since") == 0)
828 request->hr_if_modified_since = hdr->h_value;
829 else if (strcasecmp(hdr->h_header,
830 "accept-encoding") == 0)
831 request->hr_accept_encoding = hdr->h_value;
832
833 debug((httpd, DEBUG_FAT, "adding header %s: %s",
834 hdr->h_header, hdr->h_value));
835 }
836 next_header:
837 alarm(httpd->header_timeout);
838 }
839
840 /* now, clear it all out */
841 alarm(0);
842 signal(SIGALRM, SIG_DFL);
843
844 /* RFC1945, 8.3 */
845 if (request->hr_method == HTTP_POST &&
846 request->hr_content_length == NULL) {
847 bozo_http_error(httpd, 400, request, "missing content length");
848 goto cleanup;
849 }
850
851 /* RFC 2616 (HTTP/1.1), 14.23 & 19.6.1.1 */
852 if (request->hr_proto == httpd->consts.http_11 &&
853 /*(strncasecmp(request->hr_file, "http://", 7) != 0) &&*/
854 request->hr_host == NULL) {
855 bozo_http_error(httpd, 400, request, "missing Host header");
856 goto cleanup;
857 }
858
859 if (request->hr_range != NULL) {
860 debug((httpd, DEBUG_FAT, "hr_range: %s", request->hr_range));
861 /* support only simple ranges %d- and %d-%d */
862 if (strchr(request->hr_range, ',') == NULL) {
863 const char *rstart, *dash;
864
865 rstart = strchr(request->hr_range, '=');
866 if (rstart != NULL) {
867 rstart++;
868 dash = strchr(rstart, '-');
869 if (dash != NULL && dash != rstart) {
870 dash++;
871 request->hr_have_range = 1;
872 request->hr_first_byte_pos =
873 strtoll(rstart, NULL, 10);
874 if (request->hr_first_byte_pos < 0)
875 request->hr_first_byte_pos = 0;
876 if (*dash != '\0') {
877 request->hr_last_byte_pos =
878 strtoll(dash, NULL, 10);
879 if (request->hr_last_byte_pos < 0)
880 request->hr_last_byte_pos = -1;
881 }
882 }
883 }
884 }
885 }
886
887 debug((httpd, DEBUG_FAT, "bozo_read_request returns url %s in request",
888 request->hr_file));
889 return request;
890
891 cleanup:
892 bozo_clean_request(request);
893
894 return NULL;
895 }
896
897 static int
898 mmap_and_write_part(bozohttpd_t *httpd, int fd, off_t first_byte_pos, size_t sz)
899 {
900 size_t mappedsz, wroffset;
901 off_t mappedoffset;
902 char *addr;
903 void *mappedaddr;
904
905 /*
906 * we need to ensure that both the size *and* offset arguments to
907 * mmap() are page-aligned. our formala for this is:
908 *
909 * input offset: first_byte_pos
910 * input size: sz
911 *
912 * mapped offset = page align truncate (input offset)
913 * mapped size =
914 * page align extend (input offset - mapped offset + input size)
915 * write offset = input offset - mapped offset
916 *
917 * we use the write offset in all writes
918 */
919 mappedoffset = first_byte_pos & ~(httpd->page_size - 1);
920 mappedsz = (size_t)
921 (first_byte_pos - mappedoffset + sz + httpd->page_size - 1) &
922 ~(httpd->page_size - 1);
923 wroffset = (size_t)(first_byte_pos - mappedoffset);
924
925 addr = mmap(0, mappedsz, PROT_READ, MAP_SHARED, fd, mappedoffset);
926 if (addr == (char *)-1) {
927 bozowarn(httpd, "mmap failed: %s", strerror(errno));
928 return -1;
929 }
930 mappedaddr = addr;
931
932 #ifdef MADV_SEQUENTIAL
933 (void)madvise(addr, sz, MADV_SEQUENTIAL);
934 #endif
935 while (sz > BOZO_WRSZ) {
936 if (bozo_write(httpd, STDOUT_FILENO, addr + wroffset,
937 BOZO_WRSZ) != BOZO_WRSZ) {
938 bozowarn(httpd, "write failed: %s", strerror(errno));
939 goto out;
940 }
941 debug((httpd, DEBUG_OBESE, "wrote %d bytes", BOZO_WRSZ));
942 sz -= BOZO_WRSZ;
943 addr += BOZO_WRSZ;
944 }
945 if (sz && (size_t)bozo_write(httpd, STDOUT_FILENO, addr + wroffset,
946 sz) != sz) {
947 bozowarn(httpd, "final write failed: %s", strerror(errno));
948 goto out;
949 }
950 debug((httpd, DEBUG_OBESE, "wrote %d bytes", (int)sz));
951 out:
952 if (munmap(mappedaddr, mappedsz) < 0) {
953 bozowarn(httpd, "munmap failed");
954 return -1;
955 }
956
957 return 0;
958 }
959
960 static int
961 parse_http_date(const char *val, time_t *timestamp)
962 {
963 char *remainder;
964 struct tm tm;
965
966 if ((remainder = strptime(val, "%a, %d %b %Y %T GMT", &tm)) == NULL &&
967 (remainder = strptime(val, "%a, %d-%b-%y %T GMT", &tm)) == NULL &&
968 (remainder = strptime(val, "%a %b %d %T %Y", &tm)) == NULL)
969 return 0; /* Invalid HTTP date format */
970
971 if (*remainder)
972 return 0; /* No trailing garbage */
973
974 *timestamp = timegm(&tm);
975 return 1;
976 }
977
978 /*
979 * given an url, encode it ala rfc 3986. ie, escape ? and friends.
980 * note that this function returns a static buffer, and thus needs
981 * to be updated for any sort of parallel processing. escape only
982 * chosen characters for absolute redirects
983 */
984 char *
985 bozo_escape_rfc3986(bozohttpd_t *httpd, const char *url, int absolute)
986 {
987 static char *buf;
988 static size_t buflen = 0;
989 size_t len;
990 const char *s;
991 char *d;
992
993 len = strlen(url);
994 if (buflen < len * 3 + 1) {
995 buflen = len * 3 + 1;
996 buf = bozorealloc(httpd, buf, buflen);
997 }
998
999 for (len = 0, s = url, d = buf; *s;) {
1000 if (*s & 0x80)
1001 goto encode_it;
1002 switch (*s) {
1003 case ':':
1004 case '?':
1005 case '#':
1006 case '[':
1007 case ']':
1008 case '@':
1009 case '!':
1010 case '$':
1011 case '&':
1012 case '\'':
1013 case '(':
1014 case ')':
1015 case '*':
1016 case '+':
1017 case ',':
1018 case ';':
1019 case '=':
1020 case '%':
1021 case '"':
1022 if (absolute)
1023 goto leave_it;
1024 /*FALLTHROUGH*/
1025 case '\n':
1026 case '\r':
1027 case ' ':
1028 encode_it:
1029 snprintf(d, 4, "%%%02X", (unsigned char)*s++);
1030 d += 3;
1031 len += 3;
1032 break;
1033 default:
1034 leave_it:
1035 *d++ = *s++;
1036 len++;
1037 break;
1038 }
1039 }
1040 buf[len] = 0;
1041
1042 return buf;
1043 }
1044
1045 /*
1046 * do automatic redirection -- if there are query parameters or userdir for
1047 * the URL we will tack these on to the new (redirected) URL.
1048 */
1049 static void
1050 handle_redirect(bozo_httpreq_t *request, const char *url, int absolute)
1051 {
1052 bozohttpd_t *httpd = request->hr_httpd;
1053 char *finalurl, *urlbuf;
1054 #ifndef NO_USER_SUPPORT
1055 char *userbuf;
1056 #endif /* !NO_USER_SUPPORT */
1057 char portbuf[20];
1058 const char *scheme, *query, *quest;
1059 const char *hostname = BOZOHOST(httpd, request);
1060 int absproto = 0; /* absolute redirect provides own schema */
1061
1062 if (url == NULL) {
1063 bozoasprintf(httpd, &urlbuf, "/%s/", request->hr_file);
1064 url = urlbuf;
1065 } else
1066 urlbuf = NULL;
1067
1068 #ifndef NO_USER_SUPPORT
1069 if (request->hr_user && !absolute) {
1070 bozoasprintf(httpd, &userbuf, "/~%s%s", request->hr_user, url);
1071 url = userbuf;
1072 } else
1073 userbuf = NULL;
1074 #endif /* !NO_USER_SUPPORT */
1075
1076 if (absolute) {
1077 char *sep = NULL;
1078 const char *s;
1079
1080 /*
1081 * absolute redirect may specify own protocol i.e. to redirect
1082 * to another schema like https:// or ftp://.
1083 * Details: RFC 3986, section 3.
1084 */
1085
1086 /* 1. check if url contains :// */
1087 sep = strstr(url, "://");
1088
1089 /*
1090 * RFC 3986, section 3.1:
1091 * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
1092 */
1093 if (sep) {
1094 for (s = url; s != sep;) {
1095 if (!isalnum((int)*s) &&
1096 *s != '+' && *s != '-' && *s != '.')
1097 break;
1098 if (++s == sep) {
1099 absproto = 1;
1100 }
1101 }
1102 }
1103 }
1104
1105 /* construct final redirection url */
1106
1107 scheme = absproto ? "" : httpd->sslinfo ? "https://" : "http://";
1108
1109 if (absolute) {
1110 hostname = "";
1111 portbuf[0] = '\0';
1112 } else {
1113 const char *defport = httpd->sslinfo ? "443" : "80";
1114
1115 if (request->hr_serverport &&
1116 strcmp(request->hr_serverport, defport) != 0)
1117 snprintf(portbuf, sizeof(portbuf), ":%s",
1118 request->hr_serverport);
1119 else
1120 portbuf[0] = '\0';
1121 }
1122
1123 url = bozo_escape_rfc3986(httpd, url, absolute);
1124
1125 if (request->hr_query && strlen(request->hr_query)) {
1126 query = request->hr_query;
1127 quest = "?";
1128 } else {
1129 query = quest = "";
1130 }
1131
1132 bozoasprintf(httpd, &finalurl, "%s%s%s%s%s%s",
1133 scheme, hostname, portbuf, url, quest, query);
1134
1135 bozowarn(httpd, "redirecting %s", finalurl);
1136 debug((httpd, DEBUG_FAT, "redirecting %s", finalurl));
1137
1138 bozo_printf(httpd, "%s 301 Document Moved\r\n", request->hr_proto);
1139 if (request->hr_proto != httpd->consts.http_09)
1140 bozo_print_header(request, NULL, "text/html", NULL);
1141 if (request->hr_proto != httpd->consts.http_09)
1142 bozo_printf(httpd, "Location: %s\r\n", finalurl);
1143 bozo_printf(httpd, "\r\n");
1144 if (request->hr_method == HTTP_HEAD)
1145 goto head;
1146 bozo_printf(httpd, "<html><head><title>Document Moved</title></head>\n");
1147 bozo_printf(httpd, "<body><h1>Document Moved</h1>\n");
1148 bozo_printf(httpd, "This document had moved <a href=\"%s\">here</a>\n",
1149 finalurl);
1150 bozo_printf(httpd, "</body></html>\n");
1151 head:
1152 bozo_flush(httpd, stdout);
1153 free(urlbuf);
1154 free(finalurl);
1155 #ifndef NO_USER_SUPPORT
1156 free(userbuf);
1157 #endif /* !NO_USER_SUPPORT */
1158 }
1159
1160 /*
1161 * Like strncmp(), but s_esc may contain characters escaped by \.
1162 * The len argument does not include the backslashes used for escaping,
1163 * that is: it gives the raw len, after unescaping the string.
1164 */
1165 static int
1166 esccmp(const char *s_plain, const char *s_esc, size_t len)
1167 {
1168 bool esc = false;
1169
1170 while (len) {
1171 if (!esc && *s_esc == '\\') {
1172 esc = true;
1173 s_esc++;
1174 continue;
1175 }
1176 esc = false;
1177 if (*s_plain == 0 || *s_esc == 0 || *s_plain != *s_esc)
1178 return *s_esc - *s_plain;
1179 s_esc++;
1180 s_plain++;
1181 len--;
1182 }
1183 return 0;
1184 }
1185
1186 /*
1187 * Check if the request refers to a uri that is mapped via a .bzremap.
1188 * We have /requested/path:/re/mapped/to/this.html lines in there,
1189 * and the : separator may be use in the left hand side escaped with
1190 * \ to encode a path containig a : character.
1191 */
1192 static void
1193 check_remap(bozo_httpreq_t *request)
1194 {
1195 bozohttpd_t *httpd = request->hr_httpd;
1196 char *file = request->hr_file, *newfile;
1197 void *fmap;
1198 const char *replace, *map_to, *p;
1199 struct stat st;
1200 int mapfile;
1201 size_t avail, len, rlen, reqlen, num_esc = 0;
1202 bool escaped = false;
1203
1204 mapfile = open(REMAP_FILE, O_RDONLY, 0);
1205 if (mapfile == -1)
1206 return;
1207 debug((httpd, DEBUG_FAT, "remap file found"));
1208 if (fstat(mapfile, &st) == -1) {
1209 bozowarn(httpd, "could not stat " REMAP_FILE ", errno: %d",
1210 errno);
1211 goto out;
1212 }
1213
1214 fmap = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, mapfile, 0);
1215 if (fmap == NULL) {
1216 bozowarn(httpd, "could not mmap " REMAP_FILE ", error %d",
1217 errno);
1218 goto out;
1219 }
1220 reqlen = strlen(file);
1221 for (p = fmap, avail = st.st_size; avail; ) {
1222 /*
1223 * We have lines like:
1224 * /this/url:/replacement/that/url
1225 * If we find a matching left hand side, replace will point
1226 * to it and len will be its length. map_to will point to
1227 * the right hand side and rlen wil be its length.
1228 * If we have no match, both pointers will be NULL.
1229 */
1230
1231 /* skip empty lines */
1232 while ((*p == '\r' || *p == '\n') && avail) {
1233 p++;
1234 avail--;
1235 }
1236 replace = p;
1237 escaped = false;
1238 while (avail) {
1239 if (*p == '\r' || *p == '\n')
1240 break;
1241 if (!escaped && *p == ':')
1242 break;
1243 if (escaped) {
1244 escaped = false;
1245 num_esc++;
1246 } else if (*p == '\\') {
1247 escaped = true;
1248 }
1249 p++;
1250 avail--;
1251 }
1252 if (!avail || *p != ':') {
1253 replace = NULL;
1254 map_to = NULL;
1255 break;
1256 }
1257 len = p - replace - num_esc;
1258 /*
1259 * reqlen < len: the left hand side is too long, can't be a
1260 * match
1261 * reqlen == len: full string has to match
1262 * reqlen > len: make sure there is a path separator at 'len'
1263 * avail < 2: we are at eof, missing right hand side
1264 */
1265 if (avail < 2 || reqlen < len ||
1266 (reqlen == len && esccmp(file, replace, len) != 0) ||
1267 (reqlen > len && (file[len] != '/' ||
1268 esccmp(file, replace, len) != 0))) {
1269
1270 /* non-match, skip to end of line and continue */
1271 while (*p != '\r' && *p != '\n' && avail) {
1272 p++;
1273 avail--;
1274 }
1275 replace = NULL;
1276 map_to = NULL;
1277 continue;
1278 }
1279 p++;
1280 avail--;
1281
1282 /* found a match, parse the target */
1283 map_to = p;
1284 while (*p != '\r' && *p != '\n' && avail) {
1285 p++;
1286 avail--;
1287 }
1288 rlen = p - map_to;
1289 break;
1290 }
1291
1292 if (replace && map_to) {
1293 newfile = bozomalloc(httpd, strlen(file) + rlen - len + 1);
1294 memcpy(newfile, map_to, rlen);
1295 strcpy(newfile+rlen, file + len);
1296 debug((httpd, DEBUG_NORMAL, "remapping found '%s'",
1297 newfile));
1298 free(request->hr_file);
1299 request->hr_file = newfile;
1300 }
1301
1302 munmap(fmap, st.st_size);
1303 out:
1304 close(mapfile);
1305 }
1306
1307 /*
1308 * deal with virtual host names; we do this:
1309 * if we have a virtual path root (httpd->virtbase), and we are given a
1310 * virtual host spec (Host: ho.st or http://ho.st/), see if this
1311 * directory exists under httpd->virtbase. if it does, use this as the
1312 # new slashdir.
1313 */
1314 static int
1315 check_virtual(bozo_httpreq_t *request)
1316 {
1317 bozohttpd_t *httpd = request->hr_httpd;
1318 char *file = request->hr_file, *s;
1319 size_t len;
1320
1321 /*
1322 * convert http://virtual.host/ to request->hr_host
1323 */
1324 debug((httpd, DEBUG_OBESE,
1325 "checking for http:// virtual host in '%s'", file));
1326 if (strncasecmp(file, "http://", 7) == 0) {
1327 /* we would do virtual hosting here? */
1328 file += 7;
1329 /* RFC 2616 (HTTP/1.1), 5.2: URI takes precedence over Host: */
1330 free(request->hr_host);
1331 request->hr_host = bozostrdup(httpd, request, file);
1332 if ((s = strchr(request->hr_host, '/')) != NULL)
1333 *s = '\0';
1334 s = strchr(file, '/');
1335 free(request->hr_file);
1336 request->hr_file = bozostrdup(httpd, request, s ? s : "/");
1337 debug((httpd, DEBUG_OBESE, "got host '%s' file is now '%s'",
1338 request->hr_host, request->hr_file));
1339 } else if (!request->hr_host)
1340 goto use_slashdir;
1341
1342 /*
1343 * canonicalise hr_host - that is, remove any :80.
1344 */
1345 len = strlen(request->hr_host);
1346 if (len > 3 && strcmp(request->hr_host + len - 3, ":80") == 0) {
1347 request->hr_host[len - 3] = '\0';
1348 len = strlen(request->hr_host);
1349 }
1350
1351 if (!httpd->virtbase) {
1352 /*
1353 * if we don't use vhost support, then set virthostname if
1354 * user supplied Host header. It will be used for possible
1355 * redirections
1356 */
1357 if (request->hr_host) {
1358 s = strrchr(request->hr_host, ':');
1359 if (s != NULL)
1360 /* truncate Host: as we want to copy it without port part */
1361 *s = '\0';
1362 request->hr_virthostname = bozostrdup(httpd, request,
1363 request->hr_host);
1364 if (s != NULL)
1365 /* fix Host: again, if we truncated it */
1366 *s = ':';
1367 }
1368 goto use_slashdir;
1369 }
1370
1371 /*
1372 * ok, we have a virtual host, use opendir(3) to find a case
1373 * insensitive match for the virtual host we are asked for.
1374 * note that if the virtual host is the same as the master,
1375 * we don't need to do anything special.
1376 */
1377 debug((httpd, DEBUG_OBESE,
1378 "check_virtual: checking host `%s' under httpd->virtbase `%s' "
1379 "for file `%s'",
1380 request->hr_host, httpd->virtbase, request->hr_file));
1381 if (strncasecmp(httpd->virthostname, request->hr_host, len) != 0) {
1382 s = NULL;
1383 DIR *dirp;
1384 struct dirent *d;
1385
1386 if ((dirp = opendir(httpd->virtbase)) != NULL) {
1387 while ((d = readdir(dirp)) != NULL) {
1388 if (strcmp(d->d_name, ".") == 0 ||
1389 strcmp(d->d_name, "..") == 0) {
1390 continue;
1391 }
1392 debug((httpd, DEBUG_OBESE, "looking at dir '%s'",
1393 d->d_name));
1394 if (strcmp(d->d_name, request->hr_host) == 0) {
1395 /* found it, punch it */
1396 debug((httpd, DEBUG_OBESE, "found it punch it"));
1397 request->hr_virthostname =
1398 bozostrdup(httpd, request, d->d_name);
1399 bozoasprintf(httpd, &s, "%s/%s",
1400 httpd->virtbase,
1401 request->hr_virthostname);
1402 break;
1403 }
1404 }
1405 closedir(dirp);
1406 }
1407 else {
1408 debug((httpd, DEBUG_FAT, "opendir %s failed: %s",
1409 httpd->virtbase, strerror(errno)));
1410 }
1411 if (s == 0) {
1412 if (httpd->unknown_slash)
1413 goto use_slashdir;
1414 return bozo_http_error(httpd, 404, request,
1415 "unknown URL");
1416 }
1417 } else
1418 use_slashdir:
1419 s = httpd->slashdir;
1420
1421 /*
1422 * ok, nailed the correct slashdir, chdir to it
1423 */
1424 if (chdir(s) < 0)
1425 return bozo_http_error(httpd, 404, request,
1426 "can't chdir to slashdir");
1427
1428 /*
1429 * is there a mapping for this request?
1430 */
1431 check_remap(request);
1432
1433 return 0;
1434 }
1435
1436 /*
1437 * checks to see if this request has a valid .bzredirect file. returns
1438 * 0 when no redirection happend, or 1 when handle_redirect() has been
1439 * called, -1 on error.
1440 */
1441 static int
1442 check_bzredirect(bozo_httpreq_t *request)
1443 {
1444 bozohttpd_t *httpd = request->hr_httpd;
1445 struct stat sb;
1446 char dir[MAXPATHLEN], redir[MAXPATHLEN], redirpath[MAXPATHLEN + 1],
1447 path[MAXPATHLEN + 1];
1448 char *basename, *finalredir;
1449 int rv, absolute;
1450
1451 /*
1452 * if this pathname is really a directory, but doesn't end in /,
1453 * use it as the directory to look for the redir file.
1454 */
1455 if ((size_t)snprintf(dir, sizeof(dir), "%s", request->hr_file + 1) >=
1456 sizeof(dir)) {
1457 bozo_http_error(httpd, 404, request, "file path too long");
1458 return -1;
1459 }
1460 debug((httpd, DEBUG_FAT, "check_bzredirect: dir %s", dir));
1461 basename = strrchr(dir, '/');
1462
1463 if ((!basename || basename[1] != '\0') &&
1464 lstat(dir, &sb) == 0 && S_ISDIR(sb.st_mode)) {
1465 strcpy(path, dir);
1466 basename = dir;
1467 } else if (basename == NULL) {
1468 strcpy(path, ".");
1469 strcpy(dir, "");
1470 basename = request->hr_file + 1;
1471 } else {
1472 *basename++ = '\0';
1473 strcpy(path, dir);
1474 }
1475 if (bozo_check_special_files(request, basename))
1476 return -1;
1477
1478 debug((httpd, DEBUG_FAT, "check_bzredirect: path %s", path));
1479
1480 if ((size_t)snprintf(redir, sizeof(redir), "%s/%s", path,
1481 REDIRECT_FILE) >= sizeof(redir)) {
1482 return bozo_http_error(httpd, 404, request,
1483 "redirectfile path too long");
1484 }
1485 if (lstat(redir, &sb) == 0) {
1486 if (!S_ISLNK(sb.st_mode))
1487 return 0;
1488 absolute = 0;
1489 } else {
1490 if ((size_t)snprintf(redir, sizeof(redir), "%s/%s", path,
1491 ABSREDIRECT_FILE) >= sizeof(redir)) {
1492 bozo_http_error(httpd, 404, request,
1493 "redirectfile path too long");
1494 return -1;
1495 }
1496 if (lstat(redir, &sb) < 0 || !S_ISLNK(sb.st_mode))
1497 return 0;
1498 absolute = 1;
1499 }
1500 debug((httpd, DEBUG_FAT, "check_bzredirect: calling readlink"));
1501 rv = readlink(redir, redirpath, sizeof redirpath - 1);
1502 if (rv == -1 || rv == 0) {
1503 debug((httpd, DEBUG_FAT, "readlink failed"));
1504 return 0;
1505 }
1506 redirpath[rv] = '\0';
1507 debug((httpd, DEBUG_FAT, "readlink returned \"%s\"", redirpath));
1508
1509 /* check if we need authentication */
1510 snprintf(path, sizeof(path), "%s/", dir);
1511 if (bozo_auth_check(request, path))
1512 return 1;
1513
1514 /* now we have the link pointer, redirect to the real place */
1515 if (!absolute && redirpath[0] != '/') {
1516 if ((size_t)snprintf(finalredir = redir, sizeof(redir), "%s%s/%s",
1517 (strlen(dir) > 0 ? "/" : ""), dir, redirpath) >= sizeof(redir)) {
1518 bozo_http_error(httpd, 404, request,
1519 "redirect path too long");
1520 return -1;
1521 }
1522 } else
1523 finalredir = redirpath;
1524
1525 debug((httpd, DEBUG_FAT, "check_bzredirect: new redir %s", finalredir));
1526 handle_redirect(request, finalredir, absolute);
1527 return 1;
1528 }
1529
1530 /* this fixes the %HH hack that RFC2396 requires. */
1531 int
1532 bozo_decode_url_percent(bozo_httpreq_t *request, char *str)
1533 {
1534 bozohttpd_t *httpd = request->hr_httpd;
1535 char *s, *t, buf[3];
1536 char *end; /* if end is not-zero, we don't translate beyond that */
1537
1538 end = str + strlen(str);
1539
1540 /* fast forward to the first % */
1541 if ((s = strchr(str, '%')) == NULL)
1542 return 0;
1543
1544 t = s;
1545 do {
1546 if (end && s >= end) {
1547 debug((httpd, DEBUG_EXPLODING,
1548 "fu_%%: past end, filling out.."));
1549 while (*s)
1550 *t++ = *s++;
1551 break;
1552 }
1553 debug((httpd, DEBUG_EXPLODING,
1554 "fu_%%: got s == %%, s[1]s[2] == %c%c",
1555 s[1], s[2]));
1556 if (s[1] == '\0' || s[2] == '\0')
1557 return bozo_http_error(httpd, 400, request,
1558 "percent hack missing two chars afterwards");
1559 if (s[1] == '0' && s[2] == '0')
1560 return bozo_http_error(httpd, 404, request,
1561 "percent hack was %00");
1562 if (s[1] == '2' && s[2] == 'f')
1563 return bozo_http_error(httpd, 404, request,
1564 "percent hack was %2f (/)");
1565
1566 buf[0] = *++s;
1567 buf[1] = *++s;
1568 buf[2] = '\0';
1569 s++;
1570 *t = (char)strtol(buf, NULL, 16);
1571 debug((httpd, DEBUG_EXPLODING,
1572 "fu_%%: strtol put '%02x' into *t", *t));
1573 if (*t++ == '\0')
1574 return bozo_http_error(httpd, 400, request,
1575 "percent hack got a 0 back");
1576
1577 while (*s && *s != '%') {
1578 if (end && s >= end)
1579 break;
1580 *t++ = *s++;
1581 }
1582 } while (*s);
1583 *t = '\0';
1584
1585 debug((httpd, DEBUG_FAT, "bozo_decode_url_percent returns `%s'",
1586 request->hr_file));
1587
1588 return 0;
1589 }
1590
1591 /*
1592 * transform_request does this:
1593 * - ``expand'' %20 crapola
1594 * - punt if it doesn't start with /
1595 * - look for "http://myname/" and deal with it.
1596 * - maybe call bozo_process_cgi()
1597 * - check for ~user and call bozo_user_transform() if so
1598 * - if the length > 1, check for trailing slash. if so,
1599 * add the index.html file
1600 * - if the length is 1, return the index.html file
1601 * - disallow anything ending up with a file starting
1602 * at "/" or having ".." in it.
1603 * - anything else is a really weird internal error
1604 * - returns malloced file to serve, if unhandled
1605 */
1606 static int
1607 transform_request(bozo_httpreq_t *request, int *isindex)
1608 {
1609 bozohttpd_t *httpd = request->hr_httpd;
1610 char *file, *newfile = NULL;
1611 size_t len;
1612
1613 file = NULL;
1614 *isindex = 0;
1615 debug((httpd, DEBUG_FAT, "tf_req: file %s", request->hr_file));
1616
1617 if (bozo_decode_url_percent(request, request->hr_file) ||
1618 check_virtual(request))
1619 goto bad_done;
1620
1621 file = request->hr_file;
1622
1623 if (file[0] != '/') {
1624 bozo_http_error(httpd, 404, request, "unknown URL");
1625 goto bad_done;
1626 }
1627
1628 /* omit additional slashes at the beginning */
1629 while (file[1] == '/')
1630 file++;
1631
1632 /* fix file provided by user as it's used in other handlers */
1633 request->hr_file = file;
1634
1635 len = strlen(file);
1636
1637 #ifndef NO_USER_SUPPORT
1638 /* first of all expand user path */
1639 if (len > 1 && httpd->enable_users && file[1] == '~') {
1640 if (file[2] == '\0') {
1641 bozo_http_error(httpd, 404, request,
1642 "missing username");
1643 goto bad_done;
1644 }
1645 if (strchr(file + 2, '/') == NULL) {
1646 char *userredirecturl;
1647
1648 bozoasprintf(httpd, &userredirecturl, "%s/", file);
1649 handle_redirect(request, userredirecturl, 0);
1650 free(userredirecturl);
1651 return 0;
1652 }
1653 debug((httpd, DEBUG_FAT, "calling bozo_user_transform"));
1654
1655 if (!bozo_user_transform(request))
1656 return 0;
1657
1658 file = request->hr_file;
1659 len = strlen(file);
1660 }
1661 #endif /* NO_USER_SUPPORT */
1662
1663
1664 switch (check_bzredirect(request)) {
1665 case -1:
1666 goto bad_done;
1667 case 0:
1668 break;
1669 default:
1670 return 0;
1671 }
1672
1673 if (len > 1) {
1674 debug((httpd, DEBUG_FAT, "file[len-1] == %c", file[len-1]));
1675 if (file[len-1] == '/') { /* append index.html */
1676 *isindex = 1;
1677 debug((httpd, DEBUG_FAT, "appending index.html"));
1678 newfile = bozomalloc(httpd,
1679 len + strlen(httpd->index_html) + 1);
1680 strcpy(newfile, file + 1);
1681 strcat(newfile, httpd->index_html);
1682 } else
1683 newfile = bozostrdup(httpd, request, file + 1);
1684 } else if (len == 1) {
1685 debug((httpd, DEBUG_EXPLODING, "tf_req: len == 1"));
1686 newfile = bozostrdup(httpd, request, httpd->index_html);
1687 *isindex = 1;
1688 } else { /* len == 0 ? */
1689 bozo_http_error(httpd, 500, request, "request->hr_file is nul");
1690 goto bad_done;
1691 }
1692
1693 if (newfile == NULL) {
1694 bozo_http_error(httpd, 500, request, "internal failure");
1695 goto bad_done;
1696 }
1697
1698 /*
1699 * stop traversing outside our domain
1700 *
1701 * XXX true security only comes from our parent using chroot(2)
1702 * before execve(2)'ing us. or our own built in chroot(2) support.
1703 */
1704
1705 debug((httpd, DEBUG_FAT, "newfile: %s", newfile));
1706
1707 if (*newfile == '/' || strcmp(newfile, "..") == 0 ||
1708 strstr(newfile, "/..") || strstr(newfile, "../")) {
1709 bozo_http_error(httpd, 403, request, "illegal request");
1710 goto bad_done;
1711 }
1712
1713 if (bozo_auth_check(request, newfile))
1714 goto bad_done;
1715
1716 if (strlen(newfile)) {
1717 request->hr_oldfile = request->hr_file;
1718 request->hr_file = newfile;
1719 }
1720
1721 if (bozo_process_cgi(request) ||
1722 bozo_process_lua(request))
1723 return 0;
1724
1725 debug((httpd, DEBUG_FAT, "transform_request set: %s", newfile));
1726 return 1;
1727
1728 bad_done:
1729 debug((httpd, DEBUG_FAT, "transform_request returning: 0"));
1730 free(newfile);
1731 return 0;
1732 }
1733
1734 /*
1735 * can_gzip checks if the request supports and prefers gzip encoding.
1736 *
1737 * XXX: we do not consider the associated q with gzip in making our
1738 * decision which is broken.
1739 */
1740
1741 static int
1742 can_gzip(bozo_httpreq_t *request)
1743 {
1744 const char *pos;
1745 const char *tmp;
1746 size_t len;
1747
1748 /* First we decide if the request can be gzipped at all. */
1749
1750 /* not if we already are encoded... */
1751 tmp = bozo_content_encoding(request, request->hr_file);
1752 if (tmp && *tmp)
1753 return 0;
1754
1755 /* not if we are not asking for the whole file... */
1756 if (request->hr_last_byte_pos != -1 || request->hr_have_range)
1757 return 0;
1758
1759 /* Then we determine if gzip is on the cards. */
1760
1761 for (pos = request->hr_accept_encoding; pos && *pos; pos += len) {
1762 while (*pos == ' ')
1763 pos++;
1764
1765 len = strcspn(pos, ";,");
1766
1767 if ((len == 4 && strncasecmp("gzip", pos, 4) == 0) ||
1768 (len == 6 && strncasecmp("x-gzip", pos, 6) == 0))
1769 return 1;
1770
1771 if (pos[len] == ';')
1772 len += strcspn(&pos[len], ",");
1773
1774 if (pos[len])
1775 len++;
1776 }
1777
1778 return 0;
1779 }
1780
1781 /*
1782 * bozo_process_request does the following:
1783 * - check the request is valid
1784 * - process cgi-bin if necessary
1785 * - transform a filename if necesarry
1786 * - return the HTTP request
1787 */
1788 void
1789 bozo_process_request(bozo_httpreq_t *request)
1790 {
1791 bozohttpd_t *httpd = request->hr_httpd;
1792 struct stat sb;
1793 time_t timestamp;
1794 char *file;
1795 const char *type, *encoding;
1796 int fd, isindex;
1797
1798 /*
1799 * note that transform_request chdir()'s if required. also note
1800 * that cgi is handed here. if transform_request() returns 0
1801 * then the request has been handled already.
1802 */
1803 if (transform_request(request, &isindex) == 0)
1804 return;
1805
1806 fd = -1;
1807 encoding = NULL;
1808 if (can_gzip(request)) {
1809 bozoasprintf(httpd, &file, "%s.gz", request->hr_file);
1810 fd = open(file, O_RDONLY);
1811 if (fd >= 0)
1812 encoding = "gzip";
1813 free(file);
1814 }
1815
1816 file = request->hr_file;
1817
1818 if (fd < 0)
1819 fd = open(file, O_RDONLY);
1820
1821 if (fd < 0) {
1822 debug((httpd, DEBUG_FAT, "open failed: %s", strerror(errno)));
1823 switch (errno) {
1824 case EPERM:
1825 case EACCES:
1826 bozo_http_error(httpd, 403, request,
1827 "no permission to open file");
1828 break;
1829 case ENAMETOOLONG:
1830 /*FALLTHROUGH*/
1831 case ENOENT:
1832 if (!bozo_dir_index(request, file, isindex))
1833 bozo_http_error(httpd, 404, request, "no file");
1834 break;
1835 default:
1836 bozo_http_error(httpd, 500, request, "open file");
1837 }
1838 goto cleanup_nofd;
1839 }
1840 if (fstat(fd, &sb) < 0) {
1841 bozo_http_error(httpd, 500, request, "can't fstat");
1842 goto cleanup;
1843 }
1844 if (S_ISDIR(sb.st_mode)) {
1845 handle_redirect(request, NULL, 0);
1846 goto cleanup;
1847 }
1848
1849 if (request->hr_if_modified_since &&
1850 parse_http_date(request->hr_if_modified_since, ×tamp) &&
1851 timestamp >= sb.st_mtime) {
1852 /* XXX ignore subsecond of timestamp */
1853 bozo_printf(httpd, "%s 304 Not Modified\r\n",
1854 request->hr_proto);
1855 bozo_printf(httpd, "\r\n");
1856 bozo_flush(httpd, stdout);
1857 goto cleanup;
1858 }
1859
1860 /* validate requested range */
1861 if (request->hr_last_byte_pos == -1 ||
1862 request->hr_last_byte_pos >= sb.st_size)
1863 request->hr_last_byte_pos = sb.st_size - 1;
1864 if (request->hr_have_range &&
1865 request->hr_first_byte_pos > request->hr_last_byte_pos) {
1866 request->hr_have_range = 0; /* punt */
1867 request->hr_first_byte_pos = 0;
1868 request->hr_last_byte_pos = sb.st_size - 1;
1869 }
1870 debug((httpd, DEBUG_FAT, "have_range %d first_pos %lld last_pos %lld",
1871 request->hr_have_range,
1872 (long long)request->hr_first_byte_pos,
1873 (long long)request->hr_last_byte_pos));
1874 if (request->hr_have_range)
1875 bozo_printf(httpd, "%s 206 Partial Content\r\n",
1876 request->hr_proto);
1877 else
1878 bozo_printf(httpd, "%s 200 OK\r\n", request->hr_proto);
1879
1880 if (request->hr_proto != httpd->consts.http_09) {
1881 type = bozo_content_type(request, file);
1882 if (!encoding)
1883 encoding = bozo_content_encoding(request, file);
1884
1885 bozo_print_header(request, &sb, type, encoding);
1886 bozo_printf(httpd, "\r\n");
1887 }
1888 bozo_flush(httpd, stdout);
1889
1890 if (request->hr_method != HTTP_HEAD) {
1891 off_t szleft, cur_byte_pos;
1892
1893 szleft =
1894 request->hr_last_byte_pos - request->hr_first_byte_pos + 1;
1895 cur_byte_pos = request->hr_first_byte_pos;
1896
1897 retry:
1898 while (szleft) {
1899 size_t sz;
1900
1901 if ((off_t)httpd->mmapsz < szleft)
1902 sz = httpd->mmapsz;
1903 else
1904 sz = (size_t)szleft;
1905 if (mmap_and_write_part(httpd, fd, cur_byte_pos, sz)) {
1906 if (errno == ENOMEM) {
1907 httpd->mmapsz /= 2;
1908 if (httpd->mmapsz >= httpd->page_size)
1909 goto retry;
1910 }
1911 goto cleanup;
1912 }
1913 cur_byte_pos += sz;
1914 szleft -= sz;
1915 }
1916 }
1917 cleanup:
1918 close(fd);
1919 cleanup_nofd:
1920 close(STDIN_FILENO);
1921 close(STDOUT_FILENO);
1922 /*close(STDERR_FILENO);*/
1923 }
1924
1925 /* make sure we're not trying to access special files */
1926 int
1927 bozo_check_special_files(bozo_httpreq_t *request, const char *name)
1928 {
1929 bozohttpd_t *httpd = request->hr_httpd;
1930 size_t i;
1931
1932 for (i = 0; specials[i].file; i++)
1933 if (strcmp(name, specials[i].file) == 0)
1934 return bozo_http_error(httpd, 403, request,
1935 specials[i].name);
1936
1937 return 0;
1938 }
1939
1940 /* generic header printing routine */
1941 void
1942 bozo_print_header(bozo_httpreq_t *request,
1943 struct stat *sbp, const char *type, const char *encoding)
1944 {
1945 bozohttpd_t *httpd = request->hr_httpd;
1946 off_t len;
1947 char date[40];
1948 bozoheaders_t *hdr;
1949
1950 SIMPLEQ_FOREACH(hdr, &request->hr_replheaders, h_next) {
1951 bozo_printf(httpd, "%s: %s\r\n", hdr->h_header,
1952 hdr->h_value);
1953 }
1954
1955 bozo_printf(httpd, "Date: %s\r\n", bozo_http_date(date, sizeof(date)));
1956 bozo_printf(httpd, "Server: %s\r\n", httpd->server_software);
1957 bozo_printf(httpd, "Accept-Ranges: bytes\r\n");
1958 if (sbp) {
1959 char filedate[40];
1960 struct tm *tm;
1961
1962 tm = gmtime(&sbp->st_mtime);
1963 strftime(filedate, sizeof filedate,
1964 "%a, %d %b %Y %H:%M:%S GMT", tm);
1965 bozo_printf(httpd, "Last-Modified: %s\r\n", filedate);
1966 }
1967 if (type && *type)
1968 bozo_printf(httpd, "Content-Type: %s\r\n", type);
1969 if (encoding && *encoding)
1970 bozo_printf(httpd, "Content-Encoding: %s\r\n", encoding);
1971 if (sbp) {
1972 if (request->hr_have_range) {
1973 len = request->hr_last_byte_pos -
1974 request->hr_first_byte_pos +1;
1975 bozo_printf(httpd,
1976 "Content-Range: bytes %qd-%qd/%qd\r\n",
1977 (long long) request->hr_first_byte_pos,
1978 (long long) request->hr_last_byte_pos,
1979 (long long) sbp->st_size);
1980 } else
1981 len = sbp->st_size;
1982 bozo_printf(httpd, "Content-Length: %qd\r\n", (long long)len);
1983 }
1984 if (request->hr_proto == httpd->consts.http_11)
1985 bozo_printf(httpd, "Connection: close\r\n");
1986 bozo_flush(httpd, stdout);
1987 }
1988
1989 #ifndef NO_DEBUG
1990 void
1991 debug__(bozohttpd_t *httpd, int level, const char *fmt, ...)
1992 {
1993 va_list ap;
1994 int savederrno;
1995
1996 /* only log if the level is low enough */
1997 if (httpd->debug < level)
1998 return;
1999
2000 savederrno = errno;
2001 va_start(ap, fmt);
2002 if (httpd->logstderr) {
2003 vfprintf(stderr, fmt, ap);
2004 fputs("\n", stderr);
2005 } else
2006 vsyslog(LOG_DEBUG, fmt, ap);
2007 va_end(ap);
2008 errno = savederrno;
2009 }
2010 #endif /* NO_DEBUG */
2011
2012 /* these are like warn() and err(), except for syslog not stderr */
2013 void
2014 bozowarn(bozohttpd_t *httpd, const char *fmt, ...)
2015 {
2016 va_list ap;
2017
2018 va_start(ap, fmt);
2019 if (httpd->logstderr || isatty(STDERR_FILENO)) {
2020 //fputs("warning: ", stderr);
2021 vfprintf(stderr, fmt, ap);
2022 fputs("\n", stderr);
2023 } else
2024 vsyslog(LOG_INFO, fmt, ap);
2025 va_end(ap);
2026 }
2027
2028 void
2029 bozoerr(bozohttpd_t *httpd, int code, const char *fmt, ...)
2030 {
2031 va_list ap;
2032
2033 va_start(ap, fmt);
2034 if (httpd->logstderr || isatty(STDERR_FILENO)) {
2035 //fputs("error: ", stderr);
2036 vfprintf(stderr, fmt, ap);
2037 fputs("\n", stderr);
2038 } else
2039 vsyslog(LOG_ERR, fmt, ap);
2040 va_end(ap);
2041 exit(code);
2042 }
2043
2044 void
2045 bozoasprintf(bozohttpd_t *httpd, char **str, const char *fmt, ...)
2046 {
2047 va_list ap;
2048 int e;
2049
2050 va_start(ap, fmt);
2051 e = vasprintf(str, fmt, ap);
2052 va_end(ap);
2053
2054 if (e < 0)
2055 bozoerr(httpd, EXIT_FAILURE, "asprintf");
2056 }
2057
2058 /*
2059 * this escapes HTML tags. returns allocated escaped
2060 * string if needed, or NULL on allocation failure or
2061 * lack of escape need.
2062 * call with NULL httpd in error paths, to avoid recursive
2063 * malloc failure. call with valid httpd in normal paths
2064 * to get automatic allocation failure handling.
2065 */
2066 char *
2067 bozo_escape_html(bozohttpd_t *httpd, const char *url)
2068 {
2069 int i, j;
2070 char *tmp;
2071 size_t len;
2072
2073 for (i = 0, j = 0; url[i]; i++) {
2074 switch (url[i]) {
2075 case '<':
2076 case '>':
2077 j += 4;
2078 break;
2079 case '&':
2080 j += 5;
2081 break;
2082 case '"':
2083 j += 6;
2084 break;
2085 }
2086 }
2087
2088 if (j == 0)
2089 return NULL;
2090
2091 /*
2092 * we need to handle being called from different
2093 * pathnames.
2094 */
2095 len = strlen(url) + j;
2096 if (httpd)
2097 tmp = bozomalloc(httpd, len);
2098 else if ((tmp = malloc(len)) == 0)
2099 return NULL;
2100
2101 for (i = 0, j = 0; url[i]; i++) {
2102 switch (url[i]) {
2103 case '<':
2104 memcpy(tmp + j, "<", 4);
2105 j += 4;
2106 break;
2107 case '>':
2108 memcpy(tmp + j, ">", 4);
2109 j += 4;
2110 break;
2111 case '&':
2112 memcpy(tmp + j, "&", 5);
2113 j += 5;
2114 break;
2115 case '"':
2116 memcpy(tmp + j, """, 6);
2117 j += 6;
2118 break;
2119 default:
2120 tmp[j++] = url[i];
2121 }
2122 }
2123 tmp[j] = 0;
2124
2125 return tmp;
2126 }
2127
2128 /* short map between error code, and short/long messages */
2129 static struct errors_map {
2130 int code; /* HTTP return code */
2131 const char *shortmsg; /* short version of message */
2132 const char *longmsg; /* long version of message */
2133 } errors_map[] = {
2134 { 400, "400 Bad Request", "The request was not valid", },
2135 { 401, "401 Unauthorized", "No authorization", },
2136 { 403, "403 Forbidden", "Access to this item has been denied",},
2137 { 404, "404 Not Found", "This item has not been found", },
2138 { 408, "408 Request Timeout", "This request took too long", },
2139 { 413, "413 Payload Too Large", "Use smaller requests", },
2140 { 417, "417 Expectation Failed","Expectations not available", },
2141 { 420, "420 Enhance Your Calm","Chill, Winston", },
2142 { 500, "500 Internal Error", "An error occured on the server", },
2143 { 501, "501 Not Implemented", "This request is not available", },
2144 { 0, NULL, NULL, },
2145 };
2146
2147 static const char *help = "DANGER! WILL ROBINSON! DANGER!";
2148
2149 static const char *
2150 http_errors_short(int code)
2151 {
2152 struct errors_map *ep;
2153
2154 for (ep = errors_map; ep->code; ep++)
2155 if (ep->code == code)
2156 return (ep->shortmsg);
2157 return (help);
2158 }
2159
2160 static const char *
2161 http_errors_long(int code)
2162 {
2163 struct errors_map *ep;
2164
2165 for (ep = errors_map; ep->code; ep++)
2166 if (ep->code == code)
2167 return (ep->longmsg);
2168 return (help);
2169 }
2170
2171 /* the follow functions and variables are used in handling HTTP errors */
2172 /* ARGSUSED */
2173 int
2174 bozo_http_error(bozohttpd_t *httpd, int code, bozo_httpreq_t *request,
2175 const char *msg)
2176 {
2177 char portbuf[20];
2178 const char *header = http_errors_short(code);
2179 const char *reason = http_errors_long(code);
2180 const char *proto = (request && request->hr_proto) ?
2181 request->hr_proto : httpd->consts.http_11;
2182 int size;
2183 bozoheaders_t *hdr;
2184
2185 debug((httpd, DEBUG_FAT, "bozo_http_error %d: %s", code, msg));
2186 if (header == NULL || reason == NULL) {
2187 bozoerr(httpd, 1,
2188 "bozo_http_error() failed (short = %p, long = %p)",
2189 header, reason);
2190 return code;
2191 }
2192
2193 if (request && request->hr_serverport &&
2194 strcmp(request->hr_serverport, "80") != 0)
2195 snprintf(portbuf, sizeof(portbuf), ":%s",
2196 request->hr_serverport);
2197 else
2198 portbuf[0] = '\0';
2199
2200 if (request && request->hr_file) {
2201 char *file = NULL, *user = NULL;
2202 int file_alloc = 0;
2203 const char *hostname = BOZOHOST(httpd, request);
2204
2205 /* bozo_escape_html() failure here is just too bad. */
2206 file = bozo_escape_html(NULL, request->hr_file);
2207 if (file == NULL)
2208 file = request->hr_file;
2209 else
2210 file_alloc = 1;
2211
2212 #ifndef NO_USER_SUPPORT
2213 if (request->hr_user != NULL) {
2214 char *user_escaped;
2215
2216 user_escaped = bozo_escape_html(NULL, request->hr_user);
2217 if (user_escaped == NULL)
2218 user_escaped = request->hr_user;
2219 /* expand username to ~user/ */
2220 bozoasprintf(httpd, &user, "~%s/", user_escaped);
2221 if (user_escaped != request->hr_user)
2222 free(user_escaped);
2223 }
2224 #endif /* !NO_USER_SUPPORT */
2225
2226 size = snprintf(httpd->errorbuf, BUFSIZ,
2227 "<html><head><title>%s</title></head>\n"
2228 "<body><h1>%s</h1>\n"
2229 "%s%s: <pre>%s</pre>\n"
2230 "<hr><address><a href=\"//%s%s/\">%s%s</a></address>\n"
2231 "</body></html>\n",
2232 header, header,
2233 user ? user : "", file,
2234 reason, hostname, portbuf, hostname, portbuf);
2235 free(user);
2236 if (size >= (int)BUFSIZ) {
2237 bozowarn(httpd,
2238 "bozo_http_error buffer too small, truncated");
2239 size = (int)BUFSIZ;
2240 }
2241
2242 if (file_alloc)
2243 free(file);
2244 } else
2245 size = 0;
2246
2247 bozo_printf(httpd, "%s %s\r\n", proto, header);
2248
2249 if (request) {
2250 bozo_auth_check_401(request, code);
2251 SIMPLEQ_FOREACH(hdr, &request->hr_replheaders, h_next) {
2252 bozo_printf(httpd, "%s: %s\r\n", hdr->h_header,
2253 hdr->h_value);
2254 }
2255 }
2256
2257 bozo_printf(httpd, "Content-Type: text/html\r\n");
2258 bozo_printf(httpd, "Content-Length: %d\r\n", size);
2259 bozo_printf(httpd, "Server: %s\r\n", httpd->server_software);
2260 if (request && request->hr_allow)
2261 bozo_printf(httpd, "Allow: %s\r\n", request->hr_allow);
2262 /* RFC 7231 (HTTP/1.1) 6.5.7 */
2263 if (code == 408 && request &&
2264 request->hr_proto == httpd->consts.http_11)
2265 bozo_printf(httpd, "Connection: close\r\n");
2266 bozo_printf(httpd, "\r\n");
2267 /* According to the RFC 2616 sec. 9.4 HEAD method MUST NOT return a
2268 * message-body in the response */
2269 if (size && request && request->hr_method != HTTP_HEAD)
2270 bozo_printf(httpd, "%s", httpd->errorbuf);
2271 bozo_flush(httpd, stdout);
2272
2273 return code;
2274 }
2275
2276 /* Below are various modified libc functions */
2277
2278 /*
2279 * returns -1 in lenp if the string ran out before finding a delimiter,
2280 * but is otherwise the same as strsep. Note that the length must be
2281 * correctly passed in.
2282 */
2283 char *
2284 bozostrnsep(char **strp, const char *delim, ssize_t *lenp)
2285 {
2286 char *s;
2287 const char *spanp;
2288 int c, sc;
2289 char *tok;
2290
2291 if ((s = *strp) == NULL)
2292 return (NULL);
2293 for (tok = s;;) {
2294 if (lenp && --(*lenp) == -1)
2295 return (NULL);
2296 c = *s++;
2297 spanp = delim;
2298 do {
2299 if ((sc = *spanp++) == c) {
2300 if (c == 0)
2301 s = NULL;
2302 else
2303 s[-1] = '\0';
2304 *strp = s;
2305 return (tok);
2306 }
2307 } while (sc != 0);
2308 }
2309 /* NOTREACHED */
2310 }
2311
2312 /*
2313 * inspired by fgetln(3), but works for fd's. should work identically
2314 * except it, however, does *not* return the newline, and it does nul
2315 * terminate the string.
2316 */
2317 char *
2318 bozodgetln(bozohttpd_t *httpd, int fd, ssize_t *lenp,
2319 ssize_t (*readfn)(bozohttpd_t *, int, void *, size_t))
2320 {
2321 ssize_t len;
2322 int got_cr = 0;
2323 char c, *nbuffer;
2324
2325 /* initialise */
2326 if (httpd->getln_buflen == 0) {
2327 /* should be plenty for most requests */
2328 httpd->getln_buflen = 128;
2329 httpd->getln_buffer = malloc((size_t)httpd->getln_buflen);
2330 if (httpd->getln_buffer == NULL) {
2331 httpd->getln_buflen = 0;
2332 return NULL;
2333 }
2334 }
2335 len = 0;
2336
2337 /*
2338 * we *have* to read one byte at a time, to not break cgi
2339 * programs (for we pass stdin off to them). could fix this
2340 * by becoming a fd-passing program instead of just exec'ing
2341 * the program
2342 *
2343 * the above is no longer true, we are the fd-passing
2344 * program already.
2345 */
2346 for (; readfn(httpd, fd, &c, 1) == 1; ) {
2347 debug((httpd, DEBUG_EXPLODING, "bozodgetln read %c", c));
2348
2349 if (len >= httpd->getln_buflen - 1) {
2350 httpd->getln_buflen *= 2;
2351 debug((httpd, DEBUG_EXPLODING, "bozodgetln: "
2352 "reallocating buffer to buflen %zu",
2353 httpd->getln_buflen));
2354 nbuffer = bozorealloc(httpd, httpd->getln_buffer,
2355 (size_t)httpd->getln_buflen);
2356 httpd->getln_buffer = nbuffer;
2357 }
2358
2359 httpd->getln_buffer[len++] = c;
2360 if (c == '\r') {
2361 got_cr = 1;
2362 continue;
2363 } else if (c == '\n') {
2364 /*
2365 * HTTP/1.1 spec says to ignore CR and treat
2366 * LF as the real line terminator. even though
2367 * the same spec defines CRLF as the line
2368 * terminator, it is recommended in section 19.3
2369 * to do the LF trick for tolerance.
2370 */
2371 if (got_cr)
2372 len -= 2;
2373 else
2374 len -= 1;
2375 break;
2376 }
2377
2378 }
2379 httpd->getln_buffer[len] = '\0';
2380 debug((httpd, DEBUG_OBESE, "bozodgetln returns: '%s' with len %zd",
2381 httpd->getln_buffer, len));
2382 *lenp = len;
2383 return httpd->getln_buffer;
2384 }
2385
2386 void *
2387 bozorealloc(bozohttpd_t *httpd, void *ptr, size_t size)
2388 {
2389 void *p;
2390
2391 p = realloc(ptr, size);
2392 if (p)
2393 return p;
2394
2395 bozo_http_error(httpd, 500, NULL, "memory allocation failure");
2396 exit(EXIT_FAILURE);
2397 }
2398
2399 void *
2400 bozomalloc(bozohttpd_t *httpd, size_t size)
2401 {
2402 void *p;
2403
2404 p = malloc(size);
2405 if (p)
2406 return p;
2407
2408 bozo_http_error(httpd, 500, NULL, "memory allocation failure");
2409 exit(EXIT_FAILURE);
2410 }
2411
2412 char *
2413 bozostrdup(bozohttpd_t *httpd, bozo_httpreq_t *request, const char *str)
2414 {
2415 char *p;
2416
2417 p = strdup(str);
2418 if (p)
2419 return p;
2420
2421 if (!request)
2422 bozoerr(httpd, EXIT_FAILURE, "strdup");
2423
2424 bozo_http_error(httpd, 500, request, "memory allocation failure");
2425 exit(EXIT_FAILURE);
2426 }
2427
2428 /* set default values in bozohttpd_t struct */
2429 int
2430 bozo_init_httpd(bozohttpd_t *httpd)
2431 {
2432 /* make sure everything is clean */
2433 (void) memset(httpd, 0x0, sizeof(*httpd));
2434
2435 /* constants */
2436 httpd->consts.http_09 = "HTTP/0.9";
2437 httpd->consts.http_10 = "HTTP/1.0";
2438 httpd->consts.http_11 = "HTTP/1.1";
2439 httpd->consts.text_plain = "text/plain";
2440
2441 /* mmap region size */
2442 httpd->mmapsz = BOZO_MMAPSZ;
2443
2444 /* error buffer for bozo_http_error() */
2445 if ((httpd->errorbuf = malloc(BUFSIZ)) == NULL) {
2446 fprintf(stderr,
2447 "bozohttpd: memory_allocation failure\n");
2448 return 0;
2449 }
2450 #ifndef NO_LUA_SUPPORT
2451 SIMPLEQ_INIT(&httpd->lua_states);
2452 #endif
2453 return 1;
2454 }
2455
2456 /* set default values in bozoprefs_t struct */
2457 int
2458 bozo_init_prefs(bozohttpd_t *httpd, bozoprefs_t *prefs)
2459 {
2460 int rv = 0;
2461
2462 /* make sure everything is clean */
2463 (void) memset(prefs, 0x0, sizeof(*prefs));
2464
2465 /* set up default values */
2466 if (!bozo_set_pref(httpd, prefs, "server software", SERVER_SOFTWARE))
2467 rv = 1;
2468 if (!bozo_set_pref(httpd, prefs, "index.html", INDEX_HTML))
2469 rv = 1;
2470 if (!bozo_set_pref(httpd, prefs, "public_html", PUBLIC_HTML))
2471 rv = 1;
2472 if (!bozo_set_pref(httpd, prefs, "initial timeout", INITIAL_TIMEOUT))
2473 rv = 1;
2474 if (!bozo_set_pref(httpd, prefs, "header timeout", HEADER_WAIT_TIME))
2475 rv = 1;
2476 if (!bozo_set_pref(httpd, prefs, "request timeout", TOTAL_MAX_REQ_TIME))
2477 rv = 1;
2478
2479 return rv;
2480 }
2481
2482 /* set default values */
2483 int
2484 bozo_set_defaults(bozohttpd_t *httpd, bozoprefs_t *prefs)
2485 {
2486 return bozo_init_httpd(httpd) && bozo_init_prefs(httpd, prefs);
2487 }
2488
2489 /* set the virtual host name, port and root */
2490 int
2491 bozo_setup(bozohttpd_t *httpd, bozoprefs_t *prefs, const char *vhost,
2492 const char *root)
2493 {
2494 struct passwd *pw;
2495 extern char **environ;
2496 static char *cleanenv[1] = { NULL };
2497 uid_t uid;
2498 int uidset = 0;
2499 char *chrootdir;
2500 char *username;
2501 char *portnum;
2502 char *cp;
2503 int dirtyenv;
2504
2505 dirtyenv = 0;
2506
2507 if (vhost == NULL) {
2508 httpd->virthostname = bozomalloc(httpd, MAXHOSTNAMELEN+1);
2509 if (gethostname(httpd->virthostname, MAXHOSTNAMELEN+1) < 0)
2510 bozoerr(httpd, 1, "gethostname");
2511 httpd->virthostname[MAXHOSTNAMELEN] = '\0';
2512 } else {
2513 httpd->virthostname = bozostrdup(httpd, NULL, vhost);
2514 }
2515 httpd->slashdir = bozostrdup(httpd, NULL, root);
2516 if ((portnum = bozo_get_pref(prefs, "port number")) != NULL) {
2517 httpd->bindport = bozostrdup(httpd, NULL, portnum);
2518 }
2519
2520 /* go over preferences now */
2521 if ((cp = bozo_get_pref(prefs, "numeric")) != NULL &&
2522 strcmp(cp, "true") == 0) {
2523 httpd->numeric = 1;
2524 }
2525 if ((cp = bozo_get_pref(prefs, "log to stderr")) != NULL &&
2526 strcmp(cp, "true") == 0) {
2527 httpd->logstderr = 1;
2528 }
2529 if ((cp = bozo_get_pref(prefs, "bind address")) != NULL) {
2530 httpd->bindaddress = bozostrdup(httpd, NULL, cp);
2531 }
2532 if ((cp = bozo_get_pref(prefs, "background")) != NULL) {
2533 httpd->background = atoi(cp);
2534 }
2535 if ((cp = bozo_get_pref(prefs, "foreground")) != NULL &&
2536 strcmp(cp, "true") == 0) {
2537 httpd->foreground = 1;
2538 }
2539 if ((cp = bozo_get_pref(prefs, "pid file")) != NULL) {
2540 httpd->pidfile = bozostrdup(httpd, NULL, cp);
2541 }
2542 if ((cp = bozo_get_pref(prefs, "unknown slash")) != NULL &&
2543 strcmp(cp, "true") == 0) {
2544 httpd->unknown_slash = 1;
2545 }
2546 if ((cp = bozo_get_pref(prefs, "virtual base")) != NULL) {
2547 httpd->virtbase = bozostrdup(httpd, NULL, cp);
2548 }
2549 if ((cp = bozo_get_pref(prefs, "enable users")) != NULL &&
2550 strcmp(cp, "true") == 0) {
2551 httpd->enable_users = 1;
2552 }
2553 if ((cp = bozo_get_pref(prefs, "enable user cgibin")) != NULL &&
2554 strcmp(cp, "true") == 0) {
2555 httpd->enable_cgi_users = 1;
2556 }
2557 if ((cp = bozo_get_pref(prefs, "dirty environment")) != NULL &&
2558 strcmp(cp, "true") == 0) {
2559 dirtyenv = 1;
2560 }
2561 if ((cp = bozo_get_pref(prefs, "hide dots")) != NULL &&
2562 strcmp(cp, "true") == 0) {
2563 httpd->hide_dots = 1;
2564 }
2565 if ((cp = bozo_get_pref(prefs, "directory indexing")) != NULL &&
2566 strcmp(cp, "true") == 0) {
2567 httpd->dir_indexing = 1;
2568 }
2569 if ((cp = bozo_get_pref(prefs, "public_html")) != NULL) {
2570 httpd->public_html = bozostrdup(httpd, NULL, cp);
2571 }
2572 if ((cp = bozo_get_pref(prefs, "initial timeout")) != NULL) {
2573 httpd->initial_timeout = atoi(cp);
2574 }
2575 if ((cp = bozo_get_pref(prefs, "header timeout")) != NULL) {
2576 httpd->header_timeout = atoi(cp);
2577 }
2578 if ((cp = bozo_get_pref(prefs, "request timeout")) != NULL) {
2579 httpd->request_timeout = atoi(cp);
2580 }
2581 httpd->server_software =
2582 bozostrdup(httpd, NULL, bozo_get_pref(prefs, "server software"));
2583 httpd->index_html =
2584 bozostrdup(httpd, NULL, bozo_get_pref(prefs, "index.html"));
2585
2586 /*
2587 * initialise ssl and daemon mode if necessary.
2588 */
2589 bozo_ssl_init(httpd);
2590 bozo_daemon_init(httpd);
2591
2592 username = bozo_get_pref(prefs, "username");
2593 if (username != NULL) {
2594 if ((pw = getpwnam(username)) == NULL)
2595 bozoerr(httpd, 1, "getpwnam(%s): %s", username,
2596 strerror(errno));
2597 if (initgroups(pw->pw_name, pw->pw_gid) == -1)
2598 bozoerr(httpd, 1, "initgroups: %s", strerror(errno));
2599 if (setgid(pw->pw_gid) == -1)
2600 bozoerr(httpd, 1, "setgid(%u): %s", pw->pw_gid,
2601 strerror(errno));
2602 uid = pw->pw_uid;
2603 uidset = 1;
2604 }
2605 /*
2606 * handle chroot.
2607 */
2608 if ((chrootdir = bozo_get_pref(prefs, "chroot dir")) != NULL) {
2609 httpd->rootdir = bozostrdup(httpd, NULL, chrootdir);
2610 if (chdir(httpd->rootdir) == -1)
2611 bozoerr(httpd, 1, "chdir(%s): %s", httpd->rootdir,
2612 strerror(errno));
2613 if (chroot(httpd->rootdir) == -1)
2614 bozoerr(httpd, 1, "chroot(%s): %s", httpd->rootdir,
2615 strerror(errno));
2616 }
2617
2618 if (uidset && setuid(uid) == -1)
2619 bozoerr(httpd, 1, "setuid(%d): %s", uid, strerror(errno));
2620
2621 /*
2622 * prevent info leakage between different compartments.
2623 * some PATH values in the environment would be invalided
2624 * by chroot. cross-user settings might result in undesirable
2625 * effects.
2626 */
2627 if ((chrootdir != NULL || username != NULL) && !dirtyenv)
2628 environ = cleanenv;
2629
2630 #ifdef _SC_PAGESIZE
2631 httpd->page_size = (long)sysconf(_SC_PAGESIZE);
2632 #else
2633 httpd->page_size = 4096;
2634 #endif
2635 debug((httpd, DEBUG_OBESE, "myname is %s, slashdir is %s",
2636 httpd->virthostname, httpd->slashdir));
2637
2638 return 1;
2639 }
2640
2641 int
2642 bozo_get_version(char *buf, size_t size)
2643 {
2644 return snprintf(buf, size, "%s", SERVER_SOFTWARE);
2645 }
2646