home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / share / os2 / varios / apache / mod_env.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-27  |  8.9 KB  |  258 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. typedef struct {
  100.     table *vars;
  101.     char *unsetenv;
  102.     int vars_present;
  103. } env_server_config_rec;
  104.  
  105. module env_module;
  106.  
  107. void *create_env_server_config (pool *p, server_rec *dummy)
  108. {
  109.     env_server_config_rec *new =
  110.       (env_server_config_rec *) palloc (p, sizeof(env_server_config_rec));
  111.     new->vars = make_table (p, 50);
  112.     new->unsetenv = "";
  113.     new->vars_present = 0;
  114.     return (void *) new;
  115. }
  116.  
  117. void *merge_env_server_configs (pool *p, void *basev, void *addv)
  118. {
  119.     env_server_config_rec *base = (env_server_config_rec *)basev;
  120.     env_server_config_rec *add = (env_server_config_rec *)addv;
  121.     env_server_config_rec *new =
  122.       (env_server_config_rec *)palloc (p, sizeof(env_server_config_rec));
  123.  
  124.     table *new_table;
  125.     table_entry *elts;
  126.  
  127.     int i;
  128.     char *uenv, *copy;
  129.  
  130.       /* 
  131.        * new_table = copy_table( p, base->vars );
  132.        * foreach $element ( @add->vars ) {
  133.        *     table_set( new_table, $element.key, $element.val );
  134.        * };
  135.        * foreach $unsetenv ( @UNSETENV ) {
  136.        *     table_unset( new_table, $unsetenv );
  137.        * }
  138.        */
  139.  
  140.     new_table = copy_table( p, base->vars );
  141.  
  142.     elts = (table_entry *) add->vars->elts;
  143.  
  144.     for ( i = 0; i < add->vars->nelts; ++i ) {
  145.     table_set( new_table, elts[i].key, elts[i].val ); 
  146.     };
  147.  
  148.     copy = pstrdup( p, add->unsetenv );
  149.     uenv = getword_conf( p, © );
  150.     while ( uenv[0] != '\0' ) {
  151.     table_unset( new_table, uenv );
  152.     uenv = getword_conf( p, © );
  153.     };
  154.  
  155.     new->vars = new_table;
  156.  
  157.     new->vars_present = base->vars_present || add->vars_present;
  158.  
  159.     return new;
  160. }
  161.  
  162. char *add_env_module_vars_passed (cmd_parms *cmd, char *struct_ptr, char *arg)
  163. {
  164.     env_server_config_rec *sconf =
  165.       get_module_config (cmd->server->module_config, &env_module);
  166.     table *vars = sconf->vars;
  167.     char *env_var;
  168.     char *name_ptr;
  169.  
  170.     while (*arg) {
  171.         name_ptr = getword_conf (cmd->pool, &arg);
  172.         env_var = getenv(name_ptr);
  173.         if ( env_var != NULL ) { 
  174.             sconf->vars_present = 1;
  175.             table_set (vars, name_ptr, env_var);
  176.         };
  177.     }
  178.     return NULL;
  179. }
  180.  
  181. char *add_env_module_vars_set (cmd_parms *cmd, char *struct_ptr, char *arg)
  182. {
  183.     env_server_config_rec *sconf =
  184.       get_module_config (cmd->server->module_config, &env_module);
  185.     table *vars = sconf->vars;
  186.     char *name, *value;
  187.  
  188.     name = getword_conf( cmd->pool, &arg );
  189.     value = getword_conf( cmd->pool, &arg );
  190.  
  191.     /* name is mandatory, value is optional.  no value means
  192.      * set the variable to an empty string
  193.      */
  194.  
  195.  
  196.     if ( (*name == '\0') || (*arg != '\0')) {
  197.     return "SetEnv takes one or two arguments.  An environment variable name and an optional value to pass to CGI." ;
  198.     };
  199.  
  200.     sconf->vars_present = 1;
  201.     table_set (vars, name, value);
  202.  
  203.     return NULL;
  204. }
  205.  
  206. char *add_env_module_vars_unset (cmd_parms *cmd, char *struct_ptr, char *arg)
  207. {
  208.     env_server_config_rec *sconf =
  209.       get_module_config (cmd->server->module_config, &env_module);
  210.     sconf->unsetenv = sconf->unsetenv ? 
  211.     pstrcat( cmd->pool, sconf->unsetenv, " ", arg, NULL ) : 
  212.     pstrdup( cmd->pool, arg );
  213.     return NULL;
  214. }
  215.  
  216. command_rec env_module_cmds[] = {
  217. { "PassEnv", add_env_module_vars_passed, NULL,
  218.     RSRC_CONF, RAW_ARGS, "a list of environment variables to pass to CGI." },
  219. { "SetEnv", add_env_module_vars_set, NULL,
  220.     RSRC_CONF, RAW_ARGS, "an environment variable name and a value to pass to CGI." },
  221. { "UnsetEnv", add_env_module_vars_unset, NULL,
  222.     RSRC_CONF, RAW_ARGS, "a list of variables to remove from the CGI environment." },
  223. { NULL },
  224. };
  225.  
  226. int fixup_env_module(request_rec *r)
  227. {
  228.     table *e = r->subprocess_env;
  229.     server_rec *s = r->server;
  230.     env_server_config_rec *sconf = get_module_config (s->module_config,
  231.                            &env_module);
  232.     table *vars = sconf->vars;
  233.  
  234.     if ( !sconf->vars_present ) return DECLINED;
  235.  
  236.     r->subprocess_env = overlay_tables( r->pool, e, vars );
  237.  
  238.     return OK;  
  239. }
  240.  
  241. module env_module = {
  242.    STANDARD_MODULE_STUFF,
  243.    NULL,            /* initializer */
  244.    NULL,            /* dir config creater */
  245.    NULL,            /* dir merger --- default is to override */
  246.    create_env_server_config,    /* server config */
  247.    merge_env_server_configs,    /* merge server configs */
  248.    env_module_cmds,        /* command table */
  249.    NULL,            /* handlers */
  250.    NULL,            /* filename translation */
  251.    NULL,            /* check_user_id */
  252.    NULL,            /* check auth */
  253.    NULL,            /* check access */
  254.    NULL,            /* type_checker */
  255.    fixup_env_module,        /* fixups */
  256.    NULL,            /* logger */
  257. };
  258.