home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / linux / apache / contrib / modules / probably_obsolete / mod_env.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-11  |  9.8 KB  |  292 lines

  1. /* ====================================================================
  2.  * Copyright (c) 1995 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.
  24.  *
  25.  * 5. Redistributions of any form whatsoever must retain the following
  26.  *    acknowledgment:
  27.  *    "This product includes software developed by the Apache Group
  28.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  29.  *
  30.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  31.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  34.  * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  42.  * ====================================================================
  43.  *
  44.  * This software consists of voluntary contributions made by many
  45.  * individuals on behalf of the Apache Group and was originally based
  46.  * on public domain software written at the National Center for
  47.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  48.  * For more information on the Apache Group and the Apache HTTP server
  49.  * project, please see <http://www.apache.org/>.
  50.  *
  51.  */
  52.  
  53. /*
  54.  * mod_env.c
  55.  * version 0.0.5
  56.  * status beta
  57.  * Pass environment variables to CGI/SSI scripts.
  58.  * 
  59.  * Andrew Wilson <Andrew.Wilson@cm.cf.ac.uk> 06.Dec.95
  60.  *
  61.  * Change log:
  62.  * 08.Dec.95 Now allows PassEnv directive to appear more than once in
  63.  *           conf files.
  64.  * 10.Dec.95 optimisation.  getenv() only called at startup and used 
  65.  *           to build a fast-to-access table.  table used to build 
  66.  *           per-server environment for each request.
  67.  *           robustness.  better able to handle errors in configuration
  68.  *           files:
  69.  *           1)  PassEnv directive present, but no environment variable listed
  70.  *           2)  PassEnv FOO present, but $FOO not present in environment
  71.  *           3)  no PassEnv directive present
  72.  * 23.Dec.95 Now allows SetEnv directive with same semantics as 'sh' setenv:
  73.  *        SetEnv Var    sets Var to the empty string
  74.  *        SetEnv Var Val    sets Var to the value Val
  75.  *         Values containing whitespace should be quoted, eg:
  76.  *        SetEnv Var "this is some text"
  77.  *         Environment variables take their value from the last instance
  78.  *         of PassEnv / SetEnv to be reached in the configuration file.
  79.  *         For example, the sequence:
  80.  *        PassEnv PATH
  81.  *        SetEnv PATH /special/path
  82.  *         Causes PATH to take the value '/special/path'.
  83.  * 23.Feb.96 Added UnsetEnv directive to allow environment variables
  84.  *           to be removed.
  85.  *           Virtual hosts now 'inherit' parent server environment which
  86.  *         they're able to overwrite with their own directives or
  87.  *         selectively ignore with UnsetEnv.
  88.  *       *** IMPORTANT - the way that virtual hosts inherit their ***
  89.  *       *** environment variables from the default server's      ***
  90.  *     *** configuration has changed.  You should test your     ***
  91.  *       *** configuration carefully before accepting this        ***
  92.  *       *** version of the module in a live webserver which used ***
  93.  *     *** older versions of the module.                        ***
  94.  */
  95.  
  96. #include "httpd.h"
  97. #include "http_config.h"
  98.  
  99. /* utility functions, in the module code for now, but could
  100.  * be moved to alloc.c when they've been proofed.
  101.  */
  102.  
  103. void mod_env_table_unset( table *t, char *key ) 
  104. {
  105.     table_entry *elts = (table_entry *)t->elts;
  106.     int i;   
  107.     int j;   
  108.  
  109.     for (i = 0; i < t->nelts; ++i)
  110.         if (!strcasecmp (elts[i].key, key)) {
  111.  
  112.             /* found the element to skip over
  113.              * there are any number of ways to remove an element from
  114.              * a contiguous block of memory.  I've chosen one that
  115.              * doesn't do a memcpy/bcopy/array_delete, *shrug*...
  116.              */
  117.             j = i;
  118.             ++i;
  119.             for ( ; i < t->nelts; ) {
  120.                 elts[j].key = elts[i].key;
  121.                 elts[j].val = elts[i].val;
  122.                 ++i;
  123.                 ++j;
  124.             };
  125.             --t->nelts;
  126.  
  127.             return;
  128.         }
  129. }     
  130.  
  131. /* end of utility functions */
  132.  
  133. typedef struct {
  134.     table *vars;
  135.     char *unsetenv;
  136.     int vars_present;
  137. } env_server_config_rec;
  138.  
  139. module env_module;
  140.  
  141. void *create_env_server_config (pool *p, server_rec *dummy)
  142. {
  143.     env_server_config_rec *new =
  144.       (env_server_config_rec *) palloc (p, sizeof(env_server_config_rec));
  145.     new->vars = make_table (p, 50);
  146.     new->unsetenv = "";
  147.     new->vars_present = 0;
  148.     return (void *) new;
  149. }
  150.  
  151. void *merge_env_server_configs (pool *p, void *basev, void *addv)
  152. {
  153.     env_server_config_rec *base = (env_server_config_rec *)basev;
  154.     env_server_config_rec *add = (env_server_config_rec *)addv;
  155.     env_server_config_rec *new =
  156.       (env_server_config_rec *)palloc (p, sizeof(env_server_config_rec));
  157.  
  158.     table *new_table;
  159.     table_entry *elts;
  160.  
  161.     int i;
  162.     char *uenv, *copy;
  163.  
  164.       /* 
  165.        * new_table = copy_table( p, base->vars );
  166.        * foreach $element ( @add->vars ) {
  167.        *     table_set( new_table, $element.key, $element.val );
  168.        * };
  169.        * foreach $unsetenv ( @UNSETENV ) {
  170.        *     table_unset( new_table, $unsetenv );
  171.        * }
  172.        */
  173.  
  174.     new_table = copy_table( p, base->vars );
  175.  
  176.     elts = (table_entry *) add->vars->elts;
  177.  
  178.     for ( i = 0; i < add->vars->nelts; ++i ) {
  179.     table_set( new_table, elts[i].key, elts[i].val ); 
  180.     };
  181.  
  182.     copy = pstrdup( p, add->unsetenv );
  183.     uenv = getword_conf( p, © );
  184.     while ( uenv[0] != '\0' ) {
  185.     mod_env_table_unset( new_table, uenv );
  186.     uenv = getword_conf( p, © );
  187.     };
  188.  
  189.     new->vars = new_table;
  190.  
  191.     new->vars_present = base->vars_present || add->vars_present;
  192.  
  193.     return new;
  194. }
  195.  
  196. char *add_env_module_vars_passed (cmd_parms *cmd, char *struct_ptr, char *arg)
  197. {
  198.     env_server_config_rec *sconf =
  199.       get_module_config (cmd->server->module_config, &env_module);
  200.     table *vars = sconf->vars;
  201.     char *env_var;
  202.     char *name_ptr;
  203.  
  204.     while (*arg) {
  205.         name_ptr = getword_conf (cmd->pool, &arg);
  206.         env_var = getenv(name_ptr);
  207.         if ( env_var != NULL ) { 
  208.             sconf->vars_present = 1;
  209.             table_set (vars, name_ptr, env_var);
  210.         };
  211.     }
  212.     return NULL;
  213. }
  214.  
  215. char *add_env_module_vars_set (cmd_parms *cmd, char *struct_ptr, char *arg)
  216. {
  217.     env_server_config_rec *sconf =
  218.       get_module_config (cmd->server->module_config, &env_module);
  219.     table *vars = sconf->vars;
  220.     char *name, *value;
  221.  
  222.     name = getword_conf( cmd->pool, &arg );
  223.     value = getword_conf( cmd->pool, &arg );
  224.  
  225.     /* name is mandatory, value is optional.  no value means
  226.      * set the variable to an empty string
  227.      */
  228.  
  229.  
  230.     if ( (*name == '\0') || (*arg != '\0')) {
  231.     return "SetEnv takes one or two arguments.  An environment variable name and an optional value to pass to CGI." ;
  232.     };
  233.  
  234.     sconf->vars_present = 1;
  235.     table_set (vars, name, value);
  236.  
  237.     return NULL;
  238. }
  239.  
  240. char *add_env_module_vars_unset (cmd_parms *cmd, char *struct_ptr, char *arg)
  241. {
  242.     env_server_config_rec *sconf =
  243.       get_module_config (cmd->server->module_config, &env_module);
  244.     sconf->unsetenv = sconf->unsetenv ? 
  245.     pstrcat( cmd->pool, sconf->unsetenv, " ", arg, NULL ) : 
  246.     pstrdup( cmd->pool, arg );
  247.     return NULL;
  248. }
  249.  
  250. command_rec env_module_cmds[] = {
  251. { "PassEnv", add_env_module_vars_passed, NULL,
  252.     RSRC_CONF, RAW_ARGS, "a list of environment variables to pass to CGI." },
  253. { "SetEnv", add_env_module_vars_set, NULL,
  254.     RSRC_CONF, RAW_ARGS, "an environment variable name and a value to pass to CGI." },
  255. { "UnsetEnv", add_env_module_vars_unset, NULL,
  256.     RSRC_CONF, RAW_ARGS, "a list of variables to remove from the CGI environment." },
  257. { NULL },
  258. };
  259.  
  260. int fixup_env_module(request_rec *r)
  261. {
  262.     table *e = r->subprocess_env;
  263.     server_rec *s = r->server;
  264.     env_server_config_rec *sconf = get_module_config (s->module_config,
  265.                            &env_module);
  266.     table *vars = sconf->vars;
  267.  
  268.     if ( !sconf->vars_present ) return DECLINED;
  269.  
  270.     r->subprocess_env = overlay_tables( r->pool, e, vars );
  271.  
  272.     return OK;  
  273. }
  274.  
  275. module env_module = {
  276.    STANDARD_MODULE_STUFF,
  277.    NULL,            /* initializer */
  278.    NULL,            /* dir config creater */
  279.    NULL,            /* dir merger --- default is to override */
  280.    create_env_server_config,    /* server config */
  281.    merge_env_server_configs,    /* merge server configs */
  282.    env_module_cmds,        /* command table */
  283.    NULL,            /* handlers */
  284.    NULL,            /* filename translation */
  285.    NULL,            /* check_user_id */
  286.    NULL,            /* check auth */
  287.    NULL,            /* check access */
  288.    NULL,            /* type_checker */
  289.    fixup_env_module,        /* fixups */
  290.    NULL,            /* logger */
  291. };
  292.