ip_allow_deny.lib.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * This library is used with the server IP allow/deny host authentication
  5. * feature
  6. *
  7. * @package PhpMyAdmin
  8. */
  9. if (! defined('PHPMYADMIN')) {
  10. exit;
  11. }
  12. /**
  13. * Gets the "true" IP address of the current user
  14. *
  15. * @return string the ip of the user
  16. *
  17. * @access private
  18. */
  19. function PMA_getIp()
  20. {
  21. /* Get the address of user */
  22. if (empty($_SERVER['REMOTE_ADDR'])) {
  23. /* We do not know remote IP */
  24. return false;
  25. }
  26. $direct_ip = $_SERVER['REMOTE_ADDR'];
  27. /* Do we trust this IP as a proxy? If yes we will use it's header. */
  28. if (!isset($GLOBALS['cfg']['TrustedProxies'][$direct_ip])) {
  29. /* Return true IP */
  30. return $direct_ip;
  31. }
  32. $trusted_header_value
  33. = PMA_getenv($GLOBALS['cfg']['TrustedProxies'][$direct_ip]);
  34. $matches = array();
  35. // the $ checks that the header contains only one IP address,
  36. // ?: makes sure the () don't capture
  37. $is_ip = preg_match(
  38. '|^(?:[0-9]{1,3}\.){3,3}[0-9]{1,3}$|',
  39. $trusted_header_value, $matches
  40. );
  41. if ($is_ip && (count($matches) == 1)) {
  42. // True IP behind a proxy
  43. return $matches[0];
  44. }
  45. /* Return true IP */
  46. return $direct_ip;
  47. } // end of the 'PMA_getIp()' function
  48. /**
  49. * Matches for IPv4 or IPv6 addresses
  50. *
  51. * @param string $testRange string of IP range to match
  52. * @param string $ipToTest string of IP to test against range
  53. *
  54. * @return boolean whether the IP mask matches
  55. *
  56. * @access public
  57. */
  58. function PMA_ipMaskTest($testRange, $ipToTest)
  59. {
  60. $result = true;
  61. if (strpos($testRange, ':') > -1 || strpos($ipToTest, ':') > -1) {
  62. // assume IPv6
  63. $result = PMA_ipv6MaskTest($testRange, $ipToTest);
  64. } else {
  65. $result = PMA_ipv4MaskTest($testRange, $ipToTest);
  66. }
  67. return $result;
  68. } // end of the "PMA_ipMaskTest()" function
  69. /**
  70. * Based on IP Pattern Matcher
  71. * Originally by J.Adams <jna@retina.net>
  72. * Found on <http://www.php.net/manual/en/function.ip2long.php>
  73. * Modified for phpMyAdmin
  74. *
  75. * Matches:
  76. * xxx.xxx.xxx.xxx (exact)
  77. * xxx.xxx.xxx.[yyy-zzz] (range)
  78. * xxx.xxx.xxx.xxx/nn (CIDR)
  79. *
  80. * Does not match:
  81. * xxx.xxx.xxx.xx[yyy-zzz] (range, partial octets not supported)
  82. *
  83. * @param string $testRange string of IP range to match
  84. * @param string $ipToTest string of IP to test against range
  85. *
  86. * @return boolean whether the IP mask matches
  87. *
  88. * @access public
  89. */
  90. function PMA_ipv4MaskTest($testRange, $ipToTest)
  91. {
  92. $result = true;
  93. $match = preg_match(
  94. '|([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/([0-9]+)|',
  95. $testRange,
  96. $regs
  97. );
  98. if ($match) {
  99. // performs a mask match
  100. $ipl = ip2long($ipToTest);
  101. $rangel = ip2long(
  102. $regs[1] . '.' . $regs[2] . '.' . $regs[3] . '.' . $regs[4]
  103. );
  104. $maskl = 0;
  105. for ($i = 0; $i < 31; $i++) {
  106. if ($i < $regs[5] - 1) {
  107. $maskl = $maskl + PMA_Util::pow(2, (30 - $i));
  108. } // end if
  109. } // end for
  110. if (($maskl & $rangel) == ($maskl & $ipl)) {
  111. return true;
  112. }
  113. return false;
  114. }
  115. // range based
  116. $maskocts = explode('.', $testRange);
  117. $ipocts = explode('.', $ipToTest);
  118. // perform a range match
  119. for ($i = 0; $i < 4; $i++) {
  120. if (preg_match('|\[([0-9]+)\-([0-9]+)\]|', $maskocts[$i], $regs)) {
  121. if (($ipocts[$i] > $regs[2]) || ($ipocts[$i] < $regs[1])) {
  122. $result = false;
  123. } // end if
  124. } else {
  125. if ($maskocts[$i] <> $ipocts[$i]) {
  126. $result = false;
  127. } // end if
  128. } // end if/else
  129. } //end for
  130. return $result;
  131. } // end of the "PMA_ipv4MaskTest()" function
  132. /**
  133. * IPv6 matcher
  134. * CIDR section taken from http://stackoverflow.com/a/10086404
  135. * Modified for phpMyAdmin
  136. *
  137. * Matches:
  138. * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
  139. * (exact)
  140. * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:[yyyy-zzzz]
  141. * (range, only at end of IP - no subnets)
  142. * xxxx:xxxx:xxxx:xxxx/nn
  143. * (CIDR)
  144. *
  145. * Does not match:
  146. * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xx[yyy-zzz]
  147. * (range, partial octets not supported)
  148. *
  149. * @param string $test_range string of IP range to match
  150. * @param string $ip_to_test string of IP to test against range
  151. *
  152. * @return boolean whether the IP mask matches
  153. *
  154. * @access public
  155. */
  156. function PMA_ipv6MaskTest($test_range, $ip_to_test)
  157. {
  158. $result = true;
  159. // convert to lowercase for easier comparison
  160. $test_range = strtolower($test_range);
  161. $ip_to_test = strtolower($ip_to_test);
  162. $is_cidr = strpos($test_range, '/') > -1;
  163. $is_range = strpos($test_range, '[') > -1;
  164. $is_single = ! $is_cidr && ! $is_range;
  165. $ip_hex = bin2hex(inet_pton($ip_to_test));
  166. if ($is_single) {
  167. $range_hex = bin2hex(inet_pton($test_range));
  168. $result = $ip_hex === $range_hex;
  169. return $result;
  170. }
  171. if ($is_range) {
  172. // what range do we operate on?
  173. $range_match = array();
  174. $match = preg_match(
  175. '/\[([0-9a-f]+)\-([0-9a-f]+)\]/', $test_range, $range_match
  176. );
  177. if ($match) {
  178. $range_start = $range_match[1];
  179. $range_end = $range_match[2];
  180. // get the first and last allowed IPs
  181. $first_ip = str_replace($range_match[0], $range_start, $test_range);
  182. $first_hex = bin2hex(inet_pton($first_ip));
  183. $last_ip = str_replace($range_match[0], $range_end, $test_range);
  184. $last_hex = bin2hex(inet_pton($last_ip));
  185. // check if the IP to test is within the range
  186. $result = ($ip_hex >= $first_hex && $ip_hex <= $last_hex);
  187. }
  188. return $result;
  189. }
  190. if ($is_cidr) {
  191. // Split in address and prefix length
  192. list($first_ip, $subnet) = explode('/', $test_range);
  193. // Parse the address into a binary string
  194. $first_bin = inet_pton($first_ip);
  195. $first_hex = bin2hex($first_bin);
  196. // Overwriting first address string to make sure notation is optimal
  197. $first_ip = inet_ntop($first_bin);
  198. $flexbits = 128 - $subnet;
  199. // Build the hexadecimal string of the last address
  200. $last_hex = $first_hex;
  201. $pos = 31;
  202. while ($flexbits > 0) {
  203. // Get the character at this position
  204. $orig = substr($last_hex, $pos, 1);
  205. // Convert it to an integer
  206. $origval = hexdec($orig);
  207. // OR it with (2^flexbits)-1, with flexbits limited to 4 at a time
  208. $newval = $origval | (pow(2, min(4, $flexbits)) - 1);
  209. // Convert it back to a hexadecimal character
  210. $new = dechex($newval);
  211. // And put that character back in the string
  212. $last_hex = substr_replace($last_hex, $new, $pos, 1);
  213. // We processed one nibble, move to previous position
  214. $flexbits -= 4;
  215. $pos -= 1;
  216. }
  217. // check if the IP to test is within the range
  218. $result = ($ip_hex >= $first_hex && $ip_hex <= $last_hex);
  219. }
  220. return $result;
  221. } // end of the "PMA_ipv6MaskTest()" function
  222. /**
  223. * Runs through IP Allow/Deny rules the use of it below for more information
  224. *
  225. * @param string $type 'allow' | 'deny' type of rule to match
  226. *
  227. * @return bool Matched a rule ?
  228. *
  229. * @access public
  230. *
  231. * @see PMA_getIp()
  232. */
  233. function PMA_allowDeny($type)
  234. {
  235. global $cfg;
  236. // Grabs true IP of the user and returns if it can't be found
  237. $remote_ip = PMA_getIp();
  238. if (empty($remote_ip)) {
  239. return false;
  240. }
  241. // copy username
  242. $username = $cfg['Server']['user'];
  243. // copy rule database
  244. $rules = $cfg['Server']['AllowDeny']['rules'];
  245. // lookup table for some name shortcuts
  246. $shortcuts = array(
  247. 'all' => '0.0.0.0/0',
  248. 'localhost' => '127.0.0.1/8'
  249. );
  250. // Provide some useful shortcuts if server gives us address:
  251. if (PMA_getenv('SERVER_ADDR')) {
  252. $shortcuts['localnetA'] = PMA_getenv('SERVER_ADDR') . '/8';
  253. $shortcuts['localnetB'] = PMA_getenv('SERVER_ADDR') . '/16';
  254. $shortcuts['localnetC'] = PMA_getenv('SERVER_ADDR') . '/24';
  255. }
  256. foreach ($rules as $rule) {
  257. // extract rule data
  258. $rule_data = explode(' ', $rule);
  259. // check for rule type
  260. if ($rule_data[0] != $type) {
  261. continue;
  262. }
  263. // check for username
  264. if (($rule_data[1] != '%') //wildcarded first
  265. && ($rule_data[1] != $username)
  266. ) {
  267. continue;
  268. }
  269. // check if the config file has the full string with an extra
  270. // 'from' in it and if it does, just discard it
  271. if ($rule_data[2] == 'from') {
  272. $rule_data[2] = $rule_data[3];
  273. }
  274. // Handle shortcuts with above array
  275. if (isset($shortcuts[$rule_data[2]])) {
  276. $rule_data[2] = $shortcuts[$rule_data[2]];
  277. }
  278. // Add code for host lookups here
  279. // Excluded for the moment
  280. // Do the actual matching now
  281. if (PMA_ipMaskTest($rule_data[2], $remote_ip)) {
  282. return true;
  283. }
  284. } // end while
  285. return false;
  286. } // end of the "PMA_AllowDeny()" function
  287. ?>