1 /*
2 Copyright (c) 2013 Etsy
3
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation
6 files (the "Software"), to deal in the Software without
7 restriction, including without limitation the rights to use,
8 copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following
11 conditions:
12
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #define CORE_PRIVATE
27
28 #include "httpd.h"
29 #include "http_config.h"
30 #include "http_core.h"
31 #include "http_protocol.h"
32 #include "http_log.h"
33 #include "apr_strings.h"
34 #include <sys/types.h>
35 #include <unistd.h>
36
37 typedef struct {
38 const char **docroot;
39 const char *original;
40 } realdoc_request_save_struct;
41
42 typedef struct {
43 apr_time_t realpath_every;
44 } realdoc_config_struct;
45
46 #define AP_LOG_DEBUG(rec, fmt, ...) ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, rec, "[realdoc] " fmt, ##__VA_ARGS__)
47 #define AP_LOG_ERROR(rec, fmt, ...) ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, rec, "[realdoc] " fmt, ##__VA_ARGS__)
48
49 module AP_MODULE_DECLARE_DATA realdoc_module;
50
create_realdoc_config(apr_pool_t * p,server_rec * d)51 static void *create_realdoc_config(apr_pool_t *p, server_rec *d)
52 {
53 return apr_pcalloc(p, sizeof(realdoc_config_struct));
54 }
55
merge_realdoc_config(apr_pool_t * p,void * basev,void * addv)56 static void *merge_realdoc_config(apr_pool_t *p, void *basev, void *addv)
57 {
58 realdoc_config_struct *base = (realdoc_config_struct *) basev;
59 realdoc_config_struct *add = (realdoc_config_struct *) addv;
60 realdoc_config_struct *new = (realdoc_config_struct *)
61 apr_palloc(p, sizeof(realdoc_config_struct));
62
63 new->realpath_every = add->realpath_every ? add->realpath_every : base->realpath_every;
64 return new;
65 }
66
set_realdoc_config(cmd_parms * cmd,void * dummy,const char * arg)67 static const char *set_realdoc_config(cmd_parms *cmd, void *dummy, const char *arg)
68 {
69 char *endptr = NULL;
70 realdoc_config_struct *conf = (realdoc_config_struct *) ap_get_module_config(cmd->server->module_config,
71 &realdoc_module);
72
73 if (!conf) {
74 return apr_psprintf(cmd->pool,
75 "realdoc configuration not initialized");
76 }
77
78 conf->realpath_every = strtol(arg, &endptr, 10);
79
80 if ((*arg == '\0') || (*endptr != '\0')) {
81 return apr_psprintf(cmd->pool,
82 "Invalid value for realdoc directive %s, expected integer",
83 cmd->directive->directive);
84 }
85
86 return NULL;
87 }
88
89 static const command_rec realdoc_cmds[] =
90 {
91 AP_INIT_TAKE1("RealpathEvery", set_realdoc_config, NULL, RSRC_CONF,
92 "Run the realpath at most every so many seconds"),
93 {NULL}
94 };
95
realdoc_restore_docroot(void * data)96 static apr_status_t realdoc_restore_docroot(void *data) {
97 realdoc_request_save_struct *save = (realdoc_request_save_struct *)data;
98 *save->docroot = save->original;
99 return APR_SUCCESS;
100 }
101
realdoc_hook_handler(request_rec * r)102 static int realdoc_hook_handler(request_rec *r) {
103 core_server_config *core_conf;
104 realdoc_config_struct *realdoc_conf;
105 realdoc_request_save_struct *save;
106 char *last_saved_real_docroot;
107 apr_time_t *last_saved_real_time;
108 apr_time_t current_request_time;
109 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));
110 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));
111
112 core_conf = ap_get_module_config(r->server->module_config, &core_module);
113 realdoc_conf = (realdoc_config_struct *) ap_get_module_config(r->server->module_config, &realdoc_module);
114
115 apr_pool_userdata_get((void **) &last_saved_real_docroot, last_saved_docroot_key, r->server->process->pool);
116
117 /* Guard against being run a second time on the same request */
118 if(core_conf->ap_document_root == last_saved_real_docroot) {
119 return DECLINED;
120 }
121
122 /* Grab the current docroot address and value and save it
123 so we can restore it in our cleanup func */
124 save = apr_pcalloc(r->pool, sizeof(realdoc_request_save_struct));
125 save->docroot = &core_conf->ap_document_root;
126 save->original = core_conf->ap_document_root;
127
128 apr_pool_cleanup_register(r->pool, save, realdoc_restore_docroot, realdoc_restore_docroot);
129 current_request_time = apr_time_sec(r->request_time);
130
131 apr_pool_userdata_get((void **) &last_saved_real_time, last_saved_time_key, r->server->process->pool);
132 if (!last_saved_real_time) {
133 last_saved_real_time = apr_pcalloc(r->server->process->pool, sizeof(apr_time_t));
134 if (!last_saved_real_time) {
135 AP_LOG_ERROR(r, "Failed to allocate memory for saving time data");
136 return DECLINED;
137 } else {
138 apr_pool_userdata_set((const void *) last_saved_real_time, last_saved_time_key, NULL,
139 r->server->process->pool);
140 }
141 }
142
143 if (!last_saved_real_docroot) {
144 last_saved_real_docroot = apr_pcalloc(r->server->process->pool, PATH_MAX * sizeof(char));
145 if (!last_saved_real_docroot) {
146 AP_LOG_ERROR(r, "Failed to allocate memory for saving docroot data");
147 return DECLINED;
148 } else {
149 apr_pool_userdata_set((const void *) last_saved_real_docroot, last_saved_docroot_key, NULL,
150 r->server->process->pool);
151 }
152 }
153
154 if (*last_saved_real_time < (current_request_time - realdoc_conf->realpath_every)) {
155 if (NULL == realpath(core_conf->ap_document_root, last_saved_real_docroot)) {
156 AP_LOG_ERROR(r, "Error from realpath: %d. Original docroot: %s", errno, core_conf->ap_document_root);
157 return DECLINED;
158 }
159
160 AP_LOG_DEBUG(r, "PID %d calling realpath. Original docroot: %s. Resolved: %s", getpid(), core_conf->ap_document_root,
161 last_saved_real_docroot);
162 *last_saved_real_time = current_request_time;
163 }
164
165 core_conf->ap_document_root = last_saved_real_docroot;
166 return DECLINED;
167 }
168
realdoc_register_hook(apr_pool_t * p)169 void realdoc_register_hook(apr_pool_t *p) {
170 ap_hook_post_read_request(realdoc_hook_handler, NULL, NULL, APR_HOOK_REALLY_FIRST);
171 }
172
173 AP_MODULE_DECLARE_DATA module realdoc_module = {
174 STANDARD20_MODULE_STUFF,
175 NULL, /* create per-directory config structure */
176 NULL, /* merge per-directory config structures */
177 create_realdoc_config, /* create per-server config structure */
178 merge_realdoc_config, /* merge per-server config structures */
179 realdoc_cmds, /* command apr_table_t */
180 realdoc_register_hook /* register hooks */
181 };
182