mod_cgi.c revision a3fbce4c162e73ad70f26bfebcbce48f89523b7f
76318284fc970b30e9dc4c079960807345331dadLennart Poettering/* ====================================================================
76318284fc970b30e9dc4c079960807345331dadLennart Poettering * The Apache Software License, Version 1.1
76318284fc970b30e9dc4c079960807345331dadLennart Poettering * Copyright (c) 2000 The Apache Software Foundation. All rights
76318284fc970b30e9dc4c079960807345331dadLennart Poettering * Redistribution and use in source and binary forms, with or without
76318284fc970b30e9dc4c079960807345331dadLennart Poettering * modification, are permitted provided that the following conditions
76318284fc970b30e9dc4c079960807345331dadLennart Poettering * 1. Redistributions of source code must retain the above copyright
76318284fc970b30e9dc4c079960807345331dadLennart Poettering * notice, this list of conditions and the following disclaimer.
76318284fc970b30e9dc4c079960807345331dadLennart Poettering * 2. Redistributions in binary form must reproduce the above copyright
76318284fc970b30e9dc4c079960807345331dadLennart Poettering * notice, this list of conditions and the following disclaimer in
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering * the documentation and/or other materials provided with the
76318284fc970b30e9dc4c079960807345331dadLennart Poettering * distribution.
76318284fc970b30e9dc4c079960807345331dadLennart Poettering * 3. The end-user documentation included with the redistribution,
76318284fc970b30e9dc4c079960807345331dadLennart Poettering * if any, must include the following acknowledgment:
76318284fc970b30e9dc4c079960807345331dadLennart Poettering * "This product includes software developed by the
76318284fc970b30e9dc4c079960807345331dadLennart Poettering * Apache Software Foundation (http://www.apache.org/)."
76318284fc970b30e9dc4c079960807345331dadLennart Poettering * Alternately, this acknowledgment may appear in the software itself,
76318284fc970b30e9dc4c079960807345331dadLennart Poettering * if and wherever such third-party acknowledgments normally appear.
ecca17f6eec83b58f39ff5dc7894044c524ddf41Kay Sievers * 4. The names "Apache" and "Apache Software Foundation" must
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * not be used to endorse or promote products derived from this
ecca17f6eec83b58f39ff5dc7894044c524ddf41Kay Sievers * software without prior written permission. For written
ecca17f6eec83b58f39ff5dc7894044c524ddf41Kay Sievers * permission, please contact apache@apache.org.
ecca17f6eec83b58f39ff5dc7894044c524ddf41Kay Sievers * 5. Products derived from this software may not be called "Apache",
ecca17f6eec83b58f39ff5dc7894044c524ddf41Kay Sievers * nor may "Apache" appear in their name, without prior written
ecca17f6eec83b58f39ff5dc7894044c524ddf41Kay Sievers * permission of the Apache Software Foundation.
ecca17f6eec83b58f39ff5dc7894044c524ddf41Kay Sievers * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * ====================================================================
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * This software consists of voluntary contributions made by many
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * individuals on behalf of the Apache Software Foundation. For more
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * information on the Apache Software Foundation, please see
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * Portions of this software are based upon public domain software
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * originally written at the National Center for Supercomputing Applications,
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * University of Illinois, Urbana-Champaign.
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * http_script: keeps all script-related ramblings together.
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * Compliant to CGI/1.1 spec
74a6d87d0cd1f2213869e168b6ca55eded6f4ae8Zbigniew Jędrzejewski-Szmek * Adapted by rst from original NCSA code by Rob McCool
74a6d87d0cd1f2213869e168b6ca55eded6f4ae8Zbigniew Jędrzejewski-Szmek * Apache adds some new env vars; REDIRECT_URL and REDIRECT_QUERY_STRING for
74a6d87d0cd1f2213869e168b6ca55eded6f4ae8Zbigniew Jędrzejewski-Szmek * custom error responses, and DOCUMENT_ROOT because we found it useful.
74a6d87d0cd1f2213869e168b6ca55eded6f4ae8Zbigniew Jędrzejewski-Szmek * It also adds SERVER_ADMIN - useful for scripts to know who to mail when
b5c7d097ec7d16facaaeb0da5ba2abb2b1fc230bZbigniew Jędrzejewski-Szmekmodule MODULE_VAR_EXPORT cgi_module;
b5c7d097ec7d16facaaeb0da5ba2abb2b1fc230bZbigniew Jędrzejewski-Szmek/* KLUDGE --- for back-combatibility, we don't have to check ExecCGI
b5c7d097ec7d16facaaeb0da5ba2abb2b1fc230bZbigniew Jędrzejewski-Szmek * in ScriptAliased directories, which means we need to know if this
b5c7d097ec7d16facaaeb0da5ba2abb2b1fc230bZbigniew Jędrzejewski-Szmek * request came through ScriptAlias or not... so the Alias module
b5c7d097ec7d16facaaeb0da5ba2abb2b1fc230bZbigniew Jędrzejewski-Szmek * leaves a note for us.
b5c7d097ec7d16facaaeb0da5ba2abb2b1fc230bZbigniew Jędrzejewski-Szmekstatic int is_scriptaliased(request_rec *r)
b5c7d097ec7d16facaaeb0da5ba2abb2b1fc230bZbigniew Jędrzejewski-Szmek const char *t = apr_table_get(r->notes, "alias-forced-type");
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek return t && (!strcasecmp(t, "cgi-script"));
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek/* Configuration stuff */
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek#define DEFAULT_LOGBYTES 10385760
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmekstatic void *create_cgi_config(apr_pool_t *p, server_rec *s)
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek (cgi_server_conf *) apr_pcalloc(p, sizeof(cgi_server_conf));
fa13e4a78487971a5093db3fdc61cad224d47c16Zbigniew Jędrzejewski-Szmekstatic void *merge_cgi_config(apr_pool_t *p, void *basev, void *overridesv)
fa13e4a78487971a5093db3fdc61cad224d47c16Zbigniew Jędrzejewski-Szmek cgi_server_conf *base = (cgi_server_conf *) basev, *overrides = (cgi_server_conf *) overridesv;
fa13e4a78487971a5093db3fdc61cad224d47c16Zbigniew Jędrzejewski-Szmek return overrides->logname ? overrides : base;
fa13e4a78487971a5093db3fdc61cad224d47c16Zbigniew Jędrzejewski-Szmekstatic const char *set_scriptlog(cmd_parms *cmd, void *dummy, const char *arg)
fa13e4a78487971a5093db3fdc61cad224d47c16Zbigniew Jędrzejewski-Szmek (cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
fa13e4a78487971a5093db3fdc61cad224d47c16Zbigniew Jędrzejewski-Szmekstatic const char *set_scriptlog_length(cmd_parms *cmd, void *dummy,
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek (cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmekstatic const char *set_scriptlog_buffer(cmd_parms *cmd, void *dummy,
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek (cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmekstatic const command_rec cgi_cmds[] =
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-SzmekAP_INIT_TAKE1("ScriptLog", set_scriptlog, NULL, RSRC_CONF,
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek "the name of a log for script debugging info"),
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-SzmekAP_INIT_TAKE1("ScriptLogLength", set_scriptlog_length, NULL, RSRC_CONF,
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek "the maximum length (in bytes) of the script debug log"),
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-SzmekAP_INIT_TAKE1("ScriptLogBuffer", set_scriptlog_buffer, NULL, RSRC_CONF,
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek "the maximum size (in bytes) to record of a POST request"),
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmekstatic int log_scripterror(request_rec *r, cgi_server_conf * conf, int ret,
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek ap_log_rerror(APLOG_MARK, show_errno|APLOG_ERR, errno, r,
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek ((apr_stat(&finfo, ap_server_root_relative(r->pool, conf->logname), r->pool) == APR_SUCCESS)
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek && (finfo.size > conf->logbytes)) ||
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek (apr_open(&f, ap_server_root_relative(r->pool, conf->logname),
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek /* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek apr_fprintf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek r->args ? "?" : "", r->args ? r->args : "", r->protocol);
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek /* "%% 500 /usr/local/apache/cgi-bin */
20089f95e83dd6a3e9238b3448ce2e55536184ffZbigniew Jędrzejewski-Szmek apr_fprintf(f, "%%%% %d %s\n", ret, r->filename);
2cc8d9731aff3e401bc6a5a243f20fec123d48e6Zbigniew Jędrzejewski-Szmek/* Soak up stderr from a script and redirect it to the error log.
2cc8d9731aff3e401bc6a5a243f20fec123d48e6Zbigniew Jędrzejewski-Szmekstatic void log_script_err(request_rec *r, BUFF *script_err)
2cc8d9731aff3e401bc6a5a243f20fec123d48e6Zbigniew Jędrzejewski-Szmek char argsbuffer[HUGE_STRING_LEN];
2cc8d9731aff3e401bc6a5a243f20fec123d48e6Zbigniew Jędrzejewski-Szmek while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0) {
2cc8d9731aff3e401bc6a5a243f20fec123d48e6Zbigniew Jędrzejewski-Szmek newline = strchr(argsbuffer, '\n');
2cc8d9731aff3e401bc6a5a243f20fec123d48e6Zbigniew Jędrzejewski-Szmek ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r,
2cc8d9731aff3e401bc6a5a243f20fec123d48e6Zbigniew Jędrzejewski-Szmekstatic int log_script(request_rec *r, cgi_server_conf * conf, int ret,
2cc8d9731aff3e401bc6a5a243f20fec123d48e6Zbigniew Jędrzejewski-Szmek char *dbuf, const char *sbuf, BUFF *script_in, BUFF *script_err)
2cc8d9731aff3e401bc6a5a243f20fec123d48e6Zbigniew Jędrzejewski-Szmek apr_array_header_t *hdrs_arr = apr_table_elts(r->headers_in);
2cc8d9731aff3e401bc6a5a243f20fec123d48e6Zbigniew Jędrzejewski-Szmek apr_table_entry_t *hdrs = (apr_table_entry_t *) hdrs_arr->elts;
2cc8d9731aff3e401bc6a5a243f20fec123d48e6Zbigniew Jędrzejewski-Szmek char argsbuffer[HUGE_STRING_LEN];
ecca17f6eec83b58f39ff5dc7894044c524ddf41Kay Sievers ((apr_stat(&finfo, ap_server_root_relative(r->pool, conf->logname), r->pool) == APR_SUCCESS)
ecca17f6eec83b58f39ff5dc7894044c524ddf41Kay Sievers (apr_open(&f, ap_server_root_relative(r->pool, conf->logname),
909f413d3c572baadf9b13e36e1e90beba42af86Zbigniew Jędrzejewski-Szmek APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
909f413d3c572baadf9b13e36e1e90beba42af86Zbigniew Jędrzejewski-Szmek /* Soak up script output */
909f413d3c572baadf9b13e36e1e90beba42af86Zbigniew Jędrzejewski-Szmek while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_in) > 0)
76318284fc970b30e9dc4c079960807345331dadLennart Poettering /* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
&& *dbuf) {
apr_close(f);
return ret;
char **env;
#ifdef DEBUG_CGI
#ifdef OS2
#ifdef DEBUG_CGI
ap_add_cgi_vars(r);
#ifdef DEBUG_CGI
for (i = 0; env[i]; ++i)
#ifdef RLIMIT_CPU
#ifdef RLIMIT_NPROC
if (!file)
return APR_EBADF;
if (!file)
return APR_EBADF;
if (!file)
return APR_EBADF;
return (rc);
++numwords;
ap_unescape_url(w);
return APR_SUCCESS;
#ifdef WIN32
r->filename);
return APR_EBADF;
return APR_SUCCESS;
char *command;
apr_pool_t *p;
return DECLINED;
argv0++;
char *newfile;
return retval;
return HTTP_INTERNAL_SERVER_ERROR;
return HTTP_INTERNAL_SERVER_ERROR;
return HTTP_INTERNAL_SERVER_ERROR;
if (ap_should_client_block(r)) {
dbpos = 0;
while ((len_read =
const char *location;
int ret;
return OK;
return HTTP_MOVED_TEMPORARILY;
if (!r->header_only) {
{NULL}