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