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