diff --git a/composer.json b/composer.json index b3b623d..d116ed9 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,7 @@ "ext-shmop": "*", "ext-sockets": "*", "ext-posix": "*", - "ext-mbstring": "*" + "ext-mbstring": "*", + "ext-ctype": "*" } } diff --git a/signup/email/Net/DNS2.php b/signup/email/Net/DNS2.php index 378c800..0f4d3fc 100644 --- a/signup/email/Net/DNS2.php +++ b/signup/email/Net/DNS2.php @@ -77,13 +77,6 @@ class Net_DNS2 } } - static public function autoload($name) - { - if (strncmp($name, 'Net_DNS2', 8) == 0) { - include str_replace('_', '/', $name) . '.php'; - } - } - public function setServers($nameservers) { if (is_array($nameservers)) { @@ -175,6 +168,44 @@ class Net_DNS2 return true; } + public static function isIPv4($_address): bool + { + if (extension_loaded('filter')) { + if (!filter_var($_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { + return false; + } + } else { + if (inet_pton($_address) === false) { + return false; + } + + if (preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $_address) == 0) { + return false; + } + } + + return true; + } + + public static function isIPv6($_address): bool + { + if (extension_loaded('filter')) { + if (!filter_var($_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { + return false; + } + } else { + if (inet_pton($_address) === false) { + return false; + } + + if (preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $_address) == 1) { + return false; + } + } + + return true; + } + private function parseOptions($value) { if (!$this->use_resolv_options || (strlen($value) == 0)) { @@ -219,6 +250,62 @@ class Net_DNS2 return true; } + static public function autoload($name) + { + if (strncmp($name, 'Net_DNS2', 8) == 0) { + include str_replace('_', '/', $name) . '.php'; + } + } + + public static function expandUint32($_int) + { + if (($_int < 0) && (PHP_INT_MAX == 2147483647)) { + return sprintf('%u', $_int); + } else { + return $_int; + } + } + + public static function expandIPv6($_address) + { + if (str_contains($_address, '::')) { + + $part = explode('::', $_address); + $part[0] = explode(':', $part[0]); + $part[1] = explode(':', $part[1]); + + $missing = array(); + + $x = (8 - (count($part[0]) + count($part[1]))); + for ($i = 0; $i < $x; $i++) { + + $missing[] = '0000'; + } + + $missing = array_merge($part[0], $missing); + $part = array_merge($missing, $part[1]); + + } else { + $part = explode(':', $_address); + } + + foreach ($part as &$p) { + while (strlen($p) < 4) { + $p = '0' . $p; + } + } + + unset($p); + + $result = implode(':', $part); + + if (strlen($result) == 39) { + return $result; + } else { + return false; + } + } + public function signTSIG( $keyname, $signature = '', $algorithm = Net_DNS2_RR_TSIG::HMAC_MD5 ) @@ -299,93 +386,6 @@ class Net_DNS2 return true; } - public static function expandUint32($_int) - { - if (($_int < 0) && (PHP_INT_MAX == 2147483647)) { - return sprintf('%u', $_int); - } else { - return $_int; - } - } - - public static function isIPv4($_address): bool - { - if (extension_loaded('filter')) { - if (!filter_var($_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { - return false; - } - } else { - if (inet_pton($_address) === false) { - return false; - } - - if (preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $_address) == 0) { - return false; - } - } - - return true; - } - - public static function isIPv6($_address): bool - { - if (extension_loaded('filter')) { - if (!filter_var($_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - return false; - } - } else { - if (inet_pton($_address) === false) { - return false; - } - - if (preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $_address) == 1) { - return false; - } - } - - return true; - } - - public static function expandIPv6($_address) - { - if (str_contains($_address, '::')) { - - $part = explode('::', $_address); - $part[0] = explode(':', $part[0]); - $part[1] = explode(':', $part[1]); - - $missing = array(); - - $x = (8 - (count($part[0]) + count($part[1]))); - for ($i = 0; $i < $x; $i++) { - - $missing[] = '0000'; - } - - $missing = array_merge($part[0], $missing); - $part = array_merge($missing, $part[1]); - - } else { - $part = explode(':', $_address); - } - - foreach ($part as &$p) { - while (strlen($p) < 4) { - $p = '0' . $p; - } - } - - unset($p); - - $result = implode(':', $part); - - if (strlen($result) == 39) { - return $result; - } else { - return false; - } - } - protected function sendPacket(Net_DNS2_Packet $request, $use_tcp) { $data = $request->get(); @@ -960,7 +960,6 @@ class Net_DNS2_Cache } } -?> 65535) { - - Net_DNS2_Lookups::$next_packet_id = 1; - } - - return Net_DNS2_Lookups::$next_packet_id; - } - - public function __toString() - { - $output = ";;\n;; Header:\n"; - - $output .= ";;\t id = " . $this->id . "\n"; - $output .= ";;\t qr = " . $this->qr . "\n"; - $output .= ";;\t opcode = " . $this->opcode . "\n"; - $output .= ";;\t aa = " . $this->aa . "\n"; - $output .= ";;\t tc = " . $this->tc . "\n"; - $output .= ";;\t rd = " . $this->rd . "\n"; - $output .= ";;\t ra = " . $this->ra . "\n"; - $output .= ";;\t z = " . $this->z . "\n"; - $output .= ";;\t ad = " . $this->ad . "\n"; - $output .= ";;\t cd = " . $this->cd . "\n"; - $output .= ";;\t rcode = " . $this->rcode . "\n"; - $output .= ";;\t qdcount = " . $this->qdcount . "\n"; - $output .= ";;\t ancount = " . $this->ancount . "\n"; - $output .= ";;\t nscount = " . $this->nscount . "\n"; - $output .= ";;\t arcount = " . $this->arcount . "\n"; - - return $output; - } - public function set(Net_DNS2_Packet &$packet) { @@ -1122,6 +1088,39 @@ class Net_DNS2_Header return true; } + public function nextPacketId() + { + if (++Net_DNS2_Lookups::$next_packet_id > 65535) { + + Net_DNS2_Lookups::$next_packet_id = 1; + } + + return Net_DNS2_Lookups::$next_packet_id; + } + + public function __toString() + { + $output = ";;\n;; Header:\n"; + + $output .= ";;\t id = " . $this->id . "\n"; + $output .= ";;\t qr = " . $this->qr . "\n"; + $output .= ";;\t opcode = " . $this->opcode . "\n"; + $output .= ";;\t aa = " . $this->aa . "\n"; + $output .= ";;\t tc = " . $this->tc . "\n"; + $output .= ";;\t rd = " . $this->rd . "\n"; + $output .= ";;\t ra = " . $this->ra . "\n"; + $output .= ";;\t z = " . $this->z . "\n"; + $output .= ";;\t ad = " . $this->ad . "\n"; + $output .= ";;\t cd = " . $this->cd . "\n"; + $output .= ";;\t rcode = " . $this->rcode . "\n"; + $output .= ";;\t qdcount = " . $this->qdcount . "\n"; + $output .= ";;\t ancount = " . $this->ancount . "\n"; + $output .= ";;\t nscount = " . $this->nscount . "\n"; + $output .= ";;\t arcount = " . $this->arcount . "\n"; + + return $output; + } + public function get(Net_DNS2_Packet &$packet) { $data = pack('n', $this->id) . @@ -1547,7 +1546,6 @@ class Net_DNS2_Lookups ); } -?>header->__toString(); - - foreach ($this->question as $x) { - - $output .= $x->__toString() . "\n"; - } - foreach ($this->answer as $x) { - - $output .= $x->__toString() . "\n"; - } - foreach ($this->authority as $x) { - - $output .= $x->__toString() . "\n"; - } - foreach ($this->additional as $x) { - - $output .= $x->__toString() . "\n"; - } - - return $output; - } - - public function get() - { - $data = $this->header->get($this); - - foreach ($this->question as $x) { - - $data .= $x->get($this); - } - foreach ($this->answer as $x) { - - $data .= $x->get($this); - } - foreach ($this->authority as $x) { - - $data .= $x->get($this); - } - foreach ($this->additional as $x) { - - $data .= $x->get($this); - } - - return $data; - } - - public function compress($name, &$offset) - { - $names = explode('.', $name); - $compname = ''; - - while (!empty($names)) { - - $dname = join('.', $names); - - if (isset($this->_compressed[$dname])) { - - $compname .= pack('n', 0xc000 | $this->_compressed[$dname]); - $offset += 2; - - break; - } - - $this->_compressed[$dname] = $offset; - $first = array_shift($names); - - $length = strlen($first); - if ($length <= 0) { - continue; - } - - - if ($length > 63) { - - $length = 63; - $first = substr($first, 0, $length); - } - - $compname .= pack('Ca*', $length, $first); - $offset += $length + 1; - } - - if (empty($names)) { - - $compname .= pack('C', 0); - $offset++; - } - - return $compname; - } - public static function pack($name) { $offset = 0; @@ -1761,6 +1666,104 @@ class Net_DNS2_Packet return $name; } + public static function formatIPv6($address) + { + return Net_DNS2::expandIPv6($address); + } + + public function __toString() + { + $output = $this->header->__toString(); + + foreach ($this->question as $x) { + + $output .= $x->__toString() . "\n"; + } + foreach ($this->answer as $x) { + + $output .= $x->__toString() . "\n"; + } + foreach ($this->authority as $x) { + + $output .= $x->__toString() . "\n"; + } + foreach ($this->additional as $x) { + + $output .= $x->__toString() . "\n"; + } + + return $output; + } + + public function get() + { + $data = $this->header->get($this); + + foreach ($this->question as $x) { + + $data .= $x->get($this); + } + foreach ($this->answer as $x) { + + $data .= $x->get($this); + } + foreach ($this->authority as $x) { + + $data .= $x->get($this); + } + foreach ($this->additional as $x) { + + $data .= $x->get($this); + } + + return $data; + } + + public function compress($name, &$offset) + { + $names = explode('.', $name); + $compname = ''; + + while (!empty($names)) { + + $dname = join('.', $names); + + if (isset($this->_compressed[$dname])) { + + $compname .= pack('n', 0xc000 | $this->_compressed[$dname]); + $offset += 2; + + break; + } + + $this->_compressed[$dname] = $offset; + $first = array_shift($names); + + $length = strlen($first); + if ($length <= 0) { + continue; + } + + + if ($length > 63) { + + $length = 63; + $first = substr($first, 0, $length); + } + + $compname .= pack('Ca*', $length, $first); + $offset += $length + 1; + } + + if (empty($names)) { + + $compname .= pack('C', 0); + $offset++; + } + + return $compname; + } + public function copy(Net_DNS2_Packet $packet) { $this->header = $packet->header; @@ -1785,14 +1788,8 @@ class Net_DNS2_Packet return true; } - - public static function formatIPv6($address) - { - return Net_DNS2::expandIPv6($address); - } } -?>qname . '. ' . - $this->qtype . ' ' . $this->qclass . "\n"; - } - public function set(Net_DNS2_Packet &$packet) { @@ -2117,6 +2094,12 @@ class Net_DNS2_Question return true; } + public function __toString() + { + return ";;\n;; Question:\n;;\t " . $this->qname . '. ' . + $this->qtype . ' ' . $this->qclass . "\n"; + } + public function get(Net_DNS2_Packet &$packet) { @@ -2142,7 +2125,6 @@ class Net_DNS2_Question } } -?>name . '. ' . $this->ttl . ' ' . $this->class . - ' ' . $this->type . ' ' . $this->rrToString(); - } - - protected function formatString($string) - { - return '"' . str_replace('"', '\"', trim($string, '"')) . '"'; - } - - protected function buildString(array $chunks) - { - $data = array(); - $c = 0; - $in = false; - - foreach ($chunks as $r) { - - $r = trim($r); - if (strlen($r) == 0) { - continue; - } - - if (($r[0] == '"') - && ($r[strlen($r) - 1] == '"') - && ($r[strlen($r) - 2] != '\\') - ) { - - $data[$c] = $r; - ++$c; - $in = false; - - } else if ($r[0] == '"') { - - $data[$c] = $r; - $in = true; - - } else if (($r[strlen($r) - 1] == '"') - && ($r[strlen($r) - 2] != '\\') - ) { - - $data[$c] .= ' ' . $r; - ++$c; - $in = false; - - } else { - - if ($in) { - $data[$c] .= ' ' . $r; - } else { - $data[$c++] = $r; - } - } - } - - foreach ($data as $index => $string) { - - $data[$index] = str_replace('\"', '"', trim($string, '"')); - } - - return $data; - } - public function set(Net_DNS2_Packet &$packet, array $rr) { $this->name = $rr['name']; @@ -2440,46 +2342,7 @@ abstract class Net_DNS2_RR return $this->rrSet($packet); } - public function get(Net_DNS2_Packet &$packet) - { - $rdata = ''; - - $data = $packet->compress($this->name, $packet->offset); - - if ($this->type == 'OPT') { - - $this->preBuild(); - - $data .= pack( - 'nnN', - Net_DNS2_Lookups::$rr_types_by_name[$this->type], - $this->class, - $this->ttl - ); - } else { - - $data .= pack( - 'nnN', - Net_DNS2_Lookups::$rr_types_by_name[$this->type], - Net_DNS2_Lookups::$classes_by_name[$this->class], - $this->ttl - ); - } - - - $packet->offset += 10; - - - if ($this->rdlength != -1) { - - $rdata = $this->rrGet($packet); - } - - - $data .= pack('n', strlen($rdata)) . $rdata; - - return $data; - } + abstract protected function rrSet(Net_DNS2_Packet &$packet); public static function parse(Net_DNS2_Packet &$packet) { @@ -2542,11 +2405,6 @@ abstract class Net_DNS2_RR return $o; } - public function cleanString($data) - { - return strtolower(rtrim($data, '.')); - } - public static function fromString($line) { if (strlen($line) == 0) { @@ -2647,9 +2505,124 @@ abstract class Net_DNS2_RR return $o; } + + abstract protected function rrFromString(array $rdata); + + public function __toString() + { + return $this->name . '. ' . $this->ttl . ' ' . $this->class . + ' ' . $this->type . ' ' . $this->rrToString(); + } + + abstract protected function rrToString(); + + public function get(Net_DNS2_Packet &$packet) + { + $rdata = ''; + + $data = $packet->compress($this->name, $packet->offset); + + if ($this->type == 'OPT') { + + $this->preBuild(); + + $data .= pack( + 'nnN', + Net_DNS2_Lookups::$rr_types_by_name[$this->type], + $this->class, + $this->ttl + ); + } else { + + $data .= pack( + 'nnN', + Net_DNS2_Lookups::$rr_types_by_name[$this->type], + Net_DNS2_Lookups::$classes_by_name[$this->class], + $this->ttl + ); + } + + + $packet->offset += 10; + + + if ($this->rdlength != -1) { + + $rdata = $this->rrGet($packet); + } + + + $data .= pack('n', strlen($rdata)) . $rdata; + + return $data; + } + + abstract protected function rrGet(Net_DNS2_Packet &$packet); + + public function cleanString($data) + { + return strtolower(rtrim($data, '.')); + } + + protected function formatString($string) + { + return '"' . str_replace('"', '\"', trim($string, '"')) . '"'; + } + + protected function buildString(array $chunks) + { + $data = array(); + $c = 0; + $in = false; + + foreach ($chunks as $r) { + + $r = trim($r); + if (strlen($r) == 0) { + continue; + } + + if (($r[0] == '"') + && ($r[strlen($r) - 1] == '"') + && ($r[strlen($r) - 2] != '\\') + ) { + + $data[$c] = $r; + ++$c; + $in = false; + + } else if ($r[0] == '"') { + + $data[$c] = $r; + $in = true; + + } else if (($r[strlen($r) - 1] == '"') + && ($r[strlen($r) - 2] != '\\') + ) { + + $data[$c] .= ' ' . $r; + ++$c; + $in = false; + + } else { + + if ($in) { + $data[$c] .= ' ' . $r; + } else { + $data[$c++] = $r; + } + } + } + + foreach ($data as $index => $string) { + + $data[$index] = str_replace('\"', '"', trim($string, '"')); + } + + return $data; + } } -?>type = $type; @@ -2687,6 +2657,8 @@ abstract class Net_DNS2_Socket $this->close(); } + abstract public function close(); + public function bindAddress($address, $port = 0) { $this->local_host = $address; @@ -2697,14 +2669,11 @@ abstract class Net_DNS2_Socket abstract public function open(); - abstract public function close(); - abstract public function write($data); abstract public function read(&$size, $max_size); } -?>_packet->header->opcode = Net_DNS2_Lookups::OPCODE_UPDATE; } - private function _checkName($name) - { - if (!preg_match('/' . $this->_packet->question[0]->qname . '$/', $name)) { - - throw new Net_DNS2_Exception( - 'name provided (' . $name . ') does not match zone name (' . - $this->_packet->question[0]->qname . ')', - Net_DNS2_Lookups::E_PACKET_INVALID - ); - } - - return true; - } - public function signature($keyname, $signature) { return $this->signTSIG($keyname, $signature); @@ -2755,6 +2710,20 @@ class Net_DNS2_Updater extends Net_DNS2 return true; } + private function _checkName($name) + { + if (!preg_match('/' . $this->_packet->question[0]->qname . '$/', $name)) { + + throw new Net_DNS2_Exception( + 'name provided (' . $name . ') does not match zone name (' . + $this->_packet->question[0]->qname . ')', + Net_DNS2_Lookups::E_PACKET_INVALID + ); + } + + return true; + } + public function delete(Net_DNS2_RR $rr) { $this->_checkName($rr->name); @@ -2999,7 +2968,6 @@ class Net_DNS2_Updater extends Net_DNS2 } } -?> implode('.', array_reverse($a)), + 2 => implode(':', array_reverse($a)), + default => '', + }; + } + protected function rrSet(Net_DNS2_Packet &$packet) { if ($this->rdlength > 0) { @@ -3795,37 +3784,8 @@ class Net_DNS2_RR_APL extends Net_DNS2_RR return null; } - - private function _trimZeros($family, $address) - { - switch ($family) { - case 1: - $a = array_reverse(explode('.', $address)); - break; - case 2: - $a = array_reverse(explode(':', $address)); - break; - default: - return ''; - } - - foreach ($a as $value) { - - if ($value === '0') { - - array_shift($a); - } - } - - return match ($family) { - 1 => implode('.', array_reverse($a)), - 2 => implode(':', array_reverse($a)), - default => '', - }; - } } -?> 0) ? 'N' : 'S'; + } else { + $hem = ($data > 0) ? 'E' : 'W'; + } + + $data = abs($data); + + $deg = (int)$data; + $min = (int)(($data - $deg) * 60); + $sec = (int)(((($data - $deg) * 60) - $min) * 60); + $msec = round((((((($data - $deg) * 60) - $min) * 60) - $sec) * 1000)); + + return sprintf('%d %02d %02d.%03d %s', $deg, $min, $sec, round($msec), $hem); + } + protected function rrFromString(array $rdata) { @@ -5408,6 +5349,15 @@ class Net_DNS2_RR_LOC extends Net_DNS2_RR return false; } + private function _dms2d($deg, $min, $sec, $hem) + { + $deg = $deg - 0; + $min = $min - 0; + + $sign = ($hem == 'W' || $hem == 'S') ? -1 : 1; + return ((($sec / 60 + $min) / 60) + $deg) * $sign; + } + protected function rrSet(Net_DNS2_Packet &$packet) { if ($this->rdlength > 0) { @@ -5461,6 +5411,14 @@ class Net_DNS2_RR_LOC extends Net_DNS2_RR return false; } + private function _precsizeNtoA($prec) + { + $mantissa = (($prec >> 4) & 0x0f) % 10; + $exponent = (($prec >> 0) & 0x0f) % 10; + + return $mantissa * $this->_powerOfTen[$exponent]; + } + protected function rrGet(Net_DNS2_Packet &$packet) { if ($this->version == 0) { @@ -5497,14 +5455,6 @@ class Net_DNS2_RR_LOC extends Net_DNS2_RR return null; } - private function _precsizeNtoA($prec) - { - $mantissa = (($prec >> 4) & 0x0f) % 10; - $exponent = (($prec >> 0) & 0x0f) % 10; - - return $mantissa * $this->_powerOfTen[$exponent]; - } - private function _precsizeAtoN($prec) { $exponent = 0; @@ -5516,36 +5466,8 @@ class Net_DNS2_RR_LOC extends Net_DNS2_RR return ($prec << 4) | ($exponent & 0x0f); } - - private function _dms2d($deg, $min, $sec, $hem) - { - $deg = $deg - 0; - $min = $min - 0; - - $sign = ($hem == 'W' || $hem == 'S') ? -1 : 1; - return ((($sec / 60 + $min) / 60) + $deg) * $sign; - } - - private function _d2Dms($data, $latlng) - { - if ($latlng == 'LAT') { - $hem = ($data > 0) ? 'N' : 'S'; - } else { - $hem = ($data > 0) ? 'E' : 'W'; - } - - $data = abs($data); - - $deg = (int)$data; - $min = (int)(($data - $deg) * 60); - $sec = (int)(((($data - $deg) * 60) - $min) * 60); - $msec = round((((((($data - $deg) * 60) - $min) * 60) - $sec) * 1000)); - - return sprintf('%d %02d %02d.%03d %s', $deg, $min, $sec, round($msec), $hem); - } } -?> 'Reserved', @@ -7518,7 +7410,6 @@ class Net_DNS2_RR_TKEY extends Net_DNS2_RR } } -?>