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