mod_cgi.c revision 2975523a2901fc601ae9510082a7d4fb11bb9aec
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering/* ====================================================================
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * The Apache Software License, Version 1.1
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * Copyright (c) 2000 The Apache Software Foundation. All rights
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * Redistribution and use in source and binary forms, with or without
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * modification, are permitted provided that the following conditions
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * 1. Redistributions of source code must retain the above copyright
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * notice, this list of conditions and the following disclaimer.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * 2. Redistributions in binary form must reproduce the above copyright
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * notice, this list of conditions and the following disclaimer in
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * the documentation and/or other materials provided with the
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * distribution.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * 3. The end-user documentation included with the redistribution,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * if any, must include the following acknowledgment:
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * "This product includes software developed by the
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * Apache Software Foundation (http://www.apache.org/)."
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * Alternately, this acknowledgment may appear in the software itself,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * if and wherever such third-party acknowledgments normally appear.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * 4. The names "Apache" and "Apache Software Foundation" must
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * not be used to endorse or promote products derived from this
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * software without prior written permission. For written
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * permission, please contact apache@apache.org.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * 5. Products derived from this software may not be called "Apache",
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * nor may "Apache" appear in their name, without prior written
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * permission of the Apache Software Foundation.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * SUCH DAMAGE.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * ====================================================================
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * This software consists of voluntary contributions made by many
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * individuals on behalf of the Apache Software Foundation. For more
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * information on the Apache Software Foundation, please see
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * Portions of this software are based upon public domain software
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * originally written at the National Center for Supercomputing Applications,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * University of Illinois, Urbana-Champaign.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * http_script: keeps all script-related ramblings together.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * Compliant to CGI/1.1 spec
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * Adapted by rst from original NCSA code by Rob McCool
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * Apache adds some new env vars; REDIRECT_URL and REDIRECT_QUERY_STRING for
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * custom error responses, and DOCUMENT_ROOT because we found it useful.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * It also adds SERVER_ADMIN - useful for scripts to know who to mail when
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include "apr_thread_proc.h" /* for RLIMIT stuff */
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering/* KLUDGE --- for back-combatibility, we don't have to check ExecCGI
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * in ScriptAliased directories, which means we need to know if this
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * request came through ScriptAlias or not... so the Alias module
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * leaves a note for us.
4f76ef0423a30ee672891056aeb5df2422947e1dThomas Hindoe Paaboel Andersenstatic int is_scriptaliased(request_rec *r)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering const char *t = apr_table_get(r->notes, "alias-forced-type");
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering/* Configuration stuff */
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringtypedef struct {
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic void *create_cgi_config(apr_pool_t *p, server_rec *s)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering (cgi_server_conf *) apr_pcalloc(p, sizeof(cgi_server_conf));
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic void *merge_cgi_config(apr_pool_t *p, void *basev, void *overridesv)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering cgi_server_conf *base = (cgi_server_conf *) basev, *overrides = (cgi_server_conf *) overridesv;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering return overrides->logname ? overrides : base;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic const char *set_scriptlog(cmd_parms *cmd, void *dummy, const char *arg)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering (cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic const char *set_scriptlog_length(cmd_parms *cmd, void *dummy,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering (cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic const char *set_scriptlog_buffer(cmd_parms *cmd, void *dummy,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering (cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
f757855e81fc0bc116de372220096e532afb5cb8Lennart PoetteringAP_INIT_TAKE1("ScriptLog", set_scriptlog, NULL, RSRC_CONF,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering "the name of a log for script debugging info"),
f757855e81fc0bc116de372220096e532afb5cb8Lennart PoetteringAP_INIT_TAKE1("ScriptLogLength", set_scriptlog_length, NULL, RSRC_CONF,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering "the maximum length (in bytes) of the script debug log"),
f757855e81fc0bc116de372220096e532afb5cb8Lennart PoetteringAP_INIT_TAKE1("ScriptLogBuffer", set_scriptlog_buffer, NULL, RSRC_CONF,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering "the maximum size (in bytes) to record of a POST request"),
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic int log_scripterror(request_rec *r, cgi_server_conf * conf, int ret,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ap_log_rerror(APLOG_MARK, show_errno|APLOG_ERR, errno, r,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ((apr_stat(&finfo, ap_server_root_relative(r->pool, conf->logname), r->pool) == APR_SUCCESS)
4f76ef0423a30ee672891056aeb5df2422947e1dThomas Hindoe Paaboel Andersen (apr_open(&f, ap_server_root_relative(r->pool, conf->logname),
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering /* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_fprintf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering r->args ? "?" : "", r->args ? r->args : "", r->protocol);
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt apr_fprintf(f, "%%%% %d %s\n", ret, r->filename);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering/* Soak up stderr from a script and redirect it to the error log.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic void log_script_err(request_rec *r, apr_file_t *script_err)
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt while (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_err) == 0) {
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic int log_script(request_rec *r, cgi_server_conf * conf, int ret,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering char *dbuf, const char *sbuf, apr_file_t *script_in,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_array_header_t *hdrs_arr = apr_table_elts(r->headers_in);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_table_entry_t *hdrs = (apr_table_entry_t *) hdrs_arr->elts;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ((apr_stat(&finfo, ap_server_root_relative(r->pool, conf->logname), r->pool) == APR_SUCCESS)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering (apr_open(&f, ap_server_root_relative(r->pool, conf->logname),
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering /* Soak up script output */
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering while (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_in) == 0)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering /* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_fprintf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering r->args ? "?" : "", r->args ? r->args : "", r->protocol);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering /* "%% 500 /usr/local/apache/cgi-bin" */
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_fprintf(f, "%%%% %d %s\n", ret, r->filename);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_fprintf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering if ((r->method_number == M_POST || r->method_number == M_PUT)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering hdrs_arr = apr_table_elts(r->err_headers_out);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering hdrs = (apr_table_entry_t *) hdrs_arr->elts;
4f76ef0423a30ee672891056aeb5df2422947e1dThomas Hindoe Paaboel Andersen apr_fprintf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering if (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_in) == 0) {
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering while (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_in) == 0)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering if (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_err) == 0) {
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering while (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_err) == 0)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic apr_status_t run_cgi_child(apr_file_t **script_out,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering const char * const argv[],
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering const char * const *env;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_proc_t *procnew = apr_pcalloc(p, sizeof(*procnew));
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#if defined(RLIMIT_CPU) || defined(RLIMIT_NPROC) || \
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined (RLIMIT_AS)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering conf = (core_dir_config *) ap_get_module_config(r->per_dir_config, &core_module);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering /* Under OS/2 need to use device con. */
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering fprintf(dbg, "Attempting to exec %s as %sCGI child (argv0 = %s)\n",
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering r->filename, cld->nph ? "NPH " : "", argv0);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering env = (const char * const *)ap_create_environment(p, r->subprocess_env);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering for (i = 0; env[i]; ++i)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering /* Transmute ourselves into the script.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * NB only ISINDEX scripts get decoded arguments.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering if (((rc = apr_createprocattr_init(&procattr, p)) != APR_SUCCESS) ||
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ap_make_dirstr_parent(r->pool, r->filename))) != APR_SUCCESS) ||
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ((rc = apr_setprocattr_limit(procattr, APR_LIMIT_CPU, conf->limit_cpu)) != APR_SUCCESS) ||
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ((rc = apr_setprocattr_limit(procattr, APR_LIMIT_MEM, conf->limit_mem)) != APR_SUCCESS) ||
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ((rc = apr_setprocattr_limit(procattr, APR_LIMIT_NPROC, conf->limit_nproc)) != APR_SUCCESS) ||
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ((rc = apr_setprocattr_cmdtype(procattr, APR_PROGRAM)) != APR_SUCCESS)) {
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering /* Something bad happened, tell the world. */
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering "couldn't set child process attributes: %s", r->filename);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering rc = ap_os_create_privileged_process(r, procnew, command, argv, env, procattr, p);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering /* Bad things happened. Everyone should have cleaned up. */
if (!script_in)
return APR_EBADF;
if (!*script_out)
return APR_EBADF;
if (!*script_err)
return APR_EBADF;
return (rc);
apr_pool_t *p)
++numwords;
ap_unescape_url(w);
return APR_SUCCESS;
apr_pool_t *p)
#ifdef WIN32
const char *argv0;
argv0++;
#ifdef WIN32
r->filename);
return APR_EBADF;
return APR_SUCCESS;
const char *argv0;
const char *command;
const char **argv;
ap_bucket *b;
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 =
bytes_written = 0;
const char *location;
int ret;
return OK;
return HTTP_MOVED_TEMPORARILY;
if (!r->header_only) {
b = ap_bucket_create_eos();
b = ap_bucket_create_eos();
{NULL}