mod_cgi.c revision 7619b51fca6ecf16c28389e52a666e82f3c8d3ef
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* 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"
#include "apr_poll.h"
#define APR_WANT_STRFUNC
#define APR_WANT_MEMFUNC
#include "apr_want.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 "mod_core.h"
#include "mod_cgi.h"
#if defined (RLIMIT_CPU) || defined (RLIMIT_NPROC) || defined (RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
#define AP_CGI_USE_RLIMIT
#endif
#endif
/* Read and discard the data in the brigade produced by a CGI script */
/* 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;
{
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! */
r->pool) != 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);
}
return rv;
}
{
char argsbuffer[HUGE_STRING_LEN];
apr_file_t *f = NULL;
apr_bucket *e;
const char *buf;
int first;
int i;
char time_str[APR_CTIME_LEN];
/* XXX Very expensive mainline case! Open, then getfileinfo! */
r->pool) != APR_SUCCESS)) {
/* Soak up script output */
log_script_err(r, script_err);
return ret;
}
apr_file_puts("%request\n", f);
continue;
}
*dbuf) {
}
apr_file_puts("%response\n", f);
continue;
}
first = 1;
for (e = APR_BRIGADE_FIRST(bb);
e != APR_BRIGADE_SENTINEL(bb);
e = APR_BUCKET_NEXT(e))
{
if (APR_BUCKET_IS_EOS(e)) {
break;
}
break;
}
if (first) {
apr_file_puts("%stdout\n", f);
first = 0;
}
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.
*/
static void add_ssi_vars(request_rec *r)
{
apr_table_t *e = r->subprocess_env;
r->path_info));
r, NULL);
apr_table_setn(e, "PATH_TRANSLATED",
}
}
if (r->args) {
apr_table_setn(e, "QUERY_STRING_UNESCAPED",
}
}
const char *description)
{
/* Escape the logged string because it may be something that
* came in over the network.
*/
"(%d)%pm: %s\n",
err,
&err,
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
#endif
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
)
#endif
);
}
const char *command,
const char * const argv[],
request_rec *r,
apr_pool_t *p,
{
const char * const *env;
#ifdef AP_CGI_USE_RLIMIT
#endif
#ifdef DEBUG_CGI
#ifdef OS2
/* Under OS/2 need to use device con. */
#else
#endif
int i;
#endif
#ifdef DEBUG_CGI
#endif
#ifdef DEBUG_CGI
for (i = 0; env[i]; ++i)
#endif
/* Transmute ourselves into the script.
* NB only ISINDEX scripts get decoded arguments.
*/
r->filename))) != APR_SUCCESS) ||
#if defined(RLIMIT_CPU) && defined(AP_CGI_USE_RLIMIT)
#endif
#if defined(AP_CGI_USE_RLIMIT) && (defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS))
#endif
#if defined(RLIMIT_NPROC) && defined(AP_CGI_USE_RLIMIT)
#endif
/* Something bad happened, tell the world. */
"couldn't set child process attributes: %s", r->filename);
}
else {
procattr, p);
if (rc != APR_SUCCESS) {
/* Bad things happened. Everyone should have cleaned up. */
"couldn't create child process: %d: %s", rc,
}
else {
if (!*script_in)
return APR_EBADF;
if (!*script_out)
return APR_EBADF;
if (!*script_err)
return APR_EBADF;
}
}
}
return (rc);
}
request_rec *r, apr_pool_t *p,
{
char *w;
if (e_info->process_cgi) {
/* Do not process r->args if they contain an '=' assignment
*/
}
}
if (!args) {
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;
}
{
apr_bucket *e;
const char *buf;
for (e = APR_BRIGADE_FIRST(bb);
e != APR_BRIGADE_SENTINEL(bb);
e = APR_BUCKET_NEXT(e))
{
if (APR_BUCKET_IS_EOS(e)) {
break;
}
if (rv != APR_SUCCESS) {
break;
}
}
}
/* A CGI bucket type is needed to catch any output to stderr from the
* script; see PR 22030. */
static const apr_bucket_type_t bucket_type_cgi;
struct cgi_bucket_data {
request_rec *r;
};
/* Create a CGI bucket using pipes from script stdout 'out'
* and stderr 'err', for request 'r'. */
{
APR_BUCKET_INIT(b);
b->free = apr_bucket_free;
b->type = &bucket_type_cgi;
b->start = -1;
/* Create the pollset */
if (rv != APR_SUCCESS) {
"apr_pollset_create(); check system or user limits");
return NULL;
}
if (rv != APR_SUCCESS) {
"apr_pollset_add(); check system or user limits");
return NULL;
}
if (rv != APR_SUCCESS) {
"apr_pollset_add(); check system or user limits");
return NULL;
}
data->r = r;
return b;
}
/* Create a duplicate CGI bucket using given bucket data */
{
APR_BUCKET_INIT(b);
b->free = apr_bucket_free;
b->type = &bucket_type_cgi;
b->start = -1;
return b;
}
/* Handle stdout from CGI child. Duplicate of logic from the _read
* method of the real APR pipe bucket implementation. */
{
char *buf;
return rv;
}
if (*len > 0) {
apr_bucket_heap *h;
/* Change the current bucket to refer to what we read */
h = a->data;
}
else {
a = apr_bucket_immortal_make(a, "", 0);
}
return rv;
}
/* Read method of CGI bucket: polls on stderr and stdout of the child,
* sending any stderr output immediately away to the error log. */
{
int gotdata = 0;
do {
const apr_pollfd_t *results;
if (APR_STATUS_IS_TIMEUP(rv)) {
if (timeout) {
"Timeout waiting for output from CGI script %s",
return rv;
}
else {
return APR_EAGAIN;
}
}
else if (APR_STATUS_IS_EINTR(rv)) {
continue;
}
else if (rv != APR_SUCCESS) {
"poll failed waiting for CGI child");
return rv;
}
/* stdout */
if (APR_STATUS_IS_EOF(rv)) {
rv = APR_SUCCESS;
}
gotdata = 1;
} else {
/* stderr */
if (APR_STATUS_IS_EOF(rv2)) {
}
}
}
} while (!gotdata);
return rv;
}
static const apr_bucket_type_t bucket_type_cgi = {
};
#endif
static int cgi_handler(request_rec *r)
{
int nph;
apr_size_t dbpos = 0;
const char *argv0;
const char *command;
const char **argv;
apr_bucket *b;
int is_included;
apr_pool_t *p;
conn_rec *c;
return DECLINED;
}
c = r->connection;
"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");
if ((r->used_path_info == AP_REQ_REJECT_PATH_INFO) &&
{
/* default to accept */
"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");
}
*/
ap_add_cgi_vars(r);
/* 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.
*/
seen_eos = 0;
dbpos = 0;
}
do {
if (rv != APR_SUCCESS) {
if (APR_STATUS_IS_TIMEUP(rv)) {
"Timeout during reading request entity data");
return HTTP_REQUEST_TIME_OUT;
}
"Error reading request entity data");
}
{
const char *data;
if (APR_BUCKET_IS_EOS(bucket)) {
seen_eos = 1;
break;
}
/* We can't do much with this. */
if (APR_BUCKET_IS_FLUSH(bucket)) {
continue;
}
/* If the child stopped, we still must read to EOS. */
if (child_stopped_reading) {
continue;
}
/* read */
int cursize;
}
else {
}
}
/* Keep writing data to the child until done or too much time
* elapses with no progress or an error occurs.
*/
if (rv != APR_SUCCESS) {
/* silly script stopped reading, soak up remaining message */
}
}
}
while (!seen_eos);
}
/* Is this flush really needed? */
if (b == NULL)
return HTTP_INTERNAL_SERVER_ERROR;
#else
#endif
b = apr_bucket_eos_create(c->bucket_alloc);
/* Handle script return... */
if (!nph) {
const char *location;
char sbuf[MAX_STRING_LEN];
int ret;
{
/*
* ret could be HTTP_NOT_MODIFIED in the case that the CGI script
* does not set an explicit status and ap_meets_conditions, which
* is called by ap_scan_script_header_err_brigade, detects that
* the conditions of the requests are met and the response is
* not modified.
* In this case set r->status and return OK in order to prevent
* running through the error processing stack as this would
* break with mod_cache, if the conditions had been set by
* mod_cache itself to validate a stale entity.
* BTW: We circumvent the error processing stack anyway if the
* CGI script set an explicit status code (whatever it is) and
* the only possible values for ret here are:
*
* HTTP_NOT_MODIFIED (set by ap_meets_conditions)
* HTTP_PRECONDITION_FAILED (set by ap_meets_conditions)
* HTTP_INTERNAL_SERVER_ERROR (if something went wrong during the
* processing of the response of the CGI script, e.g broken headers
* or a crashed CGI process).
*/
if (ret == HTTP_NOT_MODIFIED) {
return OK;
}
return ret;
}
/* For a redirect whether internal or not, discard any
* remaining stdout from the script, and log any remaining
* stderr output, as normal. */
log_script_err(r, script_err);
}
/* This redirect needs to be a GET no matter what the original
* method was.
*/
r->method = "GET";
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;
}
/* XXX: Note that if a script wants to produce its own Redirect
* body, it now has to explicitly *say* "Status: 302"
*/
return HTTP_MOVED_TEMPORARILY;
}
}
else /* nph */ {
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;
}
}
/* don't soak up script output if errors occurred writing it
* out... otherwise, we prolong the life of the script when the
* connection drops or we stopped sending output for some other
* reason */
log_script_err(r, script_err);
}
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.
*============================================================================
*============================================================================*/
apr_bucket_brigade *bb, char *s)
{
request_rec *r = f->r;
int rr_status;
return APR_EGENERAL;
}
/* No hardwired path info or query allowed */
return APR_EGENERAL;
}
return APR_EGENERAL;
}
/* 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 (location) {
char *buffer;
f->c->bucket_alloc));
}
}
return APR_SUCCESS;
}
{
const char **argv;
request_rec *r = f->r;
add_ssi_vars(r);
e_info.process_cgi = 0;
&e_info)) != APR_SUCCESS) {
"don't know how to spawn cmd child process: %s",
r->filename);
return rv;
}
/* run the script in its own process */
&e_info)) != APR_SUCCESS) {
"couldn't spawn child process: %s", r->filename);
return rv;
}
f->c->bucket_alloc));
/* 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 APR_SUCCESS;
}
{
request_rec *r = f->r;
char parsed_string[MAX_STRING_LEN];
? APLOG_ERR : APLOG_WARNING,
0, r, "missing argument for exec element in %s",
r->filename);
}
return APR_SUCCESS;
}
return APR_SUCCESS;
}
"in %s", r->filename);
return APR_SUCCESS;
}
while (1) {
break;
}
if (rv != APR_SUCCESS) {
"for parameter \"%s\" to tag exec in file %s",
break;
}
}
if (rv != APR_SUCCESS) {
break;
}
}
else {
break;
}
}
return APR_SUCCESS;
}
/*============================================================================
*============================================================================
* 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 */
};