forked from gnif/mod_rpaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_rpaf.c
330 lines (285 loc) · 11.5 KB
/
mod_rpaf.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
Copyright 2011 Ask Bjørn Hansen
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"
#include "http_vhost.h"
#include "apr_strings.h"
#include <arpa/inet.h>
module AP_MODULE_DECLARE_DATA rpaf_module;
typedef struct {
int enable;
int sethostname;
int sethttps;
int setport;
const char *headername;
apr_array_header_t *proxy_ips;
const char *orig_scheme;
const char *https_scheme;
int orig_port;
} rpaf_server_cfg;
typedef struct {
const char *old_ip;
request_rec *r;
} rpaf_cleanup_rec;
static void *rpaf_create_server_cfg(apr_pool_t *p, server_rec *s) {
rpaf_server_cfg *cfg = (rpaf_server_cfg *)apr_pcalloc(p, sizeof(rpaf_server_cfg));
if (!cfg)
return NULL;
cfg->proxy_ips = apr_array_make(p, 0, sizeof(char *));
cfg->enable = 0;
cfg->sethostname = 0;
cfg->orig_scheme = s->server_scheme;
cfg->https_scheme = apr_pstrdup(p, "https");
cfg->orig_port = s->port;
return (void *)cfg;
}
static const char *rpaf_set_proxy_ip(cmd_parms *cmd, void *dummy, const char *proxy_ip) {
server_rec *s = cmd->server;
rpaf_server_cfg *cfg = (rpaf_server_cfg *)ap_get_module_config(s->module_config,
&rpaf_module);
/* check for valid syntax of ip */
*(char **)apr_array_push(cfg->proxy_ips) = apr_pstrdup(cmd->pool, proxy_ip);
return NULL;
}
static const char *rpaf_set_headername(cmd_parms *cmd, void *dummy, const char *headername) {
server_rec *s = cmd->server;
rpaf_server_cfg *cfg = (rpaf_server_cfg *)ap_get_module_config(s->module_config,
&rpaf_module);
cfg->headername = headername;
return NULL;
}
static const char *rpaf_enable(cmd_parms *cmd, void *dummy, int flag) {
server_rec *s = cmd->server;
rpaf_server_cfg *cfg = (rpaf_server_cfg *)ap_get_module_config(s->module_config,
&rpaf_module);
cfg->enable = flag;
return NULL;
}
static const char *rpaf_sethostname(cmd_parms *cmd, void *dummy, int flag) {
server_rec *s = cmd->server;
rpaf_server_cfg *cfg = (rpaf_server_cfg *)ap_get_module_config(s->module_config,
&rpaf_module);
cfg->sethostname = flag;
return NULL;
}
static const char *rpaf_sethttps(cmd_parms *cmd, void *dummy, int flag) {
server_rec *s = cmd->server;
rpaf_server_cfg *cfg = (rpaf_server_cfg *)ap_get_module_config(s->module_config,
&rpaf_module);
cfg->sethttps = flag;
return NULL;
}
static const char *rpaf_setport(cmd_parms *cmd, void *dummy, int flag) {
server_rec *s = cmd->server;
rpaf_server_cfg *cfg = (rpaf_server_cfg *)ap_get_module_config(s->module_config,
&rpaf_module);
cfg->setport = flag;
return NULL;
}
static int check_cidr(apr_pool_t *pool, const char *ipcidr, const char *testip) {
char *ip;
int cidr_val;
unsigned int netmask;
char *cidr;
/* TODO: this might not be portable. just use struct in_addr instead? */
uint32_t ipval, testipval;
/* TODO: this iterates once to copy and iterates again to tokenize */
ip = apr_pstrdup(pool, ipcidr);
cidr = ip;
while (*cidr != '\0') {
if (*cidr == '/') {
*(cidr++) = '\0';
break;
}
cidr++;
}
if (cidr == NULL) {
return -1;
}
cidr_val = atoi(cidr);
if (cidr_val < 1 || cidr_val > 32) {
return -1;
}
netmask = 0xffffffff << (32 - cidr_val);
ipval = ntohl(inet_addr(ip));
testipval = ntohl(inet_addr(testip));
return (ipval & netmask) == (testipval & netmask);
}
static int is_in_array(apr_pool_t *pool, const char *remote_ip, apr_array_header_t *proxy_ips) {
int i;
char **list = (char**)proxy_ips->elts;
for (i = 0; i < proxy_ips->nelts; i++) {
if (check_cidr(pool, list[i], remote_ip) == 1) {
return 1;
}
if (strcmp(remote_ip, list[i]) == 0) {
return 1;
}
}
return 0;
}
static apr_status_t rpaf_cleanup(void *data) {
rpaf_cleanup_rec *rcr = (rpaf_cleanup_rec *)data;
rcr->r->connection->remote_ip = apr_pstrdup(rcr->r->connection->pool, rcr->old_ip);
rcr->r->connection->remote_addr->sa.sin.sin_addr.s_addr = apr_inet_addr(rcr->r->connection->remote_ip);
return APR_SUCCESS;
}
static char* last_not_in_array(apr_pool_t *pool,
apr_array_header_t *forwarded_for,
apr_array_header_t *proxy_ips) {
int i;
for (i = (forwarded_for->nelts)-1; i > 0; i--) {
if (!is_in_array(pool, ((char **)forwarded_for->elts)[i], proxy_ips))
break;
}
return ((char **)forwarded_for->elts)[i];
}
static int change_remote_ip(request_rec *r) {
const char *fwdvalue;
char *val;
apr_port_t tmpport;
apr_pool_t *tmppool;
rpaf_server_cfg *cfg = (rpaf_server_cfg *)ap_get_module_config(r->server->module_config,
&rpaf_module);
if (!cfg->enable)
return DECLINED;
if (is_in_array(r->pool, r->connection->remote_ip, cfg->proxy_ips) == 1) {
/* check if cfg->headername is set and if it is use
that instead of X-Forwarded-For by default */
if (cfg->headername && (fwdvalue = apr_table_get(r->headers_in, cfg->headername))) {
//
} else if ((fwdvalue = apr_table_get(r->headers_in, "X-Forwarded-For"))) {
//
} else {
return DECLINED;
}
if (fwdvalue) {
rpaf_cleanup_rec *rcr = (rpaf_cleanup_rec *)apr_pcalloc(r->pool, sizeof(rpaf_cleanup_rec));
apr_array_header_t *arr = apr_array_make(r->pool, 0, sizeof(char*));
while (*fwdvalue && (val = ap_get_token(r->pool, &fwdvalue, 1))) {
*(char **)apr_array_push(arr) = apr_pstrdup(r->pool, val);
if (*fwdvalue != '\0')
++fwdvalue;
}
rcr->old_ip = apr_pstrdup(r->connection->pool, r->connection->remote_ip);
rcr->r = r;
apr_pool_cleanup_register(r->pool, (void *)rcr, rpaf_cleanup, apr_pool_cleanup_null);
r->connection->remote_ip = apr_pstrdup(r->connection->pool, last_not_in_array(r->pool, arr, cfg->proxy_ips));
tmppool = r->connection->remote_addr->pool;
tmpport = r->connection->remote_addr->port;
memset(r->connection->remote_addr, '\0', sizeof(apr_sockaddr_t));
r->connection->remote_addr = NULL;
apr_sockaddr_info_get(&(r->connection->remote_addr), r->connection->remote_ip, APR_UNSPEC, tmpport, 0, tmppool);
if (cfg->sethostname) {
const char *hostvalue;
if ((hostvalue = apr_table_get(r->headers_in, "X-Forwarded-Host")) ||
(hostvalue = apr_table_get(r->headers_in, "X-Host"))) {
apr_array_header_t *arr = apr_array_make(r->pool, 0, sizeof(char*));
while (*hostvalue && (val = ap_get_token(r->pool, &hostvalue, 1))) {
*(char **)apr_array_push(arr) = apr_pstrdup(r->pool, val);
if (*hostvalue != '\0')
++hostvalue;
}
apr_table_set(r->headers_in, "Host", apr_pstrdup(r->pool, ((char **)arr->elts)[((arr->nelts)-1)]));
r->hostname = apr_pstrdup(r->pool, ((char **)arr->elts)[((arr->nelts)-1)]);
ap_update_vhost_from_headers(r);
}
}
if (cfg->sethttps) {
const char *httpsvalue;
if ((httpsvalue = apr_table_get(r->headers_in, "X-Forwarded-HTTPS")) ||
(httpsvalue = apr_table_get(r->headers_in, "X-HTTPS"))) {
apr_table_set(r->subprocess_env, "HTTPS", apr_pstrdup(r->pool, httpsvalue));
r->server->server_scheme = cfg->https_scheme;
} else if ((httpsvalue = apr_table_get(r->headers_in, "X-Forwarded-Proto"))
&& (strcmp(httpsvalue, cfg->https_scheme) == 0)) {
apr_table_set(r->subprocess_env, "HTTPS", apr_pstrdup(r->pool, "on"));
r->server->server_scheme = cfg->https_scheme;
} else {
r->server->server_scheme = cfg->orig_scheme;
}
}
if (cfg->setport) {
const char *portvalue;
if ((portvalue = apr_table_get(r->headers_in, "X-Forwarded-Port")) ||
(portvalue = apr_table_get(r->headers_in, "X-Port"))) {
r->server->port = atoi(portvalue);
r->parsed_uri.port = r->server->port;
} else {
r->server->port = cfg->orig_port;
}
}
}
}
return DECLINED;
}
static const command_rec rpaf_cmds[] = {
AP_INIT_FLAG(
"RPAF_Enable",
rpaf_enable,
NULL,
RSRC_CONF,
"Enable mod_rpaf"
),
AP_INIT_FLAG(
"RPAF_SetHostName",
rpaf_sethostname,
NULL,
RSRC_CONF,
"Let mod_rpaf set the hostname from the X-Host header and update vhosts"
),
AP_INIT_FLAG(
"RPAF_SetHTTPS",
rpaf_sethttps,
NULL,
RSRC_CONF,
"Let mod_rpaf set the HTTPS environment variable from the X-HTTPS header"
),
AP_INIT_FLAG(
"RPAF_SetPort",
rpaf_setport,
NULL,
RSRC_CONF,
"Let mod_rpaf set the server port from the X-Port header"
),
AP_INIT_ITERATE(
"RPAF_ProxyIPs",
rpaf_set_proxy_ip,
NULL,
RSRC_CONF,
"IP(s) of Proxy server setting X-Forwarded-For header"
),
AP_INIT_TAKE1(
"RPAF_Header",
rpaf_set_headername,
NULL,
RSRC_CONF,
"Which header to look for when trying to find the real ip of the client in a proxy setup"
),
{ NULL }
};
static void register_hooks(apr_pool_t *p) {
ap_hook_post_read_request(change_remote_ip, NULL, NULL, APR_HOOK_FIRST);
}
module AP_MODULE_DECLARE_DATA rpaf_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
rpaf_create_server_cfg,
NULL,
rpaf_cmds,
register_hooks,
};