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