Fix for bug 2522
[lhc/web/wiklou.git] / includes / ProxyTools.php
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die();
4 /**
5 * Functions for dealing with proxies
6 * @package MediaWiki
7 */
8
9 function wfGetForwardedFor() {
10 if( function_exists( 'apache_request_headers' ) ) {
11 // More reliable than $_SERVER due to case and -/_ folding
12 $set = apache_request_headers();
13 $index = 'X-Forwarded-For';
14 } else {
15 // Subject to spoofing with headers like X_Forwarded_For
16 $set = $_SERVER;
17 $index = 'HTTP_X_FORWARDED_FOR';
18 }
19 if( isset( $set[$index] ) ) {
20 return $set[$index];
21 } else {
22 return null;
23 }
24 }
25
26 /** Work out the IP address based on various globals */
27 function wfGetIP() {
28 global $wgSquidServers, $wgSquidServersNoPurge, $wgIP;
29
30 # Return cached result
31 if ( !empty( $wgIP ) ) {
32 return $wgIP;
33 }
34
35 /* collect the originating ips */
36 # Client connecting to this webserver
37 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
38 $ipchain = array( $_SERVER['REMOTE_ADDR'] );
39 } else {
40 # Running on CLI?
41 $ipchain = array( '127.0.0.1' );
42 }
43 $ip = $ipchain[0];
44
45 # Get list of trusted proxies
46 # Flipped for quicker access
47 $trustedProxies = array_flip( array_merge( $wgSquidServers, $wgSquidServersNoPurge ) );
48 if ( count( $trustedProxies ) ) {
49 # Append XFF on to $ipchain
50 $forwardedFor = wfGetForwardedFor();
51 if ( isset( $forwardedFor ) ) {
52 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
53 $xff = array_reverse( $xff );
54 $ipchain = array_merge( $ipchain, $xff );
55 }
56 # Step through XFF list and find the last address in the list which is a trusted server
57 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
58 foreach ( $ipchain as $i => $curIP ) {
59 if ( array_key_exists( $curIP, $trustedProxies ) ) {
60 if ( isset( $ipchain[$i + 1] ) && wfIsIPPublic( $ipchain[$i + 1] ) ) {
61 $ip = $ipchain[$i + 1];
62 }
63 } else {
64 break;
65 }
66 }
67 }
68
69 wfDebug( "IP: $ip\n" );
70 $wgIP = $ip;
71 return $ip;
72 }
73
74 /**
75 * Given an IP address in dotted-quad notation, returns an unsigned integer.
76 * Like ip2long() except that it actually works and has a consistent error return value.
77 */
78 function wfIP2Unsigned( $ip ) {
79 $n = ip2long( $ip );
80 if ( $n == -1 || $n === false ) { # Return value on error depends on PHP version
81 $n = false;
82 } elseif ( $n < 0 ) {
83 $n += pow( 2, 32 );
84 }
85 return $n;
86 }
87
88 /**
89 * Return a zero-padded hexadecimal representation of an IP address
90 */
91 function wfIP2Hex( $ip ) {
92 $n = wfIP2Unsigned( $ip );
93 if ( $n !== false ) {
94 $n = sprintf( '%08X', $n );
95 }
96 return $n;
97 }
98
99 /**
100 * Determine if an IP address really is an IP address, and if it is public,
101 * i.e. not RFC 1918 or similar
102 */
103 function wfIsIPPublic( $ip ) {
104 $n = wfIP2Unsigned( $ip );
105 if ( !$n ) {
106 return false;
107 }
108
109 static $privateRanges = false;
110 if ( !$privateRanges ) {
111 $privateRanges = array(
112 array( '10.0.0.0', '10.255.255.255' ), # RFC 1918 (private)
113 array( '172.16.0.0', '172.31.255.255' ), # "
114 array( '192.168.0.0', '192.168.255.255' ), # "
115 array( '0.0.0.0', '0.255.255.255' ), # this network
116 array( '127.0.0.0', '127.255.255.255' ), # loopback
117 );
118 }
119
120 foreach ( $privateRanges as $r ) {
121 $start = wfIP2Unsigned( $r[0] );
122 $end = wfIP2Unsigned( $r[1] );
123 if ( $n >= $start && $n <= $end ) {
124 return false;
125 }
126 }
127 return true;
128 }
129
130 /**
131 * Forks processes to scan the originating IP for an open proxy server
132 * MemCached can be used to skip IPs that have already been scanned
133 */
134 function wfProxyCheck() {
135 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
136 global $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
137
138 if ( !$wgBlockOpenProxies ) {
139 return;
140 }
141
142 $ip = wfGetIP();
143
144 # Get MemCached key
145 $skip = false;
146 if ( $wgUseMemCached ) {
147 $mcKey = "$wgDBname:proxy:ip:$ip";
148 $mcValue = $wgMemc->get( $mcKey );
149 if ( $mcValue ) {
150 $skip = true;
151 }
152 }
153
154 # Fork the processes
155 if ( !$skip ) {
156 $title = Title::makeTitle( NS_SPECIAL, 'Blockme' );
157 $iphash = md5( $ip . $wgProxyKey );
158 $url = $title->getFullURL( 'ip='.$iphash );
159
160 foreach ( $wgProxyPorts as $port ) {
161 $params = implode( ' ', array(
162 escapeshellarg( $wgProxyScriptPath ),
163 escapeshellarg( $ip ),
164 escapeshellarg( $port ),
165 escapeshellarg( $url )
166 ));
167 exec( "php $params &>/dev/null &" );
168 }
169 # Set MemCached key
170 if ( $wgUseMemCached ) {
171 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
172 }
173 }
174 }
175
176 /**
177 * Convert a network specification in CIDR notation to an integer network and a number of bits
178 */
179 function wfParseCIDR( $range ) {
180 $parts = explode( '/', $range, 2 );
181 if ( count( $parts ) != 2 ) {
182 return array( false, false );
183 }
184 $network = wfIP2Unsigned( $parts[0] );
185 if ( $network !== false && is_numeric( $parts[1] ) && $parts[1] >= 0 && $parts[1] <= 32 ) {
186 $bits = $parts[1];
187 } else {
188 $network = false;
189 $bits = false;
190 }
191 return array( $network, $bits );
192 }
193
194 /**
195 * Check if an IP address is in the local proxy list
196 */
197 function wfIsLocallyBlockedProxy( $ip ) {
198 global $wgProxyList;
199 $fname = 'wfIsLocallyBlockedProxy';
200
201 if ( !$wgProxyList ) {
202 return false;
203 }
204 wfProfileIn( $fname );
205
206 if ( !is_array( $wgProxyList ) ) {
207 # Load from the specified file
208 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
209 }
210
211 if ( !is_array( $wgProxyList ) ) {
212 $ret = false;
213 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
214 $ret = true;
215 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
216 # Old-style flipped proxy list
217 $ret = true;
218 } else {
219 $ret = false;
220 }
221 wfProfileOut( $fname );
222 return $ret;
223 }
224
225
226
227
228 ?>