util_script.c revision 480c790b074c94e2a9b53ae768a4d6f710e5caa0
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* ====================================================================
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding *
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * Redistribution and use in source and binary forms, with or without
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * modification, are permitted provided that the following conditions
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * are met:
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 1. Redistributions of source code must retain the above copyright
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * notice, this list of conditions and the following disclaimer.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 2. Redistributions in binary form must reproduce the above copyright
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * notice, this list of conditions and the following disclaimer in
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * the documentation and/or other materials provided with the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * distribution.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 3. All advertising materials mentioning features or use of this
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * software must display the following acknowledgment:
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * "This product includes software developed by the Apache Group
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * for use in the Apache HTTP server project (http://www.apache.org/)."
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding *
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * 4. The names "Apache Server" and "Apache Group" must not be used to
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * endorse or promote products derived from this software without
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * prior written permission. For written permission, please contact
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * apache@apache.org.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * 5. Products derived from this software may not be called "Apache"
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * nor may "Apache" appear in their names without prior written
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * permission of the Apache Group.
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 6. Redistributions of any form whatsoever must retain the following
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * acknowledgment:
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * "This product includes software developed by the Apache Group
64185f9824e42f21ca7b9ae6c004484215c031a7rbb * for use in the Apache HTTP server project (http://www.apache.org/)."
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * OF THE POSSIBILITY OF SUCH DAMAGE.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * ====================================================================
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * This software consists of voluntary contributions made by many
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * individuals on behalf of the Apache Group and was originally based
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * on public domain software written at the National Center for
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * Supercomputing Applications, University of Illinois, Urbana-Champaign.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * For more information on the Apache Group and the Apache HTTP server
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * project, please see <http://www.apache.org/>.
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding *
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#define CORE_PRIVATE
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "httpd.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "http_config.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "http_main.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "http_log.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "http_core.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "http_protocol.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "http_request.h" /* for sub_req_lookup_uri() */
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "util_script.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "util_date.h" /* For parseHTTPdate() */
1b21d7b3d97def358b2e923655edeb16613a1c31gstein
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#ifdef OS2
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#define INCL_DOS
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include <os2.h>
2d71630471d1c23f0137309e3c3957c633ecbfd6rbb#endif
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Various utility functions which are common to a whole lot of
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * script-type extensions mechanisms, and might as well be gathered
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * in one place (if only to avoid creating inter-module dependancies
51af95bb51b5084e883bad250b2afa2838e9ceebfielding * where there don't have to be).
d4f1d9c1ff112a8ab9bee31f196973761329b236rbb */
7fae9cc4639013f3c04c085547256c68814aee8ftrawick
7fae9cc4639013f3c04c085547256c68814aee8ftrawick#define MALFORMED_MESSAGE "malformed header from script. Bad header="
7fae9cc4639013f3c04c085547256c68814aee8ftrawick#define MALFORMED_HEADER_LENGTH_TO_SHOW 30
7fae9cc4639013f3c04c085547256c68814aee8ftrawick
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#if defined(OS2) || defined(WIN32)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* If a request includes query info in the URL (stuff after "?"), and
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * the query info does not contain "=" (indicative of a FORM submission),
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * then this routine is called to create the argument list to be passed
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * to the CGI script. When suexec is enabled, the suexec path, user, and
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * group are the first three arguments to be passed; if not, all three
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * must be NULL. The query info is split into separate arguments, where
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * "+" is the separator between keyword arguments.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * XXXX: note that the WIN32 code uses one of the suexec strings
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * to pass an interpreter name. Remember this if changing the way they
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * are handled in create_argv.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic char **create_argv(ap_context_t *p, char *path, char *user, char *group,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *av0, const char *args)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding int x, numwords;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char **av;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *w;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding int idx = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* count the number of keywords */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (x = 0, numwords = 1; args[x]; x++) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (args[x] == '+') {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ++numwords;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm if (numwords > APACHE_ARG_MAX - 5) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding numwords = APACHE_ARG_MAX - 5; /* Truncate args to prevent overrun */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding av = (char **) ap_palloc(p, (numwords + 5) * sizeof(char *));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (path) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding av[idx++] = path;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (user) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding av[idx++] = user;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (group) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding av[idx++] = group;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding av[idx++] = av0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (x = 1; x <= numwords; x++) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding w = ap_getword_nulls(p, &args, '+');
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_unescape_url(w);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding av[idx++] = ap_escape_shell_cmd(p, w);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding av[idx] = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return av;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#endif /* defined(OS2) || defined(WIN32) */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic char *http2env(ap_context_t *a, char *w)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *res = ap_pstrcat(a, "HTTP_", w, NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *cp = res;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding while (*++cp) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!ap_isalnum(*cp) && *cp != '_') {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *cp = '_';
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *cp = ap_toupper(*cp);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return res;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingAPI_EXPORT(char **) ap_create_environment(ap_context_t *p, ap_table_t *t)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_array_header_t *env_arr = ap_table_elts(t);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_entry_t *elts = (ap_table_entry_t *) env_arr->elts;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char **env = (char **) ap_palloc(p, (env_arr->nelts + 2) * sizeof(char *));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding int i, j;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm char *tz;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *whack;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding j = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!ap_table_get(t, "TZ")) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding tz = getenv("TZ");
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (tz != NULL) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding env[j++] = ap_pstrcat(p, "TZ=", tz, NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (i = 0; i < env_arr->nelts; ++i) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!elts[i].key) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding continue;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding env[j] = ap_pstrcat(p, elts[i].key, "=", elts[i].val, NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding whack = env[j];
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm if (ap_isdigit(*whack)) {
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick *whack++ = '_';
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding while (*whack != '=') {
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick if (!ap_isalnum(*whack) && *whack != '_') {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *whack = '_';
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick }
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick ++whack;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick }
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick ++j;
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick env[j] = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return env;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawickAPI_EXPORT(void) ap_add_common_vars(request_rec *r)
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick{
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick ap_table_t *e;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick server_rec *s = r->server;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick conn_rec *c = r->connection;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick const char *rem_logname;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick char *env_path;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick#ifdef WIN32
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick char *env_temp;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick#endif
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick const char *host;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick ap_array_header_t *hdrs_arr = ap_table_elts(r->headers_in);
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick ap_table_entry_t *hdrs = (ap_table_entry_t *) hdrs_arr->elts;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick int i;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick /* use a temporary ap_table_t which we'll overlap onto
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick * r->subprocess_env later
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding e = ap_make_table(r->pool, 25 + hdrs_arr->nelts);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
eae32ab3fb398ca408bc2d45b22adf1b67a75471rbb /* First, add environment vars from headers... this is as per
066877f1a045103acfdd376d48cdd473c33f409bdougm * CGI specs, though other sorts of scripting interfaces see
eae32ab3fb398ca408bc2d45b22adf1b67a75471rbb * the same vars...
eae32ab3fb398ca408bc2d45b22adf1b67a75471rbb */
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick
066877f1a045103acfdd376d48cdd473c33f409bdougm for (i = 0; i < hdrs_arr->nelts; ++i) {
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick if (!hdrs[i].key) {
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick continue;
74b39333600dee3260355ad3a06e36ef6c61c8f1dreid }
066877f1a045103acfdd376d48cdd473c33f409bdougm
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick /* A few headers are special cased --- Authorization to prevent
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick * rogue scripts from capturing passwords; content-type and -length
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick * for no particular reason.
74b39333600dee3260355ad3a06e36ef6c61c8f1dreid */
74b39333600dee3260355ad3a06e36ef6c61c8f1dreid
74b39333600dee3260355ad3a06e36ef6c61c8f1dreid if (!strcasecmp(hdrs[i].key, "Content-type")) {
74b39333600dee3260355ad3a06e36ef6c61c8f1dreid ap_table_addn(e, "CONTENT_TYPE", hdrs[i].val);
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick }
066877f1a045103acfdd376d48cdd473c33f409bdougm else if (!strcasecmp(hdrs[i].key, "Content-length")) {
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick ap_table_addn(e, "CONTENT_LENGTH", hdrs[i].val);
d17890657bc529b3f9db20e5546511182b829565dreid }
d17890657bc529b3f9db20e5546511182b829565dreid /*
d17890657bc529b3f9db20e5546511182b829565dreid * You really don't want to disable this check, since it leaves you
d17890657bc529b3f9db20e5546511182b829565dreid * wide open to CGIs stealing passwords and people viewing them
d17890657bc529b3f9db20e5546511182b829565dreid * in the environment with "ps -e". But, if you must...
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#ifndef SECURITY_HOLE_PASS_AUTHORIZATION
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else if (!strcasecmp(hdrs[i].key, "Authorization")
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding || !strcasecmp(hdrs[i].key, "Proxy-Authorization")) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding continue;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm#endif
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, http2env(r->pool, hdrs[i].key), hdrs[i].val);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!(env_path = getenv("PATH"))) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding env_path = DEFAULT_PATH;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#ifdef WIN32
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (env_temp = getenv("SystemRoot")) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, "SystemRoot", env_temp);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (env_temp = getenv("COMSPEC")) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, "COMSPEC", env_temp);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (env_temp = getenv("WINDIR")) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, "WINDIR", env_temp);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#endif
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, "PATH", env_path);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, "SERVER_SIGNATURE", ap_psignature("", r));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, "SERVER_SOFTWARE", ap_get_server_version());
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, "SERVER_NAME", ap_get_server_name(r));
0e6e93183d91142d7cf9ffbf502114ff77bd9e19ben ap_table_addn(e, "SERVER_ADDR", r->connection->local_ip); /* Apache */
0e6e93183d91142d7cf9ffbf502114ff77bd9e19ben ap_table_addn(e, "SERVER_PORT",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_psprintf(r->pool, "%u", ap_get_server_port(r)));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding host = ap_get_remote_host(c, r->per_dir_config, REMOTE_HOST);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (host) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, "REMOTE_HOST", host);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, "REMOTE_ADDR", c->remote_ip);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, "DOCUMENT_ROOT", ap_document_root(r)); /* Apache */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ap_table_addn(e, "SERVER_ADMIN", s->server_admin); /* Apache */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, "SCRIPT_FILENAME", r->filename); /* Apache */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, "REMOTE_PORT",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_psprintf(r->pool, "%d", ntohs(c->remote_addr.sin_port)));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (r->user) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, "REMOTE_USER", r->user);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (r->ap_auth_type) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, "AUTH_TYPE", r->ap_auth_type);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding rem_logname = ap_get_remote_logname(r);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (rem_logname) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, "REMOTE_IDENT", ap_pstrdup(r->pool, rem_logname));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Apache custom error responses. If we have redirected set two new vars */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (r->prev) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (r->prev->args) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, "REDIRECT_QUERY_STRING", r->prev->args);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (r->prev->uri) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(e, "REDIRECT_URL", r->prev->uri);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_overlap_tables(r->subprocess_env, e, AP_OVERLAP_TABLES_SET);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm/* This "cute" little function comes about because the path info on
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * filenames and URLs aren't always the same. So we take the two,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * and find as much of the two that match as possible.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingAPI_EXPORT(int) ap_find_path_info(const char *uri, const char *path_info)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm int lu = strlen(uri);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding int lp = strlen(path_info);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding while (lu-- && lp-- && uri[lu] == path_info[lp]);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (lu == -1) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding lu = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding while (uri[lu] != '\0' && uri[lu] != '/') {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding lu++;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return lu;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* Obtain the Request-URI from the original request-line, returning
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * a new string from the request pool containing the URI or "".
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic char *original_uri(request_rec *r)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *first, *last;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (r->the_request == NULL) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return (char *) ap_pcalloc(r->pool, 1);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding first = r->the_request; /* use the request-line */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding while (*first && !ap_isspace(*first)) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ++first; /* skip over the method */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick while (ap_isspace(*first)) {
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick ++first; /* and the space(s) */
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick last = first;
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick while (*last && !ap_isspace(*last)) {
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick ++last; /* end at next whitespace */
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick }
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick return ap_pstrndup(r->pool, first, last - first);
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmAPI_EXPORT(void) ap_add_cgi_vars(request_rec *r)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_t *e = r->subprocess_env;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_setn(e, "GATEWAY_INTERFACE", "CGI/1.1");
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ap_table_setn(e, "SERVER_PROTOCOL", r->protocol);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_setn(e, "REQUEST_METHOD", r->method);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_setn(e, "QUERY_STRING", r->args ? r->args : "");
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_setn(e, "REQUEST_URI", original_uri(r));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Note that the code below special-cases scripts run from includes,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * because it "knows" that the sub_request has been hacked to have the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * args and path_info of the original request, and not any that may have
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * come with the script URI in the include command. Ugh.
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!strcmp(r->protocol, "INCLUDED")) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_setn(e, "SCRIPT_NAME", r->uri);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm if (r->path_info && *r->path_info) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_setn(e, "PATH_INFO", r->path_info);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else if (!r->path_info || !*r->path_info) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_setn(e, "SCRIPT_NAME", r->uri);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else {
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick int path_info_start = ap_find_path_info(r->uri, r->path_info);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_setn(e, "SCRIPT_NAME",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_pstrndup(r->pool, r->uri, path_info_start));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ap_table_setn(e, "PATH_INFO", r->path_info);
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (r->path_info && r->path_info[0]) {
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick /*
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick * To get PATH_TRANSLATED, treat PATH_INFO as a URI path.
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick * Need to re-escape it for this, since the entire URI was
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick * un-escaped before we determined where the PATH_INFO began.
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick */
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick request_rec *pa_req;
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick pa_req = ap_sub_req_lookup_uri(ap_escape_uri(r->pool, r->path_info), r);
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick if (pa_req->filename) {
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick#ifdef WIN32
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick char buffer[HUGE_STRING_LEN];
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#endif
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *pt = ap_pstrcat(r->pool, pa_req->filename, pa_req->path_info,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#ifdef WIN32
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* We need to make this a real Windows path name */
8f8ec0957334f50b7ac11359f90490ee467258eedreid GetFullPathName(pt, HUGE_STRING_LEN, buffer, NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_setn(e, "PATH_TRANSLATED", ap_pstrdup(r->pool, buffer));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#else
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_setn(e, "PATH_TRANSLATED", pt);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#endif
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_destroy_sub_req(pa_req);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic int set_cookie_doo_doo(void *v, const char *key, const char *val)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_addn(v, key, val);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return 1;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingAPI_EXPORT(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf int (*getsfunc) (char *, int, void *),
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding void *getsfunc_data)
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char x[MAX_STRING_LEN];
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick char *w, *l;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick int p;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf int cgi_status = HTTP_OK;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf ap_table_t *merge;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf ap_table_t *cookie_table;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick if (buffer) {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf *buffer = '\0';
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf w = buffer ? buffer : x;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick /* temporary place to hold headers to merge in later */
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf merge = ap_make_table(r->pool, 10);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf /* The HTTP specification says that it is legal to merge duplicate
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * headers into one. Some browsers that support Cookies don't like
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * merged headers and prefer that each Set-Cookie header is sent
066877f1a045103acfdd376d48cdd473c33f409bdougm * separately. Lets humour those browsers by not merging.
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * Oh what a pain it is.
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf */
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf cookie_table = ap_make_table(r->pool, 2);
066877f1a045103acfdd376d48cdd473c33f409bdougm ap_table_do(set_cookie_doo_doo, cookie_table, r->err_headers_out, "Set-Cookie", NULL);
1e75e6b00db385266648e76f94d6c192b26dcd2erbb
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf while (1) {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if ((*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data) == 0) {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
066877f1a045103acfdd376d48cdd473c33f409bdougm "Premature end of script headers: %s", r->filename);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf return HTTP_INTERNAL_SERVER_ERROR;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
066877f1a045103acfdd376d48cdd473c33f409bdougm
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf /* Delete terminal (CR?)LF */
066877f1a045103acfdd376d48cdd473c33f409bdougm
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf p = strlen(w);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if (p > 0 && w[p - 1] == '\n') {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if (p > 1 && w[p - 2] == '\015') {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf w[p - 2] = '\0';
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf else {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf w[p - 1] = '\0';
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
066877f1a045103acfdd376d48cdd473c33f409bdougm
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * If we've finished reading the headers, check to make sure any
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * HTTP/1.1 conditions are met. If so, we're done; normal processing
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * will handle the script's output. If not, just return the error.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * The appropriate thing to do would be to send the script process a
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * SIGPIPE to let it know we're ignoring it, close the channel to the
066877f1a045103acfdd376d48cdd473c33f409bdougm * script process, and *then* return the failed-to-meet-condition
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * error. Otherwise we'd be waiting for the script to finish
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * blithering before telling the client the output was no good.
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * However, we don't have the information to do that, so we have to
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * leave it to an upper layer.
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf */
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if (w[0] == '\0') {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf int cond_status = OK;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if ((cgi_status == HTTP_OK) && (r->method_number == M_GET)) {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf cond_status = ap_meets_conditions(r);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf ap_overlap_tables(r->err_headers_out, merge,
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf AP_OVERLAP_TABLES_MERGE);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if (!ap_is_empty_table(cookie_table)) {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf /* the cookies have already been copied to the cookie_table */
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf ap_table_unset(r->err_headers_out, "Set-Cookie");
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf r->err_headers_out = ap_overlay_tables(r->pool,
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf r->err_headers_out, cookie_table);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf return cond_status;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf /* if we see a bogus header don't ignore it. Shout and scream */
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf#ifdef CHARSET_EBCDIC
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf /* Chances are that we received an ASCII header text instead of
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * the expected EBCDIC header lines. Try to auto-detect:
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf */
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if (!(l = strchr(w, ':'))) {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf int maybeASCII = 0, maybeEBCDIC = 0;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf char *cp;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf for (cp = w; *cp != '\0'; ++cp) {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if (isprint(*cp) && !isprint(os_toebcdic[*cp]))
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf ++maybeEBCDIC;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if (!isprint(*cp) && isprint(os_toebcdic[*cp]))
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf ++maybeASCII;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if (maybeASCII > maybeEBCDIC) {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server,
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf "CGI Interface Error: Script headers apparently ASCII: (CGI = %s)", r->filename);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf ascii2ebcdic(w, w, cp - w);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf#endif
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if (!(l = strchr(w, ':'))) {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf char malformed[(sizeof MALFORMED_MESSAGE) + 1
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf + MALFORMED_HEADER_LENGTH_TO_SHOW];
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf strcpy(malformed, MALFORMED_MESSAGE);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf strncat(malformed, w, MALFORMED_HEADER_LENGTH_TO_SHOW);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if (!buffer) {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf /* Soak up all the script output - may save an outright kill */
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf while ((*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data)) {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf continue;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "%s: %s", malformed, r->filename);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return HTTP_INTERNAL_SERVER_ERROR;
ebc18d48bea83ee5ed7a1b4e30007e5192539829wrowe }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *l++ = '\0';
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding while (*l && ap_isspace(*l)) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ++l;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!strcasecmp(w, "Content-type")) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *tmp;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Nuke trailing whitespace */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *endp = l + strlen(l) - 1;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding while (endp > l && ap_isspace(*endp)) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *endp-- = '\0';
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding tmp = ap_pstrdup(r->pool, l);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_content_type_tolower(tmp);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding r->content_type = tmp;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * If the script returned a specific status, that's what
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * we'll use - otherwise we assume 200 OK.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else if (!strcasecmp(w, "Status")) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding r->status = cgi_status = atoi(l);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding r->status_line = ap_pstrdup(r->pool, l);
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf else if (!strcasecmp(w, "Location")) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_set(r->headers_out, w, l);
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf else if (!strcasecmp(w, "Content-Length")) {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf ap_table_set(r->headers_out, w, l);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf else if (!strcasecmp(w, "Transfer-Encoding")) {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf ap_table_set(r->headers_out, w, l);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf /*
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * If the script gave us a Last-Modified header, we can't just
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * pass it on blindly because of restrictions on future values.
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else if (!strcasecmp(w, "Last-Modified")) {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf time_t mtime = ap_parseHTTPdate(l);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_update_mtime(r, mtime);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_set_last_modified(r);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm else if (!strcasecmp(w, "Set-Cookie")) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_add(cookie_table, w, l);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_table_add(merge, w, l);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic int getsfunc_FILE(char *buf, int len, void *f)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick return fgets(buf, len, (FILE *) f) != NULL;
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick}
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanfAPI_EXPORT(int) ap_scan_script_header_err(request_rec *r, FILE *f,
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf char *buffer)
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf{
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf return ap_scan_script_header_err_core(r, buffer, getsfunc_FILE, f);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanfstatic int getsfunc_BUFF(char *w, int len, void *fb)
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf{
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf return ap_bgets(w, len, (BUFF *) fb) > 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingAPI_EXPORT(int) ap_scan_script_header_err_buff(request_rec *r, BUFF *fb,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *buffer)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick return ap_scan_script_header_err_core(r, buffer, getsfunc_BUFF, fb);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
3233e75959b9d5e99acedfcc50012a87d789abd0trawick
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanfAPI_EXPORT(void) ap_send_size(size_t size, request_rec *r)
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf{
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf /* XXX: this -1 thing is a gross hack */
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if (size == (size_t)-1) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_rputs(" -", r);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
0942697a9b5de44865676345a3828741c827efe6rbb else if (!size) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_rputs(" 0k", r);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else if (size < 1024) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_rputs(" 1k", r);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else if (size < 1048576) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_rprintf(r, "%4dk", (size + 512) / 1024);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else if (size < 103809024) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_rprintf(r, "%4.1fM", size / 1048576.0);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_rprintf(r, "%4dM", (size + 524288) / 1048576);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#if defined(OS2) || defined(WIN32)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic char **create_argv_cmd(ap_context_t *p, char *av0, const char *args, char *path)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding register int x, n;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char **av;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *w;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (x = 0, n = 2; args[x]; x++) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (args[x] == '+') {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ++n;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick /* Add extra strings to array. */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding n = n + 2;
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick av = (char **) ap_palloc(p, (n + 1) * sizeof(char *));
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick av[0] = av0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Now insert the extra strings we made room for above. */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding av[1] = strdup("/C");
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding av[2] = strdup(path);
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick for (x = (1 + 2); x < n; x++) {
066877f1a045103acfdd376d48cdd473c33f409bdougm w = ap_getword(p, &args, '+');
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick ap_unescape_url(w);
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick av[x] = ap_escape_shell_cmd(p, w);
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick }
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick av[n] = NULL;
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick return av;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm#endif
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* ZZZ need to look at this in more depth and convert to an AP func so we
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding can get rid of OS specific code.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#if 0
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingAPI_EXPORT(int) ap_call_exec(request_rec *r, ap_child_info_t *pinfo, char *argv0,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char **env, int shellcmd)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding int pid = 0;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf int errfileno = STDERR_FILENO;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf#if !defined(WIN32) && !defined(OS2)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* the fd on r->server->error_log is closed, but we need somewhere to
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * put the error messages from the log_* functions. So, we use stderr,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * since that is better than allowing errors to go unnoticed. Don't do
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * this on Win32, though, since we haven't fork()'d.
bbbf8f0e622ad5a37ccf70f35660fc755575278arbb */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ap_put_os_file(&r->server->error_log, &errfileno, r->pool);
066877f1a045103acfdd376d48cdd473c33f409bdougm#endif
93b653492fb3b4b65006e95a435629d806a9e589rbb
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* TODO: all that RLimit stuff should become part of the spawning API,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * and not something in the core_dir_config... define a special rlimit
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * structure and pass it in here.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#ifdef OS2
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Additions by Alec Kloss, to allow exec'ing of scripts under OS/2 */
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf int is_script = 0;
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf char interpreter[2048]; /* hope it's enough for the interpreter path */
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf char error_object[260];
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf FILE *program;
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf char *cmdline = r->filename, *cmdline_pos;
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf int cmdlen;
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf char *args = "", *args_end;
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf ULONG rc;
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf RESULTCODES rescodes;
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf int env_len, e;
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf char *env_block, *env_block_pos;
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (r->args && r->args[0] && !strchr(r->args, '='))
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding args = r->args;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick program = fopen(r->filename, "rt");
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick if (!program) {
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick ap_log_rerror(APLOG_MARK, APLOG_ERR, r, "fopen(%s) failed",
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf r->filename);
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick return (pid);
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick }
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding fgets(interpreter, sizeof(interpreter), program);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding fclose(program);
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick if (!strncmp(interpreter, "#!", 2)) {
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick is_script = 1;
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick interpreter[strlen(interpreter) - 1] = '\0';
71715c646d5231de578431f8961e711764b899d3fanf if (interpreter[2] != '/' && interpreter[2] != '\\' && interpreter[3] != ':') {
71715c646d5231de578431f8961e711764b899d3fanf char buffer[300];
71715c646d5231de578431f8961e711764b899d3fanf if (DosSearchPath(SEARCH_ENVIRONMENT, "PATH", interpreter+2, buffer, sizeof(buffer)) == 0) {
71715c646d5231de578431f8961e711764b899d3fanf strcpy(interpreter+2, buffer);
71715c646d5231de578431f8961e711764b899d3fanf } else {
71715c646d5231de578431f8961e711764b899d3fanf strcat(interpreter, ".exe");
71715c646d5231de578431f8961e711764b899d3fanf if (DosSearchPath(SEARCH_ENVIRONMENT, "PATH", interpreter+2, buffer, sizeof(buffer)) == 0) {
71715c646d5231de578431f8961e711764b899d3fanf strcpy(interpreter+2, buffer);
71715c646d5231de578431f8961e711764b899d3fanf }
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick }
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick }
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick }
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick if (is_script) {
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick cmdline = ap_pstrcat(r->pool, interpreter+2, " ", r->filename, NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf else if (strstr(strupr(r->filename), ".CMD") > 0) {
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf /* Special case to allow use of REXX commands as scripts. */
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf os2pathname(r->filename);
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf cmdline = ap_pstrcat(r->pool, SHELL_PATH, " /C ", r->filename, NULL);
5e4791fd54c10fdd77ca81bee8ac8398f16d6d6astoddard }
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf else {
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf cmdline = r->filename;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding args = ap_pstrdup(r->pool, args);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_unescape_url(args);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding args = ap_double_quotes(r->pool, args);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding args_end = args + strlen(args);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm if (args_end - args > 4000) { /* cmd.exe won't handle lines longer than 4k */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding args_end = args + 4000;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *args_end = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* +4 = 1 space between progname and args, 2 for double null at end, 2 for possible quote on first arg */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cmdlen = strlen(cmdline) + strlen(args) + 4;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cmdline_pos = cmdline;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding while (*cmdline_pos) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cmdlen += 2 * (*cmdline_pos == '+'); /* Allow space for each arg to be quoted */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cmdline_pos++;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cmdline = ap_pstrndup(r->pool, cmdline, cmdlen);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cmdline_pos = cmdline + strlen(cmdline);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding while (args < args_end) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *arg;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding arg = ap_getword_nc(r->pool, &args, '+');
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (strpbrk(arg, "&|<> "))
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding arg = ap_pstrcat(r->pool, "\"", arg, "\"", NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *(cmdline_pos++) = ' ';
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding strcpy(cmdline_pos, arg);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cmdline_pos += strlen(cmdline_pos);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *(++cmdline_pos) = 0; /* Add required second terminator */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding args = strchr(cmdline, ' ');
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (args) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *args = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding args++;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
3d96ee83babeec32482c9082c9426340cee8c44dwrowe
eaa7fe3cc26f8118ed72e5811aead02328d62448dreid /* Create environment block from list of envariables */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (env_len=1, e=0; env[e]; e++)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding env_len += strlen(env[e]) + 1;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding env_block = ap_palloc(r->pool, env_len);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding env_block_pos = env_block;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (e=0; env[e]; e++) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding strcpy(env_block_pos, env[e]);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding env_block_pos += strlen(env_block_pos) + 1;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *env_block_pos = 0; /* environment block is terminated by a double null */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding rc = DosExecPgm(error_object, sizeof(error_object), EXEC_ASYNC, cmdline, env_block, &rescodes, cmdline);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (rc) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_log_rerror(APLOG_MARK, APLOG_ERR, r, "DosExecPgm(%s %s) failed, %s - %s",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cmdline, args ? args : "", ap_os_error_message(rc), error_object );
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return -1;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return rescodes.codeTerminate;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#elif defined(WIN32)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Adapted from Alec Kloss' work for OS/2 */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *interpreter = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *arguments = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *ext = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *exename = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *s = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *quoted_filename;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *pCommand;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *pEnvBlock, *pNext;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding int i;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding int iEnvBlockLen;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding file_type_e fileType;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding STARTUPINFO si;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding PROCESS_INFORMATION pi;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding memset(&si, 0, sizeof(si));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding memset(&pi, 0, sizeof(pi));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding pid = -1;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!shellcmd) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding fileType = ap_get_win32_interpreter(r, &interpreter);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (fileType == eFileTypeUNKNOWN) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, r,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "%s is not executable; ensure interpreted scripts have "
dad234382d8424e1c5a30af2838e172aec9d6d1bdreid "\"#!\" first line",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding r->filename);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return (pid);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
382fa07a63096c4a1aabfed36433ea5ac9c40ad0trawick
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Look at the arguments...
066877f1a045103acfdd376d48cdd473c33f409bdougm */
066877f1a045103acfdd376d48cdd473c33f409bdougm arguments = "";
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if ((r->args) && (r->args[0]) && !strchr(r->args, '=')) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* If we are in this leg, there are some other arguments
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * that we must include in the execution of the CGI.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Because CreateProcess is the way it is, we have to
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * create a command line like format for the execution
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * of the CGI. This means we need to create on long
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * string with the executable and arguments.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * The arguments string comes in the request structure,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * and each argument is separated by a '+'. We'll replace
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * these pluses with spaces.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding int iStringSize = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding int x;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Duplicate the request structure string so we don't change it.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding arguments = ap_pstrdup(r->pool, r->args);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Change the '+' to ' '
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (x=0; arguments[x]; x++) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if ('+' == arguments[x]) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding arguments[x] = ' ';
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * We need to unescape any characters that are
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * in the arguments list.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_unescape_url(arguments);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding arguments = ap_escape_shell_cmd(r->pool, arguments);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * We have the interpreter (if there is one) and we have
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * the arguments (if there are any).
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Build the command string to pass to CreateProcess.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding quoted_filename = ap_pstrcat(r->pool, "\"", r->filename, "\"", NULL);
51af95bb51b5084e883bad250b2afa2838e9ceebfielding if (interpreter && *interpreter) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding pCommand = ap_pstrcat(r->pool, interpreter, " ",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding quoted_filename, " ", arguments, NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding pCommand = ap_pstrcat(r->pool, quoted_filename, " ", arguments, NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding } else {
dad234382d8424e1c5a30af2838e172aec9d6d1bdreid
382fa07a63096c4a1aabfed36433ea5ac9c40ad0trawick char *shell_cmd = "CMD.EXE /C ";
382fa07a63096c4a1aabfed36433ea5ac9c40ad0trawick OSVERSIONINFO osver;
066877f1a045103acfdd376d48cdd473c33f409bdougm osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
066877f1a045103acfdd376d48cdd473c33f409bdougm
e93ddd15fd44e7542b9ace8567852ab2b7604bc7dreid /*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Use CMD.EXE for NT, COMMAND.COM for WIN95
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (GetVersionEx(&osver)) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (osver.dwPlatformId != VER_PLATFORM_WIN32_NT) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding shell_cmd = "COMMAND.COM /C ";
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding pCommand = ap_pstrcat(r->pool, shell_cmd, argv0, NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Make child process use hPipeOutputWrite as standard out,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * and make sure it does not show on screen.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding si.cb = sizeof(si);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding si.wShowWindow = SW_HIDE;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding si.hStdInput = pinfo->hPipeInputRead;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding si.hStdOutput = pinfo->hPipeOutputWrite;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding si.hStdError = pinfo->hPipeErrorWrite;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Win32's CreateProcess call requires that the environment
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * be passed in an environment block, a null terminated block of
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * null terminated strings.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding i = 0;
51af95bb51b5084e883bad250b2afa2838e9ceebfielding iEnvBlockLen = 1;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding while (env[i]) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding iEnvBlockLen += strlen(env[i]) + 1;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding i++;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding pEnvBlock = (char *)ap_pcalloc(r->pool,iEnvBlockLen);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding i = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding pNext = pEnvBlock;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm while (env[i]) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding strcpy(pNext, env[i]);
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf pNext = pNext + strlen(pNext) + 1;
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf i++;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (CreateProcess(NULL, pCommand, NULL, NULL, TRUE, DETACHED_PROCESS, pEnvBlock,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_make_dirstr_parent(r->pool, r->filename),
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding &si, &pi)) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (fileType == eFileTypeEXE16) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Hack to get 16-bit CGI's working. It works for all the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * standard modules shipped with Apache. pi.dwProcessId is 0
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * for 16-bit CGIs and all the Unix specific code that calls
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * ap_call_exec interprets this as a failure case. And we can't
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * use -1 either because it is mapped to 0 by the caller.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding pid = -2;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding pid = pi.dwProcessId;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /*
8f8ec0957334f50b7ac11359f90490ee467258eedreid * We must close the handles to the new process and its main thread
382fa07a63096c4a1aabfed36433ea5ac9c40ad0trawick * to prevent handle and memory leaks.
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm */
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick CloseHandle(pi.hProcess);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding CloseHandle(pi.hThread);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
51af95bb51b5084e883bad250b2afa2838e9ceebfielding return (pid);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#else
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf /* TODO: reimplement suexec */
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf#if 0
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (ap_suexec_enabled
066877f1a045103acfdd376d48cdd473c33f409bdougm && ((r->server->server_uid != ap_user_id)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding || (r->server->server_gid != ap_group_id)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding || (!strncmp("/~", r->uri, 2)))) {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
51af95bb51b5084e883bad250b2afa2838e9ceebfielding char *execuser, *grpname;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf struct passwd *pw;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding struct group *gr;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if (!strncmp("/~", r->uri, 2)) {
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf gid_t user_gid;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf char *username = ap_pstrdup(r->pool, r->uri + 2);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf char *pos = strchr(username, '/');
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
if (pos) {
*pos = '\0';
}
if ((pw = getpwnam(username)) == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
"getpwnam: invalid username %s", username);
return (pid);
}
execuser = ap_pstrcat(r->pool, "~", pw->pw_name, NULL);
user_gid = pw->pw_gid;
if ((gr = getgrgid(user_gid)) == NULL) {
if ((grpname = ap_palloc(r->pool, 16)) == NULL) {
return (pid);
}
else {
ap_snprintf(grpname, 16, "%ld", (long) user_gid);
}
}
else {
grpname = gr->gr_name;
}
}
else {
if ((pw = getpwuid(r->server->server_uid)) == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
"getpwuid: invalid userid %ld",
(long) r->server->server_uid);
return (pid);
}
execuser = ap_pstrdup(r->pool, pw->pw_name);
if ((gr = getgrgid(r->server->server_gid)) == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
"getgrgid: invalid groupid %ld",
(long) r->server->server_gid);
return (pid);
}
grpname = gr->gr_name;
}
if (shellcmd) {
execle(SUEXEC_BIN, SUEXEC_BIN, execuser, grpname, argv0,
NULL, env);
}
else if ((!r->args) || (!r->args[0]) || strchr(r->args, '=')) {
execle(SUEXEC_BIN, SUEXEC_BIN, execuser, grpname, argv0,
NULL, env);
}
else {
execve(SUEXEC_BIN,
create_argv(r->pool, SUEXEC_BIN, execuser, grpname,
argv0, r->args),
env);
}
}
else {
#endif
if (shellcmd) {
execle(SHELL_PATH, SHELL_PATH, "-c", argv0, NULL, env);
}
else if ((!r->args) || (!r->args[0]) || strchr(r->args, '=')) {
execle(r->filename, argv0, NULL, env);
}
else {
execve(r->filename,
create_argv(r->pool, NULL, NULL, NULL, argv0, r->args),
env);
}
#if 0
}
#endif
return (pid);
#endif
}
#endif