mod_cgi.c revision 2975523a2901fc601ae9510082a7d4fb11bb9aec
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering/* ====================================================================
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * The Apache Software License, Version 1.1
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering *
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * Copyright (c) 2000 The Apache Software Foundation. All rights
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * reserved.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering *
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 * are met:
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering *
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 *
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 *
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 *
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 *
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 *
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 *
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 * <http://www.apache.org/>.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering *
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 */
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering/*
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * http_script: keeps all script-related ramblings together.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering *
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * Compliant to CGI/1.1 spec
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering *
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * Adapted by rst from original NCSA code by Rob McCool
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering *
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 * they fail.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering */
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include "apr.h"
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include "apr_strings.h"
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include "apr_thread_proc.h" /* for RLIMIT stuff */
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
a8eaaee72a2f06e0fb64fb71de3b71ecba31dafbJan Engelhardt#define CORE_PRIVATE
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include "ap_buckets.h"
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include "util_filter.h"
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt#include "ap_config.h"
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include "httpd.h"
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include "http_config.h"
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include "http_request.h"
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include "http_core.h"
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include "http_protocol.h"
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include "http_main.h"
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include "http_log.h"
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include "util_script.h"
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include "ap_mpm.h"
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include "http_conf_globals.h"
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#ifdef HAVE_STRING_H
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include <string.h>
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#endif
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#ifdef HAVE_STRINGS_H
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#include <strings.h>
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#endif
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringmodule AP_MODULE_DECLARE_DATA cgi_module;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
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.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering */
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt
4f76ef0423a30ee672891056aeb5df2422947e1dThomas Hindoe Paaboel Andersenstatic int is_scriptaliased(request_rec *r)
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt{
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering const char *t = apr_table_get(r->notes, "alias-forced-type");
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt return t && (!strcasecmp(t, "cgi-script"));
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering}
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering/* Configuration stuff */
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#define DEFAULT_LOGBYTES 10385760
a8eaaee72a2f06e0fb64fb71de3b71ecba31dafbJan Engelhardt#define DEFAULT_BUFBYTES 1024
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringtypedef struct {
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering const char *logname;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering long logbytes;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering int bufbytes;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering} cgi_server_conf;
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic void *create_cgi_config(apr_pool_t *p, server_rec *s)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering{
a8eaaee72a2f06e0fb64fb71de3b71ecba31dafbJan Engelhardt cgi_server_conf *c =
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering (cgi_server_conf *) apr_pcalloc(p, sizeof(cgi_server_conf));
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering c->logname = NULL;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering c->logbytes = DEFAULT_LOGBYTES;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering c->bufbytes = DEFAULT_BUFBYTES;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering return c;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering}
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic void *merge_cgi_config(apr_pool_t *p, void *basev, void *overridesv)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering{
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering cgi_server_conf *base = (cgi_server_conf *) basev, *overrides = (cgi_server_conf *) overridesv;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering return overrides->logname ? overrides : base;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering}
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic const char *set_scriptlog(cmd_parms *cmd, void *dummy, const char *arg)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering{
a8eaaee72a2f06e0fb64fb71de3b71ecba31dafbJan Engelhardt server_rec *s = cmd->server;
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt cgi_server_conf *conf =
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering (cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering conf->logname = arg;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering return NULL;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering}
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic const char *set_scriptlog_length(cmd_parms *cmd, void *dummy,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering const char *arg)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering{
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering server_rec *s = cmd->server;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering cgi_server_conf *conf =
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering (cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering conf->logbytes = atol(arg);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering return NULL;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering}
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic const char *set_scriptlog_buffer(cmd_parms *cmd, void *dummy,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering const char *arg)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering{
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering server_rec *s = cmd->server;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering cgi_server_conf *conf =
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering (cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering conf->bufbytes = atoi(arg);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering return NULL;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering}
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic const command_rec cgi_cmds[] =
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering{
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 Poettering {NULL}
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering};
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic int log_scripterror(request_rec *r, cgi_server_conf * conf, int ret,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering int show_errno, char *error)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering{
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_file_t *f = NULL;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_finfo_t finfo;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering char time_str[APR_CTIME_LEN];
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ap_log_rerror(APLOG_MARK, show_errno|APLOG_ERR, errno, r,
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt "%s: %s", error, r->filename);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering if (!conf->logname ||
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ((apr_stat(&finfo, ap_server_root_relative(r->pool, conf->logname), r->pool) == APR_SUCCESS)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering && (finfo.size > conf->logbytes)) ||
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 return ret;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering }
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering /* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_ctime(time_str, apr_now());
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 */
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt apr_fprintf(f, "%%%% %d %s\n", ret, r->filename);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_fprintf(f, "%%error\n%s\n", error);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_close(f);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering return ret;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering}
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering/* Soak up stderr from a script and redirect it to the error log.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering */
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic void log_script_err(request_rec *r, apr_file_t *script_err)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering{
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering char argsbuffer[HUGE_STRING_LEN];
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering char *newline;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt while (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_err) == 0) {
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering newline = strchr(argsbuffer, '\n');
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering if (newline) {
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering *newline = '\0';
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering }
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering "%s", argsbuffer);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering }
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering}
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
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_file_t *script_err)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering{
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 char argsbuffer[HUGE_STRING_LEN];
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_file_t *f = NULL;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering int i;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_finfo_t finfo;
a8eaaee72a2f06e0fb64fb71de3b71ecba31dafbJan Engelhardt char time_str[APR_CTIME_LEN];
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering if (!conf->logname ||
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ((apr_stat(&finfo, ap_server_root_relative(r->pool, conf->logname), r->pool) == APR_SUCCESS)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering && (finfo.size > conf->logbytes)) ||
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 continue;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering log_script_err(r, script_err);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering return ret;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering }
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering /* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_ctime(time_str, apr_now());
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
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_puts("%request\n", f);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering for (i = 0; i < hdrs_arr->nelts; ++i) {
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering if (!hdrs[i].key)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering continue;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_fprintf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering }
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering if ((r->method_number == M_POST || r->method_number == M_PUT)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering && *dbuf) {
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_fprintf(f, "\n%s\n", dbuf);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering }
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_puts("%response\n", f);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering hdrs_arr = apr_table_elts(r->err_headers_out);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering hdrs = (apr_table_entry_t *) hdrs_arr->elts;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering for (i = 0; i < hdrs_arr->nelts; ++i) {
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering if (!hdrs[i].key)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering continue;
4f76ef0423a30ee672891056aeb5df2422947e1dThomas Hindoe Paaboel Andersen apr_fprintf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering }
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering if (sbuf && *sbuf)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_fprintf(f, "%s\n", sbuf);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering if (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_in) == 0) {
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_puts("%stdout\n", f);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_puts(argsbuffer, f);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering while (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_in) == 0)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_puts(argsbuffer, f);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_puts("\n", f);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering }
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering if (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_err) == 0) {
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_puts("%stderr\n", f);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_puts(argsbuffer, f);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering while (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_err) == 0)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_puts(argsbuffer, f);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_puts("\n", f);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering }
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_close(script_in);
a8eaaee72a2f06e0fb64fb71de3b71ecba31dafbJan Engelhardt apr_close(script_err);
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_close(f);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering return ret;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering}
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poetteringstatic apr_status_t run_cgi_child(apr_file_t **script_out,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_file_t **script_in,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_file_t **script_err,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering const char *command,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering const char * const argv[],
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering request_rec *r, apr_pool_t *p)
a8eaaee72a2f06e0fb64fb71de3b71ecba31dafbJan Engelhardt{
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering const char * const *env;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_procattr_t *procattr;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_proc_t *procnew = apr_pcalloc(p, sizeof(*procnew));
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering apr_status_t rc = APR_SUCCESS;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#if defined(RLIMIT_CPU) || defined(RLIMIT_NPROC) || \
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined (RLIMIT_AS)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering core_dir_config *conf;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering conf = (core_dir_config *) ap_get_module_config(r->per_dir_config, &core_module);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#endif
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#ifdef DEBUG_CGI
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#ifdef OS2
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering /* Under OS/2 need to use device con. */
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering FILE *dbg = fopen("con", "w");
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#else
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering FILE *dbg = fopen("/dev/tty", "w");
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#endif
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering int i;
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#endif
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt RAISE_SIGSTOP(CGI_CHILD);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#ifdef DEBUG_CGI
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering fprintf(dbg, "Attempting to exec %s as %sCGI child (argv0 = %s)\n",
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering r->filename, cld->nph ? "NPH " : "", argv0);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#endif
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ap_add_cgi_vars(r);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering env = (const char * const *)ap_create_environment(p, r->subprocess_env);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#ifdef DEBUG_CGI
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering fprintf(dbg, "Environment: \n");
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering for (i = 0; env[i]; ++i)
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering fprintf(dbg, "'%s'\n", env[i]);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#endif
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering /* Transmute ourselves into the script.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering * NB only ISINDEX scripts get decoded arguments.
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering */
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering if (((rc = apr_createprocattr_init(&procattr, p)) != APR_SUCCESS) ||
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ((rc = apr_setprocattr_io(procattr,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering APR_CHILD_BLOCK,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering APR_CHILD_BLOCK,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering APR_CHILD_BLOCK)) != APR_SUCCESS) ||
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ((rc = apr_setprocattr_dir(procattr,
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ap_make_dirstr_parent(r->pool, r->filename))) != APR_SUCCESS) ||
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#ifdef RLIMIT_CPU
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ((rc = apr_setprocattr_limit(procattr, APR_LIMIT_CPU, conf->limit_cpu)) != APR_SUCCESS) ||
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#endif
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#endif
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#ifdef RLIMIT_NPROC
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering ((rc = apr_setprocattr_limit(procattr, APR_LIMIT_NPROC, conf->limit_nproc)) != APR_SUCCESS) ||
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering#endif
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 }
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering else {
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering rc = ap_os_create_privileged_process(r, procnew, command, argv, env, procattr, p);
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering if (rc != APR_SUCCESS) {
f757855e81fc0bc116de372220096e532afb5cb8Lennart Poettering /* Bad things happened. Everyone should have cleaned up. */
ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r,
"couldn't create child process: %d: %s", rc, r->filename);
}
else {
apr_note_subprocess(p, procnew, kill_after_timeout);
*script_in = procnew->out;
if (!script_in)
return APR_EBADF;
apr_set_pipe_timeout(*script_in,
(int)(r->server->timeout * APR_USEC_PER_SEC));
*script_out = procnew->in;
if (!*script_out)
return APR_EBADF;
apr_set_pipe_timeout(*script_out,
(int)(r->server->timeout * APR_USEC_PER_SEC));
*script_err = procnew->err;
if (!*script_err)
return APR_EBADF;
apr_set_pipe_timeout(*script_err,
(int)(r->server->timeout * APR_USEC_PER_SEC));
}
}
return (rc);
}
static apr_status_t build_argv_list(const char ***argv, request_rec *r,
apr_pool_t *p)
{
int numwords, x, idx;
char *w;
const char *args = r->args;
if (!args || !args[0] || ap_strchr_c(args, '=')) {
numwords = 1;
}
else {
/* count the number of keywords */
for (x = 0, numwords = 2; args[x]; x++) {
if (args[x] == '+') {
++numwords;
}
}
}
/* Everything is - 1 to account for the first parameter which is the
* program name. We didn't used to have to do this, but APR wants it.
*/
if (numwords > APACHE_ARG_MAX - 1) {
numwords = APACHE_ARG_MAX - 1; /* Truncate args to prevent overrun */
}
*argv = apr_palloc(p, (numwords + 2) * sizeof(char *));
for (x = 1, idx = 1; x < numwords; x++) {
w = ap_getword_nulls(p, &args, '+');
ap_unescape_url(w);
(*argv)[idx++] = ap_escape_shell_cmd(p, w);
}
(*argv)[idx] = NULL;
return APR_SUCCESS;
}
static apr_status_t build_command_line(const char **cmd, request_rec *r,
apr_pool_t *p)
{
#ifdef WIN32
char *quoted_filename = NULL;
char *interpreter = NULL;
char *arguments = NULL;
file_type_e fileType;
#endif
const char *argv0;
/* Allow suexec's "/" check to succeed */
if ((argv0 = strrchr(r->filename, '/')) != NULL)
argv0++;
else
argv0 = r->filename;
#ifdef WIN32
*cmd = NULL;
fileType = ap_get_win32_interpreter(r, &interpreter, &arguments);
if (fileType == eFileTypeUNKNOWN) {
ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r,
"%s is not executable; ensure interpreted scripts have "
"\"#!\" first line",
r->filename);
return APR_EBADF;
}
/*
* Build the command string to pass to ap_os_create_privileged_process()
*/
quoted_filename = apr_pstrcat(p, "\"", r->filename, "\"", NULL);
if (interpreter && *interpreter) {
if (arguments && *arguments)
*cmd = apr_pstrcat(p, interpreter, " ", quoted_filename, " ",
arguments, NULL);
else
*cmd = apr_pstrcat(p, interpreter, " ", quoted_filename, " ", NULL);
}
else if (arguments && *arguments) {
*cmd = apr_pstrcat(p, quoted_filename, " ", arguments, NULL);
}
else {
*cmd = apr_pstrcat(p, quoted_filename, NULL);
}
#else
*cmd = apr_pstrcat(p, r->filename, NULL);
#endif
return APR_SUCCESS;
}
static int cgi_handler(request_rec *r)
{
int retval, nph, dbpos = 0;
const char *argv0;
const char *command;
const char **argv;
char *dbuf = NULL;
apr_file_t *script_out = NULL, *script_in = NULL, *script_err = NULL;
ap_bucket_brigade *bb;
ap_bucket *b;
char argsbuffer[HUGE_STRING_LEN];
int is_included = !strcmp(r->protocol, "INCLUDED");
apr_pool_t *p;
cgi_server_conf *conf;
p = r->main ? r->main->pool : r->pool;
if (r->method_number == M_OPTIONS) {
/* 99 out of 100 CGI scripts, this is all they support */
r->allowed |= (1 << M_GET);
r->allowed |= (1 << M_POST);
return DECLINED;
}
if ((argv0 = strrchr(r->filename, '/')) != NULL)
argv0++;
else
argv0 = r->filename;
nph = !(strncmp(argv0, "nph-", 4));
conf = ap_get_module_config(r->server->module_config, &cgi_module);
if (!(ap_allow_options(r) & OPT_EXECCGI) && !is_scriptaliased(r))
return log_scripterror(r, conf, HTTP_FORBIDDEN, APLOG_NOERRNO,
"Options ExecCGI is off in this directory");
if (nph && is_included)
return log_scripterror(r, conf, HTTP_FORBIDDEN, APLOG_NOERRNO,
"attempt to include NPH CGI script");
#if defined(OS2) || defined(WIN32)
/* XXX: This is wrong. As an option, perhaps, but not by default...
* we are back to the same argument that a url should be a unique
* entity until the sysadmin overrides that behavior.
* Allow for cgi files without the .EXE extension on them under OS/2
*/
if (r->finfo.protection == 0) {
apr_finfo_t finfo;
char *newfile;
newfile = apr_pstrcat(r->pool, r->filename, ".EXE", NULL);
if ((apr_stat(&finfo, newfile, r->pool) != APR_SUCCESS) ||
(finfo.filetype != APR_REG)) {
return log_scripterror(r, conf, HTTP_NOT_FOUND, 0,
"script not found or unable to stat");
} else {
r->filename = newfile;
}
}
#else
if (r->finfo.protection == 0)
return log_scripterror(r, conf, HTTP_NOT_FOUND, APLOG_NOERRNO,
"script not found or unable to stat");
#endif
if (r->finfo.filetype == APR_DIR)
return log_scripterror(r, conf, HTTP_FORBIDDEN, APLOG_NOERRNO,
"attempt to invoke directory as script");
/*
if (!ap_suexec_enabled) {
if (!ap_can_exec(&r->finfo))
return log_scripterror(r, conf, HTTP_FORBIDDEN, APLOG_NOERRNO,
"file permissions deny server execution");
}
*/
if ((retval = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)))
return retval;
ap_add_common_vars(r);
/* build the command line */
if (build_command_line(&command, r, p) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
"couldn't spawn child process: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* build the argument list */
else if (build_argv_list(&argv, r, p) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
"couldn't spawn child process: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
argv[0] = apr_pstrdup(p, command);
/* run the script in its own process */
if (run_cgi_child(&script_out, &script_in, &script_err,
command, argv, r, p) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
"couldn't spawn child process: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* Transfer any put/post args, CERN style...
* Note that we already ignore SIGPIPE in the core server.
*/
if (ap_should_client_block(r)) {
int dbsize, len_read;
apr_size_t bytes_written, bytes_to_write;
apr_status_t rv;
if (conf->logname) {
dbuf = apr_pcalloc(r->pool, conf->bufbytes + 1);
dbpos = 0;
}
while ((len_read =
ap_get_client_block(r, argsbuffer, HUGE_STRING_LEN)) > 0) {
if (conf->logname) {
if ((dbpos + len_read) > conf->bufbytes) {
dbsize = conf->bufbytes - dbpos;
}
else {
dbsize = len_read;
}
memcpy(dbuf + dbpos, argsbuffer, dbsize);
dbpos += dbsize;
}
/* Keep writing data to the child until done or too much time
* elapses with no progress or an error occurs.
*/
bytes_written = 0;
do {
bytes_to_write = len_read - bytes_written;
rv = apr_write(script_out, argsbuffer + bytes_written,
&bytes_to_write);
bytes_written += bytes_to_write;
} while (rv == APR_SUCCESS && bytes_written < len_read);
if (rv != APR_SUCCESS || bytes_written < len_read) {
/* silly script stopped reading, soak up remaining message */
while (ap_get_client_block(r, argsbuffer, HUGE_STRING_LEN) > 0) {
/* dump it */
}
break;
}
}
apr_flush(script_out);
}
apr_close(script_out);
/* Handle script return... */
if (script_in && !nph) {
const char *location;
char sbuf[MAX_STRING_LEN];
int ret;
if ((ret = ap_scan_script_header_err(r, script_in, sbuf))) {
return log_script(r, conf, ret, dbuf, sbuf, script_in, script_err);
}
location = apr_table_get(r->headers_out, "Location");
if (location && location[0] == '/' && r->status == 200) {
/* Soak up all the script output */
while (apr_fgets(argsbuffer, HUGE_STRING_LEN, script_in) == 0) {
continue;
}
log_script_err(r, script_err);
/* This redirect needs to be a GET no matter what the original
* method was.
*/
r->method = apr_pstrdup(r->pool, "GET");
r->method_number = M_GET;
/* We already read the message body (if any), so don't allow
* the redirected request to think it has one. We can ignore
* Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
*/
apr_table_unset(r->headers_in, "Content-Length");
ap_internal_redirect_handler(location, r);
return OK;
}
else if (location && r->status == 200) {
/* XX Note that if a script wants to produce its own Redirect
* body, it now has to explicitly *say* "Status: 302"
*/
return HTTP_MOVED_TEMPORARILY;
}
ap_send_http_header(r);
if (!r->header_only) {
bb = ap_brigade_create(r->pool);
b = ap_bucket_create_pipe(script_in);
AP_BRIGADE_INSERT_TAIL(bb, b);
b = ap_bucket_create_eos();
AP_BRIGADE_INSERT_TAIL(bb, b);
ap_pass_brigade(r->output_filters, bb);
}
log_script_err(r, script_err);
apr_close(script_err);
}
if (script_in && nph) {
bb = ap_brigade_create(r->pool);
b = ap_bucket_create_pipe(script_in);
AP_BRIGADE_INSERT_TAIL(bb, b);
b = ap_bucket_create_eos();
AP_BRIGADE_INSERT_TAIL(bb, b);
ap_pass_brigade(r->output_filters, bb);
}
return OK; /* NOT r->status, even if it has changed. */
}
static const handler_rec cgi_handlers[] =
{
{CGI_MAGIC_TYPE, cgi_handler},
{"cgi-script", cgi_handler},
{NULL}
};
module AP_MODULE_DECLARE_DATA cgi_module =
{
STANDARD20_MODULE_STUFF,
NULL, /* dir config creater */
NULL, /* dir merger --- default is to override */
create_cgi_config, /* server config */
merge_cgi_config, /* merge server config */
cgi_cmds, /* command apr_table_t */
cgi_handlers, /* handlers */
NULL /* register hooks */
};