Home | History | Annotate | Line # | Download | only in httpd
cgi-bozo.c revision 1.35.4.1
      1 /*	$NetBSD: cgi-bozo.c,v 1.35.4.1 2017/04/21 16:53:12 bouyer Exp $	*/
      2 
      3 /*	$eterna: cgi-bozo.c,v 1.40 2011/11/18 09:21:15 mrg Exp $	*/
      4 
      5 /*
      6  * Copyright (c) 1997-2017 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 
     33 /* this code implements CGI/1.2 for bozohttpd */
     34 
     35 #ifndef NO_CGIBIN_SUPPORT
     36 
     37 #include <sys/param.h>
     38 #include <sys/socket.h>
     39 
     40 #include <ctype.h>
     41 #include <errno.h>
     42 #include <paths.h>
     43 #include <signal.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 #include <syslog.h>
     47 #include <unistd.h>
     48 
     49 #include <netinet/in.h>
     50 
     51 #include "bozohttpd.h"
     52 
     53 #define CGIBIN_PREFIX		"cgi-bin/"
     54 #define CGIBIN_PREFIX_LEN	(sizeof(CGIBIN_PREFIX)-1)
     55 
     56 #ifndef USE_ARG
     57 #define USE_ARG(x)	/*LINTED*/(void)&(x)
     58 #endif
     59 
     60 /*
     61  * given the file name, return a CGI interpreter
     62  */
     63 static const char *
     64 content_cgihandler(bozohttpd_t *httpd, bozo_httpreq_t *request,
     65 		const char *file)
     66 {
     67 	bozo_content_map_t	*map;
     68 
     69 	USE_ARG(request);
     70 	debug((httpd, DEBUG_FAT, "content_cgihandler: trying file %s", file));
     71 	map = bozo_match_content_map(httpd, file, 0);
     72 	if (map)
     73 		return map->cgihandler;
     74 	return NULL;
     75 }
     76 
     77 static int
     78 parse_header(bozo_httpreq_t *request, const char *str, ssize_t len,
     79 	     char **hdr_str, char **hdr_val)
     80 {
     81 	struct	bozohttpd_t *httpd = request->hr_httpd;
     82 	char	*name, *value;
     83 
     84 	/* if the string passed is zero-length bail out */
     85 	if (*str == '\0')
     86 		return -1;
     87 
     88 	value = bozostrdup(httpd, request, str);
     89 
     90 	/* locate the ':' separator in the header/value */
     91 	name = bozostrnsep(&value, ":", &len);
     92 
     93 	if (NULL == name || -1 == len) {
     94 		free(value);
     95 		return -1;
     96 	}
     97 
     98 	/* skip leading space/tab */
     99 	while (*value == ' ' || *value == '\t')
    100 		len--, value++;
    101 
    102 	*hdr_str = name;
    103 	*hdr_val = value;
    104 
    105 	return 0;
    106 }
    107 
    108 /*
    109  * handle parsing a CGI header output, transposing a Status: header
    110  * into the HTTP reply (ie, instead of "200 OK").
    111  */
    112 static void
    113 finish_cgi_output(bozohttpd_t *httpd, bozo_httpreq_t *request, int in, int nph)
    114 {
    115 	char	buf[BOZO_WRSZ];
    116 	char	*str;
    117 	ssize_t	len;
    118 	ssize_t rbytes;
    119 	SIMPLEQ_HEAD(, bozoheaders)	headers;
    120 	bozoheaders_t *hdr, *nhdr;
    121 	int	write_header, nheaders = 0;
    122 
    123 	/* much of this code is like bozo_read_request()'s header loop. */
    124 	SIMPLEQ_INIT(&headers);
    125 	write_header = nph == 0;
    126 	/* was read(2) here - XXX - agc */
    127 	while (nph == 0 &&
    128 		(str = bozodgetln(httpd, in, &len, bozo_read)) != NULL) {
    129 		char	*hdr_name, *hdr_value;
    130 
    131 		if (parse_header(request, str, len, &hdr_name, &hdr_value))
    132 			break;
    133 
    134 		/*
    135 		 * The CGI 1.{1,2} spec both say that if the cgi program
    136 		 * returns a `Status:' header field then the server MUST
    137 		 * return it in the response.  If the cgi program does
    138 		 * not return any `Status:' header then the server should
    139 		 * respond with 200 OK.
    140 		 * XXX The CGI 1.1 and 1.2 specification differ slightly on
    141 		 * this in that v1.2 says that the script MUST NOT return a
    142 		 * `Status:' header if it is returning a `Location:' header.
    143 		 * For compatibility we are going with the CGI 1.1 behavior.
    144 		 */
    145 		if (strcasecmp(hdr_name, "status") == 0) {
    146 			debug((httpd, DEBUG_OBESE,
    147 				"bozo_process_cgi:  writing HTTP header "
    148 				"from status %s ..", hdr_value));
    149 			bozo_printf(httpd, "%s %s\r\n", request->hr_proto,
    150 					hdr_value);
    151 			bozo_flush(httpd, stdout);
    152 			write_header = 0;
    153 			free(hdr_name);
    154 			break;
    155 		}
    156 
    157 		hdr = bozomalloc(httpd, sizeof *hdr);
    158 		hdr->h_header = hdr_name;
    159 		hdr->h_value = hdr_value;
    160 		SIMPLEQ_INSERT_TAIL(&headers, hdr, h_next);
    161 		nheaders++;
    162 	}
    163 
    164 	if (write_header) {
    165 		debug((httpd, DEBUG_OBESE,
    166 			"bozo_process_cgi:  writing HTTP header .."));
    167 		bozo_printf(httpd,
    168 			"%s 200 OK\r\n", request->hr_proto);
    169 		bozo_flush(httpd, stdout);
    170 	}
    171 
    172 	if (nheaders) {
    173 		debug((httpd, DEBUG_OBESE,
    174 			"bozo_process_cgi:  writing delayed HTTP headers .."));
    175 		SIMPLEQ_FOREACH_SAFE(hdr, &headers, h_next, nhdr) {
    176 			bozo_printf(httpd, "%s: %s\r\n", hdr->h_header,
    177 					hdr->h_value);
    178 			free(hdr->h_header);
    179 			free(hdr);
    180 		}
    181 		bozo_printf(httpd, "\r\n");
    182 		bozo_flush(httpd, stdout);
    183 	}
    184 
    185 	/* XXX we should have some goo that times us out
    186 	 */
    187 	while ((rbytes = read(in, buf, sizeof buf)) > 0) {
    188 		ssize_t wbytes;
    189 		char *bp = buf;
    190 
    191 		while (rbytes) {
    192 			wbytes = bozo_write(httpd, STDOUT_FILENO, buf,
    193 						(size_t)rbytes);
    194 			if (wbytes > 0) {
    195 				rbytes -= wbytes;
    196 				bp += wbytes;
    197 			} else
    198 				bozoerr(httpd, 1,
    199 					"cgi output write failed: %s",
    200 					strerror(errno));
    201 		}
    202 	}
    203 }
    204 
    205 static void
    206 append_index_html(bozohttpd_t *httpd, char **url)
    207 {
    208 	*url = bozorealloc(httpd, *url,
    209 			strlen(*url) + strlen(httpd->index_html) + 1);
    210 	strcat(*url, httpd->index_html);
    211 	debug((httpd, DEBUG_NORMAL,
    212 		"append_index_html: url adjusted to `%s'", *url));
    213 }
    214 
    215 /* This function parse search-string according to section 4.4 of RFC3875 */
    216 static char **
    217 parse_search_string(bozo_httpreq_t *request, const char *query, size_t *args_len)
    218 {
    219 	struct	bozohttpd_t *httpd = request->hr_httpd;
    220 	size_t i;
    221 	char *s, *str, **args;
    222 
    223 	*args_len = 0;
    224 
    225 	/* URI MUST not contain any unencoded '=' - RFC3875, section 4.4 */
    226 	if (strchr(query, '=')) {
    227 		return NULL;
    228 	}
    229 
    230 	str = bozostrdup(httpd, request, query);
    231 
    232 	/*
    233 	 * there's no more arguments than '+' chars in the query string as it's
    234 	 * the separator
    235 	 */
    236 	*args_len = 1;
    237 	/* count '+' in str */
    238 	for (s = str; (s = strchr(s, '+')); (*args_len)++)
    239 		s++;
    240 
    241 	args = bozomalloc(httpd, sizeof(*args) * (*args_len + 1));
    242 
    243 	args[0] = str;
    244 	args[*args_len] = NULL;
    245 	for (s = str, i = 0; (s = strchr(s, '+'));) {
    246 		*s = '\0';
    247 		s++;
    248 		args[i++] = s;
    249 	}
    250 
    251 	/*
    252 	 * check if search-strings are valid:
    253 	 *
    254 	 * RFC3875, section 4.4:
    255 	 *
    256 	 * search-string = search-word *( "+" search-word )
    257 	 * search-word   = 1*schar
    258 	 * schar		 = unreserved | escaped | xreserved
    259 	 * xreserved	 = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "," |
    260 	 *		   "$"
    261 	 *
    262 	 * section 2.3:
    263 	 *
    264 	 * hex	      = digit | "A" | "B" | "C" | "D" | "E" | "F" | "a" |
    265 	 *		"b" | "c" | "d" | "e" | "f"
    266 	 * escaped    = "%" hex hex
    267 	 * unreserved = alpha | digit | mark
    268 	 * mark	      = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
    269 	 *
    270 	 * section 2.2:
    271 	 *
    272 	 * alpha	= lowalpha | hialpha
    273 	 * lowalpha	= "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" |
    274 	 *		  "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" |
    275 	 *		  "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" |
    276 	 *		  "y" | "z"
    277 	 * hialpha	= "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" |
    278 	 *		  "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" |
    279 	 *		  "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" |
    280 	 *	          "Y" | "Z"
    281 	 * digit        = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
    282 	 *		  "8" | "9"
    283 	 */
    284 #define	UNRESERVED_CHAR	"-_.!~*'()"
    285 #define	XRESERVED_CHAR	";/?:@&=,$"
    286 
    287 	for (i = 0; i < *args_len; i++) {
    288 		s = args[i];
    289 		/* search-word MUST have at least one schar */
    290 		if (*s == '\0')
    291 			goto parse_err;
    292 		while(*s) {
    293 			/* check if it's unreserved */
    294 			if (isalpha((int)*s) || isdigit((int)*s) ||
    295 			    strchr(UNRESERVED_CHAR, *s)) {
    296 				s++;
    297 				continue;
    298 			}
    299 
    300 			/* check if it's escaped */
    301 			if (*s == '%') {
    302 				if (s[1] == '\0' || s[2] == '\0')
    303 					goto parse_err;
    304 				if (!isxdigit((int)s[1]) ||
    305 				    !isxdigit((int)s[2]))
    306 					goto parse_err;
    307 				s += 3;
    308 				continue;
    309 			}
    310 
    311 			/* check if it's xreserved */
    312 
    313 			if (strchr(XRESERVED_CHAR, *s)) {
    314 				s++;
    315 				continue;
    316 			}
    317 
    318 			goto parse_err;
    319 		}
    320 	}
    321 
    322 	/* decode percent encoding */
    323 	for (i = 0; i < *args_len; i++) {
    324 		if (bozo_decode_url_percent(request, args[i]))
    325 			goto parse_err;
    326 	}
    327 
    328 	/* allocate each arg separately */
    329 	for (i = 0; i < *args_len; i++)
    330 		args[i] = bozostrdup(httpd, request, args[i]);
    331 	free(str);
    332 
    333 	return args;
    334 
    335 parse_err:
    336 
    337 	free (str);
    338 	free (*args);
    339 	free(args);
    340 	*args_len = 0;
    341 
    342 	return NULL;
    343 
    344 }
    345 
    346 void
    347 bozo_cgi_setbin(bozohttpd_t *httpd, const char *path)
    348 {
    349 	httpd->cgibin = bozostrdup(httpd, NULL, path);
    350 	debug((httpd, DEBUG_OBESE, "cgibin (cgi-bin directory) is %s",
    351 		httpd->cgibin));
    352 }
    353 
    354 /* help build up the environ pointer */
    355 void
    356 bozo_setenv(bozohttpd_t *httpd, const char *env, const char *val,
    357 		char **envp)
    358 {
    359 	char *s1 = bozomalloc(httpd, strlen(env) + strlen(val) + 2);
    360 
    361 	strcpy(s1, env);
    362 	strcat(s1, "=");
    363 	strcat(s1, val);
    364 	debug((httpd, DEBUG_OBESE, "bozo_setenv: %s", s1));
    365 	*envp = s1;
    366 }
    367 
    368 /*
    369  * Checks if the request has asked for a cgi-bin.  Should only be called if
    370  * cgibin is set.  If it starts CGIBIN_PREFIX or has a ncontent handler,
    371  * process the cgi, otherwise just return.  Returns 0 if it did not handle
    372  * the request.
    373  */
    374 int
    375 bozo_process_cgi(bozo_httpreq_t *request)
    376 {
    377 	bozohttpd_t *httpd = request->hr_httpd;
    378 	char	buf[BOZO_WRSZ];
    379 	char	date[40];
    380 	bozoheaders_t *headp;
    381 	const char *type, *clen, *info, *cgihandler;
    382 	char	*query, *s, *t, *path, *env, *command, *file, *url;
    383 	char	**envp, **curenvp, **argv, **search_string_argv = NULL;
    384 	char	*uri;
    385 	size_t	i, len, search_string_argc = 0;
    386 	ssize_t rbytes;
    387 	pid_t	pid;
    388 	int	envpsize, ix, nph;
    389 	int	sv[2];
    390 
    391 	if (!httpd->cgibin && !httpd->process_cgi)
    392 		return 0;
    393 
    394 #ifndef NO_USER_SUPPORT
    395 	if (request->hr_user && !httpd->enable_cgi_users)
    396 		return 0;
    397 #endif /* !NO_USER_SUPPORT */
    398 
    399 	if (request->hr_oldfile && strcmp(request->hr_oldfile, "/") != 0)
    400 		uri = request->hr_oldfile;
    401 	else
    402 		uri = request->hr_file;
    403 
    404 	if (uri[0] == '/')
    405 		file = bozostrdup(httpd, request, uri);
    406 	else
    407 		bozoasprintf(httpd, &file, "/%s", uri);
    408 
    409 	if (request->hr_query && strlen(request->hr_query))
    410 		query = bozostrdup(httpd, request, request->hr_query);
    411 	else
    412 		query = NULL;
    413 
    414 	bozoasprintf(httpd, &url, "%s%s%s",
    415 		     file,
    416 		     query ? "?" : "",
    417 		     query ? query : "");
    418 	debug((httpd, DEBUG_NORMAL, "bozo_process_cgi: url `%s'", url));
    419 
    420 	path = NULL;
    421 	envp = NULL;
    422 	cgihandler = NULL;
    423 	command = NULL;
    424 	info = NULL;
    425 
    426 	len = strlen(url);
    427 
    428 	if (bozo_auth_check(request, url + 1))
    429 		goto out;
    430 
    431 	if (!httpd->cgibin ||
    432 	    strncmp(url + 1, CGIBIN_PREFIX, CGIBIN_PREFIX_LEN) != 0) {
    433 		cgihandler = content_cgihandler(httpd, request, file + 1);
    434 		if (cgihandler == NULL) {
    435 			debug((httpd, DEBUG_FAT,
    436 				"bozo_process_cgi: no handler, returning"));
    437 			goto out;
    438 		}
    439 		if (len == 0 || file[len - 1] == '/')
    440 			append_index_html(httpd, &file);
    441 		debug((httpd, DEBUG_NORMAL, "bozo_process_cgi: cgihandler `%s'",
    442 		    cgihandler));
    443 	} else if (len - 1 == CGIBIN_PREFIX_LEN)	/* url is "/cgi-bin/" */
    444 		append_index_html(httpd, &file);
    445 
    446 	/* RFC3875  sect. 4.4. - search-string support */
    447 	if (query != NULL) {
    448 		search_string_argv = parse_search_string(request, query,
    449 		    &search_string_argc);
    450 	}
    451 
    452 	debug((httpd, DEBUG_NORMAL, "parse_search_string args no: %zu",
    453 	    search_string_argc));
    454 	for (i = 0; i < search_string_argc; i++) {
    455 		debug((httpd, DEBUG_FAT,
    456 		    "search_string[%zu]: `%s'", i, search_string_argv[i]));
    457 	}
    458 
    459 	argv = bozomalloc(httpd, sizeof(*argv) * (3 + search_string_argc));
    460 
    461 	ix = 0;
    462 	if (cgihandler) {
    463 		command = file + 1;
    464 		path = bozostrdup(httpd, request, cgihandler);
    465 	} else {
    466 		command = file + CGIBIN_PREFIX_LEN + 1;
    467 		if ((s = strchr(command, '/')) != NULL) {
    468 			info = bozostrdup(httpd, request, s);
    469 			*s = '\0';
    470 		}
    471 		path = bozomalloc(httpd,
    472 				strlen(httpd->cgibin) + 1 + strlen(command) + 1);
    473 		strcpy(path, httpd->cgibin);
    474 		strcat(path, "/");
    475 		strcat(path, command);
    476 	}
    477 
    478 	argv[ix++] = path;
    479 
    480 	/* copy search-string args */
    481 	for (i = 0; i < search_string_argc; i++)
    482 		argv[ix++] = search_string_argv[i];
    483 
    484 	argv[ix++] = NULL;
    485 	nph = strncmp(command, "nph-", 4) == 0;
    486 
    487 	type = request->hr_content_type;
    488 	clen = request->hr_content_length;
    489 
    490 	envpsize = 13 + request->hr_nheaders +
    491 	    (info && *info ? 1 : 0) +
    492 	    (query && *query ? 1 : 0) +
    493 	    (type && *type ? 1 : 0) +
    494 	    (clen && *clen ? 1 : 0) +
    495 	    (request->hr_remotehost && *request->hr_remotehost ? 1 : 0) +
    496 	    (request->hr_remoteaddr && *request->hr_remoteaddr ? 1 : 0) +
    497 	    bozo_auth_cgi_count(request) +
    498 	    (request->hr_serverport && *request->hr_serverport ? 1 : 0);
    499 
    500 	debug((httpd, DEBUG_FAT,
    501 		"bozo_process_cgi: path `%s', cmd `%s', info `%s', "
    502 		"query `%s', nph `%d', envpsize `%d'",
    503 		path, command, strornull(info),
    504 		strornull(query), nph, envpsize));
    505 
    506 	envp = bozomalloc(httpd, sizeof(*envp) * envpsize);
    507 	for (ix = 0; ix < envpsize; ix++)
    508 		envp[ix] = NULL;
    509 	curenvp = envp;
    510 
    511 	SIMPLEQ_FOREACH(headp, &request->hr_headers, h_next) {
    512 		const char *s2;
    513 		env = bozomalloc(httpd, 6 + strlen(headp->h_header) + 1 +
    514 		    strlen(headp->h_value));
    515 
    516 		t = env;
    517 		strcpy(t, "HTTP_");
    518 		t += strlen(t);
    519 		for (s2 = headp->h_header; *s2; t++, s2++)
    520 			if (islower((u_int)*s2))
    521 				*t = toupper((u_int)*s2);
    522 			else if (*s2 == '-')
    523 				*t = '_';
    524 			else
    525 				*t = *s2;
    526 		*t = '\0';
    527 		debug((httpd, DEBUG_OBESE, "setting header %s as %s = %s",
    528 		    headp->h_header, env, headp->h_value));
    529 		bozo_setenv(httpd, env, headp->h_value, curenvp++);
    530 		free(env);
    531 	}
    532 
    533 #ifndef _PATH_DEFPATH
    534 #define _PATH_DEFPATH "/usr/bin:/bin"
    535 #endif
    536 
    537 	bozo_setenv(httpd, "PATH", _PATH_DEFPATH, curenvp++);
    538 	bozo_setenv(httpd, "IFS", " \t\n", curenvp++);
    539 	bozo_setenv(httpd, "SERVER_NAME", BOZOHOST(httpd,request), curenvp++);
    540 	bozo_setenv(httpd, "GATEWAY_INTERFACE", "CGI/1.1", curenvp++);
    541 	bozo_setenv(httpd, "SERVER_PROTOCOL", request->hr_proto, curenvp++);
    542 	bozo_setenv(httpd, "REQUEST_METHOD", request->hr_methodstr, curenvp++);
    543 	bozo_setenv(httpd, "SCRIPT_NAME", file, curenvp++);
    544 	bozo_setenv(httpd, "SCRIPT_FILENAME", file + 1, curenvp++);
    545 	bozo_setenv(httpd, "SERVER_SOFTWARE", httpd->server_software,
    546 			curenvp++);
    547 	bozo_setenv(httpd, "REQUEST_URI", uri, curenvp++);
    548 	bozo_setenv(httpd, "DATE_GMT", bozo_http_date(date, sizeof(date)),
    549 			curenvp++);
    550 	/* RFC3875 section 4.1.7 says that QUERY_STRING MUST be defined. */
    551 	if (query && *query)
    552 		bozo_setenv(httpd, "QUERY_STRING", query, curenvp++);
    553 	else
    554 		bozo_setenv(httpd, "QUERY_STRING", "", curenvp++);
    555 	if (info && *info)
    556 		bozo_setenv(httpd, "PATH_INFO", info, curenvp++);
    557 	if (type && *type)
    558 		bozo_setenv(httpd, "CONTENT_TYPE", type, curenvp++);
    559 	if (clen && *clen)
    560 		bozo_setenv(httpd, "CONTENT_LENGTH", clen, curenvp++);
    561 	if (request->hr_serverport && *request->hr_serverport)
    562 		bozo_setenv(httpd, "SERVER_PORT", request->hr_serverport,
    563 				curenvp++);
    564 	if (request->hr_remotehost && *request->hr_remotehost)
    565 		bozo_setenv(httpd, "REMOTE_HOST", request->hr_remotehost,
    566 				curenvp++);
    567 	if (request->hr_remoteaddr && *request->hr_remoteaddr)
    568 		bozo_setenv(httpd, "REMOTE_ADDR", request->hr_remoteaddr,
    569 				curenvp++);
    570 	/*
    571 	 * Apache does this when invoking content handlers, and PHP
    572 	 * 5.3 requires it as a "security" measure.
    573 	 */
    574 	if (cgihandler)
    575 		bozo_setenv(httpd, "REDIRECT_STATUS", "200", curenvp++);
    576 	bozo_auth_cgi_setenv(request, &curenvp);
    577 
    578 	debug((httpd, DEBUG_FAT, "bozo_process_cgi: going exec %s with args:",
    579 	    path));
    580 
    581 	for (i = 0; argv[i] != NULL; i++) {
    582 		debug((httpd, DEBUG_FAT, "bozo_process_cgi: argv[%zu] = `%s'",
    583 		    i, argv[i]));
    584 	}
    585 
    586 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sv) == -1)
    587 		bozoerr(httpd, 1, "child socketpair failed: %s",
    588 				strerror(errno));
    589 
    590 	/*
    591 	 * We create 2 procs: one to become the CGI, one read from
    592 	 * the CGI and output to the network, and this parent will
    593 	 * continue reading from the network and writing to the
    594 	 * CGI procsss.
    595 	 */
    596 	switch (fork()) {
    597 	case -1: /* eep, failure */
    598 		bozoerr(httpd, 1, "child fork failed: %s", strerror(errno));
    599 		/*NOTREACHED*/
    600 	case 0:
    601 		close(sv[0]);
    602 		dup2(sv[1], STDIN_FILENO);
    603 		dup2(sv[1], STDOUT_FILENO);
    604 		close(2);
    605 		close(sv[1]);
    606 		closelog();
    607 		bozo_daemon_closefds(httpd);
    608 
    609 		if (-1 == execve(path, argv, envp))
    610 			bozoerr(httpd, 1, "child exec failed: %s: %s",
    611 			      path, strerror(errno));
    612 		/* NOT REACHED */
    613 		bozoerr(httpd, 1, "child execve returned?!");
    614 	}
    615 
    616 	free(query);
    617 	free(file);
    618 	free(url);
    619 	for (i = 0; i < search_string_argc; i++)
    620 		free(search_string_argv[i]);
    621 	free(search_string_argv);
    622 
    623 	close(sv[1]);
    624 
    625 	/* parent: read from stdin (bozo_read()) write to sv[0] */
    626 	/* child: read from sv[0] (bozo_write()) write to stdout */
    627 	pid = fork();
    628 	if (pid == -1)
    629 		bozoerr(httpd, 1, "io child fork failed: %s", strerror(errno));
    630 	else if (pid == 0) {
    631 		/* child reader/writer */
    632 		close(STDIN_FILENO);
    633 		finish_cgi_output(httpd, request, sv[0], nph);
    634 		/* if we're done output, our parent is useless... */
    635 		kill(getppid(), SIGKILL);
    636 		debug((httpd, DEBUG_FAT, "done processing cgi output"));
    637 		_exit(0);
    638 	}
    639 	close(STDOUT_FILENO);
    640 
    641 	/* XXX we should have some goo that times us out
    642 	 */
    643 	while ((rbytes = bozo_read(httpd, STDIN_FILENO, buf, sizeof buf)) > 0) {
    644 		ssize_t wbytes;
    645 		char *bp = buf;
    646 
    647 		while (rbytes) {
    648 			wbytes = write(sv[0], buf, (size_t)rbytes);
    649 			if (wbytes > 0) {
    650 				rbytes -= wbytes;
    651 				bp += wbytes;
    652 			} else
    653 				bozoerr(httpd, 1, "write failed: %s",
    654 					strerror(errno));
    655 		}
    656 	}
    657 	debug((httpd, DEBUG_FAT, "done processing cgi input"));
    658 	exit(0);
    659 
    660  out:
    661 
    662 	for (i = 0; i < search_string_argc; i++)
    663 		free(search_string_argv[i]);
    664 	free(search_string_argv);
    665 	free(query);
    666 	free(file);
    667 	free(url);
    668 	return 0;
    669 }
    670 
    671 #ifndef NO_DYNAMIC_CONTENT
    672 /* cgi maps are simple ".postfix /path/to/prog" */
    673 void
    674 bozo_add_content_map_cgi(bozohttpd_t *httpd, const char *arg,
    675                          const char *cgihandler)
    676 {
    677 	bozo_content_map_t *map;
    678 
    679 	debug((httpd, DEBUG_NORMAL, "bozo_add_content_map_cgi: name %s cgi %s",
    680 		arg, cgihandler));
    681 
    682 	httpd->process_cgi = 1;
    683 
    684 	map = bozo_get_content_map(httpd, arg);
    685 	map->name = arg;
    686 	map->type = map->encoding = map->encoding11 = NULL;
    687 	map->cgihandler = cgihandler;
    688 }
    689 #endif /* NO_DYNAMIC_CONTENT */
    690 
    691 #endif /* NO_CGIBIN_SUPPORT */
    692