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