IpAddress.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /**
  38. * @param string $ip IP to normalize
  39. */
  40. public function __construct(
  41. private string $ip,
  42. ) {
  43. }
  44. /**
  45. * Return the given subnet for an IPv6 address (64 first bits)
  46. */
  47. private function getIPv6Subnet(string $ip): string {
  48. if ($ip[0] === '[' && $ip[-1] === ']') { // If IP is with brackets, for example [::1]
  49. $ip = substr($ip, 1, strlen($ip) - 2);
  50. }
  51. $pos = strpos($ip, '%'); // if there is an explicit interface added to the IP, e.g. fe80::ae2d:d1e7:fe1e:9a8d%enp2s0
  52. if ($pos !== false) {
  53. $ip = substr($ip, 0, $pos - 1);
  54. }
  55. $binary = \inet_pton($ip);
  56. $mask = inet_pton('FFFF:FFFF:FFFF:FFFF::');
  57. return inet_ntop($binary & $mask).'/64';
  58. }
  59. /**
  60. * Returns the IPv4 address embedded in an IPv6 if applicable.
  61. * The detected format is "::ffff:x.x.x.x" using the binary form.
  62. *
  63. * @return string|null embedded IPv4 string or null if none was found
  64. */
  65. private function getEmbeddedIpv4(string $ipv6): ?string {
  66. $binary = inet_pton($ipv6);
  67. if (!$binary) {
  68. return null;
  69. }
  70. $mask = inet_pton('::FFFF:FFFF');
  71. if (($binary & ~$mask) !== inet_pton('::FFFF:0.0.0.0')) {
  72. return null;
  73. }
  74. return inet_ntop(substr($binary, -4));
  75. }
  76. /**
  77. * Gets either the /32 (IPv4) or the /64 (IPv6) subnet of an IP address
  78. */
  79. public function getSubnet(): string {
  80. if (filter_var($this->ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  81. return $this->ip.'/32';
  82. }
  83. $ipv4 = $this->getEmbeddedIpv4($this->ip);
  84. if ($ipv4 !== null) {
  85. return $ipv4.'/32';
  86. }
  87. return $this->getIPv6Subnet($this->ip);
  88. }
  89. /**
  90. * Returns the specified IP address
  91. */
  92. public function __toString(): string {
  93. return $this->ip;
  94. }
  95. }