forked from etsy/mod_realdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_realdoc.c
175 lines (146 loc) · 6.79 KB
/
mod_realdoc.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
/*
Copyright (c) 2013 Etsy
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#define CORE_PRIVATE
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_log.h"
#include "apr_strings.h"
#include <sys/types.h>
#include <unistd.h>
typedef struct {
const char **docroot;
const char *original;
} realdoc_request_save_struct;
typedef struct {
apr_time_t realpath_every;
} realdoc_config_struct;
#define AP_LOG_DEBUG(rec, fmt, ...) ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, rec, "[realdoc] " fmt, ##__VA_ARGS__)
#define AP_LOG_ERROR(rec, fmt, ...) ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, rec, "[realdoc] " fmt, ##__VA_ARGS__)
module AP_MODULE_DECLARE_DATA realdoc_module;
static void *create_realdoc_config(apr_pool_t *p, server_rec *d)
{
return apr_pcalloc(p, sizeof(realdoc_config_struct));
}
static void *merge_realdoc_config(apr_pool_t *p, void *basev, void *addv)
{
realdoc_config_struct *base = (realdoc_config_struct *) basev;
realdoc_config_struct *add = (realdoc_config_struct *) addv;
realdoc_config_struct *new = (realdoc_config_struct *)
apr_palloc(p, sizeof(realdoc_config_struct));
new->realpath_every = add->realpath_every ? add->realpath_every : base->realpath_every;
return new;
}
static const char *set_realdoc_config(cmd_parms *cmd, void *dummy, const char *arg)
{
char *endptr = NULL;
realdoc_config_struct *conf = (realdoc_config_struct *) ap_get_module_config(cmd->server->module_config,
&realdoc_module);
if (!conf) {
return apr_psprintf(cmd->pool,
"realdoc configuration not initialized");
}
conf->realpath_every = strtol(arg, &endptr, 10);
if ((*arg == '\0') || (*endptr != '\0')) {
return apr_psprintf(cmd->pool,
"Invalid value for realdoc directive %s, expected integer",
cmd->directive->directive);
}
return NULL;
}
static const command_rec realdoc_cmds[] =
{
AP_INIT_TAKE1("RealpathEvery", set_realdoc_config, NULL, RSRC_CONF,
"Run the realpath at most every so many seconds"),
{NULL}
};
static apr_status_t realdoc_restore_docroot(void *data) {
realdoc_request_save_struct *save = (realdoc_request_save_struct *)data;
*save->docroot = save->original;
return APR_SUCCESS;
}
static int realdoc_hook_handler(request_rec *r) {
core_server_config *core_conf;
realdoc_config_struct *realdoc_conf;
realdoc_request_save_struct *save;
char *last_saved_real_docroot;
apr_time_t *last_saved_real_time;
apr_time_t current_request_time;
const char *last_saved_docroot_key = apr_psprintf(r->pool, "%s:%u:realdoc_saved_docroot", ap_get_server_name(r), ap_get_server_port(r));
const char *last_saved_time_key = apr_psprintf(r->pool, "%s:%u:realdoc_saved_time", ap_get_server_name(r), ap_get_server_port(r));
core_conf = ap_get_module_config(r->server->module_config, &core_module);
realdoc_conf = (realdoc_config_struct *) ap_get_module_config(r->server->module_config,
&realdoc_module);
/* Grab the current docroot address and value and save it
so we can restore it in our cleanup func */
save = apr_pcalloc(r->pool, sizeof(realdoc_request_save_struct));
save->docroot = &core_conf->ap_document_root;
save->original = core_conf->ap_document_root;
apr_pool_cleanup_register(r->pool, save, realdoc_restore_docroot, realdoc_restore_docroot);
current_request_time = apr_time_sec(r->request_time);
apr_pool_userdata_get((void **) &last_saved_real_docroot, last_saved_docroot_key, r->server->process->pool);
apr_pool_userdata_get((void **) &last_saved_real_time, last_saved_time_key, r->server->process->pool);
if (!last_saved_real_time) {
last_saved_real_time = apr_pcalloc(r->server->process->pool, sizeof(apr_time_t));
if (!last_saved_real_time) {
AP_LOG_ERROR(r, "Failed to allocate memory for saving time data");
return DECLINED;
} else {
apr_pool_userdata_set((const void *) last_saved_real_time, last_saved_time_key, NULL,
r->server->process->pool);
}
}
if (!last_saved_real_docroot) {
last_saved_real_docroot = apr_pcalloc(r->server->process->pool, PATH_MAX * sizeof(char));
if (!last_saved_real_docroot) {
AP_LOG_ERROR(r, "Failed to allocate memory for saving docroot data");
return DECLINED;
} else {
apr_pool_userdata_set((const void *) last_saved_real_docroot, last_saved_docroot_key, NULL,
r->server->process->pool);
}
}
if (*last_saved_real_time < (current_request_time - realdoc_conf->realpath_every)) {
if (NULL == realpath(core_conf->ap_document_root, last_saved_real_docroot)) {
AP_LOG_ERROR(r, "Error from realpath: %d. Original docroot: %s", errno, core_conf->ap_document_root);
return DECLINED;
}
AP_LOG_DEBUG(r, "PID %d calling realpath. Original docroot: %s. Resolved: %s", getpid(), core_conf->ap_document_root,
last_saved_real_docroot);
*last_saved_real_time = current_request_time;
}
core_conf->ap_document_root = last_saved_real_docroot;
return DECLINED;
}
void realdoc_register_hook(apr_pool_t *p) {
ap_hook_post_read_request(realdoc_hook_handler, NULL, NULL, APR_HOOK_REALLY_FIRST);
}
AP_MODULE_DECLARE_DATA module realdoc_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
create_realdoc_config, /* create per-server config structure */
merge_realdoc_config, /* merge per-server config structures */
realdoc_cmds, /* command apr_table_t */
realdoc_register_hook /* register hooks */
};