Home | History | Annotate | Line # | Download | only in httpd
bozohttpd.h revision 1.33
      1 /*	$NetBSD: bozohttpd.h,v 1.33 2014/07/17 06:27:52 mrg Exp $	*/
      2 
      3 /*	$eterna: bozohttpd.h,v 1.39 2011/11/18 09:21:15 mrg Exp $	*/
      4 
      5 /*
      6  * Copyright (c) 1997-2014 Matthew R. Green
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer and
     16  *    dedication in the documentation and/or other materials provided
     17  *    with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  */
     32 #ifndef BOZOHTTOPD_H_
     33 #define BOZOHTTOPD_H_	1
     34 
     35 #include "netbsd_queue.h"
     36 
     37 #include <sys/stat.h>
     38 
     39 #ifndef NO_LUA_SUPPORT
     40 #include <lua.h>
     41 #endif
     42 #include <stdio.h>
     43 
     44 /* lots of "const" but gets free()'ed etc at times, sigh */
     45 
     46 /* headers */
     47 typedef struct bozoheaders {
     48 	/*const*/ char *h_header;
     49 	/*const*/ char *h_value;	/* this gets free()'ed etc at times */
     50 	SIMPLEQ_ENTRY(bozoheaders)	h_next;
     51 } bozoheaders_t;
     52 
     53 #ifndef NO_LUA_SUPPORT
     54 typedef struct lua_handler {
     55 	const char	*name;
     56 	int		 ref;
     57 	SIMPLEQ_ENTRY(lua_handler)	h_next;
     58 } lua_handler_t;
     59 
     60 typedef struct lua_state_map {
     61 	const char 	*script;
     62 	const char	*prefix;
     63 	lua_State	*L;
     64 	SIMPLEQ_HEAD(, lua_handler)	handlers;
     65 	SIMPLEQ_ENTRY(lua_state_map)	s_next;
     66 } lua_state_map_t;
     67 #endif
     68 
     69 typedef struct bozo_content_map_t {
     70 	const char	*name;		/* postfix of file */
     71 	size_t	 	 namelen;	/* length of postfix */
     72 	const char	*type;		/* matching content-type */
     73 	const char	*encoding;	/* matching content-encoding */
     74 	const char	*encoding11;	/* matching content-encoding (HTTP/1.1) */
     75 	const char	*cgihandler;	/* optional CGI handler */
     76 } bozo_content_map_t;
     77 
     78 /* this struct holds the bozo constants */
     79 typedef struct bozo_consts_t {
     80 	const char	*http_09;	/* "HTTP/0.9" */
     81 	const char	*http_10;	/* "HTTP/1.0" */
     82 	const char	*http_11;	/* "HTTP/1.1" */
     83 	const char	*text_plain;	/* "text/plain" */
     84 } bozo_consts_t;
     85 
     86 /* this structure encapsulates all the bozo flags and control vars */
     87 typedef struct bozohttpd_t {
     88 	char		*rootdir;	/* root directory */
     89 	char		*username;	/* username to switch to */
     90 	int		 numeric;	/* avoid gethostby*() */
     91 	char		*virtbase;	/* virtual directory base */
     92 	int		 unknown_slash;	/* unknown vhosts go to normal slashdir */
     93 	int		 untrustedref;	/* make sure referrer = me unless url = / */
     94 	int		 logstderr;	/* log to stderr (even if not tty) */
     95 	int		 background;	/* drop into daemon mode */
     96 	int		 foreground;	/* keep daemon mode in foreground */
     97 	char		*pidfile;	/* path to the pid file, if any */
     98 	size_t		 page_size;	/* page size */
     99 	char		*slashdir;	/* www slash directory */
    100 	char		*bindport;	/* bind port; default "http" */
    101 	char		*bindaddress;	/* address for binding - INADDR_ANY */
    102 	int		 debug;		/* debugging level */
    103 	char		*virthostname;	/* my name */
    104 	const char	*server_software;/* our brand :-) */
    105 	const char	*index_html;	/* our home page */
    106 	const char	*public_html;	/* ~user/public_html page */
    107 	int		 enable_users;	/* enable public_html */
    108 	int		*sock;		/* bound sockets */
    109 	int		 nsock;		/* number of above */
    110 	struct pollfd	*fds;		/* current poll fd set */
    111 	int		 request_times;	/* # times a request was processed */
    112 	int		 dir_indexing;	/* handle directories */
    113 	int		 hide_dots;	/* hide .* */
    114 	int		 process_cgi;	/* use the cgi handler */
    115 	char		*cgibin;	/* cgi-bin directory */
    116 #ifndef NO_LUA_SUPPORT
    117 	int		 process_lua;	/* use the Lua handler */
    118 	SIMPLEQ_HEAD(, lua_state_map)	lua_states;
    119 #endif
    120 	void		*sslinfo;	/* pointer to ssl struct */
    121 	int		dynamic_content_map_size;/* size of dyn cont map */
    122 	bozo_content_map_t	*dynamic_content_map;/* dynamic content map */
    123 	size_t		 mmapsz;	/* size of region to mmap */
    124 	char		*getln_buffer;	/* space for getln buffer */
    125 	ssize_t		 getln_buflen;	/* length of allocated space */
    126 	char		*errorbuf;	/* no dynamic allocation allowed */
    127 	bozo_consts_t	 consts;	/* various constants */
    128 } bozohttpd_t;
    129 
    130 /* bozo_httpreq_t */
    131 typedef struct bozo_httpreq_t {
    132 	bozohttpd_t	*hr_httpd;
    133 	int		hr_method;
    134 #define	HTTP_GET	0x01
    135 #define HTTP_POST	0x02
    136 #define HTTP_HEAD	0x03
    137 #define HTTP_OPTIONS	0x04	/* not supported */
    138 #define HTTP_PUT	0x05	/* not supported */
    139 #define HTTP_DELETE	0x06	/* not supported */
    140 #define HTTP_TRACE	0x07	/* not supported */
    141 #define HTTP_CONNECT	0x08	/* not supported */
    142 	const char *hr_methodstr;
    143 	char	*hr_virthostname;	/* server name (if not identical
    144 					   to hr_httpd->virthostname) */
    145 	char	*hr_file;
    146 	char	*hr_oldfile;	/* if we added an index_html */
    147 	char	*hr_query;
    148 	char	*hr_host;	/* HTTP/1.1 Host: or virtual hostname,
    149 				   possibly including a port number */
    150 	const char *hr_proto;
    151 	const char *hr_content_type;
    152 	const char *hr_content_length;
    153 	const char *hr_allow;
    154 	const char *hr_referrer;
    155 	const char *hr_range;
    156 	const char *hr_if_modified_since;
    157 	const char *hr_accept_encoding;
    158 	int         hr_have_range;
    159 	off_t       hr_first_byte_pos;
    160 	off_t       hr_last_byte_pos;
    161 	/*const*/ char *hr_remotehost;
    162 	/*const*/ char *hr_remoteaddr;
    163 	/*const*/ char *hr_serverport;
    164 #ifdef DO_HTPASSWD
    165 	/*const*/ char *hr_authrealm;
    166 	/*const*/ char *hr_authuser;
    167 	/*const*/ char *hr_authpass;
    168 #endif
    169 	SIMPLEQ_HEAD(, bozoheaders)	hr_headers;
    170 	int	hr_nheaders;
    171 } bozo_httpreq_t;
    172 
    173 /* helper to access the "active" host name from a httpd/request pair */
    174 #define	BOZOHOST(HTTPD,REQUEST)	((REQUEST)->hr_virthostname ?		\
    175 					(REQUEST)->hr_virthostname :	\
    176 					(HTTPD)->virthostname)
    177 
    178 /* structure to hold string based (name, value) pairs with preferences */
    179 typedef struct bozoprefs_t {
    180 	unsigned	  size;		/* size of the two arrays */
    181 	unsigned	  c;		/* # of entries in arrays */
    182 	char		**name;		/* names of each entry */
    183 	char		**value;	/* values for the name entries */
    184 } bozoprefs_t;
    185 
    186 /* by default write in upto 64KiB chunks, and mmap in upto 64MiB chunks */
    187 #ifndef BOZO_WRSZ
    188 #define BOZO_WRSZ	(64 * 1024)
    189 #endif
    190 #ifndef BOZO_MMAPSZ
    191 #define BOZO_MMAPSZ	(BOZO_WRSZ * 1024)
    192 #endif
    193 
    194 /* debug flags */
    195 #define DEBUG_NORMAL	1
    196 #define DEBUG_FAT	2
    197 #define DEBUG_OBESE	3
    198 #define DEBUG_EXPLODING	4
    199 
    200 #define	strornull(x)	((x) ? (x) : "<null>")
    201 
    202 #if defined(__GNUC__) && __GNUC__ >= 3
    203 #define BOZO_PRINTFLIKE(x,y) __attribute__((__format__(__printf__, x,y)))
    204 #define BOZO_DEAD __attribute__((__noreturn__))
    205 #endif
    206 
    207 #ifndef NO_DEBUG
    208 void	debug__(bozohttpd_t *, int, const char *, ...) BOZO_PRINTFLIKE(3, 4);
    209 #define debug(x)	debug__ x
    210 #else
    211 #define	debug(x)
    212 #endif /* NO_DEBUG */
    213 
    214 void	bozo_warn(bozohttpd_t *, const char *, ...)
    215 		BOZO_PRINTFLIKE(2, 3);
    216 void	bozo_err(bozohttpd_t *, int, const char *, ...)
    217 		BOZO_PRINTFLIKE(3, 4)
    218 		BOZO_DEAD;
    219 int	bozo_http_error(bozohttpd_t *, int, bozo_httpreq_t *, const char *);
    220 
    221 int	bozo_check_special_files(bozo_httpreq_t *, const char *);
    222 char	*bozo_http_date(char *, size_t);
    223 void	bozo_print_header(bozo_httpreq_t *, struct stat *, const char *, const char *);
    224 char	*bozo_escape_rfc3986(bozohttpd_t *httpd, const char *url);
    225 char	*bozo_escape_html(bozohttpd_t *httpd, const char *url);
    226 
    227 char	*bozodgetln(bozohttpd_t *, int, ssize_t *, ssize_t (*)(bozohttpd_t *, int, void *, size_t));
    228 char	*bozostrnsep(char **, const char *, ssize_t *);
    229 
    230 void	*bozomalloc(bozohttpd_t *, size_t);
    231 void	*bozorealloc(bozohttpd_t *, void *, size_t);
    232 char	*bozostrdup(bozohttpd_t *, const char *);
    233 
    234 /* ssl-bozo.c */
    235 #ifdef NO_SSL_SUPPORT
    236 #define bozo_ssl_set_opts(w, x, y)	do { /* nothing */ } while (0)
    237 #define bozo_ssl_init(x)		do { /* nothing */ } while (0)
    238 #define bozo_ssl_accept(x)		(0)
    239 #define bozo_ssl_destroy(x)		do { /* nothing */ } while (0)
    240 #else
    241 void	bozo_ssl_set_opts(bozohttpd_t *, const char *, const char *);
    242 void	bozo_ssl_init(bozohttpd_t *);
    243 int	bozo_ssl_accept(bozohttpd_t *);
    244 void	bozo_ssl_destroy(bozohttpd_t *);
    245 #endif
    246 
    247 
    248 /* auth-bozo.c */
    249 #ifdef DO_HTPASSWD
    250 int	bozo_auth_check(bozo_httpreq_t *, const char *);
    251 void	bozo_auth_cleanup(bozo_httpreq_t *);
    252 int	bozo_auth_check_headers(bozo_httpreq_t *, char *, char *, ssize_t);
    253 int	bozo_auth_check_special_files(bozo_httpreq_t *, const char *);
    254 void	bozo_auth_check_401(bozo_httpreq_t *, int);
    255 void	bozo_auth_cgi_setenv(bozo_httpreq_t *, char ***);
    256 int	bozo_auth_cgi_count(bozo_httpreq_t *);
    257 #else
    258 #define	bozo_auth_check(x, y)			0
    259 #define	bozo_auth_cleanup(x)			do { /* nothing */ } while (0)
    260 #define	bozo_auth_check_headers(y, z, a, b)	0
    261 #define	bozo_auth_check_special_files(x, y)	0
    262 #define	bozo_auth_check_401(x, y)		do { /* nothing */ } while (0)
    263 #define	bozo_auth_cgi_setenv(x, y)		do { /* nothing */ } while (0)
    264 #define	bozo_auth_cgi_count(x)			0
    265 #endif /* DO_HTPASSWD */
    266 
    267 
    268 /* cgi-bozo.c */
    269 #ifdef NO_CGIBIN_SUPPORT
    270 #define	bozo_process_cgi(h)				0
    271 #else
    272 void	bozo_cgi_setbin(bozohttpd_t *, const char *);
    273 void	bozo_setenv(bozohttpd_t *, const char *, const char *, char **);
    274 int	bozo_process_cgi(bozo_httpreq_t *);
    275 void	bozo_add_content_map_cgi(bozohttpd_t *, const char *, const char *);
    276 #endif /* NO_CGIBIN_SUPPORT */
    277 
    278 
    279 /* lua-bozo.c */
    280 #ifdef NO_LUA_SUPPORT
    281 #define bozo_process_lua(h)				0
    282 #else
    283 void	bozo_add_lua_map(bozohttpd_t *, const char *, const char *);
    284 int	bozo_process_lua(bozo_httpreq_t *);
    285 #endif /* NO_LUA_SUPPORT */
    286 
    287 
    288 /* daemon-bozo.c */
    289 #ifdef NO_DAEMON_MODE
    290 #define bozo_daemon_init(x)				do { /* nothing */ } while (0)
    291 #define bozo_daemon_fork(x)				0
    292 #define bozo_daemon_closefds(x)				do { /* nothing */ } while (0)
    293 #else
    294 void	bozo_daemon_init(bozohttpd_t *);
    295 int	bozo_daemon_fork(bozohttpd_t *);
    296 void	bozo_daemon_closefds(bozohttpd_t *);
    297 #endif /* NO_DAEMON_MODE */
    298 
    299 
    300 /* tilde-luzah-bozo.c */
    301 #ifdef NO_USER_SUPPORT
    302 #define bozo_user_transform(a, c)			0
    303 #else
    304 int	bozo_user_transform(bozo_httpreq_t *, int *);
    305 #endif /* NO_USER_SUPPORT */
    306 
    307 
    308 /* dir-index-bozo.c */
    309 #ifdef NO_DIRINDEX_SUPPORT
    310 #define bozo_dir_index(a, b, c)				0
    311 #else
    312 int	bozo_dir_index(bozo_httpreq_t *, const char *, int);
    313 #endif /* NO_DIRINDEX_SUPPORT */
    314 
    315 
    316 /* content-bozo.c */
    317 const char *bozo_content_type(bozo_httpreq_t *, const char *);
    318 const char *bozo_content_encoding(bozo_httpreq_t *, const char *);
    319 bozo_content_map_t *bozo_match_content_map(bozohttpd_t *, const char *, int);
    320 bozo_content_map_t *bozo_get_content_map(bozohttpd_t *, const char *);
    321 #ifndef NO_DYNAMIC_CONTENT
    322 void	bozo_add_content_map_mime(bozohttpd_t *, const char *, const char *, const char *, const char *);
    323 #endif
    324 
    325 /* I/O */
    326 int bozo_printf(bozohttpd_t *, const char *, ...) BOZO_PRINTFLIKE(2, 3);;
    327 ssize_t bozo_read(bozohttpd_t *, int, void *, size_t);
    328 ssize_t bozo_write(bozohttpd_t *, int, const void *, size_t);
    329 int bozo_flush(bozohttpd_t *, FILE *);
    330 
    331 /* misc */
    332 int bozo_init_httpd(bozohttpd_t *);
    333 int bozo_init_prefs(bozoprefs_t *);
    334 int bozo_set_defaults(bozohttpd_t *, bozoprefs_t *);
    335 int bozo_setup(bozohttpd_t *, bozoprefs_t *, const char *, const char *);
    336 bozo_httpreq_t *bozo_read_request(bozohttpd_t *);
    337 void bozo_process_request(bozo_httpreq_t *);
    338 void bozo_clean_request(bozo_httpreq_t *);
    339 
    340 /* variables */
    341 int bozo_set_pref(bozoprefs_t *, const char *, const char *);
    342 char *bozo_get_pref(bozoprefs_t *, const char *);
    343 
    344 #endif	/* BOZOHTTOPD_H_ */
    345