mod_cgi.c revision e0d102c882a7ed34d3eec24b36da49f097066a36
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
/*
* http_script: keeps all script-related ramblings together.
*
* Compliant to CGI/1.1 spec
*
* Adapted by rst from original NCSA code by Rob McCool
*
* Apache adds some new env vars; REDIRECT_URL and REDIRECT_QUERY_STRING for
* custom error responses, and DOCUMENT_ROOT because we found it useful.
* It also adds SERVER_ADMIN - useful for scripts to know who to mail when
* they fail.
*/
#include "apr.h"
#include "apr_strings.h"
#include "apr_thread_proc.h" /* for RLIMIT stuff */
#include "apr_optional.h"
#include "apr_buckets.h"
#include "apr_lib.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"
#define CORE_PRIVATE
#include "util_filter.h"
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_request.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_main.h"
#include "http_log.h"
#include "util_script.h"
#include "ap_mpm.h"
#include "mod_core.h"
#include "../filters/mod_include.h"
#include "mod_cgi.h"
typedef struct {
} exec_info;
/* KLUDGE --- for back-combatibility, we don't have to check ExecCGI
* in ScriptAliased directories, which means we need to know if this
* request came through ScriptAlias or not... so the Alias module
* leaves a note for us.
*/
static int is_scriptaliased(request_rec *r)
{
return t && (!strcasecmp(t, "cgi-script"));
}
/* Configuration stuff */
#define DEFAULT_LOGBYTES 10385760
#define DEFAULT_BUFBYTES 1024
typedef struct {
const char *logname;
long logbytes;
int bufbytes;
{
cgi_server_conf *c =
c->logbytes = DEFAULT_LOGBYTES;
c->bufbytes = DEFAULT_BUFBYTES;
return c;
}
{
}
{
&cgi_module);
}
return NULL;
}
const char *arg)
{
&cgi_module);
return NULL;
}
const char *arg)
{
&cgi_module);
return NULL;
}
static const command_rec cgi_cmds[] =
{
"the name of a log for script debugging info"),
"the maximum length (in bytes) of the script debug log"),
"the maximum size (in bytes) to record of a POST request"),
{NULL}
};
{
apr_file_t *f = NULL;
char time_str[APR_CTIME_LEN];
/* XXX Very expensive mainline case! Open, then getfileinfo! */
!= APR_SUCCESS)) {
return ret;
}
apr_file_close(f);
return ret;
}
/* Soak up stderr from a script and redirect it to the error log.
*/
{
char argsbuffer[HUGE_STRING_LEN];
char *newline;
script_err) == APR_SUCCESS) {
if (newline) {
*newline = '\0';
}
"%s", argsbuffer);
}
}
{
char argsbuffer[HUGE_STRING_LEN];
apr_file_t *f = NULL;
int i;
char time_str[APR_CTIME_LEN];
/* XXX Very expensive mainline case! Open, then getfileinfo! */
/* Soak up script output */
script_in) == APR_SUCCESS)
continue;
log_script_err(r, script_err);
return ret;
}
apr_file_puts("%request\n", f);
continue;
}
&& *dbuf) {
}
apr_file_puts("%response\n", f);
continue;
}
apr_file_puts("%stdout\n", f);
apr_file_puts(argsbuffer, f);
script_in) == APR_SUCCESS)
apr_file_puts(argsbuffer, f);
apr_file_puts("\n", f);
}
apr_file_puts("%stderr\n", f);
apr_file_puts(argsbuffer, f);
script_err) == APR_SUCCESS)
apr_file_puts(argsbuffer, f);
apr_file_puts("\n", f);
}
apr_file_close(f);
return ret;
}
/* This is the special environment used for running the "exec cmd="
* variety of SSI directives.
*/
{
apr_table_t *e = r->subprocess_env;
apr_table_setn(e, "PATH_TRANSLATED",
}
}
if (r->args) {
}
}
const char *command,
const char * const argv[],
request_rec *r,
apr_pool_t *p,
{
const char * const *env;
#if defined(RLIMIT_CPU) || defined(RLIMIT_NPROC) || \
&core_module);
#endif
#ifdef DEBUG_CGI
#ifdef OS2
/* Under OS/2 need to use device con. */
#else
#endif
int i;
#endif
#ifdef DEBUG_CGI
#endif
ap_add_cgi_vars(r);
}
else /* SSIs want a controlled environment and a special path. */
{
}
#ifdef DEBUG_CGI
for (i = 0; env[i]; ++i)
#endif
/* Transmute ourselves into the script.
* NB only ISINDEX scripts get decoded arguments.
*/
#ifdef RLIMIT_CPU
#endif
#endif
#ifdef RLIMIT_NPROC
#endif
/* Something bad happened, tell the world. */
"couldn't set child process attributes: %s", r->filename);
}
else {
if (rc != APR_SUCCESS) {
return rc;
}
}
if (rc != APR_SUCCESS) {
/* Bad things happened. Everyone should have cleaned up. */
}
else {
if (!*script_in)
return APR_EBADF;
if (!*script_out)
return APR_EBADF;
if (!*script_err)
return APR_EBADF;
}
}
}
#ifdef DEBUG_CGI
#endif
return (rc);
}
request_rec *r, apr_pool_t *p,
int replace_cmd)
{
char *w;
const char *argv0;
if (replace_cmd) {
/* Allow suexec's "/" check to succeed */
argv0++;
else
}
numwords = 1;
}
else {
/* count the number of keywords */
if (args[x] == '+') {
++numwords;
}
}
}
/* Everything is - 1 to account for the first parameter
* which is the program name.
*/
}
ap_unescape_url(w);
}
return APR_SUCCESS;
}
static int cgi_handler(request_rec *r)
{
const char *argv0;
const char *command;
const char **argv;
apr_bucket *b;
char argsbuffer[HUGE_STRING_LEN];
int is_included;
apr_pool_t *p;
return DECLINED;
if (r->method_number == M_OPTIONS) {
/* 99 out of 100 CGI scripts, this is all they support */
return DECLINED;
}
"Options ExecCGI is off in this directory");
if (nph && is_included)
"attempt to include NPH CGI script");
"script not found or unable to stat");
"attempt to invoke directory as script");
"AcceptPathInfo off disallows user's path");
}
/*
if (!ap_suexec_enabled) {
if (!ap_can_exec(&r->finfo))
return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
"file permissions deny server execution");
}
*/
return retval;
/* build the command line */
"don't know how to spawn child process: %s",
r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* run the script in its own process */
"couldn't spawn child process: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
* Note that we already ignore SIGPIPE in the core server.
*/
if (ap_should_client_block(r)) {
dbpos = 0;
}
while ((len_read =
}
else {
}
}
/* Keep writing data to the child until done or too much time
* elapses with no progress or an error occurs.
*/
bytes_written = 0;
do {
} while (rv == APR_SUCCESS
/* silly script stopped reading, soak up remaining message */
/* dump it */
}
break;
}
}
}
/* Handle script return... */
const char *location;
char sbuf[MAX_STRING_LEN];
int ret;
}
/* Soak up all the script output */
script_in) == APR_SUCCESS) {
continue;
}
log_script_err(r, script_err);
/* This redirect needs to be a GET no matter what the original
* method was.
*/
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.
*/
return OK;
}
/* 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;
}
if (!r->header_only) {
b = apr_bucket_eos_create();
}
log_script_err(r, script_err);
}
struct ap_filter_t *cur;
/* get rid of all filters up through protocol... since we
* haven't parsed off the headers, there is no way they can
* work
*/
cur = r->proto_output_filters;
}
b = apr_bucket_eos_create();
}
return OK; /* NOT r->status, even if it has changed. */
}
/*============================================================================
*============================================================================
* This is the beginning of the cgi filter code moved from mod_include. This
* is the code required to handle the "exec" SSI directive.
*============================================================================
*============================================================================*/
{
int rr_status;
return -1;
}
/* No hardwired path info or query allowed */
return -1;
}
return -1;
}
/* Script gets parameters of the *document*, for back compatibility */
/* Force sub_req to be treated as a CGI request, even if ordinary
* typing rules would have called it something else.
*/
/* Run it. */
if (ap_is_HTTP_REDIRECT(rr_status)) {
/* 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 */
sizeof("<A HREF=\"") - 1);
if (*inserted_head == NULL) {
}
}
return 0;
}
{
const char **argv;
apr_bucket *b;
"don't know how to spawn cmd child process: %s",
r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* run the script in its own process */
"couldn't spawn child process: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* 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;
}
{
char parsed_string[MAX_STRING_LEN];
*inserted_head = NULL;
"exec used but not allowed in %s", r->filename);
}
else {
while (1) {
return (0);
}
else {
return 1;
}
}
"execution failure for parameter \"%s\" "
}
}
if (retval != APR_SUCCESS) {
return retval;
}
}
}
else {
}
}
}
}
return 0;
}
/*============================================================================
*============================================================================
* This is the end of the cgi filter code moved from mod_include.
*============================================================================
*============================================================================*/
{
/* Required by mod_include filter. This is how mod_cgi registers
* with mod_include to provide processing of the exec directive.
*/
}
/* This is the means by which unusual (non-unix) os's may find alternate
*/
if (!cgi_build_command) {
}
return OK;
}
static void register_hooks(apr_pool_t *p)
{
}
{
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 */
};