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 "ap_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 int show_errno, char *error)
614 apr_file_t *f = NULL;
616 char time_str[APR_CTIME_LEN];
618 ap_log_rerror(APLOG_MARK, show_errno|APLOG_ERR, errno, r,
619 "%s: %s", error, r->filename);
621 if (!conf->logname ||
622 ((stat(ap_server_root_relative(r->pool, conf->logname), &finfo) == 0)
623 && (finfo.st_size > conf->logbytes)) ||
624 (apr_open(&f, ap_server_root_relative(r->pool, conf->logname),
625 APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
629 /* "%% [Wed Jun 19 10:53:21 1996] GET /cgid-bin/printenv HTTP/1.0" */
630 apr_ctime(time_str, apr_now());
631 apr_fprintf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
632 r->args ? "?" : "", r->args ? r->args : "", r->protocol);
633 /* "%% 500 /usr/local/apache/cgid-bin */
634 apr_fprintf(f, "%%%% %d %s\n", ret, r->filename);
636 apr_fprintf(f, "%%error\n%s\n", error);
642 static int log_script(request_rec *r, cgid_server_conf * conf, int ret,
643 char *dbuf, const char *sbuf, apr_file_t *script_in, apr_file_t *script_err)
645 apr_array_header_t *hdrs_arr = apr_table_elts(r->headers_in);
646 apr_table_entry_t *hdrs = (apr_table_entry_t *) hdrs_arr->elts;
647 char argsbuffer[HUGE_STRING_LEN];
648 apr_file_t *f = NULL;
651 char time_str[APR_CTIME_LEN];
653 if (!conf->logname ||
654 ((stat(ap_server_root_relative(r->pool, conf->logname), &finfo) == 0)
655 && (finfo.st_size > conf->logbytes)) ||
656 (apr_open(&f, ap_server_root_relative(r->pool, conf->logname),
657 APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
658 /* Soak up script output */
659 while (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_in) == 0)
662 while (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_err) == 0)
668 /* "%% [Wed Jun 19 10:53:21 1996] GET /cgid-bin/printenv HTTP/1.0" */
669 apr_ctime(time_str, apr_now());
670 apr_fprintf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
671 r->args ? "?" : "", r->args ? r->args : "", r->protocol);
672 /* "%% 500 /usr/local/apache/cgid-bin" */
673 apr_fprintf(f, "%%%% %d %s\n", ret, r->filename);
675 apr_puts("%request\n", f);
676 for (i = 0; i < hdrs_arr->nelts; ++i) {
679 apr_fprintf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
681 if ((r->method_number == M_POST || r->method_number == M_PUT)
683 apr_fprintf(f, "\n%s\n", dbuf);
686 apr_puts("%response\n", f);
687 hdrs_arr = apr_table_elts(r->err_headers_out);
688 hdrs = (apr_table_entry_t *) hdrs_arr->elts;
690 for (i = 0; i < hdrs_arr->nelts; ++i) {
693 apr_fprintf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
697 apr_fprintf(f, "%s\n", sbuf);
699 if (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_in) == 0) {
700 apr_puts("%stdout\n", f);
701 apr_puts(argsbuffer, f);
702 while (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_in) == 0)
703 apr_puts(argsbuffer, f);
708 if (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_err) == 0) {
709 apr_puts("%stderr\n", f);
710 apr_puts(argsbuffer, f);
711 while (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_err) == 0)
712 apr_puts(argsbuffer, f);
717 apr_close(script_in);
719 apr_close(script_err);
728 /****************************************************************
730 * Actual cgid handling...
732 static int cgid_handler(request_rec *r)
734 int retval, nph, dbpos = 0;
735 char *argv0, *dbuf = NULL;
736 ap_bucket_brigade *bb;
738 char argsbuffer[HUGE_STRING_LEN];
739 void *sconf = r->server->module_config;
740 cgid_server_conf *conf = (cgid_server_conf *) ap_get_module_config(sconf, &cgid_module);
741 int is_included = !strcmp(r->protocol, "INCLUDED");
744 struct sockaddr_un unix_addr;
745 apr_file_t *tempsock = NULL;
748 if (r->method_number == M_OPTIONS) {
749 /* 99 out of 100 cgid scripts, this is all they support */
750 r->allowed |= (1 << M_GET);
751 r->allowed |= (1 << M_POST);
755 if ((argv0 = strrchr(r->filename, '/')) != NULL)
760 nph = !(strncmp(argv0, "nph-", 4));
762 if ((argv0 = strrchr(r->filename, '/')) != NULL)
767 if (!(ap_allow_options(r) & OPT_EXECCGI) && !is_scriptaliased(r))
768 return log_scripterror(r, conf, HTTP_FORBIDDEN, APLOG_NOERRNO,
769 "Options ExecCGI is off in this directory");
770 if (nph && is_included)
771 return log_scripterror(r, conf, HTTP_FORBIDDEN, APLOG_NOERRNO,
772 "attempt to include NPH CGI script");
774 #if defined(OS2) || defined(WIN32)
775 /* Allow for cgid files without the .EXE extension on them under OS/2 */
776 if (r->finfo.st_mode == 0) {
780 newfile = apr_pstrcat(r->pool, r->filename, ".EXE", NULL);
782 if ((stat(newfile, &statbuf) != 0) || (!S_ISREG(statbuf.st_mode))) {
783 return log_scripterror(r, conf, HTTP_NOT_FOUND, 0,
784 "script not found or unable to stat");
786 r->filename = newfile;
790 if (r->finfo.protection == 0)
791 return log_scripterror(r, conf, HTTP_NOT_FOUND, APLOG_NOERRNO,
792 "script not found or unable to stat");
794 if (r->finfo.filetype == APR_DIR)
795 return log_scripterror(r, conf, HTTP_FORBIDDEN, APLOG_NOERRNO,
796 "attempt to invoke directory as script");
798 if (!ap_suexec_enabled) {
799 if (!ap_can_exec(&r->finfo))
800 return log_scripterror(r, conf, HTTP_FORBIDDEN, APLOG_NOERRNO,
801 "file permissions deny server execution");
804 ap_add_common_vars(r);
806 env = ap_create_environment(r->pool, r->subprocess_env);
808 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
809 return log_scripterror(r, conf, HTTP_INTERNAL_SERVER_ERROR, 0,
810 "unable to create socket to cgi daemon");
812 memset(&unix_addr, 0, sizeof(unix_addr));
813 unix_addr.sun_family = AF_UNIX;
814 strcpy(unix_addr.sun_path, conf->sockname);
816 if (connect(sd, (struct sockaddr *)&unix_addr, sizeof(unix_addr)) < 0) {
817 return log_scripterror(r, conf, HTTP_INTERNAL_SERVER_ERROR, 0,
818 "unable to connect to cgi daemon");
821 send_req(sd, r, argv0, env);
823 /* We are putting the tempsock variable into a file so that we can use
824 * a pipe bucket to send the data to the client.
826 apr_put_os_file(&tempsock, &sd, r->pool);
828 if ((retval = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)))
831 if ((argv0 = strrchr(r->filename, '/')) != NULL)
836 /* Transfer any put/post args, CERN style...
837 * Note that we already ignore SIGPIPE in the core server.
840 if (ap_should_client_block(r)) {
841 int dbsize, len_read;
844 dbuf = apr_pcalloc(r->pool, conf->bufbytes + 1);
849 ap_get_client_block(r, argsbuffer, HUGE_STRING_LEN)) > 0) {
851 if ((dbpos + len_read) > conf->bufbytes) {
852 dbsize = conf->bufbytes - dbpos;
857 memcpy(dbuf + dbpos, argsbuffer, dbsize);
861 apr_write(tempsock, argsbuffer, &nbytes);
862 if (nbytes < len_read) {
863 /* silly script stopped reading, soak up remaining message */
864 while (ap_get_client_block(r, argsbuffer, HUGE_STRING_LEN) > 0) {
870 shutdown(sd, 1); /* done writing; force EOF on child's stdin */
873 /* Handle script return... */
875 const char *location;
876 char sbuf[MAX_STRING_LEN];
879 if ((ret = ap_scan_script_header_err(r, tempsock, sbuf))) {
880 return log_script(r, conf, ret, dbuf, sbuf, tempsock, NULL);
883 location = apr_table_get(r->headers_out, "Location");
885 if (location && location[0] == '/' && r->status == 200) {
887 /* Soak up all the script output */
888 while (apr_fgets(argsbuffer, HUGE_STRING_LEN, tempsock) > 0) {
891 /* This redirect needs to be a GET no matter what the original
894 r->method = apr_pstrdup(r->pool, "GET");
895 r->method_number = M_GET;
897 /* We already read the message body (if any), so don't allow
898 * the redirected request to think it has one. We can ignore
899 * Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
901 apr_table_unset(r->headers_in, "Content-Length");
903 ap_internal_redirect_handler(location, r);
906 else if (location && r->status == 200) {
907 /* XX Note that if a script wants to produce its own Redirect
908 * body, it now has to explicitly *say* "Status: 302"
910 return HTTP_MOVED_TEMPORARILY;
913 ap_send_http_header(r);
914 if (!r->header_only) {
915 bb = ap_brigade_create(r->pool);
916 b = ap_bucket_create_pipe(tempsock);
917 AP_BRIGADE_INSERT_TAIL(bb, b);
918 b = ap_bucket_create_eos();
919 AP_BRIGADE_INSERT_TAIL(bb, b);
920 ap_pass_brigade(r->output_filters, bb);
925 bb = ap_brigade_create(r->pool);
926 b = ap_bucket_create_pipe(tempsock);
927 AP_BRIGADE_INSERT_TAIL(bb, b);
928 b = ap_bucket_create_eos();
929 AP_BRIGADE_INSERT_TAIL(bb, b);
930 ap_pass_brigade(r->output_filters, bb);
935 return OK; /* NOT r->status, even if it has changed. */
938 static const handler_rec cgid_handlers[] =
940 {CGI_MAGIC_TYPE, cgid_handler},
941 {"cgi-script", cgid_handler},
945 static void register_hook(void)
947 ap_hook_post_config(cgid_init, NULL, NULL, AP_HOOK_MIDDLE);
950 module AP_MODULE_DECLARE_DATA cgid_module = {
951 STANDARD20_MODULE_STUFF,
952 NULL, /* dir config creater */
953 NULL, /* dir merger --- default is to override */
954 create_cgid_config, /* server config */
955 merge_cgid_config, /* merge server config */
956 cgid_cmds, /* command table */
957 cgid_handlers, /* handlers */
958 register_hook /* register_handlers */