2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * This library is used with the server IP allow/deny host authentication
9 if (! defined('PHPMYADMIN')) {
14 * Gets the "true" IP address of the current user
16 * @return string the ip of the user
22 /* Get the address of user */
23 if (!empty($_SERVER['REMOTE_ADDR'])) {
24 $direct_ip = $_SERVER['REMOTE_ADDR'];
26 /* We do not know remote IP */
30 /* Do we trust this IP as a proxy? If yes we will use it's header. */
31 if (isset($GLOBALS['cfg']['TrustedProxies'][$direct_ip])) {
33 = PMA_getenv($GLOBALS['cfg']['TrustedProxies'][$direct_ip]);
35 // the $ checks that the header contains only one IP address,
36 // ?: makes sure the () don't capture
38 '|^(?:[0-9]{1,3}\.){3,3}[0-9]{1,3}$|',
39 $trusted_header_value, $matches
41 if ($is_ip && (count($matches) == 1)) {
42 // True IP behind a proxy
49 } // end of the 'PMA_getIp()' function
53 * Matches for IPv4 or IPv6 addresses
55 * @param string $testRange string of IP range to match
56 * @param string $ipToTest string of IP to test against range
58 * @return boolean whether the IP mask matches
62 function PMA_ipMaskTest($testRange, $ipToTest)
66 if (strpos($testRange, ':') > -1 ||
strpos($ipToTest, ':') > -1) {
68 $result = PMA_ipv6MaskTest($testRange, $ipToTest);
70 $result = PMA_ipv4MaskTest($testRange, $ipToTest);
74 } // end of the "PMA_ipMaskTest()" function
78 * Based on IP Pattern Matcher
79 * Originally by J.Adams <jna@retina.net>
80 * Found on <http://www.php.net/manual/en/function.ip2long.php>
81 * Modified for phpMyAdmin
84 * xxx.xxx.xxx.xxx (exact)
85 * xxx.xxx.xxx.[yyy-zzz] (range)
86 * xxx.xxx.xxx.xxx/nn (CIDR)
89 * xxx.xxx.xxx.xx[yyy-zzz] (range, partial octets not supported)
91 * @param string $testRange string of IP range to match
92 * @param string $ipToTest string of IP to test against range
94 * @return boolean whether the IP mask matches
98 function PMA_ipv4MaskTest($testRange, $ipToTest)
102 '|([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/([0-9]+)|',
107 // performs a mask match
108 $ipl = ip2long($ipToTest);
110 $regs[1] . '.' . $regs[2] . '.' . $regs[3] . '.' . $regs[4]
115 for ($i = 0; $i < 31; $i++
) {
116 if ($i < $regs[5] - 1) {
117 $maskl = $maskl + PMA_Util
::pow(2, (30 - $i));
121 if (($maskl & $rangel) == ($maskl & $ipl)) {
128 $maskocts = explode('.', $testRange);
129 $ipocts = explode('.', $ipToTest);
131 // perform a range match
132 for ($i = 0; $i < 4; $i++
) {
133 if (preg_match('|\[([0-9]+)\-([0-9]+)\]|', $maskocts[$i], $regs)) {
134 if (($ipocts[$i] > $regs[2]) ||
($ipocts[$i] < $regs[1])) {
138 if ($maskocts[$i] <> $ipocts[$i]) {
146 } // end of the "PMA_ipv4MaskTest()" function
151 * CIDR section taken from http://stackoverflow.com/a/10086404
152 * Modified for phpMyAdmin
155 * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx (exact)
156 * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:[yyyy-zzzz] (range, only at end of IP - no subnets)
157 * xxxx:xxxx:xxxx:xxxx/nn (CIDR)
160 * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xx[yyy-zzz] (range, partial octets not supported)
162 * @param string $test_range string of IP range to match
163 * @param string $ip_to_test string of IP to test against range
165 * @return boolean whether the IP mask matches
169 function PMA_ipv6MaskTest($test_range, $ip_to_test)
173 // convert to lowercase for easier comparison
174 $test_range = strtolower($test_range);
175 $ip_to_test = strtolower($ip_to_test);
177 $is_cidr = strpos($test_range, '/') > -1;
178 $is_range = strpos($test_range, '[') > -1;
179 $is_single = ! $is_cidr && ! $is_range;
181 $ip_hex = bin2hex(inet_pton($ip_to_test));
184 $range_hex = bin2hex(inet_pton($test_range));
185 $result = $ip_hex === $range_hex;
186 } elseif ($is_range) {
187 // what range do we operate on?
188 $range_match = array();
189 if (preg_match('/\[([0-9a-f]+)\-([0-9a-f]+)\]/', $test_range, $range_match)) {
190 $range_start = $range_match[1];
191 $range_end = $range_match[2];
193 // get the first and last allowed IPs
194 $first_ip = str_replace($range_match[0], $range_start, $test_range);
195 $first_hex = bin2hex(inet_pton($first_ip));
196 $last_ip = str_replace($range_match[0], $range_end, $test_range);
197 $last_hex = bin2hex(inet_pton($last_ip));
199 // check if the IP to test is within the range
200 $result = ($ip_hex >= $first_hex && $ip_hex <= $last_hex);
202 } elseif ($is_cidr) {
203 // Split in address and prefix length
204 list($first_ip, $subnet) = explode('/', $test_range);
206 // Parse the address into a binary string
207 $first_bin = inet_pton($first_ip);
208 $first_hex = bin2hex($first_bin);
210 // Overwriting first address string to make sure notation is optimal
211 $first_ip = inet_ntop($first_bin);
213 $flexbits = 128 - $subnet;
215 // Build the hexadecimal string of the last address
216 $last_hex = $first_hex;
219 while ($flexbits > 0) {
220 // Get the character at this position
221 $orig = substr($last_hex, $pos, 1);
223 // Convert it to an integer
224 $origval = hexdec($orig);
226 // OR it with (2^flexbits)-1, with flexbits limited to 4 at a time
227 $newval = $origval |
(pow(2, min(4, $flexbits)) - 1);
229 // Convert it back to a hexadecimal character
230 $new = dechex($newval);
232 // And put that character back in the string
233 $last_hex = substr_replace($last_hex, $new, $pos, 1);
235 // We processed one nibble, move to previous position
240 // check if the IP to test is within the range
241 $result = ($ip_hex >= $first_hex && $ip_hex <= $last_hex);
245 } // end of the "PMA_ipv6MaskTest()" function
249 * Runs through IP Allow/Deny rules the use of it below for more information
251 * @param string $type 'allow' | 'deny' type of rule to match
253 * @return bool Matched a rule ?
259 function PMA_allowDeny($type)
263 // Grabs true IP of the user and returns if it can't be found
264 $remote_ip = PMA_getIp();
265 if (empty($remote_ip)) {
270 $username = $cfg['Server']['user'];
272 // copy rule database
273 $rules = $cfg['Server']['AllowDeny']['rules'];
275 // lookup table for some name shortcuts
277 'all' => '0.0.0.0/0',
278 'localhost' => '127.0.0.1/8'
281 // Provide some useful shortcuts if server gives us address:
282 if (PMA_getenv('SERVER_ADDR')) {
283 $shortcuts['localnetA'] = PMA_getenv('SERVER_ADDR') . '/8';
284 $shortcuts['localnetB'] = PMA_getenv('SERVER_ADDR') . '/16';
285 $shortcuts['localnetC'] = PMA_getenv('SERVER_ADDR') . '/24';
288 foreach ($rules as $rule) {
290 $rule_data = explode(' ', $rule);
292 // check for rule type
293 if ($rule_data[0] != $type) {
297 // check for username
298 if (($rule_data[1] != '%') //wildcarded first
299 && ($rule_data[1] != $username)
304 // check if the config file has the full string with an extra
305 // 'from' in it and if it does, just discard it
306 if ($rule_data[2] == 'from') {
307 $rule_data[2] = $rule_data[3];
310 // Handle shortcuts with above array
311 if (isset($shortcuts[$rule_data[2]])) {
312 $rule_data[2] = $shortcuts[$rule_data[2]];
315 // Add code for host lookups here
316 // Excluded for the moment
318 // Do the actual matching now
319 if (PMA_ipMaskTest($rule_data[2], $remote_ip)) {
325 } // end of the "PMA_AllowDeny()" function