mod_cgi.c revision 15cdcf089057a76a4dbda4d7f22e1ab69ebe051a
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend/* ====================================================================
2d0611ffc9f91c5fc2ddccb93f9a3d17791ae650takashi * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend *
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * Redistribution and use in source and binary forms, with or without
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * modification, are permitted provided that the following conditions
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem * are met:
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend *
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * 1. Redistributions of source code must retain the above copyright
b4a00883f358625923365ca1560c96edec172a52sf * notice, this list of conditions and the following disclaimer.
50c04f297d76a57ead2fa6b73845f7563b1fc788sf *
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * 2. Redistributions in binary form must reproduce the above copyright
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * notice, this list of conditions and the following disclaimer in
d9b843d090f14405079b4a61a493316cd3f1e5b9minfrin * the documentation and/or other materials provided with the
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * distribution.
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes *
909ce17e2bd0faef7b1c294f2307f009793fd493nd * 3. All advertising materials mentioning features or use of this
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * software must display the following acknowledgment:
0ce5630695a7aa568811a9dd0aceedd685f040dend * "This product includes software developed by the Apache Group
a29610af88e278144045bfa1bc63b4a1a4b5ff14trawick * for use in the Apache HTTP server project (http://www.apache.org/)."
42af92a661a06b3cebc88d585aad75064a309d51nd *
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes * 4. The names "Apache Server" and "Apache Group" must not be used to
6fe26506780e73be2a412d758af77fafdf03291and * endorse or promote products derived from this software without
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * prior written permission. For written permission, please contact
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * apache@apache.org.
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend *
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * 5. Products derived from this software may not be called "Apache"
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * nor may "Apache" appear in their names without prior written
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * permission of the Apache Group.
43f6cc6aa3312619633538b8978619a48c0ce52eminfrin *
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * 6. Redistributions of any form whatsoever must retain the following
78f97ce162b66a0dbfd7af4dcd9984f162569b04minfrin * acknowledgment:
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * "This product includes software developed by the Apache Group
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * for use in the Apache HTTP server project (http://www.apache.org/)."
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend *
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
7906201913b68fe78b9d6a22ab33bf21d82c490eminfrin * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
05ede5110427cb9dc071cc671d5aaba5d3b88c79nd * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
e8b603fa9ccf7b17b11b42df6d8916fd97c2331dnd * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2f39535ac2206b346dcfbcaf271e3addc901f520rbowen * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
d6ce05b6521a82cc93da69f7c2116c4a5bc54f8cjim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * OF THE POSSIBILITY OF SUCH DAMAGE.
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * ====================================================================
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend *
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * This software consists of voluntary contributions made by many
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * individuals on behalf of the Apache Group and was originally based
03a4ff9ac4c9b8009249010e7c53bb86ff05915and * on public domain software written at the National Center for
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * Supercomputing Applications, University of Illinois, Urbana-Champaign.
1e367a83311e5243e04d68857d007d04765a48f9chrisd * For more information on the Apache Group and the Apache HTTP server
1e367a83311e5243e04d68857d007d04765a48f9chrisd * project, please see <http://www.apache.org/>.
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend *
7d15331eeb5429d7148d13d6fd914a641bf1c000pquerna */
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend/*
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * http_script: keeps all script-related ramblings together.
2704de98885368683621b01c8f8f4e4b01557611takashi *
2704de98885368683621b01c8f8f4e4b01557611takashi * Compliant to CGI/1.1 spec
2704de98885368683621b01c8f8f4e4b01557611takashi *
1e367a83311e5243e04d68857d007d04765a48f9chrisd * 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
c819c19c2f1ffbf3a3f12a4070cc6c3f4ea2a6f2sf * custom error responses, and DOCUMENT_ROOT because we found it useful.
d2b809e5d72658bff23819d8b77f20e4939af541nd * It also adds SERVER_ADMIN - useful for scripts to know who to mail when
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * they fail.
fb51a6b789d85113d0976148685b0063c294220drbowen */
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend#include "httpd.h"
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend#include "http_config.h"
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend#include "http_request.h"
27d778df0b517e1578f907d2e51eb961cd8ee5fbjim#include "http_core.h"
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend#include "http_protocol.h"
6b0fe1f447ba35827cd5cf1d2a703bd8517f33ffmturk#include "http_main.h"
6b0fe1f447ba35827cd5cf1d2a703bd8517f33ffmturk#include "http_log.h"
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend#include "util_script.h"
438b4817913a5ff55d9cad4c7ddf133330b4466ejim#include "http_conf_globals.h"
e5ce3ac0e9b720c0fa23782e29168a0810697fdetakashi
f73390048b37c973a8016ffd22108d7a73c6cc22pquernamodule MODULE_VAR_EXPORT cgi_module;
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend
4126704c4950bfd46d32ad54e3b106ac6d868a73sf/* KLUDGE --- for back-combatibility, we don't have to check ExecCGI
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend * in ScriptAliased directories, which means we need to know if this
0a69d9bb491d9810892a9949c01403a1de3c7ac2nd * request came through ScriptAlias or not... so the Alias module
ebdfe3e677aaa92fe2df702295eac81a97a4610frbowen * leaves a note for us.
68c4447ba8e057cf38cbbec918e0549b817f20b4minfrin */
5f4e50966b2b9b58436a1651cbe588d1b595657ewrowe
5332c9934b1c0136e9cd4815fe7d70b79d83fddcsfstatic int is_scriptaliased(request_rec *r)
623eebe956d9c2d6d073ed3eae855b56030b40e9noodl{
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend const char *t = ap_table_get(r->notes, "alias-forced-type");
79b024b81f6bb3c44dce77a7552191daf8b522d2jim return t && (!strcasecmp(t, "cgi-script"));
4277c95b5a9454a5c25f8d8762634fcff37aa840minfrin}
4277c95b5a9454a5c25f8d8762634fcff37aa840minfrin
4277c95b5a9454a5c25f8d8762634fcff37aa840minfrin/* Configuration stuff */
f195a136dcd90148b116fdae8ce95b41ba92b87eminfrin
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend#define DEFAULT_LOGBYTES 10385760
1de1266f0ea387d6373be8415745dfd2ab876341jim#define DEFAULT_BUFBYTES 1024
1de1266f0ea387d6373be8415745dfd2ab876341jim
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fendtypedef struct {
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend char *logname;
0ce5630695a7aa568811a9dd0aceedd685f040dend long logbytes;
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend int bufbytes;
10eed2803538d660048f6e733602e82a75ef6885noodl} cgi_server_conf;
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fendstatic void *create_cgi_config(ap_context_t *p, server_rec *s)
4db28ee269aa06f7c6232e11cd01f58c3349af23noodl{
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend cgi_server_conf *c =
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend (cgi_server_conf *) ap_pcalloc(p, sizeof(cgi_server_conf));
03c25fb6f628ac81f2ecb637d1e7502dcee783f3nd
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend c->logname = NULL;
4126704c4950bfd46d32ad54e3b106ac6d868a73sf c->logbytes = DEFAULT_LOGBYTES;
0ce5630695a7aa568811a9dd0aceedd685f040dend c->bufbytes = DEFAULT_BUFBYTES;
7fa75a06a4fee19e995c069ee00310455d1452e1pquerna
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend return c;
316f02e3836836c82e19019ff23f90a7ebc65289nilgun}
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend
0ce5630695a7aa568811a9dd0aceedd685f040dendstatic void *merge_cgi_config(ap_context_t *p, void *basev, void *overridesv)
0ce5630695a7aa568811a9dd0aceedd685f040dend{
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend 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;
struct stat finfo;
ap_log_rerror(APLOG_MARK, show_errno|APLOG_ERR, r,
"%s: %s", error, r->filename);
if (!conf->logname ||
((stat(ap_server_root_relative(r->pool, conf->logname), &finfo) == 0)
&& (finfo.st_size > conf->logbytes)) ||
(ap_open(&f, ap_server_root_relative(r->pool, conf->logname),
APR_APPEND | APR_BUFFERED, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
return ret;
}
/* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
ap_fprintf(f, "%%%% [%s] %s %s%s%s %s\n", ap_get_time(), 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;
}
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;
int i;
struct stat finfo;
if (!conf->logname ||
((stat(ap_server_root_relative(r->pool, conf->logname), &finfo) == 0)
&& (finfo.st_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;
#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().
*/
while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0) {
ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, r,
"%s", argsbuffer);
}
#else
while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0)
continue;
#endif
return ret;
}
/* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
ap_fprintf(f, "%%%% [%s] %s %s%s%s %s\n", ap_get_time(), 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_context_t *p)
{
char **env;
ap_procattr_t *procattr;
ap_proc_t *procnew;
ap_os_proc_t fred;
ap_status_t rc = APR_SUCCESS;
ap_file_t *file;
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
ap_block_alarms();
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.
*/
ap_cleanup_for_exec();
if ((ap_createprocattr_init(&procattr, p) != APR_SUCCESS) ||
(ap_setprocattr_io(procattr,
script_in ? 1 : 0,
script_out ? 1 : 0,
script_err ? 1 : 0) != APR_SUCCESS) ||
(ap_setprocattr_dir(procattr, ap_make_dirstr_parent(r->pool, r->filename)) != APR_SUCCESS) ||
(ap_setprocattr_cmdtype(procattr, APR_PROGRAM) != APR_SUCCESS)) {
/* Something bad happened, tell the world. */
ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
"couldn't create child process: %s", r->filename);
rc = !APR_SUCCESS;
}
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, r,
"couldn't create child process: %d: %s", rc, r->filename);
}
else {
#ifndef WIN32
/* pjr - this is a cheap hack for now to get the basics working in
* stages. ap_note_subprocess and free_proc need to be redone
* to make use of ap_proc_t instead of pid.
*/
ap_get_os_proc(&fred, procnew);
ap_note_subprocess(p, fred, kill_after_timeout);
#endif
if (script_in) {
ap_get_childout(&file, procnew);
iol = ap_create_file_iol(file);
if (!iol)
return APR_EBADF;
*script_in = ap_bcreate(p, B_RD);
ap_bpush_iol(*script_in, iol);
}
if (script_out) {
ap_get_childin(&file, procnew);
iol = ap_create_file_iol(file);
if (!iol)
return APR_EBADF;
*script_out = ap_bcreate(p, B_WR);
ap_bpush_iol(*script_out, iol);
}
if (script_err) {
ap_get_childerr(&file, procnew);
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_unblock_alarms();
return (rc);
}
static ap_status_t build_argv_list(char *argv[], request_rec *r, ap_context_t *p)
{
int numwords, x, idx;
char *w;
const char *args = r->args;
if (!args || !args[0] || strchr(args, '=')) {
*argv = NULL;
}
else {
/* count the number of keywords */
for (x = 0, numwords = 1; args[x]; x++) {
if (args[x] == '+') {
++numwords;
}
}
if (numwords > APACHE_ARG_MAX) {
numwords = APACHE_ARG_MAX; /* Truncate args to prevent overrun */
}
argv = (char **) ap_palloc(p, (numwords + 1) * sizeof(char *));
for (x = 1, idx = 0; 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_context_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, 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, *script_in, *script_err;
char argsbuffer[HUGE_STRING_LEN];
int is_included = !strcmp(r->protocol, "INCLUDED");
void *sconf = r->server->module_config;
ap_context_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.st_mode == 0) {
struct stat statbuf;
char *newfile;
newfile = ap_pstrcat(r->pool, r->filename, ".EXE", NULL);
if ((stat(newfile, &statbuf) != 0) || (!S_ISREG(statbuf.st_mode))) {
return log_scripterror(r, conf, NOT_FOUND, 0,
"script not found or unable to stat");
} else {
r->filename = newfile;
}
}
#else
if (r->finfo.st_mode == 0)
return log_scripterror(r, conf, NOT_FOUND, APLOG_NOERRNO,
"script not found or unable to stat");
#endif
if (S_ISDIR(r->finfo.st_mode))
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, 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, r,
"couldn't spawn child process: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* run the script in its own process */
else if (run_cgi_child(&script_out, &script_in, &script_err, command, &argv, r, p) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 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;
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;
}
if (ap_bwrite(script_out, argsbuffer, len_read) < 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;
}
while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0) {
continue;
}
/* 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);
while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0) {
continue;
}
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 */
};