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