mod_cgi.c revision 10a4cdd68ef1ca0e54af296fe1d08ac00150c90b
/* ====================================================================
* 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.
*/
#define CORE_PRIVATE
#include "apr_strings.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 "http_conf_globals.h"
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
/* 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}
};
int show_errno, char *error)
{
apr_file_t *f = NULL;
char time_str[APR_CTIME_LEN];
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;
}
{
char **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);
#ifdef DEBUG_CGI
for (i = 0; env[i]; ++i)
#endif
/* Transumute ourselves into the script.
* NB only ISINDEX scripts get decoded arguments.
*/
APR_CHILD_BLOCK)) != APR_SUCCESS) ||
#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 {
/* Fill in BUFF structure for parents pipe to child's stdout */
if (!iol)
return APR_EBADF;
/* Fill in BUFF structure for parents pipe to child's stdin */
if (!iol)
return APR_EBADF;
/* Fill in BUFF structure for parents pipe to child's stderr */
if (!iol)
return APR_EBADF;
}
}
return (rc);
}
{
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;
}
{
#ifdef WIN32
char *quoted_filename = NULL;
char *interpreter = NULL;
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 apr_create_process()
*/
if (interpreter && *interpreter) {
else
}
}
else {
}
#else
#endif
return APR_SUCCESS;
}
static int cgi_handler(request_rec *r)
{
char *command;
char argsbuffer[HUGE_STRING_LEN];
apr_pool_t *p;
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");
/* Allow for cgi files without the .EXE extension on them under OS/2 */
if (r->finfo.protection == 0) {
char *newfile;
"script not found or unable to stat");
} else {
}
}
#else
if (r->finfo.protection == 0)
"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, APLOG_NOERRNO,
"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 {
}
}
if (bytes_written < len_read) {
/* 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) {
ap_send_fb(script_in, r);
}
log_script_err(r, script_err);
}
ap_send_fb(script_in, r);
}
return OK; /* NOT r->status, even if it has changed. */
}
static const handler_rec cgi_handlers[] =
{
{"cgi-script", cgi_handler},
{NULL}
};
{
NULL, /* dir config creater */
NULL, /* dir merger --- default is to override */
create_cgi_config, /* server config */
merge_cgi_config, /* merge server config */
cgi_cmds, /* command apr_table_t */
cgi_handlers, /* handlers */
NULL /* register hooks */
};