mod_cgi.c revision 4f6effa17a5084085c9104b0bb97c2ba1622cfa6
c6d33447e28403a90ad817dba4df75fae785be28pquerna/* ====================================================================
c6d33447e28403a90ad817dba4df75fae785be28pquerna * The Apache Software License, Version 1.1
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
c6d33447e28403a90ad817dba4df75fae785be28pquerna * reserved.
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Redistribution and use in source and binary forms, with or without
c6d33447e28403a90ad817dba4df75fae785be28pquerna * modification, are permitted provided that the following conditions
c6d33447e28403a90ad817dba4df75fae785be28pquerna * are met:
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * 1. Redistributions of source code must retain the above copyright
c6d33447e28403a90ad817dba4df75fae785be28pquerna * notice, this list of conditions and the following disclaimer.
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * 2. Redistributions in binary form must reproduce the above copyright
c6d33447e28403a90ad817dba4df75fae785be28pquerna * notice, this list of conditions and the following disclaimer in
c6d33447e28403a90ad817dba4df75fae785be28pquerna * the documentation and/or other materials provided with the
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * distribution.
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * 3. The end-user documentation included with the redistribution,
c6d33447e28403a90ad817dba4df75fae785be28pquerna * if any, must include the following acknowledgment:
c6d33447e28403a90ad817dba4df75fae785be28pquerna * "This product includes software developed by the
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Apache Software Foundation (http://www.apache.org/)."
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Alternately, this acknowledgment may appear in the software itself,
c6d33447e28403a90ad817dba4df75fae785be28pquerna * if and wherever such third-party acknowledgments normally appear.
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * 4. The names "Apache" and "Apache Software Foundation" must
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * not be used to endorse or promote products derived from this
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * software without prior written permission. For written
c6d33447e28403a90ad817dba4df75fae785be28pquerna * permission, please contact apache@apache.org.
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * 5. Products derived from this software may not be called "Apache",
c6d33447e28403a90ad817dba4df75fae785be28pquerna * nor may "Apache" appear in their name, without prior written
c6d33447e28403a90ad817dba4df75fae785be28pquerna * permission of the Apache Software Foundation.
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * SUCH DAMAGE.
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * ====================================================================
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * This software consists of voluntary contributions made by many
c6d33447e28403a90ad817dba4df75fae785be28pquerna * individuals on behalf of the Apache Software Foundation. For more
c6d33447e28403a90ad817dba4df75fae785be28pquerna * information on the Apache Software Foundation, please see
c6d33447e28403a90ad817dba4df75fae785be28pquerna * <http://www.apache.org/>.
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Portions of this software are based upon public domain software
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * originally written at the National Center for Supercomputing Applications,
c6d33447e28403a90ad817dba4df75fae785be28pquerna * University of Illinois, Urbana-Champaign.
c6d33447e28403a90ad817dba4df75fae785be28pquerna */
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna/*
c6d33447e28403a90ad817dba4df75fae785be28pquerna * http_script: keeps all script-related ramblings together.
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Compliant to CGI/1.1 spec
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Adapted by rst from original NCSA code by Rob McCool
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Apache adds some new env vars; REDIRECT_URL and REDIRECT_QUERY_STRING for
c6d33447e28403a90ad817dba4df75fae785be28pquerna * custom error responses, and DOCUMENT_ROOT because we found it useful.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * It also adds SERVER_ADMIN - useful for scripts to know who to mail when
c6d33447e28403a90ad817dba4df75fae785be28pquerna * they fail.
c6d33447e28403a90ad817dba4df75fae785be28pquerna */
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "apr.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "apr_strings.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "apr_thread_proc.h" /* for RLIMIT stuff */
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "apr_optional.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "apr_buckets.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "apr_lib.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna#define APR_WANT_STRFUNC
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "apr_want.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna#define CORE_PRIVATE
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "util_filter.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "ap_config.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "httpd.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "http_config.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "http_request.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "http_core.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "http_protocol.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "http_main.h"
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna#include "http_log.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "util_script.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "ap_mpm.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "mod_core.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "mod_cgi.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernamodule AP_MODULE_DECLARE_DATA cgi_module;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic APR_OPTIONAL_FN_TYPE(ap_register_include_handler) *cgi_pfn_reg_with_ssi;
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic APR_OPTIONAL_FN_TYPE(ap_ssi_get_tag_and_value) *cgi_pfn_gtv;
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic APR_OPTIONAL_FN_TYPE(ap_ssi_parse_string) *cgi_pfn_ps;
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic APR_OPTIONAL_FN_TYPE(ap_cgi_build_command) *cgi_build_command;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna/* Read and discard the data in the brigade produced by a CGI script */
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic void discard_script_output(apr_bucket_brigade *bb);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna/* KLUDGE --- for back-combatibility, we don't have to check ExecCGI
c6d33447e28403a90ad817dba4df75fae785be28pquerna * in ScriptAliased directories, which means we need to know if this
c6d33447e28403a90ad817dba4df75fae785be28pquerna * request came through ScriptAlias or not... so the Alias module
c6d33447e28403a90ad817dba4df75fae785be28pquerna * leaves a note for us.
c6d33447e28403a90ad817dba4df75fae785be28pquerna */
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic int is_scriptaliased(request_rec *r)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna const char *t = apr_table_get(r->notes, "alias-forced-type");
c6d33447e28403a90ad817dba4df75fae785be28pquerna return t && (!strcasecmp(t, "cgi-script"));
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna/* Configuration stuff */
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna#define DEFAULT_LOGBYTES 10385760
c6d33447e28403a90ad817dba4df75fae785be28pquerna#define DEFAULT_BUFBYTES 1024
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernatypedef struct {
c6d33447e28403a90ad817dba4df75fae785be28pquerna const char *logname;
c6d33447e28403a90ad817dba4df75fae785be28pquerna long logbytes;
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_size_t bufbytes;
c6d33447e28403a90ad817dba4df75fae785be28pquerna} cgi_server_conf;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic void *create_cgi_config(apr_pool_t *p, server_rec *s)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna cgi_server_conf *c =
c6d33447e28403a90ad817dba4df75fae785be28pquerna (cgi_server_conf *) apr_pcalloc(p, sizeof(cgi_server_conf));
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna c->logname = NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna c->logbytes = DEFAULT_LOGBYTES;
c6d33447e28403a90ad817dba4df75fae785be28pquerna c->bufbytes = DEFAULT_BUFBYTES;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna return c;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic void *merge_cgi_config(apr_pool_t *p, void *basev, void *overridesv)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna cgi_server_conf *base = (cgi_server_conf *) basev,
c6d33447e28403a90ad817dba4df75fae785be28pquerna *overrides = (cgi_server_conf *) overridesv;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return overrides->logname ? overrides : base;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic const char *set_scriptlog(cmd_parms *cmd, void *dummy, const char *arg)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna server_rec *s = cmd->server;
c6d33447e28403a90ad817dba4df75fae785be28pquerna cgi_server_conf *conf = ap_get_module_config(s->module_config,
c6d33447e28403a90ad817dba4df75fae785be28pquerna &cgi_module);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna conf->logname = ap_server_root_relative(cmd->pool, arg);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!conf->logname) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna return apr_pstrcat(cmd->pool, "Invalid ScriptLog path ",
c6d33447e28403a90ad817dba4df75fae785be28pquerna arg, NULL);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna return NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic const char *set_scriptlog_length(cmd_parms *cmd, void *dummy,
c6d33447e28403a90ad817dba4df75fae785be28pquerna const char *arg)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna server_rec *s = cmd->server;
c6d33447e28403a90ad817dba4df75fae785be28pquerna cgi_server_conf *conf = ap_get_module_config(s->module_config,
c6d33447e28403a90ad817dba4df75fae785be28pquerna &cgi_module);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna conf->logbytes = atol(arg);
c6d33447e28403a90ad817dba4df75fae785be28pquerna return NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic const char *set_scriptlog_buffer(cmd_parms *cmd, void *dummy,
c6d33447e28403a90ad817dba4df75fae785be28pquerna const char *arg)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna server_rec *s = cmd->server;
c6d33447e28403a90ad817dba4df75fae785be28pquerna cgi_server_conf *conf = ap_get_module_config(s->module_config,
c6d33447e28403a90ad817dba4df75fae785be28pquerna &cgi_module);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna conf->bufbytes = atoi(arg);
c6d33447e28403a90ad817dba4df75fae785be28pquerna return NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic const command_rec cgi_cmds[] =
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquernaAP_INIT_TAKE1("ScriptLog", set_scriptlog, NULL, RSRC_CONF,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "the name of a log for script debugging info"),
c6d33447e28403a90ad817dba4df75fae785be28pquernaAP_INIT_TAKE1("ScriptLogLength", set_scriptlog_length, NULL, RSRC_CONF,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "the maximum length (in bytes) of the script debug log"),
c6d33447e28403a90ad817dba4df75fae785be28pquernaAP_INIT_TAKE1("ScriptLogBuffer", set_scriptlog_buffer, NULL, RSRC_CONF,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "the maximum size (in bytes) to record of a POST request"),
c6d33447e28403a90ad817dba4df75fae785be28pquerna {NULL}
c6d33447e28403a90ad817dba4df75fae785be28pquerna};
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic int log_scripterror(request_rec *r, cgi_server_conf * conf, int ret,
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_status_t rv, char *error)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_t *f = NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_finfo_t finfo;
c6d33447e28403a90ad817dba4df75fae785be28pquerna char time_str[APR_CTIME_LEN];
c6d33447e28403a90ad817dba4df75fae785be28pquerna int log_flags = rv ? APLOG_ERR : APLOG_ERR;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_log_rerror(APLOG_MARK, log_flags, rv, r,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "%s: %s", error, r->filename);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* XXX Very expensive mainline case! Open, then getfileinfo! */
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!conf->logname ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna ((apr_stat(&finfo, conf->logname,
c6d33447e28403a90ad817dba4df75fae785be28pquerna APR_FINFO_SIZE, r->pool) == APR_SUCCESS) &&
c6d33447e28403a90ad817dba4df75fae785be28pquerna (finfo.size > conf->logbytes)) ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna (apr_file_open(&f, conf->logname,
c6d33447e28403a90ad817dba4df75fae785be28pquerna APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT,
c6d33447e28403a90ad817dba4df75fae785be28pquerna r->pool) != APR_SUCCESS)) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna return ret;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_ctime(time_str, apr_time_now());
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_printf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
c6d33447e28403a90ad817dba4df75fae785be28pquerna r->args ? "?" : "", r->args ? r->args : "", r->protocol);
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* "%% 500 /usr/local/apache/cgi-bin */
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_printf(f, "%%%% %d %s\n", ret, r->filename);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_printf(f, "%%error\n%s\n", error);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_close(f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna return ret;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna/* Soak up stderr from a script and redirect it to the error log.
c6d33447e28403a90ad817dba4df75fae785be28pquerna */
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic void log_script_err(request_rec *r, apr_file_t *script_err)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna char argsbuffer[HUGE_STRING_LEN];
c6d33447e28403a90ad817dba4df75fae785be28pquerna char *newline;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna while (apr_file_gets(argsbuffer, HUGE_STRING_LEN,
c6d33447e28403a90ad817dba4df75fae785be28pquerna script_err) == APR_SUCCESS) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna newline = strchr(argsbuffer, '\n');
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (newline) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna *newline = '\0';
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "%s", argsbuffer);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic int log_script(request_rec *r, cgi_server_conf * conf, int ret,
c6d33447e28403a90ad817dba4df75fae785be28pquerna char *dbuf, const char *sbuf, apr_bucket_brigade *bb,
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_t *script_err)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna const apr_array_header_t *hdrs_arr = apr_table_elts(r->headers_in);
c6d33447e28403a90ad817dba4df75fae785be28pquerna const apr_table_entry_t *hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
c6d33447e28403a90ad817dba4df75fae785be28pquerna char argsbuffer[HUGE_STRING_LEN];
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_t *f = NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_bucket *e;
c6d33447e28403a90ad817dba4df75fae785be28pquerna const char *buf;
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_size_t len;
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_status_t rv;
c6d33447e28403a90ad817dba4df75fae785be28pquerna int first;
c6d33447e28403a90ad817dba4df75fae785be28pquerna int i;
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_finfo_t finfo;
c6d33447e28403a90ad817dba4df75fae785be28pquerna char time_str[APR_CTIME_LEN];
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* XXX Very expensive mainline case! Open, then getfileinfo! */
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!conf->logname ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna ((apr_stat(&finfo, conf->logname,
c6d33447e28403a90ad817dba4df75fae785be28pquerna APR_FINFO_SIZE, r->pool) == APR_SUCCESS) &&
c6d33447e28403a90ad817dba4df75fae785be28pquerna (finfo.size > conf->logbytes)) ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna (apr_file_open(&f, conf->logname,
c6d33447e28403a90ad817dba4df75fae785be28pquerna APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT,
c6d33447e28403a90ad817dba4df75fae785be28pquerna r->pool) != APR_SUCCESS)) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Soak up script output */
c6d33447e28403a90ad817dba4df75fae785be28pquerna discard_script_output(bb);
c6d33447e28403a90ad817dba4df75fae785be28pquerna log_script_err(r, script_err);
c6d33447e28403a90ad817dba4df75fae785be28pquerna return ret;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_ctime(time_str, apr_time_now());
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_printf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
c6d33447e28403a90ad817dba4df75fae785be28pquerna r->args ? "?" : "", r->args ? r->args : "", r->protocol);
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* "%% 500 /usr/local/apache/cgi-bin" */
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_printf(f, "%%%% %d %s\n", ret, r->filename);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_puts("%request\n", f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna for (i = 0; i < hdrs_arr->nelts; ++i) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!hdrs[i].key)
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna continue;
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna apr_file_printf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna }
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna if ((r->method_number == M_POST || r->method_number == M_PUT) &&
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna *dbuf) {
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna apr_file_printf(f, "\n%s\n", dbuf);
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_puts("%response\n", f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna hdrs_arr = apr_table_elts(r->err_headers_out);
c6d33447e28403a90ad817dba4df75fae785be28pquerna hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna for (i = 0; i < hdrs_arr->nelts; ++i) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!hdrs[i].key)
c6d33447e28403a90ad817dba4df75fae785be28pquerna continue;
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_printf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (sbuf && *sbuf)
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_printf(f, "%s\n", sbuf);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna first = 1;
c6d33447e28403a90ad817dba4df75fae785be28pquerna APR_BRIGADE_FOREACH(e, bb) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (APR_BUCKET_IS_EOS(e)) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna break;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna rv = apr_bucket_read(e, &buf, &len, APR_BLOCK_READ);
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (rv != APR_SUCCESS || (len == 0)) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna break;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (first) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_puts("%stdout\n", f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna first = 0;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_file_write(f, buf, &len);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_file_puts("\n", f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (apr_file_gets(argsbuffer, HUGE_STRING_LEN, script_err) == APR_SUCCESS) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_puts("%stderr\n", f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_puts(argsbuffer, f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna while (apr_file_gets(argsbuffer, HUGE_STRING_LEN,
c6d33447e28403a90ad817dba4df75fae785be28pquerna script_err) == APR_SUCCESS) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_puts(argsbuffer, f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_file_puts("\n", f);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_brigade_destroy(bb);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_file_close(script_err);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_file_close(f);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return ret;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna}
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna/* This is the special environment used for running the "exec cmd="
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * variety of SSI directives.
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic void add_ssi_vars(request_rec *r, ap_filter_t *next)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_t *e = r->subprocess_env;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (r->path_info && r->path_info[0] != '\0') {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna request_rec *pa_req;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_setn(e, "PATH_INFO", ap_escape_shell_cmd(r->pool,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna r->path_info));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna pa_req = ap_sub_req_lookup_uri(ap_escape_uri(r->pool, r->path_info),
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna r, next);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (pa_req->filename) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_setn(e, "PATH_TRANSLATED",
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_pstrcat(r->pool, pa_req->filename,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna pa_req->path_info, NULL));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_destroy_sub_req(pa_req);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (r->args) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna char *arg_copy = apr_pstrdup(r->pool, r->args);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_setn(e, "QUERY_STRING", r->args);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_unescape_url(arg_copy);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_setn(e, "QUERY_STRING_UNESCAPED",
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_escape_shell_cmd(r->pool, arg_copy));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna}
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic apr_status_t run_cgi_child(apr_file_t **script_out,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_file_t **script_in,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_file_t **script_err,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char *command,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char * const argv[],
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna request_rec *r,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_pool_t *p,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna cgi_exec_info_t *e_info)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna const char * const *env;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_procattr_t *procattr;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_proc_t *procnew;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_status_t rc = APR_SUCCESS;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna#if defined(RLIMIT_CPU) || defined(RLIMIT_NPROC) || \
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined (RLIMIT_AS)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna core_dir_config *conf = ap_get_module_config(r->per_dir_config,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna &core_module);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna#endif
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna#ifdef DEBUG_CGI
c6d33447e28403a90ad817dba4df75fae785be28pquerna#ifdef OS2
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Under OS/2 need to use device con. */
c6d33447e28403a90ad817dba4df75fae785be28pquerna FILE *dbg = fopen("con", "w");
c6d33447e28403a90ad817dba4df75fae785be28pquerna#else
c6d33447e28403a90ad817dba4df75fae785be28pquerna FILE *dbg = fopen("/dev/tty", "w");
c6d33447e28403a90ad817dba4df75fae785be28pquerna#endif
c6d33447e28403a90ad817dba4df75fae785be28pquerna int i;
c6d33447e28403a90ad817dba4df75fae785be28pquerna#endif
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna RAISE_SIGSTOP(CGI_CHILD);
c6d33447e28403a90ad817dba4df75fae785be28pquerna#ifdef DEBUG_CGI
c6d33447e28403a90ad817dba4df75fae785be28pquerna fprintf(dbg, "Attempting to exec %s as CGI child (argv0 = %s)\n",
c6d33447e28403a90ad817dba4df75fae785be28pquerna r->filename, argv[0]);
c6d33447e28403a90ad817dba4df75fae785be28pquerna#endif
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (e_info->prog_type == RUN_AS_CGI) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_add_cgi_vars(r);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna else /* SSIs want a controlled environment and a special path. */
c6d33447e28403a90ad817dba4df75fae785be28pquerna {
c6d33447e28403a90ad817dba4df75fae785be28pquerna add_ssi_vars(r, e_info->next);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna env = (const char * const *)ap_create_environment(p, r->subprocess_env);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna#ifdef DEBUG_CGI
c6d33447e28403a90ad817dba4df75fae785be28pquerna fprintf(dbg, "Environment: \n");
c6d33447e28403a90ad817dba4df75fae785be28pquerna for (i = 0; env[i]; ++i)
c6d33447e28403a90ad817dba4df75fae785be28pquerna fprintf(dbg, "'%s'\n", env[i]);
c6d33447e28403a90ad817dba4df75fae785be28pquerna#endif
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Transmute ourselves into the script.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * NB only ISINDEX scripts get decoded arguments.
c6d33447e28403a90ad817dba4df75fae785be28pquerna */
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (((rc = apr_procattr_create(&procattr, p)) != APR_SUCCESS) ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna ((rc = apr_procattr_io_set(procattr,
c6d33447e28403a90ad817dba4df75fae785be28pquerna e_info->in_pipe,
c6d33447e28403a90ad817dba4df75fae785be28pquerna e_info->out_pipe,
c6d33447e28403a90ad817dba4df75fae785be28pquerna e_info->err_pipe)) != APR_SUCCESS) ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna ((rc = apr_procattr_dir_set(procattr,
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_make_dirstr_parent(r->pool,
c6d33447e28403a90ad817dba4df75fae785be28pquerna r->filename))) != APR_SUCCESS) ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna#ifdef RLIMIT_CPU
c6d33447e28403a90ad817dba4df75fae785be28pquerna ((rc = apr_procattr_limit_set(procattr, APR_LIMIT_CPU,
c6d33447e28403a90ad817dba4df75fae785be28pquerna conf->limit_cpu)) != APR_SUCCESS) ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna#endif
c6d33447e28403a90ad817dba4df75fae785be28pquerna#if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
c6d33447e28403a90ad817dba4df75fae785be28pquerna ((rc = apr_procattr_limit_set(procattr, APR_LIMIT_MEM,
c6d33447e28403a90ad817dba4df75fae785be28pquerna conf->limit_mem)) != APR_SUCCESS) ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna#endif
c6d33447e28403a90ad817dba4df75fae785be28pquerna#ifdef RLIMIT_NPROC
c6d33447e28403a90ad817dba4df75fae785be28pquerna ((rc = apr_procattr_limit_set(procattr, APR_LIMIT_NPROC,
c6d33447e28403a90ad817dba4df75fae785be28pquerna conf->limit_nproc)) != APR_SUCCESS) ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna#endif
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ((rc = apr_procattr_cmdtype_set(procattr,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna e_info->cmd_type)) != APR_SUCCESS) ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna ((rc = apr_procattr_detach_set(procattr,
c6d33447e28403a90ad817dba4df75fae785be28pquerna e_info->detached)) != APR_SUCCESS)) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Something bad happened, tell the world. */
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "couldn't set child process attributes: %s", r->filename);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna else {
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna procnew = apr_pcalloc(p, sizeof(*procnew));
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna if (e_info->prog_type == RUN_AS_SSI) {
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna SPLIT_AND_PASS_PRETAG_BUCKETS(*(e_info->bb), e_info->ctx,
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna e_info->next, rc);
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna if (rc != APR_SUCCESS) {
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna return rc;
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna }
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna }
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna rc = ap_os_create_privileged_process(r, procnew, command, argv, env,
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna procattr, p);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna if (rc != APR_SUCCESS) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Bad things happened. Everyone should have cleaned up. */
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "couldn't create child process: %d: %s", rc,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna r->filename);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna else {
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT);
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna *script_in = procnew->out;
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna if (!*script_in)
c6d33447e28403a90ad817dba4df75fae785be28pquerna return APR_EBADF;
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_pipe_timeout_set(*script_in, r->server->timeout);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (e_info->prog_type == RUN_AS_CGI) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna *script_out = procnew->in;
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!*script_out)
c6d33447e28403a90ad817dba4df75fae785be28pquerna return APR_EBADF;
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_pipe_timeout_set(*script_out, r->server->timeout);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna *script_err = procnew->err;
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!*script_err)
c6d33447e28403a90ad817dba4df75fae785be28pquerna return APR_EBADF;
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_file_pipe_timeout_set(*script_err, r->server->timeout);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna }
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna }
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna#ifdef DEBUG_CGI
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna fclose(dbg);
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna#endif
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna return (rc);
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna}
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna
fa9496078a83e18311b90b33574fdeb9c115ed7dpquernastatic apr_status_t default_build_command(const char **cmd, const char ***argv,
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna request_rec *r, apr_pool_t *p,
c6d33447e28403a90ad817dba4df75fae785be28pquerna cgi_exec_info_t *e_info)
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna{
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna int numwords, x, idx;
c6d33447e28403a90ad817dba4df75fae785be28pquerna char *w;
c6d33447e28403a90ad817dba4df75fae785be28pquerna const char *args = NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (e_info->process_cgi) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* Allow suexec's "/" check to succeed */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char *argv0 = strrchr(r->filename, '/');
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (argv0 != NULL)
c6d33447e28403a90ad817dba4df75fae785be28pquerna argv0++;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna else
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna argv0 = r->filename;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna *cmd = argv0;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna args = r->args;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* Do not process r->args if they contain an '=' assignment
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (r->args && r->args[0] && !ap_strchr_c(r->args, '=')) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna args = r->args;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (!args) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna numwords = 1;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna else {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* count the number of keywords */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna for (x = 0, numwords = 2; args[x]; x++) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (args[x] == '+') {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ++numwords;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* Everything is - 1 to account for the first parameter
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * which is the program name.
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (numwords > APACHE_ARG_MAX - 1) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna numwords = APACHE_ARG_MAX - 1; /* Truncate args to prevent overrun */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna *argv = apr_palloc(p, (numwords + 2) * sizeof(char *));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna (*argv)[0] = *cmd;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna for (x = 1, idx = 1; x < numwords; x++) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna w = ap_getword_nulls(p, &args, '+');
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_unescape_url(w);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna (*argv)[idx++] = ap_escape_shell_cmd(p, w);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna (*argv)[idx] = NULL;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return APR_SUCCESS;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna}
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic void discard_script_output(apr_bucket_brigade *bb)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_bucket *e;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char *buf;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_size_t len;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_status_t rv;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna APR_BRIGADE_FOREACH(e, bb) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (APR_BUCKET_IS_EOS(e)) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna break;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna rv = apr_bucket_read(e, &buf, &len, APR_BLOCK_READ);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (rv != APR_SUCCESS) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna break;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna}
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic int cgi_handler(request_rec *r)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna int nph;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_size_t dbpos = 0;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char *argv0;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char *command;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char **argv;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna char *dbuf = NULL;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_file_t *script_out = NULL, *script_in = NULL, *script_err = NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_bucket_brigade *bb;
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna apr_bucket *b;
c6d33447e28403a90ad817dba4df75fae785be28pquerna int is_included;
c6d33447e28403a90ad817dba4df75fae785be28pquerna int seen_eos, child_stopped_reading;
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_pool_t *p;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna cgi_server_conf *conf;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_status_t rv;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna cgi_exec_info_t e_info;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if(strcmp(r->handler, CGI_MAGIC_TYPE) && strcmp(r->handler, "cgi-script"))
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return DECLINED;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna is_included = !strcmp(r->protocol, "INCLUDED");
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna p = r->main ? r->main->pool : r->pool;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (r->method_number == M_OPTIONS) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* 99 out of 100 CGI scripts, this is all they support */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna r->allowed |= (AP_METHOD_BIT << M_GET);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna r->allowed |= (AP_METHOD_BIT << M_POST);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return DECLINED;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna argv0 = apr_filename_of_pathname(r->filename);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna nph = !(strncmp(argv0, "nph-", 4));
c6d33447e28403a90ad817dba4df75fae785be28pquerna conf = ap_get_module_config(r->server->module_config, &cgi_module);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (!(ap_allow_options(r) & OPT_EXECCGI) && !is_scriptaliased(r))
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna "Options ExecCGI is off in this directory");
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna if (nph && is_included)
c6d33447e28403a90ad817dba4df75fae785be28pquerna return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "attempt to include NPH CGI script");
c6d33447e28403a90ad817dba4df75fae785be28pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (r->finfo.filetype == 0)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return log_scripterror(r, conf, HTTP_NOT_FOUND, 0,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "script not found or unable to stat");
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (r->finfo.filetype == APR_DIR)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "attempt to invoke directory as script");
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if ((r->used_path_info == AP_REQ_REJECT_PATH_INFO) &&
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna r->path_info && *r->path_info)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* default to accept */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return log_scripterror(r, conf, HTTP_NOT_FOUND, 0,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "AcceptPathInfo off disallows user's path");
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna/*
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (!ap_suexec_enabled) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (!ap_can_exec(&r->finfo))
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "file permissions deny server execution");
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna*/
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_add_common_vars(r);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna e_info.process_cgi = 1;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna e_info.cmd_type = APR_PROGRAM;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna e_info.detached = 0;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna e_info.in_pipe = APR_CHILD_BLOCK;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna e_info.out_pipe = APR_CHILD_BLOCK;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna e_info.err_pipe = APR_CHILD_BLOCK;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna e_info.prog_type = RUN_AS_CGI;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna e_info.bb = NULL;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna e_info.ctx = NULL;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna e_info.next = NULL;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* build the command line */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if ((rv = cgi_build_command(&command, &argv, r, p, &e_info)) != APR_SUCCESS) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "don't know how to spawn child process: %s",
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna r->filename);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return HTTP_INTERNAL_SERVER_ERROR;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* run the script in its own process */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if ((rv = run_cgi_child(&script_out, &script_in, &script_err,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna command, argv, r, p, &e_info)) != APR_SUCCESS) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "couldn't spawn child process: %s", r->filename);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return HTTP_INTERNAL_SERVER_ERROR;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* Transfer any put/post args, CERN style...
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * Note that we already ignore SIGPIPE in the core server.
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna seen_eos = 0;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna child_stopped_reading = 0;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (conf->logname) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna dbuf = apr_palloc(r->pool, conf->bufbytes + 1);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna dbpos = 0;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna do {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_bucket *bucket;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna rv = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna APR_BLOCK_READ, HUGE_STRING_LEN);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (rv != APR_SUCCESS) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return rv;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna APR_BRIGADE_FOREACH(bucket, bb) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char *data;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_size_t len;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (APR_BUCKET_IS_EOS(bucket)) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna seen_eos = 1;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna break;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* We can't do much with this. */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (APR_BUCKET_IS_FLUSH(bucket)) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna continue;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* If the child stopped, we still must read to EOS. */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (child_stopped_reading) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna continue;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* read */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (conf->logname && dbpos < conf->bufbytes) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna int cursize;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if ((dbpos + len) > conf->bufbytes) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna cursize = conf->bufbytes - dbpos;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna else {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna cursize = len;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna memcpy(dbuf + dbpos, data, cursize);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna dbpos += cursize;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* Keep writing data to the child until done or too much time
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * elapses with no progress or an error occurs.
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna rv = apr_file_write_full(script_out, data, len, NULL);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (rv != APR_SUCCESS) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* silly script stopped reading, soak up remaining message */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna child_stopped_reading = 1;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_brigade_cleanup(bb);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna while (!seen_eos);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (conf->logname) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna dbuf[dbpos] = '\0';
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* Is this flush really needed? */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_file_flush(script_out);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_file_close(script_out);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* Handle script return... */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (script_in && !nph) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna conn_rec *c = r->connection;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char *location;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna char sbuf[MAX_STRING_LEN];
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna int ret;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna b = apr_bucket_pipe_create(script_in, c->bucket_alloc);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna APR_BRIGADE_INSERT_TAIL(bb, b);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna b = apr_bucket_eos_create(c->bucket_alloc);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna APR_BRIGADE_INSERT_TAIL(bb, b);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if ((ret = ap_scan_script_header_err_brigade(r, bb, sbuf))) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return log_script(r, conf, ret, dbuf, sbuf, bb, script_err);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna location = apr_table_get(r->headers_out, "Location");
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (location && location[0] == '/' && r->status == 200) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna discard_script_output(bb);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_brigade_destroy(bb);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna log_script_err(r, script_err);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* This redirect needs to be a GET no matter what the original
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * method was.
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna r->method = apr_pstrdup(r->pool, "GET");
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna r->method_number = M_GET;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* We already read the message body (if any), so don't allow
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * the redirected request to think it has one. We can ignore
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_unset(r->headers_in, "Content-Length");
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_internal_redirect_handler(location, r);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return OK;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna else if (location && r->status == 200) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* XX Note that if a script wants to produce its own Redirect
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * body, it now has to explicitly *say* "Status: 302"
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna discard_script_output(bb);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_brigade_destroy(bb);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return HTTP_MOVED_TEMPORARILY;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_pass_brigade(r->output_filters, bb);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna log_script_err(r, script_err);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_file_close(script_err);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (script_in && nph) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna conn_rec *c = r->connection;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna struct ap_filter_t *cur;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* get rid of all filters up through protocol... since we
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * haven't parsed off the headers, there is no way they can
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * work
34d7b4e5fca8d933d55caefebd7238b38c928b25pquerna */
34d7b4e5fca8d933d55caefebd7238b38c928b25pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna cur = r->proto_output_filters;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna while (cur && cur->frec->ftype < AP_FTYPE_CONNECTION) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna cur = cur->next;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna r->output_filters = r->proto_output_filters = cur;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna bb = apr_brigade_create(r->pool, c->bucket_alloc);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna b = apr_bucket_pipe_create(script_in, c->bucket_alloc);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna APR_BRIGADE_INSERT_TAIL(bb, b);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna b = apr_bucket_eos_create(c->bucket_alloc);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna APR_BRIGADE_INSERT_TAIL(bb, b);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_pass_brigade(r->output_filters, bb);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return OK; /* NOT r->status, even if it has changed. */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna}
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna/*============================================================================
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna *============================================================================
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * This is the beginning of the cgi filter code moved from mod_include. This
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * is the code required to handle the "exec" SSI directive.
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna *============================================================================
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna *============================================================================*/
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic int include_cgi(char *s, request_rec *r, ap_filter_t *next,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_bucket *head_ptr, apr_bucket **inserted_head)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna request_rec *rr = ap_sub_req_lookup_uri(s, r, next);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna int rr_status;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_bucket *tmp_buck, *tmp2_buck;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (rr->status != HTTP_OK) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_destroy_sub_req(rr);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return -1;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* No hardwired path info or query allowed */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if ((rr->path_info && rr->path_info[0]) || rr->args) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_destroy_sub_req(rr);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return -1;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (rr->finfo.filetype != APR_REG) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_destroy_sub_req(rr);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return -1;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Script gets parameters of the *document*, for back compatibility */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna rr->path_info = r->path_info; /* hard to get right; see mod_cgi.c */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna rr->args = r->args;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Force sub_req to be treated as a CGI request, even if ordinary
c6d33447e28403a90ad817dba4df75fae785be28pquerna * typing rules would have called it something else.
c6d33447e28403a90ad817dba4df75fae785be28pquerna */
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_set_content_type(rr, CGI_MAGIC_TYPE);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Run it. */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna rr_status = ap_run_sub_req(rr);
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (ap_is_HTTP_REDIRECT(rr_status)) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_size_t len_loc;
c6d33447e28403a90ad817dba4df75fae785be28pquerna const char *location = apr_table_get(rr->headers_out, "Location");
conn_rec *c = r->connection;
location = ap_escape_html(rr->pool, location);
len_loc = strlen(location);
/* XXX: if most of this stuff is going to get copied anyway,
* it'd be more efficient to pstrcat it into a single pool buffer
* and a single pool bucket */
tmp_buck = apr_bucket_immortal_create("<A HREF=\"",
sizeof("<A HREF=\"") - 1,
c->bucket_alloc);
APR_BUCKET_INSERT_BEFORE(head_ptr, tmp_buck);
tmp2_buck = apr_bucket_heap_create(location, len_loc, NULL,
c->bucket_alloc);
APR_BUCKET_INSERT_BEFORE(head_ptr, tmp2_buck);
tmp2_buck = apr_bucket_immortal_create("\">", sizeof("\">") - 1,
c->bucket_alloc);
APR_BUCKET_INSERT_BEFORE(head_ptr, tmp2_buck);
tmp2_buck = apr_bucket_heap_create(location, len_loc, NULL,
c->bucket_alloc);
APR_BUCKET_INSERT_BEFORE(head_ptr, tmp2_buck);
tmp2_buck = apr_bucket_immortal_create("</A>", sizeof("</A>") - 1,
c->bucket_alloc);
APR_BUCKET_INSERT_BEFORE(head_ptr, tmp2_buck);
if (*inserted_head == NULL) {
*inserted_head = tmp_buck;
}
}
ap_destroy_sub_req(rr);
return 0;
}
static int include_cmd(include_ctx_t *ctx, apr_bucket_brigade **bb,
const char *command, request_rec *r, ap_filter_t *f)
{
cgi_exec_info_t e_info;
const char **argv;
apr_file_t *script_out = NULL, *script_in = NULL, *script_err = NULL;
apr_bucket_brigade *bcgi;
apr_bucket *b;
apr_status_t rv;
e_info.process_cgi = 0;
e_info.cmd_type = APR_SHELLCMD;
e_info.detached = 0;
e_info.in_pipe = APR_NO_PIPE;
e_info.out_pipe = APR_FULL_BLOCK;
e_info.err_pipe = APR_NO_PIPE;
e_info.prog_type = RUN_AS_SSI;
e_info.bb = bb;
e_info.ctx = ctx;
e_info.next = f->next;
if ((rv = cgi_build_command(&command, &argv, r, r->pool, &e_info)) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"don't know how to spawn cmd child process: %s",
r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* run the script in its own process */
if ((rv = run_cgi_child(&script_out, &script_in, &script_err,
command, argv, r, r->pool,
&e_info)) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"couldn't spawn child process: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
bcgi = apr_brigade_create(r->pool, f->c->bucket_alloc);
b = apr_bucket_pipe_create(script_in, f->c->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bcgi, b);
ap_pass_brigade(f->next, bcgi);
/* We can't close the pipe here, because we may return before the
* full CGI has been sent to the network. That's okay though,
* because we can rely on the pool to close the pipe for us.
*/
return 0;
}
static int handle_exec(include_ctx_t *ctx, apr_bucket_brigade **bb,
request_rec *r, ap_filter_t *f, apr_bucket *head_ptr,
apr_bucket **inserted_head)
{
char *tag = NULL;
char *tag_val = NULL;
char *file = r->filename;
apr_bucket *tmp_buck;
char parsed_string[MAX_STRING_LEN];
*inserted_head = NULL;
if (ctx->flags & FLAG_PRINTING) {
if (ctx->flags & FLAG_NO_EXEC) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"exec used but not allowed in %s", r->filename);
CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
}
else {
while (1) {
cgi_pfn_gtv(ctx, &tag, &tag_val, 1);
if (tag_val == NULL) {
if (tag == NULL) {
return 0;
}
else {
return 1;
}
}
if (!strcmp(tag, "cmd")) {
cgi_pfn_ps(r, ctx, tag_val, parsed_string,
sizeof(parsed_string), 1);
if (include_cmd(ctx, bb, parsed_string, r, f) == -1) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"execution failure for parameter \"%s\" "
"to tag exec in file %s", tag, r->filename);
CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr,
*inserted_head);
}
}
else if (!strcmp(tag, "cgi")) {
apr_status_t retval = APR_SUCCESS;
cgi_pfn_ps(r, ctx, tag_val, parsed_string,
sizeof(parsed_string), 0);
SPLIT_AND_PASS_PRETAG_BUCKETS(*bb, ctx, f->next, retval);
if (retval != APR_SUCCESS) {
return retval;
}
if (include_cgi(parsed_string, r, f->next, head_ptr,
inserted_head) == -1) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"invalid CGI ref \"%s\" in %s",
tag_val, file);
CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr,
*inserted_head);
}
}
else {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"unknown parameter \"%s\" to tag exec in %s",
tag, file);
CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr,
*inserted_head);
}
}
}
}
return 0;
}
/*============================================================================
*============================================================================
* This is the end of the cgi filter code moved from mod_include.
*============================================================================
*============================================================================*/
static int cgi_post_config(apr_pool_t *p, apr_pool_t *plog,
apr_pool_t *ptemp, server_rec *s)
{
cgi_pfn_reg_with_ssi = APR_RETRIEVE_OPTIONAL_FN(ap_register_include_handler);
cgi_pfn_gtv = APR_RETRIEVE_OPTIONAL_FN(ap_ssi_get_tag_and_value);
cgi_pfn_ps = APR_RETRIEVE_OPTIONAL_FN(ap_ssi_parse_string);
if ((cgi_pfn_reg_with_ssi) && (cgi_pfn_gtv) && (cgi_pfn_ps)) {
/* Required by mod_include filter. This is how mod_cgi registers
* with mod_include to provide processing of the exec directive.
*/
cgi_pfn_reg_with_ssi("exec", handle_exec);
}
/* This is the means by which unusual (non-unix) os's may find alternate
* means to run a given command (e.g. shebang/registry parsing on Win32)
*/
cgi_build_command = APR_RETRIEVE_OPTIONAL_FN(ap_cgi_build_command);
if (!cgi_build_command) {
cgi_build_command = default_build_command;
}
return OK;
}
static void register_hooks(apr_pool_t *p)
{
static const char * const aszPre[] = { "mod_include.c", NULL };
ap_hook_handler(cgi_handler, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_post_config(cgi_post_config, aszPre, NULL, APR_HOOK_REALLY_FIRST);
}
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 */
register_hooks /* register hooks */
};