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

  1. /* ====================================================================
  2.  * Copyright (c) 1995-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_env.c
  60.  * version 0.0.5
  61.  * status beta
  62.  * Pass environment variables to CGI/SSI scripts.
  63.  * 
  64.  * Andrew Wilson <Andrew.Wilson@cm.cf.ac.uk> 06.Dec.95
  65.  *
  66.  * Change log:
  67.  * 08.Dec.95 Now allows PassEnv directive to appear more than once in
  68.  *           conf files.
  69.  * 10.Dec.95 optimisation.  getenv() only called at startup and used 
  70.  *           to build a fast-to-access table.  table used to build 
  71.  *           per-server environment for each request.
  72.  *           robustness.  better able to handle errors in configuration
  73.  *           files:
  74.  *           1)  PassEnv directive present, but no environment variable listed
  75.  *           2)  PassEnv FOO present, but $FOO not present in environment
  76.  *           3)  no PassEnv directive present
  77.  * 23.Dec.95 Now allows SetEnv directive with same semantics as 'sh' setenv:
  78.  *              SetEnv Var      sets Var to the empty string
  79.  *              SetEnv Var Val  sets Var to the value Val
  80.  *           Values containing whitespace should be quoted, eg:
  81.  *              SetEnv Var "this is some text"
  82.  *           Environment variables take their value from the last instance
  83.  *           of PassEnv / SetEnv to be reached in the configuration file.
  84.  *           For example, the sequence:
  85.  *              PassEnv FOO
  86.  *              SetEnv FOO override
  87.  *           Causes FOO to take the value 'override'.
  88.  * 23.Feb.96 Added UnsetEnv directive to allow environment variables
  89.  *           to be removed.
  90.  *           Virtual hosts now 'inherit' parent server environment which
  91.  *           they're able to overwrite with their own directives or
  92.  *           selectively ignore with UnsetEnv.
  93.  *       *** IMPORTANT - the way that virtual hosts inherit their ***
  94.  *       *** environment variables from the default server's      ***
  95.  *       *** configuration has changed.  You should test your     ***
  96.  *       *** configuration carefully before accepting this        ***
  97.  *       *** version of the module in a live webserver which used ***
  98.  *       *** older versions of the module.                        ***
  99.  */
  100.  
  101. #include "httpd.h"
  102. #include "http_config.h"
  103.  
  104. typedef struct {
  105.     table *vars;
  106.     char *unsetenv;
  107.     int vars_present;
  108. } env_server_config_rec;
  109.  
  110. module MODULE_VAR_EXPORT env_module;
  111.  
  112. static void *create_env_server_config(pool *p, server_rec *dummy)
  113. {
  114.     env_server_config_rec *new =
  115.     (env_server_config_rec *) ap_palloc(p, sizeof(env_server_config_rec));
  116.     new->vars = ap_make_table(p, 50);
  117.     new->unsetenv = "";
  118.     new->vars_present = 0;
  119.     return (void *) new;
  120. }
  121.  
  122. static void *merge_env_server_configs(pool *p, void *basev, void *addv)
  123. {
  124.     env_server_config_rec *base = (env_server_config_rec *) basev;
  125.     env_server_config_rec *add = (env_server_config_rec *) addv;
  126.     env_server_config_rec *new =
  127.     (env_server_config_rec *) ap_palloc(p, sizeof(env_server_config_rec));
  128.  
  129.     table *new_table;
  130.     table_entry *elts;
  131.     array_header *arr;
  132.  
  133.     int i;
  134.     const char *uenv, *unset;
  135.  
  136.     /* 
  137.      * new_table = copy_table( p, base->vars );
  138.      * foreach $element ( @add->vars ) {
  139.      *     table_set( new_table, $element.key, $element.val );
  140.      * };
  141.      * foreach $unsetenv ( @UNSETENV ) {
  142.      *     table_unset( new_table, $unsetenv );
  143.      * }
  144.      */
  145.  
  146.     new_table = ap_copy_table(p, base->vars);
  147.  
  148.     arr = ap_table_elts(add->vars);
  149.     elts = (table_entry *)arr->elts;
  150.  
  151.     for (i = 0; i < arr->nelts; ++i) {
  152.         ap_table_setn(new_table, elts[i].key, elts[i].val);
  153.     }
  154.  
  155.     unset = add->unsetenv;
  156.     uenv = ap_getword_conf(p, &unset);
  157.     while (uenv[0] != '\0') {
  158.         ap_table_unset(new_table, uenv);
  159.         uenv = ap_getword_conf(p, &unset);
  160.     }
  161.  
  162.     new->vars = new_table;
  163.  
  164.     new->vars_present = base->vars_present || add->vars_present;
  165.  
  166.     return new;
  167. }
  168.  
  169. static const char *add_env_module_vars_passed(cmd_parms *cmd, char *struct_ptr,
  170.                                               const char *arg)
  171. {
  172.     env_server_config_rec *sconf =
  173.     ap_get_module_config(cmd->server->module_config, &env_module);
  174.     table *vars = sconf->vars;
  175.     char *env_var;
  176.     char *name_ptr;
  177.  
  178.     while (*arg) {
  179.         name_ptr = ap_getword_conf(cmd->pool, &arg);
  180.         env_var = getenv(name_ptr);
  181.         if (env_var != NULL) {
  182.             sconf->vars_present = 1;
  183.             ap_table_setn(vars, name_ptr, ap_pstrdup(cmd->pool, env_var));
  184.         }
  185.     }
  186.     return NULL;
  187. }
  188.  
  189. static const char *add_env_module_vars_set(cmd_parms *cmd, char *struct_ptr,
  190.                                            const char *arg)
  191. {
  192.     env_server_config_rec *sconf =
  193.     ap_get_module_config(cmd->server->module_config, &env_module);
  194.     table *vars = sconf->vars;
  195.     char *name, *value;
  196.  
  197.     name = ap_getword_conf(cmd->pool, &arg);
  198.     value = ap_getword_conf(cmd->pool, &arg);
  199.  
  200.     /* name is mandatory, value is optional.  no value means
  201.      * set the variable to an empty string
  202.      */
  203.  
  204.  
  205.     if ((*name == '\0') || (*arg != '\0')) {
  206.         return "SetEnv takes one or two arguments.  An environment variable name and an optional value to pass to CGI.";
  207.     }
  208.  
  209.     sconf->vars_present = 1;
  210.     ap_table_setn(vars, name, value);
  211.  
  212.     return NULL;
  213. }
  214.  
  215. static const char *add_env_module_vars_unset(cmd_parms *cmd, char *struct_ptr,
  216.                                              char *arg)
  217. {
  218.     env_server_config_rec *sconf =
  219.     ap_get_module_config(cmd->server->module_config, &env_module);
  220.     sconf->unsetenv = sconf->unsetenv ?
  221.         ap_pstrcat(cmd->pool, sconf->unsetenv, " ", arg, NULL) :
  222.          arg;
  223.     return NULL;
  224. }
  225.  
  226. static const command_rec env_module_cmds[] =
  227. {
  228.     {"PassEnv", add_env_module_vars_passed, NULL,
  229.      RSRC_CONF, RAW_ARGS, "a list of environment variables to pass to CGI."},
  230.     {"SetEnv", add_env_module_vars_set, NULL,
  231.      RSRC_CONF, RAW_ARGS, "an environment variable name and a value to pass to CGI."},
  232.     {"UnsetEnv", add_env_module_vars_unset, NULL,
  233.      RSRC_CONF, RAW_ARGS, "a list of variables to remove from the CGI environment."},
  234.     {NULL},
  235. };
  236.  
  237. static int fixup_env_module(request_rec *r)
  238. {
  239.     table *e = r->subprocess_env;
  240.     server_rec *s = r->server;
  241.     env_server_config_rec *sconf = ap_get_module_config(s->module_config,
  242.                                                      &env_module);
  243.     table *vars = sconf->vars;
  244.  
  245.     if (!sconf->vars_present)
  246.         return DECLINED;
  247.  
  248.     r->subprocess_env = ap_overlay_tables(r->pool, e, vars);
  249.  
  250.     return OK;
  251. }
  252.  
  253. module MODULE_VAR_EXPORT env_module =
  254. {
  255.     STANDARD_MODULE_STUFF,
  256.     NULL,                       /* initializer */
  257.     NULL,                       /* dir config creater */
  258.     NULL,                       /* dir merger --- default is to override */
  259.     create_env_server_config,   /* server config */
  260.     merge_env_server_configs,   /* merge server configs */
  261.     env_module_cmds,            /* command table */
  262.     NULL,                       /* handlers */
  263.     NULL,                       /* filename translation */
  264.     NULL,                       /* check_user_id */
  265.     NULL,                       /* check auth */
  266.     NULL,                       /* check access */
  267.     NULL,                       /* type_checker */
  268.     fixup_env_module,           /* fixups */
  269.     NULL,                       /* logger */
  270.     NULL,                       /* header parser */
  271.     NULL,                       /* child_init */
  272.     NULL,                       /* child_exit */
  273.     NULL                        /* post read-request */
  274. };
  275.