Skip to content

Commit ee2cc23

Browse files
committed
Create mod_rpaf-2.0.c
1 parent fa759ab commit ee2cc23

File tree

1 file changed

+252
-0
lines changed

1 file changed

+252
-0
lines changed

mod_rpaf-2.0.c

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
2+
/* ====================================================================
3+
* Copyright (c) 1995 The Apache Group. All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in
14+
* the documentation and/or other materials provided with the
15+
* distribution.
16+
*
17+
* 3. All advertising materials mentioning features or use of this
18+
* software must display the following acknowledgment:
19+
* "This product includes software developed by the Apache Group
20+
* for use in the Apache HTTP server project (http://www.apache.org/)."
21+
*
22+
* 4. The names "Apache Server" and "Apache Group" must not be used to
23+
* endorse or promote products derived from this software without
24+
* prior written permission.
25+
*
26+
* 5. Redistributions of any form whatsoever must retain the following
27+
* acknowledgment:
28+
* "This product includes software developed by the Apache Group
29+
* for use in the Apache HTTP server project (http://www.apache.org/)."
30+
*
31+
* THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
32+
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
35+
* IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
42+
* OF THE POSSIBILITY OF SUCH DAMAGE.
43+
* ====================================================================
44+
*
45+
* This software consists of voluntary contributions made by many
46+
* individuals on behalf of the Apache Group and was originally based
47+
* on public domain software written at the National Center for
48+
* Supercomputing Applications, University of Illinois, Urbana-Champaign.
49+
* For more information on the Apache Group and the Apache HTTP server
50+
* project, please see <http://www.apache.org/>.
51+
*
52+
*/
53+
54+
/*
55+
* $Id: mod_rpaf-2.0.c 18 2008-01-01 03:05:40Z thomas $
56+
*
57+
* Author: Thomas Eibner, <thomas@stderr.net>
58+
* URL: http://stderr.net/apache/rpaf/
59+
* rpaf is short for reverse proxy add forward
60+
*
61+
* This module does the opposite of mod_proxy_add_forward written by
62+
* Ask Bj�rn Hansen. http://develooper.com/code/mpaf/ or mod_proxy
63+
* in 1.3.25 and above and mod_proxy from Apache 2.0
64+
*
65+
*/
66+
67+
#include "httpd.h"
68+
#include "http_config.h"
69+
#include "http_core.h"
70+
#include "http_log.h"
71+
#include "http_protocol.h"
72+
#include "http_vhost.h"
73+
#include "apr_strings.h"
74+
75+
module AP_MODULE_DECLARE_DATA rpaf_module;
76+
77+
typedef struct {
78+
int enable;
79+
int sethostname;
80+
const char *headername;
81+
apr_array_header_t *proxy_ips;
82+
} rpaf_server_cfg;
83+
84+
typedef struct {
85+
const char *old_ip;
86+
request_rec *r;
87+
} rpaf_cleanup_rec;
88+
89+
static void *rpaf_create_server_cfg(apr_pool_t *p, server_rec *s) {
90+
rpaf_server_cfg *cfg = (rpaf_server_cfg *)apr_pcalloc(p, sizeof(rpaf_server_cfg));
91+
if (!cfg)
92+
return NULL;
93+
94+
cfg->proxy_ips = apr_array_make(p, 0, sizeof(char *));
95+
cfg->enable = 0;
96+
cfg->sethostname = 0;
97+
98+
return (void *)cfg;
99+
}
100+
101+
static const char *rpaf_set_proxy_ip(cmd_parms *cmd, void *dummy, const char *proxy_ip) {
102+
server_rec *s = cmd->server;
103+
rpaf_server_cfg *cfg = (rpaf_server_cfg *)ap_get_module_config(s->module_config,
104+
&rpaf_module);
105+
106+
/* check for valid syntax of ip */
107+
*(char **)apr_array_push(cfg->proxy_ips) = apr_pstrdup(cmd->pool, proxy_ip);
108+
return NULL;
109+
}
110+
111+
static const char *rpaf_set_headername(cmd_parms *cmd, void *dummy, const char *headername) {
112+
server_rec *s = cmd->server;
113+
rpaf_server_cfg *cfg = (rpaf_server_cfg *)ap_get_module_config(s->module_config,
114+
&rpaf_module);
115+
116+
cfg->headername = headername;
117+
return NULL;
118+
}
119+
120+
static const char *rpaf_enable(cmd_parms *cmd, void *dummy, int flag) {
121+
server_rec *s = cmd->server;
122+
rpaf_server_cfg *cfg = (rpaf_server_cfg *)ap_get_module_config(s->module_config,
123+
&rpaf_module);
124+
125+
cfg->enable = flag;
126+
return NULL;
127+
}
128+
129+
static const char *rpaf_sethostname(cmd_parms *cmd, void *dummy, int flag) {
130+
server_rec *s = cmd->server;
131+
rpaf_server_cfg *cfg = (rpaf_server_cfg *)ap_get_module_config(s->module_config,
132+
&rpaf_module);
133+
134+
cfg->sethostname = flag;
135+
return NULL;
136+
}
137+
138+
static int is_in_array(const char *remote_ip, apr_array_header_t *proxy_ips) {
139+
int i;
140+
char **list = (char**)proxy_ips->elts;
141+
for (i = 0; i < proxy_ips->nelts; i++) {
142+
if (strcmp(remote_ip, list[i]) == 0)
143+
return 1;
144+
}
145+
return 0;
146+
}
147+
148+
static apr_status_t rpaf_cleanup(void *data) {
149+
rpaf_cleanup_rec *rcr = (rpaf_cleanup_rec *)data;
150+
rcr->r->connection->remote_ip = apr_pstrdup(rcr->r->connection->pool, rcr->old_ip);
151+
rcr->r->connection->remote_addr->sa.sin.sin_addr.s_addr = apr_inet_addr(rcr->r->connection->remote_ip);
152+
return APR_SUCCESS;
153+
}
154+
155+
static int change_remote_ip(request_rec *r) {
156+
const char *fwdvalue;
157+
char *val;
158+
rpaf_server_cfg *cfg = (rpaf_server_cfg *)ap_get_module_config(r->server->module_config,
159+
&rpaf_module);
160+
161+
if (!cfg->enable)
162+
return DECLINED;
163+
164+
if (is_in_array(r->connection->remote_ip, cfg->proxy_ips) == 1) {
165+
/* check if cfg->headername is set and if it is use
166+
that instead of X-Forwarded-For by default */
167+
if (cfg->headername && (fwdvalue = apr_table_get(r->headers_in, cfg->headername))) {
168+
//
169+
} else if (fwdvalue = apr_table_get(r->headers_in, "X-Forwarded-For")) {
170+
//
171+
} else {
172+
return DECLINED;
173+
}
174+
175+
if (fwdvalue) {
176+
rpaf_cleanup_rec *rcr = (rpaf_cleanup_rec *)apr_pcalloc(r->pool, sizeof(rpaf_cleanup_rec));
177+
apr_array_header_t *arr = apr_array_make(r->pool, 0, sizeof(char*));
178+
while (*fwdvalue && (val = ap_get_token(r->pool, &fwdvalue, 1))) {
179+
*(char **)apr_array_push(arr) = apr_pstrdup(r->pool, val);
180+
if (*fwdvalue != '\0')
181+
++fwdvalue;
182+
}
183+
rcr->old_ip = apr_pstrdup(r->connection->pool, r->connection->remote_ip);
184+
rcr->r = r;
185+
apr_pool_cleanup_register(r->pool, (void *)rcr, rpaf_cleanup, apr_pool_cleanup_null);
186+
r->connection->remote_ip = apr_pstrdup(r->connection->pool, ((char **)arr->elts)[((arr->nelts)-1)]);
187+
r->connection->remote_addr->sa.sin.sin_addr.s_addr = apr_inet_addr(r->connection->remote_ip);
188+
if (cfg->sethostname) {
189+
const char *hostvalue;
190+
if (hostvalue = apr_table_get(r->headers_in, "X-Forwarded-Host")) {
191+
/* 2.0 proxy frontend or 1.3 => 1.3.25 proxy frontend */
192+
apr_table_set(r->headers_in, "Host", apr_pstrdup(r->pool, hostvalue));
193+
r->hostname = apr_pstrdup(r->pool, hostvalue);
194+
ap_update_vhost_from_headers(r);
195+
} else if (hostvalue = apr_table_get(r->headers_in, "X-Host")) {
196+
/* 1.3 proxy frontend with mod_proxy_add_forward */
197+
apr_table_set(r->headers_in, "Host", apr_pstrdup(r->pool, hostvalue));
198+
r->hostname = apr_pstrdup(r->pool, hostvalue);
199+
ap_update_vhost_from_headers(r);
200+
}
201+
}
202+
203+
}
204+
}
205+
return DECLINED;
206+
}
207+
208+
static const command_rec rpaf_cmds[] = {
209+
AP_INIT_FLAG(
210+
"RPAFenable",
211+
rpaf_enable,
212+
NULL,
213+
RSRC_CONF,
214+
"Enable mod_rpaf"
215+
),
216+
AP_INIT_FLAG(
217+
"RPAFsethostname",
218+
rpaf_sethostname,
219+
NULL,
220+
RSRC_CONF,
221+
"Let mod_rpaf set the hostname from X-Host header and update vhosts"
222+
),
223+
AP_INIT_ITERATE(
224+
"RPAFproxy_ips",
225+
rpaf_set_proxy_ip,
226+
NULL,
227+
RSRC_CONF,
228+
"IP(s) of Proxy server setting X-Forwarded-For header"
229+
),
230+
AP_INIT_TAKE1(
231+
"RPAFheader",
232+
rpaf_set_headername,
233+
NULL,
234+
RSRC_CONF,
235+
"Which header to look for when trying to find the real ip of the client in a proxy setup"
236+
),
237+
{ NULL }
238+
};
239+
240+
static void register_hooks(apr_pool_t *p) {
241+
ap_hook_post_read_request(change_remote_ip, NULL, NULL, APR_HOOK_FIRST);
242+
}
243+
244+
module AP_MODULE_DECLARE_DATA rpaf_module = {
245+
STANDARD20_MODULE_STUFF,
246+
NULL,
247+
NULL,
248+
rpaf_create_server_cfg,
249+
NULL,
250+
rpaf_cmds,
251+
register_hooks,
252+
};

0 commit comments

Comments
 (0)