Home | History | Annotate | Line # | Download | only in httpd
bozohttpd.c revision 1.23
      1  1.23    mrg /*	$NetBSD: bozohttpd.c,v 1.23 2010/09/20 21:58:43 mrg Exp $	*/
      2   1.3    tls 
      3  1.21    mrg /*	$eterna: bozohttpd.c,v 1.174 2010/06/21 06:47:23 mrg Exp $	*/
      4   1.1    tls 
      5   1.1    tls /*
      6  1.16    mrg  * Copyright (c) 1997-2010 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  *	  however, this does not include cgi-bin support
     44   1.1    tls  *	- access lists via libwrap via inetd/tcpd
     45   1.1    tls  *	- virtual hosting
     46   1.1    tls  *	- not that we do not even pretend to understand MIME, but
     47   1.1    tls  *	  rely only on the HTTP specification
     48   1.1    tls  *	- ipv6 support
     49   1.1    tls  *	- automatic `index.html' generation
     50   1.1    tls  *	- configurable server name
     51   1.1    tls  *	- directory index generation
     52   1.1    tls  *	- daemon mode (lacks libwrap support)
     53   1.1    tls  *	- .htpasswd support
     54   1.1    tls  */
     55   1.1    tls 
     56   1.1    tls /*
     57   1.1    tls  * requirements for minimal http/1.1 (at least, as documented in
     58   1.1    tls  * <draft-ietf-http-v11-spec-rev-06> which expired may 18, 1999):
     59   1.1    tls  *
     60   1.1    tls  *	- 14.15: content-encoding handling. [1]
     61   1.1    tls  *
     62   1.1    tls  *	- 14.16: content-length handling.  this is only a SHOULD header
     63   1.1    tls  *	  thus we could just not send it ever.  [1]
     64   1.1    tls  *
     65   1.1    tls  *	- 14.17: content-type handling. [1]
     66   1.1    tls  *
     67   1.1    tls  *	- 14.25/28: if-{,un}modified-since handling.  maybe do this, but
     68   1.1    tls  *	  i really don't want to have to parse 3 differnet date formats
     69   1.1    tls  *
     70   1.1    tls  * [1] need to revisit to ensure proper behaviour
     71   1.1    tls  *
     72   1.1    tls  * and the following is a list of features that we do not need
     73   1.1    tls  * to have due to other limits, or are too lazy.  there are more
     74   1.1    tls  * of these than are listed, but these are of particular note,
     75   1.1    tls  * and could perhaps be implemented.
     76   1.1    tls  *
     77   1.1    tls  *	- 3.5/3.6: content/transfer codings.  probably can ignore
     78   1.1    tls  *	  this?  we "SHOULD"n't.  but 4.4 says we should ignore a
     79   1.1    tls  *	  `content-length' header upon reciept of a `transfer-encoding'
     80   1.1    tls  *	  header.
     81   1.1    tls  *
     82   1.1    tls  *	- 5.1.1: request methods.  only MUST support GET and HEAD,
     83   1.1    tls  *	  but there are new ones besides POST that are currently
     84   1.1    tls  *	  supported: OPTIONS PUT DELETE TRACE and CONNECT, plus
     85   1.1    tls  *	  extensions not yet known?
     86   1.1    tls  *
     87   1.1    tls  * 	- 10.1: we can ignore informational status codes
     88   1.1    tls  *
     89   1.1    tls  *	- 10.3.3/10.3.4/10.3.8:  just use '302' codes always.
     90   1.1    tls  *
     91   1.1    tls  *	- 14.1/14.2/14.3/14.27: we do not support Accept: headers..
     92   1.1    tls  *	  just ignore them and send the request anyway.  they are
     93   1.1    tls  *	  only SHOULD.
     94   1.1    tls  *
     95   1.1    tls  *	- 14.5/14.16/14.35: we don't do ranges.  from section 14.35.2
     96   1.1    tls  *	  `A server MAY ignore the Range header'.  but it might be nice.
     97   1.6    mrg  *	  since 20080301 we support simple range headers.
     98   1.1    tls  *
     99   1.1    tls  *	- 14.9: we aren't a cache.
    100   1.1    tls  *
    101   1.1    tls  *	- 14.15: content-md5 would be nice...
    102   1.1    tls  *
    103   1.1    tls  *	- 14.24/14.26/14.27: be nice to support this...
    104   1.1    tls  *
    105   1.1    tls  *	- 14.44: not sure about this Vary: header.  ignore it for now.
    106   1.1    tls  */
    107   1.1    tls 
    108   1.1    tls #ifndef INDEX_HTML
    109   1.1    tls #define INDEX_HTML		"index.html"
    110   1.1    tls #endif
    111   1.1    tls #ifndef SERVER_SOFTWARE
    112  1.21    mrg #define SERVER_SOFTWARE		"bozohttpd/20100621"
    113   1.1    tls #endif
    114   1.1    tls #ifndef DIRECT_ACCESS_FILE
    115   1.1    tls #define DIRECT_ACCESS_FILE	".bzdirect"
    116   1.1    tls #endif
    117   1.1    tls #ifndef REDIRECT_FILE
    118   1.1    tls #define REDIRECT_FILE		".bzredirect"
    119   1.1    tls #endif
    120   1.1    tls #ifndef ABSREDIRECT_FILE
    121   1.1    tls #define ABSREDIRECT_FILE	".bzabsredirect"
    122   1.1    tls #endif
    123  1.16    mrg #ifndef PUBLIC_HTML
    124  1.16    mrg #define PUBLIC_HTML		"public_html"
    125  1.16    mrg #endif
    126  1.16    mrg 
    127  1.16    mrg #ifndef USE_ARG
    128  1.16    mrg #define USE_ARG(x)	/*LINTED*/(void)&(x)
    129  1.16    mrg #endif
    130   1.1    tls 
    131   1.1    tls /*
    132   1.1    tls  * And so it begins ..
    133   1.1    tls  */
    134   1.1    tls 
    135   1.1    tls #include <sys/param.h>
    136   1.1    tls #include <sys/socket.h>
    137   1.1    tls #include <sys/time.h>
    138   1.1    tls #include <sys/mman.h>
    139   1.1    tls 
    140   1.1    tls #include <arpa/inet.h>
    141   1.1    tls 
    142   1.1    tls #include <ctype.h>
    143   1.1    tls #include <dirent.h>
    144   1.1    tls #include <errno.h>
    145   1.1    tls #include <fcntl.h>
    146   1.1    tls #include <netdb.h>
    147   1.1    tls #include <pwd.h>
    148   1.1    tls #include <grp.h>
    149   1.1    tls #include <signal.h>
    150   1.1    tls #include <stdarg.h>
    151   1.1    tls #include <stdlib.h>
    152   1.1    tls #include <string.h>
    153   1.1    tls #include <syslog.h>
    154   1.1    tls #include <time.h>
    155   1.1    tls #include <unistd.h>
    156   1.1    tls 
    157   1.1    tls #ifndef __attribute__
    158   1.1    tls #define __attribute__(x)
    159   1.1    tls #endif /* __attribute__ */
    160   1.1    tls 
    161   1.1    tls #include "bozohttpd.h"
    162   1.1    tls 
    163   1.1    tls #ifndef MAX_WAIT_TIME
    164   1.1    tls #define	MAX_WAIT_TIME	60	/* hang around for 60 seconds max */
    165   1.1    tls #endif
    166   1.1    tls 
    167   1.1    tls /* variables and functions */
    168   1.1    tls #ifndef LOG_FTP
    169   1.1    tls #define LOG_FTP LOG_DAEMON
    170   1.1    tls #endif
    171   1.1    tls 
    172   1.1    tls volatile sig_atomic_t	alarmhit;
    173   1.1    tls 
    174  1.16    mrg /*
    175  1.16    mrg  * check there's enough space in the prefs and names arrays.
    176  1.16    mrg  */
    177  1.16    mrg static int
    178  1.16    mrg size_arrays(bozoprefs_t *bozoprefs, unsigned needed)
    179  1.16    mrg {
    180  1.16    mrg 	char	**temp;
    181   1.1    tls 
    182  1.16    mrg 	if (bozoprefs->size == 0) {
    183  1.16    mrg 		/* only get here first time around */
    184  1.16    mrg 		bozoprefs->size = needed;
    185  1.16    mrg 		if ((bozoprefs->name = calloc(sizeof(char *), needed)) == NULL) {
    186  1.16    mrg 			(void) fprintf(stderr, "size_arrays: bad alloc\n");
    187  1.16    mrg 			return 0;
    188  1.16    mrg 		}
    189  1.16    mrg 		if ((bozoprefs->value = calloc(sizeof(char *), needed)) == NULL) {
    190  1.16    mrg 			free(bozoprefs->name);
    191  1.16    mrg 			(void) fprintf(stderr, "size_arrays: bad alloc\n");
    192  1.16    mrg 			return 0;
    193  1.16    mrg 		}
    194  1.16    mrg 	} else if (bozoprefs->c == bozoprefs->size) {
    195  1.16    mrg 		/* only uses 'needed' when filled array */
    196  1.16    mrg 		bozoprefs->size += needed;
    197  1.16    mrg 		temp = realloc(bozoprefs->name, sizeof(char *) * needed);
    198  1.16    mrg 		if (temp == NULL) {
    199  1.16    mrg 			(void) fprintf(stderr, "size_arrays: bad alloc\n");
    200  1.16    mrg 			return 0;
    201  1.16    mrg 		}
    202  1.16    mrg 		bozoprefs->name = temp;
    203  1.16    mrg 		temp = realloc(bozoprefs->value, sizeof(char *) * needed);
    204  1.16    mrg 		if (temp == NULL) {
    205  1.16    mrg 			(void) fprintf(stderr, "size_arrays: bad alloc\n");
    206  1.16    mrg 			return 0;
    207  1.16    mrg 		}
    208  1.16    mrg 		bozoprefs->value = temp;
    209  1.16    mrg 	}
    210  1.16    mrg 	return 1;
    211  1.16    mrg }
    212   1.1    tls 
    213  1.16    mrg static int
    214  1.16    mrg findvar(bozoprefs_t *bozoprefs, const char *name)
    215  1.16    mrg {
    216  1.16    mrg 	unsigned	i;
    217   1.1    tls 
    218  1.16    mrg 	for (i = 0 ; i < bozoprefs->c && strcmp(bozoprefs->name[i], name) != 0; i++)
    219  1.16    mrg 		;
    220  1.16    mrg 	return (i == bozoprefs->c) ? -1 : (int)i;
    221   1.1    tls }
    222   1.1    tls 
    223   1.1    tls int
    224  1.16    mrg bozo_set_pref(bozoprefs_t *bozoprefs, const char *name, const char *value)
    225   1.1    tls {
    226  1.16    mrg 	int	i;
    227   1.1    tls 
    228  1.16    mrg 	if ((i = findvar(bozoprefs, name)) < 0) {
    229  1.16    mrg 		/* add the element to the array */
    230  1.16    mrg 		if (size_arrays(bozoprefs, bozoprefs->size + 15)) {
    231  1.16    mrg 			bozoprefs->name[i = bozoprefs->c++] = strdup(name);
    232  1.16    mrg 		}
    233  1.16    mrg 	} else {
    234  1.16    mrg 		/* replace the element in the array */
    235  1.16    mrg 		if (bozoprefs->value[i]) {
    236  1.16    mrg 			free(bozoprefs->value[i]);
    237  1.16    mrg 			bozoprefs->value[i] = NULL;
    238   1.1    tls 		}
    239   1.1    tls 	}
    240  1.16    mrg 	/* sanity checks for range of values go here */
    241  1.16    mrg 	bozoprefs->value[i] = strdup(value);
    242  1.16    mrg 	return 1;
    243  1.16    mrg }
    244   1.1    tls 
    245  1.16    mrg /*
    246  1.16    mrg  * get a variable's value, or NULL
    247  1.16    mrg  */
    248  1.16    mrg char *
    249  1.16    mrg bozo_get_pref(bozoprefs_t *bozoprefs, const char *name)
    250  1.16    mrg {
    251  1.16    mrg 	int	i;
    252   1.1    tls 
    253  1.16    mrg 	return ((i = findvar(bozoprefs, name)) < 0) ? NULL :
    254  1.16    mrg 			bozoprefs->value[i];
    255   1.1    tls }
    256   1.1    tls 
    257   1.1    tls char *
    258  1.16    mrg bozo_http_date(char *date, size_t datelen)
    259   1.1    tls {
    260   1.1    tls 	struct	tm *tm;
    261   1.1    tls 	time_t	now;
    262   1.1    tls 
    263   1.1    tls 	/* Sun, 06 Nov 1994 08:49:37 GMT */
    264   1.1    tls 	now = time(NULL);
    265   1.1    tls 	tm = gmtime(&now);	/* HTTP/1.1 spec rev 06 sez GMT only */
    266  1.16    mrg 	strftime(date, datelen, "%a, %d %b %Y %H:%M:%S GMT", tm);
    267   1.1    tls 	return date;
    268   1.1    tls }
    269   1.1    tls 
    270   1.1    tls /*
    271  1.12    mrg  * convert "in" into the three parts of a request (first line).
    272  1.12    mrg  * we allocate into file and query, but return pointers into
    273  1.12    mrg  * "in" for proto and method.
    274   1.1    tls  */
    275   1.1    tls static void
    276  1.16    mrg parse_request(bozohttpd_t *httpd, char *in, char **method, char **file,
    277  1.16    mrg 		char **query, char **proto)
    278   1.1    tls {
    279   1.1    tls 	ssize_t	len;
    280   1.1    tls 	char	*val;
    281  1.16    mrg 
    282  1.16    mrg 	USE_ARG(httpd);
    283  1.16    mrg 	debug((httpd, DEBUG_EXPLODING, "parse in: %s", in));
    284  1.12    mrg 	*method = *file = *query = *proto = NULL;
    285   1.1    tls 
    286   1.1    tls 	len = (ssize_t)strlen(in);
    287   1.6    mrg 	val = bozostrnsep(&in, " \t\n\r", &len);
    288   1.1    tls 	if (len < 1 || val == NULL)
    289   1.1    tls 		return;
    290   1.1    tls 	*method = val;
    291  1.12    mrg 
    292   1.1    tls 	while (*in == ' ' || *in == '\t')
    293   1.1    tls 		in++;
    294   1.6    mrg 	val = bozostrnsep(&in, " \t\n\r", &len);
    295   1.1    tls 	if (len < 1) {
    296   1.1    tls 		if (len == 0)
    297   1.9    tls 			*file = val;
    298   1.1    tls 		else
    299   1.9    tls 			*file = in;
    300  1.16    mrg 	} else {
    301  1.16    mrg 		*file = val;
    302   1.9    tls 
    303  1.16    mrg 		*query = strchr(*file, '?');
    304  1.16    mrg 		if (*query)
    305  1.16    mrg 			*(*query)++ = '\0';
    306  1.16    mrg 
    307  1.16    mrg 		if (in) {
    308  1.16    mrg 			while (*in && (*in == ' ' || *in == '\t'))
    309  1.16    mrg 				in++;
    310  1.16    mrg 			if (*in)
    311  1.16    mrg 				*proto = in;
    312  1.16    mrg 		}
    313  1.12    mrg 	}
    314  1.12    mrg 
    315  1.12    mrg 	/* allocate private copies */
    316  1.16    mrg 	*file = bozostrdup(httpd, *file);
    317  1.12    mrg 	if (*query)
    318  1.16    mrg 		*query = bozostrdup(httpd, *query);
    319  1.12    mrg 
    320  1.16    mrg 	debug((httpd, DEBUG_FAT,
    321  1.16    mrg 		"url: method: \"%s\" file: \"%s\" query: \"%s\" proto: \"%s\"",
    322  1.16    mrg 		*method, *file, *query, *proto));
    323   1.1    tls }
    324   1.1    tls 
    325   1.1    tls /*
    326  1.16    mrg  * cleanup a bozo_httpreq_t after use
    327   1.1    tls  */
    328  1.16    mrg void
    329  1.16    mrg bozo_clean_request(bozo_httpreq_t *request)
    330   1.1    tls {
    331  1.16    mrg 	struct bozoheaders *hdr, *ohdr = NULL;
    332  1.12    mrg 
    333  1.12    mrg 	if (request == NULL)
    334  1.12    mrg 		return;
    335  1.12    mrg 
    336  1.21    mrg 	/* If SSL enabled cleanup SSL structure. */
    337  1.22    mrg 	bozo_ssl_destroy(request->hr_httpd);
    338  1.21    mrg 
    339  1.12    mrg 	/* clean up request */
    340  1.12    mrg #define MF(x)	if (request->x) free(request->x)
    341  1.12    mrg 	MF(hr_remotehost);
    342  1.12    mrg 	MF(hr_remoteaddr);
    343  1.12    mrg 	MF(hr_serverport);
    344  1.12    mrg 	MF(hr_file);
    345  1.20    mrg 	MF(hr_oldfile);
    346  1.12    mrg 	MF(hr_query);
    347  1.12    mrg #undef MF
    348  1.16    mrg 	bozo_auth_cleanup(request);
    349  1.12    mrg 	for (hdr = SIMPLEQ_FIRST(&request->hr_headers); hdr;
    350  1.12    mrg 	    hdr = SIMPLEQ_NEXT(hdr, h_next)) {
    351  1.12    mrg 		free(hdr->h_value);
    352  1.12    mrg 		free(hdr->h_header);
    353  1.12    mrg 		if (ohdr)
    354  1.12    mrg 			free(ohdr);
    355  1.12    mrg 		ohdr = hdr;
    356  1.12    mrg 	}
    357  1.12    mrg 	if (ohdr)
    358  1.12    mrg 		free(ohdr);
    359  1.12    mrg 
    360  1.12    mrg 	free(request);
    361  1.12    mrg }
    362  1.12    mrg 
    363  1.12    mrg /*
    364  1.16    mrg  * send a HTTP/1.1 408 response if we timeout.
    365  1.16    mrg  */
    366  1.16    mrg /* ARGSUSED */
    367  1.16    mrg static void
    368  1.16    mrg alarmer(int sig)
    369  1.16    mrg {
    370  1.16    mrg 	alarmhit = 1;
    371  1.16    mrg }
    372  1.16    mrg 
    373  1.16    mrg /*
    374  1.16    mrg  * add or merge this header (val: str) into the requests list
    375  1.16    mrg  */
    376  1.16    mrg static bozoheaders_t *
    377  1.16    mrg addmerge_header(bozo_httpreq_t *request, char *val,
    378  1.16    mrg 		char *str, ssize_t len)
    379  1.16    mrg {
    380  1.16    mrg 	struct	bozoheaders *hdr;
    381  1.16    mrg 
    382  1.16    mrg 	USE_ARG(len);
    383  1.16    mrg 	/* do we exist already? */
    384  1.16    mrg 	SIMPLEQ_FOREACH(hdr, &request->hr_headers, h_next) {
    385  1.16    mrg 		if (strcasecmp(val, hdr->h_header) == 0)
    386  1.16    mrg 			break;
    387  1.16    mrg 	}
    388  1.16    mrg 
    389  1.16    mrg 	if (hdr) {
    390  1.16    mrg 		/* yup, merge it in */
    391  1.16    mrg 		char *nval;
    392  1.16    mrg 
    393  1.16    mrg 		if (asprintf(&nval, "%s, %s", hdr->h_value, str) == -1) {
    394  1.16    mrg 			(void)bozo_http_error(request->hr_httpd, 500, NULL,
    395  1.16    mrg 			     "memory allocation failure");
    396  1.16    mrg 			return NULL;
    397  1.16    mrg 		}
    398  1.16    mrg 		free(hdr->h_value);
    399  1.16    mrg 		hdr->h_value = nval;
    400  1.16    mrg 	} else {
    401  1.16    mrg 		/* nope, create a new one */
    402  1.16    mrg 
    403  1.16    mrg 		hdr = bozomalloc(request->hr_httpd, sizeof *hdr);
    404  1.16    mrg 		hdr->h_header = bozostrdup(request->hr_httpd, val);
    405  1.16    mrg 		if (str && *str)
    406  1.16    mrg 			hdr->h_value = bozostrdup(request->hr_httpd, str);
    407  1.16    mrg 		else
    408  1.16    mrg 			hdr->h_value = bozostrdup(request->hr_httpd, " ");
    409  1.16    mrg 
    410  1.16    mrg 		SIMPLEQ_INSERT_TAIL(&request->hr_headers, hdr, h_next);
    411  1.16    mrg 		request->hr_nheaders++;
    412  1.16    mrg 	}
    413  1.16    mrg 
    414  1.16    mrg 	return hdr;
    415  1.16    mrg }
    416  1.16    mrg 
    417  1.16    mrg /*
    418  1.16    mrg  * as the prototype string is not constant (eg, "HTTP/1.1" is equivalent
    419  1.16    mrg  * to "HTTP/001.01"), we MUST parse this.
    420  1.16    mrg  */
    421  1.16    mrg static int
    422  1.16    mrg process_proto(bozo_httpreq_t *request, const char *proto)
    423  1.16    mrg {
    424  1.16    mrg 	char	majorstr[16], *minorstr;
    425  1.16    mrg 	int	majorint, minorint;
    426  1.16    mrg 
    427  1.16    mrg 	if (proto == NULL) {
    428  1.16    mrg got_proto_09:
    429  1.16    mrg 		request->hr_proto = request->hr_httpd->consts.http_09;
    430  1.16    mrg 		debug((request->hr_httpd, DEBUG_FAT, "request %s is http/0.9",
    431  1.16    mrg 			request->hr_file));
    432  1.16    mrg 		return 0;
    433  1.16    mrg 	}
    434  1.16    mrg 
    435  1.16    mrg 	if (strncasecmp(proto, "HTTP/", 5) != 0)
    436  1.16    mrg 		goto bad;
    437  1.16    mrg 	strncpy(majorstr, proto + 5, sizeof majorstr);
    438  1.16    mrg 	majorstr[sizeof(majorstr)-1] = 0;
    439  1.16    mrg 	minorstr = strchr(majorstr, '.');
    440  1.16    mrg 	if (minorstr == NULL)
    441  1.16    mrg 		goto bad;
    442  1.16    mrg 	*minorstr++ = 0;
    443  1.16    mrg 
    444  1.16    mrg 	majorint = atoi(majorstr);
    445  1.16    mrg 	minorint = atoi(minorstr);
    446  1.16    mrg 
    447  1.16    mrg 	switch (majorint) {
    448  1.16    mrg 	case 0:
    449  1.16    mrg 		if (minorint != 9)
    450  1.16    mrg 			break;
    451  1.16    mrg 		goto got_proto_09;
    452  1.16    mrg 	case 1:
    453  1.16    mrg 		if (minorint == 0)
    454  1.16    mrg 			request->hr_proto = request->hr_httpd->consts.http_10;
    455  1.16    mrg 		else if (minorint == 1)
    456  1.16    mrg 			request->hr_proto = request->hr_httpd->consts.http_11;
    457  1.16    mrg 		else
    458  1.16    mrg 			break;
    459  1.16    mrg 
    460  1.16    mrg 		debug((request->hr_httpd, DEBUG_FAT, "request %s is %s",
    461  1.16    mrg 		    request->hr_file, request->hr_proto));
    462  1.16    mrg 		SIMPLEQ_INIT(&request->hr_headers);
    463  1.16    mrg 		request->hr_nheaders = 0;
    464  1.16    mrg 		return 0;
    465  1.16    mrg 	}
    466  1.16    mrg bad:
    467  1.16    mrg 	return bozo_http_error(request->hr_httpd, 404, NULL, "unknown prototype");
    468  1.16    mrg }
    469  1.16    mrg 
    470  1.16    mrg /*
    471  1.16    mrg  * process each type of HTTP method, setting this HTTP requests
    472  1.16    mrg  # method type.
    473  1.16    mrg  */
    474  1.16    mrg static struct method_map {
    475  1.16    mrg 	const char *name;
    476  1.16    mrg 	int	type;
    477  1.16    mrg } method_map[] = {
    478  1.16    mrg 	{ "GET", 	HTTP_GET, },
    479  1.16    mrg 	{ "POST",	HTTP_POST, },
    480  1.16    mrg 	{ "HEAD",	HTTP_HEAD, },
    481  1.16    mrg #if 0	/* other non-required http/1.1 methods */
    482  1.16    mrg 	{ "OPTIONS",	HTTP_OPTIONS, },
    483  1.16    mrg 	{ "PUT",	HTTP_PUT, },
    484  1.16    mrg 	{ "DELETE",	HTTP_DELETE, },
    485  1.16    mrg 	{ "TRACE",	HTTP_TRACE, },
    486  1.16    mrg 	{ "CONNECT",	HTTP_CONNECT, },
    487  1.16    mrg #endif
    488  1.16    mrg 	{ NULL,		0, },
    489  1.16    mrg };
    490  1.16    mrg 
    491  1.16    mrg static int
    492  1.16    mrg process_method(bozo_httpreq_t *request, const char *method)
    493  1.16    mrg {
    494  1.16    mrg 	struct	method_map *mmp;
    495  1.16    mrg 
    496  1.16    mrg 	if (request->hr_proto == request->hr_httpd->consts.http_11)
    497  1.16    mrg 		request->hr_allow = "GET, HEAD, POST";
    498  1.16    mrg 
    499  1.16    mrg 	for (mmp = method_map; mmp->name; mmp++)
    500  1.16    mrg 		if (strcasecmp(method, mmp->name) == 0) {
    501  1.16    mrg 			request->hr_method = mmp->type;
    502  1.16    mrg 			request->hr_methodstr = mmp->name;
    503  1.16    mrg 			return 0;
    504  1.16    mrg 		}
    505  1.16    mrg 
    506  1.16    mrg 	return bozo_http_error(request->hr_httpd, 404, request, "unknown method");
    507  1.16    mrg }
    508  1.16    mrg 
    509  1.16    mrg /*
    510   1.1    tls  * This function reads a http request from stdin, returning a pointer to a
    511  1.16    mrg  * bozo_httpreq_t structure, describing the request.
    512   1.1    tls  */
    513  1.16    mrg bozo_httpreq_t *
    514  1.16    mrg bozo_read_request(bozohttpd_t *httpd)
    515   1.1    tls {
    516   1.1    tls 	struct	sigaction	sa;
    517   1.9    tls 	char	*str, *val, *method, *file, *proto, *query;
    518   1.1    tls 	char	*host, *addr, *port;
    519   1.1    tls 	char	bufport[10];
    520   1.1    tls 	char	hbuf[NI_MAXHOST], abuf[NI_MAXHOST];
    521   1.1    tls 	struct	sockaddr_storage ss;
    522   1.1    tls 	ssize_t	len;
    523   1.1    tls 	int	line = 0;
    524   1.1    tls 	socklen_t slen;
    525  1.16    mrg 	bozo_httpreq_t *request;
    526   1.1    tls 
    527   1.1    tls 	/*
    528  1.20    mrg 	 * if we're in daemon mode, bozo_daemon_fork() will return here twice
    529  1.20    mrg 	 * for each call.  once in the child, returning 0, and once in the
    530  1.20    mrg 	 * parent, returning 1.  for each child, then we can setup SSL, and
    531  1.20    mrg 	 * the parent can signal the caller there was no request to process
    532  1.20    mrg 	 * and it will wait for another.
    533   1.1    tls 	 */
    534  1.20    mrg 	if (bozo_daemon_fork(httpd))
    535  1.20    mrg 		return NULL;
    536  1.16    mrg 	bozo_ssl_accept(httpd);
    537   1.1    tls 
    538  1.16    mrg 	request = bozomalloc(httpd, sizeof(*request));
    539  1.16    mrg 	memset(request, 0, sizeof(*request));
    540  1.16    mrg 	request->hr_httpd = httpd;
    541   1.1    tls 	request->hr_allow = request->hr_host = NULL;
    542   1.1    tls 	request->hr_content_type = request->hr_content_length = NULL;
    543   1.6    mrg 	request->hr_range = NULL;
    544  1.12    mrg 	request->hr_last_byte_pos = -1;
    545  1.10  joerg 	request->hr_if_modified_since = NULL;
    546  1.12    mrg 	request->hr_file = NULL;
    547  1.20    mrg 	request->hr_oldfile = NULL;
    548   1.1    tls 
    549   1.1    tls 	slen = sizeof(ss);
    550  1.16    mrg 	if (getpeername(0, (struct sockaddr *)(void *)&ss, &slen) < 0)
    551   1.1    tls 		host = addr = NULL;
    552   1.1    tls 	else {
    553  1.16    mrg 		if (getnameinfo((struct sockaddr *)(void *)&ss, slen,
    554   1.1    tls 		    abuf, sizeof abuf, NULL, 0, NI_NUMERICHOST) == 0)
    555   1.1    tls 			addr = abuf;
    556   1.1    tls 		else
    557   1.1    tls 			addr = NULL;
    558  1.16    mrg 		if (httpd->numeric == 0 &&
    559  1.16    mrg 		    getnameinfo((struct sockaddr *)(void *)&ss, slen,
    560  1.16    mrg 				hbuf, sizeof hbuf, NULL, 0, 0) == 0)
    561   1.1    tls 			host = hbuf;
    562   1.1    tls 		else
    563   1.1    tls 			host = NULL;
    564   1.1    tls 	}
    565   1.1    tls 	if (host != NULL)
    566  1.16    mrg 		request->hr_remotehost = bozostrdup(request->hr_httpd, host);
    567   1.1    tls 	if (addr != NULL)
    568  1.16    mrg 		request->hr_remoteaddr = bozostrdup(request->hr_httpd, addr);
    569   1.1    tls 	slen = sizeof(ss);
    570  1.16    mrg 	if (getsockname(0, (struct sockaddr *)(void *)&ss, &slen) < 0)
    571   1.1    tls 		port = NULL;
    572   1.1    tls 	else {
    573  1.16    mrg 		if (getnameinfo((struct sockaddr *)(void *)&ss, slen, NULL, 0,
    574  1.16    mrg 				bufport, sizeof bufport, NI_NUMERICSERV) == 0)
    575   1.1    tls 			port = bufport;
    576   1.1    tls 		else
    577   1.1    tls 			port = NULL;
    578   1.1    tls 	}
    579   1.1    tls 	if (port != NULL)
    580  1.16    mrg 		request->hr_serverport = bozostrdup(request->hr_httpd, port);
    581   1.1    tls 
    582   1.1    tls 	/*
    583   1.1    tls 	 * setup a timer to make sure the request is not hung
    584   1.1    tls 	 */
    585   1.1    tls 	sa.sa_handler = alarmer;
    586   1.1    tls 	sigemptyset(&sa.sa_mask);
    587   1.1    tls 	sigaddset(&sa.sa_mask, SIGALRM);
    588   1.1    tls 	sa.sa_flags = 0;
    589   1.1    tls 	sigaction(SIGALRM, &sa, NULL);	/* XXX */
    590   1.1    tls 
    591   1.1    tls 	alarm(MAX_WAIT_TIME);
    592  1.16    mrg 	while ((str = bozodgetln(httpd, STDIN_FILENO, &len, bozo_read)) != NULL) {
    593   1.1    tls 		alarm(0);
    594  1.12    mrg 		if (alarmhit) {
    595  1.16    mrg 			(void)bozo_http_error(httpd, 408, NULL,
    596  1.16    mrg 					"request timed out");
    597  1.12    mrg 			goto cleanup;
    598  1.12    mrg 		}
    599   1.1    tls 		line++;
    600   1.1    tls 
    601   1.1    tls 		if (line == 1) {
    602   1.1    tls 
    603  1.12    mrg 			if (len < 1) {
    604  1.16    mrg 				(void)bozo_http_error(httpd, 404, NULL,
    605  1.16    mrg 						"null method");
    606  1.12    mrg 				goto cleanup;
    607  1.12    mrg 			}
    608  1.12    mrg 
    609  1.16    mrg 			bozo_warn(httpd, "got request ``%s'' from host %s to port %s",
    610  1.16    mrg 				str,
    611  1.16    mrg 				host ? host : addr ? addr : "<local>",
    612  1.16    mrg 				port ? port : "<stdin>");
    613  1.12    mrg #if 0
    614  1.16    mrg 			debug((httpd, DEBUG_FAT,
    615  1.16    mrg 				"read_req, getting request: ``%s''", str));
    616  1.12    mrg #endif
    617   1.1    tls 
    618  1.12    mrg 			/* we allocate return space in file and query only */
    619  1.16    mrg 			parse_request(httpd, str, &method, &file, &query, &proto);
    620  1.12    mrg 			request->hr_file = file;
    621  1.12    mrg 			request->hr_query = query;
    622  1.12    mrg 			if (method == NULL) {
    623  1.16    mrg 				(void)bozo_http_error(httpd, 404, NULL,
    624  1.16    mrg 						"null method");
    625  1.12    mrg 				goto cleanup;
    626  1.12    mrg 			}
    627  1.12    mrg 			if (file == NULL) {
    628  1.16    mrg 				(void)bozo_http_error(httpd, 404, NULL,
    629  1.16    mrg 						"null file");
    630  1.12    mrg 				goto cleanup;
    631  1.12    mrg 			}
    632   1.1    tls 
    633   1.1    tls 			/*
    634   1.1    tls 			 * note that we parse the proto first, so that we
    635   1.1    tls 			 * can more properly parse the method and the url.
    636   1.1    tls 			 */
    637   1.9    tls 
    638  1.12    mrg 			if (process_proto(request, proto) ||
    639  1.12    mrg 			    process_method(request, method)) {
    640  1.12    mrg 				goto cleanup;
    641  1.12    mrg 			}
    642  1.12    mrg 
    643  1.16    mrg 			debug((httpd, DEBUG_FAT, "got file \"%s\" query \"%s\"",
    644  1.12    mrg 			    request->hr_file,
    645  1.12    mrg 			    request->hr_query ? request->hr_query : "<none>"));
    646   1.1    tls 
    647   1.1    tls 			/* http/0.9 has no header processing */
    648  1.16    mrg 			if (request->hr_proto == httpd->consts.http_09)
    649   1.1    tls 				break;
    650   1.1    tls 		} else {		/* incoming headers */
    651  1.16    mrg 			bozoheaders_t *hdr;
    652   1.1    tls 
    653   1.1    tls 			if (*str == '\0')
    654   1.1    tls 				break;
    655   1.1    tls 
    656   1.6    mrg 			val = bozostrnsep(&str, ":", &len);
    657  1.16    mrg 			debug((httpd, DEBUG_EXPLODING,
    658   1.6    mrg 			    "read_req2: after bozostrnsep: str ``%s'' val ``%s''",
    659   1.1    tls 			    str, val));
    660  1.12    mrg 			if (val == NULL || len == -1) {
    661  1.16    mrg 				(void)bozo_http_error(httpd, 404, request,
    662  1.16    mrg 						"no header");
    663  1.12    mrg 				goto cleanup;
    664  1.12    mrg 			}
    665   1.1    tls 			while (*str == ' ' || *str == '\t')
    666   1.1    tls 				len--, str++;
    667   1.6    mrg 			while (*val == ' ' || *val == '\t')
    668   1.6    mrg 				val++;
    669   1.1    tls 
    670  1.17    mrg 			if (bozo_auth_check_headers(request, val, str, len))
    671   1.1    tls 				goto next_header;
    672   1.1    tls 
    673   1.1    tls 			hdr = addmerge_header(request, val, str, len);
    674   1.1    tls 
    675   1.1    tls 			if (strcasecmp(hdr->h_header, "content-type") == 0)
    676   1.1    tls 				request->hr_content_type = hdr->h_value;
    677   1.1    tls 			else if (strcasecmp(hdr->h_header, "content-length") == 0)
    678   1.1    tls 				request->hr_content_length = hdr->h_value;
    679   1.1    tls 			else if (strcasecmp(hdr->h_header, "host") == 0)
    680   1.1    tls 				request->hr_host = hdr->h_value;
    681   1.1    tls 			/* HTTP/1.1 rev06 draft spec: 14.20 */
    682  1.12    mrg 			else if (strcasecmp(hdr->h_header, "expect") == 0) {
    683  1.16    mrg 				(void)bozo_http_error(httpd, 417, request,
    684  1.16    mrg 						"we don't support Expect:");
    685  1.12    mrg 				goto cleanup;
    686  1.12    mrg 			}
    687   1.1    tls 			else if (strcasecmp(hdr->h_header, "referrer") == 0 ||
    688   1.1    tls 			         strcasecmp(hdr->h_header, "referer") == 0)
    689   1.1    tls 				request->hr_referrer = hdr->h_value;
    690   1.6    mrg 			else if (strcasecmp(hdr->h_header, "range") == 0)
    691   1.6    mrg 				request->hr_range = hdr->h_value;
    692  1.16    mrg 			else if (strcasecmp(hdr->h_header,
    693  1.16    mrg 					"if-modified-since") == 0)
    694  1.10  joerg 				request->hr_if_modified_since = hdr->h_value;
    695   1.1    tls 
    696  1.16    mrg 			debug((httpd, DEBUG_FAT, "adding header %s: %s",
    697   1.1    tls 			    hdr->h_header, hdr->h_value));
    698   1.1    tls 		}
    699   1.1    tls next_header:
    700   1.1    tls 		alarm(MAX_WAIT_TIME);
    701   1.1    tls 	}
    702   1.1    tls 
    703   1.1    tls 	/* now, clear it all out */
    704   1.1    tls 	alarm(0);
    705   1.1    tls 	signal(SIGALRM, SIG_DFL);
    706   1.1    tls 
    707   1.1    tls 	/* RFC1945, 8.3 */
    708  1.16    mrg 	if (request->hr_method == HTTP_POST &&
    709  1.16    mrg 	    request->hr_content_length == NULL) {
    710  1.16    mrg 		(void)bozo_http_error(httpd, 400, request,
    711  1.16    mrg 				"missing content length");
    712  1.12    mrg 		goto cleanup;
    713  1.12    mrg 	}
    714   1.1    tls 
    715   1.1    tls 	/* HTTP/1.1 draft rev-06, 14.23 & 19.6.1.1 */
    716  1.16    mrg 	if (request->hr_proto == httpd->consts.http_11 &&
    717  1.16    mrg 	    request->hr_host == NULL) {
    718  1.16    mrg 		(void)bozo_http_error(httpd, 400, request,
    719  1.16    mrg 				"missing Host header");
    720  1.12    mrg 		goto cleanup;
    721  1.12    mrg 	}
    722  1.12    mrg 
    723  1.12    mrg 	if (request->hr_range != NULL) {
    724  1.16    mrg 		debug((httpd, DEBUG_FAT, "hr_range: %s", request->hr_range));
    725  1.12    mrg 		/* support only simple ranges %d- and %d-%d */
    726  1.12    mrg 		if (strchr(request->hr_range, ',') == NULL) {
    727  1.12    mrg 			const char *rstart, *dash;
    728  1.12    mrg 
    729  1.12    mrg 			rstart = strchr(request->hr_range, '=');
    730  1.12    mrg 			if (rstart != NULL) {
    731  1.12    mrg 				rstart++;
    732  1.12    mrg 				dash = strchr(rstart, '-');
    733  1.12    mrg 				if (dash != NULL && dash != rstart) {
    734  1.12    mrg 					dash++;
    735  1.12    mrg 					request->hr_have_range = 1;
    736  1.12    mrg 					request->hr_first_byte_pos =
    737  1.12    mrg 					    strtoll(rstart, NULL, 10);
    738  1.12    mrg 					if (request->hr_first_byte_pos < 0)
    739  1.12    mrg 						request->hr_first_byte_pos = 0;
    740  1.12    mrg 					if (*dash != '\0') {
    741  1.12    mrg 						request->hr_last_byte_pos =
    742  1.12    mrg 						    strtoll(dash, NULL, 10);
    743  1.12    mrg 						if (request->hr_last_byte_pos < 0)
    744  1.12    mrg 							request->hr_last_byte_pos = -1;
    745  1.12    mrg 					}
    746  1.12    mrg 				}
    747  1.12    mrg 			}
    748  1.12    mrg 		}
    749  1.12    mrg 	}
    750   1.1    tls 
    751  1.16    mrg 	debug((httpd, DEBUG_FAT, "bozo_read_request returns url %s in request",
    752  1.16    mrg 	       request->hr_file));
    753  1.16    mrg 	return request;
    754   1.1    tls 
    755  1.16    mrg cleanup:
    756  1.16    mrg 	bozo_clean_request(request);
    757   1.1    tls 
    758  1.16    mrg 	return NULL;
    759   1.1    tls }
    760   1.1    tls 
    761  1.10  joerg static int
    762  1.16    mrg mmap_and_write_part(bozohttpd_t *httpd, int fd, off_t first_byte_pos, size_t sz)
    763  1.12    mrg {
    764  1.14    mrg 	size_t mappedsz, wroffset;
    765  1.14    mrg 	off_t mappedoffset;
    766  1.12    mrg 	char *addr;
    767  1.14    mrg 	void *mappedaddr;
    768  1.14    mrg 
    769  1.14    mrg 	/*
    770  1.14    mrg 	 * we need to ensure that both the size *and* offset arguments to
    771  1.14    mrg 	 * mmap() are page-aligned.  our formala for this is:
    772  1.14    mrg 	 *
    773  1.14    mrg 	 *    input offset: first_byte_pos
    774  1.14    mrg 	 *    input size: sz
    775  1.14    mrg 	 *
    776  1.14    mrg 	 *    mapped offset = page align truncate (input offset)
    777  1.14    mrg 	 *    mapped size   =
    778  1.14    mrg 	 *        page align extend (input offset - mapped offset + input size)
    779  1.14    mrg 	 *    write offset  = input offset - mapped offset
    780  1.14    mrg 	 *
    781  1.14    mrg 	 * we use the write offset in all writes
    782  1.14    mrg 	 */
    783  1.16    mrg 	mappedoffset = first_byte_pos & ~(httpd->page_size - 1);
    784  1.16    mrg 	mappedsz = (size_t)
    785  1.16    mrg 		(first_byte_pos - mappedoffset + sz + httpd->page_size - 1) &
    786  1.16    mrg 		~(httpd->page_size - 1);
    787  1.16    mrg 	wroffset = (size_t)(first_byte_pos - mappedoffset);
    788  1.12    mrg 
    789  1.14    mrg 	addr = mmap(0, mappedsz, PROT_READ, MAP_SHARED, fd, mappedoffset);
    790  1.12    mrg 	if (addr == (char *)-1) {
    791  1.16    mrg 		bozo_warn(httpd, "mmap failed: %s", strerror(errno));
    792  1.12    mrg 		return -1;
    793  1.12    mrg 	}
    794  1.14    mrg 	mappedaddr = addr;
    795  1.12    mrg 
    796  1.12    mrg #ifdef MADV_SEQUENTIAL
    797  1.12    mrg 	(void)madvise(addr, sz, MADV_SEQUENTIAL);
    798  1.12    mrg #endif
    799  1.16    mrg 	while (sz > BOZO_WRSZ) {
    800  1.16    mrg 		if (bozo_write(httpd, STDOUT_FILENO, addr + wroffset,
    801  1.16    mrg 				BOZO_WRSZ) != BOZO_WRSZ) {
    802  1.16    mrg 			bozo_warn(httpd, "write failed: %s", strerror(errno));
    803  1.12    mrg 			goto out;
    804  1.12    mrg 		}
    805  1.16    mrg 		debug((httpd, DEBUG_OBESE, "wrote %d bytes", BOZO_WRSZ));
    806  1.16    mrg 		sz -= BOZO_WRSZ;
    807  1.16    mrg 		addr += BOZO_WRSZ;
    808  1.16    mrg 	}
    809  1.16    mrg 	if (sz && (size_t)bozo_write(httpd, STDOUT_FILENO, addr + wroffset,
    810  1.16    mrg 				sz) != sz) {
    811  1.16    mrg 		bozo_warn(httpd, "final write failed: %s", strerror(errno));
    812  1.12    mrg 		goto out;
    813  1.12    mrg 	}
    814  1.16    mrg 	debug((httpd, DEBUG_OBESE, "wrote %d bytes", (int)sz));
    815  1.12    mrg  out:
    816  1.14    mrg 	if (munmap(mappedaddr, mappedsz) < 0) {
    817  1.16    mrg 		bozo_warn(httpd, "munmap failed");
    818  1.12    mrg 		return -1;
    819  1.12    mrg 	}
    820  1.12    mrg 
    821  1.12    mrg 	return 0;
    822  1.12    mrg }
    823  1.12    mrg 
    824  1.12    mrg static int
    825  1.10  joerg parse_http_date(const char *val, time_t *timestamp)
    826  1.10  joerg {
    827  1.10  joerg 	char *remainder;
    828  1.10  joerg 	struct tm tm;
    829  1.10  joerg 
    830  1.10  joerg 	if ((remainder = strptime(val, "%a, %d %b %Y %T GMT", &tm)) == NULL &&
    831  1.10  joerg 	    (remainder = strptime(val, "%a, %d-%b-%y %T GMT", &tm)) == NULL &&
    832  1.10  joerg 	    (remainder = strptime(val, "%a %b %d %T %Y", &tm)) == NULL)
    833  1.10  joerg 		return 0; /* Invalid HTTP date format */
    834  1.10  joerg 
    835  1.10  joerg 	if (*remainder)
    836  1.10  joerg 		return 0; /* No trailing garbage */
    837  1.10  joerg 
    838  1.10  joerg 	*timestamp = timegm(&tm);
    839  1.10  joerg 	return 1;
    840  1.10  joerg }
    841  1.10  joerg 
    842   1.1    tls /*
    843  1.16    mrg  * checks to see if this request has a valid .bzdirect file.  returns
    844  1.16    mrg  * 0 on failure and 1 on success.
    845   1.1    tls  */
    846  1.16    mrg static int
    847  1.16    mrg check_direct_access(bozo_httpreq_t *request)
    848   1.1    tls {
    849  1.16    mrg 	FILE *fp;
    850  1.16    mrg 	struct stat sb;
    851  1.16    mrg 	char dir[MAXPATHLEN], dirfile[MAXPATHLEN], *basename;
    852   1.1    tls 
    853  1.16    mrg 	snprintf(dir, sizeof(dir), "%s", request->hr_file + 1);
    854  1.17    mrg 	debug((request->hr_httpd, DEBUG_FAT, "check_bzredirect: dir %s", dir));
    855  1.16    mrg 	basename = strrchr(dir, '/');
    856  1.12    mrg 
    857  1.16    mrg 	if ((!basename || basename[1] != '\0') &&
    858  1.16    mrg 	    lstat(dir, &sb) == 0 && S_ISDIR(sb.st_mode))
    859  1.16    mrg 		/* nothing */;
    860  1.16    mrg 	else if (basename == NULL)
    861  1.16    mrg 		strcpy(dir, ".");
    862  1.16    mrg 	else {
    863  1.16    mrg 		*basename++ = '\0';
    864  1.16    mrg 		bozo_check_special_files(request, basename);
    865  1.12    mrg 	}
    866  1.12    mrg 
    867  1.16    mrg 	snprintf(dirfile, sizeof(dirfile), "%s/%s", dir, DIRECT_ACCESS_FILE);
    868  1.16    mrg 	if (stat(dirfile, &sb) < 0 ||
    869  1.16    mrg 	    (fp = fopen(dirfile, "r")) == NULL)
    870  1.16    mrg 		return 0;
    871  1.16    mrg 	fclose(fp);
    872  1.16    mrg 	return 1;
    873  1.16    mrg }
    874   1.1    tls 
    875  1.16    mrg /*
    876  1.16    mrg  * do automatic redirection -- if there are query parameters for the URL
    877  1.16    mrg  * we will tack these on to the new (redirected) URL.
    878  1.16    mrg  */
    879  1.16    mrg static void
    880  1.16    mrg handle_redirect(bozo_httpreq_t *request,
    881  1.16    mrg 		const char *url, int absolute)
    882  1.16    mrg {
    883  1.16    mrg 	bozohttpd_t *httpd = request->hr_httpd;
    884  1.16    mrg 	char *urlbuf;
    885  1.16    mrg 	char portbuf[20];
    886  1.16    mrg 	int query = 0;
    887  1.16    mrg 
    888  1.16    mrg 	if (url == NULL) {
    889  1.16    mrg 		if (asprintf(&urlbuf, "/%s/", request->hr_file) < 0)
    890  1.16    mrg 			bozo_err(httpd, 1, "asprintf");
    891  1.16    mrg 		url = urlbuf;
    892  1.16    mrg 	} else
    893  1.16    mrg 		urlbuf = NULL;
    894   1.1    tls 
    895  1.16    mrg 	if (request->hr_query && strlen(request->hr_query)) {
    896  1.16    mrg 		query = 1;
    897   1.1    tls 	}
    898   1.1    tls 
    899  1.16    mrg 	if (request->hr_serverport && strcmp(request->hr_serverport, "80") != 0)
    900  1.16    mrg 		snprintf(portbuf, sizeof(portbuf), ":%s",
    901  1.16    mrg 		    request->hr_serverport);
    902  1.16    mrg 	else
    903  1.16    mrg 		portbuf[0] = '\0';
    904  1.16    mrg 	bozo_warn(httpd, "redirecting %s%s%s", httpd->virthostname, portbuf, url);
    905  1.16    mrg 	debug((httpd, DEBUG_FAT, "redirecting %s", url));
    906  1.16    mrg 	bozo_printf(httpd, "%s 301 Document Moved\r\n", request->hr_proto);
    907  1.16    mrg 	if (request->hr_proto != httpd->consts.http_09)
    908  1.16    mrg 		bozo_print_header(request, NULL, "text/html", NULL);
    909  1.16    mrg 	if (request->hr_proto != httpd->consts.http_09) {
    910  1.16    mrg 		bozo_printf(httpd, "Location: http://");
    911  1.16    mrg 		if (absolute == 0)
    912  1.16    mrg 			bozo_printf(httpd, "%s%s", httpd->virthostname, portbuf);
    913  1.16    mrg 		if (query) {
    914  1.16    mrg 		  bozo_printf(httpd, "%s?%s\r\n", url, request->hr_query);
    915  1.16    mrg 		} else {
    916  1.16    mrg 		  bozo_printf(httpd, "%s\r\n", url);
    917  1.12    mrg 		}
    918   1.1    tls 	}
    919  1.16    mrg 	bozo_printf(httpd, "\r\n");
    920  1.16    mrg 	if (request->hr_method == HTTP_HEAD)
    921  1.16    mrg 		goto head;
    922  1.16    mrg 	bozo_printf(httpd, "<html><head><title>Document Moved</title></head>\n");
    923  1.16    mrg 	bozo_printf(httpd, "<body><h1>Document Moved</h1>\n");
    924  1.16    mrg 	bozo_printf(httpd, "This document had moved <a href=\"http://");
    925  1.16    mrg 	if (query) {
    926  1.16    mrg 	  if (absolute)
    927  1.16    mrg 	    bozo_printf(httpd, "%s?%s", url, request->hr_query);
    928  1.16    mrg 	  else
    929  1.16    mrg 	    bozo_printf(httpd, "%s%s%s?%s", httpd->virthostname, portbuf, url,
    930  1.16    mrg 	    		request->hr_query);
    931  1.16    mrg         } else {
    932  1.16    mrg 	  if (absolute)
    933  1.16    mrg 	    bozo_printf(httpd, "%s", url);
    934  1.16    mrg 	  else
    935  1.16    mrg 	    bozo_printf(httpd, "%s%s%s", httpd->virthostname, portbuf, url);
    936  1.16    mrg 	}
    937  1.16    mrg 	bozo_printf(httpd, "\">here</a>\n");
    938  1.16    mrg 	bozo_printf(httpd, "</body></html>\n");
    939  1.16    mrg head:
    940  1.16    mrg 	bozo_flush(httpd, stdout);
    941  1.16    mrg 	if (urlbuf)
    942  1.16    mrg 		free(urlbuf);
    943   1.1    tls }
    944   1.1    tls 
    945   1.1    tls /*
    946   1.1    tls  * deal with virtual host names; we do this:
    947  1.16    mrg  *	if we have a virtual path root (httpd->virtbase), and we are given a
    948   1.1    tls  *	virtual host spec (Host: ho.st or http://ho.st/), see if this
    949  1.16    mrg  *	directory exists under httpd->virtbase.  if it does, use this as the
    950   1.1    tls  #	new slashdir.
    951   1.1    tls  */
    952  1.12    mrg static int
    953  1.16    mrg check_virtual(bozo_httpreq_t *request)
    954   1.1    tls {
    955  1.16    mrg 	bozohttpd_t *httpd = request->hr_httpd;
    956   1.9    tls 	char *file = request->hr_file, *s;
    957   1.1    tls 	struct dirent **list;
    958   1.1    tls 	size_t len;
    959   1.1    tls 	int i;
    960   1.1    tls 
    961  1.16    mrg 	if (!httpd->virtbase)
    962   1.1    tls 		goto use_slashdir;
    963   1.1    tls 
    964   1.1    tls 	/*
    965   1.1    tls 	 * convert http://virtual.host/ to request->hr_host
    966   1.1    tls 	 */
    967  1.16    mrg 	debug((httpd, DEBUG_OBESE, "checking for http:// virtual host in ``%s''",
    968  1.16    mrg 			file));
    969   1.9    tls 	if (strncasecmp(file, "http://", 7) == 0) {
    970   1.1    tls 		/* we would do virtual hosting here? */
    971   1.9    tls 		file += 7;
    972   1.9    tls 		s = strchr(file, '/');
    973   1.1    tls 		/* HTTP/1.1 draft rev-06, 5.2: URI takes precedence over Host: */
    974   1.9    tls 		request->hr_host = file;
    975  1.16    mrg 		request->hr_file = bozostrdup(request->hr_httpd, s ? s : "/");
    976  1.16    mrg 		debug((httpd, DEBUG_OBESE, "got host ``%s'' file is now ``%s''",
    977   1.9    tls 		    request->hr_host, request->hr_file));
    978   1.1    tls 	} else if (!request->hr_host)
    979   1.1    tls 		goto use_slashdir;
    980   1.1    tls 
    981   1.1    tls 	/*
    982   1.1    tls 	 * ok, we have a virtual host, use scandir(3) to find a case
    983   1.1    tls 	 * insensitive match for the virtual host we are asked for.
    984   1.1    tls 	 * note that if the virtual host is the same as the master,
    985   1.1    tls 	 * we don't need to do anything special.
    986   1.1    tls 	 */
    987   1.1    tls 	len = strlen(request->hr_host);
    988  1.16    mrg 	debug((httpd, DEBUG_OBESE,
    989  1.16    mrg 	    "check_virtual: checking host `%s' under httpd->virtbase `%s' "
    990  1.16    mrg 	    "for file `%s'",
    991  1.16    mrg 	    request->hr_host, httpd->virtbase, request->hr_file));
    992  1.16    mrg 	if (strncasecmp(httpd->virthostname, request->hr_host, len) != 0) {
    993   1.1    tls 		s = 0;
    994  1.23    mrg 		if ((dirp = opendir(httpd->virtbase)) != NULL) {
    995  1.23    mrg 			while ((d = readdir(dirp)) != NULL) {
    996  1.23    mrg 				if (strcmp(d->d_name, ".") == 0 ||
    997  1.23    mrg 				    strcmp(d->d_name, "..") == 0) {
    998  1.23    mrg 					continue;
    999  1.23    mrg 				}
   1000  1.23    mrg 				debug((httpd, DEBUG_OBESE, "looking at dir``%s''",
   1001  1.23    mrg 			 	   d->d_name));
   1002  1.23    mrg 				if (strncasecmp(d->d_name, request->hr_host,
   1003  1.23    mrg 				    len) == 0) {
   1004  1.23    mrg 					/* found it, punch it */
   1005  1.23    mrg 					debug((httpd, DEBUG_OBESE, "found it punch it"));
   1006  1.23    mrg 					httpd->virthostname = d->d_name;
   1007  1.23    mrg 					if (asprintf(&s, "%s/%s", httpd->virtbase,
   1008  1.23    mrg 					    httpd->virthostname) < 0)
   1009  1.23    mrg 						bozo_err(httpd, 1, "asprintf");
   1010  1.23    mrg 					break;
   1011  1.23    mrg 				}
   1012   1.1    tls 			}
   1013  1.23    mrg 			closedir(dirp);
   1014  1.23    mrg 		}
   1015  1.23    mrg 		else {
   1016  1.23    mrg 			debug((httpd, DEBUG_FAT, "opendir %s failed: %s",
   1017  1.23    mrg 			    httpd->virtbase, strerror(errno)));
   1018   1.1    tls 		}
   1019   1.1    tls 		if (s == 0) {
   1020  1.16    mrg 			if (httpd->unknown_slash)
   1021   1.1    tls 				goto use_slashdir;
   1022  1.16    mrg 			return bozo_http_error(httpd, 404, request,
   1023  1.16    mrg 						"unknown URL");
   1024   1.1    tls 		}
   1025   1.1    tls 	} else
   1026   1.1    tls use_slashdir:
   1027  1.16    mrg 		s = httpd->slashdir;
   1028   1.1    tls 
   1029   1.1    tls 	/*
   1030   1.1    tls 	 * ok, nailed the correct slashdir, chdir to it
   1031   1.1    tls 	 */
   1032   1.1    tls 	if (chdir(s) < 0)
   1033  1.16    mrg 		return bozo_http_error(httpd, 404, request,
   1034  1.16    mrg 					"can't chdir to slashdir");
   1035  1.12    mrg 	return 0;
   1036   1.1    tls }
   1037   1.1    tls 
   1038   1.1    tls /*
   1039   1.1    tls  * checks to see if this request has a valid .bzredirect file.  returns
   1040   1.1    tls  * 0 on failure and 1 on success.
   1041   1.1    tls  */
   1042   1.1    tls static void
   1043  1.16    mrg check_bzredirect(bozo_httpreq_t *request)
   1044   1.1    tls {
   1045   1.1    tls 	struct stat sb;
   1046   1.1    tls 	char dir[MAXPATHLEN], redir[MAXPATHLEN], redirpath[MAXPATHLEN + 1];
   1047   1.1    tls 	char *basename, *finalredir;
   1048   1.1    tls 	int rv, absolute;
   1049   1.1    tls 
   1050   1.1    tls 	/*
   1051   1.1    tls 	 * if this pathname is really a directory, but doesn't end in /,
   1052   1.1    tls 	 * use it as the directory to look for the redir file.
   1053   1.1    tls 	 */
   1054   1.9    tls 	snprintf(dir, sizeof(dir), "%s", request->hr_file + 1);
   1055  1.17    mrg 	debug((request->hr_httpd, DEBUG_FAT, "check_bzredirect: dir %s", dir));
   1056   1.1    tls 	basename = strrchr(dir, '/');
   1057   1.1    tls 
   1058   1.1    tls 	if ((!basename || basename[1] != '\0') &&
   1059   1.1    tls 	    lstat(dir, &sb) == 0 && S_ISDIR(sb.st_mode))
   1060   1.1    tls 		/* nothing */;
   1061   1.1    tls 	else if (basename == NULL)
   1062   1.1    tls 		strcpy(dir, ".");
   1063   1.1    tls 	else {
   1064   1.1    tls 		*basename++ = '\0';
   1065  1.16    mrg 		bozo_check_special_files(request, basename);
   1066   1.1    tls 	}
   1067   1.1    tls 
   1068   1.1    tls 	snprintf(redir, sizeof(redir), "%s/%s", dir, REDIRECT_FILE);
   1069   1.1    tls 	if (lstat(redir, &sb) == 0) {
   1070  1.16    mrg 		if (!S_ISLNK(sb.st_mode))
   1071   1.1    tls 			return;
   1072   1.1    tls 		absolute = 0;
   1073   1.1    tls 	} else {
   1074   1.1    tls 		snprintf(redir, sizeof(redir), "%s/%s", dir, ABSREDIRECT_FILE);
   1075  1.16    mrg 		if (lstat(redir, &sb) < 0 || !S_ISLNK(sb.st_mode))
   1076   1.1    tls 			return;
   1077   1.1    tls 		absolute = 1;
   1078   1.1    tls 	}
   1079  1.17    mrg 	debug((request->hr_httpd, DEBUG_FAT,
   1080  1.17    mrg 	       "check_bzredirect: calling readlink"));
   1081   1.1    tls 	rv = readlink(redir, redirpath, sizeof redirpath - 1);
   1082   1.1    tls 	if (rv == -1 || rv == 0) {
   1083  1.17    mrg 		debug((request->hr_httpd, DEBUG_FAT, "readlink failed"));
   1084   1.1    tls 		return;
   1085   1.1    tls 	}
   1086   1.1    tls 	redirpath[rv] = '\0';
   1087  1.17    mrg 	debug((request->hr_httpd, DEBUG_FAT,
   1088  1.17    mrg 	       "readlink returned \"%s\"", redirpath));
   1089   1.1    tls 
   1090   1.1    tls 	/* now we have the link pointer, redirect to the real place */
   1091   1.1    tls 	if (absolute)
   1092   1.1    tls 		finalredir = redirpath;
   1093   1.1    tls 	else
   1094   1.1    tls 		snprintf(finalredir = redir, sizeof(redir), "/%s/%s", dir,
   1095   1.1    tls 			 redirpath);
   1096   1.1    tls 
   1097  1.17    mrg 	debug((request->hr_httpd, DEBUG_FAT,
   1098  1.17    mrg 	       "check_bzredirect: new redir %s", finalredir));
   1099   1.1    tls 	handle_redirect(request, finalredir, absolute);
   1100   1.1    tls }
   1101   1.1    tls 
   1102  1.16    mrg /* this fixes the %HH hack that RFC2396 requires.  */
   1103  1.16    mrg static void
   1104  1.16    mrg fix_url_percent(bozo_httpreq_t *request)
   1105   1.1    tls {
   1106  1.16    mrg 	bozohttpd_t *httpd = request->hr_httpd;
   1107  1.16    mrg 	char	*s, *t, buf[3], *url;
   1108  1.16    mrg 	char	*end;	/* if end is not-zero, we don't translate beyond that */
   1109  1.16    mrg 
   1110  1.16    mrg 	url = request->hr_file;
   1111  1.16    mrg 
   1112  1.16    mrg 	end = url + strlen(url);
   1113  1.16    mrg 
   1114  1.16    mrg 	/* fast forward to the first % */
   1115  1.16    mrg 	if ((s = strchr(url, '%')) == NULL)
   1116  1.16    mrg 		return;
   1117   1.1    tls 
   1118  1.16    mrg 	t = s;
   1119  1.16    mrg 	do {
   1120  1.16    mrg 		if (end && s >= end) {
   1121  1.16    mrg 			debug((httpd, DEBUG_EXPLODING,
   1122  1.16    mrg 				"fu_%%: past end, filling out.."));
   1123  1.16    mrg 			while (*s)
   1124  1.16    mrg 				*t++ = *s++;
   1125  1.16    mrg 			break;
   1126  1.16    mrg 		}
   1127  1.16    mrg 		debug((httpd, DEBUG_EXPLODING,
   1128  1.16    mrg 			"fu_%%: got s == %%, s[1]s[2] == %c%c",
   1129  1.16    mrg 			s[1], s[2]));
   1130  1.16    mrg 		if (s[1] == '\0' || s[2] == '\0') {
   1131  1.16    mrg 			(void)bozo_http_error(httpd, 400, request,
   1132  1.16    mrg 			    "percent hack missing two chars afterwards");
   1133  1.16    mrg 			goto copy_rest;
   1134  1.16    mrg 		}
   1135  1.16    mrg 		if (s[1] == '0' && s[2] == '0') {
   1136  1.16    mrg 			(void)bozo_http_error(httpd, 404, request,
   1137  1.16    mrg 					"percent hack was %00");
   1138  1.16    mrg 			goto copy_rest;
   1139  1.16    mrg 		}
   1140  1.16    mrg 		if (s[1] == '2' && s[2] == 'f') {
   1141  1.16    mrg 			(void)bozo_http_error(httpd, 404, request,
   1142  1.16    mrg 					"percent hack was %2f (/)");
   1143  1.16    mrg 			goto copy_rest;
   1144  1.16    mrg 		}
   1145  1.16    mrg 
   1146  1.16    mrg 		buf[0] = *++s;
   1147  1.16    mrg 		buf[1] = *++s;
   1148  1.16    mrg 		buf[2] = '\0';
   1149  1.16    mrg 		s++;
   1150  1.16    mrg 		*t = (char)strtol(buf, NULL, 16);
   1151  1.16    mrg 		debug((httpd, DEBUG_EXPLODING,
   1152  1.16    mrg 				"fu_%%: strtol put '%02x' into *t", *t));
   1153  1.16    mrg 		if (*t++ == '\0') {
   1154  1.16    mrg 			(void)bozo_http_error(httpd, 400, request,
   1155  1.16    mrg 					"percent hack got a 0 back");
   1156  1.16    mrg 			goto copy_rest;
   1157  1.16    mrg 		}
   1158   1.1    tls 
   1159  1.16    mrg 		while (*s && *s != '%') {
   1160  1.16    mrg 			if (end && s >= end)
   1161  1.16    mrg 				break;
   1162  1.16    mrg 			*t++ = *s++;
   1163  1.16    mrg 		}
   1164  1.16    mrg 	} while (*s);
   1165  1.16    mrg copy_rest:
   1166  1.16    mrg 	while (*s) {
   1167  1.16    mrg 		if (s >= end)
   1168  1.16    mrg 			break;
   1169  1.16    mrg 		*t++ = *s++;
   1170   1.1    tls 	}
   1171  1.16    mrg 	*t = '\0';
   1172  1.16    mrg 	debug((httpd, DEBUG_FAT, "fix_url_percent returns %s in url",
   1173  1.16    mrg 			request->hr_file));
   1174   1.1    tls }
   1175   1.1    tls 
   1176   1.1    tls /*
   1177   1.1    tls  * transform_request does this:
   1178   1.1    tls  *	- ``expand'' %20 crapola
   1179   1.1    tls  *	- punt if it doesn't start with /
   1180  1.16    mrg  *	- check httpd->untrustedref / referrer
   1181   1.1    tls  *	- look for "http://myname/" and deal with it.
   1182  1.16    mrg  *	- maybe call bozo_process_cgi()
   1183  1.16    mrg  *	- check for ~user and call bozo_user_transform() if so
   1184   1.1    tls  *	- if the length > 1, check for trailing slash.  if so,
   1185   1.1    tls  *	  add the index.html file
   1186   1.1    tls  *	- if the length is 1, return the index.html file
   1187   1.1    tls  *	- disallow anything ending up with a file starting
   1188   1.1    tls  *	  at "/" or having ".." in it.
   1189   1.1    tls  *	- anything else is a really weird internal error
   1190  1.12    mrg  *	- returns malloced file to serve, if unhandled
   1191   1.1    tls  */
   1192  1.16    mrg static int
   1193  1.16    mrg transform_request(bozo_httpreq_t *request, int *isindex)
   1194   1.1    tls {
   1195  1.16    mrg 	bozohttpd_t *httpd = request->hr_httpd;
   1196  1.12    mrg 	char	*file, *newfile = NULL;
   1197   1.1    tls 	size_t	len;
   1198   1.1    tls 
   1199  1.12    mrg 	file = NULL;
   1200   1.1    tls 	*isindex = 0;
   1201  1.16    mrg 	debug((httpd, DEBUG_FAT, "tf_req: file %s", request->hr_file));
   1202   1.1    tls 	fix_url_percent(request);
   1203  1.12    mrg 	if (check_virtual(request)) {
   1204  1.12    mrg 		goto bad_done;
   1205  1.12    mrg 	}
   1206  1.12    mrg 	file = request->hr_file;
   1207   1.1    tls 
   1208  1.12    mrg 	if (file[0] != '/') {
   1209  1.16    mrg 		(void)bozo_http_error(httpd, 404, request, "unknown URL");
   1210  1.12    mrg 		goto bad_done;
   1211  1.12    mrg 	}
   1212   1.1    tls 
   1213   1.1    tls 	check_bzredirect(request);
   1214   1.1    tls 
   1215  1.16    mrg 	if (httpd->untrustedref) {
   1216   1.1    tls 		int to_indexhtml = 0;
   1217   1.1    tls 
   1218   1.1    tls #define TOP_PAGE(x)	(strcmp((x), "/") == 0 || \
   1219  1.16    mrg 			 strcmp((x) + 1, httpd->index_html) == 0 || \
   1220   1.1    tls 			 strcmp((x) + 1, "favicon.ico") == 0)
   1221   1.1    tls 
   1222  1.16    mrg 		debug((httpd, DEBUG_EXPLODING, "checking httpd->untrustedref"));
   1223   1.1    tls 		/*
   1224   1.1    tls 		 * first check that this path isn't allowed via .bzdirect file,
   1225   1.1    tls 		 * and then check referrer; make sure that people come via the
   1226   1.1    tls 		 * real name... otherwise if we aren't looking at / or
   1227   1.1    tls 		 * /index.html, redirect...  we also special case favicon.ico.
   1228   1.1    tls 		 */
   1229   1.1    tls 		if (check_direct_access(request))
   1230   1.1    tls 			/* nothing */;
   1231   1.1    tls 		else if (request->hr_referrer) {
   1232   1.1    tls 			const char *r = request->hr_referrer;
   1233   1.1    tls 
   1234  1.16    mrg 			debug((httpd, DEBUG_FAT,
   1235  1.16    mrg 				"checking referrer \"%s\" vs virthostname %s",
   1236  1.16    mrg 				r, httpd->virthostname));
   1237   1.1    tls 			if (strncmp(r, "http://", 7) != 0 ||
   1238  1.16    mrg 			    (strncasecmp(r + 7, httpd->virthostname,
   1239  1.16    mrg 			    		 strlen(httpd->virthostname)) != 0 &&
   1240  1.12    mrg 			     !TOP_PAGE(file)))
   1241   1.1    tls 				to_indexhtml = 1;
   1242   1.1    tls 		} else {
   1243   1.1    tls 			const char *h = request->hr_host;
   1244   1.1    tls 
   1245  1.16    mrg 			debug((httpd, DEBUG_FAT, "url has no referrer at all"));
   1246   1.1    tls 			/* if there's no referrer, let / or /index.html past */
   1247  1.12    mrg 			if (!TOP_PAGE(file) ||
   1248  1.16    mrg 			    (h && strncasecmp(h, httpd->virthostname,
   1249  1.16    mrg 			    		strlen(httpd->virthostname)) != 0))
   1250   1.1    tls 				to_indexhtml = 1;
   1251   1.1    tls 		}
   1252   1.1    tls 
   1253   1.1    tls 		if (to_indexhtml) {
   1254   1.1    tls 			char *slashindexhtml;
   1255   1.1    tls 
   1256  1.16    mrg 			if (asprintf(&slashindexhtml, "/%s",
   1257  1.16    mrg 					httpd->index_html) < 0)
   1258  1.16    mrg 				bozo_err(httpd, 1, "asprintf");
   1259  1.16    mrg 			debug((httpd, DEBUG_FAT,
   1260  1.16    mrg 				"httpd->untrustedref: redirecting %s to %s",
   1261  1.16    mrg 				file, slashindexhtml));
   1262   1.1    tls 			handle_redirect(request, slashindexhtml, 0);
   1263  1.12    mrg 			free(slashindexhtml);
   1264  1.12    mrg 			return 0;
   1265   1.1    tls 		}
   1266   1.1    tls 	}
   1267   1.1    tls 
   1268  1.12    mrg 	len = strlen(file);
   1269  1.16    mrg 	if (/*CONSTCOND*/0) {
   1270   1.1    tls #ifndef NO_USER_SUPPORT
   1271  1.16    mrg 	} else if (len > 1 && httpd->enable_users && file[1] == '~') {
   1272  1.12    mrg 		if (file[2] == '\0') {
   1273  1.16    mrg 			(void)bozo_http_error(httpd, 404, request,
   1274  1.16    mrg 						"missing username");
   1275  1.12    mrg 			goto bad_done;
   1276  1.12    mrg 		}
   1277  1.12    mrg 		if (strchr(file + 2, '/') == NULL) {
   1278   1.1    tls 			handle_redirect(request, NULL, 0);
   1279  1.12    mrg 			return 0;
   1280  1.12    mrg 		}
   1281  1.16    mrg 		debug((httpd, DEBUG_FAT, "calling bozo_user_transform"));
   1282  1.12    mrg 
   1283  1.16    mrg 		return bozo_user_transform(request, isindex);
   1284   1.1    tls #endif /* NO_USER_SUPPORT */
   1285   1.1    tls 	} else if (len > 1) {
   1286  1.16    mrg 		debug((httpd, DEBUG_FAT, "file[len-1] == %c", file[len-1]));
   1287  1.12    mrg 		if (file[len-1] == '/') {	/* append index.html */
   1288   1.1    tls 			*isindex = 1;
   1289  1.16    mrg 			debug((httpd, DEBUG_FAT, "appending index.html"));
   1290  1.16    mrg 			newfile = bozomalloc(httpd,
   1291  1.16    mrg 					len + strlen(httpd->index_html) + 1);
   1292  1.12    mrg 			strcpy(newfile, file + 1);
   1293  1.16    mrg 			strcat(newfile, httpd->index_html);
   1294   1.1    tls 		} else
   1295  1.16    mrg 			newfile = bozostrdup(request->hr_httpd, file + 1);
   1296   1.1    tls 	} else if (len == 1) {
   1297  1.16    mrg 		debug((httpd, DEBUG_EXPLODING, "tf_req: len == 1"));
   1298  1.16    mrg 		newfile = bozostrdup(request->hr_httpd, httpd->index_html);
   1299   1.1    tls 		*isindex = 1;
   1300  1.12    mrg 	} else {	/* len == 0 ? */
   1301  1.16    mrg 		(void)bozo_http_error(httpd, 500, request,
   1302  1.16    mrg 					"request->hr_file is nul?");
   1303  1.12    mrg 		goto bad_done;
   1304  1.12    mrg 	}
   1305   1.1    tls 
   1306  1.12    mrg 	if (newfile == NULL) {
   1307  1.16    mrg 		(void)bozo_http_error(httpd, 500, request, "internal failure");
   1308  1.12    mrg 		goto bad_done;
   1309  1.12    mrg 	}
   1310   1.1    tls 
   1311   1.1    tls 	/*
   1312   1.1    tls 	 * look for "http://myname/" and deal with it as necessary.
   1313   1.1    tls 	 */
   1314   1.1    tls 
   1315   1.1    tls 	/*
   1316   1.1    tls 	 * stop traversing outside our domain
   1317   1.1    tls 	 *
   1318   1.1    tls 	 * XXX true security only comes from our parent using chroot(2)
   1319   1.1    tls 	 * before execve(2)'ing us.  or our own built in chroot(2) support.
   1320   1.1    tls 	 */
   1321  1.12    mrg 	if (*newfile == '/' || strcmp(newfile, "..") == 0 ||
   1322  1.12    mrg 	    strstr(newfile, "/..") || strstr(newfile, "../")) {
   1323  1.16    mrg 		(void)bozo_http_error(httpd, 403, request, "illegal request");
   1324  1.12    mrg 		goto bad_done;
   1325  1.12    mrg 	}
   1326  1.12    mrg 
   1327  1.17    mrg 	if (bozo_auth_check(request, newfile))
   1328  1.12    mrg 		goto bad_done;
   1329   1.1    tls 
   1330  1.13    mrg 	if (strlen(newfile)) {
   1331  1.20    mrg 		request->hr_oldfile = request->hr_file;
   1332  1.12    mrg 		request->hr_file = newfile;
   1333  1.13    mrg 	}
   1334   1.9    tls 
   1335  1.16    mrg 	if (bozo_process_cgi(request))
   1336  1.12    mrg 		return 0;
   1337   1.1    tls 
   1338  1.16    mrg 	debug((httpd, DEBUG_FAT, "transform_request set: %s", newfile));
   1339  1.12    mrg 	return 1;
   1340  1.12    mrg bad_done:
   1341  1.16    mrg 	debug((httpd, DEBUG_FAT, "transform_request returning: 0"));
   1342  1.12    mrg 	if (newfile)
   1343  1.12    mrg 		free(newfile);
   1344  1.12    mrg 	return 0;
   1345   1.1    tls }
   1346   1.1    tls 
   1347   1.1    tls /*
   1348  1.16    mrg  * bozo_process_request does the following:
   1349  1.16    mrg  *	- check the request is valid
   1350  1.16    mrg  *	- process cgi-bin if necessary
   1351  1.16    mrg  *	- transform a filename if necesarry
   1352  1.16    mrg  *	- return the HTTP request
   1353   1.1    tls  */
   1354  1.16    mrg void
   1355  1.16    mrg bozo_process_request(bozo_httpreq_t *request)
   1356   1.1    tls {
   1357  1.16    mrg 	bozohttpd_t *httpd = request->hr_httpd;
   1358  1.16    mrg 	struct	stat sb;
   1359  1.16    mrg 	time_t timestamp;
   1360  1.16    mrg 	char	*file;
   1361  1.16    mrg 	const char *type, *encoding;
   1362  1.16    mrg 	int	fd, isindex;
   1363  1.16    mrg 
   1364  1.16    mrg 	/*
   1365  1.16    mrg 	 * note that transform_request chdir()'s if required.  also note
   1366  1.16    mrg 	 * that cgi is handed here.  if transform_request() returns 0
   1367  1.16    mrg 	 * then the request has been handled already.
   1368  1.16    mrg 	 */
   1369  1.16    mrg 	if (transform_request(request, &isindex) == 0)
   1370  1.16    mrg 		return;
   1371  1.12    mrg 
   1372  1.16    mrg 	file = request->hr_file;
   1373   1.9    tls 
   1374  1.16    mrg 	fd = open(file, O_RDONLY);
   1375  1.16    mrg 	if (fd < 0) {
   1376  1.16    mrg 		debug((httpd, DEBUG_FAT, "open failed: %s", strerror(errno)));
   1377  1.16    mrg 		if (errno == EPERM)
   1378  1.16    mrg 			(void)bozo_http_error(httpd, 403, request,
   1379  1.16    mrg 						"no permission to open file");
   1380  1.16    mrg 		else if (errno == ENOENT) {
   1381  1.16    mrg 			if (!bozo_dir_index(request, file, isindex))
   1382  1.16    mrg 				(void)bozo_http_error(httpd, 404, request,
   1383  1.16    mrg 							"no file");
   1384  1.16    mrg 		} else
   1385  1.16    mrg 			(void)bozo_http_error(httpd, 500, request, "open file");
   1386  1.16    mrg 		goto cleanup_nofd;
   1387   1.1    tls 	}
   1388  1.16    mrg 	if (fstat(fd, &sb) < 0) {
   1389  1.16    mrg 		(void)bozo_http_error(httpd, 500, request, "can't fstat");
   1390  1.16    mrg 		goto cleanup;
   1391   1.9    tls 	}
   1392  1.16    mrg 	if (S_ISDIR(sb.st_mode)) {
   1393  1.16    mrg 		handle_redirect(request, NULL, 0);
   1394  1.16    mrg 		goto cleanup;
   1395   1.6    mrg 	}
   1396   1.1    tls 
   1397  1.16    mrg 	if (request->hr_if_modified_since &&
   1398  1.16    mrg 	    parse_http_date(request->hr_if_modified_since, &timestamp) &&
   1399  1.16    mrg 	    timestamp >= sb.st_mtime) {
   1400  1.16    mrg 		/* XXX ignore subsecond of timestamp */
   1401  1.16    mrg 		bozo_printf(httpd, "%s 304 Not Modified\r\n",
   1402  1.16    mrg 				request->hr_proto);
   1403  1.16    mrg 		bozo_printf(httpd, "\r\n");
   1404  1.16    mrg 		bozo_flush(httpd, stdout);
   1405  1.16    mrg 		goto cleanup;
   1406   1.1    tls 	}
   1407   1.1    tls 
   1408  1.16    mrg 	/* validate requested range */
   1409  1.16    mrg 	if (request->hr_last_byte_pos == -1 ||
   1410  1.16    mrg 	    request->hr_last_byte_pos >= sb.st_size)
   1411  1.16    mrg 		request->hr_last_byte_pos = sb.st_size - 1;
   1412  1.16    mrg 	if (request->hr_have_range &&
   1413  1.16    mrg 	    request->hr_first_byte_pos > request->hr_last_byte_pos) {
   1414  1.16    mrg 		request->hr_have_range = 0;	/* punt */
   1415  1.16    mrg 		request->hr_first_byte_pos = 0;
   1416  1.16    mrg 		request->hr_last_byte_pos = sb.st_size - 1;
   1417   1.1    tls 	}
   1418  1.16    mrg 	debug((httpd, DEBUG_FAT, "have_range %d first_pos %qd last_pos %qd",
   1419  1.16    mrg 	    request->hr_have_range,
   1420  1.16    mrg 	    request->hr_first_byte_pos, request->hr_last_byte_pos));
   1421  1.16    mrg 	if (request->hr_have_range)
   1422  1.16    mrg 		bozo_printf(httpd, "%s 206 Partial Content\r\n",
   1423  1.16    mrg 				request->hr_proto);
   1424  1.16    mrg 	else
   1425  1.16    mrg 		bozo_printf(httpd, "%s 200 OK\r\n", request->hr_proto);
   1426   1.1    tls 
   1427  1.16    mrg 	if (request->hr_proto != httpd->consts.http_09) {
   1428  1.16    mrg 		type = bozo_content_type(request, file);
   1429  1.16    mrg 		encoding = bozo_content_encoding(request, file);
   1430   1.1    tls 
   1431  1.16    mrg 		bozo_print_header(request, &sb, type, encoding);
   1432  1.16    mrg 		bozo_printf(httpd, "\r\n");
   1433  1.12    mrg 	}
   1434  1.16    mrg 	bozo_flush(httpd, stdout);
   1435   1.1    tls 
   1436  1.16    mrg 	if (request->hr_method != HTTP_HEAD) {
   1437  1.16    mrg 		off_t szleft, cur_byte_pos;
   1438   1.1    tls 
   1439  1.16    mrg 		szleft =
   1440  1.16    mrg 		     request->hr_last_byte_pos - request->hr_first_byte_pos + 1;
   1441  1.16    mrg 		cur_byte_pos = request->hr_first_byte_pos;
   1442   1.1    tls 
   1443  1.16    mrg  retry:
   1444  1.16    mrg 		while (szleft) {
   1445  1.16    mrg 			size_t sz;
   1446  1.12    mrg 
   1447  1.16    mrg 			/* This should take care of the first unaligned chunk */
   1448  1.16    mrg 			if ((cur_byte_pos & (httpd->page_size - 1)) != 0)
   1449  1.16    mrg 				sz = (size_t)(cur_byte_pos & ~httpd->page_size);
   1450  1.16    mrg 			if ((off_t)httpd->mmapsz < szleft)
   1451  1.16    mrg 				sz = httpd->mmapsz;
   1452  1.16    mrg 			else
   1453  1.16    mrg 				sz = (size_t)szleft;
   1454  1.16    mrg 			if (mmap_and_write_part(httpd, fd, cur_byte_pos, sz)) {
   1455  1.16    mrg 				if (errno == ENOMEM) {
   1456  1.16    mrg 					httpd->mmapsz /= 2;
   1457  1.16    mrg 					if (httpd->mmapsz >= httpd->page_size)
   1458  1.16    mrg 						goto retry;
   1459  1.16    mrg 				}
   1460  1.16    mrg 				goto cleanup;
   1461  1.16    mrg 			}
   1462  1.16    mrg 			cur_byte_pos += sz;
   1463  1.16    mrg 			szleft -= sz;
   1464   1.1    tls 		}
   1465  1.16    mrg 	}
   1466  1.16    mrg  cleanup:
   1467  1.16    mrg 	close(fd);
   1468  1.16    mrg  cleanup_nofd:
   1469  1.16    mrg 	close(STDIN_FILENO);
   1470  1.16    mrg 	close(STDOUT_FILENO);
   1471  1.16    mrg 	/*close(STDERR_FILENO);*/
   1472   1.1    tls }
   1473   1.1    tls 
   1474  1.16    mrg /* make sure we're not trying to access special files */
   1475  1.16    mrg int
   1476  1.16    mrg bozo_check_special_files(bozo_httpreq_t *request, const char *name)
   1477   1.1    tls {
   1478  1.16    mrg 	bozohttpd_t *httpd = request->hr_httpd;
   1479   1.1    tls 
   1480  1.16    mrg 	/* ensure basename(name) != special files */
   1481  1.16    mrg 	if (strcmp(name, DIRECT_ACCESS_FILE) == 0)
   1482  1.16    mrg 		return bozo_http_error(httpd, 403, request,
   1483  1.16    mrg 		    "no permission to open direct access file");
   1484  1.16    mrg 	if (strcmp(name, REDIRECT_FILE) == 0)
   1485  1.16    mrg 		return bozo_http_error(httpd, 403, request,
   1486  1.16    mrg 		    "no permission to open redirect file");
   1487  1.16    mrg 	if (strcmp(name, ABSREDIRECT_FILE) == 0)
   1488  1.16    mrg 		return bozo_http_error(httpd, 403, request,
   1489  1.16    mrg 		    "no permission to open redirect file");
   1490  1.17    mrg 	return bozo_auth_check_special_files(request, name);
   1491  1.16    mrg }
   1492   1.1    tls 
   1493  1.16    mrg /* generic header printing routine */
   1494  1.16    mrg void
   1495  1.16    mrg bozo_print_header(bozo_httpreq_t *request,
   1496  1.16    mrg 		struct stat *sbp, const char *type, const char *encoding)
   1497  1.16    mrg {
   1498  1.16    mrg 	bozohttpd_t *httpd = request->hr_httpd;
   1499  1.16    mrg 	off_t len;
   1500  1.16    mrg 	char	date[40];
   1501   1.1    tls 
   1502  1.16    mrg 	bozo_printf(httpd, "Date: %s\r\n", bozo_http_date(date, sizeof(date)));
   1503  1.16    mrg 	bozo_printf(httpd, "Server: %s\r\n", httpd->server_software);
   1504  1.16    mrg 	bozo_printf(httpd, "Accept-Ranges: bytes\r\n");
   1505  1.16    mrg 	if (sbp) {
   1506  1.16    mrg 		char filedate[40];
   1507  1.16    mrg 		struct	tm *tm;
   1508   1.1    tls 
   1509  1.16    mrg 		tm = gmtime(&sbp->st_mtime);
   1510  1.16    mrg 		strftime(filedate, sizeof filedate,
   1511  1.16    mrg 		    "%a, %d %b %Y %H:%M:%S GMT", tm);
   1512  1.16    mrg 		bozo_printf(httpd, "Last-Modified: %s\r\n", filedate);
   1513  1.16    mrg 	}
   1514  1.16    mrg 	if (type && *type)
   1515  1.16    mrg 		bozo_printf(httpd, "Content-Type: %s\r\n", type);
   1516  1.16    mrg 	if (encoding && *encoding)
   1517  1.16    mrg 		bozo_printf(httpd, "Content-Encoding: %s\r\n", encoding);
   1518  1.16    mrg 	if (sbp) {
   1519  1.16    mrg 		if (request->hr_have_range) {
   1520  1.16    mrg 			len = request->hr_last_byte_pos -
   1521  1.16    mrg 					request->hr_first_byte_pos +1;
   1522  1.16    mrg 			bozo_printf(httpd,
   1523  1.16    mrg 				"Content-Range: bytes %qd-%qd/%qd\r\n",
   1524  1.16    mrg 				(long long) request->hr_first_byte_pos,
   1525  1.16    mrg 				(long long) request->hr_last_byte_pos,
   1526  1.16    mrg 				(long long) sbp->st_size);
   1527  1.16    mrg 		} else
   1528  1.16    mrg 			len = sbp->st_size;
   1529  1.16    mrg 		bozo_printf(httpd, "Content-Length: %qd\r\n", (long long)len);
   1530   1.1    tls 	}
   1531  1.16    mrg 	if (request && request->hr_proto == httpd->consts.http_11)
   1532  1.16    mrg 		bozo_printf(httpd, "Connection: close\r\n");
   1533  1.16    mrg 	bozo_flush(httpd, stdout);
   1534   1.1    tls }
   1535   1.1    tls 
   1536   1.1    tls #ifdef DEBUG
   1537   1.1    tls void
   1538  1.16    mrg debug__(bozohttpd_t *httpd, int level, const char *fmt, ...)
   1539   1.1    tls {
   1540   1.1    tls 	va_list	ap;
   1541   1.1    tls 	int savederrno;
   1542   1.1    tls 
   1543   1.1    tls 	/* only log if the level is low enough */
   1544  1.16    mrg 	if (httpd->debug < level)
   1545   1.1    tls 		return;
   1546   1.1    tls 
   1547   1.1    tls 	savederrno = errno;
   1548   1.1    tls 	va_start(ap, fmt);
   1549  1.16    mrg 	if (httpd->logstderr) {
   1550   1.1    tls 		vfprintf(stderr, fmt, ap);
   1551   1.1    tls 		fputs("\n", stderr);
   1552   1.1    tls 	} else
   1553   1.1    tls 		vsyslog(LOG_DEBUG, fmt, ap);
   1554   1.1    tls 	va_end(ap);
   1555   1.1    tls 	errno = savederrno;
   1556   1.1    tls }
   1557   1.1    tls #endif /* DEBUG */
   1558   1.1    tls 
   1559   1.1    tls /* these are like warn() and err(), except for syslog not stderr */
   1560   1.1    tls void
   1561  1.16    mrg bozo_warn(bozohttpd_t *httpd, const char *fmt, ...)
   1562   1.1    tls {
   1563   1.1    tls 	va_list ap;
   1564   1.1    tls 
   1565   1.1    tls 	va_start(ap, fmt);
   1566  1.16    mrg 	if (httpd->logstderr || isatty(STDERR_FILENO)) {
   1567  1.14    mrg 		//fputs("warning: ", stderr);
   1568   1.1    tls 		vfprintf(stderr, fmt, ap);
   1569   1.1    tls 		fputs("\n", stderr);
   1570   1.1    tls 	} else
   1571   1.1    tls 		vsyslog(LOG_INFO, fmt, ap);
   1572   1.1    tls 	va_end(ap);
   1573   1.1    tls }
   1574   1.1    tls 
   1575   1.1    tls void
   1576  1.16    mrg bozo_err(bozohttpd_t *httpd, int code, const char *fmt, ...)
   1577   1.1    tls {
   1578   1.1    tls 	va_list ap;
   1579   1.1    tls 
   1580   1.1    tls 	va_start(ap, fmt);
   1581  1.16    mrg 	if (httpd->logstderr || isatty(STDERR_FILENO)) {
   1582  1.14    mrg 		//fputs("error: ", stderr);
   1583   1.1    tls 		vfprintf(stderr, fmt, ap);
   1584   1.1    tls 		fputs("\n", stderr);
   1585   1.1    tls 	} else
   1586   1.1    tls 		vsyslog(LOG_ERR, fmt, ap);
   1587   1.1    tls 	va_end(ap);
   1588   1.1    tls 	exit(code);
   1589   1.1    tls }
   1590   1.1    tls 
   1591  1.16    mrg /* this escape HTML tags */
   1592  1.16    mrg static void
   1593  1.16    mrg escape_html(bozo_httpreq_t *request)
   1594   1.1    tls {
   1595  1.16    mrg 	int	i, j;
   1596  1.16    mrg 	char	*url = request->hr_file, *tmp;
   1597   1.1    tls 
   1598  1.16    mrg 	for (i = 0, j = 0; url[i]; i++) {
   1599  1.16    mrg 		switch (url[i]) {
   1600  1.16    mrg 		case '<':
   1601  1.16    mrg 		case '>':
   1602  1.16    mrg 			j += 4;
   1603  1.16    mrg 			break;
   1604  1.16    mrg 		case '&':
   1605  1.16    mrg 			j += 5;
   1606  1.16    mrg 			break;
   1607  1.16    mrg 		}
   1608  1.12    mrg 	}
   1609   1.1    tls 
   1610  1.16    mrg 	if (j == 0)
   1611  1.16    mrg 		return;
   1612  1.16    mrg 
   1613  1.16    mrg 	if ((tmp = (char *) malloc(strlen(url) + j)) == 0)
   1614  1.16    mrg 		/*
   1615  1.16    mrg 		 * ouch, but we are only called from an error context, and
   1616  1.16    mrg 		 * most paths here come from malloc(3) failures anyway...
   1617  1.16    mrg 		 * we could completely punt and just exit, but isn't returning
   1618  1.16    mrg 		 * an not-quite-correct error better than nothing at all?
   1619  1.16    mrg 		 */
   1620  1.16    mrg 		return;
   1621   1.1    tls 
   1622  1.16    mrg 	for (i = 0, j = 0; url[i]; i++) {
   1623  1.16    mrg 		switch (url[i]) {
   1624  1.16    mrg 		case '<':
   1625  1.16    mrg 			memcpy(tmp + j, "&lt;", 4);
   1626  1.16    mrg 			j += 4;
   1627  1.16    mrg 			break;
   1628  1.16    mrg 		case '>':
   1629  1.16    mrg 			memcpy(tmp + j, "&gt;", 4);
   1630  1.16    mrg 			j += 4;
   1631  1.16    mrg 			break;
   1632  1.16    mrg 		case '&':
   1633  1.16    mrg 			memcpy(tmp + j, "&amp;", 5);
   1634  1.16    mrg 			j += 5;
   1635  1.16    mrg 			break;
   1636  1.16    mrg 		default:
   1637  1.16    mrg 			tmp[j++] = url[i];
   1638  1.12    mrg 		}
   1639  1.16    mrg 	}
   1640  1.16    mrg 	tmp[j] = 0;
   1641   1.1    tls 
   1642  1.16    mrg 	free(request->hr_file);
   1643  1.16    mrg 	request->hr_file = tmp;
   1644   1.1    tls }
   1645   1.1    tls 
   1646   1.1    tls /* short map between error code, and short/long messages */
   1647   1.1    tls static struct errors_map {
   1648   1.1    tls 	int	code;			/* HTTP return code */
   1649   1.1    tls 	const char *shortmsg;		/* short version of message */
   1650   1.1    tls 	const char *longmsg;		/* long version of message */
   1651   1.1    tls } errors_map[] = {
   1652   1.1    tls 	{ 400,	"400 Bad Request",	"The request was not valid", },
   1653   1.1    tls 	{ 401,	"401 Unauthorized",	"No authorization", },
   1654   1.6    mrg 	{ 403,	"403 Forbidden",	"Access to this item has been denied",},
   1655   1.1    tls 	{ 404, 	"404 Not Found",	"This item has not been found", },
   1656   1.1    tls 	{ 408, 	"408 Request Timeout",	"This request took too long", },
   1657   1.1    tls 	{ 417,	"417 Expectation Failed","Expectations not available", },
   1658   1.1    tls 	{ 500,	"500 Internal Error",	"An error occured on the server", },
   1659   1.1    tls 	{ 501,	"501 Not Implemented",	"This request is not available", },
   1660   1.1    tls 	{ 0,	NULL,			NULL, },
   1661   1.1    tls };
   1662   1.1    tls 
   1663   1.1    tls static const char *help = "DANGER! WILL ROBINSON! DANGER!";
   1664   1.1    tls 
   1665   1.1    tls static const char *
   1666   1.1    tls http_errors_short(int code)
   1667   1.1    tls {
   1668   1.1    tls 	struct errors_map *ep;
   1669   1.1    tls 
   1670   1.1    tls 	for (ep = errors_map; ep->code; ep++)
   1671   1.1    tls 		if (ep->code == code)
   1672   1.1    tls 			return (ep->shortmsg);
   1673   1.1    tls 	return (help);
   1674   1.1    tls }
   1675   1.1    tls 
   1676   1.1    tls static const char *
   1677   1.1    tls http_errors_long(int code)
   1678   1.1    tls {
   1679   1.1    tls 	struct errors_map *ep;
   1680   1.1    tls 
   1681   1.1    tls 	for (ep = errors_map; ep->code; ep++)
   1682   1.1    tls 		if (ep->code == code)
   1683   1.1    tls 			return (ep->longmsg);
   1684   1.1    tls 	return (help);
   1685   1.1    tls }
   1686   1.1    tls 
   1687  1.16    mrg /* the follow functions and variables are used in handling HTTP errors */
   1688  1.16    mrg /* ARGSUSED */
   1689  1.16    mrg int
   1690  1.16    mrg bozo_http_error(bozohttpd_t *httpd, int code, bozo_httpreq_t *request,
   1691  1.16    mrg 		const char *msg)
   1692  1.16    mrg {
   1693  1.16    mrg 	char portbuf[20];
   1694  1.16    mrg 	const char *header = http_errors_short(code);
   1695  1.16    mrg 	const char *reason = http_errors_long(code);
   1696  1.16    mrg 	const char *proto = (request && request->hr_proto) ?
   1697  1.16    mrg 				request->hr_proto : httpd->consts.http_11;
   1698  1.16    mrg 	int	size;
   1699  1.16    mrg 
   1700  1.16    mrg 	debug((httpd, DEBUG_FAT, "bozo_http_error %d: %s", code, msg));
   1701  1.16    mrg 	if (header == NULL || reason == NULL) {
   1702  1.16    mrg 		bozo_err(httpd, 1,
   1703  1.16    mrg 			"bozo_http_error() failed (short = %p, long = %p)",
   1704  1.16    mrg 			header, reason);
   1705  1.16    mrg 		return code;
   1706  1.16    mrg 	}
   1707  1.16    mrg 
   1708  1.16    mrg 	if (request && request->hr_serverport &&
   1709  1.16    mrg 	    strcmp(request->hr_serverport, "80") != 0)
   1710  1.16    mrg 		snprintf(portbuf, sizeof(portbuf), ":%s",
   1711  1.16    mrg 				request->hr_serverport);
   1712  1.16    mrg 	else
   1713  1.16    mrg 		portbuf[0] = '\0';
   1714  1.16    mrg 
   1715  1.16    mrg 	if (request && request->hr_file) {
   1716  1.16    mrg 		escape_html(request);
   1717  1.16    mrg 		size = snprintf(httpd->errorbuf, BUFSIZ,
   1718  1.16    mrg 		    "<html><head><title>%s</title></head>\n"
   1719  1.16    mrg 		    "<body><h1>%s</h1>\n"
   1720  1.16    mrg 		    "%s: <pre>%s</pre>\n"
   1721  1.16    mrg  		    "<hr><address><a href=\"http://%s%s/\">%s%s</a></address>\n"
   1722  1.16    mrg 		    "</body></html>\n",
   1723  1.16    mrg 		    header, header, request->hr_file, reason,
   1724  1.16    mrg 		    httpd->virthostname, portbuf, httpd->virthostname, portbuf);
   1725  1.16    mrg 		if (size >= (int)BUFSIZ) {
   1726  1.16    mrg 			bozo_warn(httpd,
   1727  1.16    mrg 				"bozo_http_error buffer too small, truncated");
   1728  1.16    mrg 			size = (int)BUFSIZ;
   1729  1.16    mrg 		}
   1730  1.16    mrg 	} else
   1731  1.16    mrg 		size = 0;
   1732  1.16    mrg 
   1733  1.16    mrg 	bozo_printf(httpd, "%s %s\r\n", proto, header);
   1734  1.17    mrg 	bozo_auth_check_401(request, code);
   1735  1.16    mrg 
   1736  1.16    mrg 	bozo_printf(httpd, "Content-Type: text/html\r\n");
   1737  1.16    mrg 	bozo_printf(httpd, "Content-Length: %d\r\n", size);
   1738  1.16    mrg 	bozo_printf(httpd, "Server: %s\r\n", httpd->server_software);
   1739  1.16    mrg 	if (request && request->hr_allow)
   1740  1.16    mrg 		bozo_printf(httpd, "Allow: %s\r\n", request->hr_allow);
   1741  1.16    mrg 	bozo_printf(httpd, "\r\n");
   1742  1.16    mrg 	if (size)
   1743  1.16    mrg 		bozo_printf(httpd, "%s", httpd->errorbuf);
   1744  1.16    mrg 	bozo_flush(httpd, stdout);
   1745  1.16    mrg 
   1746  1.16    mrg 	return code;
   1747  1.16    mrg }
   1748  1.16    mrg 
   1749   1.1    tls /* Below are various modified libc functions */
   1750   1.1    tls 
   1751   1.1    tls /*
   1752   1.1    tls  * returns -1 in lenp if the string ran out before finding a delimiter,
   1753   1.1    tls  * but is otherwise the same as strsep.  Note that the length must be
   1754   1.1    tls  * correctly passed in.
   1755   1.1    tls  */
   1756   1.1    tls char *
   1757   1.6    mrg bozostrnsep(char **strp, const char *delim, ssize_t	*lenp)
   1758   1.1    tls {
   1759   1.1    tls 	char	*s;
   1760   1.1    tls 	const	char *spanp;
   1761   1.1    tls 	int	c, sc;
   1762   1.1    tls 	char	*tok;
   1763   1.1    tls 
   1764   1.1    tls 	if ((s = *strp) == NULL)
   1765   1.1    tls 		return (NULL);
   1766   1.1    tls 	for (tok = s;;) {
   1767   1.1    tls 		if (lenp && --(*lenp) == -1)
   1768   1.1    tls 			return (NULL);
   1769   1.1    tls 		c = *s++;
   1770   1.1    tls 		spanp = delim;
   1771   1.1    tls 		do {
   1772   1.1    tls 			if ((sc = *spanp++) == c) {
   1773   1.1    tls 				if (c == 0)
   1774   1.1    tls 					s = NULL;
   1775   1.1    tls 				else
   1776   1.1    tls 					s[-1] = '\0';
   1777   1.1    tls 				*strp = s;
   1778   1.1    tls 				return (tok);
   1779   1.1    tls 			}
   1780   1.1    tls 		} while (sc != 0);
   1781   1.1    tls 	}
   1782   1.1    tls 	/* NOTREACHED */
   1783   1.1    tls }
   1784   1.1    tls 
   1785   1.1    tls /*
   1786   1.1    tls  * inspired by fgetln(3), but works for fd's.  should work identically
   1787   1.1    tls  * except it, however, does *not* return the newline, and it does nul
   1788   1.1    tls  * terminate the string.
   1789   1.1    tls  */
   1790   1.1    tls char *
   1791  1.16    mrg bozodgetln(bozohttpd_t *httpd, int fd, ssize_t *lenp,
   1792  1.16    mrg 	ssize_t (*readfn)(bozohttpd_t *, int, void *, size_t))
   1793   1.1    tls {
   1794   1.1    tls 	ssize_t	len;
   1795   1.1    tls 	int	got_cr = 0;
   1796   1.1    tls 	char	c, *nbuffer;
   1797   1.1    tls 
   1798   1.1    tls 	/* initialise */
   1799  1.16    mrg 	if (httpd->getln_buflen == 0) {
   1800  1.16    mrg 		/* should be plenty for most requests */
   1801  1.16    mrg 		httpd->getln_buflen = 128;
   1802  1.16    mrg 		httpd->getln_buffer = malloc((size_t)httpd->getln_buflen);
   1803  1.16    mrg 		if (httpd->getln_buffer == NULL) {
   1804  1.16    mrg 			httpd->getln_buflen = 0;
   1805   1.1    tls 			return NULL;
   1806   1.1    tls 		}
   1807   1.1    tls 	}
   1808   1.1    tls 	len = 0;
   1809   1.1    tls 
   1810   1.1    tls 	/*
   1811   1.1    tls 	 * we *have* to read one byte at a time, to not break cgi
   1812   1.1    tls 	 * programs (for we pass stdin off to them).  could fix this
   1813   1.1    tls 	 * by becoming a fd-passing program instead of just exec'ing
   1814   1.1    tls 	 * the program
   1815  1.17    mrg 	 *
   1816  1.17    mrg 	 * the above is no longer true, we are the fd-passing
   1817  1.17    mrg 	 * program already.
   1818   1.1    tls 	 */
   1819  1.16    mrg 	for (; readfn(httpd, fd, &c, 1) == 1; ) {
   1820  1.16    mrg 		debug((httpd, DEBUG_EXPLODING, "bozodgetln read %c", c));
   1821   1.1    tls 
   1822  1.16    mrg 		if (len >= httpd->getln_buflen - 1) {
   1823  1.16    mrg 			httpd->getln_buflen *= 2;
   1824  1.16    mrg 			debug((httpd, DEBUG_EXPLODING, "bozodgetln: "
   1825  1.16    mrg 				"reallocating buffer to buflen %zu",
   1826  1.16    mrg 				httpd->getln_buflen));
   1827  1.16    mrg 			nbuffer = bozorealloc(httpd, httpd->getln_buffer,
   1828  1.16    mrg 				(size_t)httpd->getln_buflen);
   1829  1.16    mrg 			httpd->getln_buffer = nbuffer;
   1830   1.1    tls 		}
   1831   1.1    tls 
   1832  1.16    mrg 		httpd->getln_buffer[len++] = c;
   1833   1.1    tls 		if (c == '\r') {
   1834   1.1    tls 			got_cr = 1;
   1835   1.1    tls 			continue;
   1836   1.1    tls 		} else if (c == '\n') {
   1837   1.1    tls 			/*
   1838   1.1    tls 			 * HTTP/1.1 spec says to ignore CR and treat
   1839   1.1    tls 			 * LF as the real line terminator.  even though
   1840   1.1    tls 			 * the same spec defines CRLF as the line
   1841   1.1    tls 			 * terminator, it is recommended in section 19.3
   1842   1.1    tls 			 * to do the LF trick for tolerance.
   1843   1.1    tls 			 */
   1844   1.1    tls 			if (got_cr)
   1845   1.1    tls 				len -= 2;
   1846   1.1    tls 			else
   1847   1.1    tls 				len -= 1;
   1848   1.1    tls 			break;
   1849   1.1    tls 		}
   1850   1.1    tls 
   1851   1.1    tls 	}
   1852  1.16    mrg 	httpd->getln_buffer[len] = '\0';
   1853  1.16    mrg 	debug((httpd, DEBUG_OBESE, "bozodgetln returns: ``%s'' with len %d",
   1854  1.16    mrg 	       httpd->getln_buffer, len));
   1855   1.1    tls 	*lenp = len;
   1856  1.16    mrg 	return httpd->getln_buffer;
   1857   1.1    tls }
   1858   1.1    tls 
   1859   1.1    tls void *
   1860  1.16    mrg bozorealloc(bozohttpd_t *httpd, void *ptr, size_t size)
   1861   1.1    tls {
   1862   1.1    tls 	void	*p;
   1863   1.1    tls 
   1864   1.1    tls 	p = realloc(ptr, size);
   1865  1.12    mrg 	if (p == NULL) {
   1866  1.16    mrg 		(void)bozo_http_error(httpd, 500, NULL,
   1867  1.16    mrg 				"memory allocation failure");
   1868  1.12    mrg 		exit(1);
   1869  1.12    mrg 	}
   1870   1.1    tls 	return (p);
   1871   1.1    tls }
   1872   1.1    tls 
   1873   1.1    tls void *
   1874  1.16    mrg bozomalloc(bozohttpd_t *httpd, size_t size)
   1875   1.1    tls {
   1876   1.1    tls 	void	*p;
   1877   1.1    tls 
   1878   1.1    tls 	p = malloc(size);
   1879  1.12    mrg 	if (p == NULL) {
   1880  1.16    mrg 		(void)bozo_http_error(httpd, 500, NULL,
   1881  1.16    mrg 				"memory allocation failure");
   1882  1.12    mrg 		exit(1);
   1883  1.12    mrg 	}
   1884   1.1    tls 	return (p);
   1885   1.1    tls }
   1886   1.1    tls 
   1887   1.1    tls char *
   1888  1.16    mrg bozostrdup(bozohttpd_t *httpd, const char *str)
   1889   1.1    tls {
   1890   1.1    tls 	char	*p;
   1891   1.1    tls 
   1892   1.1    tls 	p = strdup(str);
   1893  1.12    mrg 	if (p == NULL) {
   1894  1.16    mrg 		(void)bozo_http_error(httpd, 500, NULL,
   1895  1.16    mrg 					"memory allocation failure");
   1896  1.12    mrg 		exit(1);
   1897  1.12    mrg 	}
   1898   1.1    tls 	return (p);
   1899   1.1    tls }
   1900  1.16    mrg 
   1901  1.16    mrg /* set default values in bozohttpd_t struct */
   1902  1.16    mrg int
   1903  1.16    mrg bozo_init_httpd(bozohttpd_t *httpd)
   1904  1.16    mrg {
   1905  1.16    mrg 	/* make sure everything is clean */
   1906  1.16    mrg 	(void) memset(httpd, 0x0, sizeof(*httpd));
   1907  1.16    mrg 
   1908  1.16    mrg 	/* constants */
   1909  1.16    mrg 	httpd->consts.http_09 = "HTTP/0.9";
   1910  1.16    mrg 	httpd->consts.http_10 = "HTTP/1.0";
   1911  1.16    mrg 	httpd->consts.http_11 = "HTTP/1.1";
   1912  1.16    mrg 	httpd->consts.text_plain = "text/plain";
   1913  1.16    mrg 
   1914  1.16    mrg 	/* mmap region size */
   1915  1.16    mrg 	httpd->mmapsz = BOZO_MMAPSZ;
   1916  1.16    mrg 
   1917  1.16    mrg 	/* error buffer for bozo_http_error() */
   1918  1.16    mrg 	if ((httpd->errorbuf = malloc(BUFSIZ)) == NULL) {
   1919  1.16    mrg 		(void) fprintf(stderr,
   1920  1.16    mrg 			"bozohttpd: memory_allocation failure\n");
   1921  1.16    mrg 		return 0;
   1922  1.16    mrg 	}
   1923  1.16    mrg 	return 1;
   1924  1.16    mrg }
   1925  1.16    mrg 
   1926  1.16    mrg /* set default values in bozoprefs_t struct */
   1927  1.16    mrg int
   1928  1.16    mrg bozo_init_prefs(bozoprefs_t *prefs)
   1929  1.16    mrg {
   1930  1.16    mrg 	/* make sure everything is clean */
   1931  1.16    mrg 	(void) memset(prefs, 0x0, sizeof(*prefs));
   1932  1.16    mrg 
   1933  1.16    mrg 	/* set up default values */
   1934  1.16    mrg 	bozo_set_pref(prefs, "server software", SERVER_SOFTWARE);
   1935  1.16    mrg 	bozo_set_pref(prefs, "index.html", INDEX_HTML);
   1936  1.16    mrg 	bozo_set_pref(prefs, "public_html", PUBLIC_HTML);
   1937  1.16    mrg 
   1938  1.16    mrg 	return 1;
   1939  1.16    mrg }
   1940  1.16    mrg 
   1941  1.16    mrg /* set default values */
   1942  1.16    mrg int
   1943  1.16    mrg bozo_set_defaults(bozohttpd_t *httpd, bozoprefs_t *prefs)
   1944  1.16    mrg {
   1945  1.16    mrg 	return bozo_init_httpd(httpd) && bozo_init_prefs(prefs);
   1946  1.16    mrg }
   1947  1.16    mrg 
   1948  1.16    mrg /* set the virtual host name, port and root */
   1949  1.16    mrg int
   1950  1.16    mrg bozo_setup(bozohttpd_t *httpd, bozoprefs_t *prefs, const char *vhost,
   1951  1.16    mrg 		const char *root)
   1952  1.16    mrg {
   1953  1.16    mrg 	struct passwd	 *pw;
   1954  1.16    mrg 	extern char	**environ;
   1955  1.21    mrg 	static char	 *cleanenv[1] = { NULL };
   1956  1.16    mrg 	uid_t		  uid;
   1957  1.16    mrg 	char		 *chrootdir;
   1958  1.16    mrg 	char		 *username;
   1959  1.16    mrg 	char		 *portnum;
   1960  1.16    mrg 	char		 *cp;
   1961  1.16    mrg 	int		  dirtyenv;
   1962  1.16    mrg 
   1963  1.16    mrg 	dirtyenv = 0;
   1964  1.16    mrg 
   1965  1.16    mrg 	if (vhost == NULL) {
   1966  1.16    mrg 		httpd->virthostname = bozomalloc(httpd, MAXHOSTNAMELEN+1);
   1967  1.16    mrg 		/* XXX we do not check for FQDN here */
   1968  1.16    mrg 		if (gethostname(httpd->virthostname, MAXHOSTNAMELEN+1) < 0)
   1969  1.16    mrg 			bozo_err(httpd, 1, "gethostname");
   1970  1.16    mrg 		httpd->virthostname[MAXHOSTNAMELEN] = '\0';
   1971  1.16    mrg 	} else {
   1972  1.16    mrg 		httpd->virthostname = strdup(vhost);
   1973  1.16    mrg 	}
   1974  1.16    mrg 	httpd->slashdir = strdup(root);
   1975  1.16    mrg 	if ((portnum = bozo_get_pref(prefs, "port number")) != NULL) {
   1976  1.16    mrg 		httpd->bindport = strdup(portnum);
   1977  1.16    mrg 	}
   1978  1.16    mrg 
   1979  1.16    mrg 	/* go over preferences now */
   1980  1.16    mrg 	if ((cp = bozo_get_pref(prefs, "numeric")) != NULL &&
   1981  1.16    mrg 	    strcmp(cp, "true") == 0) {
   1982  1.16    mrg 		httpd->numeric = 1;
   1983  1.16    mrg 	}
   1984  1.16    mrg 	if ((cp = bozo_get_pref(prefs, "trusted referal")) != NULL &&
   1985  1.16    mrg 	    strcmp(cp, "true") == 0) {
   1986  1.16    mrg 		httpd->untrustedref = 1;
   1987  1.16    mrg 	}
   1988  1.16    mrg 	if ((cp = bozo_get_pref(prefs, "log to stderr")) != NULL &&
   1989  1.16    mrg 	    strcmp(cp, "true") == 0) {
   1990  1.16    mrg 		httpd->logstderr = 1;
   1991  1.16    mrg 	}
   1992  1.16    mrg 	if ((cp = bozo_get_pref(prefs, "bind address")) != NULL) {
   1993  1.16    mrg 		httpd->bindaddress = strdup(cp);
   1994  1.16    mrg 	}
   1995  1.16    mrg 	if ((cp = bozo_get_pref(prefs, "background")) != NULL) {
   1996  1.16    mrg 		httpd->background = atoi(cp);
   1997  1.16    mrg 	}
   1998  1.16    mrg 	if ((cp = bozo_get_pref(prefs, "foreground")) != NULL &&
   1999  1.16    mrg 	    strcmp(cp, "true") == 0) {
   2000  1.16    mrg 		httpd->foreground = 1;
   2001  1.16    mrg 	}
   2002  1.16    mrg 	if ((cp = bozo_get_pref(prefs, "unknown slash")) != NULL &&
   2003  1.16    mrg 	    strcmp(cp, "true") == 0) {
   2004  1.16    mrg 		httpd->unknown_slash = 1;
   2005  1.16    mrg 	}
   2006  1.16    mrg 	if ((cp = bozo_get_pref(prefs, "virtual base")) != NULL) {
   2007  1.16    mrg 		httpd->virtbase = strdup(cp);
   2008  1.16    mrg 	}
   2009  1.16    mrg 	if ((cp = bozo_get_pref(prefs, "enable users")) != NULL &&
   2010  1.16    mrg 	    strcmp(cp, "true") == 0) {
   2011  1.16    mrg 		httpd->enable_users = 1;
   2012  1.16    mrg 	}
   2013  1.16    mrg 	if ((cp = bozo_get_pref(prefs, "dirty environment")) != NULL &&
   2014  1.16    mrg 	    strcmp(cp, "true") == 0) {
   2015  1.16    mrg 		dirtyenv = 1;
   2016  1.16    mrg 	}
   2017  1.16    mrg 	if ((cp = bozo_get_pref(prefs, "hide dots")) != NULL &&
   2018  1.16    mrg 	    strcmp(cp, "true") == 0) {
   2019  1.16    mrg 		httpd->hide_dots = 1;
   2020  1.16    mrg 	}
   2021  1.16    mrg 	if ((cp = bozo_get_pref(prefs, "directory indexing")) != NULL &&
   2022  1.16    mrg 	    strcmp(cp, "true") == 0) {
   2023  1.16    mrg 		httpd->dir_indexing = 1;
   2024  1.16    mrg 	}
   2025  1.20    mrg 	if ((cp = bozo_get_pref(prefs, "public_html")) != NULL) {
   2026  1.20    mrg 		httpd->public_html = strdup(cp);
   2027  1.20    mrg 	}
   2028  1.16    mrg 	httpd->server_software =
   2029  1.16    mrg 			strdup(bozo_get_pref(prefs, "server software"));
   2030  1.16    mrg 	httpd->index_html = strdup(bozo_get_pref(prefs, "index.html"));
   2031  1.16    mrg 
   2032  1.16    mrg 	/*
   2033  1.16    mrg 	 * initialise ssl and daemon mode if necessary.
   2034  1.16    mrg 	 */
   2035  1.16    mrg 	bozo_ssl_init(httpd);
   2036  1.16    mrg 	bozo_daemon_init(httpd);
   2037  1.16    mrg 
   2038  1.16    mrg 	if ((username = bozo_get_pref(prefs, "username")) == NULL) {
   2039  1.16    mrg 		if ((pw = getpwuid(uid = 0)) == NULL)
   2040  1.16    mrg 			bozo_err(httpd, 1, "getpwuid(0): %s", strerror(errno));
   2041  1.16    mrg 		httpd->username = strdup(pw->pw_name);
   2042  1.16    mrg 	} else {
   2043  1.16    mrg 		httpd->username = strdup(username);
   2044  1.16    mrg 		if ((pw = getpwnam(httpd->username)) == NULL)
   2045  1.16    mrg 			bozo_err(httpd, 1, "getpwnam(%s): %s", httpd->username,
   2046  1.16    mrg 					strerror(errno));
   2047  1.16    mrg 		if (initgroups(pw->pw_name, pw->pw_gid) == -1)
   2048  1.16    mrg 			bozo_err(httpd, 1, "initgroups: %s", strerror(errno));
   2049  1.16    mrg 		if (setgid(pw->pw_gid) == -1)
   2050  1.16    mrg 			bozo_err(httpd, 1, "setgid(%u): %s", pw->pw_gid,
   2051  1.16    mrg 					strerror(errno));
   2052  1.16    mrg 		uid = pw->pw_uid;
   2053  1.16    mrg 	}
   2054  1.16    mrg 	/*
   2055  1.16    mrg 	 * handle chroot.
   2056  1.16    mrg 	 */
   2057  1.16    mrg 	if ((chrootdir = bozo_get_pref(prefs, "chroot dir")) != NULL) {
   2058  1.16    mrg 		httpd->rootdir = strdup(chrootdir);
   2059  1.16    mrg 		if (chdir(httpd->rootdir) == -1)
   2060  1.16    mrg 			bozo_err(httpd, 1, "chdir(%s): %s", httpd->rootdir,
   2061  1.16    mrg 				strerror(errno));
   2062  1.16    mrg 		if (chroot(httpd->rootdir) == -1)
   2063  1.16    mrg 			bozo_err(httpd, 1, "chroot(%s): %s", httpd->rootdir,
   2064  1.16    mrg 				strerror(errno));
   2065  1.16    mrg 	}
   2066  1.16    mrg 
   2067  1.16    mrg 	if (username != NULL)
   2068  1.16    mrg 		if (setuid(uid) == -1)
   2069  1.16    mrg 			bozo_err(httpd, 1, "setuid(%d): %s", uid,
   2070  1.16    mrg 					strerror(errno));
   2071  1.16    mrg 
   2072  1.16    mrg 	/*
   2073  1.16    mrg 	 * prevent info leakage between different compartments.
   2074  1.16    mrg 	 * some PATH values in the environment would be invalided
   2075  1.16    mrg 	 * by chroot. cross-user settings might result in undesirable
   2076  1.16    mrg 	 * effects.
   2077  1.16    mrg 	 */
   2078  1.21    mrg 	if ((chrootdir != NULL || username != NULL) && !dirtyenv)
   2079  1.16    mrg 		environ = cleanenv;
   2080  1.21    mrg 
   2081  1.16    mrg #ifdef _SC_PAGESIZE
   2082  1.16    mrg 	httpd->page_size = (long)sysconf(_SC_PAGESIZE);
   2083  1.16    mrg #else
   2084  1.16    mrg 	httpd->page_size = 4096;
   2085  1.16    mrg #endif
   2086  1.16    mrg 	debug((httpd, DEBUG_OBESE, "myname is %s, slashdir is %s",
   2087  1.16    mrg 			httpd->virthostname, httpd->slashdir));
   2088  1.16    mrg 
   2089  1.16    mrg 	return 1;
   2090  1.16    mrg }
   2091