1 /* $NetBSD: cgi-bozo.c,v 1.13 2009/04/18 21:22:03 mrg Exp $ */
3 /* $eterna: cgi-bozo.c,v 1.32 2009/05/22 21:51:38 mrg Exp $ */
6 * Copyright (c) 1997-2009 Matthew R. Green
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
33 /* this code implements CGI/1.2 for bozohttpd */
35 #ifndef NO_CGIBIN_SUPPORT
37 #include <sys/param.h>
38 #include <sys/socket.h>
49 #include <netinet/in.h>
51 #include "bozohttpd.h"
53 #define CGIBIN_PREFIX "cgi-bin/"
54 #define CGIBIN_PREFIX_LEN (sizeof(CGIBIN_PREFIX)-1)
56 static char *cgibin
; /* cgi-bin directory */
57 static int Cflag
; /* added a cgi handler, always process_cgi() */
59 static const char * content_cgihandler(http_req
*, const char *);
60 static void finish_cgi_output(http_req
*request
, int, int);
61 static int parse_header(const char *, ssize_t
, char **, char **);
62 static void append_index_html(char **);
65 set_cgibin(char *path
)
68 debug((DEBUG_OBESE
, "cgibin (cgi-bin directory) is %s", cgibin
));
71 /* help build up the environ pointer */
73 spsetenv(const char *env
, const char *val
, char **envp
)
75 char *s1
= bozomalloc(strlen(env
) + strlen(val
) + 2);
80 debug((DEBUG_OBESE
, "spsetenv: %s", s1
));
85 * Checks if the request has asked for a cgi-bin. Should only be called if
86 * cgibin is set. If it starts CGIBIN_PREFIX or has a ncontent handler,
87 * process the cgi, otherwise just return. Returns 0 if it did not handle
91 process_cgi(http_req
*request
)
94 struct headers
*headp
;
95 const char *type
, *clen
, *info
, *cgihandler
;
96 char *query
, *s
, *t
, *path
, *env
, *command
, *file
, *url
;
97 char **envp
, **curenvp
, *argv
[4];
101 int envpsize
, ix
, nph
;
104 if (!cgibin
&& !Cflag
)
107 asprintf(&file
, "/%s", request
->hr_file
);
110 if (request
->hr_query
&& strlen(request
->hr_query
))
111 query
= bozostrdup(request
->hr_query
);
115 asprintf(&url
, "%s%s%s", file
, query
? "?" : "", query
? query
: "");
118 debug((DEBUG_NORMAL
, "process_cgi: url `%s'", url
));
128 if (auth_check(request
, url
+ 1))
131 if (!cgibin
|| strncmp(url
+ 1, CGIBIN_PREFIX
, CGIBIN_PREFIX_LEN
) != 0) {
132 cgihandler
= content_cgihandler(request
, file
+ 1);
133 if (cgihandler
== NULL
) {
134 debug((DEBUG_FAT
, "process_cgi: no handler, returning"));
137 if (len
== 0 || file
[len
- 1] == '/')
138 append_index_html(&file
);
139 debug((DEBUG_NORMAL
, "process_cgi: cgihandler `%s'",
141 } else if (len
- 1 == CGIBIN_PREFIX_LEN
) /* url is "/cgi-bin/" */
142 append_index_html(&file
);
147 path
= bozostrdup(cgihandler
);
149 /* argv[] = [ path, command, query, NULL ] */
151 command
= file
+ CGIBIN_PREFIX_LEN
+ 1;
152 if ((s
= strchr(command
, '/')) != NULL
) {
153 info
= bozostrdup(s
);
156 path
= bozomalloc(strlen(cgibin
) + 1 + strlen(command
) + 1);
157 strcpy(path
, cgibin
);
159 strcat(path
, command
);
160 /* argv[] = [ command, query, NULL ] */
162 argv
[ix
++] = command
;
166 nph
= strncmp(command
, "nph-", 4) == 0;
168 type
= request
->hr_content_type
;
169 clen
= request
->hr_content_length
;
171 envpsize
= 13 + request
->hr_nheaders
+
172 (info
&& *info
? 1 : 0) +
173 (query
&& *query
? 1 : 0) +
174 (type
&& *type
? 1 : 0) +
175 (clen
&& *clen
? 1 : 0) +
176 (request
->hr_remotehost
&& *request
->hr_remotehost
? 1 : 0) +
177 (request
->hr_remoteaddr
&& *request
->hr_remoteaddr
? 1 : 0) +
178 auth_cgi_count(request
) +
179 (request
->hr_serverport
&& *request
->hr_serverport
? 1 : 0);
182 "process_cgi: path `%s' cmd `%s' info `%s' query `%s' nph `%d' envpsize `%d'",
183 path
, command
, strornull(info
), strornull(query
), nph
, envpsize
));
185 envp
= bozomalloc(sizeof(*envp
) * envpsize
);
186 for (ix
= 0; ix
< envpsize
; ix
++)
190 SIMPLEQ_FOREACH(headp
, &request
->hr_headers
, h_next
) {
192 env
= bozomalloc(6 + strlen(headp
->h_header
) + 1 +
193 strlen(headp
->h_value
));
198 for (s2
= headp
->h_header
; *s2
; t
++, s2
++)
199 if (islower((u_int
)*s2
))
200 *t
= toupper((u_int
)*s2
);
206 debug((DEBUG_OBESE
, "setting header %s as %s = %s",
207 headp
->h_header
, env
, headp
->h_value
));
208 spsetenv(env
, headp
->h_value
, curenvp
++);
212 #ifndef _PATH_DEFPATH
213 #define _PATH_DEFPATH "/usr/bin:/bin"
216 spsetenv("PATH", _PATH_DEFPATH
, curenvp
++);
217 spsetenv("IFS", " \t\n", curenvp
++);
218 spsetenv("SERVER_NAME", myname
, curenvp
++);
219 spsetenv("GATEWAY_INTERFACE", "CGI/1.1", curenvp
++);
220 spsetenv("SERVER_PROTOCOL", request
->hr_proto
, curenvp
++);
221 spsetenv("REQUEST_METHOD", request
->hr_methodstr
, curenvp
++);
222 spsetenv("SCRIPT_NAME", file
, curenvp
++);
223 spsetenv("SCRIPT_FILENAME", file
+ 1, curenvp
++);
224 spsetenv("SERVER_SOFTWARE", server_software
, curenvp
++);
225 spsetenv("REQUEST_URI", request
->hr_file
, curenvp
++);
226 spsetenv("DATE_GMT", http_date(), curenvp
++);
228 spsetenv("QUERY_STRING", query
, curenvp
++);
230 spsetenv("PATH_INFO", info
, curenvp
++);
232 spsetenv("CONTENT_TYPE", type
, curenvp
++);
234 spsetenv("CONTENT_LENGTH", clen
, curenvp
++);
235 if (request
->hr_serverport
&& *request
->hr_serverport
)
236 spsetenv("SERVER_PORT", request
->hr_serverport
, curenvp
++);
237 if (request
->hr_remotehost
&& *request
->hr_remotehost
)
238 spsetenv("REMOTE_HOST", request
->hr_remotehost
, curenvp
++);
239 if (request
->hr_remoteaddr
&& *request
->hr_remoteaddr
)
240 spsetenv("REMOTE_ADDR", request
->hr_remoteaddr
, curenvp
++);
241 auth_cgi_setenv(request
, &curenvp
);
246 debug((DEBUG_FAT
, "process_cgi: going exec %s, %s %s %s",
247 path
, argv
[0], strornull(argv
[1]), strornull(argv
[2])));
249 if (-1 == socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, sv
))
250 error(1, "child socketpair failed: %s", strerror(errno
));
253 * We create 2 procs: one to become the CGI, one read from
254 * the CGI and output to the network, and this parent will
255 * continue reading from the network and writing to the
259 case -1: /* eep, failure */
260 error(1, "child fork failed: %s", strerror(errno
));
263 dup2(sv
[1], STDIN_FILENO
);
264 dup2(sv
[1], STDOUT_FILENO
);
270 if (-1 == execve(path
, argv
, envp
))
271 error(1, "child exec failed: %s: %s",
272 path
, strerror(errno
));
274 error(1, "child execve returned?!");
279 /* parent: read from stdin (bozoread()) write to sv[0] */
280 /* child: read from sv[0] (bozowrite()) write to stdout */
283 error(1, "io child fork failed: %s", strerror(errno
));
285 /* child reader/writer */
287 finish_cgi_output(request
, sv
[0], nph
);
288 /* if we're done output, our parent is useless... */
289 kill(getppid(), SIGKILL
);
290 debug((DEBUG_FAT
, "done processing cgi output"));
293 close(STDOUT_FILENO
);
295 /* XXX we should have some goo that times us out
297 while ((rbytes
= bozoread(STDIN_FILENO
, buf
, sizeof buf
)) > 0) {
302 wbytes
= write(sv
[0], buf
, rbytes
);
307 error(1, "write failed: %s", strerror(errno
));
310 debug((DEBUG_FAT
, "done processing cgi input"));
324 * handle parsing a CGI header output, transposing a Status: header
325 * into the HTTP reply (ie, instead of "200 OK").
328 finish_cgi_output(http_req
*request
, int in
, int nph
)
334 SIMPLEQ_HEAD(, headers
) headers
;
335 struct headers
*hdr
, *nhdr
;
336 int write_header
, nheaders
= 0;
338 /* much of this code is like read_request()'s header loop. hmmm... */
339 SIMPLEQ_INIT(&headers
);
340 write_header
= nph
== 0;
341 while (nph
== 0 && (str
= bozodgetln(in
, &len
, read
)) != NULL
) {
342 char *hdr_name
, *hdr_value
;
344 if (parse_header(str
, len
, &hdr_name
, &hdr_value
))
348 * The CGI 1.{1,2} spec both say that if the cgi program
349 * returns a `Status:' header field then the server MUST
350 * return it in the response. If the cgi program does
351 * not return any `Status:' header then the server should
352 * respond with 200 OK.
353 * XXX The CGI 1.1 and 1.2 specification differ slightly on
354 * this in that v1.2 says that the script MUST NOT return a
355 * `Status:' header if it is returning a `Location:' header.
356 * For compatibility we are going with the CGI 1.1 behavior.
358 if (strcasecmp(hdr_name
, "status") == 0) {
359 debug((DEBUG_OBESE
, "process_cgi: writing HTTP header "
360 "from status %s ..", hdr_value
));
361 bozoprintf("%s %s\r\n", request
->hr_proto
, hdr_value
);
368 hdr
= bozomalloc(sizeof *hdr
);
369 hdr
->h_header
= hdr_name
;
370 hdr
->h_value
= hdr_value
;
371 SIMPLEQ_INSERT_TAIL(&headers
, hdr
, h_next
);
376 debug((DEBUG_OBESE
, "process_cgi: writing HTTP header .."));
377 bozoprintf("%s 200 OK\r\n", request
->hr_proto
);
382 debug((DEBUG_OBESE
, "process_cgi: writing delayed HTTP "
384 SIMPLEQ_FOREACH_SAFE(hdr
, &headers
, h_next
, nhdr
) {
385 bozoprintf("%s: %s\r\n", hdr
->h_header
, hdr
->h_value
);
393 /* XXX we should have some goo that times us out
395 while ((rbytes
= read(in
, buf
, sizeof buf
)) > 0) {
400 wbytes
= bozowrite(STDOUT_FILENO
, buf
, rbytes
);
405 error(1, "cgi output write failed: %s",
412 parse_header(const char *str
, ssize_t len
, char **hdr_str
, char **hdr_val
)
416 /* if the string passed is zero-length bail out */
420 value
= bozostrdup(str
);
422 /* locate the ':' separator in the header/value */
423 name
= bozostrnsep(&value
, ":", &len
);
425 if (NULL
== name
|| -1 == len
) {
430 /* skip leading space/tab */
431 while (*value
== ' ' || *value
== '\t')
441 * given the file name, return a CGI interpreter
444 content_cgihandler(http_req
*request
, const char *file
)
446 struct content_map
*map
;
448 debug((DEBUG_FAT
, "content_cgihandler: trying file %s", file
));
449 map
= match_content_map(file
, 0);
451 return (map
->cgihandler
);
456 append_index_html(char **url
)
458 *url
= bozorealloc(*url
, strlen(*url
) + strlen(index_html
) + 1);
459 strcat(*url
, index_html
);
460 debug((DEBUG_NORMAL
, "append_index_html: url adjusted to `%s'", *url
));
463 #ifndef NO_DYNAMIC_CONTENT
464 /* cgi maps are simple ".postfix /path/to/prog" */
466 add_content_map_cgi(char *arg
, char *cgihandler
)
468 struct content_map
*map
;
470 debug((DEBUG_NORMAL
, "add_content_map_cgi: name %s cgi %s", arg
, cgihandler
));
474 map
= get_content_map(arg
);
476 map
->type
= map
->encoding
= map
->encoding11
= NULL
;
477 map
->cgihandler
= cgihandler
;
479 #endif /* NO_DYNAMIC_CONTENT */
481 #endif /* NO_CGIBIN_SUPPORT */