Home | History | Annotate | Line # | Download | only in httpd
cgi-bozo.c revision 1.1.1.5
      1  1.1.1.5  mrg /*	$eterna: cgi-bozo.c,v 1.32 2009/05/22 21:51:38 mrg Exp $	*/
      2      1.1  tls 
      3      1.1  tls /*
      4  1.1.1.3  mrg  * Copyright (c) 1997-2009 Matthew R. Green
      5      1.1  tls  * All rights reserved.
      6      1.1  tls  *
      7      1.1  tls  * Redistribution and use in source and binary forms, with or without
      8      1.1  tls  * modification, are permitted provided that the following conditions
      9      1.1  tls  * are met:
     10      1.1  tls  * 1. Redistributions of source code must retain the above copyright
     11      1.1  tls  *    notice, this list of conditions and the following disclaimer.
     12      1.1  tls  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  tls  *    notice, this list of conditions and the following disclaimer and
     14      1.1  tls  *    dedication in the documentation and/or other materials provided
     15      1.1  tls  *    with the distribution.
     16      1.1  tls  *
     17      1.1  tls  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18      1.1  tls  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19      1.1  tls  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20      1.1  tls  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21      1.1  tls  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     22      1.1  tls  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23      1.1  tls  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     24      1.1  tls  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     25      1.1  tls  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26      1.1  tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27      1.1  tls  * SUCH DAMAGE.
     28      1.1  tls  *
     29      1.1  tls  */
     30      1.1  tls 
     31      1.1  tls /* this code implements CGI/1.2 for bozohttpd */
     32      1.1  tls 
     33      1.1  tls #ifndef NO_CGIBIN_SUPPORT
     34      1.1  tls 
     35      1.1  tls #include <sys/param.h>
     36      1.1  tls #include <sys/socket.h>
     37      1.1  tls 
     38      1.1  tls #include <ctype.h>
     39      1.1  tls #include <errno.h>
     40      1.1  tls #include <paths.h>
     41      1.1  tls #include <signal.h>
     42      1.1  tls #include <stdlib.h>
     43      1.1  tls #include <string.h>
     44  1.1.1.4  mrg #include <syslog.h>
     45      1.1  tls #include <unistd.h>
     46      1.1  tls 
     47      1.1  tls #include <netinet/in.h>
     48      1.1  tls 
     49      1.1  tls #include "bozohttpd.h"
     50      1.1  tls 
     51      1.1  tls #define CGIBIN_PREFIX		"cgi-bin/"
     52      1.1  tls #define CGIBIN_PREFIX_LEN	(sizeof(CGIBIN_PREFIX)-1)
     53      1.1  tls 
     54      1.1  tls static	char	*cgibin;	/* cgi-bin directory */
     55      1.1  tls static	int	Cflag;		/* added a cgi handler, always process_cgi() */
     56      1.1  tls 
     57  1.1.1.2  mrg static	const char *	content_cgihandler(http_req *, const char *);
     58  1.1.1.2  mrg static	void		finish_cgi_output(http_req *request, int, int);
     59  1.1.1.2  mrg static	int		parse_header(const char *, ssize_t, char **, char **);
     60  1.1.1.3  mrg static	void		append_index_html(char **);
     61      1.1  tls 
     62      1.1  tls void
     63      1.1  tls set_cgibin(char *path)
     64      1.1  tls {
     65      1.1  tls 	cgibin = path;
     66      1.1  tls 	debug((DEBUG_OBESE, "cgibin (cgi-bin directory) is %s", cgibin));
     67      1.1  tls }
     68      1.1  tls 
     69      1.1  tls /* help build up the environ pointer */
     70      1.1  tls void
     71      1.1  tls spsetenv(const char *env, const char *val, char **envp)
     72      1.1  tls {
     73      1.1  tls 	char *s1 = bozomalloc(strlen(env) + strlen(val) + 2);
     74      1.1  tls 
     75      1.1  tls 	strcpy(s1, env);
     76      1.1  tls 	strcat(s1, "=");
     77      1.1  tls 	strcat(s1, val);
     78      1.1  tls 	debug((DEBUG_OBESE, "spsetenv: %s", s1));
     79      1.1  tls 	*envp = s1;
     80      1.1  tls }
     81      1.1  tls 
     82      1.1  tls /*
     83      1.1  tls  * Checks if the request has asked for a cgi-bin.  Should only be called if
     84      1.1  tls  * cgibin is set.  If it starts CGIBIN_PREFIX or has a ncontent handler,
     85  1.1.1.3  mrg  * process the cgi, otherwise just return.  Returns 0 if it did not handle
     86  1.1.1.3  mrg  * the request.
     87      1.1  tls  */
     88  1.1.1.3  mrg int
     89      1.1  tls process_cgi(http_req *request)
     90      1.1  tls {
     91      1.1  tls 	char	buf[WRSZ];
     92      1.1  tls 	struct	headers *headp;
     93      1.1  tls 	const char *type, *clen, *info, *cgihandler;
     94  1.1.1.3  mrg 	char	*query, *s, *t, *path, *env, *command, *file, *url;
     95      1.1  tls 	char	**envp, **curenvp, *argv[4];
     96      1.1  tls 	size_t	len;
     97      1.1  tls 	ssize_t rbytes;
     98      1.1  tls 	pid_t	pid;
     99      1.1  tls 	int	envpsize, ix, nph;
    100      1.1  tls 	int	sv[2];
    101      1.1  tls 
    102      1.1  tls 	if (!cgibin && !Cflag)
    103  1.1.1.3  mrg 		return 0;
    104      1.1  tls 
    105  1.1.1.3  mrg 	asprintf(&file, "/%s", request->hr_file);
    106  1.1.1.3  mrg 	if (file == NULL)
    107  1.1.1.3  mrg 		return 0;
    108  1.1.1.3  mrg 	if (request->hr_query && strlen(request->hr_query))
    109  1.1.1.3  mrg 		query = bozostrdup(request->hr_query);
    110  1.1.1.3  mrg 	else
    111      1.1  tls 		query = NULL;
    112  1.1.1.3  mrg 
    113  1.1.1.3  mrg 	asprintf(&url, "%s%s%s", file, query ? "?" : "", query ? query : "");
    114  1.1.1.3  mrg 	if (url == NULL)
    115  1.1.1.3  mrg 		goto out;
    116  1.1.1.3  mrg 	debug((DEBUG_NORMAL, "process_cgi: url `%s'", url));
    117  1.1.1.3  mrg 
    118      1.1  tls 	path = NULL;
    119      1.1  tls 	envp = NULL;
    120      1.1  tls 	cgihandler = NULL;
    121      1.1  tls 	command = NULL;
    122      1.1  tls 	info = NULL;
    123      1.1  tls 
    124      1.1  tls 	len = strlen(url);
    125      1.1  tls 
    126  1.1.1.3  mrg 	if (auth_check(request, url + 1))
    127  1.1.1.3  mrg 		goto out;
    128      1.1  tls 
    129      1.1  tls 	if (!cgibin || strncmp(url + 1, CGIBIN_PREFIX, CGIBIN_PREFIX_LEN) != 0) {
    130  1.1.1.3  mrg 		cgihandler = content_cgihandler(request, file + 1);
    131      1.1  tls 		if (cgihandler == NULL) {
    132  1.1.1.3  mrg 			debug((DEBUG_FAT, "process_cgi: no handler, returning"));
    133  1.1.1.3  mrg 			goto out;
    134      1.1  tls 		}
    135  1.1.1.3  mrg 		if (len == 0 || file[len - 1] == '/')
    136  1.1.1.3  mrg 			append_index_html(&file);
    137      1.1  tls 		debug((DEBUG_NORMAL, "process_cgi: cgihandler `%s'",
    138      1.1  tls 		    cgihandler));
    139  1.1.1.3  mrg 	} else if (len - 1 == CGIBIN_PREFIX_LEN)	/* url is "/cgi-bin/" */
    140  1.1.1.3  mrg 		append_index_html(&file);
    141      1.1  tls 
    142      1.1  tls 	ix = 0;
    143      1.1  tls 	if (cgihandler) {
    144  1.1.1.3  mrg 		command = file + 1;
    145      1.1  tls 		path = bozostrdup(cgihandler);
    146      1.1  tls 		argv[ix++] = path;
    147      1.1  tls 			/* argv[] = [ path, command, query, NULL ] */
    148      1.1  tls 	} else {
    149  1.1.1.3  mrg 		command = file + CGIBIN_PREFIX_LEN + 1;
    150      1.1  tls 		if ((s = strchr(command, '/')) != NULL) {
    151      1.1  tls 			info = bozostrdup(s);
    152      1.1  tls 			*s = '\0';
    153      1.1  tls 		}
    154      1.1  tls 		path = bozomalloc(strlen(cgibin) + 1 + strlen(command) + 1);
    155      1.1  tls 		strcpy(path, cgibin);
    156      1.1  tls 		strcat(path, "/");
    157      1.1  tls 		strcat(path, command);
    158      1.1  tls 			/* argv[] = [ command, query, NULL ] */
    159      1.1  tls 	}
    160      1.1  tls 	argv[ix++] = command;
    161      1.1  tls 	argv[ix++] = query;
    162      1.1  tls 	argv[ix++] = NULL;
    163      1.1  tls 
    164      1.1  tls 	nph = strncmp(command, "nph-", 4) == 0;
    165      1.1  tls 
    166      1.1  tls 	type = request->hr_content_type;
    167      1.1  tls 	clen = request->hr_content_length;
    168      1.1  tls 
    169      1.1  tls 	envpsize = 13 + request->hr_nheaders +
    170      1.1  tls 	    (info && *info ? 1 : 0) +
    171      1.1  tls 	    (query && *query ? 1 : 0) +
    172      1.1  tls 	    (type && *type ? 1 : 0) +
    173      1.1  tls 	    (clen && *clen ? 1 : 0) +
    174      1.1  tls 	    (request->hr_remotehost && *request->hr_remotehost ? 1 : 0) +
    175      1.1  tls 	    (request->hr_remoteaddr && *request->hr_remoteaddr ? 1 : 0) +
    176      1.1  tls 	    auth_cgi_count(request) +
    177      1.1  tls 	    (request->hr_serverport && *request->hr_serverport ? 1 : 0);
    178      1.1  tls 
    179  1.1.1.5  mrg 	debug((DEBUG_FAT,
    180  1.1.1.5  mrg 	    "process_cgi: path `%s' cmd `%s' info `%s' query `%s' nph `%d' envpsize `%d'",
    181  1.1.1.5  mrg 	    path, command, strornull(info), strornull(query), nph, envpsize));
    182  1.1.1.5  mrg 
    183      1.1  tls 	envp = bozomalloc(sizeof(*envp) * envpsize);
    184      1.1  tls 	for (ix = 0; ix < envpsize; ix++)
    185      1.1  tls 		envp[ix] = NULL;
    186      1.1  tls 	curenvp = envp;
    187  1.1.1.2  mrg 
    188  1.1.1.2  mrg 	SIMPLEQ_FOREACH(headp, &request->hr_headers, h_next) {
    189      1.1  tls 		const char *s2;
    190      1.1  tls 		env = bozomalloc(6 + strlen(headp->h_header) + 1 +
    191      1.1  tls 		    strlen(headp->h_value));
    192      1.1  tls 
    193      1.1  tls 		t = env;
    194      1.1  tls 		strcpy(t, "HTTP_");
    195      1.1  tls 		t += strlen(t);
    196      1.1  tls 		for (s2 = headp->h_header; *s2; t++, s2++)
    197      1.1  tls 			if (islower((u_int)*s2))
    198      1.1  tls 				*t = toupper((u_int)*s2);
    199      1.1  tls 			else if (*s2 == '-')
    200      1.1  tls 				*t = '_';
    201      1.1  tls 			else
    202      1.1  tls 				*t = *s2;
    203      1.1  tls 		*t = '\0';
    204      1.1  tls 		debug((DEBUG_OBESE, "setting header %s as %s = %s",
    205      1.1  tls 		    headp->h_header, env, headp->h_value));
    206      1.1  tls 		spsetenv(env, headp->h_value, curenvp++);
    207      1.1  tls 		free(env);
    208      1.1  tls 	}
    209  1.1.1.5  mrg 
    210      1.1  tls #ifndef _PATH_DEFPATH
    211      1.1  tls #define _PATH_DEFPATH "/usr/bin:/bin"
    212      1.1  tls #endif
    213      1.1  tls 
    214      1.1  tls 	spsetenv("PATH", _PATH_DEFPATH, curenvp++);
    215      1.1  tls 	spsetenv("IFS", " \t\n", curenvp++);
    216      1.1  tls 	spsetenv("SERVER_NAME", myname, curenvp++);
    217      1.1  tls 	spsetenv("GATEWAY_INTERFACE", "CGI/1.1", curenvp++);
    218      1.1  tls 	spsetenv("SERVER_PROTOCOL", request->hr_proto, curenvp++);
    219      1.1  tls 	spsetenv("REQUEST_METHOD", request->hr_methodstr, curenvp++);
    220  1.1.1.3  mrg 	spsetenv("SCRIPT_NAME", file, curenvp++);
    221  1.1.1.3  mrg 	spsetenv("SCRIPT_FILENAME", file + 1, curenvp++);
    222      1.1  tls 	spsetenv("SERVER_SOFTWARE", server_software, curenvp++);
    223  1.1.1.3  mrg 	spsetenv("REQUEST_URI", request->hr_file, curenvp++);
    224      1.1  tls 	spsetenv("DATE_GMT", http_date(), curenvp++);
    225      1.1  tls 	if (query && *query)
    226      1.1  tls 		spsetenv("QUERY_STRING", query, curenvp++);
    227      1.1  tls 	if (info && *info)
    228      1.1  tls 		spsetenv("PATH_INFO", info, curenvp++);
    229      1.1  tls 	if (type && *type)
    230      1.1  tls 		spsetenv("CONTENT_TYPE", type, curenvp++);
    231      1.1  tls 	if (clen && *clen)
    232      1.1  tls 		spsetenv("CONTENT_LENGTH", clen, curenvp++);
    233      1.1  tls 	if (request->hr_serverport && *request->hr_serverport)
    234      1.1  tls 		spsetenv("SERVER_PORT", request->hr_serverport, curenvp++);
    235      1.1  tls 	if (request->hr_remotehost && *request->hr_remotehost)
    236      1.1  tls 		spsetenv("REMOTE_HOST", request->hr_remotehost, curenvp++);
    237      1.1  tls 	if (request->hr_remoteaddr && *request->hr_remoteaddr)
    238      1.1  tls 		spsetenv("REMOTE_ADDR", request->hr_remoteaddr, curenvp++);
    239      1.1  tls 	auth_cgi_setenv(request, &curenvp);
    240      1.1  tls 
    241  1.1.1.3  mrg 	free(file);
    242  1.1.1.3  mrg 	free(url);
    243  1.1.1.3  mrg 
    244      1.1  tls 	debug((DEBUG_FAT, "process_cgi: going exec %s, %s %s %s",
    245      1.1  tls 	    path, argv[0], strornull(argv[1]), strornull(argv[2])));
    246      1.1  tls 
    247      1.1  tls 	if (-1 == socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sv))
    248      1.1  tls 		error(1, "child socketpair failed: %s", strerror(errno));
    249      1.1  tls 
    250      1.1  tls 	/*
    251      1.1  tls 	 * We create 2 procs: one to become the CGI, one read from
    252      1.1  tls 	 * the CGI and output to the network, and this parent will
    253      1.1  tls 	 * continue reading from the network and writing to the
    254      1.1  tls 	 * CGI procsss.
    255      1.1  tls 	 */
    256      1.1  tls 	switch (fork()) {
    257      1.1  tls 	case -1: /* eep, failure */
    258      1.1  tls 		error(1, "child fork failed: %s", strerror(errno));
    259      1.1  tls 	case 0:
    260      1.1  tls 		close(sv[0]);
    261      1.1  tls 		dup2(sv[1], STDIN_FILENO);
    262      1.1  tls 		dup2(sv[1], STDOUT_FILENO);
    263  1.1.1.4  mrg 		close(2);
    264  1.1.1.4  mrg 		close(sv[1]);
    265  1.1.1.4  mrg 		closelog();
    266  1.1.1.5  mrg 		daemon_closefds();
    267      1.1  tls 
    268      1.1  tls 		if (-1 == execve(path, argv, envp))
    269  1.1.1.4  mrg 			error(1, "child exec failed: %s: %s",
    270  1.1.1.4  mrg 			      path, strerror(errno));
    271      1.1  tls 		/* NOT REACHED */
    272      1.1  tls 		error(1, "child execve returned?!");
    273      1.1  tls 	}
    274      1.1  tls 
    275      1.1  tls 	close(sv[1]);
    276      1.1  tls 
    277      1.1  tls 	/* parent: read from stdin (bozoread()) write to sv[0] */
    278      1.1  tls 	/* child: read from sv[0] (bozowrite()) write to stdout */
    279      1.1  tls 	pid = fork();
    280      1.1  tls 	if (pid == -1)
    281      1.1  tls 		error(1, "io child fork failed: %s", strerror(errno));
    282      1.1  tls 	else if (pid == 0) {
    283      1.1  tls 		/* child reader/writer */
    284      1.1  tls 		close(STDIN_FILENO);
    285      1.1  tls 		finish_cgi_output(request, sv[0], nph);
    286      1.1  tls 		/* if we're done output, our parent is useless... */
    287      1.1  tls 		kill(getppid(), SIGKILL);
    288      1.1  tls 		debug((DEBUG_FAT, "done processing cgi output"));
    289      1.1  tls 		_exit(0);
    290      1.1  tls 	}
    291      1.1  tls 	close(STDOUT_FILENO);
    292      1.1  tls 
    293      1.1  tls 	/* XXX we should have some goo that times us out
    294      1.1  tls 	 */
    295      1.1  tls 	while ((rbytes = bozoread(STDIN_FILENO, buf, sizeof buf)) > 0) {
    296      1.1  tls 		ssize_t wbytes;
    297      1.1  tls 		char *bp = buf;
    298      1.1  tls 
    299      1.1  tls 		while (rbytes) {
    300      1.1  tls 			wbytes = write(sv[0], buf , rbytes);
    301      1.1  tls 			if (wbytes > 0) {
    302      1.1  tls 				rbytes -= wbytes;
    303      1.1  tls 				bp += wbytes;
    304      1.1  tls 			} else
    305      1.1  tls 				error(1, "write failed: %s", strerror(errno));
    306      1.1  tls 		}
    307      1.1  tls 	}
    308      1.1  tls 	debug((DEBUG_FAT, "done processing cgi input"));
    309      1.1  tls 	exit(0);
    310  1.1.1.3  mrg 
    311  1.1.1.3  mrg  out:
    312  1.1.1.3  mrg 	if (query)
    313  1.1.1.3  mrg 		free(query);
    314  1.1.1.3  mrg 	if (file)
    315  1.1.1.3  mrg 		free(file);
    316  1.1.1.3  mrg 	if (url)
    317  1.1.1.3  mrg 		free(url);
    318  1.1.1.3  mrg 	return 0;
    319      1.1  tls }
    320      1.1  tls 
    321      1.1  tls /*
    322      1.1  tls  * handle parsing a CGI header output, transposing a Status: header
    323      1.1  tls  * into the HTTP reply (ie, instead of "200 OK").
    324      1.1  tls  */
    325      1.1  tls static void
    326      1.1  tls finish_cgi_output(http_req *request, int in, int nph)
    327      1.1  tls {
    328      1.1  tls 	char	buf[WRSZ];
    329  1.1.1.2  mrg 	char	*str;
    330  1.1.1.2  mrg 	ssize_t	len;
    331      1.1  tls 	ssize_t rbytes;
    332      1.1  tls 	SIMPLEQ_HEAD(, headers)	headers;
    333  1.1.1.3  mrg 	struct	headers *hdr, *nhdr;
    334  1.1.1.2  mrg 	int	write_header, nheaders = 0;
    335      1.1  tls 
    336      1.1  tls 	/* much of this code is like read_request()'s header loop. hmmm... */
    337      1.1  tls 	SIMPLEQ_INIT(&headers);
    338      1.1  tls 	write_header = nph == 0;
    339  1.1.1.2  mrg 	while (nph == 0 && (str = bozodgetln(in, &len, read)) != NULL) {
    340  1.1.1.3  mrg 		char	*hdr_name, *hdr_value;
    341      1.1  tls 
    342  1.1.1.2  mrg 		if (parse_header(str, len, &hdr_name, &hdr_value))
    343      1.1  tls 			break;
    344      1.1  tls 
    345      1.1  tls 		/*
    346      1.1  tls 		 * The CGI 1.{1,2} spec both say that if the cgi program
    347      1.1  tls 		 * returns a `Status:' header field then the server MUST
    348      1.1  tls 		 * return it in the response.  If the cgi program does
    349      1.1  tls 		 * not return any `Status:' header then the server should
    350      1.1  tls 		 * respond with 200 OK.
    351      1.1  tls 		 * XXX The CGI 1.1 and 1.2 specification differ slightly on
    352      1.1  tls 		 * this in that v1.2 says that the script MUST NOT return a
    353      1.1  tls 		 * `Status:' header if it is returning a `Location:' header.
    354      1.1  tls 		 * For compatibility we are going with the CGI 1.1 behavior.
    355      1.1  tls 		 */
    356  1.1.1.2  mrg 		if (strcasecmp(hdr_name, "status") == 0) {
    357      1.1  tls 			debug((DEBUG_OBESE, "process_cgi:  writing HTTP header "
    358  1.1.1.2  mrg 					    "from status %s ..", hdr_value));
    359  1.1.1.2  mrg 			bozoprintf("%s %s\r\n", request->hr_proto, hdr_value);
    360      1.1  tls 			bozoflush(stdout);
    361      1.1  tls 			write_header = 0;
    362  1.1.1.2  mrg 			free(hdr_name);
    363      1.1  tls 			break;
    364      1.1  tls 		}
    365      1.1  tls 
    366      1.1  tls 		hdr = bozomalloc(sizeof *hdr);
    367  1.1.1.2  mrg 		hdr->h_header = hdr_name;
    368  1.1.1.2  mrg 		hdr->h_value = hdr_value;
    369      1.1  tls 		SIMPLEQ_INSERT_TAIL(&headers, hdr, h_next);
    370      1.1  tls 		nheaders++;
    371      1.1  tls 	}
    372      1.1  tls 
    373      1.1  tls 	if (write_header) {
    374      1.1  tls 		debug((DEBUG_OBESE, "process_cgi:  writing HTTP header .."));
    375      1.1  tls 		bozoprintf("%s 200 OK\r\n", request->hr_proto);
    376      1.1  tls 		bozoflush(stdout);
    377      1.1  tls 	}
    378      1.1  tls 
    379      1.1  tls 	if (nheaders) {
    380      1.1  tls 		debug((DEBUG_OBESE, "process_cgi:  writing delayed HTTP "
    381      1.1  tls 				    "headers .."));
    382  1.1.1.3  mrg 		SIMPLEQ_FOREACH_SAFE(hdr, &headers, h_next, nhdr) {
    383  1.1.1.2  mrg 			bozoprintf("%s: %s\r\n", hdr->h_header, hdr->h_value);
    384  1.1.1.2  mrg 			free(hdr->h_header);
    385  1.1.1.2  mrg 			free(hdr);
    386      1.1  tls 		}
    387  1.1.1.2  mrg 		bozoprintf("\r\n");
    388      1.1  tls 		bozoflush(stdout);
    389      1.1  tls 	}
    390      1.1  tls 
    391      1.1  tls 	/* XXX we should have some goo that times us out
    392      1.1  tls 	 */
    393      1.1  tls 	while ((rbytes = read(in, buf, sizeof buf)) > 0) {
    394      1.1  tls 		ssize_t wbytes;
    395      1.1  tls 		char *bp = buf;
    396      1.1  tls 
    397      1.1  tls 		while (rbytes) {
    398      1.1  tls 			wbytes = bozowrite(STDOUT_FILENO, buf, rbytes);
    399      1.1  tls 			if (wbytes > 0) {
    400      1.1  tls 				rbytes -= wbytes;
    401      1.1  tls 				bp += wbytes;
    402      1.1  tls 			} else
    403      1.1  tls 				error(1, "cgi output write failed: %s",
    404      1.1  tls 				    strerror(errno));
    405      1.1  tls 		}
    406      1.1  tls 	}
    407      1.1  tls }
    408      1.1  tls 
    409  1.1.1.2  mrg static int
    410  1.1.1.3  mrg parse_header(const char *str, ssize_t len, char **hdr_str, char **hdr_val)
    411  1.1.1.2  mrg {
    412  1.1.1.3  mrg 	char	*name, *value;
    413  1.1.1.2  mrg 
    414  1.1.1.2  mrg 	/* if the string passed is zero-length bail out */
    415  1.1.1.2  mrg 	if (*str == '\0')
    416  1.1.1.2  mrg 		return -1;
    417  1.1.1.2  mrg 
    418  1.1.1.3  mrg 	value = bozostrdup(str);
    419  1.1.1.2  mrg 
    420  1.1.1.2  mrg 	/* locate the ':' separator in the header/value */
    421  1.1.1.2  mrg 	name = bozostrnsep(&value, ":", &len);
    422  1.1.1.2  mrg 
    423  1.1.1.2  mrg 	if (NULL == name || -1 == len) {
    424  1.1.1.2  mrg 		free(name);
    425  1.1.1.2  mrg 		return -1;
    426  1.1.1.2  mrg 	}
    427  1.1.1.2  mrg 
    428  1.1.1.2  mrg 	/* skip leading space/tab */
    429  1.1.1.2  mrg 	while (*value == ' ' || *value == '\t')
    430  1.1.1.2  mrg 		len--, value++;
    431  1.1.1.2  mrg 
    432  1.1.1.2  mrg 	*hdr_str = name;
    433  1.1.1.2  mrg 	*hdr_val = value;
    434  1.1.1.2  mrg 
    435  1.1.1.2  mrg 	return 0;
    436  1.1.1.2  mrg }
    437  1.1.1.2  mrg 
    438      1.1  tls /*
    439      1.1  tls  * given the file name, return a CGI interpreter
    440      1.1  tls  */
    441      1.1  tls static const char *
    442      1.1  tls content_cgihandler(http_req *request, const char *file)
    443      1.1  tls {
    444      1.1  tls 	struct	content_map	*map;
    445      1.1  tls 
    446  1.1.1.3  mrg 	debug((DEBUG_FAT, "content_cgihandler: trying file %s", file));
    447      1.1  tls 	map = match_content_map(file, 0);
    448      1.1  tls 	if (map)
    449      1.1  tls 		return (map->cgihandler);
    450      1.1  tls 	return (NULL);
    451      1.1  tls }
    452      1.1  tls 
    453  1.1.1.3  mrg static void
    454  1.1.1.3  mrg append_index_html(char **url)
    455  1.1.1.3  mrg {
    456  1.1.1.3  mrg 	*url = bozorealloc(*url, strlen(*url) + strlen(index_html) + 1);
    457  1.1.1.3  mrg 	strcat(*url, index_html);
    458  1.1.1.3  mrg 	debug((DEBUG_NORMAL, "append_index_html: url adjusted to `%s'", *url));
    459  1.1.1.3  mrg }
    460  1.1.1.3  mrg 
    461      1.1  tls #ifndef NO_DYNAMIC_CONTENT
    462      1.1  tls /* cgi maps are simple ".postfix /path/to/prog" */
    463      1.1  tls void
    464      1.1  tls add_content_map_cgi(char *arg, char *cgihandler)
    465      1.1  tls {
    466      1.1  tls 	struct content_map *map;
    467      1.1  tls 
    468      1.1  tls 	debug((DEBUG_NORMAL, "add_content_map_cgi: name %s cgi %s", arg, cgihandler));
    469      1.1  tls 
    470      1.1  tls 	Cflag = 1;
    471      1.1  tls 
    472      1.1  tls 	map = get_content_map(arg);
    473      1.1  tls 	map->name = arg;
    474      1.1  tls 	map->type = map->encoding = map->encoding11 = NULL;
    475      1.1  tls 	map->cgihandler = cgihandler;
    476      1.1  tls }
    477      1.1  tls #endif /* NO_DYNAMIC_CONTENT */
    478      1.1  tls 
    479      1.1  tls #endif /* NO_CGIBIN_SUPPORT */
    480