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