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