1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * 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 in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
60 * http_script: keeps all script-related ramblings together.
62 * Compliant to cgi/1.1 spec
64 * Adapted by rst from original NCSA code by Rob McCool
66 * Apache adds some new env vars; REDIRECT_URL and REDIRECT_QUERY_STRING for
67 * custom error responses, and DOCUMENT_ROOT because we found it useful.
68 * It also adds SERVER_ADMIN - useful for scripts to know who to mail when
77 #include "apr_strings.h"
78 #include "apr_general.h"
79 #include "apr_file_io.h"
80 #include "apr_portable.h"
81 #include "apr_buckets.h"
82 #include "util_filter.h"
84 #include "http_config.h"
85 #include "http_request.h"
86 #include "http_core.h"
87 #include "http_protocol.h"
88 #include "http_main.h"
90 #include "util_script.h"
91 #include "http_conf_globals.h"
95 #ifdef HAVE_SYS_SOCKET_H
96 #include <sys/socket.h>
101 #ifdef HAVE_STRINGS_H
104 #include <sys/un.h> /* for sockaddr_un */
105 #include <sys/types.h>
107 module AP_MODULE_DECLARE_DATA cgid_module;
109 static void cgid_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *main_server);
111 static apr_pool_t *pcgi;
113 /* KLUDGE --- for back-combatibility, we don't have to check Execcgid
114 * in ScriptAliased directories, which means we need to know if this
115 * request came through ScriptAlias or not... so the Alias module
116 * leaves a note for us.
119 static int is_scriptaliased(request_rec *r)
121 const char *t = apr_table_get(r->notes, "alias-forced-type");
122 return t && (!strcasecmp(t, "cgi-script"));
125 /* Configuration stuff */
127 #define DEFAULT_LOGBYTES 10385760
128 #define DEFAULT_BUFBYTES 1024
129 #define DEFAULT_SOCKET "logs/cgisock"
131 #define SHELL_PATH "/bin/sh"
133 /* DEFAULT_CGID_LISTENBACKLOG controls the max depth on the unix socket's
134 * pending connection queue. If a bunch of cgi requests arrive at about
135 * the same time, connections from httpd threads/processes will back up
136 * in the queue while the cgid process slowly forks off a child to process
137 * each connection on the unix socket. If the queue is too short, the
138 * httpd process will get ECONNREFUSED when trying to connect.
140 #ifndef DEFAULT_CGID_LISTENBACKLOG
141 #define DEFAULT_CGID_LISTENBACKLOG 100
145 const char *sockname;
151 /* If a request includes query info in the URL (stuff after "?"), and
152 * the query info does not contain "=" (indicative of a FORM submission),
153 * then this routine is called to create the argument list to be passed
154 * to the CGI script. When suexec is enabled, the suexec path, user, and
155 * group are the first three arguments to be passed; if not, all three
156 * must be NULL. The query info is split into separate arguments, where
157 * "+" is the separator between keyword arguments.
159 * XXXX: note that the WIN32 code uses one of the suexec strings
160 * to pass an interpreter name. Remember this if changing the way they
161 * are handled in create_argv.
164 static char **create_argv(apr_pool_t *p, char *path, char *user, char *group,
165 char *av0, const char *args)
172 /* count the number of keywords */
174 for (x = 0, numwords = 1; args[x]; x++) {
175 if (args[x] == '+') {
180 if (numwords > APACHE_ARG_MAX - 5) {
181 numwords = APACHE_ARG_MAX - 5; /* Truncate args to prevent overrun */
183 av = (char **) apr_palloc(p, (numwords + 5) * sizeof(char *));
197 for (x = 1; x <= numwords; x++) {
198 w = ap_getword_nulls(p, &args, '+');
200 av[idx++] = ap_escape_shell_cmd(p, w);
206 static void cgid_maint(int reason, void *data, apr_wait_t status)
208 #if APR_HAS_OTHER_CHILD
211 case APR_OC_REASON_DEATH:
212 case APR_OC_REASON_LOST:
213 /* stop gap to make sure everything else works. In the end,
214 * we'll just restart the cgid server. */
215 apr_destroy_pool(pcgi);
216 kill(getppid(), SIGWINCH);
218 case APR_OC_REASON_RESTART:
219 case APR_OC_REASON_UNREGISTER:
220 apr_destroy_pool(pcgi);
227 static void get_req(int fd, request_rec *r, char **filename, char **argv0, char ***env)
232 core_dir_config *temp_core;
235 r->server = apr_pcalloc(r->pool, sizeof(server_rec));
237 read(fd, &j, sizeof(int));
238 read(fd, &len, sizeof(int));
239 data = apr_pcalloc(r->pool, len + 1); /* get a cleared byte for final '\0' */
240 i = read(fd, data, len);
242 r->filename = ap_getword(r->pool, (const char **)&data, '\n');
243 *argv0 = ap_getword(r->pool, (const char **)&data, '\n');
245 r->uri = ap_getword(r->pool, (const char **)&data, '\n');
247 environ = apr_pcalloc(r->pool, (j + 2) *sizeof(char *));
249 for (i = 0; i < j; i++) {
250 environ[i] = ap_getword(r->pool, (const char **)&data, '\n');
253 r->args = ap_getword(r->pool, (const char **)&data, '\n');
255 read(fd, &i, sizeof(int));
257 /* add 1, so that if i == 0, we still malloc something. */
258 dconf = (void **)apr_palloc(r->pool, sizeof(void *) * i + 1);
259 temp_core = (core_dir_config *)apr_palloc(r->pool, sizeof(core_module));
261 dconf[i] = (void *)temp_core;
262 r->per_dir_config = dconf;
266 read(fd, &j, sizeof(int));
268 temp_core->limit_cpu = (struct rlimit *)apr_palloc (sizeof(struct rlimit));
269 read(fd, temp_core->limit_cpu, sizeof(struct rlimit));
272 temp_core->limit_cpu = NULL;
276 #if defined (RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
277 read(fd, &j, sizeof(int));
279 temp_core->limit_mem = (struct rlimit *)apr_palloc(r->pool, sizeof(struct rlimit));
280 read(fd, temp_core->limit_mem, sizeof(struct rlimit));
283 temp_core->limit_mem = NULL;
288 read(fd, &j, sizeof(int));
290 temp_core->limit_nproc = (struct rlimit *)apr_palloc(r->pool, sizeof(struct rlimit));
291 read(fd, temp_core->limit_nproc, sizeof(struct rlimit));
294 temp_core->limit_nproc = NULL;
298 /* For right now, just make the notes table. At some point we will need
299 * to actually fill this out, but for now we just don't want suexec to
302 r->notes = apr_make_table(r->pool, 1);
307 static void send_req(int fd, request_rec *r, char *argv0, char **env)
313 data = apr_pstrcat(r->pool, r->filename, "\n", argv0, "\n", r->uri, "\n",
316 for (i =0; env[i]; i++) {
320 if (write(fd, &i, sizeof(int)) < 0) {
321 ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
322 "write to cgi daemon process");
325 for (i = 0; env[i]; i++) {
326 data = apr_pstrcat(r->pool, data, env[i], "\n", NULL);
328 data = apr_pstrcat(r->pool, data, r->args, NULL);
330 if (write(fd, &len, sizeof(int)) < 0) {
331 ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
332 "write to cgi daemon process");
334 if (write(fd, data, len) < 0) {
335 ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
336 "write to cgi daemon process");
338 if (write(fd, &core_module.module_index, sizeof(int)) < 0) {
339 ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
340 "write to cgi daemon process");
344 if (conf->limit_cpu) {
346 write(fd, &len, sizeof(int));
347 write(fd, conf->limit_cpu, sizeof(struct rlimit));
351 write(fd, &len, sizeof(int));
355 #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
356 if (conf->limit_mem) {
358 write(fd, &len, sizeof(int));
359 write(fd, conf->limit_mem, sizeof(struct rlimit));
363 write(fd, &len, sizeof(int));
368 if (conf->limit_nproc) {
370 write(fd, &len, sizeof(int));
371 write(fd, conf->limit_nproc, sizeof(struct rlimit));
375 write(fd, &len, sizeof(int));
381 static int cgid_server(void *data)
383 struct sockaddr_un unix_addr;
387 server_rec *main_server = data;
388 cgid_server_conf *sconf = (cgid_server_conf *)ap_get_module_config(
389 main_server->module_config, &cgid_module);
391 apr_signal(SIGCHLD, SIG_IGN);
392 if (unlink(sconf->sockname) < 0 && errno != ENOENT) {
393 ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
394 "Couldn't unlink unix domain socket %s",
396 /* just a warning; don't bail out */
399 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
400 ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
401 "Couldn't create unix domain socket");
405 memset(&unix_addr, 0, sizeof(unix_addr));
406 unix_addr.sun_family = AF_UNIX;
407 strcpy(unix_addr.sun_path, sconf->sockname);
409 omask = umask(0077); /* so that only Apache can use socket */
410 rc = bind(sd, (struct sockaddr *)&unix_addr, sizeof(unix_addr));
411 umask(omask); /* can't fail, so can't clobber errno */
413 ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
414 "Couldn't bind unix domain socket %s",
419 if (listen(sd, DEFAULT_CGID_LISTENBACKLOG) < 0) {
420 ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
421 "Couldn't listen on unix domain socket");
426 if (chown(sconf->sockname, unixd_config.user_id, -1) < 0) {
427 ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
428 "Couldn't change owner of unix domain socket %s",
434 unixd_setup_child(); /* if running as root, switch to configured user/group */
437 int errfileno = STDERR_FILENO;
441 const char * const *argv;
444 apr_procattr_t *procattr = NULL;
445 apr_proc_t *procnew = NULL;
446 apr_file_t *inout = NULL;
449 len = sizeof(unix_addr);
450 sd2 = accept(sd, (struct sockaddr *)&unix_addr, &len);
452 if (errno != EINTR) {
453 ap_log_error(APLOG_MARK, APLOG_ERR, errno,
455 "Error accepting on cgid socket.");
460 apr_create_pool(&p, pcgi);
462 r = apr_pcalloc(p, sizeof(request_rec));
463 procnew = apr_pcalloc(p, sizeof(*procnew));
465 get_req(sd2, r, &filename, &argv0, &env);
466 apr_put_os_file(&r->server->error_log, &errfileno, r->pool);
467 apr_put_os_file(&inout, &sd2, r->pool);
469 if (((rc = apr_createprocattr_init(&procattr, p)) != APR_SUCCESS) ||
470 ((rc = apr_setprocattr_io(procattr,
473 APR_CHILD_BLOCK)) != APR_SUCCESS) ||
474 ((rc = apr_setprocattr_childin(procattr, inout, NULL)) != APR_SUCCESS) ||
475 ((rc = apr_setprocattr_childout(procattr, inout, NULL)) != APR_SUCCESS) ||
476 ((rc = apr_setprocattr_childerr(procattr, r->server->error_log, NULL)) != APR_SUCCESS) ||
477 ((rc = apr_setprocattr_dir(procattr,
478 ap_make_dirstr_parent(r->pool, r->filename))) != APR_SUCCESS) ||
479 ((rc = apr_setprocattr_cmdtype(procattr, APR_PROGRAM)) != APR_SUCCESS)) {
480 /* Something bad happened, tell the world. */
481 ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r,
482 "couldn't set child process attributes: %s", r->filename);
485 argv = (const char * const *)create_argv(r->pool, NULL, NULL, NULL, argv0, r->args);
486 rc = ap_os_create_privileged_process(r, procnew, argv0, argv,
487 (const char * const *)env,
491 if (rc != APR_SUCCESS) {
492 /* Bad things happened. Everyone should have cleaned up. */
493 ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r,
494 "couldn't create child process: %d: %s", rc, r->filename);
501 static void cgid_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp,
502 server_rec *main_server)
508 const char *userdata_key = "cgid_init";
510 apr_get_userdata(&data, userdata_key, main_server->process->pool);
513 apr_set_userdata((const void *)1, userdata_key,
514 apr_null_cleanup, main_server->process->pool);
518 apr_create_pool(&pcgi, p);
520 if ((pid = fork()) < 0) {
521 ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
522 "Couldn't spawn cgid daemon process");
525 cgid_server(main_server);
528 procnew = apr_pcalloc(p, sizeof(*procnew));
530 procnew->err = procnew->in = procnew->out = NULL;
531 apr_note_subprocess(p, procnew, kill_after_timeout);
532 #if APR_HAS_OTHER_CHILD
533 apr_register_other_child(procnew, cgid_maint, NULL, NULL, p);
538 static void *create_cgid_config(apr_pool_t *p, server_rec *s)
540 cgid_server_conf *c =
541 (cgid_server_conf *) apr_pcalloc(p, sizeof(cgid_server_conf));
544 c->logbytes = DEFAULT_LOGBYTES;
545 c->bufbytes = DEFAULT_BUFBYTES;
546 c->sockname = ap_server_root_relative(p, DEFAULT_SOCKET);
550 static void *merge_cgid_config(apr_pool_t *p, void *basev, void *overridesv)
552 cgid_server_conf *base = (cgid_server_conf *) basev, *overrides = (cgid_server_conf *) overridesv;
554 return overrides->logname ? overrides : base;
557 static const char *set_scriptlog(cmd_parms *cmd, void *dummy, const char *arg)
559 server_rec *s = cmd->server;
560 cgid_server_conf *conf =
561 (cgid_server_conf *) ap_get_module_config(s->module_config, &cgid_module);
567 static const char *set_scriptlog_length(cmd_parms *cmd, void *dummy, const char *arg)
569 server_rec *s = cmd->server;
570 cgid_server_conf *conf =
571 (cgid_server_conf *) ap_get_module_config(s->module_config, &cgid_module);
573 conf->logbytes = atol(arg);
577 static const char *set_scriptlog_buffer(cmd_parms *cmd, void *dummy, const char *arg)
579 server_rec *s = cmd->server;
580 cgid_server_conf *conf =
581 (cgid_server_conf *) ap_get_module_config(s->module_config, &cgid_module);
583 conf->bufbytes = atoi(arg);
587 static const char *set_script_socket(cmd_parms *cmd, void *dummy, const char *arg)
589 server_rec *s = cmd->server;
590 cgid_server_conf *conf =
591 (cgid_server_conf *) ap_get_module_config(s->module_config, &cgid_module);
593 conf->sockname = ap_server_root_relative(cmd->pool, arg);
597 static const command_rec cgid_cmds[] =
599 AP_INIT_TAKE1("ScriptLog", set_scriptlog, NULL, RSRC_CONF,
600 "the name of a log for script debugging info"),
601 AP_INIT_TAKE1("ScriptLogLength", set_scriptlog_length, NULL, RSRC_CONF,
602 "the maximum length (in bytes) of the script debug log"),
603 AP_INIT_TAKE1("ScriptLogBuffer", set_scriptlog_buffer, NULL, RSRC_CONF,
604 "the maximum size (in bytes) to record of a POST request"),
605 AP_INIT_TAKE1("Scriptsock", set_script_socket, NULL, RSRC_CONF,
606 "the name of the socket to use for communication with "
611 static int log_scripterror(request_rec *r, cgid_server_conf * conf, int ret,
612 apr_status_t rv, char *error)
614 apr_file_t *f = NULL;
616 char time_str[APR_CTIME_LEN];
617 int log_flags = rv ? APLOG_ERR : APLOG_NOERRNO | APLOG_ERR;
619 ap_log_rerror(APLOG_MARK, log_flags, rv, r,
620 "%s: %s", error, r->filename);
622 if (!conf->logname ||
623 ((stat(ap_server_root_relative(r->pool, conf->logname), &finfo) == 0)
624 && (finfo.st_size > conf->logbytes)) ||
625 (apr_open(&f, ap_server_root_relative(r->pool, conf->logname),
626 APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
630 /* "%% [Wed Jun 19 10:53:21 1996] GET /cgid-bin/printenv HTTP/1.0" */
631 apr_ctime(time_str, apr_now());
632 apr_fprintf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
633 r->args ? "?" : "", r->args ? r->args : "", r->protocol);
634 /* "%% 500 /usr/local/apache/cgid-bin */
635 apr_fprintf(f, "%%%% %d %s\n", ret, r->filename);
637 apr_fprintf(f, "%%error\n%s\n", error);
643 static int log_script(request_rec *r, cgid_server_conf * conf, int ret,
644 char *dbuf, const char *sbuf, apr_file_t *script_in, apr_file_t *script_err)
646 apr_array_header_t *hdrs_arr = apr_table_elts(r->headers_in);
647 apr_table_entry_t *hdrs = (apr_table_entry_t *) hdrs_arr->elts;
648 char argsbuffer[HUGE_STRING_LEN];
649 apr_file_t *f = NULL;
652 char time_str[APR_CTIME_LEN];
654 if (!conf->logname ||
655 ((stat(ap_server_root_relative(r->pool, conf->logname), &finfo) == 0)
656 && (finfo.st_size > conf->logbytes)) ||
657 (apr_open(&f, ap_server_root_relative(r->pool, conf->logname),
658 APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
659 /* Soak up script output */
660 while (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_in) == 0)
663 while (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_err) == 0)
669 /* "%% [Wed Jun 19 10:53:21 1996] GET /cgid-bin/printenv HTTP/1.0" */
670 apr_ctime(time_str, apr_now());
671 apr_fprintf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
672 r->args ? "?" : "", r->args ? r->args : "", r->protocol);
673 /* "%% 500 /usr/local/apache/cgid-bin" */
674 apr_fprintf(f, "%%%% %d %s\n", ret, r->filename);
676 apr_puts("%request\n", f);
677 for (i = 0; i < hdrs_arr->nelts; ++i) {
680 apr_fprintf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
682 if ((r->method_number == M_POST || r->method_number == M_PUT)
684 apr_fprintf(f, "\n%s\n", dbuf);
687 apr_puts("%response\n", f);
688 hdrs_arr = apr_table_elts(r->err_headers_out);
689 hdrs = (apr_table_entry_t *) hdrs_arr->elts;
691 for (i = 0; i < hdrs_arr->nelts; ++i) {
694 apr_fprintf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
698 apr_fprintf(f, "%s\n", sbuf);
700 if (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_in) == 0) {
701 apr_puts("%stdout\n", f);
702 apr_puts(argsbuffer, f);
703 while (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_in) == 0)
704 apr_puts(argsbuffer, f);
709 if (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_err) == 0) {
710 apr_puts("%stderr\n", f);
711 apr_puts(argsbuffer, f);
712 while (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_err) == 0)
713 apr_puts(argsbuffer, f);
718 apr_close(script_in);
720 apr_close(script_err);
729 /****************************************************************
731 * Actual cgid handling...
733 static int cgid_handler(request_rec *r)
735 int retval, nph, dbpos = 0;
736 char *argv0, *dbuf = NULL;
737 apr_bucket_brigade *bb;
739 char argsbuffer[HUGE_STRING_LEN];
741 cgid_server_conf *conf;
745 struct sockaddr_un unix_addr;
746 apr_file_t *tempsock = NULL;
749 if(strcmp(r->handler,CGI_MAGIC_TYPE) && strcmp(r->handler,"cgi-script"))
752 if (r->method_number == M_OPTIONS) {
753 /* 99 out of 100 cgid scripts, this is all they support */
754 r->allowed |= (1 << M_GET);
755 r->allowed |= (1 << M_POST);
759 sconf = r->server->module_config;
760 conf = (cgid_server_conf *) ap_get_module_config(sconf, &cgid_module);
761 is_included = !strcmp(r->protocol, "INCLUDED");
763 if ((argv0 = strrchr(r->filename, '/')) != NULL)
768 nph = !(strncmp(argv0, "nph-", 4));
770 if ((argv0 = strrchr(r->filename, '/')) != NULL)
775 if (!(ap_allow_options(r) & OPT_EXECCGI) && !is_scriptaliased(r))
776 return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
777 "Options ExecCGI is off in this directory");
778 if (nph && is_included)
779 return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
780 "attempt to include NPH CGI script");
782 #if defined(OS2) || defined(WIN32)
783 #error mod_cgid does not work on this platform. If you teach it to, look
784 #error at mod_cgi.c for required code in this path.
786 if (r->finfo.protection == 0)
787 return log_scripterror(r, conf, HTTP_NOT_FOUND, 0,
788 "script not found or unable to stat");
790 if (r->finfo.filetype == APR_DIR)
791 return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
792 "attempt to invoke directory as script");
794 if (!ap_suexec_enabled) {
795 if (!ap_can_exec(&r->finfo))
796 return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
797 "file permissions deny server execution");
800 ap_add_common_vars(r);
802 env = ap_create_environment(r->pool, r->subprocess_env);
804 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
805 return log_scripterror(r, conf, HTTP_INTERNAL_SERVER_ERROR, errno,
806 "unable to create socket to cgi daemon");
808 memset(&unix_addr, 0, sizeof(unix_addr));
809 unix_addr.sun_family = AF_UNIX;
810 strcpy(unix_addr.sun_path, conf->sockname);
812 if (connect(sd, (struct sockaddr *)&unix_addr, sizeof(unix_addr)) < 0) {
813 return log_scripterror(r, conf, HTTP_INTERNAL_SERVER_ERROR, errno,
814 "unable to connect to cgi daemon");
817 send_req(sd, r, argv0, env);
819 /* We are putting the tempsock variable into a file so that we can use
820 * a pipe bucket to send the data to the client.
822 apr_put_os_file(&tempsock, &sd, r->pool);
824 if ((retval = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)))
827 if ((argv0 = strrchr(r->filename, '/')) != NULL)
832 /* Transfer any put/post args, CERN style...
833 * Note that we already ignore SIGPIPE in the core server.
836 if (ap_should_client_block(r)) {
837 int dbsize, len_read;
840 dbuf = apr_pcalloc(r->pool, conf->bufbytes + 1);
845 ap_get_client_block(r, argsbuffer, HUGE_STRING_LEN)) > 0) {
847 if ((dbpos + len_read) > conf->bufbytes) {
848 dbsize = conf->bufbytes - dbpos;
853 memcpy(dbuf + dbpos, argsbuffer, dbsize);
857 apr_write(tempsock, argsbuffer, &nbytes);
858 if (nbytes < len_read) {
859 /* silly script stopped reading, soak up remaining message */
860 while (ap_get_client_block(r, argsbuffer, HUGE_STRING_LEN) > 0) {
866 shutdown(sd, 1); /* done writing; force EOF on child's stdin */
869 /* Handle script return... */
871 const char *location;
872 char sbuf[MAX_STRING_LEN];
875 if ((ret = ap_scan_script_header_err(r, tempsock, sbuf))) {
876 return log_script(r, conf, ret, dbuf, sbuf, tempsock, NULL);
879 location = apr_table_get(r->headers_out, "Location");
881 if (location && location[0] == '/' && r->status == 200) {
883 /* Soak up all the script output */
884 while (apr_fgets(argsbuffer, HUGE_STRING_LEN, tempsock) > 0) {
887 /* This redirect needs to be a GET no matter what the original
890 r->method = apr_pstrdup(r->pool, "GET");
891 r->method_number = M_GET;
893 /* We already read the message body (if any), so don't allow
894 * the redirected request to think it has one. We can ignore
895 * Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
897 apr_table_unset(r->headers_in, "Content-Length");
899 ap_internal_redirect_handler(location, r);
902 else if (location && r->status == 200) {
903 /* XX Note that if a script wants to produce its own Redirect
904 * body, it now has to explicitly *say* "Status: 302"
906 return HTTP_MOVED_TEMPORARILY;
909 ap_send_http_header(r);
910 if (!r->header_only) {
911 bb = apr_brigade_create(r->pool);
912 b = apr_bucket_create_pipe(tempsock);
913 APR_BRIGADE_INSERT_TAIL(bb, b);
914 b = apr_bucket_create_eos();
915 APR_BRIGADE_INSERT_TAIL(bb, b);
916 ap_pass_brigade(r->output_filters, bb);
921 bb = apr_brigade_create(r->pool);
922 b = apr_bucket_create_pipe(tempsock);
923 APR_BRIGADE_INSERT_TAIL(bb, b);
924 b = apr_bucket_create_eos();
925 APR_BRIGADE_INSERT_TAIL(bb, b);
926 ap_pass_brigade(r->output_filters, bb);
931 return OK; /* NOT r->status, even if it has changed. */
934 static void register_hook(apr_pool_t *p)
936 ap_hook_post_config(cgid_init, NULL, NULL, APR_HOOK_MIDDLE);
937 ap_hook_handler(cgid_handler, NULL, NULL, APR_HOOK_MIDDLE);
940 module AP_MODULE_DECLARE_DATA cgid_module = {
941 STANDARD20_MODULE_STUFF,
942 NULL, /* dir config creater */
943 NULL, /* dir merger --- default is to override */
944 create_cgid_config, /* server config */
945 merge_cgid_config, /* merge server config */
946 cgid_cmds, /* command table */
947 register_hook /* register_handlers */