mod_cgi.c revision ea94c7eb3ab932fa14a2e5d787ff276553880d13
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend/* ====================================================================
2d0611ffc9f91c5fc2ddccb93f9a3d17791ae650takashi * The Apache Software License, Version 1.1
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend *
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * Copyright (c) 2000 The Apache Software Foundation. All rights
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * reserved.
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem *
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * Redistribution and use in source and binary forms, with or without
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * modification, are permitted provided that the following conditions
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * are met:
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend *
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * 1. Redistributions of source code must retain the above copyright
d9b843d090f14405079b4a61a493316cd3f1e5b9minfrin * notice, this list of conditions and the following disclaimer.
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend *
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes * 2. Redistributions in binary form must reproduce the above copyright
909ce17e2bd0faef7b1c294f2307f009793fd493nd * notice, this list of conditions and the following disclaimer in
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * the documentation and/or other materials provided with the
0ce5630695a7aa568811a9dd0aceedd685f040dend * distribution.
42af92a661a06b3cebc88d585aad75064a309d51nd *
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes * 3. The end-user documentation included with the redistribution,
6fe26506780e73be2a412d758af77fafdf03291and * if any, must include the following acknowledgment:
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * "This product includes software developed by the
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * Apache Software Foundation (http://www.apache.org/)."
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * Alternately, this acknowledgment may appear in the software itself,
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * if and wherever such third-party acknowledgments normally appear.
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend *
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * 4. The names "Apache" and "Apache Software Foundation" must
43f6cc6aa3312619633538b8978619a48c0ce52eminfrin * not be used to endorse or promote products derived from this
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * software without prior written permission. For written
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * permission, please contact apache@apache.org.
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend *
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * 5. Products derived from this software may not be called "Apache",
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * nor may "Apache" appear in their name, without prior written
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * permission of the Apache Software Foundation.
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend *
05ede5110427cb9dc071cc671d5aaba5d3b88c79nd * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
e8b603fa9ccf7b17b11b42df6d8916fd97c2331dnd * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
d6ce05b6521a82cc93da69f7c2116c4a5bc54f8cjim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * SUCH DAMAGE.
03a4ff9ac4c9b8009249010e7c53bb86ff05915and * ====================================================================
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend *
1e367a83311e5243e04d68857d007d04765a48f9chrisd * This software consists of voluntary contributions made by many
1e367a83311e5243e04d68857d007d04765a48f9chrisd * individuals on behalf of the Apache Software Foundation. For more
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * information on the Apache Software Foundation, please see
7d15331eeb5429d7148d13d6fd914a641bf1c000pquerna * <http://www.apache.org/>.
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend *
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * Portions of this software are based upon public domain software
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * originally written at the National Center for Supercomputing Applications,
2704de98885368683621b01c8f8f4e4b01557611takashi * University of Illinois, Urbana-Champaign.
2704de98885368683621b01c8f8f4e4b01557611takashi */
2704de98885368683621b01c8f8f4e4b01557611takashi
1e367a83311e5243e04d68857d007d04765a48f9chrisd/*
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * http_script: keeps all script-related ramblings together.
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend *
d2b809e5d72658bff23819d8b77f20e4939af541nd * Compliant to CGI/1.1 spec
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend *
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * Adapted by rst from original NCSA code by Rob McCool
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend *
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * Apache adds some new env vars; REDIRECT_URL and REDIRECT_QUERY_STRING for
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * custom error responses, and DOCUMENT_ROOT because we found it useful.
27d778df0b517e1578f907d2e51eb961cd8ee5fbjim * It also adds SERVER_ADMIN - useful for scripts to know who to mail when
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * they fail.
6b0fe1f447ba35827cd5cf1d2a703bd8517f33ffmturk */
6b0fe1f447ba35827cd5cf1d2a703bd8517f33ffmturk
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend#include "ap_config.h"
e5ce3ac0e9b720c0fa23782e29168a0810697fdetakashi#include "httpd.h"
f73390048b37c973a8016ffd22108d7a73c6cc22pquerna#include "http_config.h"
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend#include "http_request.h"
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend#include "http_core.h"
0a69d9bb491d9810892a9949c01403a1de3c7ac2nd#include "http_protocol.h"
5f4e50966b2b9b58436a1651cbe588d1b595657ewrowe#include "http_main.h"
623eebe956d9c2d6d073ed3eae855b56030b40e9noodl#include "http_log.h"
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend#include "util_script.h"
79b024b81f6bb3c44dce77a7552191daf8b522d2jim#include "http_conf_globals.h"
4277c95b5a9454a5c25f8d8762634fcff37aa840minfrin#ifdef HAVE_SYS_STAT_H
4277c95b5a9454a5c25f8d8762634fcff37aa840minfrin#include <sys/stat.h>
4277c95b5a9454a5c25f8d8762634fcff37aa840minfrin#endif
f195a136dcd90148b116fdae8ce95b41ba92b87eminfrin#ifdef HAVE_STRING_H
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend#include <string.h>
1de1266f0ea387d6373be8415745dfd2ab876341jim#endif
1de1266f0ea387d6373be8415745dfd2ab876341jim
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fendmodule MODULE_VAR_EXPORT cgi_module;
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend
0ce5630695a7aa568811a9dd0aceedd685f040dend/* KLUDGE --- for back-combatibility, we don't have to check ExecCGI
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * in ScriptAliased directories, which means we need to know if this
10eed2803538d660048f6e733602e82a75ef6885noodl * request came through ScriptAlias or not... so the Alias module
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * leaves a note for us.
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend */
4db28ee269aa06f7c6232e11cd01f58c3349af23noodl
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fendstatic int is_scriptaliased(request_rec *r)
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend{
03c25fb6f628ac81f2ecb637d1e7502dcee783f3nd const char *t = ap_table_get(r->notes, "alias-forced-type");
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend return t && (!strcasecmp(t, "cgi-script"));
0ce5630695a7aa568811a9dd0aceedd685f040dend}
7fa75a06a4fee19e995c069ee00310455d1452e1pquerna
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend/* Configuration stuff */
316f02e3836836c82e19019ff23f90a7ebc65289nilgun
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend#define DEFAULT_LOGBYTES 10385760
0ce5630695a7aa568811a9dd0aceedd685f040dend#define DEFAULT_BUFBYTES 1024
0ce5630695a7aa568811a9dd0aceedd685f040dend
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fendtypedef struct {
char *logname;
long logbytes;
int bufbytes;
} cgi_server_conf;
static void *create_cgi_config(ap_pool_t *p, server_rec *s)
{
cgi_server_conf *c =
(cgi_server_conf *) ap_pcalloc(p, sizeof(cgi_server_conf));
c->logname = NULL;
c->logbytes = DEFAULT_LOGBYTES;
c->bufbytes = DEFAULT_BUFBYTES;
return c;
}
static void *merge_cgi_config(ap_pool_t *p, void *basev, void *overridesv)
{
cgi_server_conf *base = (cgi_server_conf *) basev, *overrides = (cgi_server_conf *) overridesv;
return overrides->logname ? overrides : base;
}
static const char *set_scriptlog(cmd_parms *cmd, void *dummy, char *arg)
{
server_rec *s = cmd->server;
cgi_server_conf *conf =
(cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
conf->logname = arg;
return NULL;
}
static const char *set_scriptlog_length(cmd_parms *cmd, void *dummy, char *arg)
{
server_rec *s = cmd->server;
cgi_server_conf *conf =
(cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
conf->logbytes = atol(arg);
return NULL;
}
static const char *set_scriptlog_buffer(cmd_parms *cmd, void *dummy, char *arg)
{
server_rec *s = cmd->server;
cgi_server_conf *conf =
(cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
conf->bufbytes = atoi(arg);
return NULL;
}
static const command_rec cgi_cmds[] =
{
{"ScriptLog", set_scriptlog, NULL, RSRC_CONF, TAKE1,
"the name of a log for script debugging info"},
{"ScriptLogLength", set_scriptlog_length, NULL, RSRC_CONF, TAKE1,
"the maximum length (in bytes) of the script debug log"},
{"ScriptLogBuffer", set_scriptlog_buffer, NULL, RSRC_CONF, TAKE1,
"the maximum size (in bytes) to record of a POST request"},
{NULL}
};
static int log_scripterror(request_rec *r, cgi_server_conf * conf, int ret,
int show_errno, char *error)
{
ap_file_t *f = NULL;
ap_finfo_t finfo;
char time_str[AP_CTIME_LEN];
ap_log_rerror(APLOG_MARK, show_errno|APLOG_ERR, errno, r,
"%s: %s", error, r->filename);
if (!conf->logname ||
((ap_stat(&finfo, ap_server_root_relative(r->pool, conf->logname), r->pool) == APR_SUCCESS)
&& (finfo.size > conf->logbytes)) ||
(ap_open(&f, ap_server_root_relative(r->pool, conf->logname),
APR_APPEND, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
return ret;
}
/* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
ap_ctime(time_str, ap_now());
ap_fprintf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
r->args ? "?" : "", r->args ? r->args : "", r->protocol);
/* "%% 500 /usr/local/apache/cgi-bin */
ap_fprintf(f, "%%%% %d %s\n", ret, r->filename);
ap_fprintf(f, "%%error\n%s\n", error);
ap_close(f);
return ret;
}
/* Soak up stderr from a script and redirect it to the error log.
*/
static void log_script_err(request_rec *r, BUFF *script_err)
{
char argsbuffer[HUGE_STRING_LEN];
char *newline;
while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0) {
newline = strchr(argsbuffer, '\n');
if (newline) {
*newline = '\0';
}
ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r,
"%s", argsbuffer);
}
}
static int log_script(request_rec *r, cgi_server_conf * conf, int ret,
char *dbuf, const char *sbuf, BUFF *script_in, BUFF *script_err)
{
ap_array_header_t *hdrs_arr = ap_table_elts(r->headers_in);
ap_table_entry_t *hdrs = (ap_table_entry_t *) hdrs_arr->elts;
char argsbuffer[HUGE_STRING_LEN];
ap_file_t *f = NULL;
int i;
ap_finfo_t finfo;
char time_str[AP_CTIME_LEN];
if (!conf->logname ||
((ap_stat(&finfo, ap_server_root_relative(r->pool, conf->logname), r->pool) == APR_SUCCESS)
&& (finfo.size > conf->logbytes)) ||
(ap_open(&f, ap_server_root_relative(r->pool, conf->logname),
APR_APPEND, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
/* Soak up script output */
while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_in) > 0)
continue;
log_script_err(r, script_err);
return ret;
}
/* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
ap_ctime(time_str, ap_now());
ap_fprintf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
r->args ? "?" : "", r->args ? r->args : "", r->protocol);
/* "%% 500 /usr/local/apache/cgi-bin" */
ap_fprintf(f, "%%%% %d %s\n", ret, r->filename);
ap_puts("%request\n", f);
for (i = 0; i < hdrs_arr->nelts; ++i) {
if (!hdrs[i].key)
continue;
ap_fprintf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
}
if ((r->method_number == M_POST || r->method_number == M_PUT)
&& *dbuf) {
ap_fprintf(f, "\n%s\n", dbuf);
}
ap_puts("%response\n", f);
hdrs_arr = ap_table_elts(r->err_headers_out);
hdrs = (ap_table_entry_t *) hdrs_arr->elts;
for (i = 0; i < hdrs_arr->nelts; ++i) {
if (!hdrs[i].key)
continue;
ap_fprintf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
}
if (sbuf && *sbuf)
ap_fprintf(f, "%s\n", sbuf);
if (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_in) > 0) {
ap_puts("%stdout\n", f);
ap_puts(argsbuffer, f);
while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_in) > 0)
ap_puts(argsbuffer, f);
ap_puts("\n", f);
}
if (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0) {
ap_puts("%stderr\n", f);
ap_puts(argsbuffer, f);
while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0)
ap_puts(argsbuffer, f);
ap_puts("\n", f);
}
ap_bclose(script_in);
ap_bclose(script_err);
ap_close(f);
return ret;
}
static ap_status_t run_cgi_child(BUFF **script_out, BUFF **script_in, BUFF **script_err,
char *command, char *const argv[], request_rec *r, ap_pool_t *p)
{
char **env;
ap_procattr_t *procattr;
ap_proc_t procnew;
ap_status_t rc = APR_SUCCESS;
ap_file_t *file = NULL;
ap_iol *iol;
#ifdef DEBUG_CGI
#ifdef OS2
/* Under OS/2 need to use device con. */
FILE *dbg = fopen("con", "w");
#else
FILE *dbg = fopen("/dev/tty", "w");
#endif
int i;
#endif
RAISE_SIGSTOP(CGI_CHILD);
#ifdef DEBUG_CGI
fprintf(dbg, "Attempting to exec %s as %sCGI child (argv0 = %s)\n",
r->filename, cld->nph ? "NPH " : "", argv0);
#endif
ap_add_cgi_vars(r);
env = ap_create_environment(p, r->subprocess_env);
#ifdef DEBUG_CGI
fprintf(dbg, "Environment: \n");
for (i = 0; env[i]; ++i)
fprintf(dbg, "'%s'\n", env[i]);
#endif
/* Transumute ourselves into the script.
* NB only ISINDEX scripts get decoded arguments.
*/
if (((rc = ap_createprocattr_init(&procattr, p)) != APR_SUCCESS) ||
((rc = ap_setprocattr_io(procattr,
APR_CHILD_BLOCK,
APR_CHILD_BLOCK,
APR_CHILD_BLOCK)) != APR_SUCCESS) ||
((rc = ap_setprocattr_dir(procattr,
ap_make_dirstr_parent(r->pool, r->filename))) != APR_SUCCESS) ||
((rc = ap_setprocattr_cmdtype(procattr, APR_PROGRAM)) != APR_SUCCESS)) {
/* Something bad happened, tell the world. */
ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r,
"couldn't create child process: %s", r->filename);
}
else {
rc = ap_create_process(&procnew, command, argv, env, procattr, p);
if (rc != APR_SUCCESS) {
/* Bad things happened. Everyone should have cleaned up. */
ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r,
"couldn't create child process: %d: %s", rc, r->filename);
}
else {
ap_note_subprocess(p, &procnew, kill_after_timeout);
/* Fill in BUFF structure for parents pipe to child's stdout */
file = procnew.out;
iol = ap_create_file_iol(file);
if (!iol)
return APR_EBADF;
*script_in = ap_bcreate(p, B_RD);
ap_bpush_iol(*script_in, iol);
ap_bsetopt(*script_in, BO_TIMEOUT, &r->server->timeout);
/* Fill in BUFF structure for parents pipe to child's stdin */
file = procnew.in;
iol = ap_create_file_iol(file);
if (!iol)
return APR_EBADF;
*script_out = ap_bcreate(p, B_WR);
ap_bpush_iol(*script_out, iol);
ap_bsetopt(*script_out, BO_TIMEOUT, &r->server->timeout);
/* Fill in BUFF structure for parents pipe to child's stderr */
file = procnew.err;
iol = ap_create_file_iol(file);
if (!iol)
return APR_EBADF;
*script_err = ap_bcreate(p, B_RD);
ap_bpush_iol(*script_err, iol);
ap_bsetopt(*script_err, BO_TIMEOUT, &r->server->timeout);
}
}
return (rc);
}
static ap_status_t build_argv_list(char ***argv, request_rec *r, ap_pool_t *p)
{
int numwords, x, idx;
char *w;
const char *args = r->args;
if (!args || !args[0] || strchr(args, '=')) {
numwords = 1;
}
else {
/* count the number of keywords */
for (x = 0, numwords = 2; args[x]; x++) {
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.
*/
if (numwords > APACHE_ARG_MAX - 1) {
numwords = APACHE_ARG_MAX - 1; /* Truncate args to prevent overrun */
}
*argv = (char **) ap_palloc(p, (numwords + 2) * sizeof(char *));
for (x = 1, idx = 1; x < numwords; x++) {
w = ap_getword_nulls(p, &args, '+');
ap_unescape_url(w);
(*argv)[idx++] = ap_escape_shell_cmd(p, w);
}
(*argv)[idx] = NULL;
return APR_SUCCESS;
}
static ap_status_t build_command_line(char **c, request_rec *r, ap_pool_t *p)
{
#ifdef WIN32
char *quoted_filename = NULL;
char *interpreter = NULL;
file_type_e fileType;
*c = NULL;
fileType = ap_get_win32_interpreter(r, &interpreter);
if (fileType == eFileTypeUNKNOWN) {
ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r,
"%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()
*/
quoted_filename = ap_pstrcat(p, "\"", r->filename, "\"", NULL);
if (interpreter && *interpreter) {
*c = ap_pstrcat(p, interpreter, " ", quoted_filename, " ", NULL);
}
else {
*c = ap_pstrcat(p, quoted_filename, " ", NULL);
}
#else
*c = ap_pstrcat(p, r->filename, NULL);
#endif
return APR_SUCCESS;
}
static int cgi_handler(request_rec *r)
{
int retval, nph, dbpos = 0;
char *argv0, *dbuf = NULL;
char *command;
char **argv = NULL;
BUFF *script_out = NULL, *script_in = NULL, *script_err = NULL;
char argsbuffer[HUGE_STRING_LEN];
int is_included = !strcmp(r->protocol, "INCLUDED");
void *sconf = r->server->module_config;
ap_pool_t *p;
cgi_server_conf *conf =
(cgi_server_conf *) ap_get_module_config(sconf, &cgi_module);
p = r->main ? r->main->pool : r->pool;
if (r->method_number == M_OPTIONS) {
/* 99 out of 100 CGI scripts, this is all they support */
r->allowed |= (1 << M_GET);
r->allowed |= (1 << M_POST);
return DECLINED;
}
if ((argv0 = strrchr(r->filename, '/')) != NULL)
argv0++;
else
argv0 = r->filename;
nph = !(strncmp(argv0, "nph-", 4));
if (!(ap_allow_options(r) & OPT_EXECCGI) && !is_scriptaliased(r))
return log_scripterror(r, conf, FORBIDDEN, APLOG_NOERRNO,
"Options ExecCGI is off in this directory");
if (nph && is_included)
return log_scripterror(r, conf, FORBIDDEN, APLOG_NOERRNO,
"attempt to include NPH CGI script");
#if defined(OS2) || defined(WIN32)
/* Allow for cgi files without the .EXE extension on them under OS/2 */
if (r->finfo.protection == 0) {
ap_finfo_t finfo;
char *newfile;
newfile = ap_pstrcat(r->pool, r->filename, ".EXE", NULL);
if ((ap_stat(&finfo, newfile, r->pool) != APR_SUCCESS) ||
(finfo.filetype != APR_REG)) {
return log_scripterror(r, conf, NOT_FOUND, 0,
"script not found or unable to stat");
} else {
r->filename = newfile;
}
}
#else
if (r->finfo.protection == 0)
return log_scripterror(r, conf, NOT_FOUND, APLOG_NOERRNO,
"script not found or unable to stat");
#endif
if (r->finfo.filetype == APR_DIR)
return log_scripterror(r, conf, FORBIDDEN, APLOG_NOERRNO,
"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");
}
*/
if ((retval = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)))
return retval;
ap_add_common_vars(r);
/* build the command line */
if (build_command_line(&command, r, p) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
"couldn't spawn child process: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* build the argument list */
else if (build_argv_list(&argv, r, p) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
"couldn't spawn child process: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
argv[0] = ap_pstrdup(p, command);
/* run the script in its own process */
if (run_cgi_child(&script_out, &script_in, &script_err, command, argv, r, p) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
"couldn't spawn child process: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* Transfer any put/post args, CERN style...
* Note that we already ignore SIGPIPE in the core server.
*/
if (ap_should_client_block(r)) {
int dbsize, len_read;
ap_ssize_t bytes_written;
if (conf->logname) {
dbuf = ap_pcalloc(r->pool, conf->bufbytes + 1);
dbpos = 0;
}
while ((len_read =
ap_get_client_block(r, argsbuffer, HUGE_STRING_LEN)) > 0) {
if (conf->logname) {
if ((dbpos + len_read) > conf->bufbytes) {
dbsize = conf->bufbytes - dbpos;
}
else {
dbsize = len_read;
}
memcpy(dbuf + dbpos, argsbuffer, dbsize);
dbpos += dbsize;
}
(void) ap_bwrite(script_out, argsbuffer, len_read, &bytes_written);
if (bytes_written < len_read) {
/* silly script stopped reading, soak up remaining message */
while (ap_get_client_block(r, argsbuffer, HUGE_STRING_LEN) > 0) {
/* dump it */
}
break;
}
}
ap_bflush(script_out);
}
ap_bclose(script_out);
/* Handle script return... */
if (script_in && !nph) {
const char *location;
char sbuf[MAX_STRING_LEN];
int ret;
if ((ret = ap_scan_script_header_err_buff(r, script_in, sbuf))) {
return log_script(r, conf, ret, dbuf, sbuf, script_in, script_err);
}
#ifdef CHARSET_EBCDIC
/* Now check the Content-Type to decide if conversion is needed */
ap_checkconv(r);
#endif /*CHARSET_EBCDIC*/
location = ap_table_get(r->headers_out, "Location");
if (location && location[0] == '/' && r->status == 200) {
/* Soak up all the script output */
while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_in) > 0) {
continue;
}
log_script_err(r, script_err);
/* This redirect needs to be a GET no matter what the original
* method was.
*/
r->method = ap_pstrdup(r->pool, "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.
*/
ap_table_unset(r->headers_in, "Content-Length");
ap_internal_redirect_handler(location, r);
return OK;
}
else if (location && r->status == 200) {
/* XX Note that if a script wants to produce its own Redirect
* body, it now has to explicitly *say* "Status: 302"
*/
return REDIRECT;
}
ap_send_http_header(r);
if (!r->header_only) {
ap_send_fb(script_in, r);
}
ap_bclose(script_in);
log_script_err(r, script_err);
ap_bclose(script_err);
}
if (script_in && nph) {
ap_send_fb(script_in, r);
}
return OK; /* NOT r->status, even if it has changed. */
}
static const handler_rec cgi_handlers[] =
{
{CGI_MAGIC_TYPE, cgi_handler},
{"cgi-script", cgi_handler},
{NULL}
};
module MODULE_VAR_EXPORT cgi_module =
{
STANDARD20_MODULE_STUFF,
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 */
};