IpAddress.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Konrad Bucheli <kb@open.ch>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Citharel <nextcloud@tcit.fr>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OC\Security\Normalizer;
  30. /**
  31. * Class IpAddress is used for normalizing IPv4 and IPv6 addresses in security
  32. * relevant contexts in Nextcloud.
  33. *
  34. * @package OC\Security\Normalizer
  35. */
  36. class IpAddress {
  37. /** @var string */
  38. private $ip;
  39. /**
  40. * @param string $ip IP to normalized
  41. */
  42. public function __construct(string $ip) {
  43. $this->ip = $ip;
  44. }
  45. /**
  46. * Return the given subnet for an IPv4 address and mask bits
  47. *
  48. * @param string $ip
  49. * @param int $maskBits
  50. * @return string
  51. */
  52. private function getIPv4Subnet(string $ip, int $maskBits = 32): string {
  53. $binary = \inet_pton($ip);
  54. for ($i = 32; $i > $maskBits; $i -= 8) {
  55. $j = \intdiv($i, 8) - 1;
  56. $k = \min(8, $i - $maskBits);
  57. $mask = (0xff - ((2 ** $k) - 1));
  58. $int = \unpack('C', $binary[$j]);
  59. $binary[$j] = \pack('C', $int[1] & $mask);
  60. }
  61. return \inet_ntop($binary).'/'.$maskBits;
  62. }
  63. /**
  64. * Return the given subnet for an IPv6 address and mask bits
  65. *
  66. * @param string $ip
  67. * @param int $maskBits
  68. * @return string
  69. */
  70. private function getIPv6Subnet(string $ip, int $maskBits = 48): string {
  71. if ($ip[0] === '[' && $ip[-1] === ']') { // If IP is with brackets, for example [::1]
  72. $ip = substr($ip, 1, strlen($ip) - 2);
  73. }
  74. $pos = strpos($ip, '%'); // if there is an explicit interface added to the IP, e.g. fe80::ae2d:d1e7:fe1e:9a8d%enp2s0
  75. if ($pos !== false) {
  76. $ip = substr($ip, 0, $pos - 1);
  77. }
  78. $binary = \inet_pton($ip);
  79. for ($i = 128; $i > $maskBits; $i -= 8) {
  80. $j = \intdiv($i, 8) - 1;
  81. $k = \min(8, $i - $maskBits);
  82. $mask = (0xff - ((2 ** $k) - 1));
  83. $int = \unpack('C', $binary[$j]);
  84. $binary[$j] = \pack('C', $int[1] & $mask);
  85. }
  86. return \inet_ntop($binary).'/'.$maskBits;
  87. }
  88. /**
  89. * Returns the IPv4 address embedded in an IPv6 if applicable.
  90. * The detected format is "::ffff:x.x.x.x" using the binary form.
  91. *
  92. * @return string|null embedded IPv4 string or null if none was found
  93. */
  94. private function getEmbeddedIpv4(string $ipv6): ?string {
  95. $binary = inet_pton($ipv6);
  96. if (!$binary) {
  97. return null;
  98. }
  99. for ($i = 0; $i <= 9; $i++) {
  100. if (unpack('C', $binary[$i])[1] !== 0) {
  101. return null;
  102. }
  103. }
  104. for ($i = 10; $i <= 11; $i++) {
  105. if (unpack('C', $binary[$i])[1] !== 255) {
  106. return null;
  107. }
  108. }
  109. $binary4 = '';
  110. for ($i = 12; $i < 16; $i++) {
  111. $binary4 .= $binary[$i];
  112. }
  113. return inet_ntop($binary4);
  114. }
  115. /**
  116. * Gets either the /32 (IPv4) or the /64 (IPv6) subnet of an IP address
  117. *
  118. * @return string
  119. */
  120. public function getSubnet(): string {
  121. if (\preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $this->ip)) {
  122. return $this->getIPv4Subnet(
  123. $this->ip,
  124. 32
  125. );
  126. }
  127. $ipv4 = $this->getEmbeddedIpv4($this->ip);
  128. if ($ipv4 !== null) {
  129. return $this->getIPv4Subnet(
  130. $ipv4,
  131. 32
  132. );
  133. }
  134. return $this->getIPv6Subnet(
  135. $this->ip,
  136. 64
  137. );
  138. }
  139. /**
  140. * Returns the specified IP address
  141. *
  142. * @return string
  143. */
  144. public function __toString(): string {
  145. return $this->ip;
  146. }
  147. }