util_script.c revision 8b9df7f6e74b117cf2b76665bb12499e4d55c1b3
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "apr.h"
#include "apr_lib.h"
#include "apr_strings.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"
#include <stdlib.h>
#endif
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_main.h"
#include "http_log.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_request.h" /* for sub_req_lookup_uri() */
#include "util_script.h"
#include "apr_date.h" /* For apr_date_parse_http() */
#include "util_ebcdic.h"
#ifdef OS2
#define INCL_DOS
#include <os2.h>
#endif
/*
* Various utility functions which are common to a whole lot of
* script-type extensions mechanisms, and might as well be gathered
* in one place (if only to avoid creating inter-module dependancies
* where there don't have to be).
*/
#define MALFORMED_MESSAGE "malformed header from script. Bad header="
#define MALFORMED_HEADER_LENGTH_TO_SHOW 30
static char *http2env(request_rec *r, const char *w)
{
char c;
*cp++ = 'H';
*cp++ = 'T';
*cp++ = 'T';
*cp++ = 'P';
*cp++ = '_';
while ((c = *w++) != 0) {
if (apr_isalnum(c)) {
*cp++ = apr_toupper(c);
}
else if (c == '-') {
*cp++ = '_';
}
else {
"Not exporting header with invalid name as envvar: %s",
ap_escape_logitem(r->pool, w));
return NULL;
}
}
*cp = 0;
return res;
}
{
}
}
{
}
{
int i, j;
char *tz;
char *whack;
j = 0;
if (!apr_table_get(t, "TZ")) {
}
}
continue;
}
if (apr_isdigit(*whack)) {
*whack++ = '_';
}
while (*whack != '=') {
*whack = '_';
}
++whack;
}
++j;
}
return env;
}
{
apr_table_t *e;
server_rec *s = r->server;
conn_rec *c = r->connection;
const char *env_temp;
int i;
/* use a temporary apr_table_t which we'll overlap onto
* r->subprocess_env later
* (exception: if r->subprocess_env is empty at the start,
* write directly into it)
*/
if (apr_is_empty_table(r->subprocess_env)) {
e = r->subprocess_env;
}
else {
}
/* First, add environment vars from headers... this is as per
* CGI specs, though other sorts of scripting interfaces see
* the same vars...
*/
continue;
}
/* A few headers are special cased --- Authorization to prevent
* rogue scripts from capturing passwords; content-type and -length
* for no particular reason.
*/
}
}
/*
* You really don't want to disable this check, since it leaves you
* wide open to CGIs stealing passwords and people viewing them
* in the environment with "ps -e". But, if you must...
*/
#ifndef SECURITY_HOLE_PASS_AUTHORIZATION
continue;
}
#endif
else
}
}
}
#if defined(WIN32)
env2env(e, "SystemRoot");
env2env(e, "COMSPEC");
env2env(e, "PATHEXT");
env2env(e, "WINDIR");
env2env(e, "COMSPEC");
env2env(e, "ETC");
env2env(e, "DPATH");
env2env(e, "PERLLIB_PREFIX");
env2env(e, "LIBRARY_PATH");
env2env(e, "DYLD_LIBRARY_PATH");
env2env(e, "LIBPATH");
/* HPUX PARISC 2.0W knows both, otherwise redundancy is harmless */
env2env(e, "SHLIB_PATH");
env2env(e, "LD_LIBRARY_PATH");
#else /* Some Unix */
env2env(e, "LD_LIBRARY_PATH");
#endif
apr_table_addn(e, "SERVER_NAME",
apr_table_addn(e, "SERVER_PORT",
add_unless_null(e, "REMOTE_HOST",
if (r->user) {
}
else if (r->prev) {
while (back) {
break;
}
}
}
if (env_temp) {
}
/* Apache custom error responses. If we have redirected set two new vars */
if (r->prev) {
}
if (e != r->subprocess_env) {
}
}
/* 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.
*/
{
}
}
if (lu == -1) {
lu = 0;
}
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)
{
if (r->the_request == NULL) {
}
++first; /* skip over the method */
}
while (apr_isspace(*first)) {
++first; /* and the space(s) */
}
++last; /* end at next whitespace */
}
}
{
apr_table_t *e = r->subprocess_env;
/* 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.
*/
}
}
}
else {
apr_table_setn(e, "SCRIPT_NAME",
}
/*
* 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.
*/
NULL);
NULL);
#ifdef WIN32
/* We need to make this a real Windows path name */
#endif
}
}
}
{
return 1;
}
#define HTTP_UNSET (-HTTP_OK)
int (*getsfunc) (char *, int, void *),
void *getsfunc_data)
{
char x[MAX_STRING_LEN];
char *w, *l;
int p;
int cgi_status = HTTP_UNSET;
if (buffer) {
*buffer = '\0';
}
/* temporary place to hold headers to merge in later */
/* 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.
*/
while (1) {
if (rv == 0) {
"Premature end of script headers: %s",
return HTTP_INTERNAL_SERVER_ERROR;
}
else if (rv == -1) {
"Script timed out before returning headers: %s",
return HTTP_GATEWAY_TIME_OUT;
}
/* Delete terminal (CR?)LF */
p = strlen(w);
/* Indeed, the host's '\n':
'\012' for UNIX; '\015' for MacOS; '\025' for OS/390
-- whatever the script generates.
*/
if (p > 0 && w[p - 1] == '\n') {
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;
/* PR#38070: This fails because it gets confused when a
* CGI Status header overrides ap_meets_conditions.
*
* We can fix that by dropping ap_meets_conditions when
* Status has been set. Since this is the only place
* cgi_status gets used, let's test it explicitly.
*
* The alternative would be to ignore CGI Status when
* ap_meets_conditions returns anything interesting.
* That would be safer wrt HTTP, but would break CGI.
*/
}
if (!apr_is_empty_table(cookie_table)) {
/* the cookies have already been copied to the cookie_table */
r->err_headers_out, cookie_table);
}
return cond_status;
}
/* if we see a bogus header don't ignore it. Shout and scream */
/* 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;
++maybeEBCDIC;
++maybeASCII;
}
if (maybeASCII > maybeEBCDIC) {
"CGI Interface Error: Script headers apparently ASCII: (CGI = %s)",
r->filename);
w, &inbytes_left, w, &outbytes_left);
}
}
#endif /*APR_CHARSET_EBCDIC*/
if (!(l = strchr(w, ':'))) {
if (!buffer) {
/* Soak up all the script output - may save an outright kill */
continue;
}
}
"%s: %s", malformed,
return HTTP_INTERNAL_SERVER_ERROR;
}
*l++ = '\0';
while (*l && apr_isspace(*l)) {
++l;
}
if (!strcasecmp(w, "Content-type")) {
char *tmp;
/* Nuke trailing whitespace */
*endp-- = '\0';
}
ap_set_content_type(r, tmp);
}
/*
* If the script returned a specific status, that's what
* we'll use - otherwise we assume 200 OK.
*/
else if (!strcasecmp(w, "Status")) {
}
else if (!strcasecmp(w, "Location")) {
apr_table_set(r->headers_out, w, l);
}
else if (!strcasecmp(w, "Content-Length")) {
apr_table_set(r->headers_out, w, l);
}
else if (!strcasecmp(w, "Content-Range")) {
apr_table_set(r->headers_out, w, l);
}
else if (!strcasecmp(w, "Transfer-Encoding")) {
apr_table_set(r->headers_out, w, l);
}
else if (!strcasecmp(w, "ETag")) {
apr_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, apr_date_parse_http(l));
}
else if (!strcasecmp(w, "Set-Cookie")) {
apr_table_add(cookie_table, w, l);
}
else {
apr_table_add(merge, w, l);
}
}
/* never reached - we leave this function within the while loop above */
return OK;
}
{
}
char *buffer)
{
}
{
int done = 0;
const char *bucket_data;
const char *src;
const char *src_end;
apr_bucket * next;
}
src = bucket_data;
if (*src == '\n') {
done = 1;
}
else if (*src != '\r') {
}
src++;
}
}
next = APR_BUCKET_NEXT(e);
e = next;
}
*dst = 0;
return 1;
}
char *buffer)
{
}
struct vastrs {
int arg;
const char *curpos;
};
{
const char *p;
int t;
return 0;
if (p)
++p;
else
if (t > len)
t = len;
w[t] = '\0';
}
else
return t;
}
/* ap_scan_script_header_err_strs() accepts additional const char* args...
* each is treated as one or more header lines, and the first non-header
* character is returned to **arg, **data. (The first optional arg is
* counted as 0.)
*/
char *buffer,
const char **termch,
int *termarg, ...)
{
int res;
if (termch)
if (termarg)
return res;
}
static void
{
char *key;
char *value;
char *strtok_state;
return;
}
while (key) {
if (value) {
value++; /* Skip passed the = */
}
else {
value = "1";
}
}
}
{
*table = t;
}