Implemented param tracking for hook users, feels a bit hackish
[lhc/web/wiklou.git] / includes / ProxyTools.php
1 <?php
2 /**
3 * Functions for dealing with proxies
4 *
5 * @file
6 */
7
8 /**
9 * Extracts the XFF string from the request header
10 * Checks first for "X-Forwarded-For", then "Client-ip"
11 * Note: headers are spoofable
12 * @return string
13 */
14 function wfGetForwardedFor() {
15 $apacheHeaders = function_exists( 'apache_request_headers' ) ? apache_request_headers() : null;
16 if( is_array( $apacheHeaders ) ) {
17 // More reliable than $_SERVER due to case and -/_ folding
18 $set = array ();
19 foreach ( $apacheHeaders as $tempName => $tempValue ) {
20 $set[ strtoupper( $tempName ) ] = $tempValue;
21 }
22 $index = strtoupper ( 'X-Forwarded-For' );
23 $index2 = strtoupper ( 'Client-ip' );
24 } else {
25 // Subject to spoofing with headers like X_Forwarded_For
26 $set = $_SERVER;
27 $index = 'HTTP_X_FORWARDED_FOR';
28 $index2 = 'CLIENT-IP';
29 }
30
31 #Try a couple of headers
32 if( isset( $set[$index] ) ) {
33 return $set[$index];
34 } else if( isset( $set[$index2] ) ) {
35 return $set[$index2];
36 } else {
37 return null;
38 }
39 }
40
41 /**
42 * Returns the browser/OS data from the request header
43 * Note: headers are spoofable
44 * @return string
45 */
46 function wfGetAgent() {
47 if( function_exists( 'apache_request_headers' ) ) {
48 // More reliable than $_SERVER due to case and -/_ folding
49 $set = array ();
50 foreach ( apache_request_headers() as $tempName => $tempValue ) {
51 $set[ strtoupper( $tempName ) ] = $tempValue;
52 }
53 $index = strtoupper ( 'User-Agent' );
54 } else {
55 // Subject to spoofing with headers like X_Forwarded_For
56 $set = $_SERVER;
57 $index = 'HTTP_USER_AGENT';
58 }
59 if( isset( $set[$index] ) ) {
60 return $set[$index];
61 } else {
62 return '';
63 }
64 }
65
66 /**
67 * Work out the IP address based on various globals
68 * For trusted proxies, use the XFF client IP (first of the chain)
69 * @return string
70 */
71 function wfGetIP() {
72 global $wgUsePrivateIPs, $wgCommandLineMode;
73 static $ip = false;
74
75 # Return cached result
76 if ( !empty( $ip ) ) {
77 return $ip;
78 }
79
80 $ipchain = array();
81
82 /* collect the originating ips */
83 # Client connecting to this webserver
84 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
85 $ip = IP::canonicalize( $_SERVER['REMOTE_ADDR'] );
86 } elseif( $wgCommandLineMode ) {
87 $ip = '127.0.0.1';
88 }
89 if( $ip ) {
90 $ipchain[] = $ip;
91 }
92
93 # Append XFF on to $ipchain
94 $forwardedFor = wfGetForwardedFor();
95 if ( isset( $forwardedFor ) ) {
96 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
97 $xff = array_reverse( $xff );
98 $ipchain = array_merge( $ipchain, $xff );
99 }
100
101 # Step through XFF list and find the last address in the list which is a trusted server
102 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
103 foreach ( $ipchain as $i => $curIP ) {
104 $curIP = IP::canonicalize( $curIP );
105 if ( wfIsTrustedProxy( $curIP ) ) {
106 if ( isset( $ipchain[$i + 1] ) ) {
107 if( $wgUsePrivateIPs || IP::isPublic( $ipchain[$i + 1 ] ) ) {
108 $ip = $ipchain[$i + 1];
109 }
110 }
111 } else {
112 break;
113 }
114 }
115
116 # Allow extensions to improve our guess
117 wfRunHooks( 'GetIP', array( &$ip ) );
118
119 if( !$ip ) {
120 throw new MWException( "Unable to determine IP" );
121 }
122
123 wfDebug( "IP: $ip\n" );
124 return $ip;
125 }
126
127 /**
128 * Checks if an IP is a trusted proxy providor
129 * Useful to tell if X-Fowarded-For data is possibly bogus
130 * Squid cache servers for the site and AOL are whitelisted
131 * @param $ip String
132 * @return bool
133 */
134 function wfIsTrustedProxy( $ip ) {
135 global $wgSquidServers, $wgSquidServersNoPurge;
136
137 if ( in_array( $ip, $wgSquidServers ) ||
138 in_array( $ip, $wgSquidServersNoPurge )
139 ) {
140 $trusted = true;
141 } else {
142 $trusted = false;
143 }
144 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
145 return $trusted;
146 }
147
148 /**
149 * Forks processes to scan the originating IP for an open proxy server
150 * MemCached can be used to skip IPs that have already been scanned
151 */
152 function wfProxyCheck() {
153 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
154 global $wgMemc, $wgProxyMemcExpiry;
155 global $wgProxyKey;
156
157 if ( !$wgBlockOpenProxies ) {
158 return;
159 }
160
161 $ip = wfGetIP();
162
163 # Get MemCached key
164 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
165 $mcValue = $wgMemc->get( $mcKey );
166 $skip = (bool)$mcValue;
167
168 # Fork the processes
169 if ( !$skip ) {
170 $title = SpecialPage::getTitleFor( 'Blockme' );
171 $iphash = md5( $ip . $wgProxyKey );
172 $url = $title->getFullURL( 'ip='.$iphash );
173
174 foreach ( $wgProxyPorts as $port ) {
175 $params = implode( ' ', array(
176 escapeshellarg( $wgProxyScriptPath ),
177 escapeshellarg( $ip ),
178 escapeshellarg( $port ),
179 escapeshellarg( $url )
180 ));
181 exec( "php $params >" . wfGetNull() . " 2>&1 &" );
182 }
183 # Set MemCached key
184 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
185 }
186 }
187
188 /**
189 * Convert a network specification in CIDR notation to an integer network and a number of bits
190 *
191 * @deprecated Call IP::parseCIDR() directly, will be removed in 1.19
192 * @return array(string, int)
193 */
194 function wfParseCIDR( $range ) {
195 wfDeprecated( __FUNCTION__ );
196 return IP::parseCIDR( $range );
197 }
198
199 /**
200 * Check if an IP address is in the local proxy list
201 * @return bool
202 */
203 function wfIsLocallyBlockedProxy( $ip ) {
204 global $wgProxyList;
205
206 if ( !$wgProxyList ) {
207 return false;
208 }
209 wfProfileIn( __METHOD__ );
210
211 if ( !is_array( $wgProxyList ) ) {
212 # Load from the specified file
213 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
214 }
215
216 if ( !is_array( $wgProxyList ) ) {
217 $ret = false;
218 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
219 $ret = true;
220 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
221 # Old-style flipped proxy list
222 $ret = true;
223 } else {
224 $ret = false;
225 }
226 wfProfileOut( __METHOD__ );
227 return $ret;
228 }
229