mod_setenvif.c revision 26a4456dd6f1a5d7d7fff766551461a578687c4a
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2004 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
/*
* Set environment variables based on matching request headers or
* attributes against regex strings
*
* Paul Sutton <paul@ukweb.com> 27 Oct 1996
* Based on mod_browser by Alexei Kosut <akosut@organic.com>
*/
/*
* Used to set environment variables based on the incoming request headers,
* or some selected other attributes of the request (e.g., the remote host
* name).
*
* Usage:
*
* SetEnvIf name regex var ...
*
* where name is either a HTTP request header name, or one of the
* special values (see below). 'name' may be a regex when it is used
* to specify an HTTP request header name. The 'value' of the header
& (or the value of the special value from below) are compared against
* the regex argument. If this is a simple string, a simple sub-string
* match is performed. Otherwise, a request expression match is
* done. If the value matches the string or regular expression, the
* environment variables listed as var ... are set. Each var can
* be in one of three formats: var, which sets the named variable
* (the value value "1"); var=value, which sets the variable to
* the given value; or !var, which unsets the variable is it has
* been previously set.
*
* Normally the strings are compared with regard to case. To ignore
* case, use the directive SetEnvIfNoCase instead.
*
* Special values for 'name' are:
*
* server_addr IP address of interface on which request arrived
* (analogous to SERVER_ADDR set in ap_add_common_vars())
* remote_host Remote host name (if available)
* remote_addr Remote IP address
* remote_user Remote authenticated user (if any)
* request_method Request method (GET, POST, etc)
* request_uri Requested URI
*
* Examples:
*
* To set the enviroment variable LOCALHOST if the client is the local
* machine:
*
* SetEnvIf remote_addr 127.0.0.1 LOCALHOST
*
* To set LOCAL if the client is the local host, or within our company's
* domain (192.168.10):
*
* SetEnvIf remote_addr 192.168.10. LOCAL
* SetEnvIf remote_addr 127.0.0.1 LOCALHOST
*
* This could be written as:
*
* SetEnvIf remote_addr (127.0.0.1|192.168.10.) LOCAL
*
* To set HAVE_TS if the client request contains any header beginning
* with "TS" with a value beginning with a lower case alphabet:
*
* SetEnvIf ^TS* ^[a-z].* HAVE_TS
*/
#include "apr.h"
#include "apr_strings.h"
#include "apr_strmatch.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"
enum special {
};
typedef struct {
char *name; /* header name */
char *regex; /* regex to match against */
int icase; /* ignoring case? */
} sei_entry;
typedef struct {
} sei_cfg_rec;
/*
* These routines, the create- and merge-config functions, are called
* for both the server-wide and the per-directory contexts. This is
* because the different definitions are used at different times; the
* server-wide ones are used in the post-read-request phase, and the
* per-directory ones are used during the header-parse phase (after
* the URI has been mapped to a file and we have anything from the
* .htaccess file and <Directory> and <Files> containers).
*/
static void *create_setenvif_config(apr_pool_t *p)
{
return (void *) new;
}
{
return create_setenvif_config(p);
}
{
return create_setenvif_config(p);
}
{
return a;
}
/*
* any non-NULL magic constant will do... used to indicate if REG_ICASE should
* be used
*/
#define ICASE_MAGIC ((void *)(&setenvif_module))
#define SEI_MAGIC_HEIRLOOM "setenvif-phase-flag"
{
/* If a Header name contains characters other than:
* -,_,[A-Z\, [a-z] and [0-9].
* assume the header name is a regular expression.
*/
(REG_EXTENDED | REG_NOSUB ));
if (preg) {
return 1;
}
}
return 0;
}
/* If the input string does not take advantage of regular
* expression metacharacters, return a pointer to an equivalent
* string that can be searched using apr_strmatch(). (The
* returned string will often be the input string. But if
* the input string contains escaped characters, the returned
* string will be a copy with the escapes removed.)
*/
static const char *non_regex_pattern(apr_pool_t *p, const char *s)
{
const char *src = s;
int escapes_found = 0;
int in_escape = 0;
while (*src) {
switch (*src) {
case '^':
case '.':
case '$':
case '|':
case '(':
case ')':
case '[':
case ']':
case '*':
case '+':
case '?':
case '{':
case '}':
if (!in_escape) {
return NULL;
}
in_escape = 0;
break;
case '\\':
in_escape = 1;
escapes_found = 1;
break;
default:
if (in_escape) {
return NULL;
}
break;
}
src++;
}
if (!escapes_found) {
return s;
}
else {
src = s;
do {
if (*src == '\\') {
src++;
}
return unescaped;
}
}
{
char *regex;
const char *simple_pattern;
const char *feature;
char *var;
int i;
int beenhere = 0;
int icase;
/*
* Determine from our context into which record to put the entry.
* cmd->path == NULL means we're in server-wide context; otherwise,
* we're dealing with a per-directory setting.
*/
? (sei_cfg_rec *) mconfig
/* get regex */
if (!*regex) {
}
/*
* If we've already got a sei_entry with the same name we want to
* just copy the name pointer... so that later on we can compare
* two header names just by comparing the pointers.
*/
break;
}
}
/* if the last entry has an identical headername and regex then
* merge with it
*/
if (i < 0
/* no match, create a new entry */
simple_pattern, !icase);
" pattern could not be compiled.", NULL);
}
}
else {
" regex could not be compiled.", NULL);
}
}
}
}
}
}
}
}
}
else {
/* Handle fname as a regular expression.
* If fname a simple header string, identify as such
* (new->pnamereg = NULL) to avoid the overhead of searching
* through headers_in for a regex match.
*/
"Header name regex could not be "
"compiled.", NULL);
}
else {
}
}
}
else {
}
for ( ; ; ) {
if (!*feature) {
break;
}
beenhere++;
if (*feature) {
}
else if (*var == '!') {
}
else {
}
}
if (!beenhere) {
}
return NULL;
}
const char *args)
{
char *fname;
/* get header name */
if (!*fname) {
}
}
/*
* This routine handles the BrowserMatch* directives. It simply turns around
* and feeds them, with the appropriate embellishments, to the general-purpose
* command handler.
*/
{
}
static const command_rec setenvif_module_cmds[] =
{
"A header-name, regex and a list of variables."),
"a header-name, regex and a list of variables."),
"A browser regex and a list of variables."),
"A browser regex and a list of variables."),
{ NULL },
};
/*
* This routine gets called at two different points in request processing:
* once before the URI has been translated (during the post-read-request
* phase) and once after (during the header-parse phase). We use different
* config records for the two different calls to reduce overhead (by not
* re-doing the server-wide settings during directory processing), and
* signal which call it is by having the earlier one pass a flag to the
* later one.
*/
static int match_headers(request_rec *r)
{
const apr_table_entry_t *elts;
const char *val;
apr_size_t val_len = 0;
int i, j;
char *last_name;
}
else {
}
/* Optimize the case where a bunch of directives in a row use the
* same header. Remember we don't need to strcmp the two header
* names because we made sure the pointers were equal during
* configuration.
*/
switch (b->special_type) {
case SPECIAL_REMOTE_ADDR:
break;
case SPECIAL_SERVER_ADDR:
break;
case SPECIAL_REMOTE_HOST:
REMOTE_NAME, NULL);
break;
case SPECIAL_REMOTE_USER:
break;
case SPECIAL_REQUEST_URI:
break;
case SPECIAL_REQUEST_METHOD:
break;
case SPECIAL_REQUEST_PROTOCOL:
break;
case SPECIAL_NOT:
if (b->pnamereg) {
/* Matching headers_in against a regex. Iterate through
* the headers_in until we find a match or run out of
* headers.
*/
const apr_array_header_t
}
}
}
else {
/* Not matching against a regex */
}
}
}
}
/*
* A NULL value indicates that the header field or special entity
* wasn't present or is undefined. Represent that as an empty string
* so that REs like "^$" will work and allow envariable setting
* based on missing or empty field.
*/
val = "";
val_len = 0;
}
0))) {
}
else {
if (!b->pattern) {
if (replaced) {
replaced);
}
}
else {
}
}
}
}
}
return DECLINED;
}
static void register_hooks(apr_pool_t *p)
{
}
{
create_setenvif_config_dir, /* dir config creater */
merge_setenvif_config, /* dir merger --- default is to override */
create_setenvif_config_svr, /* server config */
merge_setenvif_config, /* merge server configs */
setenvif_module_cmds, /* command apr_table_t */
register_hooks /* register hooks */
};