util_script.c revision 7184de27ec1d62a83c41cdeac0953ca9fd661e8c
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor/* Licensed to the Apache Software Foundation (ASF) under one or more
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * contributor license agreements. See the NOTICE file distributed with
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * this work for additional information regarding copyright ownership.
0a05fab9aadd37834734ffe106fc8ad4488fb3e3rbowen * The ASF licenses this file to You under the Apache License, Version 2.0
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * (the "License"); you may not use this file except in compliance with
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * the License. You may obtain a copy of the License at
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * http://www.apache.org/licenses/LICENSE-2.0
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * Unless required by applicable law or agreed to in writing, software
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * distributed under the License is distributed on an "AS IS" BASIS,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * See the License for the specific language governing permissions and
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * limitations under the License.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "apr.h"
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "apr_lib.h"
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "apr_strings.h"
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#define APR_WANT_STRFUNC
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "apr_want.h"
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#if APR_HAVE_STDLIB_H
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include <stdlib.h>
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#endif
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "ap_config.h"
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "httpd.h"
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "http_config.h"
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "http_main.h"
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "http_log.h"
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "http_core.h"
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "http_protocol.h"
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "http_request.h" /* for sub_req_lookup_uri() */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "util_script.h"
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "apr_date.h" /* For apr_date_parse_http() */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "util_ebcdic.h"
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#ifdef OS2
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#define INCL_DOS
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include <os2.h>
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#endif
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor/*
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * Various utility functions which are common to a whole lot of
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * script-type extensions mechanisms, and might as well be gathered
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * in one place (if only to avoid creating inter-module dependancies
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * where there don't have to be).
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#define MALFORMED_MESSAGE "malformed header from script. Bad header="
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#define MALFORMED_HEADER_LENGTH_TO_SHOW 30
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis/* we know core's module_index is 0 */
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis#undef APLOG_MODULE_INDEX
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorstatic char *http2env(request_rec *r, const char *w)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor{
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char *res = (char *)apr_palloc(r->pool, sizeof("HTTP_") + strlen(w));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char *cp = res;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char c;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *cp++ = 'H';
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *cp++ = 'T';
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *cp++ = 'T';
ab426ff784a35bb6e1adf54774557b5e25b4e9b8lgentis *cp++ = 'P';
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *cp++ = '_';
ab426ff784a35bb6e1adf54774557b5e25b4e9b8lgentis
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while ((c = *w++) != 0) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (apr_isalnum(c)) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *cp++ = apr_toupper(c);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else if (c == '-') {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *cp++ = '_';
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor "Not exporting header with invalid name as envvar: %s",
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ap_escape_logitem(r->pool, w));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return NULL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *cp = 0;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
5ed11195e199bfaef4e520a412510b5ad0c158c5lgentis return res;
5ed11195e199bfaef4e520a412510b5ad0c158c5lgentis}
5ed11195e199bfaef4e520a412510b5ad0c158c5lgentis
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorstatic void add_unless_null(apr_table_t *table, const char *name, const char *val)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor{
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (name && val) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(table, name, val);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor}
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorstatic void env2env(apr_table_t *table, const char *name)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor{
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis add_unless_null(table, name, getenv(name));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor}
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorAP_DECLARE(char **) ap_create_environment(apr_pool_t *p, apr_table_t *t)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor{
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor const apr_array_header_t *env_arr = apr_table_elts(t);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor const apr_table_entry_t *elts = (const apr_table_entry_t *) env_arr->elts;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char **env = (char **) apr_palloc(p, (env_arr->nelts + 2) * sizeof(char *));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor int i, j;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char *tz;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char *whack;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor j = 0;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!apr_table_get(t, "TZ")) {
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis tz = getenv("TZ");
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (tz != NULL) {
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis env[j++] = apr_pstrcat(p, "TZ=", tz, NULL);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor for (i = 0; i < env_arr->nelts; ++i) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!elts[i].key) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor continue;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor env[j] = apr_pstrcat(p, elts[i].key, "=", elts[i].val, NULL);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor whack = env[j];
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (apr_isdigit(*whack)) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *whack++ = '_';
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (*whack != '=') {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!apr_isalnum(*whack) && *whack != '_') {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *whack = '_';
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ++whack;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ++j;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis }
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis env[j] = NULL;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis return env;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor}
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorAP_DECLARE(void) ap_add_common_vars(request_rec *r)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor{
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_t *e;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor server_rec *s = r->server;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor conn_rec *c = r->connection;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor const char *env_temp;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor const apr_array_header_t *hdrs_arr = apr_table_elts(r->headers_in);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor const apr_table_entry_t *hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis int i;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis apr_port_t rport;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis /* use a temporary apr_table_t which we'll overlap onto
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * r->subprocess_env later
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * (exception: if r->subprocess_env is empty at the start,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * write directly into it)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (apr_is_empty_table(r->subprocess_env)) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor e = r->subprocess_env;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor e = apr_table_make(r->pool, 25 + hdrs_arr->nelts);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* First, add environment vars from headers... this is as per
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * CGI specs, though other sorts of scripting interfaces see
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * the same vars...
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis for (i = 0; i < hdrs_arr->nelts; ++i) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!hdrs[i].key) {
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis continue;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* A few headers are special cased --- Authorization to prevent
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * rogue scripts from capturing passwords; content-type and -length
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * for no particular reason.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!strcasecmp(hdrs[i].key, "Content-type")) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "CONTENT_TYPE", hdrs[i].val);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else if (!strcasecmp(hdrs[i].key, "Content-length")) {
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis apr_table_addn(e, "CONTENT_LENGTH", hdrs[i].val);
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis }
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis /*
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis * You really don't want to disable this check, since it leaves you
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis * wide open to CGIs stealing passwords and people viewing them
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * in the environment with "ps -e". But, if you must...
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#ifndef SECURITY_HOLE_PASS_AUTHORIZATION
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else if (!strcasecmp(hdrs[i].key, "Authorization")
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor || !strcasecmp(hdrs[i].key, "Proxy-Authorization")) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor continue;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#endif
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor add_unless_null(e, http2env(r, hdrs[i].key), hdrs[i].val);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor env_temp = apr_table_get(r->subprocess_env, "PATH");
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (env_temp == NULL) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor env_temp = getenv("PATH");
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (env_temp == NULL) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor env_temp = DEFAULT_PATH;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis }
8202fc9176b3bca8b27ad20737863494e2f244a5lgentis apr_table_addn(e, "PATH", apr_pstrdup(r->pool, env_temp));
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#if defined(WIN32)
1828bbc20654649b007eec284bb0b751fed08b91lgentis env2env(e, "SystemRoot");
1828bbc20654649b007eec284bb0b751fed08b91lgentis env2env(e, "COMSPEC");
1828bbc20654649b007eec284bb0b751fed08b91lgentis env2env(e, "PATHEXT");
1828bbc20654649b007eec284bb0b751fed08b91lgentis env2env(e, "WINDIR");
1828bbc20654649b007eec284bb0b751fed08b91lgentis#elif defined(OS2)
1828bbc20654649b007eec284bb0b751fed08b91lgentis env2env(e, "COMSPEC");
1828bbc20654649b007eec284bb0b751fed08b91lgentis env2env(e, "ETC");
1828bbc20654649b007eec284bb0b751fed08b91lgentis env2env(e, "DPATH");
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor env2env(e, "PERLLIB_PREFIX");
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#elif defined(BEOS)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor env2env(e, "LIBRARY_PATH");
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#elif defined(DARWIN)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor env2env(e, "DYLD_LIBRARY_PATH");
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#elif defined(_AIX)
469d179ed70b8abe3730dc1519effdf68a73a343lgentis env2env(e, "LIBPATH");
fdd456e363b82501c228e3ca0f74750d9ca1472flgentis#elif defined(__HPUX__)
fdd456e363b82501c228e3ca0f74750d9ca1472flgentis /* HPUX PARISC 2.0W knows both, otherwise redundancy is harmless */
fdd456e363b82501c228e3ca0f74750d9ca1472flgentis env2env(e, "SHLIB_PATH");
7ddfde8cc1587a70759084db65c83a02e51380d1lgentis env2env(e, "LD_LIBRARY_PATH");
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#else /* Some Unix */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor env2env(e, "LD_LIBRARY_PATH");
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#endif
469d179ed70b8abe3730dc1519effdf68a73a343lgentis
5ed11195e199bfaef4e520a412510b5ad0c158c5lgentis apr_table_addn(e, "SERVER_SIGNATURE", ap_psignature("", r));
9aa7bfa70153d0811c3004202d9cfeda41c6fe66lgentis apr_table_addn(e, "SERVER_SOFTWARE", ap_get_server_banner());
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "SERVER_NAME",
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ap_escape_html(r->pool, ap_get_server_name(r)));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "SERVER_ADDR", r->connection->local_ip); /* Apache */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "SERVER_PORT",
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_psprintf(r->pool, "%u", ap_get_server_port(r)));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor add_unless_null(e, "REMOTE_HOST",
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ap_get_remote_host(c, r->per_dir_config, REMOTE_HOST, NULL));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "REMOTE_ADDR", c->remote_ip);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "DOCUMENT_ROOT", ap_document_root(r)); /* Apache */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_setn(e, "REQUEST_SCHEME", ap_http_scheme(r));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "CONTEXT_PREFIX", ap_context_prefix(r));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "CONTEXT_DOCUMENT_ROOT", ap_context_document_root(r));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "SERVER_ADMIN", s->server_admin); /* Apache */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "SCRIPT_FILENAME", r->filename); /* Apache */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor rport = c->remote_addr->port;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "REMOTE_PORT", apr_itoa(r->pool, rport));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (r->user) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_addn(e, "REMOTE_USER", r->user);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else if (r->prev) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor request_rec *back = r->prev;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (back) {
fed05bd9cff037691a12077d6d9d83f9892e70a5lgentis if (back->user) {
b2d402bf3e0826117941602dd3c1c5b41a7ed630lgentis apr_table_addn(e, "REDIRECT_REMOTE_USER", back->user);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor break;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor back = back->prev;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
fed05bd9cff037691a12077d6d9d83f9892e70a5lgentis add_unless_null(e, "AUTH_TYPE", r->ap_auth_type);
fed05bd9cff037691a12077d6d9d83f9892e70a5lgentis env_temp = ap_get_remote_logname(r);
b2d402bf3e0826117941602dd3c1c5b41a7ed630lgentis if (env_temp) {
b2d402bf3e0826117941602dd3c1c5b41a7ed630lgentis apr_table_addn(e, "REMOTE_IDENT", apr_pstrdup(r->pool, env_temp));
b2d402bf3e0826117941602dd3c1c5b41a7ed630lgentis }
b2d402bf3e0826117941602dd3c1c5b41a7ed630lgentis
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* Apache custom error responses. If we have redirected set two new vars */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (r->prev) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor add_unless_null(e, "REDIRECT_QUERY_STRING", r->prev->args);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor add_unless_null(e, "REDIRECT_URL", r->prev->uri);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (e != r->subprocess_env) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_overlap(r->subprocess_env, e, APR_OVERLAP_TABLES_SET);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor}
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor/* This "cute" little function comes about because the path info on
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * filenames and URLs aren't always the same. So we take the two,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * and find as much of the two that match as possible.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
9a2c99c15b21987ac37de808c8ff49f2d02a9c9flgentisAP_DECLARE(int) ap_find_path_info(const char *uri, const char *path_info)
9a2c99c15b21987ac37de808c8ff49f2d02a9c9flgentis{
9aa7bfa70153d0811c3004202d9cfeda41c6fe66lgentis int lu = strlen(uri);
9aa7bfa70153d0811c3004202d9cfeda41c6fe66lgentis int lp = strlen(path_info);
9a2c99c15b21987ac37de808c8ff49f2d02a9c9flgentis
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (lu-- && lp-- && uri[lu] == path_info[lp]) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (path_info[lp] == '/') {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (lu && uri[lu-1] == '/') lu--;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (lu == -1) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor lu = 0;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (uri[lu] != '\0' && uri[lu] != '/') {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor lu++;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return lu;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor}
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor/* Obtain the Request-URI from the original request-line, returning
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * a new string from the request pool containing the URI or "".
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorstatic char *original_uri(request_rec *r)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor{
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char *first, *last;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (r->the_request == NULL) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return (char *) apr_pcalloc(r->pool, 1);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor first = r->the_request; /* use the request-line */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
8202fc9176b3bca8b27ad20737863494e2f244a5lgentis while (*first && !apr_isspace(*first)) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ++first; /* skip over the method */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (apr_isspace(*first)) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ++first; /* and the space(s) */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
8202fc9176b3bca8b27ad20737863494e2f244a5lgentis
8202fc9176b3bca8b27ad20737863494e2f244a5lgentis last = first;
8202fc9176b3bca8b27ad20737863494e2f244a5lgentis while (*last && !apr_isspace(*last)) {
8202fc9176b3bca8b27ad20737863494e2f244a5lgentis ++last; /* end at next whitespace */
8202fc9176b3bca8b27ad20737863494e2f244a5lgentis }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return apr_pstrmemdup(r->pool, first, last - first);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor}
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorAP_DECLARE(void) ap_add_cgi_vars(request_rec *r)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor{
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_t *e = r->subprocess_env;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_setn(e, "GATEWAY_INTERFACE", "CGI/1.1");
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_setn(e, "SERVER_PROTOCOL", r->protocol);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_setn(e, "REQUEST_METHOD", r->method);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_setn(e, "QUERY_STRING", r->args ? r->args : "");
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_setn(e, "REQUEST_URI", original_uri(r));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
3169b556e342e88cdd0719be473eaf27d3084340lgentis /* Note that the code below special-cases scripts run from includes,
fdd456e363b82501c228e3ca0f74750d9ca1472flgentis * because it "knows" that the sub_request has been hacked to have the
fdd456e363b82501c228e3ca0f74750d9ca1472flgentis * args and path_info of the original request, and not any that may have
fdd456e363b82501c228e3ca0f74750d9ca1472flgentis * come with the script URI in the include command. Ugh.
7ddfde8cc1587a70759084db65c83a02e51380d1lgentis */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!strcmp(r->protocol, "INCLUDED")) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_setn(e, "SCRIPT_NAME", r->uri);
469d179ed70b8abe3730dc1519effdf68a73a343lgentis if (r->path_info && *r->path_info) {
5ed11195e199bfaef4e520a412510b5ad0c158c5lgentis apr_table_setn(e, "PATH_INFO", r->path_info);
9aa7bfa70153d0811c3004202d9cfeda41c6fe66lgentis }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else if (!r->path_info || !*r->path_info) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_setn(e, "SCRIPT_NAME", r->uri);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor int path_info_start = ap_find_path_info(r->uri, r->path_info);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_setn(e, "SCRIPT_NAME",
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis apr_pstrndup(r->pool, r->uri, path_info_start));
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis
1828bbc20654649b007eec284bb0b751fed08b91lgentis apr_table_setn(e, "PATH_INFO", r->path_info);
1828bbc20654649b007eec284bb0b751fed08b91lgentis }
1828bbc20654649b007eec284bb0b751fed08b91lgentis
1828bbc20654649b007eec284bb0b751fed08b91lgentis if (r->path_info && r->path_info[0]) {
1828bbc20654649b007eec284bb0b751fed08b91lgentis /*
1828bbc20654649b007eec284bb0b751fed08b91lgentis * To get PATH_TRANSLATED, treat PATH_INFO as a URI path.
1828bbc20654649b007eec284bb0b751fed08b91lgentis * Need to re-escape it for this, since the entire URI was
1828bbc20654649b007eec284bb0b751fed08b91lgentis * un-escaped before we determined where the PATH_INFO began.
1828bbc20654649b007eec284bb0b751fed08b91lgentis */
1828bbc20654649b007eec284bb0b751fed08b91lgentis request_rec *pa_req;
1828bbc20654649b007eec284bb0b751fed08b91lgentis
1828bbc20654649b007eec284bb0b751fed08b91lgentis pa_req = ap_sub_req_lookup_uri(ap_escape_uri(r->pool, r->path_info), r,
1828bbc20654649b007eec284bb0b751fed08b91lgentis NULL);
1828bbc20654649b007eec284bb0b751fed08b91lgentis
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis if (pa_req->filename) {
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis char *pt = apr_pstrcat(r->pool, pa_req->filename, pa_req->path_info,
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis NULL);
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis#ifdef WIN32
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis /* We need to make this a real Windows path name */
1828bbc20654649b007eec284bb0b751fed08b91lgentis apr_filepath_merge(&pt, "", pt, APR_FILEPATH_NATIVE, r->pool);
1828bbc20654649b007eec284bb0b751fed08b91lgentis#endif
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis apr_table_setn(e, "PATH_TRANSLATED", pt);
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis }
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis ap_destroy_sub_req(pa_req);
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis }
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis}
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentisstatic int set_cookie_doo_doo(void *v, const char *key, const char *val)
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis{
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis apr_table_addn(v, key, val);
3670f762ba6e3c38c29ea8a90fcdd5281ab13d53lgentis return 1;
09796a508c72a6aba33aa486753bb8cdea806d43lgentis}
1828bbc20654649b007eec284bb0b751fed08b91lgentis
1828bbc20654649b007eec284bb0b751fed08b91lgentis#define HTTP_UNSET (-HTTP_OK)
1828bbc20654649b007eec284bb0b751fed08b91lgentis
1828bbc20654649b007eec284bb0b751fed08b91lgentisAP_DECLARE(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
1828bbc20654649b007eec284bb0b751fed08b91lgentis int (*getsfunc) (char *, int, void *),
1828bbc20654649b007eec284bb0b751fed08b91lgentis void *getsfunc_data)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor{
020366f830905b6b5dfccfa03373379ae6a13e7blgentis char x[MAX_STRING_LEN];
020366f830905b6b5dfccfa03373379ae6a13e7blgentis char *w, *l;
020366f830905b6b5dfccfa03373379ae6a13e7blgentis int p;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor int cgi_status = HTTP_UNSET;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_t *merge;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_t *cookie_table;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (buffer) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *buffer = '\0';
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor w = buffer ? buffer : x;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* temporary place to hold headers to merge in later */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor merge = apr_table_make(r->pool, 10);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* The HTTP specification says that it is legal to merge duplicate
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * headers into one. Some browsers that support Cookies don't like
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * merged headers and prefer that each Set-Cookie header is sent
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * separately. Lets humour those browsers by not merging.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * Oh what a pain it is.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor cookie_table = apr_table_make(r->pool, 2);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_do(set_cookie_doo_doo, cookie_table, r->err_headers_out, "Set-Cookie", NULL);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (1) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
7ddfde8cc1587a70759084db65c83a02e51380d1lgentis int rv = (*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (rv == 0) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor "Premature end of script headers: %s",
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_filepath_name_get(r->filename));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return HTTP_INTERNAL_SERVER_ERROR;
7ddfde8cc1587a70759084db65c83a02e51380d1lgentis }
fed05bd9cff037691a12077d6d9d83f9892e70a5lgentis else if (rv == -1) {
7ddfde8cc1587a70759084db65c83a02e51380d1lgentis ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
7ddfde8cc1587a70759084db65c83a02e51380d1lgentis "Script timed out before returning headers: %s",
7ddfde8cc1587a70759084db65c83a02e51380d1lgentis apr_filepath_name_get(r->filename));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return HTTP_GATEWAY_TIME_OUT;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* Delete terminal (CR?)LF */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor p = strlen(w);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* Indeed, the host's '\n':
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor '\012' for UNIX; '\015' for MacOS; '\025' for OS/390
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor -- whatever the script generates.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (p > 0 && w[p - 1] == '\n') {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (p > 1 && w[p - 2] == CR) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor w[p - 2] = '\0';
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor w[p - 1] = '\0';
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
9a2c99c15b21987ac37de808c8ff49f2d02a9c9flgentis
9a2c99c15b21987ac37de808c8ff49f2d02a9c9flgentis /*
9aa7bfa70153d0811c3004202d9cfeda41c6fe66lgentis * If we've finished reading the headers, check to make sure any
9aa7bfa70153d0811c3004202d9cfeda41c6fe66lgentis * HTTP/1.1 conditions are met. If so, we're done; normal processing
9a2c99c15b21987ac37de808c8ff49f2d02a9c9flgentis * will handle the script's output. If not, just return the error.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * The appropriate thing to do would be to send the script process a
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * SIGPIPE to let it know we're ignoring it, close the channel to the
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * script process, and *then* return the failed-to-meet-condition
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * error. Otherwise we'd be waiting for the script to finish
c9cea69ad0824357e3a28411a30f966c7d299116lgentis * blithering before telling the client the output was no good.
c9cea69ad0824357e3a28411a30f966c7d299116lgentis * However, we don't have the information to do that, so we have to
c9cea69ad0824357e3a28411a30f966c7d299116lgentis * leave it to an upper layer.
c9cea69ad0824357e3a28411a30f966c7d299116lgentis */
9aa7bfa70153d0811c3004202d9cfeda41c6fe66lgentis if (w[0] == '\0') {
c9cea69ad0824357e3a28411a30f966c7d299116lgentis int cond_status = OK;
9aa7bfa70153d0811c3004202d9cfeda41c6fe66lgentis
9aa7bfa70153d0811c3004202d9cfeda41c6fe66lgentis /* PR#38070: This fails because it gets confused when a
c9cea69ad0824357e3a28411a30f966c7d299116lgentis * CGI Status header overrides ap_meets_conditions.
c9cea69ad0824357e3a28411a30f966c7d299116lgentis *
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * We can fix that by dropping ap_meets_conditions when
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * Status has been set. Since this is the only place
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * cgi_status gets used, let's test it explicitly.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * The alternative would be to ignore CGI Status when
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * ap_meets_conditions returns anything interesting.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * That would be safer wrt HTTP, but would break CGI.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if ((cgi_status == HTTP_UNSET) && (r->method_number == M_GET)) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor cond_status = ap_meets_conditions(r);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
020366f830905b6b5dfccfa03373379ae6a13e7blgentis apr_table_overlap(r->err_headers_out, merge,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor APR_OVERLAP_TABLES_MERGE);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!apr_is_empty_table(cookie_table)) {
2352e688b8a17f1d6cfb3558bcb95ae9145b4075lgentis /* the cookies have already been copied to the cookie_table */
2352e688b8a17f1d6cfb3558bcb95ae9145b4075lgentis apr_table_unset(r->err_headers_out, "Set-Cookie");
2352e688b8a17f1d6cfb3558bcb95ae9145b4075lgentis r->err_headers_out = apr_table_overlay(r->pool,
2352e688b8a17f1d6cfb3558bcb95ae9145b4075lgentis r->err_headers_out, cookie_table);
2352e688b8a17f1d6cfb3558bcb95ae9145b4075lgentis }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return cond_status;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* if we see a bogus header don't ignore it. Shout and scream */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#if APR_CHARSET_EBCDIC
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* Chances are that we received an ASCII header text instead of
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * the expected EBCDIC header lines. Try to auto-detect:
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!(l = strchr(w, ':'))) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor int maybeASCII = 0, maybeEBCDIC = 0;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor unsigned char *cp, native;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_size_t inbytes_left, outbytes_left;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor for (cp = w; *cp != '\0'; ++cp) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor native = apr_xlate_conv_byte(ap_hdrs_from_ascii, *cp);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (apr_isprint(*cp) && !apr_isprint(native))
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ++maybeEBCDIC;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!apr_isprint(*cp) && apr_isprint(native))
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis ++maybeASCII;
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis }
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis if (maybeASCII > maybeEBCDIC) {
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis "CGI Interface Error: Script headers apparently ASCII: (CGI = %s)",
3169b556e342e88cdd0719be473eaf27d3084340lgentis r->filename);
3169b556e342e88cdd0719be473eaf27d3084340lgentis inbytes_left = outbytes_left = cp - w;
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis apr_xlate_conv_buffer(ap_hdrs_from_ascii,
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis w, &inbytes_left, w, &outbytes_left);
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis }
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis }
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis#endif /*APR_CHARSET_EBCDIC*/
3169b556e342e88cdd0719be473eaf27d3084340lgentis if (!(l = strchr(w, ':'))) {
3169b556e342e88cdd0719be473eaf27d3084340lgentis char malformed[(sizeof MALFORMED_MESSAGE) + 1
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis + MALFORMED_HEADER_LENGTH_TO_SHOW];
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis strcpy(malformed, MALFORMED_MESSAGE);
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis strncat(malformed, w, MALFORMED_HEADER_LENGTH_TO_SHOW);
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis
3169b556e342e88cdd0719be473eaf27d3084340lgentis if (!buffer) {
3169b556e342e88cdd0719be473eaf27d3084340lgentis /* Soak up all the script output - may save an outright kill */
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis while ((*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data)) {
8180f4fa7587742b9395bfc4ea20ef85fb2436f0lgentis continue;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor "%s: %s", malformed,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_filepath_name_get(r->filename));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return HTTP_INTERNAL_SERVER_ERROR;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *l++ = '\0';
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (*l && apr_isspace(*l)) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ++l;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!strcasecmp(w, "Content-type")) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char *tmp;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* Nuke trailing whitespace */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char *endp = l + strlen(l) - 1;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (endp > l && apr_isspace(*endp)) {
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis *endp-- = '\0';
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis }
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis tmp = apr_pstrdup(r->pool, l);
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis ap_content_type_tolower(tmp);
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis ap_set_content_type(r, tmp);
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis }
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis /*
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis * If the script returned a specific status, that's what
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis * we'll use - otherwise we assume 200 OK.
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis */
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis else if (!strcasecmp(w, "Status")) {
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis r->status = cgi_status = atoi(l);
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis r->status_line = apr_pstrdup(r->pool, l);
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis }
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis else if (!strcasecmp(w, "Location")) {
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis apr_table_set(r->headers_out, w, l);
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis }
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis else if (!strcasecmp(w, "Content-Length")) {
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis apr_table_set(r->headers_out, w, l);
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis }
42fd21ba5cc2aec7625782b56d17d7a7016aaa7blgentis else if (!strcasecmp(w, "Content-Range")) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_set(r->headers_out, w, l);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else if (!strcasecmp(w, "Transfer-Encoding")) {
9aa7bfa70153d0811c3004202d9cfeda41c6fe66lgentis apr_table_set(r->headers_out, w, l);
9aa7bfa70153d0811c3004202d9cfeda41c6fe66lgentis }
fed05bd9cff037691a12077d6d9d83f9892e70a5lgentis else if (!strcasecmp(w, "ETag")) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_set(r->headers_out, w, l);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /*
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * If the script gave us a Last-Modified header, we can't just
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * pass it on blindly because of restrictions on future values.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else if (!strcasecmp(w, "Last-Modified")) {
8202fc9176b3bca8b27ad20737863494e2f244a5lgentis ap_update_mtime(r, apr_date_parse_http(l));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ap_set_last_modified(r);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else if (!strcasecmp(w, "Set-Cookie")) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor apr_table_add(cookie_table, w, l);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
8202fc9176b3bca8b27ad20737863494e2f244a5lgentis else {
8202fc9176b3bca8b27ad20737863494e2f244a5lgentis apr_table_add(merge, w, l);
8202fc9176b3bca8b27ad20737863494e2f244a5lgentis }
8202fc9176b3bca8b27ad20737863494e2f244a5lgentis }
8202fc9176b3bca8b27ad20737863494e2f244a5lgentis /* never reached - we leave this function within the while loop above */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return OK;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor}
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorstatic int getsfunc_FILE(char *buf, int len, void *f)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor{
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return apr_file_gets(buf, len, (apr_file_t *) f) == APR_SUCCESS;
3169b556e342e88cdd0719be473eaf27d3084340lgentis}
3169b556e342e88cdd0719be473eaf27d3084340lgentis
3169b556e342e88cdd0719be473eaf27d3084340lgentisAP_DECLARE(int) ap_scan_script_header_err(request_rec *r, apr_file_t *f,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char *buffer)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor{
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return ap_scan_script_header_err_core(r, buffer, getsfunc_FILE, f);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor}
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
static int getsfunc_BRIGADE(char *buf, int len, void *arg)
{
apr_bucket_brigade *bb = (apr_bucket_brigade *)arg;
const char *dst_end = buf + len - 1; /* leave room for terminating null */
char *dst = buf;
apr_bucket *e = APR_BRIGADE_FIRST(bb);
apr_status_t rv;
int done = 0;
while ((dst < dst_end) && !done && !APR_BUCKET_IS_EOS(e)) {
const char *bucket_data;
apr_size_t bucket_data_len;
const char *src;
const char *src_end;
apr_bucket * next;
rv = apr_bucket_read(e, &bucket_data, &bucket_data_len,
APR_BLOCK_READ);
if (rv != APR_SUCCESS || (bucket_data_len == 0)) {
return APR_STATUS_IS_TIMEUP(rv) ? -1 : 0;
}
src = bucket_data;
src_end = bucket_data + bucket_data_len;
while ((src < src_end) && (dst < dst_end) && !done) {
if (*src == '\n') {
done = 1;
}
else if (*src != '\r') {
*dst++ = *src;
}
src++;
}
if (src < src_end) {
apr_bucket_split(e, src - bucket_data);
}
next = APR_BUCKET_NEXT(e);
APR_BUCKET_REMOVE(e);
apr_bucket_destroy(e);
e = next;
}
*dst = 0;
return 1;
}
AP_DECLARE(int) ap_scan_script_header_err_brigade(request_rec *r,
apr_bucket_brigade *bb,
char *buffer)
{
return ap_scan_script_header_err_core(r, buffer, getsfunc_BRIGADE, bb);
}
struct vastrs {
va_list args;
int arg;
const char *curpos;
};
static int getsfunc_STRING(char *w, int len, void *pvastrs)
{
struct vastrs *strs = (struct vastrs*) pvastrs;
const char *p;
int t;
if (!strs->curpos || !*strs->curpos)
return 0;
p = ap_strchr_c(strs->curpos, '\n');
if (p)
++p;
else
p = ap_strchr_c(strs->curpos, '\0');
t = p - strs->curpos;
if (t > len)
t = len;
strncpy (w, strs->curpos, t);
w[t] = '\0';
if (!strs->curpos[t]) {
++strs->arg;
strs->curpos = va_arg(strs->args, const char *);
}
else
strs->curpos += t;
return t;
}
/* ap_scan_script_header_err_strs() accepts additional const char* args...
* each is treated as one or more header lines, and the first non-header
* character is returned to **arg, **data. (The first optional arg is
* counted as 0.)
*/
AP_DECLARE_NONSTD(int) ap_scan_script_header_err_strs(request_rec *r,
char *buffer,
const char **termch,
int *termarg, ...)
{
struct vastrs strs;
int res;
va_start(strs.args, termarg);
strs.arg = 0;
strs.curpos = va_arg(strs.args, char*);
res = ap_scan_script_header_err_core(r, buffer, getsfunc_STRING, (void *) &strs);
if (termch)
*termch = strs.curpos;
if (termarg)
*termarg = strs.arg;
va_end(strs.args);
return res;
}
static void
argstr_to_table(char *str, apr_table_t *parms)
{
char *key;
char *value;
char *strtok_state;
if (str == NULL) {
return;
}
key = apr_strtok(str, "&", &strtok_state);
while (key) {
value = strchr(key, '=');
if (value) {
*value = '\0'; /* Split the string in two */
value++; /* Skip passed the = */
}
else {
value = "1";
}
ap_unescape_url(key);
ap_unescape_url(value);
apr_table_set(parms, key, value);
key = apr_strtok(NULL, "&", &strtok_state);
}
}
AP_DECLARE(void) ap_args_to_table(request_rec *r, apr_table_t **table)
{
apr_table_t *t = apr_table_make(r->pool, 10);
argstr_to_table(apr_pstrdup(r->pool, r->args), t);
*table = t;
}