1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2002 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
73 #include "apr_strings.h"
74 #include "apr_thread_proc.h" /* for RLIMIT stuff */
75 #include "apr_optional.h"
76 #include "apr_buckets.h"
79 #define APR_WANT_STRFUNC
84 #include "util_filter.h"
85 #include "ap_config.h"
87 #include "http_config.h"
88 #include "http_request.h"
89 #include "http_core.h"
90 #include "http_protocol.h"
91 #include "http_main.h"
93 #include "util_script.h"
96 #include "../filters/mod_include.h"
99 module AP_MODULE_DECLARE_DATA cgi_module;
101 static APR_OPTIONAL_FN_TYPE(ap_register_include_handler) *cgi_pfn_reg_with_ssi;
102 static APR_OPTIONAL_FN_TYPE(ap_ssi_get_tag_and_value) *cgi_pfn_gtv;
103 static APR_OPTIONAL_FN_TYPE(ap_ssi_parse_string) *cgi_pfn_ps;
104 static APR_OPTIONAL_FN_TYPE(ap_cgi_build_command) *cgi_build_command;
106 typedef enum {RUN_AS_SSI, RUN_AS_CGI} prog_types;
110 apr_int32_t out_pipe;
111 apr_int32_t err_pipe;
112 apr_cmdtype_e cmd_type;
113 prog_types prog_type;
114 apr_bucket_brigade **bb;
119 /* Read and discard the data in the brigade produced by a CGI script */
120 static void discard_script_output(apr_bucket_brigade *bb);
122 /* KLUDGE --- for back-combatibility, we don't have to check ExecCGI
123 * in ScriptAliased directories, which means we need to know if this
124 * request came through ScriptAlias or not... so the Alias module
125 * leaves a note for us.
128 static int is_scriptaliased(request_rec *r)
130 const char *t = apr_table_get(r->notes, "alias-forced-type");
131 return t && (!strcasecmp(t, "cgi-script"));
134 /* Configuration stuff */
136 #define DEFAULT_LOGBYTES 10385760
137 #define DEFAULT_BUFBYTES 1024
145 static void *create_cgi_config(apr_pool_t *p, server_rec *s)
148 (cgi_server_conf *) apr_pcalloc(p, sizeof(cgi_server_conf));
151 c->logbytes = DEFAULT_LOGBYTES;
152 c->bufbytes = DEFAULT_BUFBYTES;
157 static void *merge_cgi_config(apr_pool_t *p, void *basev, void *overridesv)
159 cgi_server_conf *base = (cgi_server_conf *) basev,
160 *overrides = (cgi_server_conf *) overridesv;
162 return overrides->logname ? overrides : base;
165 static const char *set_scriptlog(cmd_parms *cmd, void *dummy, const char *arg)
167 server_rec *s = cmd->server;
168 cgi_server_conf *conf = ap_get_module_config(s->module_config,
171 conf->logname = ap_server_root_relative(cmd->pool, arg);
173 if (!conf->logname) {
174 return apr_pstrcat(cmd->pool, "Invalid ScriptLog path ",
181 static const char *set_scriptlog_length(cmd_parms *cmd, void *dummy,
184 server_rec *s = cmd->server;
185 cgi_server_conf *conf = ap_get_module_config(s->module_config,
188 conf->logbytes = atol(arg);
192 static const char *set_scriptlog_buffer(cmd_parms *cmd, void *dummy,
195 server_rec *s = cmd->server;
196 cgi_server_conf *conf = ap_get_module_config(s->module_config,
199 conf->bufbytes = atoi(arg);
203 static const command_rec cgi_cmds[] =
205 AP_INIT_TAKE1("ScriptLog", set_scriptlog, NULL, RSRC_CONF,
206 "the name of a log for script debugging info"),
207 AP_INIT_TAKE1("ScriptLogLength", set_scriptlog_length, NULL, RSRC_CONF,
208 "the maximum length (in bytes) of the script debug log"),
209 AP_INIT_TAKE1("ScriptLogBuffer", set_scriptlog_buffer, NULL, RSRC_CONF,
210 "the maximum size (in bytes) to record of a POST request"),
214 static int log_scripterror(request_rec *r, cgi_server_conf * conf, int ret,
215 apr_status_t rv, char *error)
217 apr_file_t *f = NULL;
219 char time_str[APR_CTIME_LEN];
220 int log_flags = rv ? APLOG_ERR : APLOG_ERR;
222 ap_log_rerror(APLOG_MARK, log_flags, rv, r,
223 "%s: %s", error, r->filename);
225 /* XXX Very expensive mainline case! Open, then getfileinfo! */
226 if (!conf->logname ||
227 ((apr_stat(&finfo, conf->logname,
228 APR_FINFO_SIZE, r->pool) == APR_SUCCESS) &&
229 (finfo.size > conf->logbytes)) ||
230 (apr_file_open(&f, conf->logname,
231 APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT,
232 r->pool) != APR_SUCCESS)) {
236 /* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
237 apr_ctime(time_str, apr_time_now());
238 apr_file_printf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
239 r->args ? "?" : "", r->args ? r->args : "", r->protocol);
240 /* "%% 500 /usr/local/apache/cgi-bin */
241 apr_file_printf(f, "%%%% %d %s\n", ret, r->filename);
243 apr_file_printf(f, "%%error\n%s\n", error);
249 /* Soak up stderr from a script and redirect it to the error log.
251 static void log_script_err(request_rec *r, apr_file_t *script_err)
253 char argsbuffer[HUGE_STRING_LEN];
256 while (apr_file_gets(argsbuffer, HUGE_STRING_LEN,
257 script_err) == APR_SUCCESS) {
258 newline = strchr(argsbuffer, '\n');
262 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
267 static int log_script(request_rec *r, cgi_server_conf * conf, int ret,
268 char *dbuf, const char *sbuf, apr_bucket_brigade *bb,
269 apr_file_t *script_err)
271 const apr_array_header_t *hdrs_arr = apr_table_elts(r->headers_in);
272 const apr_table_entry_t *hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
273 char argsbuffer[HUGE_STRING_LEN];
274 apr_file_t *f = NULL;
282 char time_str[APR_CTIME_LEN];
284 /* XXX Very expensive mainline case! Open, then getfileinfo! */
285 if (!conf->logname ||
286 ((apr_stat(&finfo, conf->logname,
287 APR_FINFO_SIZE, r->pool) == APR_SUCCESS) &&
288 (finfo.size > conf->logbytes)) ||
289 (apr_file_open(&f, conf->logname,
290 APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT,
291 r->pool) != APR_SUCCESS)) {
292 /* Soak up script output */
293 discard_script_output(bb);
294 log_script_err(r, script_err);
298 /* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
299 apr_ctime(time_str, apr_time_now());
300 apr_file_printf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
301 r->args ? "?" : "", r->args ? r->args : "", r->protocol);
302 /* "%% 500 /usr/local/apache/cgi-bin" */
303 apr_file_printf(f, "%%%% %d %s\n", ret, r->filename);
305 apr_file_puts("%request\n", f);
306 for (i = 0; i < hdrs_arr->nelts; ++i) {
309 apr_file_printf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
311 if ((r->method_number == M_POST || r->method_number == M_PUT) &&
313 apr_file_printf(f, "\n%s\n", dbuf);
316 apr_file_puts("%response\n", f);
317 hdrs_arr = apr_table_elts(r->err_headers_out);
318 hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
320 for (i = 0; i < hdrs_arr->nelts; ++i) {
323 apr_file_printf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
327 apr_file_printf(f, "%s\n", sbuf);
330 APR_BRIGADE_FOREACH(e, bb) {
331 if (APR_BUCKET_IS_EOS(e)) {
334 rv = apr_bucket_read(e, &buf, &len, APR_BLOCK_READ);
335 if (!APR_STATUS_IS_SUCCESS(rv) || (len == 0)) {
339 apr_file_puts("%stdout\n", f);
342 apr_file_write(f, buf, &len);
343 apr_file_puts("\n", f);
346 if (apr_file_gets(argsbuffer, HUGE_STRING_LEN, script_err) == APR_SUCCESS) {
347 apr_file_puts("%stderr\n", f);
348 apr_file_puts(argsbuffer, f);
349 while (apr_file_gets(argsbuffer, HUGE_STRING_LEN,
350 script_err) == APR_SUCCESS) {
351 apr_file_puts(argsbuffer, f);
353 apr_file_puts("\n", f);
356 apr_brigade_destroy(bb);
357 apr_file_close(script_err);
364 /* This is the special environment used for running the "exec cmd="
365 * variety of SSI directives.
367 static void add_ssi_vars(request_rec *r, ap_filter_t *next)
369 apr_table_t *e = r->subprocess_env;
371 if (r->path_info && r->path_info[0] != '\0') {
374 apr_table_setn(e, "PATH_INFO", ap_escape_shell_cmd(r->pool,
377 pa_req = ap_sub_req_lookup_uri(ap_escape_uri(r->pool, r->path_info),
379 if (pa_req->filename) {
380 apr_table_setn(e, "PATH_TRANSLATED",
381 apr_pstrcat(r->pool, pa_req->filename,
382 pa_req->path_info, NULL));
384 ap_destroy_sub_req(pa_req);
388 char *arg_copy = apr_pstrdup(r->pool, r->args);
390 apr_table_setn(e, "QUERY_STRING", r->args);
391 ap_unescape_url(arg_copy);
392 apr_table_setn(e, "QUERY_STRING_UNESCAPED",
393 ap_escape_shell_cmd(r->pool, arg_copy));
397 static apr_status_t run_cgi_child(apr_file_t **script_out,
398 apr_file_t **script_in,
399 apr_file_t **script_err,
401 const char * const argv[],
406 const char * const *env;
407 apr_procattr_t *procattr;
409 apr_status_t rc = APR_SUCCESS;
411 #if defined(RLIMIT_CPU) || defined(RLIMIT_NPROC) || \
412 defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined (RLIMIT_AS)
414 core_dir_config *conf = ap_get_module_config(r->per_dir_config,
421 /* Under OS/2 need to use device con. */
422 FILE *dbg = fopen("con", "w");
424 FILE *dbg = fopen("/dev/tty", "w");
429 RAISE_SIGSTOP(CGI_CHILD);
431 fprintf(dbg, "Attempting to exec %s as CGI child (argv0 = %s)\n",
432 r->filename, argv[0]);
435 if (e_info->prog_type == RUN_AS_CGI) {
438 else /* SSIs want a controlled environment and a special path. */
440 add_ssi_vars(r, e_info->next);
442 env = (const char * const *)ap_create_environment(p, r->subprocess_env);
445 fprintf(dbg, "Environment: \n");
446 for (i = 0; env[i]; ++i)
447 fprintf(dbg, "'%s'\n", env[i]);
450 /* Transmute ourselves into the script.
451 * NB only ISINDEX scripts get decoded arguments.
453 if (((rc = apr_procattr_create(&procattr, p)) != APR_SUCCESS) ||
454 ((rc = apr_procattr_io_set(procattr,
457 e_info->err_pipe)) != APR_SUCCESS) ||
458 ((rc = apr_procattr_dir_set(procattr,
459 ap_make_dirstr_parent(r->pool,
460 r->filename))) != APR_SUCCESS) ||
462 ((rc = apr_procattr_limit_set(procattr, APR_LIMIT_CPU,
463 conf->limit_cpu)) != APR_SUCCESS) ||
465 #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
466 ((rc = apr_procattr_limit_set(procattr, APR_LIMIT_MEM,
467 conf->limit_mem)) != APR_SUCCESS) ||
470 ((rc = apr_procattr_limit_set(procattr, APR_LIMIT_NPROC,
471 conf->limit_nproc)) != APR_SUCCESS) ||
473 ((rc = apr_procattr_cmdtype_set(procattr,
474 e_info->cmd_type)) != APR_SUCCESS)) {
475 /* Something bad happened, tell the world. */
476 ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r,
477 "couldn't set child process attributes: %s", r->filename);
480 procnew = apr_pcalloc(p, sizeof(*procnew));
481 if (e_info->prog_type == RUN_AS_SSI) {
482 SPLIT_AND_PASS_PRETAG_BUCKETS(*(e_info->bb), e_info->ctx,
484 if (rc != APR_SUCCESS) {
489 rc = ap_os_create_privileged_process(r, procnew, command, argv, env,
492 if (rc != APR_SUCCESS) {
493 /* Bad things happened. Everyone should have cleaned up. */
494 ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r,
495 "couldn't create child process: %d: %s", rc,
499 apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT);
501 *script_in = procnew->out;
504 apr_file_pipe_timeout_set(*script_in, (int)(r->server->timeout *
507 if (e_info->prog_type == RUN_AS_CGI) {
508 *script_out = procnew->in;
511 apr_file_pipe_timeout_set(*script_out,
512 (int)(r->server->timeout *
515 *script_err = procnew->err;
518 apr_file_pipe_timeout_set(*script_err,
519 (int)(r->server->timeout *
531 static apr_status_t default_build_command(const char **cmd, const char ***argv,
532 request_rec *r, apr_pool_t *p,
533 int process_cgi, apr_cmdtype_e * type)
535 int numwords, x, idx;
537 const char *args = NULL;
540 /* Allow suexec's "/" check to succeed */
541 const char *argv0 = strrchr(r->filename, '/');
548 /* Do not process r->args if they contain an '=' assignment
550 if (r->args && r->args[0] && !ap_strchr_c(r->args, '=')) {
559 /* count the number of keywords */
560 for (x = 0, numwords = 2; args[x]; x++) {
561 if (args[x] == '+') {
566 /* Everything is - 1 to account for the first parameter
567 * which is the program name.
569 if (numwords > APACHE_ARG_MAX - 1) {
570 numwords = APACHE_ARG_MAX - 1; /* Truncate args to prevent overrun */
572 *argv = apr_palloc(p, (numwords + 2) * sizeof(char *));
574 for (x = 1, idx = 1; x < numwords; x++) {
575 w = ap_getword_nulls(p, &args, '+');
577 (*argv)[idx++] = ap_escape_shell_cmd(p, w);
584 static void discard_script_output(apr_bucket_brigade *bb)
590 APR_BRIGADE_FOREACH(e, bb) {
591 if (APR_BUCKET_IS_EOS(e)) {
594 rv = apr_bucket_read(e, &buf, &len, APR_BLOCK_READ);
595 if (!APR_STATUS_IS_SUCCESS(rv)) {
601 static int cgi_handler(request_rec *r)
603 int retval, nph, dbpos = 0;
608 apr_file_t *script_out = NULL, *script_in = NULL, *script_err = NULL;
609 apr_bucket_brigade *bb;
611 char argsbuffer[HUGE_STRING_LEN];
614 cgi_server_conf *conf;
618 if(strcmp(r->handler,CGI_MAGIC_TYPE) && strcmp(r->handler,"cgi-script"))
621 is_included = !strcmp(r->protocol, "INCLUDED");
623 p = r->main ? r->main->pool : r->pool;
625 if (r->method_number == M_OPTIONS) {
626 /* 99 out of 100 CGI scripts, this is all they support */
627 r->allowed |= (AP_METHOD_BIT << M_GET);
628 r->allowed |= (AP_METHOD_BIT << M_POST);
632 argv0 = apr_filename_of_pathname(r->filename);
633 nph = !(strncmp(argv0, "nph-", 4));
634 conf = ap_get_module_config(r->server->module_config, &cgi_module);
636 if (!(ap_allow_options(r) & OPT_EXECCGI) && !is_scriptaliased(r))
637 return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
638 "Options ExecCGI is off in this directory");
639 if (nph && is_included)
640 return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
641 "attempt to include NPH CGI script");
643 if (r->finfo.filetype == 0)
644 return log_scripterror(r, conf, HTTP_NOT_FOUND, 0,
645 "script not found or unable to stat");
646 if (r->finfo.filetype == APR_DIR)
647 return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
648 "attempt to invoke directory as script");
650 if ((r->used_path_info == AP_REQ_REJECT_PATH_INFO) &&
651 r->path_info && *r->path_info)
653 /* default to accept */
654 return log_scripterror(r, conf, HTTP_NOT_FOUND, 0,
655 "AcceptPathInfo off disallows user's path");
658 if (!ap_suexec_enabled) {
659 if (!ap_can_exec(&r->finfo))
660 return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
661 "file permissions deny server execution");
665 if ((retval = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)))
668 ap_add_common_vars(r);
670 e_info.cmd_type = APR_PROGRAM;
671 e_info.in_pipe = APR_CHILD_BLOCK;
672 e_info.out_pipe = APR_CHILD_BLOCK;
673 e_info.err_pipe = APR_CHILD_BLOCK;
674 e_info.prog_type = RUN_AS_CGI;
679 /* build the command line */
680 if ((rv = cgi_build_command(&command, &argv, r, p, 1, &e_info.cmd_type))
682 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
683 "don't know how to spawn child process: %s",
685 return HTTP_INTERNAL_SERVER_ERROR;
688 /* run the script in its own process */
689 if ((rv = run_cgi_child(&script_out, &script_in, &script_err,
690 command, argv, r, p, &e_info)) != APR_SUCCESS) {
691 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
692 "couldn't spawn child process: %s", r->filename);
693 return HTTP_INTERNAL_SERVER_ERROR;
696 /* Transfer any put/post args, CERN style...
697 * Note that we already ignore SIGPIPE in the core server.
699 if (ap_should_client_block(r)) {
700 int len_read, dbsize;
701 apr_size_t bytes_written;
705 dbuf = apr_pcalloc(r->pool, conf->bufbytes + 1);
710 ap_get_client_block(r, argsbuffer, HUGE_STRING_LEN)) > 0) {
712 if ((dbpos + len_read) > conf->bufbytes) {
713 dbsize = conf->bufbytes - dbpos;
718 memcpy(dbuf + dbpos, argsbuffer, dbsize);
721 /* Keep writing data to the child until done or too much time
722 * elapses with no progress or an error occurs.
724 rv = apr_file_write_full(script_out, argsbuffer, len_read,
726 if (rv != APR_SUCCESS) {
727 /* silly script stopped reading, soak up remaining message */
728 while (ap_get_client_block(r, argsbuffer,
729 HUGE_STRING_LEN) > 0) {
735 apr_file_flush(script_out);
738 apr_file_close(script_out);
740 /* Handle script return... */
741 if (script_in && !nph) {
742 conn_rec *c = r->connection;
743 const char *location;
744 char sbuf[MAX_STRING_LEN];
747 bb = apr_brigade_create(r->pool, c->bucket_alloc);
748 b = apr_bucket_pipe_create(script_in, c->bucket_alloc);
749 APR_BRIGADE_INSERT_TAIL(bb, b);
750 b = apr_bucket_eos_create(c->bucket_alloc);
751 APR_BRIGADE_INSERT_TAIL(bb, b);
753 if ((ret = ap_scan_script_header_err_brigade(r, bb, sbuf))) {
754 return log_script(r, conf, ret, dbuf, sbuf, bb, script_err);
757 location = apr_table_get(r->headers_out, "Location");
759 if (location && location[0] == '/' && r->status == 200) {
760 discard_script_output(bb);
761 apr_brigade_destroy(bb);
762 log_script_err(r, script_err);
763 /* This redirect needs to be a GET no matter what the original
766 r->method = apr_pstrdup(r->pool, "GET");
767 r->method_number = M_GET;
769 /* We already read the message body (if any), so don't allow
770 * the redirected request to think it has one. We can ignore
771 * Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
773 apr_table_unset(r->headers_in, "Content-Length");
775 ap_internal_redirect_handler(location, r);
778 else if (location && r->status == 200) {
779 /* XX Note that if a script wants to produce its own Redirect
780 * body, it now has to explicitly *say* "Status: 302"
782 discard_script_output(bb);
783 apr_brigade_destroy(bb);
784 return HTTP_MOVED_TEMPORARILY;
787 if (!r->header_only) {
788 ap_pass_brigade(r->output_filters, bb);
791 log_script_err(r, script_err);
792 apr_file_close(script_err);
795 if (script_in && nph) {
796 conn_rec *c = r->connection;
797 struct ap_filter_t *cur;
799 /* get rid of all filters up through protocol... since we
800 * haven't parsed off the headers, there is no way they can
804 cur = r->proto_output_filters;
805 while (cur && cur->frec->ftype < AP_FTYPE_CONNECTION) {
808 r->output_filters = r->proto_output_filters = cur;
810 bb = apr_brigade_create(r->pool, c->bucket_alloc);
811 b = apr_bucket_pipe_create(script_in, c->bucket_alloc);
812 APR_BRIGADE_INSERT_TAIL(bb, b);
813 b = apr_bucket_eos_create(c->bucket_alloc);
814 APR_BRIGADE_INSERT_TAIL(bb, b);
815 ap_pass_brigade(r->output_filters, bb);
818 return OK; /* NOT r->status, even if it has changed. */
821 /*============================================================================
822 *============================================================================
823 * This is the beginning of the cgi filter code moved from mod_include. This
824 * is the code required to handle the "exec" SSI directive.
825 *============================================================================
826 *============================================================================*/
827 static int include_cgi(char *s, request_rec *r, ap_filter_t *next,
828 apr_bucket *head_ptr, apr_bucket **inserted_head)
830 request_rec *rr = ap_sub_req_lookup_uri(s, r, next);
832 apr_bucket *tmp_buck, *tmp2_buck;
834 if (rr->status != HTTP_OK) {
835 ap_destroy_sub_req(rr);
839 /* No hardwired path info or query allowed */
841 if ((rr->path_info && rr->path_info[0]) || rr->args) {
842 ap_destroy_sub_req(rr);
845 if (rr->finfo.filetype != APR_REG) {
846 ap_destroy_sub_req(rr);
850 /* Script gets parameters of the *document*, for back compatibility */
852 rr->path_info = r->path_info; /* hard to get right; see mod_cgi.c */
855 /* Force sub_req to be treated as a CGI request, even if ordinary
856 * typing rules would have called it something else.
859 ap_set_content_type(rr, CGI_MAGIC_TYPE);
863 rr_status = ap_run_sub_req(rr);
864 if (ap_is_HTTP_REDIRECT(rr_status)) {
866 const char *location = apr_table_get(rr->headers_out, "Location");
867 conn_rec *c = r->connection;
869 location = ap_escape_html(rr->pool, location);
870 len_loc = strlen(location);
872 /* XXX: if most of this stuff is going to get copied anyway,
873 * it'd be more efficient to pstrcat it into a single pool buffer
874 * and a single pool bucket */
876 tmp_buck = apr_bucket_immortal_create("<A HREF=\"",
877 sizeof("<A HREF=\"") - 1,
879 APR_BUCKET_INSERT_BEFORE(head_ptr, tmp_buck);
880 tmp2_buck = apr_bucket_heap_create(location, len_loc, NULL,
882 APR_BUCKET_INSERT_BEFORE(head_ptr, tmp2_buck);
883 tmp2_buck = apr_bucket_immortal_create("\">", sizeof("\">") - 1,
885 APR_BUCKET_INSERT_BEFORE(head_ptr, tmp2_buck);
886 tmp2_buck = apr_bucket_heap_create(location, len_loc, NULL,
888 APR_BUCKET_INSERT_BEFORE(head_ptr, tmp2_buck);
889 tmp2_buck = apr_bucket_immortal_create("</A>", sizeof("</A>") - 1,
891 APR_BUCKET_INSERT_BEFORE(head_ptr, tmp2_buck);
893 if (*inserted_head == NULL) {
894 *inserted_head = tmp_buck;
898 ap_destroy_sub_req(rr);
904 static int include_cmd(include_ctx_t *ctx, apr_bucket_brigade **bb,
905 const char *command, request_rec *r, ap_filter_t *f)
909 apr_file_t *script_out = NULL, *script_in = NULL, *script_err = NULL;
910 apr_bucket_brigade *bcgi;
914 e_info.cmd_type = APR_SHELLCMD;
915 e_info.in_pipe = APR_NO_PIPE;
916 e_info.out_pipe = APR_FULL_BLOCK;
917 e_info.err_pipe = APR_NO_PIPE;
918 e_info.prog_type = RUN_AS_SSI;
921 e_info.next = f->next;
923 if ((rv = cgi_build_command(&command, &argv, r, r->pool, 0,
924 &e_info.cmd_type)) != APR_SUCCESS) {
925 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
926 "don't know how to spawn cmd child process: %s",
928 return HTTP_INTERNAL_SERVER_ERROR;
931 /* run the script in its own process */
932 if ((rv = run_cgi_child(&script_out, &script_in, &script_err,
933 command, argv, r, r->pool,
934 &e_info)) != APR_SUCCESS) {
935 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
936 "couldn't spawn child process: %s", r->filename);
937 return HTTP_INTERNAL_SERVER_ERROR;
940 bcgi = apr_brigade_create(r->pool, f->c->bucket_alloc);
941 b = apr_bucket_pipe_create(script_in, f->c->bucket_alloc);
942 APR_BRIGADE_INSERT_TAIL(bcgi, b);
943 ap_pass_brigade(f->next, bcgi);
945 /* We can't close the pipe here, because we may return before the
946 * full CGI has been sent to the network. That's okay though,
947 * because we can rely on the pool to close the pipe for us.
953 static int handle_exec(include_ctx_t *ctx, apr_bucket_brigade **bb,
954 request_rec *r, ap_filter_t *f, apr_bucket *head_ptr,
955 apr_bucket **inserted_head)
958 char *tag_val = NULL;
959 char *file = r->filename;
960 apr_bucket *tmp_buck;
961 char parsed_string[MAX_STRING_LEN];
963 *inserted_head = NULL;
964 if (ctx->flags & FLAG_PRINTING) {
965 if (ctx->flags & FLAG_NO_EXEC) {
966 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
967 "exec used but not allowed in %s", r->filename);
968 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
972 cgi_pfn_gtv(ctx, &tag, &tag_val, 1);
973 if (tag_val == NULL) {
981 if (!strcmp(tag, "cmd")) {
982 cgi_pfn_ps(r, ctx, tag_val, parsed_string,
983 sizeof(parsed_string), 1);
984 if (include_cmd(ctx, bb, parsed_string, r, f) == -1) {
985 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
986 "execution failure for parameter \"%s\" "
987 "to tag exec in file %s", tag, r->filename);
988 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr,
992 else if (!strcmp(tag, "cgi")) {
993 apr_status_t retval = APR_SUCCESS;
995 cgi_pfn_ps(r, ctx, tag_val, parsed_string,
996 sizeof(parsed_string), 0);
998 SPLIT_AND_PASS_PRETAG_BUCKETS(*bb, ctx, f->next, retval);
999 if (retval != APR_SUCCESS) {
1003 if (include_cgi(parsed_string, r, f->next, head_ptr,
1004 inserted_head) == -1) {
1005 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1006 "invalid CGI ref \"%s\" in %s",
1008 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr,
1013 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1014 "unknown parameter \"%s\" to tag exec in %s",
1016 CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr,
1026 /*============================================================================
1027 *============================================================================
1028 * This is the end of the cgi filter code moved from mod_include.
1029 *============================================================================
1030 *============================================================================*/
1033 static int cgi_post_config(apr_pool_t *p, apr_pool_t *plog,
1034 apr_pool_t *ptemp, server_rec *s)
1036 cgi_pfn_reg_with_ssi = APR_RETRIEVE_OPTIONAL_FN(ap_register_include_handler);
1037 cgi_pfn_gtv = APR_RETRIEVE_OPTIONAL_FN(ap_ssi_get_tag_and_value);
1038 cgi_pfn_ps = APR_RETRIEVE_OPTIONAL_FN(ap_ssi_parse_string);
1040 if ((cgi_pfn_reg_with_ssi) && (cgi_pfn_gtv) && (cgi_pfn_ps)) {
1041 /* Required by mod_include filter. This is how mod_cgi registers
1042 * with mod_include to provide processing of the exec directive.
1044 cgi_pfn_reg_with_ssi("exec", handle_exec);
1047 /* This is the means by which unusual (non-unix) os's may find alternate
1048 * means to run a given command (e.g. shebang/registry parsing on Win32)
1050 cgi_build_command = APR_RETRIEVE_OPTIONAL_FN(ap_cgi_build_command);
1051 if (!cgi_build_command) {
1052 cgi_build_command = default_build_command;
1057 static void register_hooks(apr_pool_t *p)
1059 static const char * const aszPre[] = { "mod_include.c", NULL };
1060 ap_hook_handler(cgi_handler, NULL, NULL, APR_HOOK_MIDDLE);
1061 ap_hook_post_config(cgi_post_config, aszPre, NULL, APR_HOOK_REALLY_FIRST);
1064 module AP_MODULE_DECLARE_DATA cgi_module =
1066 STANDARD20_MODULE_STUFF,
1067 NULL, /* dir config creater */
1068 NULL, /* dir merger --- default is to override */
1069 create_cgi_config, /* server config */
1070 merge_cgi_config, /* merge server config */
1071 cgi_cmds, /* command apr_table_t */
1072 register_hooks /* register hooks */