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