|
Packit |
90a5c9 |
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
Packit |
90a5c9 |
* contributor license agreements. See the NOTICE file distributed with
|
|
Packit |
90a5c9 |
* this work for additional information regarding copyright ownership.
|
|
Packit |
90a5c9 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
Packit |
90a5c9 |
* (the "License"); you may not use this file except in compliance with
|
|
Packit |
90a5c9 |
* the License. You may obtain a copy of the License at
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Unless required by applicable law or agreed to in writing, software
|
|
Packit |
90a5c9 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
Packit |
90a5c9 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
Packit |
90a5c9 |
* See the License for the specific language governing permissions and
|
|
Packit |
90a5c9 |
* limitations under the License.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "apr.h"
|
|
Packit |
90a5c9 |
#include "apr_strings.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#if APR_HAVE_STDLIB_H
|
|
Packit |
90a5c9 |
#include <stdlib.h>
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "ap_config.h"
|
|
Packit |
90a5c9 |
#include "httpd.h"
|
|
Packit |
90a5c9 |
#include "http_config.h"
|
|
Packit |
90a5c9 |
#include "http_request.h"
|
|
Packit |
90a5c9 |
#include "http_log.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
typedef struct {
|
|
Packit |
90a5c9 |
apr_table_t *vars;
|
|
Packit |
90a5c9 |
apr_table_t *unsetenv;
|
|
Packit |
90a5c9 |
} env_dir_config_rec;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
module AP_MODULE_DECLARE_DATA env_module;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void *create_env_dir_config(apr_pool_t *p, char *dummy)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
env_dir_config_rec *conf = apr_palloc(p, sizeof(*conf));
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
conf->vars = apr_table_make(p, 10);
|
|
Packit |
90a5c9 |
conf->unsetenv = apr_table_make(p, 10);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return conf;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void *merge_env_dir_configs(apr_pool_t *p, void *basev, void *addv)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
env_dir_config_rec *base = basev;
|
|
Packit |
90a5c9 |
env_dir_config_rec *add = addv;
|
|
Packit |
90a5c9 |
env_dir_config_rec *res = apr_palloc(p, sizeof(*res));
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
const apr_table_entry_t *elts;
|
|
Packit |
90a5c9 |
const apr_array_header_t *arr;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
int i;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* res->vars = copy_table( p, base->vars );
|
|
Packit |
90a5c9 |
* foreach $unsetenv ( @add->unsetenv )
|
|
Packit |
90a5c9 |
* table_unset( res->vars, $unsetenv );
|
|
Packit |
90a5c9 |
* foreach $element ( @add->vars )
|
|
Packit |
90a5c9 |
* table_set( res->vars, $element.key, $element.val );
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* add->unsetenv already removed the vars from add->vars,
|
|
Packit |
90a5c9 |
* if they preceded the UnsetEnv directive.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
res->vars = apr_table_copy(p, base->vars);
|
|
Packit |
90a5c9 |
res->unsetenv = NULL;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
arr = apr_table_elts(add->unsetenv);
|
|
Packit |
90a5c9 |
if (arr) {
|
|
Packit |
90a5c9 |
elts = (const apr_table_entry_t *)arr->elts;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
for (i = 0; i < arr->nelts; ++i) {
|
|
Packit |
90a5c9 |
apr_table_unset(res->vars, elts[i].key);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
arr = apr_table_elts(add->vars);
|
|
Packit |
90a5c9 |
if (arr) {
|
|
Packit |
90a5c9 |
elts = (const apr_table_entry_t *)arr->elts;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
for (i = 0; i < arr->nelts; ++i) {
|
|
Packit |
90a5c9 |
apr_table_setn(res->vars, elts[i].key, elts[i].val);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return res;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *add_env_module_vars_passed(cmd_parms *cmd, void *sconf_,
|
|
Packit |
90a5c9 |
const char *arg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
env_dir_config_rec *sconf = sconf_;
|
|
Packit |
90a5c9 |
apr_table_t *vars = sconf->vars;
|
|
Packit |
90a5c9 |
const char *env_var;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
env_var = getenv(arg);
|
|
Packit |
90a5c9 |
if (env_var != NULL) {
|
|
Packit |
90a5c9 |
apr_table_setn(vars, arg, apr_pstrdup(cmd->pool, env_var));
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, APLOGNO(01506)
|
|
Packit |
90a5c9 |
"PassEnv variable %s was undefined", arg);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *add_env_module_vars_set(cmd_parms *cmd, void *sconf_,
|
|
Packit |
90a5c9 |
const char *name, const char *value)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
env_dir_config_rec *sconf = sconf_;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (ap_strchr_c(name, '=')) {
|
|
Packit |
90a5c9 |
char *env, *plast;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
env = apr_strtok(apr_pstrdup(cmd->temp_pool, name), "=", &plast);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, APLOGNO(10032)
|
|
Packit |
90a5c9 |
"Spurious usage of '=' in an environment variable name. "
|
|
Packit |
90a5c9 |
"'%s %s %s' expected instead?", cmd->cmd->name, env, plast);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* name is mandatory, value is optional. no value means
|
|
Packit |
90a5c9 |
* set the variable to an empty string
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
apr_table_setn(sconf->vars, name, value ? value : "");
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *add_env_module_vars_unset(cmd_parms *cmd, void *sconf_,
|
|
Packit |
90a5c9 |
const char *arg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
env_dir_config_rec *sconf = sconf_;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Always UnsetEnv FOO in the same context as {Set,Pass}Env FOO
|
|
Packit |
90a5c9 |
* only if this UnsetEnv follows the {Set,Pass}Env. The merge
|
|
Packit |
90a5c9 |
* will only apply unsetenv to the parent env (main server).
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
apr_table_set(sconf->unsetenv, arg, NULL);
|
|
Packit |
90a5c9 |
apr_table_unset(sconf->vars, arg);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const command_rec env_module_cmds[] =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
AP_INIT_ITERATE("PassEnv", add_env_module_vars_passed, NULL,
|
|
Packit |
90a5c9 |
OR_FILEINFO, "a list of environment variables to pass to CGI."),
|
|
Packit |
90a5c9 |
AP_INIT_TAKE12("SetEnv", add_env_module_vars_set, NULL,
|
|
Packit |
90a5c9 |
OR_FILEINFO, "an environment variable name and optional value to pass to CGI."),
|
|
Packit |
90a5c9 |
AP_INIT_ITERATE("UnsetEnv", add_env_module_vars_unset, NULL,
|
|
Packit |
90a5c9 |
OR_FILEINFO, "a list of variables to remove from the CGI environment."),
|
|
Packit |
90a5c9 |
{NULL},
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int fixup_env_module(request_rec *r)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
env_dir_config_rec *sconf = ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&env_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (apr_is_empty_table(sconf->vars)) {
|
|
Packit |
90a5c9 |
return DECLINED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
r->subprocess_env = apr_table_overlay(r->pool, r->subprocess_env,
|
|
Packit |
90a5c9 |
sconf->vars);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return OK;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void register_hooks(apr_pool_t *p)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
ap_hook_fixups(fixup_env_module, NULL, NULL, APR_HOOK_MIDDLE);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
AP_DECLARE_MODULE(env) =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
STANDARD20_MODULE_STUFF,
|
|
Packit |
90a5c9 |
create_env_dir_config, /* dir config creater */
|
|
Packit |
90a5c9 |
merge_env_dir_configs, /* dir merger --- default is to override */
|
|
Packit |
90a5c9 |
NULL, /* server config */
|
|
Packit |
90a5c9 |
NULL, /* merge server configs */
|
|
Packit |
90a5c9 |
env_module_cmds, /* command apr_table_t */
|
|
Packit |
90a5c9 |
register_hooks /* register hooks */
|
|
Packit |
90a5c9 |
};
|