util_script.c revision 540bbdd4d087610fc0563f0dc3aab2fc5defbad9
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance/* ====================================================================
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance *
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * Redistribution and use in source and binary forms, with or without
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * modification, are permitted provided that the following conditions
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * are met:
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance *
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * 1. Redistributions of source code must retain the above copyright
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * notice, this list of conditions and the following disclaimer.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance *
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * 2. Redistributions in binary form must reproduce the above copyright
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * notice, this list of conditions and the following disclaimer in
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * the documentation and/or other materials provided with the
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * distribution.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance *
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * 3. All advertising materials mentioning features or use of this
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * software must display the following acknowledgment:
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * "This product includes software developed by the Apache Group
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * for use in the Apache HTTP server project (http://www.apache.org/)."
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance *
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * 4. The names "Apache Server" and "Apache Group" must not be used to
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * endorse or promote products derived from this software without
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * prior written permission. For written permission, please contact
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * apache@apache.org.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance *
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * 5. Products derived from this software may not be called "Apache"
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * nor may "Apache" appear in their names without prior written
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * permission of the Apache Group.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance *
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * 6. Redistributions of any form whatsoever must retain the following
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * acknowledgment:
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * "This product includes software developed by the Apache Group
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * for use in the Apache HTTP server project (http://www.apache.org/)."
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance *
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * OF THE POSSIBILITY OF SUCH DAMAGE.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * ====================================================================
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance *
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * This software consists of voluntary contributions made by many
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * individuals on behalf of the Apache Group and was originally based
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * on public domain software written at the National Center for
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * Supercomputing Applications, University of Illinois, Urbana-Champaign.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * For more information on the Apache Group and the Apache HTTP server
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * project, please see <http://www.apache.org/>.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance *
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance */
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#define CORE_PRIVATE
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#include "httpd.h"
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#include "http_config.h"
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#include "http_main.h"
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#include "http_log.h"
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#include "http_core.h"
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#include "http_protocol.h"
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#include "http_request.h" /* for sub_req_lookup_uri() */
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#include "util_script.h"
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#include "util_date.h" /* For parseHTTPdate() */
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#include "ap_config.h"
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#include <stdlib.h>
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#include <string.h>
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#ifdef OS2
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#define INCL_DOS
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#include <os2.h>
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#endif
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance/*
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * Various utility functions which are common to a whole lot of
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * script-type extensions mechanisms, and might as well be gathered
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * in one place (if only to avoid creating inter-module dependancies
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * where there don't have to be).
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance */
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#define MALFORMED_MESSAGE "malformed header from script. Bad header="
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#define MALFORMED_HEADER_LENGTH_TO_SHOW 30
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#if defined(OS2) || defined(WIN32)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance/* If a request includes query info in the URL (stuff after "?"), and
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * the query info does not contain "=" (indicative of a FORM submission),
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * then this routine is called to create the argument list to be passed
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * to the CGI script. When suexec is enabled, the suexec path, user, and
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * group are the first three arguments to be passed; if not, all three
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * must be NULL. The query info is split into separate arguments, where
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * "+" is the separator between keyword arguments.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance *
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * XXXX: note that the WIN32 code uses one of the suexec strings
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * to pass an interpreter name. Remember this if changing the way they
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * are handled in create_argv.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance *
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance */
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mancestatic char **create_argv(ap_context_t *p, char *path, char *user, char *group,
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance char *av0, const char *args)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance{
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance int x, numwords;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance char **av;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance char *w;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance int idx = 0;
a6526952d69bccd048c954eb920493a6a83e78faFelix Gabriel Mance
a6526952d69bccd048c954eb920493a6a83e78faFelix Gabriel Mance /* count the number of keywords */
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance for (x = 0, numwords = 1; args[x]; x++) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (args[x] == '+') {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance ++numwords;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (numwords > APACHE_ARG_MAX - 5) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance numwords = APACHE_ARG_MAX - 5; /* Truncate args to prevent overrun */
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance av = (char **) ap_palloc(p, (numwords + 5) * sizeof(char *));
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (path) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance av[idx++] = path;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (user) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance av[idx++] = user;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (group) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance av[idx++] = group;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance av[idx++] = av0;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance for (x = 1; x <= numwords; x++) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance w = ap_getword_nulls(p, &args, '+');
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance ap_unescape_url(w);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance av[idx++] = ap_escape_shell_cmd(p, w);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance av[idx] = NULL;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance return av;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance}
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#endif /* defined(OS2) || defined(WIN32) */
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mancestatic char *http2env(ap_context_t *a, char *w)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance{
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance char *res = ap_pstrcat(a, "HTTP_", w, NULL);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance char *cp = res;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance while (*++cp) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (!ap_isalnum(*cp) && *cp != '_') {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance *cp = '_';
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance else {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance *cp = ap_toupper(*cp);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance return res;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance}
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel ManceAPI_EXPORT(char **) ap_create_environment(ap_context_t *p, ap_table_t *t)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance{
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance ap_array_header_t *env_arr = ap_table_elts(t);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance ap_table_entry_t *elts = (ap_table_entry_t *) env_arr->elts;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance char **env = (char **) ap_palloc(p, (env_arr->nelts + 2) * sizeof(char *));
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance int i, j;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance char *tz;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance char *whack;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance j = 0;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (!ap_table_get(t, "TZ")) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance tz = getenv("TZ");
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (tz != NULL) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance env[j++] = ap_pstrcat(p, "TZ=", tz, NULL);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance for (i = 0; i < env_arr->nelts; ++i) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (!elts[i].key) {
c41f2d65ecbf5ad9d3233a21f406a7698338a04bFelix Gabriel Mance continue;
c41f2d65ecbf5ad9d3233a21f406a7698338a04bFelix Gabriel Mance }
c41f2d65ecbf5ad9d3233a21f406a7698338a04bFelix Gabriel Mance env[j] = ap_pstrcat(p, elts[i].key, "=", elts[i].val, NULL);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance whack = env[j];
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (ap_isdigit(*whack)) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance *whack++ = '_';
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance while (*whack != '=') {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (!ap_isalnum(*whack) && *whack != '_') {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance *whack = '_';
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance ++whack;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance ++j;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
1cc559ec103ed20967587fff2e39cc88669f7b8fFelix Gabriel Mance
1cc559ec103ed20967587fff2e39cc88669f7b8fFelix Gabriel Mance env[j] = NULL;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance return env;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance}
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel ManceAPI_EXPORT(void) ap_add_common_vars(request_rec *r)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance{
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance ap_table_t *e;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance server_rec *s = r->server;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance conn_rec *c = r->connection;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance const char *rem_logname;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance char *env_path;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance#ifdef WIN32
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance char *env_temp;
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance#endif
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance const char *host;
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance ap_array_header_t *hdrs_arr = ap_table_elts(r->headers_in);
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance ap_table_entry_t *hdrs = (ap_table_entry_t *) hdrs_arr->elts;
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance int i;
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance /* use a temporary ap_table_t which we'll overlap onto
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance * r->subprocess_env later
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance */
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance e = ap_make_table(r->pool, 25 + hdrs_arr->nelts);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance /* First, add environment vars from headers... this is as per
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance * CGI specs, though other sorts of scripting interfaces see
c41f2d65ecbf5ad9d3233a21f406a7698338a04bFelix Gabriel Mance * the same vars...
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance */
c41f2d65ecbf5ad9d3233a21f406a7698338a04bFelix Gabriel Mance
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance for (i = 0; i < hdrs_arr->nelts; ++i) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (!hdrs[i].key) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance continue;
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance /* A few headers are special cased --- Authorization to prevent
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance * rogue scripts from capturing passwords; content-type and -length
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance * for no particular reason.
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance */
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if (!strcasecmp(hdrs[i].key, "Content-type")) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance ap_table_addn(e, "CONTENT_TYPE", hdrs[i].val);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance else if (!strcasecmp(hdrs[i].key, "Content-length")) {
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance ap_table_addn(e, "CONTENT_LENGTH", hdrs[i].val);
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance }
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance /*
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance * You really don't want to disable this check, since it leaves you
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance * wide open to CGIs stealing passwords and people viewing them
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance * in the environment with "ps -e". But, if you must...
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance */
6e7fe479953725884826bd38e4779229d45d3a40Felix Gabriel Mance#ifndef SECURITY_HOLE_PASS_AUTHORIZATION
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance else if (!strcasecmp(hdrs[i].key, "Authorization")
6e7fe479953725884826bd38e4779229d45d3a40Felix Gabriel Mance || !strcasecmp(hdrs[i].key, "Proxy-Authorization")) {
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance continue;
6e7fe479953725884826bd38e4779229d45d3a40Felix Gabriel Mance }
6e7fe479953725884826bd38e4779229d45d3a40Felix Gabriel Mance#endif
6e7fe479953725884826bd38e4779229d45d3a40Felix Gabriel Mance else {
6e7fe479953725884826bd38e4779229d45d3a40Felix Gabriel Mance ap_table_addn(e, http2env(r->pool, hdrs[i].key), hdrs[i].val);
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance }
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance }
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance if (!(env_path = getenv("PATH"))) {
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance env_path = DEFAULT_PATH;
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance }
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance
396f82c6cd926be759b60fd1e854acfde7068215Felix Gabriel Mance#ifdef WIN32
if (env_temp = getenv("SystemRoot")) {
ap_table_addn(e, "SystemRoot", env_temp);
}
if (env_temp = getenv("COMSPEC")) {
ap_table_addn(e, "COMSPEC", env_temp);
}
if (env_temp = getenv("WINDIR")) {
ap_table_addn(e, "WINDIR", env_temp);
}
#endif
ap_table_addn(e, "PATH", env_path);
ap_table_addn(e, "SERVER_SIGNATURE", ap_psignature("", r));
ap_table_addn(e, "SERVER_SOFTWARE", ap_get_server_version());
ap_table_addn(e, "SERVER_NAME", ap_get_server_name(r));
ap_table_addn(e, "SERVER_ADDR", r->connection->local_ip); /* Apache */
ap_table_addn(e, "SERVER_PORT",
ap_psprintf(r->pool, "%u", ap_get_server_port(r)));
host = ap_get_remote_host(c, r->per_dir_config, REMOTE_HOST);
if (host) {
ap_table_addn(e, "REMOTE_HOST", host);
}
ap_table_addn(e, "REMOTE_ADDR", c->remote_ip);
ap_table_addn(e, "DOCUMENT_ROOT", ap_document_root(r)); /* Apache */
ap_table_addn(e, "SERVER_ADMIN", s->server_admin); /* Apache */
ap_table_addn(e, "SCRIPT_FILENAME", r->filename); /* Apache */
ap_table_addn(e, "REMOTE_PORT",
ap_psprintf(r->pool, "%d", ntohs(c->remote_addr.sin_port)));
if (r->user) {
ap_table_addn(e, "REMOTE_USER", r->user);
}
if (r->ap_auth_type) {
ap_table_addn(e, "AUTH_TYPE", r->ap_auth_type);
}
rem_logname = ap_get_remote_logname(r);
if (rem_logname) {
ap_table_addn(e, "REMOTE_IDENT", ap_pstrdup(r->pool, rem_logname));
}
/* Apache custom error responses. If we have redirected set two new vars */
if (r->prev) {
if (r->prev->args) {
ap_table_addn(e, "REDIRECT_QUERY_STRING", r->prev->args);
}
if (r->prev->uri) {
ap_table_addn(e, "REDIRECT_URL", r->prev->uri);
}
}
ap_overlap_tables(r->subprocess_env, e, AP_OVERLAP_TABLES_SET);
}
/* This "cute" little function comes about because the path info on
* filenames and URLs aren't always the same. So we take the two,
* and find as much of the two that match as possible.
*/
API_EXPORT(int) ap_find_path_info(const char *uri, const char *path_info)
{
int lu = strlen(uri);
int lp = strlen(path_info);
while (lu-- && lp-- && uri[lu] == path_info[lp]);
if (lu == -1) {
lu = 0;
}
while (uri[lu] != '\0' && uri[lu] != '/') {
lu++;
}
return lu;
}
/* Obtain the Request-URI from the original request-line, returning
* a new string from the request pool containing the URI or "".
*/
static char *original_uri(request_rec *r)
{
char *first, *last;
if (r->the_request == NULL) {
return (char *) ap_pcalloc(r->pool, 1);
}
first = r->the_request; /* use the request-line */
while (*first && !ap_isspace(*first)) {
++first; /* skip over the method */
}
while (ap_isspace(*first)) {
++first; /* and the space(s) */
}
last = first;
while (*last && !ap_isspace(*last)) {
++last; /* end at next whitespace */
}
return ap_pstrndup(r->pool, first, last - first);
}
API_EXPORT(void) ap_add_cgi_vars(request_rec *r)
{
ap_table_t *e = r->subprocess_env;
ap_table_setn(e, "GATEWAY_INTERFACE", "CGI/1.1");
ap_table_setn(e, "SERVER_PROTOCOL", r->protocol);
ap_table_setn(e, "REQUEST_METHOD", r->method);
ap_table_setn(e, "QUERY_STRING", r->args ? r->args : "");
ap_table_setn(e, "REQUEST_URI", original_uri(r));
/* Note that the code below special-cases scripts run from includes,
* because it "knows" that the sub_request has been hacked to have the
* args and path_info of the original request, and not any that may have
* come with the script URI in the include command. Ugh.
*/
if (!strcmp(r->protocol, "INCLUDED")) {
ap_table_setn(e, "SCRIPT_NAME", r->uri);
if (r->path_info && *r->path_info) {
ap_table_setn(e, "PATH_INFO", r->path_info);
}
}
else if (!r->path_info || !*r->path_info) {
ap_table_setn(e, "SCRIPT_NAME", r->uri);
}
else {
int path_info_start = ap_find_path_info(r->uri, r->path_info);
ap_table_setn(e, "SCRIPT_NAME",
ap_pstrndup(r->pool, r->uri, path_info_start));
ap_table_setn(e, "PATH_INFO", r->path_info);
}
if (r->path_info && r->path_info[0]) {
/*
* To get PATH_TRANSLATED, treat PATH_INFO as a URI path.
* Need to re-escape it for this, since the entire URI was
* un-escaped before we determined where the PATH_INFO began.
*/
request_rec *pa_req;
pa_req = ap_sub_req_lookup_uri(ap_escape_uri(r->pool, r->path_info), r);
if (pa_req->filename) {
#ifdef WIN32
char buffer[HUGE_STRING_LEN];
#endif
char *pt = ap_pstrcat(r->pool, pa_req->filename, pa_req->path_info,
NULL);
#ifdef WIN32
/* We need to make this a real Windows path name */
GetFullPathName(pt, HUGE_STRING_LEN, buffer, NULL);
ap_table_setn(e, "PATH_TRANSLATED", ap_pstrdup(r->pool, buffer));
#else
ap_table_setn(e, "PATH_TRANSLATED", pt);
#endif
}
ap_destroy_sub_req(pa_req);
}
}
static int set_cookie_doo_doo(void *v, const char *key, const char *val)
{
ap_table_addn(v, key, val);
return 1;
}
API_EXPORT(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
int (*getsfunc) (char *, int, void *),
void *getsfunc_data)
{
char x[MAX_STRING_LEN];
char *w, *l;
int p;
int cgi_status = HTTP_OK;
ap_table_t *merge;
ap_table_t *cookie_table;
if (buffer) {
*buffer = '\0';
}
w = buffer ? buffer : x;
/* temporary place to hold headers to merge in later */
merge = ap_make_table(r->pool, 10);
/* The HTTP specification says that it is legal to merge duplicate
* headers into one. Some browsers that support Cookies don't like
* merged headers and prefer that each Set-Cookie header is sent
* separately. Lets humour those browsers by not merging.
* Oh what a pain it is.
*/
cookie_table = ap_make_table(r->pool, 2);
ap_table_do(set_cookie_doo_doo, cookie_table, r->err_headers_out, "Set-Cookie", NULL);
while (1) {
if ((*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data) == 0) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
"Premature end of script headers: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* Delete terminal (CR?)LF */
p = strlen(w);
if (p > 0 && w[p - 1] == '\n') {
if (p > 1 && w[p - 2] == '\015') {
w[p - 2] = '\0';
}
else {
w[p - 1] = '\0';
}
}
/*
* If we've finished reading the headers, check to make sure any
* HTTP/1.1 conditions are met. If so, we're done; normal processing
* will handle the script's output. If not, just return the error.
* The appropriate thing to do would be to send the script process a
* SIGPIPE to let it know we're ignoring it, close the channel to the
* script process, and *then* return the failed-to-meet-condition
* error. Otherwise we'd be waiting for the script to finish
* blithering before telling the client the output was no good.
* However, we don't have the information to do that, so we have to
* leave it to an upper layer.
*/
if (w[0] == '\0') {
int cond_status = OK;
if ((cgi_status == HTTP_OK) && (r->method_number == M_GET)) {
cond_status = ap_meets_conditions(r);
}
ap_overlap_tables(r->err_headers_out, merge,
AP_OVERLAP_TABLES_MERGE);
if (!ap_is_empty_table(cookie_table)) {
/* the cookies have already been copied to the cookie_table */
ap_table_unset(r->err_headers_out, "Set-Cookie");
r->err_headers_out = ap_overlay_tables(r->pool,
r->err_headers_out, cookie_table);
}
return cond_status;
}
/* if we see a bogus header don't ignore it. Shout and scream */
#ifdef CHARSET_EBCDIC
/* Chances are that we received an ASCII header text instead of
* the expected EBCDIC header lines. Try to auto-detect:
*/
if (!(l = strchr(w, ':'))) {
int maybeASCII = 0, maybeEBCDIC = 0;
char *cp;
for (cp = w; *cp != '\0'; ++cp) {
if (isprint(*cp) && !isprint(os_toebcdic[*cp]))
++maybeEBCDIC;
if (!isprint(*cp) && isprint(os_toebcdic[*cp]))
++maybeASCII;
}
if (maybeASCII > maybeEBCDIC) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server,
"CGI Interface Error: Script headers apparently ASCII: (CGI = %s)", r->filename);
ascii2ebcdic(w, w, cp - w);
}
}
#endif
if (!(l = strchr(w, ':'))) {
char malformed[(sizeof MALFORMED_MESSAGE) + 1
+ MALFORMED_HEADER_LENGTH_TO_SHOW];
strcpy(malformed, MALFORMED_MESSAGE);
strncat(malformed, w, MALFORMED_HEADER_LENGTH_TO_SHOW);
if (!buffer) {
/* Soak up all the script output - may save an outright kill */
while ((*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data)) {
continue;
}
}
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
"%s: %s", malformed, r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
*l++ = '\0';
while (*l && ap_isspace(*l)) {
++l;
}
if (!strcasecmp(w, "Content-type")) {
char *tmp;
/* Nuke trailing whitespace */
char *endp = l + strlen(l) - 1;
while (endp > l && ap_isspace(*endp)) {
*endp-- = '\0';
}
tmp = ap_pstrdup(r->pool, l);
ap_content_type_tolower(tmp);
r->content_type = tmp;
}
/*
* If the script returned a specific status, that's what
* we'll use - otherwise we assume 200 OK.
*/
else if (!strcasecmp(w, "Status")) {
r->status = cgi_status = atoi(l);
r->status_line = ap_pstrdup(r->pool, l);
}
else if (!strcasecmp(w, "Location")) {
ap_table_set(r->headers_out, w, l);
}
else if (!strcasecmp(w, "Content-Length")) {
ap_table_set(r->headers_out, w, l);
}
else if (!strcasecmp(w, "Transfer-Encoding")) {
ap_table_set(r->headers_out, w, l);
}
/*
* If the script gave us a Last-Modified header, we can't just
* pass it on blindly because of restrictions on future values.
*/
else if (!strcasecmp(w, "Last-Modified")) {
ap_update_mtime(r, ap_parseHTTPdate(l));
ap_set_last_modified(r);
}
else if (!strcasecmp(w, "Set-Cookie")) {
ap_table_add(cookie_table, w, l);
}
else {
ap_table_add(merge, w, l);
}
}
}
static int getsfunc_FILE(char *buf, int len, void *f)
{
return ap_fgets(buf, len, (ap_file_t *) f) == APR_SUCCESS;
}
API_EXPORT(int) ap_scan_script_header_err(request_rec *r, ap_file_t *f,
char *buffer)
{
return ap_scan_script_header_err_core(r, buffer, getsfunc_FILE, f);
}
static int getsfunc_BUFF(char *w, int len, void *fb)
{
return ap_bgets(w, len, (BUFF *) fb) > 0;
}
API_EXPORT(int) ap_scan_script_header_err_buff(request_rec *r, BUFF *fb,
char *buffer)
{
return ap_scan_script_header_err_core(r, buffer, getsfunc_BUFF, fb);
}
API_EXPORT(void) ap_send_size(ap_ssize_t size, request_rec *r)
{
/* XXX: this -1 thing is a gross hack */
if (size == (ap_ssize_t)-1) {
ap_rputs(" -", r);
}
else if (!size) {
ap_rputs(" 0k", r);
}
else if (size < 1024) {
ap_rputs(" 1k", r);
}
else if (size < 1048576) {
ap_rprintf(r, "%4" APR_SSIZE_T_FMT "k", (size + 512) / 1024);
}
else if (size < 103809024) {
ap_rprintf(r, "%4.1fM", size / 1048576.0);
}
else {
ap_rprintf(r, "%4" APR_SSIZE_T_FMT "M", (size + 524288) / 1048576);
}
}
#if defined(OS2) || defined(WIN32)
static char **create_argv_cmd(ap_context_t *p, char *av0, const char *args, char *path)
{
register int x, n;
char **av;
char *w;
for (x = 0, n = 2; args[x]; x++) {
if (args[x] == '+') {
++n;
}
}
/* Add extra strings to array. */
n = n + 2;
av = (char **) ap_palloc(p, (n + 1) * sizeof(char *));
av[0] = av0;
/* Now insert the extra strings we made room for above. */
av[1] = strdup("/C");
av[2] = strdup(path);
for (x = (1 + 2); x < n; x++) {
w = ap_getword(p, &args, '+');
ap_unescape_url(w);
av[x] = ap_escape_shell_cmd(p, w);
}
av[n] = NULL;
return av;
}
#endif