IpAllowDeny.php 9.2 KB

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