mod_cgi.c revision cb5134e26dede7d8a7439c5dd5f054750e42947b
c6d33447e28403a90ad817dba4df75fae785be28pquerna/* ====================================================================
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Redistribution and use in source and binary forms, with or without
c6d33447e28403a90ad817dba4df75fae785be28pquerna * modification, are permitted provided that the following conditions
c6d33447e28403a90ad817dba4df75fae785be28pquerna * 1. Redistributions of source code must retain the above copyright
c6d33447e28403a90ad817dba4df75fae785be28pquerna * notice, this list of conditions and the following disclaimer.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * 2. Redistributions in binary form must reproduce the above copyright
c6d33447e28403a90ad817dba4df75fae785be28pquerna * notice, this list of conditions and the following disclaimer in
c6d33447e28403a90ad817dba4df75fae785be28pquerna * the documentation and/or other materials provided with the
c6d33447e28403a90ad817dba4df75fae785be28pquerna * distribution.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * 3. All advertising materials mentioning features or use of this
c6d33447e28403a90ad817dba4df75fae785be28pquerna * software must display the following acknowledgment:
c6d33447e28403a90ad817dba4df75fae785be28pquerna * "This product includes software developed by the Apache Group
c6d33447e28403a90ad817dba4df75fae785be28pquerna * for use in the Apache HTTP server project (http://www.apache.org/)."
c6d33447e28403a90ad817dba4df75fae785be28pquerna * 4. The names "Apache Server" and "Apache Group" must not be used to
c6d33447e28403a90ad817dba4df75fae785be28pquerna * endorse or promote products derived from this software without
c6d33447e28403a90ad817dba4df75fae785be28pquerna * prior written permission. For written permission, please contact
c6d33447e28403a90ad817dba4df75fae785be28pquerna * apache@apache.org.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * 5. Products derived from this software may not be called "Apache"
c6d33447e28403a90ad817dba4df75fae785be28pquerna * nor may "Apache" appear in their names without prior written
c6d33447e28403a90ad817dba4df75fae785be28pquerna * permission of the Apache Group.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * 6. Redistributions of any form whatsoever must retain the following
c6d33447e28403a90ad817dba4df75fae785be28pquerna * acknowledgment:
c6d33447e28403a90ad817dba4df75fae785be28pquerna * "This product includes software developed by the Apache Group
c6d33447e28403a90ad817dba4df75fae785be28pquerna * for use in the Apache HTTP server project (http://www.apache.org/)."
c6d33447e28403a90ad817dba4df75fae785be28pquerna * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
c6d33447e28403a90ad817dba4df75fae785be28pquerna * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
c6d33447e28403a90ad817dba4df75fae785be28pquerna * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
c6d33447e28403a90ad817dba4df75fae785be28pquerna * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
c6d33447e28403a90ad817dba4df75fae785be28pquerna * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
c6d33447e28403a90ad817dba4df75fae785be28pquerna * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
c6d33447e28403a90ad817dba4df75fae785be28pquerna * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
c6d33447e28403a90ad817dba4df75fae785be28pquerna * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
c6d33447e28403a90ad817dba4df75fae785be28pquerna * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
c6d33447e28403a90ad817dba4df75fae785be28pquerna * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
c6d33447e28403a90ad817dba4df75fae785be28pquerna * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
c6d33447e28403a90ad817dba4df75fae785be28pquerna * OF THE POSSIBILITY OF SUCH DAMAGE.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * ====================================================================
c6d33447e28403a90ad817dba4df75fae785be28pquerna * This software consists of voluntary contributions made by many
c6d33447e28403a90ad817dba4df75fae785be28pquerna * individuals on behalf of the Apache Group and was originally based
c6d33447e28403a90ad817dba4df75fae785be28pquerna * on public domain software written at the National Center for
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Supercomputing Applications, University of Illinois, Urbana-Champaign.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * For more information on the Apache Group and the Apache HTTP server
c6d33447e28403a90ad817dba4df75fae785be28pquerna * project, please see <http://www.apache.org/>.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * http_script: keeps all script-related ramblings together.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Compliant to CGI/1.1 spec
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Adapted by rst from original NCSA code by Rob McCool
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Apache adds some new env vars; REDIRECT_URL and REDIRECT_QUERY_STRING for
c6d33447e28403a90ad817dba4df75fae785be28pquerna * custom error responses, and DOCUMENT_ROOT because we found it useful.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * It also adds SERVER_ADMIN - useful for scripts to know who to mail when
c6d33447e28403a90ad817dba4df75fae785be28pquerna * they fail.
c6d33447e28403a90ad817dba4df75fae785be28pquerna/* KLUDGE --- for back-combatibility, we don't have to check ExecCGI
c6d33447e28403a90ad817dba4df75fae785be28pquerna * in ScriptAliased directories, which means we need to know if this
c6d33447e28403a90ad817dba4df75fae785be28pquerna * request came through ScriptAlias or not... so the Alias module
c6d33447e28403a90ad817dba4df75fae785be28pquerna * leaves a note for us.
c6d33447e28403a90ad817dba4df75fae785be28pquerna const char *t = ap_table_get(r->notes, "alias-forced-type");
c6d33447e28403a90ad817dba4df75fae785be28pquerna/* Configuration stuff */
c6d33447e28403a90ad817dba4df75fae785be28pquernatypedef struct {
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic void *create_cgi_config(ap_context_t *p, server_rec *s)
c6d33447e28403a90ad817dba4df75fae785be28pquerna (cgi_server_conf *) ap_pcalloc(p, sizeof(cgi_server_conf));
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic void *merge_cgi_config(ap_context_t *p, void *basev, void *overridesv)
c6d33447e28403a90ad817dba4df75fae785be28pquerna cgi_server_conf *base = (cgi_server_conf *) basev, *overrides = (cgi_server_conf *) overridesv;
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic const char *set_scriptlog(cmd_parms *cmd, void *dummy, char *arg)
c6d33447e28403a90ad817dba4df75fae785be28pquerna (cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic const char *set_scriptlog_length(cmd_parms *cmd, void *dummy, char *arg)
c6d33447e28403a90ad817dba4df75fae785be28pquerna (cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic const char *set_scriptlog_buffer(cmd_parms *cmd, void *dummy, char *arg)
c6d33447e28403a90ad817dba4df75fae785be28pquerna (cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
c6d33447e28403a90ad817dba4df75fae785be28pquerna {"ScriptLog", set_scriptlog, NULL, RSRC_CONF, TAKE1,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "the name of a log for script debugging info"},
c6d33447e28403a90ad817dba4df75fae785be28pquerna {"ScriptLogLength", set_scriptlog_length, NULL, RSRC_CONF, TAKE1,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "the maximum length (in bytes) of the script debug log"},
c6d33447e28403a90ad817dba4df75fae785be28pquerna {"ScriptLogBuffer", set_scriptlog_buffer, NULL, RSRC_CONF, TAKE1,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "the maximum size (in bytes) to record of a POST request"},
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic int log_scripterror(request_rec *r, cgi_server_conf * conf, int ret,
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_log_rerror(APLOG_MARK, show_errno|APLOG_ERR, errno, r,
c6d33447e28403a90ad817dba4df75fae785be28pquerna ((stat(ap_server_root_relative(r->pool, conf->logname), &finfo) == 0)
c6d33447e28403a90ad817dba4df75fae785be28pquerna (ap_open(&f, ap_server_root_relative(r->pool, conf->logname),
c6d33447e28403a90ad817dba4df75fae785be28pquerna APR_APPEND | APR_BUFFERED, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_fprintf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
c6d33447e28403a90ad817dba4df75fae785be28pquerna r->args ? "?" : "", r->args ? r->args : "", r->protocol);
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic int log_script(request_rec *r, cgi_server_conf * conf, int ret,
c6d33447e28403a90ad817dba4df75fae785be28pquerna char *dbuf, const char *sbuf, BUFF *script_in, BUFF *script_err)
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_array_header_t *hdrs_arr = ap_table_elts(r->headers_in);
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_table_entry_t *hdrs = (ap_table_entry_t *) hdrs_arr->elts;
c6d33447e28403a90ad817dba4df75fae785be28pquerna ((stat(ap_server_root_relative(r->pool, conf->logname), &finfo) == 0)
c6d33447e28403a90ad817dba4df75fae785be28pquerna (ap_open(&f, ap_server_root_relative(r->pool, conf->logname),
c6d33447e28403a90ad817dba4df75fae785be28pquerna APR_APPEND, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Soak up script output */
c6d33447e28403a90ad817dba4df75fae785be28pquerna while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_in) > 0)
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Soak up stderr and redirect it to the error log.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Script output to stderr is already directed to the error log
c6d33447e28403a90ad817dba4df75fae785be28pquerna * on Unix, thanks to the magic of fork().
c6d33447e28403a90ad817dba4df75fae785be28pquerna while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r,
c6d33447e28403a90ad817dba4df75fae785be28pquerna while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0)
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_fprintf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
c6d33447e28403a90ad817dba4df75fae785be28pquerna r->args ? "?" : "", r->args ? r->args : "", r->protocol);
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_fprintf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
c6d33447e28403a90ad817dba4df75fae785be28pquerna if ((r->method_number == M_POST || r->method_number == M_PUT)
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_fprintf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_in) > 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_in) > 0)
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0)
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic ap_status_t run_cgi_child(BUFF **script_out, BUFF **script_in, BUFF **script_err,
c6d33447e28403a90ad817dba4df75fae785be28pquerna char *command, char *const argv[], request_rec *r, ap_context_t *p)
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Under OS/2 need to use device con. */
c6d33447e28403a90ad817dba4df75fae785be28pquerna fprintf(dbg, "Attempting to exec %s as %sCGI child (argv0 = %s)\n",
c6d33447e28403a90ad817dba4df75fae785be28pquerna for (i = 0; env[i]; ++i)
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Transumute ourselves into the script.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * NB only ISINDEX scripts get decoded arguments.
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (((rc = ap_createprocattr_init(&procattr, p)) != APR_SUCCESS) ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_make_dirstr_parent(r->pool, r->filename))) != APR_SUCCESS) ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna ((rc = ap_setprocattr_cmdtype(procattr, APR_PROGRAM)) != APR_SUCCESS)) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Something bad happened, tell the world. */
c6d33447e28403a90ad817dba4df75fae785be28pquerna rc = ap_create_process(&procnew, command, argv, env, procattr, p);
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Bad things happened. Everyone should have cleaned up. */
c6d33447e28403a90ad817dba4df75fae785be28pquerna "couldn't create child process: %d: %s", rc, r->filename);
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Fill in BUFF structure for parents pipe to child's stdout */
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Fill in BUFF structure for parents pipe to child's stdin */
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Fill in BUFF structure for parents pipe to child's stderr */
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic ap_status_t build_argv_list(char ***argv, request_rec *r, ap_context_t *p)
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* count the number of keywords */
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Everything is - 1 to account for the first parameter which is the
c6d33447e28403a90ad817dba4df75fae785be28pquerna * program name. We didn't used to have to do this, but APR wants it.
c6d33447e28403a90ad817dba4df75fae785be28pquerna numwords = APACHE_ARG_MAX - 1; /* Truncate args to prevent overrun */
c6d33447e28403a90ad817dba4df75fae785be28pquerna *argv = (char **) ap_palloc(p, (numwords + 2) * sizeof(char *));
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic ap_status_t build_command_line(char **c, request_rec *r, ap_context_t *p)
c6d33447e28403a90ad817dba4df75fae785be28pquerna fileType = ap_get_win32_interpreter(r, &interpreter);
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "%s is not executable; ensure interpreted scripts have "
c6d33447e28403a90ad817dba4df75fae785be28pquerna "\"#!\" first line",
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Build the command string to pass to ap_create_process()
c6d33447e28403a90ad817dba4df75fae785be28pquerna quoted_filename = ap_pstrcat(p, "\"", r->filename, "\"", NULL);
return APR_SUCCESS;
char *command;
ap_context_t *p;
return DECLINED;
argv0++;
char *newfile;
return retval;
return HTTP_INTERNAL_SERVER_ERROR;
return HTTP_INTERNAL_SERVER_ERROR;
return HTTP_INTERNAL_SERVER_ERROR;
if (ap_should_client_block(r)) {
dbpos = 0;
while ((len_read =
const char *location;
int ret;
#ifdef CHARSET_EBCDIC
ap_checkconv(r);
return OK;
return REDIRECT;
if (!r->header_only) {
{NULL}