Apache HTTPD
mod_env.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "apr.h"
18#include "apr_strings.h"
19
20#if APR_HAVE_STDLIB_H
21#include <stdlib.h>
22#endif
23
24#include "ap_config.h"
25#include "httpd.h"
26#include "http_config.h"
27#include "http_request.h"
28#include "http_log.h"
29
34
35module AP_MODULE_DECLARE_DATA env_module;
36
38{
39 env_dir_config_rec *conf = apr_palloc(p, sizeof(*conf));
40
41 conf->vars = apr_table_make(p, 10);
42 conf->unsetenv = apr_table_make(p, 10);
43
44 return conf;
45}
46
47static void *merge_env_dir_configs(apr_pool_t *p, void *basev, void *addv)
48{
52
53 const apr_table_entry_t *elts;
55
56 int i;
57
58 /*
59 * res->vars = copy_table( p, base->vars );
60 * foreach $unsetenv ( @add->unsetenv )
61 * table_unset( res->vars, $unsetenv );
62 * foreach $element ( @add->vars )
63 * table_set( res->vars, $element.key, $element.val );
64 *
65 * add->unsetenv already removed the vars from add->vars,
66 * if they preceded the UnsetEnv directive.
67 */
68 res->vars = apr_table_copy(p, base->vars);
69 res->unsetenv = NULL;
70
72 if (arr) {
73 elts = (const apr_table_entry_t *)arr->elts;
74
75 for (i = 0; i < arr->nelts; ++i) {
76 apr_table_unset(res->vars, elts[i].key);
77 }
78 }
79
80 arr = apr_table_elts(add->vars);
81 if (arr) {
82 elts = (const apr_table_entry_t *)arr->elts;
83
84 for (i = 0; i < arr->nelts; ++i) {
85 apr_table_setn(res->vars, elts[i].key, elts[i].val);
86 }
87 }
88
89 return res;
90}
91
93 const char *arg)
94{
96 apr_table_t *vars = sconf->vars;
97 const char *env_var;
98
100 if (env_var != NULL) {
101 apr_table_setn(vars, arg, apr_pstrdup(cmd->pool, env_var));
102 }
103 else {
104 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, APLOGNO(01506)
105 "PassEnv variable %s was undefined", arg);
106 }
107
108 return NULL;
109}
110
111static const char *add_env_module_vars_set(cmd_parms *cmd, void *sconf_,
112 const char *name, const char *value)
113{
115
116 if (ap_strchr_c(name, '=')) {
117 char *env, *plast;
118
119 env = apr_strtok(apr_pstrdup(cmd->temp_pool, name), "=", &plast);
120
121 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, APLOGNO(10032)
122 "Spurious usage of '=' in an environment variable name. "
123 "'%s %s %s' expected instead?", cmd->cmd->name, env, plast);
124 }
125
126 /* name is mandatory, value is optional. no value means
127 * set the variable to an empty string
128 */
129 apr_table_setn(sconf->vars, name, value ? value : "");
130
131 return NULL;
132}
133
135 const char *arg)
136{
138
139 /* Always UnsetEnv FOO in the same context as {Set,Pass}Env FOO
140 * only if this UnsetEnv follows the {Set,Pass}Env. The merge
141 * will only apply unsetenv to the parent env (main server).
142 */
143 apr_table_set(sconf->unsetenv, arg, NULL);
144 apr_table_unset(sconf->vars, arg);
145
146 return NULL;
147}
148
150{
152 OR_FILEINFO, "a list of environment variables to pass to CGI."),
154 OR_FILEINFO, "an environment variable name and optional value to pass to CGI."),
156 OR_FILEINFO, "a list of variables to remove from the CGI environment."),
157 {NULL},
158};
159
161{
163 &env_module);
164
165 if (apr_is_empty_table(sconf->vars)) {
166 return DECLINED;
167 }
168
170 sconf->vars);
171
172 return OK;
173}
174
179
181{
183 create_env_dir_config, /* dir config creater */
184 merge_env_dir_configs, /* dir merger --- default is to override */
185 NULL, /* server config */
186 NULL, /* merge server configs */
187 env_module_cmds, /* command apr_table_t */
188 register_hooks /* register hooks */
189};
Symbol export macros and hook functions.
APR Strings library.
#define ap_get_module_config(v, m)
#define AP_DECLARE_MODULE(foo)
ap_conf_vector_t * base
#define AP_INIT_ITERATE(directive, func, mconfig, where, help)
#define AP_INIT_TAKE12(directive, func, mconfig, where, help)
request_rec * r
#define DECLINED
Definition httpd.h:457
#define OK
Definition httpd.h:456
#define APLOGNO(n)
Definition http_log.h:117
#define ap_log_error
Definition http_log.h:370
#define APLOG_MARK
Definition http_log.h:283
#define APLOG_WARNING
Definition http_log.h:68
void ap_hook_fixups(ap_HOOK_fixups_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition request.c:87
void * dummy
Definition http_vhost.h:62
void const char * arg
Definition http_vhost.h:63
apr_pool_t apr_dbd_t apr_dbd_results_t ** res
Definition apr_dbd.h:287
#define APR_HOOK_MIDDLE
Definition apr_hooks.h:303
#define OR_FILEINFO
#define STANDARD20_MODULE_STUFF
#define ap_strchr_c(s, c)
Definition httpd.h:2353
apr_size_t size
const char * value
Definition apr_env.h:51
char const *const char const *const ** env
const apr_array_header_t * arr
Definition apr_tables.h:187
apr_cmdtype_e cmd
Apache Configuration.
Apache Logging library.
Apache Request library.
HTTP Daemon routines.
apr_pool_t * p
Definition md_event.c:32
static void * create_env_dir_config(apr_pool_t *p, char *dummy)
Definition mod_env.c:37
static const char * add_env_module_vars_unset(cmd_parms *cmd, void *sconf_, const char *arg)
Definition mod_env.c:134
static int fixup_env_module(request_rec *r)
Definition mod_env.c:160
static const char * add_env_module_vars_passed(cmd_parms *cmd, void *sconf_, const char *arg)
Definition mod_env.c:92
static const command_rec env_module_cmds[]
Definition mod_env.c:149
static void * merge_env_dir_configs(apr_pool_t *p, void *basev, void *addv)
Definition mod_env.c:47
static void register_hooks(apr_pool_t *p)
Definition mod_env.c:175
static const char * add_env_module_vars_set(cmd_parms *cmd, void *sconf_, const char *name, const char *value)
Definition mod_env.c:111
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
sconf
Definition mod_so.c:349
char * name
Definition apr_tables.h:81
char * val
Definition apr_tables.h:87
char * key
Definition apr_tables.h:83
apr_table_t * unsetenv
Definition mod_env.c:32
apr_table_t * vars
Definition mod_env.c:31
A structure that represents the current request.
Definition httpd.h:845
apr_pool_t * pool
Definition httpd.h:847
apr_table_t * subprocess_env
Definition httpd.h:983
struct ap_conf_vector_t * per_dir_config
Definition httpd.h:1047