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