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