@@ -37,11 +37,17 @@ typedef struct {
37
37
} remoteip_proxymatch_t ;
38
38
39
39
typedef struct {
40
- /** The header to retrieve a proxy-via ip list */
40
+ conn_rec * conn ;
41
+ apr_sockaddr_t * remote_addr ;
42
+ char * remote_ip ;
43
+ } remoteip_cleanup_rec_t ;
44
+
45
+ typedef struct {
46
+ /** The header to retrieve a proxy-via IP list */
41
47
const char * header_name ;
42
48
/** A header to record the proxied IP's
43
49
* (removed as the physical connection and
44
- * from the proxy-via ip header value list)
50
+ * from the proxy-via IP header value list)
45
51
*/
46
52
const char * proxies_header_name ;
47
53
/** A list of trusted proxies, ideally configured
@@ -51,11 +57,11 @@ typedef struct {
51
57
} remoteip_config_t ;
52
58
53
59
typedef struct {
54
- apr_sockaddr_t * remote_addr ;
55
- char * remote_ip ;
56
- /** The list of proxy ip 's ignored as remote ip 's */
60
+ apr_sockaddr_t * useragent_addr ;
61
+ char * useragent_ip ;
62
+ /** The list of proxy IP 's ignored as remote IP 's */
57
63
const char * proxy_ips ;
58
- /** The remaining list of untrusted proxied remote ip 's */
64
+ /** The remaining list of untrusted proxied remote IP 's */
59
65
const char * proxied_remote ;
60
66
} remoteip_req_t ;
61
67
@@ -170,7 +176,7 @@ static const char *proxies_set(cmd_parms *cmd, void *cfg,
170
176
}
171
177
172
178
if (rv != APR_SUCCESS ) {
173
- char msgbuf [128 ];
179
+ char msgbuf [MAX_STRING_LEN ];
174
180
apr_strerror (rv , msgbuf , sizeof (msgbuf ));
175
181
return apr_pstrcat (cmd -> pool , "RemoteIP: Error parsing IP " , arg ,
176
182
" (" , msgbuf , " error) for " , cmd -> cmd -> name , NULL );
@@ -200,11 +206,12 @@ static const char *proxylist_read(cmd_parms *cmd, void *cfg,
200
206
while (!(ap_cfg_getline (lbuf , MAX_STRING_LEN , cfp ))) {
201
207
args = lbuf ;
202
208
while (* (arg = ap_getword_conf (cmd -> temp_pool , & args )) != '\0' ) {
203
- if (* arg == '#' || * arg == '\0' ) {
209
+ if (* arg == '#' ) {
204
210
break ;
205
211
}
206
212
errmsg = proxies_set (cmd , cfg , arg );
207
213
if (errmsg ) {
214
+ ap_cfg_closefile (cfp );
208
215
errmsg = apr_psprintf (cmd -> pool , "%s at line %d of %s" ,
209
216
errmsg , cfp -> line_number , filename );
210
217
return errmsg ;
@@ -216,12 +223,20 @@ static const char *proxylist_read(cmd_parms *cmd, void *cfg,
216
223
return NULL ;
217
224
}
218
225
226
+ static apr_status_t remoteip_cleanup (void * data ) {
227
+ remoteip_cleanup_rec_t * cleanup_rec = (remoteip_cleanup_rec_t * )data ;
228
+ cleanup_rec -> conn -> remote_addr = cleanup_rec -> remote_addr ;
229
+ cleanup_rec -> conn -> remote_ip = cleanup_rec -> remote_ip ;
230
+ return APR_SUCCESS ;
231
+ }
232
+
219
233
static int remoteip_modify_request (request_rec * r )
220
234
{
221
235
conn_rec * c = r -> connection ;
222
236
remoteip_config_t * config = (remoteip_config_t * )
223
237
ap_get_module_config (r -> server -> module_config , & remoteip_module );
224
238
remoteip_req_t * req = NULL ;
239
+ remoteip_cleanup_rec_t * cleanup_rec ;
225
240
226
241
apr_sockaddr_t * temp_sa ;
227
242
@@ -247,14 +262,14 @@ static int remoteip_modify_request(request_rec *r)
247
262
248
263
while (remote ) {
249
264
250
- /* verify c->remote_addr is trusted if there is a trusted proxy list
265
+ /* verify user agent IP against the trusted proxy list
251
266
*/
252
267
if (config -> proxymatch_ip ) {
253
268
int i ;
254
269
remoteip_proxymatch_t * match ;
255
270
match = (remoteip_proxymatch_t * )config -> proxymatch_ip -> elts ;
256
271
for (i = 0 ; i < config -> proxymatch_ip -> nelts ; ++ i ) {
257
- if (apr_ipsubnet_test (match [i ].ip , c -> remote_addr )) {
272
+ if (apr_ipsubnet_test (match [i ].ip , temp_sa )) {
258
273
internal = match [i ].internal ;
259
274
break ;
260
275
}
@@ -291,7 +306,7 @@ static int remoteip_modify_request(request_rec *r)
291
306
break ;
292
307
}
293
308
294
- /* We map as IPv4 rather than IPv6 for equivilant host names
309
+ /* We map as IPv4 rather than IPv6 for equivalent host names
295
310
* or IPV4OVERIPV6
296
311
*/
297
312
rv = apr_sockaddr_info_get (& temp_sa , parse_remote ,
@@ -310,7 +325,6 @@ static int remoteip_modify_request(request_rec *r)
310
325
remote = parse_remote ;
311
326
}
312
327
break ;
313
-
314
328
}
315
329
316
330
addrbyte = (unsigned char * ) & temp_sa -> sa .sin .sin_addr ;
@@ -356,19 +370,19 @@ static int remoteip_modify_request(request_rec *r)
356
370
req = (remoteip_req_t * ) apr_palloc (r -> pool , sizeof (remoteip_req_t ));
357
371
}
358
372
359
- /* Set remote_ip string */
373
+ /* Set useragent_ip string */
360
374
if (!internal ) {
361
375
if (proxy_ips ) {
362
376
proxy_ips = apr_pstrcat (r -> pool , proxy_ips , ", " ,
363
- c -> remote_ip , NULL );
377
+ req -> useragent_ip , NULL );
364
378
}
365
379
else {
366
- proxy_ips = c -> remote_ip ;
380
+ proxy_ips = req -> useragent_ip ;
367
381
}
368
382
}
369
383
370
- req -> remote_addr = temp_sa ;
371
- apr_sockaddr_ip_get (& req -> remote_ip , req -> remote_addr );
384
+ req -> useragent_addr = temp_sa ;
385
+ apr_sockaddr_ip_get (& req -> useragent_ip , req -> useragent_addr );
372
386
}
373
387
374
388
/* Nothing happened? */
@@ -394,14 +408,21 @@ static int remoteip_modify_request(request_rec *r)
394
408
}
395
409
}
396
410
397
- c -> remote_addr = req -> remote_addr ;
398
- c -> remote_ip = req -> remote_ip ;
411
+ cleanup_rec = (remoteip_cleanup_rec_t * )apr_pcalloc (r -> pool , sizeof (remoteip_cleanup_rec_t ));
412
+ cleanup_rec -> conn = c ;
413
+ cleanup_rec -> remote_addr = c -> remote_addr ;
414
+ cleanup_rec -> remote_ip = c -> remote_ip ;
415
+ apr_pool_cleanup_register (r -> pool , cleanup_rec , remoteip_cleanup , apr_pool_cleanup_null );
416
+
417
+ c -> remote_addr = req -> useragent_addr ;
418
+ c -> remote_ip = req -> useragent_ip ;
399
419
400
420
ap_log_rerror (APLOG_MARK , APLOG_INFO , 0 , r ,
401
421
req -> proxy_ips
402
422
? "Using %s as client's IP by proxies %s"
403
- : "Using %s as client's IP by internal proxies" ,
404
- req -> remote_ip , req -> proxy_ips );
423
+ : "Using %s as client's IP by internal proxies%s" ,
424
+ req -> useragent_ip ,
425
+ (req -> proxy_ips ? req -> proxy_ips : "" ));
405
426
return OK ;
406
427
}
407
428
0 commit comments