mod_cgi.c revision 445997e06464e7625c7f0e22917f8f2d9876cfff
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000 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 */
#define CORE_PRIVATE
#include "apr_buckets.h"
#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 "http_conf_globals.h"
#include "apr_optional.h"
#include "../filters/mod_include.h"
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
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;
}
{
}
{
return NULL;
}
const char *arg)
{
return NULL;
}
const char *arg)
{
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];
!= APR_SUCCESS)) {
return ret;
}
apr_close(f);
return ret;
}
/* Soak up stderr from a script and redirect it to the error log.
*/
{
char argsbuffer[HUGE_STRING_LEN];
char *newline;
if (newline) {
*newline = '\0';
}
"%s", argsbuffer);
}
}
{
char argsbuffer[HUGE_STRING_LEN];
apr_file_t *f = NULL;
int i;
char time_str[APR_CTIME_LEN];
/* Soak up script output */
continue;
log_script_err(r, script_err);
return ret;
}
apr_puts("%request\n", f);
continue;
}
&& *dbuf) {
}
apr_puts("%response\n", f);
continue;
}
apr_puts("%stdout\n", f);
apr_puts(argsbuffer, f);
apr_puts(argsbuffer, f);
apr_puts("\n", f);
}
apr_puts("%stderr\n", f);
apr_puts(argsbuffer, f);
apr_puts(argsbuffer, f);
apr_puts("\n", f);
}
apr_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) || \
#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) {
/* 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;
}
}
}
return (rc);
}
apr_pool_t *p)
{
char *w;
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. We didn't used to have to do this, but APR wants it.
*/
}
ap_unescape_url(w);
}
return APR_SUCCESS;
}
apr_pool_t *p)
{
#ifdef WIN32
char *quoted_filename = NULL;
char *interpreter = NULL;
#endif
const char *argv0;
/* Allow suexec's "/" check to succeed */
argv0++;
else
#ifdef WIN32
if (fileType == eFileTypeUNKNOWN) {
"%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()
*/
if (interpreter && *interpreter) {
else
}
}
else {
}
#else
#endif
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];
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;
}
argv0++;
else
"Options ExecCGI is off in this directory");
if (nph && is_included)
"attempt to include NPH CGI script");
/* 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
*/
char *newfile;
"script not found or unable to stat");
} else {
}
}
#else
"script not found or unable to stat");
#endif
"attempt to invoke directory as script");
/*
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 */
"couldn't spawn child process: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* build the argument list */
"couldn't 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 {
/* 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 */
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_create_eos();
}
log_script_err(r, script_err);
}
b = apr_bucket_create_eos();
}
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)) {
if (*inserted_head == NULL) {
}
}
return 0;
}
request_rec *r, ap_filter_t *f)
{
const char **argv;
apr_bucket *b;
"couldn't 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\" "
}
}
}
}
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.
*/
}
}
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 */
};