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