mod_setenvif.c revision fd0edaa8e3d4dd67d0604ccef2e96b071db96643
b87efd3db0d2dc41615ea28669faf80fc1b48d56Corneliu-Claudiu Prodescu/* ====================================================================
b87efd3db0d2dc41615ea28669faf80fc1b48d56Corneliu-Claudiu Prodescu * Copyright (c) 1996-1999 The Apache Group. All rights reserved.
b87efd3db0d2dc41615ea28669faf80fc1b48d56Corneliu-Claudiu Prodescu * Redistribution and use in source and binary forms, with or without
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu * modification, are permitted provided that the following conditions
b87efd3db0d2dc41615ea28669faf80fc1b48d56Corneliu-Claudiu Prodescu * 1. Redistributions of source code must retain the above copyright
b87efd3db0d2dc41615ea28669faf80fc1b48d56Corneliu-Claudiu Prodescu * notice, this list of conditions and the following disclaimer.
b87efd3db0d2dc41615ea28669faf80fc1b48d56Corneliu-Claudiu Prodescu * 2. Redistributions in binary form must reproduce the above copyright
b87efd3db0d2dc41615ea28669faf80fc1b48d56Corneliu-Claudiu Prodescu * notice, this list of conditions and the following disclaimer in
81c09542d31779a407196fbd710aa8e69a815522Georgel Calin * the documentation and/or other materials provided with the
902bfaac7e88afebb6684fe1f2414ae2efbc7edfChristian Maeder * distribution.
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * 3. All advertising materials mentioning features or use of this
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * software must display the following acknowledgment:
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * "This product includes software developed by the Apache Group
81c09542d31779a407196fbd710aa8e69a815522Georgel Calin * for use in the Apache HTTP server project (http://www.apache.org/)."
05d4bb01e11ad19f2efb41a3efec59a7784c9942Christian Maeder * 4. The names "Apache Server" and "Apache Group" must not be used to
05d4bb01e11ad19f2efb41a3efec59a7784c9942Christian Maeder * endorse or promote products derived from this software without
81c09542d31779a407196fbd710aa8e69a815522Georgel Calin * prior written permission. For written permission, please contact
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * apache@apache.org.
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * 5. Products derived from this software may not be called "Apache"
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * nor may "Apache" appear in their names without prior written
902bfaac7e88afebb6684fe1f2414ae2efbc7edfChristian Maeder * permission of the Apache Group.
81c09542d31779a407196fbd710aa8e69a815522Georgel Calin * 6. Redistributions of any form whatsoever must retain the following
81c09542d31779a407196fbd710aa8e69a815522Georgel Calin * acknowledgment:
81c09542d31779a407196fbd710aa8e69a815522Georgel Calin * "This product includes software developed by the Apache Group
81c09542d31779a407196fbd710aa8e69a815522Georgel Calin * for use in the Apache HTTP server project (http://www.apache.org/)."
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
902bfaac7e88afebb6684fe1f2414ae2efbc7edfChristian Maeder * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
81c09542d31779a407196fbd710aa8e69a815522Georgel Calin * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
902bfaac7e88afebb6684fe1f2414ae2efbc7edfChristian Maeder * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
81c09542d31779a407196fbd710aa8e69a815522Georgel Calin * OF THE POSSIBILITY OF SUCH DAMAGE.
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * ====================================================================
902bfaac7e88afebb6684fe1f2414ae2efbc7edfChristian Maeder * This software consists of voluntary contributions made by many
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * individuals on behalf of the Apache Group and was originally based
902bfaac7e88afebb6684fe1f2414ae2efbc7edfChristian Maeder * on public domain software written at the National Center for
81c09542d31779a407196fbd710aa8e69a815522Georgel Calin * Supercomputing Applications, University of Illinois, Urbana-Champaign.
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * For more information on the Apache Group and the Apache HTTP server
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * project, please see <http://www.apache.org/>.
81c09542d31779a407196fbd710aa8e69a815522Georgel Calin * Set environment variables based on matching request headers or
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * attributes against regex strings
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * Paul Sutton <paul@ukweb.com> 27 Oct 1996
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * Based on mod_browser by Alexei Kosut <akosut@organic.com>
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * Used to set environment variables based on the incoming request headers,
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * or some selected other attributes of the request (e.g., the remote host
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * SetEnvIf name regex var ...
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * where name is either a HTTP request header name, or one of the
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * special values (see below). The 'value' of the header (or the
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * value of the special value from below) are compared against the
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * regex argument. If this is a simple string, a simple sub-string
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * match is performed. Otherwise, a request expression match is
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * done. If the value matches the string or regular expression, the
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * environment variables listed as var ... are set. Each var can
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * be in one of three formats: var, which sets the named variable
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * (the value value "1"); var=value, which sets the variable to
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * the given value; or !var, which unsets the variable is it has
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * been previously set.
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * Normally the strings are compared with regard to case. To ignore
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * case, use the directive SetEnvIfNoCase instead.
902bfaac7e88afebb6684fe1f2414ae2efbc7edfChristian Maeder * Special values for 'name' are:
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * remote_host Remote host name (if available)
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * remote_addr Remote IP address
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * remote_user Remote authenticated user (if any)
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * request_method Request method (GET, POST, etc)
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * request_uri Requested URI
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * To set the enviroment variable LOCALHOST if the client is the local
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * SetEnvIf remote_addr 127.0.0.1 LOCALHOST
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * To set LOCAL if the client is the local host, or within our company's
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * domain (192.168.10):
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * SetEnvIf remote_addr 192.168.10. LOCAL
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * SetEnvIf remote_addr 127.0.0.1 LOCALHOST
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * This could be written as:
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * SetEnvIf remote_addr (127.0.0.1|192.168.10.) LOCAL
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calintypedef struct {
902bfaac7e88afebb6684fe1f2414ae2efbc7edfChristian Maeder table *features; /* env vars to set (or unset) */
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin ENUM_BITFIELD( /* is it a "special" header ? */
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calintypedef struct {
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calinstatic void *create_setenvif_config(pool *p, server_rec *dummy)
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin sei_cfg_rec *new = (sei_cfg_rec *) ap_palloc(p, sizeof(sei_cfg_rec));
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin new->conditionals = ap_make_array(p, 20, sizeof(sei_entry));
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin return (void *) new;
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calinstatic void *merge_setenvif_config(pool *p, void *basev, void *overridesv)
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin sei_cfg_rec *a = ap_pcalloc(p, sizeof(sei_cfg_rec));
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin sei_cfg_rec *base = basev, *overrides = overridesv;
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin a->conditionals = ap_append_arrays(p, base->conditionals,
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin/* any non-NULL magic constant will do... used to indicate if REG_ICASE should
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin#define ICASE_MAGIC ((void *)(&setenvif_module))
05d4bb01e11ad19f2efb41a3efec59a7784c9942Christian Maederstatic const char *add_setenvif_core(cmd_parms *cmd, void *mconfig,
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin sei_cfg_rec *sconf = ap_get_module_config(cmd->server->module_config,
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin sei_entry *new, *entries = (sei_entry *) sconf->conditionals->elts;
if (!*regex) {
if (!*feature) {
beenhere++;
if (*feature) {
if (!beenhere) {
return NULL;
const char *args)
char *fname;
if (!*fname) {
{ NULL },
const char *val;
char *last_name;
switch (b->special_type) {
case SPECIAL_REMOTE_ADDR:
case SPECIAL_REMOTE_HOST:
case SPECIAL_REMOTE_USER:
case SPECIAL_REQUEST_URI:
case SPECIAL_REQUEST_METHOD:
case SPECIAL_REQUEST_PROTOCOL:
case SPECIAL_NOT:
return DECLINED;
static void register_hooks(void)