* Added wfDie() wrapper, and some manual die(-1), to force the return code
[lhc/web/wiklou.git] / includes / ProxyTools.php
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( -1 );
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 global $wgProxyKey;
138
139 if ( !$wgBlockOpenProxies ) {
140 return;
141 }
142
143 $ip = wfGetIP();
144
145 # Get MemCached key
146 $skip = false;
147 if ( $wgUseMemCached ) {
148 $mcKey = "$wgDBname:proxy:ip:$ip";
149 $mcValue = $wgMemc->get( $mcKey );
150 if ( $mcValue ) {
151 $skip = true;
152 }
153 }
154
155 # Fork the processes
156 if ( !$skip ) {
157 $title = Title::makeTitle( NS_SPECIAL, 'Blockme' );
158 $iphash = md5( $ip . $wgProxyKey );
159 $url = $title->getFullURL( 'ip='.$iphash );
160
161 foreach ( $wgProxyPorts as $port ) {
162 $params = implode( ' ', array(
163 escapeshellarg( $wgProxyScriptPath ),
164 escapeshellarg( $ip ),
165 escapeshellarg( $port ),
166 escapeshellarg( $url )
167 ));
168 exec( "php $params &>/dev/null &" );
169 }
170 # Set MemCached key
171 if ( $wgUseMemCached ) {
172 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
173 }
174 }
175 }
176
177 /**
178 * Convert a network specification in CIDR notation to an integer network and a number of bits
179 */
180 function wfParseCIDR( $range ) {
181 $parts = explode( '/', $range, 2 );
182 if ( count( $parts ) != 2 ) {
183 return array( false, false );
184 }
185 $network = wfIP2Unsigned( $parts[0] );
186 if ( $network !== false && is_numeric( $parts[1] ) && $parts[1] >= 0 && $parts[1] <= 32 ) {
187 $bits = $parts[1];
188 } else {
189 $network = false;
190 $bits = false;
191 }
192 return array( $network, $bits );
193 }
194
195 /**
196 * Check if an IP address is in the local proxy list
197 */
198 function wfIsLocallyBlockedProxy( $ip ) {
199 global $wgProxyList;
200 $fname = 'wfIsLocallyBlockedProxy';
201
202 if ( !$wgProxyList ) {
203 return false;
204 }
205 wfProfileIn( $fname );
206
207 if ( !is_array( $wgProxyList ) ) {
208 # Load from the specified file
209 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
210 }
211
212 if ( !is_array( $wgProxyList ) ) {
213 $ret = false;
214 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
215 $ret = true;
216 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
217 # Old-style flipped proxy list
218 $ret = true;
219 } else {
220 $ret = false;
221 }
222 wfProfileOut( $fname );
223 return $ret;
224 }
225
226
227
228
229 ?>