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