cgi-bozo.c revision 1.25 1 1.25 shm /* $NetBSD: cgi-bozo.c,v 1.25 2014/06/24 07:23:59 shm Exp $ */
2 1.4 tls
3 1.20 mrg /* $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.24 mrg * 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.13 mrg #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.15 mrg #ifndef USE_ARG
57 1.15 mrg #define USE_ARG(x) /*LINTED*/(void)&(x)
58 1.15 mrg #endif
59 1.15 mrg
60 1.15 mrg /*
61 1.15 mrg * given the file name, return a CGI interpreter
62 1.15 mrg */
63 1.15 mrg static const char *
64 1.15 mrg content_cgihandler(bozohttpd_t *httpd, bozo_httpreq_t *request,
65 1.15 mrg const char *file)
66 1.15 mrg {
67 1.15 mrg bozo_content_map_t *map;
68 1.15 mrg
69 1.15 mrg USE_ARG(request);
70 1.15 mrg debug((httpd, DEBUG_FAT, "content_cgihandler: trying file %s", file));
71 1.15 mrg map = bozo_match_content_map(httpd, file, 0);
72 1.15 mrg if (map)
73 1.15 mrg return map->cgihandler;
74 1.15 mrg return NULL;
75 1.15 mrg }
76 1.15 mrg
77 1.15 mrg static int
78 1.15 mrg parse_header(bozohttpd_t *httpd, const char *str, ssize_t len, char **hdr_str,
79 1.15 mrg char **hdr_val)
80 1.15 mrg {
81 1.15 mrg char *name, *value;
82 1.15 mrg
83 1.15 mrg /* if the string passed is zero-length bail out */
84 1.15 mrg if (*str == '\0')
85 1.15 mrg return -1;
86 1.15 mrg
87 1.15 mrg value = bozostrdup(httpd, str);
88 1.15 mrg
89 1.15 mrg /* locate the ':' separator in the header/value */
90 1.15 mrg name = bozostrnsep(&value, ":", &len);
91 1.15 mrg
92 1.15 mrg if (NULL == name || -1 == len) {
93 1.15 mrg free(name);
94 1.15 mrg return -1;
95 1.15 mrg }
96 1.1 tls
97 1.15 mrg /* skip leading space/tab */
98 1.15 mrg while (*value == ' ' || *value == '\t')
99 1.15 mrg len--, value++;
100 1.15 mrg
101 1.15 mrg *hdr_str = name;
102 1.15 mrg *hdr_val = value;
103 1.15 mrg
104 1.15 mrg return 0;
105 1.15 mrg }
106 1.15 mrg
107 1.15 mrg /*
108 1.15 mrg * handle parsing a CGI header output, transposing a Status: header
109 1.15 mrg * into the HTTP reply (ie, instead of "200 OK").
110 1.15 mrg */
111 1.15 mrg static void
112 1.15 mrg finish_cgi_output(bozohttpd_t *httpd, bozo_httpreq_t *request, int in, int nph)
113 1.15 mrg {
114 1.15 mrg char buf[BOZO_WRSZ];
115 1.15 mrg char *str;
116 1.15 mrg ssize_t len;
117 1.15 mrg ssize_t rbytes;
118 1.15 mrg SIMPLEQ_HEAD(, bozoheaders) headers;
119 1.15 mrg bozoheaders_t *hdr, *nhdr;
120 1.15 mrg int write_header, nheaders = 0;
121 1.15 mrg
122 1.15 mrg /* much of this code is like bozo_read_request()'s header loop. */
123 1.15 mrg SIMPLEQ_INIT(&headers);
124 1.15 mrg write_header = nph == 0;
125 1.15 mrg /* was read(2) here - XXX - agc */
126 1.15 mrg while (nph == 0 &&
127 1.15 mrg (str = bozodgetln(httpd, in, &len, bozo_read)) != NULL) {
128 1.15 mrg char *hdr_name, *hdr_value;
129 1.15 mrg
130 1.15 mrg if (parse_header(httpd, str, len, &hdr_name, &hdr_value))
131 1.15 mrg break;
132 1.15 mrg
133 1.15 mrg /*
134 1.15 mrg * The CGI 1.{1,2} spec both say that if the cgi program
135 1.15 mrg * returns a `Status:' header field then the server MUST
136 1.15 mrg * return it in the response. If the cgi program does
137 1.15 mrg * not return any `Status:' header then the server should
138 1.15 mrg * respond with 200 OK.
139 1.15 mrg * XXX The CGI 1.1 and 1.2 specification differ slightly on
140 1.15 mrg * this in that v1.2 says that the script MUST NOT return a
141 1.15 mrg * `Status:' header if it is returning a `Location:' header.
142 1.15 mrg * For compatibility we are going with the CGI 1.1 behavior.
143 1.15 mrg */
144 1.15 mrg if (strcasecmp(hdr_name, "status") == 0) {
145 1.15 mrg debug((httpd, DEBUG_OBESE,
146 1.15 mrg "bozo_process_cgi: writing HTTP header "
147 1.15 mrg "from status %s ..", hdr_value));
148 1.15 mrg bozo_printf(httpd, "%s %s\r\n", request->hr_proto,
149 1.15 mrg hdr_value);
150 1.15 mrg bozo_flush(httpd, stdout);
151 1.15 mrg write_header = 0;
152 1.15 mrg free(hdr_name);
153 1.15 mrg break;
154 1.15 mrg }
155 1.15 mrg
156 1.15 mrg hdr = bozomalloc(httpd, sizeof *hdr);
157 1.15 mrg hdr->h_header = hdr_name;
158 1.15 mrg hdr->h_value = hdr_value;
159 1.15 mrg SIMPLEQ_INSERT_TAIL(&headers, hdr, h_next);
160 1.15 mrg nheaders++;
161 1.15 mrg }
162 1.15 mrg
163 1.15 mrg if (write_header) {
164 1.15 mrg debug((httpd, DEBUG_OBESE,
165 1.15 mrg "bozo_process_cgi: writing HTTP header .."));
166 1.15 mrg bozo_printf(httpd,
167 1.15 mrg "%s 200 OK\r\n", request->hr_proto);
168 1.15 mrg bozo_flush(httpd, stdout);
169 1.15 mrg }
170 1.15 mrg
171 1.15 mrg if (nheaders) {
172 1.15 mrg debug((httpd, DEBUG_OBESE,
173 1.15 mrg "bozo_process_cgi: writing delayed HTTP headers .."));
174 1.15 mrg SIMPLEQ_FOREACH_SAFE(hdr, &headers, h_next, nhdr) {
175 1.15 mrg bozo_printf(httpd, "%s: %s\r\n", hdr->h_header,
176 1.15 mrg hdr->h_value);
177 1.15 mrg free(hdr->h_header);
178 1.15 mrg free(hdr);
179 1.15 mrg }
180 1.15 mrg bozo_printf(httpd, "\r\n");
181 1.15 mrg bozo_flush(httpd, stdout);
182 1.15 mrg }
183 1.15 mrg
184 1.15 mrg /* XXX we should have some goo that times us out
185 1.15 mrg */
186 1.15 mrg while ((rbytes = read(in, buf, sizeof buf)) > 0) {
187 1.15 mrg ssize_t wbytes;
188 1.15 mrg char *bp = buf;
189 1.15 mrg
190 1.15 mrg while (rbytes) {
191 1.15 mrg wbytes = bozo_write(httpd, STDOUT_FILENO, buf,
192 1.15 mrg (size_t)rbytes);
193 1.15 mrg if (wbytes > 0) {
194 1.15 mrg rbytes -= wbytes;
195 1.15 mrg bp += wbytes;
196 1.15 mrg } else
197 1.15 mrg bozo_err(httpd, 1,
198 1.15 mrg "cgi output write failed: %s",
199 1.15 mrg strerror(errno));
200 1.15 mrg }
201 1.15 mrg }
202 1.15 mrg }
203 1.15 mrg
204 1.15 mrg static void
205 1.15 mrg append_index_html(bozohttpd_t *httpd, char **url)
206 1.15 mrg {
207 1.15 mrg *url = bozorealloc(httpd, *url,
208 1.15 mrg strlen(*url) + strlen(httpd->index_html) + 1);
209 1.15 mrg strcat(*url, httpd->index_html);
210 1.15 mrg debug((httpd, DEBUG_NORMAL,
211 1.15 mrg "append_index_html: url adjusted to `%s'", *url));
212 1.15 mrg }
213 1.1 tls
214 1.1 tls void
215 1.15 mrg bozo_cgi_setbin(bozohttpd_t *httpd, const char *path)
216 1.1 tls {
217 1.15 mrg httpd->cgibin = strdup(path);
218 1.15 mrg debug((httpd, DEBUG_OBESE, "cgibin (cgi-bin directory) is %s",
219 1.15 mrg 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.15 mrg bozo_setenv(bozohttpd_t *httpd, const char *env, const char *val,
225 1.15 mrg char **envp)
226 1.1 tls {
227 1.15 mrg 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.15 mrg 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.12 mrg * process the cgi, otherwise just return. Returns 0 if it did not handle
240 1.12 mrg * the request.
241 1.1 tls */
242 1.12 mrg int
243 1.15 mrg bozo_process_cgi(bozo_httpreq_t *request)
244 1.1 tls {
245 1.15 mrg bozohttpd_t *httpd = request->hr_httpd;
246 1.15 mrg char buf[BOZO_WRSZ];
247 1.15 mrg char date[40];
248 1.15 mrg bozoheaders_t *headp;
249 1.1 tls const char *type, *clen, *info, *cgihandler;
250 1.9 tls char *query, *s, *t, *path, *env, *command, *file, *url;
251 1.1 tls char **envp, **curenvp, *argv[4];
252 1.17 mrg 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.15 mrg if (!httpd->cgibin && !httpd->process_cgi)
260 1.12 mrg return 0;
261 1.1 tls
262 1.25 shm if (request->hr_oldfile && strcmp(request->hr_oldfile, "/") != 0)
263 1.25 shm uri = request->hr_oldfile;
264 1.25 shm else
265 1.25 shm uri = request->hr_file;
266 1.25 shm
267 1.17 mrg if (uri[0] == '/')
268 1.17 mrg file = bozostrdup(httpd, uri);
269 1.17 mrg else
270 1.17 mrg asprintf(&file, "/%s", uri);
271 1.12 mrg if (file == NULL)
272 1.12 mrg return 0;
273 1.17 mrg
274 1.12 mrg if (request->hr_query && strlen(request->hr_query))
275 1.15 mrg query = bozostrdup(httpd, request->hr_query);
276 1.12 mrg else
277 1.12 mrg query = NULL;
278 1.12 mrg
279 1.12 mrg asprintf(&url, "%s%s%s", file, query ? "?" : "", query ? query : "");
280 1.12 mrg if (url == NULL)
281 1.12 mrg goto out;
282 1.15 mrg 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.13 mrg
290 1.1 tls len = strlen(url);
291 1.1 tls
292 1.16 mrg if (bozo_auth_check(request, url + 1))
293 1.12 mrg goto out;
294 1.12 mrg
295 1.15 mrg if (!httpd->cgibin ||
296 1.15 mrg strncmp(url + 1, CGIBIN_PREFIX, CGIBIN_PREFIX_LEN) != 0) {
297 1.15 mrg cgihandler = content_cgihandler(httpd, request, file + 1);
298 1.1 tls if (cgihandler == NULL) {
299 1.15 mrg debug((httpd, DEBUG_FAT,
300 1.15 mrg "bozo_process_cgi: no handler, returning"));
301 1.12 mrg goto out;
302 1.1 tls }
303 1.9 tls if (len == 0 || file[len - 1] == '/')
304 1.15 mrg append_index_html(httpd, &file);
305 1.15 mrg debug((httpd, DEBUG_NORMAL, "bozo_process_cgi: cgihandler `%s'",
306 1.1 tls cgihandler));
307 1.8 mrg } else if (len - 1 == CGIBIN_PREFIX_LEN) /* url is "/cgi-bin/" */
308 1.15 mrg append_index_html(httpd, &file);
309 1.12 mrg
310 1.1 tls ix = 0;
311 1.1 tls if (cgihandler) {
312 1.9 tls command = file + 1;
313 1.15 mrg 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.9 tls command = file + CGIBIN_PREFIX_LEN + 1;
318 1.1 tls if ((s = strchr(command, '/')) != NULL) {
319 1.15 mrg info = bozostrdup(httpd, s);
320 1.1 tls *s = '\0';
321 1.1 tls }
322 1.15 mrg path = bozomalloc(httpd,
323 1.15 mrg strlen(httpd->cgibin) + 1 + strlen(command) + 1);
324 1.15 mrg 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.15 mrg bozo_auth_cgi_count(request) +
346 1.1 tls (request->hr_serverport && *request->hr_serverport ? 1 : 0);
347 1.1 tls
348 1.15 mrg debug((httpd, DEBUG_FAT,
349 1.15 mrg "bozo_process_cgi: path `%s', cmd `%s', info `%s', "
350 1.15 mrg "query `%s', nph `%d', envpsize `%d'",
351 1.15 mrg path, command, strornull(info),
352 1.15 mrg strornull(query), nph, envpsize));
353 1.14 mrg
354 1.15 mrg 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.15 mrg 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.15 mrg debug((httpd, DEBUG_OBESE, "setting header %s as %s = %s",
376 1.1 tls headp->h_header, env, headp->h_value));
377 1.15 mrg bozo_setenv(httpd, env, headp->h_value, curenvp++);
378 1.1 tls free(env);
379 1.1 tls }
380 1.14 mrg
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.15 mrg bozo_setenv(httpd, "PATH", _PATH_DEFPATH, curenvp++);
386 1.15 mrg bozo_setenv(httpd, "IFS", " \t\n", curenvp++);
387 1.21 martin bozo_setenv(httpd, "SERVER_NAME", BOZOHOST(httpd,request), curenvp++);
388 1.15 mrg bozo_setenv(httpd, "GATEWAY_INTERFACE", "CGI/1.1", curenvp++);
389 1.15 mrg bozo_setenv(httpd, "SERVER_PROTOCOL", request->hr_proto, curenvp++);
390 1.15 mrg bozo_setenv(httpd, "REQUEST_METHOD", request->hr_methodstr, curenvp++);
391 1.15 mrg bozo_setenv(httpd, "SCRIPT_NAME", file, curenvp++);
392 1.15 mrg bozo_setenv(httpd, "SCRIPT_FILENAME", file + 1, curenvp++);
393 1.15 mrg bozo_setenv(httpd, "SERVER_SOFTWARE", httpd->server_software,
394 1.15 mrg curenvp++);
395 1.17 mrg bozo_setenv(httpd, "REQUEST_URI", uri, curenvp++);
396 1.15 mrg bozo_setenv(httpd, "DATE_GMT", bozo_http_date(date, sizeof(date)),
397 1.15 mrg curenvp++);
398 1.1 tls if (query && *query)
399 1.15 mrg bozo_setenv(httpd, "QUERY_STRING", query, curenvp++);
400 1.1 tls if (info && *info)
401 1.15 mrg bozo_setenv(httpd, "PATH_INFO", info, curenvp++);
402 1.1 tls if (type && *type)
403 1.15 mrg bozo_setenv(httpd, "CONTENT_TYPE", type, curenvp++);
404 1.1 tls if (clen && *clen)
405 1.15 mrg bozo_setenv(httpd, "CONTENT_LENGTH", clen, curenvp++);
406 1.1 tls if (request->hr_serverport && *request->hr_serverport)
407 1.15 mrg bozo_setenv(httpd, "SERVER_PORT", request->hr_serverport,
408 1.15 mrg curenvp++);
409 1.1 tls if (request->hr_remotehost && *request->hr_remotehost)
410 1.15 mrg bozo_setenv(httpd, "REMOTE_HOST", request->hr_remotehost,
411 1.15 mrg curenvp++);
412 1.1 tls if (request->hr_remoteaddr && *request->hr_remoteaddr)
413 1.15 mrg bozo_setenv(httpd, "REMOTE_ADDR", request->hr_remoteaddr,
414 1.15 mrg curenvp++);
415 1.19 tls /*
416 1.19 tls * XXX Apache does this when invoking content handlers, and PHP
417 1.19 tls * XXX 5.3 requires it as a "security" measure.
418 1.19 tls */
419 1.19 tls if (cgihandler)
420 1.19 tls bozo_setenv(httpd, "REDIRECT_STATUS", "200", curenvp++);
421 1.16 mrg bozo_auth_cgi_setenv(request, &curenvp);
422 1.1 tls
423 1.10 tls free(file);
424 1.10 tls free(url);
425 1.1 tls
426 1.15 mrg debug((httpd, DEBUG_FAT, "bozo_process_cgi: going exec %s, %s %s %s",
427 1.12 mrg path, argv[0], strornull(argv[1]), strornull(argv[2])));
428 1.12 mrg
429 1.15 mrg if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sv) == -1)
430 1.15 mrg bozo_err(httpd, 1, "child socketpair failed: %s",
431 1.15 mrg 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.15 mrg bozo_err(httpd, 1, "child fork failed: %s", strerror(errno));
442 1.15 mrg /*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.13 mrg close(2);
448 1.13 mrg close(sv[1]);
449 1.13 mrg closelog();
450 1.15 mrg bozo_daemon_closefds(httpd);
451 1.10 tls
452 1.1 tls if (-1 == execve(path, argv, envp))
453 1.15 mrg bozo_err(httpd, 1, "child exec failed: %s: %s",
454 1.13 mrg path, strerror(errno));
455 1.1 tls /* NOT REACHED */
456 1.15 mrg 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.15 mrg /* parent: read from stdin (bozo_read()) write to sv[0] */
462 1.15 mrg /* 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.15 mrg 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.15 mrg 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.15 mrg 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.15 mrg 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.15 mrg 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.15 mrg bozo_err(httpd, 1, "write failed: %s",
490 1.15 mrg strerror(errno));
491 1.1 tls }
492 1.1 tls }
493 1.15 mrg debug((httpd, DEBUG_FAT, "done processing cgi input"));
494 1.1 tls exit(0);
495 1.12 mrg
496 1.12 mrg out:
497 1.23 mbalmer free(query);
498 1.23 mbalmer free(file);
499 1.23 mbalmer free(url);
500 1.12 mrg return 0;
501 1.1 tls }
502 1.1 tls
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.15 mrg bozo_add_content_map_cgi(bozohttpd_t *httpd, const char *arg, const char *cgihandler)
507 1.1 tls {
508 1.15 mrg bozo_content_map_t *map;
509 1.1 tls
510 1.15 mrg debug((httpd, DEBUG_NORMAL, "bozo_add_content_map_cgi: name %s cgi %s",
511 1.15 mrg arg, cgihandler));
512 1.1 tls
513 1.15 mrg httpd->process_cgi = 1;
514 1.1 tls
515 1.15 mrg map = bozo_get_content_map(httpd, arg);
516 1.1 tls map->name = arg;
517 1.18 mrg 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