1 /* $NetBSD: cgi-bozo.c,v 1.53 2021/02/27 12:36:46 mrg Exp $ */
2
3 /* $eterna: cgi-bozo.c,v 1.40 2011/11/18 09:21:15 mrg Exp $ */
4
5 /*
6 * Copyright (c) 1997-2021 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 #include <assert.h>
49
50 #include <netinet/in.h>
51
52 #include "bozohttpd.h"
53
54 #define CGIBIN_PREFIX "cgi-bin/"
55 #define CGIBIN_PREFIX_LEN (sizeof(CGIBIN_PREFIX)-1)
56
57 #ifndef USE_ARG
58 #define USE_ARG(x) /*LINTED*/(void)&(x)
59 #endif
60
61 /*
62 * given the file name, return a CGI interpreter
63 */
64 static const char *
content_cgihandler(bozohttpd_t * httpd,bozo_httpreq_t * request,const char * file)65 content_cgihandler(bozohttpd_t *httpd, bozo_httpreq_t *request,
66 const char *file)
67 {
68 bozo_content_map_t *map;
69
70 USE_ARG(request);
71 debug((httpd, DEBUG_FAT, "content_cgihandler: trying file %s", file));
72 map = bozo_match_content_map(httpd, file, 0);
73 if (map)
74 return map->cgihandler;
75 return NULL;
76 }
77
78 static int
parse_header(bozo_httpreq_t * request,const char * str,ssize_t len,char ** hdr_str,char ** hdr_val)79 parse_header(bozo_httpreq_t *request, const char *str, ssize_t len,
80 char **hdr_str, char **hdr_val)
81 {
82 struct bozohttpd_t *httpd = request->hr_httpd;
83 char *name, *value;
84
85 /* if the string passed is zero-length bail out */
86 if (*str == '\0')
87 return -1;
88
89 value = bozostrdup(httpd, request, str);
90
91 /* locate the ':' separator in the header/value */
92 name = bozostrnsep(&value, ":", &len);
93
94 if (NULL == name || -1 == len) {
95 free(value);
96 return -1;
97 }
98
99 /* skip leading space/tab */
100 while (*value == ' ' || *value == '\t')
101 len--, value++;
102
103 *hdr_str = name;
104 *hdr_val = value;
105
106 return 0;
107 }
108
109 /*
110 * handle parsing a CGI header output, transposing a Status: header
111 * into the HTTP reply (ie, instead of "200 OK").
112 */
113 static void
finish_cgi_output(bozohttpd_t * httpd,bozo_httpreq_t * request,int in,int nph)114 finish_cgi_output(bozohttpd_t *httpd, bozo_httpreq_t *request, int in, int nph)
115 {
116 char buf[BOZO_WRSZ];
117 char *str;
118 ssize_t len;
119 ssize_t rbytes;
120 SIMPLEQ_HEAD(, bozoheaders) headers;
121 bozoheaders_t *hdr, *nhdr;
122 int write_header, nheaders = 0;
123
124 /* much of this code is like bozo_read_request()'s header loop. */
125 SIMPLEQ_INIT(&headers);
126 write_header = nph == 0;
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 * 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 "%s: writing HTTP header "
148 "from status %s ..", __func__, 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 "%s: writing HTTP header ..", __func__));
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 "%s: writing delayed HTTP headers ..", __func__));
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 /* CGI programs should perform their own timeouts */
186 while ((rbytes = read(in, buf, sizeof buf)) > 0) {
187 ssize_t wbytes;
188 char *bp = buf;
189
190 while (rbytes) {
191 wbytes = bozo_write(httpd, STDOUT_FILENO, buf,
192 (size_t)rbytes);
193 if (wbytes > 0) {
194 rbytes -= wbytes;
195 bp += wbytes;
196 } else
197 bozoerr(httpd, 1,
198 "cgi output write failed: %s",
199 strerror(errno));
200 }
201 }
202 }
203
204 static void
append_index_html(bozohttpd_t * httpd,char ** url)205 append_index_html(bozohttpd_t *httpd, char **url)
206 {
207 *url = bozorealloc(httpd, *url,
208 strlen(*url) + strlen(httpd->index_html) + 1);
209 strcat(*url, httpd->index_html);
210 debug((httpd, DEBUG_NORMAL,
211 "append_index_html: url adjusted to `%s'", *url));
212 }
213
214 /* This function parse search-string according to section 4.4 of RFC3875 */
215 static char **
parse_search_string(bozo_httpreq_t * request,const char * query,size_t * args_len)216 parse_search_string(bozo_httpreq_t *request, const char *query, size_t *args_len)
217 {
218 struct bozohttpd_t *httpd = request->hr_httpd;
219 size_t i;
220 char *s, *str, **args;
221
222 *args_len = 0;
223
224 /* URI MUST not contain any unencoded '=' - RFC3875, section 4.4 */
225 if (strchr(query, '='))
226 return NULL;
227
228 str = bozostrdup(httpd, request, query);
229
230 /*
231 * there's no more arguments than '+' chars in the query string as it's
232 * the separator
233 */
234 *args_len = 1;
235 /* count '+' in str */
236 for (s = str; (s = strchr(s, '+')) != NULL; (*args_len)++)
237 s++;
238
239 args = bozomalloc(httpd, sizeof(*args) * (*args_len + 1));
240
241 args[0] = str;
242 args[*args_len] = NULL;
243 for (s = str, i = 1; (s = strchr(s, '+')) != NULL; i++) {
244 *s = '\0';
245 s++;
246 args[i] = s;
247 }
248
249 /*
250 * check if search-strings are valid:
251 *
252 * RFC3875, section 4.4:
253 *
254 * search-string = search-word *( "+" search-word )
255 * search-word = 1*schar
256 * schar = unreserved | escaped | xreserved
257 * xreserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "," |
258 * "$"
259 *
260 * section 2.3:
261 *
262 * hex = digit | "A" | "B" | "C" | "D" | "E" | "F" | "a" |
263 * "b" | "c" | "d" | "e" | "f"
264 * escaped = "%" hex hex
265 * unreserved = alpha | digit | mark
266 * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
267 *
268 * section 2.2:
269 *
270 * alpha = lowalpha | hialpha
271 * lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" |
272 * "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" |
273 * "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" |
274 * "y" | "z"
275 * hialpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" |
276 * "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" |
277 * "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" |
278 * "Y" | "Z"
279 * digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
280 * "8" | "9"
281 */
282 #define UNRESERVED_CHAR "-_.!~*'()"
283 #define XRESERVED_CHAR ";/?:@&=,$"
284
285 for (i = 0; i < *args_len; i++) {
286 s = args[i];
287 /* search-word MUST have at least one schar */
288 if (*s == '\0')
289 goto parse_err;
290 while (*s) {
291 /* check if it's unreserved */
292 if (isalpha((int)*s) || isdigit((int)*s) ||
293 strchr(UNRESERVED_CHAR, *s)) {
294 s++;
295 continue;
296 }
297
298 /* check if it's escaped */
299 if (*s == '%') {
300 if (s[1] == '\0' || s[2] == '\0')
301 goto parse_err;
302 if (!isxdigit((int)s[1]) ||
303 !isxdigit((int)s[2]))
304 goto parse_err;
305 s += 3;
306 continue;
307 }
308
309 /* check if it's xreserved */
310
311 if (strchr(XRESERVED_CHAR, *s)) {
312 s++;
313 continue;
314 }
315
316 goto parse_err;
317 }
318 }
319
320 /* decode percent encoding */
321 for (i = 0; i < *args_len; i++) {
322 if (bozo_decode_url_percent(request, args[i]))
323 goto parse_err;
324 }
325
326 /* allocate each arg separately */
327 for (i = 0; i < *args_len; i++)
328 args[i] = bozostrdup(httpd, request, args[i]);
329 free(str);
330
331 return args;
332
333 parse_err:
334
335 free(str);
336 free(args);
337 *args_len = 0;
338
339 return NULL;
340
341 }
342
343 void
bozo_cgi_setbin(bozohttpd_t * httpd,const char * path)344 bozo_cgi_setbin(bozohttpd_t *httpd, const char *path)
345 {
346 httpd->cgibin = bozostrdup(httpd, NULL, path);
347 debug((httpd, DEBUG_OBESE, "cgibin (cgi-bin directory) is %s",
348 httpd->cgibin));
349 }
350
351 /* help build up the environ pointer */
352 void
bozo_setenv(bozohttpd_t * httpd,const char * env,const char * val,char ** envp)353 bozo_setenv(bozohttpd_t *httpd, const char *env, const char *val,
354 char **envp)
355 {
356 char *s1 = bozomalloc(httpd, strlen(env) + strlen(val) + 2);
357
358 strcpy(s1, env);
359 strcat(s1, "=");
360 strcat(s1, val);
361 debug((httpd, DEBUG_OBESE, "bozo_setenv: %s", s1));
362 *envp = s1;
363 }
364
365 /*
366 * Checks if the request has asked for a cgi-bin. Should only be called if
367 * cgibin is set. If it starts CGIBIN_PREFIX or has a ncontent handler,
368 * process the cgi, otherwise just return. Returns 0 if it did not handle
369 * the request.
370 */
371 int
bozo_process_cgi(bozo_httpreq_t * request)372 bozo_process_cgi(bozo_httpreq_t *request)
373 {
374 bozohttpd_t *httpd = request->hr_httpd;
375 char buf[BOZO_WRSZ];
376 char date[40];
377 bozoheaders_t *headp;
378 const char *type, *clen, *info, *cgihandler;
379 char *query, *s, *t, *path, *env, *command, *file, *url;
380 char **envp, **curenvp, **argv, **search_string_argv = NULL;
381 char **lastenvp;
382 char *uri;
383 size_t i, len, search_string_argc = 0;
384 ssize_t rbytes;
385 pid_t pid;
386 int envpsize, ix, nph;
387 int sv[2];
388
389 if (!httpd->cgibin && !httpd->process_cgi)
390 return 0;
391
392 #ifndef NO_USER_SUPPORT
393 if (request->hr_user && !httpd->enable_cgi_users)
394 return 0;
395 #endif /* !NO_USER_SUPPORT */
396
397 if (request->hr_oldfile && strcmp(request->hr_oldfile, "/") != 0)
398 uri = request->hr_oldfile;
399 else
400 uri = request->hr_file;
401
402 if (uri[0] == '/')
403 file = bozostrdup(httpd, request, uri);
404 else
405 bozoasprintf(httpd, &file, "/%s", uri);
406
407 if (request->hr_query && strlen(request->hr_query))
408 query = bozostrdup(httpd, request, request->hr_query);
409 else
410 query = NULL;
411
412 bozoasprintf(httpd, &url, "%s%s%s",
413 file,
414 query ? "?" : "",
415 query ? query : "");
416 debug((httpd, DEBUG_NORMAL, "%s: url `%s'", __func__, url));
417
418 path = NULL;
419 envp = NULL;
420 cgihandler = NULL;
421 command = NULL;
422 info = NULL;
423
424 len = strlen(url);
425
426 if (bozo_auth_check(request, url + 1))
427 goto out;
428
429 if (!httpd->cgibin ||
430 strncmp(url + 1, CGIBIN_PREFIX, CGIBIN_PREFIX_LEN) != 0) {
431 cgihandler = content_cgihandler(httpd, request, file + 1);
432 if (cgihandler == NULL) {
433 debug((httpd, DEBUG_FAT,
434 "%s: no handler, returning", __func__));
435 goto out;
436 }
437 if (len == 0 || file[len - 1] == '/')
438 append_index_html(httpd, &file);
439 debug((httpd, DEBUG_NORMAL, "%s: cgihandler `%s'",
440 __func__, cgihandler));
441 } else if (len - 1 == CGIBIN_PREFIX_LEN) /* url is "/cgi-bin/" */
442 append_index_html(httpd, &file);
443
444 /* RFC3875 sect. 4.4. - search-string support */
445 if (query != NULL) {
446 search_string_argv = parse_search_string(request, query,
447 &search_string_argc);
448 }
449
450 debug((httpd, DEBUG_NORMAL, "parse_search_string args no: %zu",
451 search_string_argc));
452 for (i = 0; i < search_string_argc; i++) {
453 debug((httpd, DEBUG_FAT,
454 "search_string[%zu]: `%s'", i, search_string_argv[i]));
455 }
456
457 argv = bozomalloc(httpd, sizeof(*argv) * (3 + search_string_argc));
458
459 ix = 0;
460 if (cgihandler) {
461 command = file + 1;
462 path = bozostrdup(httpd, request, cgihandler);
463 } else {
464 command = file + CGIBIN_PREFIX_LEN + 1;
465 if ((s = strchr(command, '/')) != NULL) {
466 info = bozostrdup(httpd, request, s);
467 *s = '\0';
468 }
469 path = bozomalloc(httpd,
470 strlen(httpd->cgibin) + 1 + strlen(command) + 1);
471 strcpy(path, httpd->cgibin);
472 strcat(path, "/");
473 strcat(path, command);
474 }
475
476 argv[ix++] = path;
477
478 /* copy search-string args */
479 for (i = 0; i < search_string_argc; i++)
480 argv[ix++] = search_string_argv[i];
481
482 argv[ix++] = NULL;
483 nph = strncmp(command, "nph-", 4) == 0;
484
485 type = request->hr_content_type;
486 clen = request->hr_content_length;
487
488 envpsize = 13 + request->hr_nheaders +
489 (info && *info ? 1 : 0) +
490 (query && *query ? 1 : 0) +
491 (type && *type ? 1 : 0) +
492 (clen && *clen ? 1 : 0) +
493 (request->hr_remotehost && *request->hr_remotehost ? 1 : 0) +
494 (request->hr_remoteaddr && *request->hr_remoteaddr ? 1 : 0) +
495 (cgihandler ? 1 : 0) +
496 bozo_auth_cgi_count(request) +
497 (request->hr_serverport && *request->hr_serverport ? 1 : 0);
498
499 debug((httpd, DEBUG_FAT,
500 "%s: path `%s', cmd `%s', info `%s', "
501 "query `%s', nph `%d', envpsize `%d'", __func__,
502 path, command, strornull(info),
503 strornull(query), nph, envpsize));
504
505 envp = bozomalloc(httpd, sizeof(*envp) * envpsize);
506 for (ix = 0; ix < envpsize; ix++)
507 envp[ix] = NULL;
508 curenvp = envp;
509 lastenvp = envp + envpsize;
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((unsigned)*s2))
521 *t = toupper((unsigned)*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, "%s: going exec %s with args:", __func__,
579 path));
580
581 for (i = 0; argv[i] != NULL; i++) {
582 debug((httpd, DEBUG_FAT, "%s: argv[%zu] = `%s'", __func__,
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 *curenvp = 0;
591 assert(lastenvp > curenvp);
592
593 /*
594 * We create 2 procs: one to become the CGI, one read from
595 * the CGI and output to the network, and this parent will
596 * continue reading from the network and writing to the
597 * CGI procsss.
598 */
599 switch (fork()) {
600 case -1: /* eep, failure */
601 bozoerr(httpd, 1, "child fork failed: %s", strerror(errno));
602 /*NOTREACHED*/
603 case 0:
604 close(sv[0]);
605 dup2(sv[1], STDIN_FILENO);
606 dup2(sv[1], STDOUT_FILENO);
607 close(2);
608 close(sv[1]);
609 closelog();
610 bozo_daemon_closefds(httpd);
611
612 if (-1 == execve(path, argv, envp)) {
613 int saveerrno = errno;
614 bozo_http_error(httpd, 404, request,
615 "Cannot execute CGI");
616 /* don't log easy to trigger events */
617 if (saveerrno != ENOENT &&
618 saveerrno != EISDIR &&
619 saveerrno != EACCES)
620 bozoerr(httpd, 1, "child exec failed: %s: %s",
621 path, strerror(saveerrno));
622 _exit(1);
623 }
624 /* NOT REACHED */
625 bozoerr(httpd, 1, "child execve returned?!");
626 }
627
628 free(query);
629 free(file);
630 free(url);
631 for (i = 0; i < search_string_argc; i++)
632 free(search_string_argv[i]);
633 free(search_string_argv);
634
635 close(sv[1]);
636
637 /* parent: read from stdin (bozo_read()) write to sv[0] */
638 /* child: read from sv[0] (bozo_write()) write to stdout */
639 pid = fork();
640 if (pid == -1)
641 bozoerr(httpd, 1, "io child fork failed: %s", strerror(errno));
642 else if (pid == 0) {
643 /* child reader/writer */
644 close(STDIN_FILENO);
645 finish_cgi_output(httpd, request, sv[0], nph);
646 /* if we do SSL, send a SSL_shutdown now */
647 bozo_ssl_shutdown(request->hr_httpd);
648 /* if we're done output, our parent is useless... */
649 kill(getppid(), SIGKILL);
650 debug((httpd, DEBUG_FAT, "done processing cgi output"));
651 _exit(0);
652 }
653 close(STDOUT_FILENO);
654
655 /* CGI programs should perform their own timeouts */
656 while ((rbytes = bozo_read(httpd, STDIN_FILENO, buf, sizeof buf)) > 0) {
657 ssize_t wbytes;
658 char *bp = buf;
659
660 while (rbytes) {
661 wbytes = write(sv[0], buf, (size_t)rbytes);
662 if (wbytes > 0) {
663 rbytes -= wbytes;
664 bp += wbytes;
665 } else
666 bozoerr(httpd, 1, "write failed: %s",
667 strerror(errno));
668 }
669 }
670 debug((httpd, DEBUG_FAT, "done processing cgi input"));
671 exit(0);
672
673 out:
674
675 for (i = 0; i < search_string_argc; i++)
676 free(search_string_argv[i]);
677 free(search_string_argv);
678 free(query);
679 free(file);
680 free(url);
681 return 0;
682 }
683
684 #ifndef NO_DYNAMIC_CONTENT
685 /* cgi maps are simple ".postfix /path/to/prog" */
686 void
bozo_add_content_map_cgi(bozohttpd_t * httpd,const char * arg,const char * cgihandler)687 bozo_add_content_map_cgi(bozohttpd_t *httpd, const char *arg,
688 const char *cgihandler)
689 {
690 bozo_content_map_t *map;
691
692 debug((httpd, DEBUG_NORMAL, "bozo_add_content_map_cgi: name %s cgi %s",
693 arg, cgihandler));
694
695 httpd->process_cgi = 1;
696
697 map = bozo_get_content_map(httpd, arg);
698 map->name = arg;
699 map->type = map->encoding = map->encoding11 = NULL;
700 map->cgihandler = cgihandler;
701 }
702 #endif /* NO_DYNAMIC_CONTENT */
703
704 #endif /* NO_CGIBIN_SUPPORT */
705