mod_cgi.c revision 9625528fcf4fa27288f3be080a1979c8ef60d7df
/* ====================================================================
* Copyright (c) 1995-1999 The Apache Group. 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. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* 4. The names "Apache Server" and "Apache Group" 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 names without prior written
* permission of the Apache Group.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``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 GROUP 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 Group and was originally based
* on public domain software written at the National Center for
* Supercomputing Applications, University of Illinois, Urbana-Champaign.
* For more information on the Apache Group and the Apache HTTP server
* project, please see <http://www.apache.org/>.
*
*/
/*
* 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 "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_SYS_STAT_H
#endif
#ifdef HAVE_STRING_H
#include <string.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 {
char *logname;
long logbytes;
int bufbytes;
{
cgi_server_conf *c =
c->logbytes = DEFAULT_LOGBYTES;
c->bufbytes = DEFAULT_BUFBYTES;
return c;
}
{
}
{
return NULL;
}
{
return NULL;
}
{
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)
{
char time_str[AP_CTIME_LEN];
return ret;
}
ap_close(f);
return ret;
}
{
char argsbuffer[HUGE_STRING_LEN];
int i;
char time_str[AP_CTIME_LEN];
/* Soak up script output */
continue;
#ifdef WIN32
/* Soak up stderr and redirect it to the error log.
* Script output to stderr is already directed to the error log
* on Unix, thanks to the magic of fork().
*/
"%s", argsbuffer);
}
#else
continue;
#endif
return ret;
}
ap_puts("%request\n", f);
continue;
}
&& *dbuf) {
}
ap_puts("%response\n", f);
continue;
}
ap_puts("%stdout\n", f);
ap_puts(argsbuffer, f);
ap_puts(argsbuffer, f);
ap_puts("\n", f);
}
ap_puts("%stderr\n", f);
ap_puts(argsbuffer, f);
ap_puts(argsbuffer, f);
ap_puts("\n", f);
}
ap_close(f);
return ret;
}
{
char **env;
#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_FULL_BLOCK) != APR_SUCCESS) ||
/* Something bad happened, tell the world. */
"couldn't create child process: %s", r->filename);
rc = !APR_SUCCESS;
}
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;
*c = 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 ap_create_process()
*/
if (interpreter && *interpreter) {
}
else {
}
#else
#endif
return APR_SUCCESS;
}
static int cgi_handler(request_rec *r)
{
char *command;
char argsbuffer[HUGE_STRING_LEN];
ap_context_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, 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;
}
#ifdef CHARSET_EBCDIC
/* Now check the Content-Type to decide if conversion is needed */
ap_checkconv(r);
#endif /*CHARSET_EBCDIC*/
/* Soak up all the script output */
continue;
}
continue;
}
/* 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 REDIRECT;
}
if (!r->header_only) {
ap_send_fb(script_in, r);
}
continue;
}
}
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 ap_table_t */
cgi_handlers, /* handlers */
NULL /* register hooks */
};