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