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