-
Notifications
You must be signed in to change notification settings - Fork 4
/
mod_reset.c
199 lines (178 loc) · 7.94 KB
/
mod_reset.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "mod_reset.h"
module AP_MODULE_DECLARE_DATA reset_module;
#ifdef MOD_RUID2
module AP_MODULE_DECLARE_DATA ruid2_module;
#endif
static int reset_check_handler(request_rec *r)
{
const char *header = NULL;
reset_config *conf = (reset_config *) ap_get_module_config(r->server->module_config, &reset_module);
if (conf->enable) {
if (conf->deny_header) {
if (!r->hostname)
goto req_ok;
/* Check if request contains arbitrary header */
header = (char *) apr_table_get(r->headers_in, conf->deny_header);
if (!header)
return HTTP_FORBIDDEN;
#ifdef MOD_RESET_AUTH_KEY
/* Do some validation by secret */
if (apr_password_validate(MOD_RESET_AUTH_KEY, header) != APR_SUCCESS)
return HTTP_FORBIDDEN;
if (!conf->hash)
goto req_ok;
if (apr_strnatcmp(conf->hash, header) == 0)
return HTTP_FORBIDDEN;
#endif
}
}
req_ok:
conf->hash = (char *)header;
return OK;
}
static int reset_handler(request_rec *r)
{
reset_config *conf = (reset_config *) ap_get_module_config(r->server->module_config, &reset_module);
#ifdef MOD_RUID2
ruid_dir_config_t *ruid = ap_get_module_config(r->per_dir_config, &ruid2_module);
#endif
#ifdef APACHE_22
core_server_config *core = ap_get_module_config(r->server->module_config, &core_module);
#endif
if (conf->enable) {
#ifndef NO_PHP
apr_array_header_t *arr = (apr_array_header_t *) apr_table_elts(conf->php_ini);
apr_table_entry_t *ini = (apr_table_entry_t *) arr->elts;
/* Setting php.ini values on the fly */
int i;
for (i = 0; i < arr->nelts; i++) {
char *value = (char *) apr_table_get(r->headers_in, ini[i].val);
if (!value)
continue;
#if PHP_MAJOR_VERSION >= 7
zend_string *key = zend_string_init(ini[i].key, strlen(ini[i].key) , 0);
zend_string *val = zend_string_init(value, strlen(value), 0);
zend_alter_ini_entry(key, val, ZEND_INI_SYSTEM, ZEND_INI_STAGE_ACTIVATE);
zend_string_release(key);
zend_string_release(val);
#else
zend_alter_ini_entry(ini[i].key, strlen(ini[i].key) + 1, value, strlen(value), ZEND_INI_SYSTEM, ZEND_INI_STAGE_ACTIVATE);
#endif
}
#ifdef MOD_RUID2
char *ruid_uid_header = (char *) apr_table_get(r->headers_in, conf->ruid_uid);
if (ruid_uid_header) {
ruid->ruid_uid = ap_uname2id(ruid_uid_header);
ruid->ruid_gid = ap_uname2id(ruid_uid_header);
}
#endif
#endif
/* Setting DocumentRoot */
char *docroot = (char *) apr_table_get(r->headers_in, conf->docroot);
if (docroot && ap_is_directory(r->pool, docroot)) {
#ifdef APACHE_22
core->ap_document_root = docroot;
#else
ap_set_document_root(r, docroot);
#endif
apr_table_setn(r->subprocess_env, "PHP_DOCUMENT_ROOT", docroot);
apr_table_setn(r->subprocess_env, "DOCUMENT_ROOT", docroot);
} else {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Can't set DocumentRoot as %s!", docroot);
return HTTP_FORBIDDEN;
}
#ifndef NO_PHP
/* Set TMPDIR environment variable */
char *tmpdir = (char *) apr_table_get(r->headers_in, conf->tmpdir);
if (tmpdir && ap_is_directory(r->pool, tmpdir)) {
if (apr_env_set("TMPDIR", tmpdir, r->pool) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Can't set TMPDIR environment as %s!", tmpdir);
}
} else {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "TMPDIR directory does not exist, check headers!");
}
#endif
/* Setting ServerAdmin */
char *admin = (char *) apr_table_get(r->headers_in, conf->admin);
if (admin) {
apr_table_setn(r->subprocess_env, "SERVER_ADMIN", admin);
r->server->server_admin = admin;
}
}
return DECLINED;
}
static void reset_module_register_hooks(apr_pool_t *p)
{
ap_hook_post_read_request(reset_check_handler, NULL, NULL, APR_HOOK_REALLY_FIRST);
ap_hook_translate_name(reset_handler, NULL, NULL, APR_HOOK_FIRST);
}
static const char *enable_reset(cmd_parms *cmd, void *cfg, const char arg[])
{
reset_config *conf = (reset_config *) ap_get_module_config(cmd->server->module_config, &reset_module);
if (arg != NULL && !strncmp(arg, "On", 2)) {
conf->enable = 1;
conf->php_ini = apr_table_make(cmd->pool, conf->nheaders);
}
return NULL;
}
static const char *denyheader_reset(cmd_parms *cmd, void *cfg, const char arg[])
{
reset_config *conf = (reset_config *) ap_get_module_config(cmd->server->module_config, &reset_module);
if (arg != NULL && strlen(arg) > 0)
conf->deny_header = arg;
return NULL;
}
static const char *headers_reset(cmd_parms *cmd, void *cfg, const char *ini, const char *header)
{
reset_config *conf = (reset_config *) ap_get_module_config(cmd->server->module_config, &reset_module);
if (conf->enable) {
if (ini != NULL && header != NULL) {
conf->nheaders++;
apr_table_setn(conf->php_ini, ini, header);
}
}
return NULL;
}
static const char *header_reset(cmd_parms *cmd, void *cfg, const char *dir, const char *header)
{
reset_config *conf = (reset_config *) ap_get_module_config(cmd->server->module_config, &reset_module);
if (conf->enable) {
if (header && *header) {
if (!strncmp(dir, "TMPDIR", sizeof("TMPDIR"))) {
conf->tmpdir = (char *) header;
} else if (!strncmp(dir, "DocumentRoot", sizeof("DocumentRoot"))) {
conf->docroot = (char *) header;
} else if (!strncmp(dir, "ServerAdmin", sizeof("ServerAdmin"))) {
conf->admin = (char *) header;
#ifdef MOD_RUID2
} else if (!strncmp(dir, "RUidGid", sizeof("RUidGid"))) {
conf->ruid_uid = (char *) header;
#endif
}
}
}
return NULL;
}
static void * reset_create_server_config(apr_pool_t * p, server_rec * s) {
reset_config *conf = (reset_config *) apr_pcalloc(p, sizeof(reset_config));
conf->enable = 0;
conf->nheaders = 0;
return (void *)conf;
}
static const command_rec reset_module_directives[] =
{
AP_INIT_TAKE1("Reset", enable_reset, NULL, RSRC_CONF, "Enable/Disable reset module."),
AP_INIT_TAKE1("ResetDenyHeader", denyheader_reset, NULL, RSRC_CONF, "Set header which is used to deny unauthenticated requests."),
AP_INIT_TAKE2("ResetHeaders", headers_reset, NULL, RSRC_CONF, "Configure headers to be used for checking."),
AP_INIT_TAKE2("ResetHeader", header_reset, NULL, RSRC_CONF, "Configure header for setting ServerAdmin/DocumentRoot."),
{NULL}
};
module AP_MODULE_DECLARE_DATA reset_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
reset_create_server_config,
NULL,
reset_module_directives,
reset_module_register_hooks
};