home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / os2 / apach134.arj / APACH134.ZIP / src / modules / standard / mod_setenvif.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-01  |  13.2 KB  |  414 lines

  1. /* ====================================================================
  2.  * Copyright (c) 1996-1999 The Apache Group.  All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer. 
  10.  *
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in
  13.  *    the documentation and/or other materials provided with the
  14.  *    distribution.
  15.  *
  16.  * 3. All advertising materials mentioning features or use of this
  17.  *    software must display the following acknowledgment:
  18.  *    "This product includes software developed by the Apache Group
  19.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  20.  *
  21.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  22.  *    endorse or promote products derived from this software without
  23.  *    prior written permission. For written permission, please contact
  24.  *    apache@apache.org.
  25.  *
  26.  * 5. Products derived from this software may not be called "Apache"
  27.  *    nor may "Apache" appear in their names without prior written
  28.  *    permission of the Apache Group.
  29.  *
  30.  * 6. Redistributions of any form whatsoever must retain the following
  31.  *    acknowledgment:
  32.  *    "This product includes software developed by the Apache Group
  33.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  36.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  39.  * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Group and was originally based
  51.  * on public domain software written at the National Center for
  52.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  53.  * For more information on the Apache Group and the Apache HTTP server
  54.  * project, please see <http://www.apache.org/>.
  55.  *
  56.  */
  57.  
  58. /*
  59.  * mod_setenvif.c
  60.  * Set environment variables based on matching request headers or
  61.  * attributes against regex strings
  62.  * 
  63.  * Paul Sutton <paul@ukweb.com> 27 Oct 1996
  64.  * Based on mod_browser by Alexei Kosut <akosut@organic.com>
  65.  */
  66.  
  67. /*
  68.  * Used to set environment variables based on the incoming request headers,
  69.  * or some selected other attributes of the request (e.g., the remote host
  70.  * name).
  71.  *
  72.  * Usage:
  73.  *
  74.  *   SetEnvIf name regex var ...
  75.  *
  76.  * where name is either a HTTP request header name, or one of the
  77.  * special values (see below). The 'value' of the header (or the
  78.  * value of the special value from below) are compared against the
  79.  * regex argument. If this is a simple string, a simple sub-string
  80.  * match is performed. Otherwise, a request expression match is
  81.  * done. If the value matches the string or regular expression, the
  82.  * environment variables listed as var ... are set. Each var can 
  83.  * be in one of three formats: var, which sets the named variable
  84.  * (the value value "1"); var=value, which sets the variable to
  85.  * the given value; or !var, which unsets the variable is it has
  86.  * been previously set.
  87.  *
  88.  * Normally the strings are compared with regard to case. To ignore
  89.  * case, use the directive SetEnvIfNoCase instead.
  90.  *
  91.  * Special values for 'name' are:
  92.  *
  93.  *   remote_host        Remote host name (if available)
  94.  *   remote_addr        Remote IP address
  95.  *   remote_user        Remote authenticated user (if any)
  96.  *   request_method     Request method (GET, POST, etc)
  97.  *   request_uri        Requested URI
  98.  *
  99.  * Examples:
  100.  *
  101.  * To set the enviroment variable LOCALHOST if the client is the local
  102.  * machine:
  103.  *
  104.  *    SetEnvIf remote_addr 127.0.0.1 LOCALHOST
  105.  *
  106.  * To set LOCAL if the client is the local host, or within our company's
  107.  * domain (192.168.10):
  108.  *
  109.  *    SetEnvIf remote_addr 192.168.10. LOCAL
  110.  *    SetEnvIf remote_addr 127.0.0.1   LOCALHOST
  111.  *
  112.  * This could be written as:
  113.  *
  114.  *    SetEnvIf remote_addr (127.0.0.1|192.168.10.) LOCAL
  115.  */
  116.  
  117. #include "httpd.h"
  118. #include "http_config.h"
  119. #include "http_core.h"
  120. #include "http_log.h"
  121.  
  122. enum special {
  123.     SPECIAL_NOT,
  124.     SPECIAL_REMOTE_ADDR,
  125.     SPECIAL_REMOTE_HOST,
  126.     SPECIAL_REMOTE_USER,
  127.     SPECIAL_REQUEST_URI,
  128.     SPECIAL_REQUEST_METHOD
  129. };
  130. typedef struct {
  131.     char *name;                 /* header name */
  132.     char *regex;                /* regex to match against */
  133.     regex_t *preg;              /* compiled regex */
  134.     table *features;            /* env vars to set (or unset) */
  135.     ENUM_BITFIELD(              /* is it a "special" header ? */
  136.     enum special,
  137.     special_type,4);
  138.     unsigned icase : 1;        /* ignoring case? */
  139. } sei_entry;
  140.  
  141. typedef struct {
  142.     array_header *conditionals;
  143. } sei_cfg_rec;
  144.  
  145. module MODULE_VAR_EXPORT setenvif_module;
  146.  
  147. static void *create_setenvif_config(pool *p, server_rec *dummy)
  148. {
  149.     sei_cfg_rec *new = (sei_cfg_rec *) ap_palloc(p, sizeof(sei_cfg_rec));
  150.  
  151.     new->conditionals = ap_make_array(p, 20, sizeof(sei_entry));
  152.     return (void *) new;
  153. }
  154.  
  155. static void *merge_setenvif_config(pool *p, void *basev, void *overridesv)
  156. {
  157.     sei_cfg_rec *a = ap_pcalloc(p, sizeof(sei_cfg_rec));
  158.     sei_cfg_rec *base = basev, *overrides = overridesv;
  159.  
  160.     a->conditionals = ap_append_arrays(p, base->conditionals,
  161.                        overrides->conditionals);
  162.     return a;
  163. }
  164.  
  165. /* any non-NULL magic constant will do... used to indicate if REG_ICASE should
  166.  * be used
  167.  */
  168. #define ICASE_MAGIC    ((void *)(&setenvif_module))
  169.  
  170. static const char *add_setenvif_core(cmd_parms *cmd, void *mconfig,
  171.                      char *fname, const char *args)
  172. {
  173.     char *regex;
  174.     const char *feature;
  175.     sei_cfg_rec *sconf = ap_get_module_config(cmd->server->module_config,
  176.                           &setenvif_module);
  177.     sei_entry *new, *entries = (sei_entry *) sconf->conditionals->elts;
  178.     char *var;
  179.     int i;
  180.     int beenhere = 0;
  181.     unsigned icase;
  182.  
  183.     /* get regex */
  184.     regex = ap_getword_conf(cmd->pool, &args);
  185.     if (!*regex) {
  186.         return ap_pstrcat(cmd->pool, "Missing regular expression for ",
  187.               cmd->cmd->name, NULL);
  188.     }
  189.  
  190.     /*
  191.      * If we've already got a sei_entry with the same name we want to
  192.      * just copy the name pointer... so that later on we can compare
  193.      * two header names just by comparing the pointers.
  194.      */
  195.  
  196.     for (i = 0; i < sconf->conditionals->nelts; ++i) {
  197.         new = &entries[i];
  198.     if (!strcasecmp(new->name, fname)) {
  199.         fname = new->name;
  200.         break;
  201.     }
  202.     }
  203.  
  204.     /* if the last entry has an idential headername and regex then
  205.      * merge with it
  206.      */
  207.     i = sconf->conditionals->nelts - 1;
  208.     icase = cmd->info == ICASE_MAGIC;
  209.     if (i < 0
  210.     || entries[i].name != fname
  211.     || entries[i].icase != icase
  212.     || strcmp(entries[i].regex, regex)) {
  213.  
  214.     /* no match, create a new entry */
  215.  
  216.     new = ap_push_array(sconf->conditionals);
  217.     new->name = fname;
  218.     new->regex = regex;
  219.     new->icase = icase;
  220.     new->preg = ap_pregcomp(cmd->pool, regex,
  221.                 (REG_EXTENDED | REG_NOSUB
  222.                  | (icase ? REG_ICASE : 0)));
  223.     if (new->preg == NULL) {
  224.         return ap_pstrcat(cmd->pool, cmd->cmd->name,
  225.                   " regex could not be compiled.", NULL);
  226.     }
  227.     new->features = ap_make_table(cmd->pool, 2);
  228.  
  229.     if (!strcasecmp(fname, "remote_addr")) {
  230.         new->special_type = SPECIAL_REMOTE_ADDR;
  231.     }
  232.     else if (!strcasecmp(fname, "remote_host")) {
  233.         new->special_type = SPECIAL_REMOTE_HOST;
  234.     }
  235.     else if (!strcasecmp(fname, "remote_user")) {
  236.         new->special_type = SPECIAL_REMOTE_USER;
  237.     }
  238.     else if (!strcasecmp(fname, "request_uri")) {
  239.         new->special_type = SPECIAL_REQUEST_URI;
  240.     }
  241.     else if (!strcasecmp(fname, "request_method")) {
  242.         new->special_type = SPECIAL_REQUEST_METHOD;
  243.     }
  244.     else {
  245.         new->special_type = SPECIAL_NOT;
  246.     }
  247.     }
  248.     else {
  249.     new = &entries[i];
  250.     }
  251.  
  252.     for ( ; ; ) {
  253.     feature = ap_getword_conf(cmd->pool, &args);
  254.     if (!*feature) {
  255.         break;
  256.     }
  257.         beenhere++;
  258.  
  259.         var = ap_getword(cmd->pool, &feature, '=');
  260.         if (*feature) {
  261.             ap_table_setn(new->features, var, feature);
  262.         }
  263.         else if (*var == '!') {
  264.             ap_table_setn(new->features, var + 1, "!");
  265.         }
  266.         else {
  267.             ap_table_setn(new->features, var, "1");
  268.         }
  269.     }
  270.  
  271.     if (!beenhere) {
  272.         return ap_pstrcat(cmd->pool, "Missing envariable expression for ",
  273.               cmd->cmd->name, NULL);
  274.     }
  275.  
  276.     return NULL;
  277. }
  278.  
  279. static const char *add_setenvif(cmd_parms *cmd, void *mconfig,
  280.                 const char *args)
  281. {
  282.     char *fname;
  283.  
  284.     /* get header name */
  285.     fname = ap_getword_conf(cmd->pool, &args);
  286.     if (!*fname) {
  287.         return ap_pstrcat(cmd->pool, "Missing header-field name for ",
  288.               cmd->cmd->name, NULL);
  289.     }
  290.     return add_setenvif_core(cmd, mconfig, fname, args);
  291. }
  292.  
  293. /*
  294.  * This routine handles the BrowserMatch* directives.  It simply turns around
  295.  * and feeds them, with the appropriate embellishments, to the general-purpose
  296.  * command handler.
  297.  */
  298. static const char *add_browser(cmd_parms *cmd, void *mconfig, const char *args)
  299. {
  300.     return add_setenvif_core(cmd, mconfig, "User-Agent", args);
  301. }
  302.  
  303. static const command_rec setenvif_module_cmds[] =
  304. {
  305.     { "SetEnvIf", add_setenvif, NULL,
  306.       RSRC_CONF, RAW_ARGS, "A header-name, regex and a list of variables." },
  307.     { "SetEnvIfNoCase", add_setenvif, ICASE_MAGIC,
  308.       RSRC_CONF, RAW_ARGS, "a header-name, regex and a list of variables." },
  309.     { "BrowserMatch", add_browser, NULL,
  310.       RSRC_CONF, RAW_ARGS, "A browser regex and a list of variables." },
  311.     { "BrowserMatchNoCase", add_browser, ICASE_MAGIC,
  312.       RSRC_CONF, RAW_ARGS, "A browser regex and a list of variables." },
  313.     { NULL },
  314. };
  315.  
  316. static int match_headers(request_rec *r)
  317. {
  318.     server_rec *s = r->server;
  319.     sei_cfg_rec *sconf;
  320.     sei_entry *entries;
  321.     table_entry *elts;
  322.     const char *val;
  323.     int i, j;
  324.     char *last_name;
  325.  
  326.     sconf = (sei_cfg_rec *) ap_get_module_config(s->module_config,
  327.                          &setenvif_module);
  328.     entries = (sei_entry *) sconf->conditionals->elts;
  329.     last_name = NULL;
  330.     val = NULL;
  331.     for (i = 0; i < sconf->conditionals->nelts; ++i) {
  332.         sei_entry *b = &entries[i];
  333.  
  334.     /* Optimize the case where a bunch of directives in a row use the
  335.      * same header.  Remember we don't need to strcmp the two header
  336.      * names because we made sure the pointers were equal during
  337.      * configuration.
  338.      */
  339.     if (b->name != last_name) {
  340.         last_name = b->name;
  341.         switch (b->special_type) {
  342.         case SPECIAL_REMOTE_ADDR:
  343.         val = r->connection->remote_ip;
  344.         break;
  345.         case SPECIAL_REMOTE_HOST:
  346.         val =  ap_get_remote_host(r->connection, r->per_dir_config,
  347.                       REMOTE_NAME);
  348.         break;
  349.         case SPECIAL_REMOTE_USER:
  350.         val = r->connection->user;
  351.         break;
  352.         case SPECIAL_REQUEST_URI:
  353.         val = r->uri;
  354.         break;
  355.         case SPECIAL_REQUEST_METHOD:
  356.         val = r->method;
  357.         break;
  358.         case SPECIAL_NOT:
  359.         val = ap_table_get(r->headers_in, b->name);
  360.         break;
  361.         }
  362.         }
  363.  
  364.     /*
  365.      * A NULL value indicates that the header field or special entity
  366.      * wasn't present or is undefined.  Represent that as an empty string
  367.      * so that REs like "^$" will work and allow envariable setting
  368.      * based on missing or empty field.
  369.      */
  370.         if (val == NULL) {
  371.             val = "";
  372.         }
  373.  
  374.         if (!regexec(b->preg, val, 0, NULL, 0)) {
  375.         array_header *arr = ap_table_elts(b->features);
  376.             elts = (table_entry *) arr->elts;
  377.  
  378.             for (j = 0; j < arr->nelts; ++j) {
  379.                 if (!strcmp(elts[j].val, "!")) {
  380.                     ap_table_unset(r->subprocess_env, elts[j].key);
  381.                 }
  382.                 else {
  383.                     ap_table_setn(r->subprocess_env, elts[j].key, elts[j].val);
  384.                 }
  385.             }
  386.         }
  387.     }
  388.  
  389.     return DECLINED;
  390. }
  391.  
  392. module MODULE_VAR_EXPORT setenvif_module =
  393. {
  394.     STANDARD_MODULE_STUFF,
  395.     NULL,                       /* initializer */
  396.     NULL,                       /* dir config creater */
  397.     NULL,                       /* dir merger --- default is to override */
  398.     create_setenvif_config,     /* server config */
  399.     merge_setenvif_config,      /* merge server configs */
  400.     setenvif_module_cmds,       /* command table */
  401.     NULL,                       /* handlers */
  402.     NULL,                       /* filename translation */
  403.     NULL,                       /* check_user_id */
  404.     NULL,                       /* check auth */
  405.     NULL,                       /* check access */
  406.     NULL,                       /* type_checker */
  407.     NULL,                       /* fixups */
  408.     NULL,                       /* logger */
  409.     NULL,                       /* input header parse */
  410.     NULL,                       /* child (process) initialization */
  411.     NULL,                       /* child (process) rundown */
  412.     match_headers               /* post_read_request */
  413. };
  414.