cgi-bozo.c revision 1.3 1 1.1 tls /* $eterna: cgi-bozo.c,v 1.13 2006/05/17 08:19:10 mrg Exp $ */
2 1.1 tls
3 1.1 tls /*
4 1.1 tls * Copyright (c) 1997-2006 Matthew R. Green
5 1.1 tls * All rights reserved.
6 1.1 tls *
7 1.1 tls * Redistribution and use in source and binary forms, with or without
8 1.1 tls * modification, are permitted provided that the following conditions
9 1.1 tls * are met:
10 1.1 tls * 1. Redistributions of source code must retain the above copyright
11 1.1 tls * notice, this list of conditions and the following disclaimer.
12 1.1 tls * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tls * notice, this list of conditions and the following disclaimer and
14 1.1 tls * dedication in the documentation and/or other materials provided
15 1.1 tls * with the distribution.
16 1.1 tls * 3. The name of the author may not be used to endorse or promote products
17 1.1 tls * derived from this software without specific prior written permission.
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.1 tls #include <unistd.h>
47 1.1 tls
48 1.1 tls #include <netinet/in.h>
49 1.1 tls
50 1.1 tls #include "bozohttpd.h"
51 1.1 tls
52 1.1 tls #define CGIBIN_PREFIX "cgi-bin/"
53 1.1 tls #define CGIBIN_PREFIX_LEN (sizeof(CGIBIN_PREFIX)-1)
54 1.1 tls
55 1.1 tls static char *cgibin; /* cgi-bin directory */
56 1.1 tls static int Cflag; /* added a cgi handler, always process_cgi() */
57 1.1 tls
58 1.1 tls static const char *content_cgihandler(http_req *, const char *);
59 1.1 tls static void finish_cgi_output(http_req *request, int, int);
60 1.1 tls
61 1.1 tls void
62 1.1 tls set_cgibin(char *path)
63 1.1 tls {
64 1.1 tls cgibin = path;
65 1.1 tls debug((DEBUG_OBESE, "cgibin (cgi-bin directory) is %s", cgibin));
66 1.1 tls }
67 1.1 tls
68 1.1 tls /* help build up the environ pointer */
69 1.1 tls void
70 1.1 tls spsetenv(const char *env, const char *val, char **envp)
71 1.1 tls {
72 1.1 tls char *s1 = bozomalloc(strlen(env) + strlen(val) + 2);
73 1.1 tls
74 1.1 tls strcpy(s1, env);
75 1.1 tls strcat(s1, "=");
76 1.1 tls strcat(s1, val);
77 1.1 tls debug((DEBUG_OBESE, "spsetenv: %s", s1));
78 1.1 tls *envp = s1;
79 1.1 tls }
80 1.1 tls
81 1.1 tls /*
82 1.1 tls * Checks if the request has asked for a cgi-bin. Should only be called if
83 1.1 tls * cgibin is set. If it starts CGIBIN_PREFIX or has a ncontent handler,
84 1.1 tls * process the cgi, otherwise just return.
85 1.1 tls */
86 1.1 tls void
87 1.1 tls process_cgi(http_req *request)
88 1.1 tls {
89 1.1 tls char buf[WRSZ];
90 1.1 tls struct headers *headp;
91 1.1 tls const char *type, *clen, *info, *cgihandler;
92 1.1 tls char *query, *s, *t, *path, *env, *command, *url;
93 1.1 tls char **envp, **curenvp, *argv[4];
94 1.1 tls size_t len;
95 1.1 tls ssize_t rbytes;
96 1.1 tls pid_t pid;
97 1.1 tls int envpsize, ix, nph;
98 1.1 tls int sv[2];
99 1.1 tls
100 1.1 tls if (!cgibin && !Cflag)
101 1.1 tls return;
102 1.1 tls
103 1.1 tls debug((DEBUG_NORMAL, "process_cgi: url `%s'", request->hr_url));
104 1.1 tls
105 1.1 tls url = bozostrdup(request->hr_url);
106 1.1 tls if ((s = strchr(url, '?')) != NULL) {
107 1.1 tls *s++ = '\0';
108 1.1 tls query = s;
109 1.1 tls } else
110 1.1 tls query = NULL;
111 1.1 tls path = NULL;
112 1.1 tls envp = NULL;
113 1.1 tls cgihandler = NULL;
114 1.1 tls command = NULL;
115 1.1 tls info = NULL;
116 1.1 tls
117 1.1 tls len = strlen(url);
118 1.1 tls if (len == 0 || url[len - 1] == '/') { /* append index.html */
119 1.1 tls debug((DEBUG_FAT, "appending index.html"));
120 1.1 tls url = bozorealloc(url, len + strlen(index_html) + 1);
121 1.1 tls strcat(url, index_html);
122 1.1 tls debug((DEBUG_NORMAL, "process_cgi: url adjusted to `%s'", url));
123 1.1 tls }
124 1.1 tls
125 1.1 tls auth_check(request, url + 1);
126 1.1 tls
127 1.1 tls if (!cgibin || strncmp(url + 1, CGIBIN_PREFIX, CGIBIN_PREFIX_LEN) != 0) {
128 1.1 tls cgihandler = content_cgihandler(request, url + 1);
129 1.1 tls if (cgihandler == NULL) {
130 1.1 tls free(url);
131 1.1 tls return;
132 1.1 tls }
133 1.1 tls debug((DEBUG_NORMAL, "process_cgi: cgihandler `%s'",
134 1.1 tls cgihandler));
135 1.1 tls }
136 1.1 tls
137 1.1 tls ix = 0;
138 1.1 tls if (cgihandler) {
139 1.1 tls command = url + 1;
140 1.1 tls path = bozostrdup(cgihandler);
141 1.1 tls argv[ix++] = path;
142 1.1 tls /* argv[] = [ path, command, query, NULL ] */
143 1.1 tls } else {
144 1.1 tls command = url + CGIBIN_PREFIX_LEN + 1;
145 1.1 tls if ((s = strchr(command, '/')) != NULL) {
146 1.1 tls info = bozostrdup(s);
147 1.1 tls *s = '\0';
148 1.1 tls }
149 1.1 tls path = bozomalloc(strlen(cgibin) + 1 + strlen(command) + 1);
150 1.1 tls strcpy(path, cgibin);
151 1.1 tls strcat(path, "/");
152 1.1 tls strcat(path, command);
153 1.1 tls /* argv[] = [ command, query, NULL ] */
154 1.1 tls }
155 1.1 tls argv[ix++] = command;
156 1.1 tls argv[ix++] = query;
157 1.1 tls argv[ix++] = NULL;
158 1.1 tls
159 1.1 tls nph = strncmp(command, "nph-", 4) == 0;
160 1.1 tls
161 1.1 tls debug((DEBUG_FAT,
162 1.1 tls "process_cgi: path `%s' cmd `%s' info `%s' query `%s' nph `%d'",
163 1.1 tls path, command, strornull(info), strornull(query), nph));
164 1.1 tls
165 1.1 tls type = request->hr_content_type;
166 1.1 tls clen = request->hr_content_length;
167 1.1 tls
168 1.1 tls envpsize = 13 + request->hr_nheaders +
169 1.1 tls (info && *info ? 1 : 0) +
170 1.1 tls (query && *query ? 1 : 0) +
171 1.1 tls (type && *type ? 1 : 0) +
172 1.1 tls (clen && *clen ? 1 : 0) +
173 1.1 tls (request->hr_remotehost && *request->hr_remotehost ? 1 : 0) +
174 1.1 tls (request->hr_remoteaddr && *request->hr_remoteaddr ? 1 : 0) +
175 1.1 tls auth_cgi_count(request) +
176 1.1 tls (request->hr_serverport && *request->hr_serverport ? 1 : 0);
177 1.1 tls
178 1.1 tls envp = bozomalloc(sizeof(*envp) * envpsize);
179 1.1 tls for (ix = 0; ix < envpsize; ix++)
180 1.1 tls envp[ix] = NULL;
181 1.1 tls curenvp = envp;
182 1.3 tls
183 1.3 tls SIMPLEQ_FOREACH(headp, &request->hr_headers, h_next) {
184 1.1 tls const char *s2;
185 1.1 tls env = bozomalloc(6 + strlen(headp->h_header) + 1 +
186 1.1 tls strlen(headp->h_value));
187 1.1 tls
188 1.1 tls t = env;
189 1.1 tls strcpy(t, "HTTP_");
190 1.1 tls t += strlen(t);
191 1.1 tls for (s2 = headp->h_header; *s2; t++, s2++)
192 1.1 tls if (islower((u_int)*s2))
193 1.1 tls *t = toupper((u_int)*s2);
194 1.1 tls else if (*s2 == '-')
195 1.1 tls *t = '_';
196 1.1 tls else
197 1.1 tls *t = *s2;
198 1.1 tls *t = '\0';
199 1.1 tls debug((DEBUG_OBESE, "setting header %s as %s = %s",
200 1.1 tls headp->h_header, env, headp->h_value));
201 1.1 tls spsetenv(env, headp->h_value, curenvp++);
202 1.1 tls free(env);
203 1.1 tls }
204 1.1 tls
205 1.1 tls #ifndef _PATH_DEFPATH
206 1.1 tls #define _PATH_DEFPATH "/usr/bin:/bin"
207 1.1 tls #endif
208 1.1 tls
209 1.1 tls spsetenv("PATH", _PATH_DEFPATH, curenvp++);
210 1.1 tls spsetenv("IFS", " \t\n", curenvp++);
211 1.1 tls spsetenv("SERVER_NAME", myname, curenvp++);
212 1.1 tls spsetenv("GATEWAY_INTERFACE", "CGI/1.1", curenvp++);
213 1.1 tls spsetenv("SERVER_PROTOCOL", request->hr_proto, curenvp++);
214 1.1 tls spsetenv("REQUEST_METHOD", request->hr_methodstr, curenvp++);
215 1.1 tls spsetenv("SCRIPT_NAME", url, curenvp++);
216 1.1 tls spsetenv("SCRIPT_FILENAME", url + 1, curenvp++);
217 1.1 tls spsetenv("SERVER_SOFTWARE", server_software, curenvp++);
218 1.1 tls spsetenv("REQUEST_URI", request->hr_url, curenvp++);
219 1.1 tls spsetenv("DATE_GMT", http_date(), curenvp++);
220 1.1 tls if (query && *query)
221 1.1 tls spsetenv("QUERY_STRING", query, curenvp++);
222 1.1 tls if (info && *info)
223 1.1 tls spsetenv("PATH_INFO", info, curenvp++);
224 1.1 tls if (type && *type)
225 1.1 tls spsetenv("CONTENT_TYPE", type, curenvp++);
226 1.1 tls if (clen && *clen)
227 1.1 tls spsetenv("CONTENT_LENGTH", clen, curenvp++);
228 1.1 tls if (request->hr_serverport && *request->hr_serverport)
229 1.1 tls spsetenv("SERVER_PORT", request->hr_serverport, curenvp++);
230 1.1 tls if (request->hr_remotehost && *request->hr_remotehost)
231 1.1 tls spsetenv("REMOTE_HOST", request->hr_remotehost, curenvp++);
232 1.1 tls if (request->hr_remoteaddr && *request->hr_remoteaddr)
233 1.1 tls spsetenv("REMOTE_ADDR", request->hr_remoteaddr, curenvp++);
234 1.1 tls auth_cgi_setenv(request, &curenvp);
235 1.1 tls
236 1.1 tls debug((DEBUG_FAT, "process_cgi: going exec %s, %s %s %s",
237 1.1 tls path, argv[0], strornull(argv[1]), strornull(argv[2])));
238 1.1 tls
239 1.1 tls if (-1 == socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sv))
240 1.1 tls error(1, "child socketpair failed: %s", strerror(errno));
241 1.1 tls
242 1.1 tls /*
243 1.1 tls * We create 2 procs: one to become the CGI, one read from
244 1.1 tls * the CGI and output to the network, and this parent will
245 1.1 tls * continue reading from the network and writing to the
246 1.1 tls * CGI procsss.
247 1.1 tls */
248 1.1 tls switch (fork()) {
249 1.1 tls case -1: /* eep, failure */
250 1.1 tls error(1, "child fork failed: %s", strerror(errno));
251 1.1 tls case 0:
252 1.1 tls close(sv[0]);
253 1.1 tls dup2(sv[1], STDIN_FILENO);
254 1.1 tls dup2(sv[1], STDOUT_FILENO);
255 1.1 tls
256 1.1 tls if (-1 == execve(path, argv, envp))
257 1.1 tls error(1, "child exec failed: %s", path);
258 1.1 tls /* NOT REACHED */
259 1.1 tls error(1, "child execve returned?!");
260 1.1 tls }
261 1.1 tls
262 1.1 tls close(sv[1]);
263 1.1 tls
264 1.1 tls /* parent: read from stdin (bozoread()) write to sv[0] */
265 1.1 tls /* child: read from sv[0] (bozowrite()) write to stdout */
266 1.1 tls pid = fork();
267 1.1 tls if (pid == -1)
268 1.1 tls error(1, "io child fork failed: %s", strerror(errno));
269 1.1 tls else if (pid == 0) {
270 1.1 tls /* child reader/writer */
271 1.1 tls close(STDIN_FILENO);
272 1.1 tls finish_cgi_output(request, sv[0], nph);
273 1.1 tls /* if we're done output, our parent is useless... */
274 1.1 tls kill(getppid(), SIGKILL);
275 1.1 tls debug((DEBUG_FAT, "done processing cgi output"));
276 1.1 tls _exit(0);
277 1.1 tls }
278 1.1 tls close(STDOUT_FILENO);
279 1.1 tls
280 1.1 tls /* XXX we should have some goo that times us out
281 1.1 tls */
282 1.1 tls while ((rbytes = bozoread(STDIN_FILENO, buf, sizeof buf)) > 0) {
283 1.1 tls ssize_t wbytes;
284 1.1 tls char *bp = buf;
285 1.1 tls
286 1.1 tls while (rbytes) {
287 1.1 tls wbytes = write(sv[0], buf , rbytes);
288 1.1 tls if (wbytes > 0) {
289 1.1 tls rbytes -= wbytes;
290 1.1 tls bp += wbytes;
291 1.1 tls } else
292 1.1 tls error(1, "write failed: %s", strerror(errno));
293 1.1 tls }
294 1.1 tls }
295 1.1 tls debug((DEBUG_FAT, "done processing cgi input"));
296 1.1 tls exit(0);
297 1.1 tls }
298 1.1 tls
299 1.1 tls /*
300 1.1 tls * handle parsing a CGI header output, transposing a Status: header
301 1.1 tls * into the HTTP reply (ie, instead of "200 OK").
302 1.1 tls */
303 1.1 tls static void
304 1.1 tls finish_cgi_output(http_req *request, int in, int nph)
305 1.1 tls {
306 1.1 tls char buf[WRSZ];
307 1.1 tls char *str, *val;
308 1.1 tls size_t len;
309 1.1 tls ssize_t rbytes;
310 1.1 tls SIMPLEQ_HEAD(, headers) headers;
311 1.1 tls struct headers *hdr;
312 1.1 tls int write_header, nheaders = 0, write_str = 0;
313 1.1 tls
314 1.1 tls /* much of this code is like read_request()'s header loop. hmmm... */
315 1.1 tls SIMPLEQ_INIT(&headers);
316 1.1 tls write_header = nph == 0;
317 1.2 tls while (nph == 0 && (str = dgetln(in, (ssize_t *)&len, read)) != NULL) {
318 1.1 tls str = bozostrdup(str); /* we use this copy */
319 1.1 tls
320 1.1 tls if (*str == '\0') {
321 1.1 tls write_str = 1;
322 1.1 tls break;
323 1.1 tls }
324 1.1 tls
325 1.2 tls val = strnsep(&str, ":", (ssize_t *)&len);
326 1.1 tls debug((DEBUG_EXPLODING,
327 1.1 tls "read_req2: after strnsep: str ``%s'' val ``%s''",
328 1.1 tls str, val));
329 1.1 tls if (val == NULL || len == -1) {
330 1.1 tls write_str = 1;
331 1.1 tls break;
332 1.1 tls }
333 1.1 tls while (*str == ' ' || *str == '\t')
334 1.1 tls len--, str++;
335 1.1 tls
336 1.1 tls /*
337 1.1 tls * The CGI 1.{1,2} spec both say that if the cgi program
338 1.1 tls * returns a `Status:' header field then the server MUST
339 1.1 tls * return it in the response. If the cgi program does
340 1.1 tls * not return any `Status:' header then the server should
341 1.1 tls * respond with 200 OK.
342 1.1 tls * XXX The CGI 1.1 and 1.2 specification differ slightly on
343 1.1 tls * this in that v1.2 says that the script MUST NOT return a
344 1.1 tls * `Status:' header if it is returning a `Location:' header.
345 1.1 tls * For compatibility we are going with the CGI 1.1 behavior.
346 1.1 tls */
347 1.1 tls if (strcasecmp(val, "status") == 0) {
348 1.1 tls debug((DEBUG_OBESE, "process_cgi: writing HTTP header "
349 1.1 tls "from status %s ..", str));
350 1.1 tls bozoprintf("%s %s\r\n", request->hr_proto, str);
351 1.1 tls bozoflush(stdout);
352 1.1 tls write_header = 0;
353 1.1 tls break;
354 1.1 tls }
355 1.1 tls
356 1.1 tls hdr = bozomalloc(sizeof *hdr);
357 1.1 tls hdr->h_header = val;
358 1.1 tls hdr->h_value = str;
359 1.1 tls SIMPLEQ_INSERT_TAIL(&headers, hdr, h_next);
360 1.1 tls nheaders++;
361 1.1 tls }
362 1.1 tls
363 1.1 tls if (write_header) {
364 1.1 tls debug((DEBUG_OBESE, "process_cgi: writing HTTP header .."));
365 1.1 tls bozoprintf("%s 200 OK\r\n", request->hr_proto);
366 1.1 tls bozoflush(stdout);
367 1.1 tls }
368 1.1 tls
369 1.1 tls if (nheaders) {
370 1.1 tls debug((DEBUG_OBESE, "process_cgi: writing delayed HTTP "
371 1.1 tls "headers .."));
372 1.3 tls SIMPLEQ_FOREACH(hdr, &headers, h_next) {
373 1.3 tls bozoprintf("%s: %s\r\n", hdr->h_header, hdr->h_value);
374 1.3 tls free(hdr->h_value);
375 1.1 tls }
376 1.1 tls bozoflush(stdout);
377 1.1 tls }
378 1.1 tls
379 1.1 tls /* XXX we should have some goo that times us out
380 1.1 tls */
381 1.1 tls while ((rbytes = read(in, buf, sizeof buf)) > 0) {
382 1.1 tls ssize_t wbytes;
383 1.1 tls char *bp = buf;
384 1.1 tls
385 1.1 tls while (rbytes) {
386 1.1 tls wbytes = bozowrite(STDOUT_FILENO, buf, rbytes);
387 1.1 tls if (wbytes > 0) {
388 1.1 tls rbytes -= wbytes;
389 1.1 tls bp += wbytes;
390 1.1 tls } else
391 1.1 tls error(1, "cgi output write failed: %s",
392 1.1 tls strerror(errno));
393 1.1 tls }
394 1.1 tls }
395 1.1 tls }
396 1.1 tls
397 1.1 tls /*
398 1.1 tls * given the file name, return a CGI interpreter
399 1.1 tls */
400 1.1 tls static const char *
401 1.1 tls content_cgihandler(http_req *request, const char *file)
402 1.1 tls {
403 1.1 tls struct content_map *map;
404 1.1 tls
405 1.1 tls map = match_content_map(file, 0);
406 1.1 tls if (map)
407 1.1 tls return (map->cgihandler);
408 1.1 tls return (NULL);
409 1.1 tls }
410 1.1 tls
411 1.1 tls #ifndef NO_DYNAMIC_CONTENT
412 1.1 tls /* cgi maps are simple ".postfix /path/to/prog" */
413 1.1 tls void
414 1.1 tls add_content_map_cgi(char *arg, char *cgihandler)
415 1.1 tls {
416 1.1 tls struct content_map *map;
417 1.1 tls
418 1.1 tls debug((DEBUG_NORMAL, "add_content_map_cgi: name %s cgi %s", arg, cgihandler));
419 1.1 tls
420 1.1 tls Cflag = 1;
421 1.1 tls
422 1.1 tls map = get_content_map(arg);
423 1.1 tls map->name = arg;
424 1.1 tls map->type = map->encoding = map->encoding11 = NULL;
425 1.1 tls map->cgihandler = cgihandler;
426 1.1 tls }
427 1.1 tls #endif /* NO_DYNAMIC_CONTENT */
428 1.1 tls
429 1.1 tls #endif /* NO_CGIBIN_SUPPORT */
430