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