function Net_DNS_RR_MX(&$rro, $data, $offset = '') { $this->name = $rro->name; $this->type = $rro->type; $this->class = $rro->class; $this->ttl = $rro->ttl; $this->rdlength = $rro->rdlength; $this->rdata = $rro->rdata; if ($offset) { if ($this->rdlength > 0) { $a = unpack("@{$offset}/npreference", $data); $offset += 2; $packet = new Net_DNS_Packet(); list($exchange, $offset) = $packet->dn_expand($data, $offset); $this->preference = $a['preference']; $this->exchange = $exchange; } } elseif (is_array($data)) { $this->preference = $data['preference']; $this->exchange = $data['exchange']; } else { preg_match("/([0-9]+)[ \t]+(.+)[ \t]*\$/", $data, $regs); $this->preference = $regs[1]; $this->exchange = preg_replace('/(.*)\\.$/', '\\1', $regs[2]); } }
function Net_DNS_RR_SRV(&$rro, $data, $offset = '') { $this->name = $rro->name; $this->type = $rro->type; $this->class = $rro->class; $this->ttl = $rro->ttl; $this->rdlength = $rro->rdlength; $this->rdata = $rro->rdata; if ($offset) { if ($this->rdlength > 0) { $a = unpack("@{$offset}/npreference/nweight/nport", $data); $offset += 6; $packet = new Net_DNS_Packet(); list($target, $offset) = $packet->dn_expand($data, $offset); $this->preference = $a['preference']; $this->weight = $a['weight']; $this->port = $a['port']; $this->target = $target; } } elseif (is_array($data)) { $this->preference = $data['preference']; $this->weight = $data['weight']; $this->port = $data['port']; $this->target = $data['target']; } else { preg_match("/([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+(.+)[ \t]*\$/", $data, $regs); $this->preference = $regs[1]; $this->weight = $regs[2]; $this->port = $regs[3]; $this->target = preg_replace('/(.*)\\.$/', '\\1', $regs[4]); } }
function Net_DNS_RR_NAPTR(&$rro, $data, $offset = '') { $this->name = $rro->name; $this->type = $rro->type; $this->class = $rro->class; $this->ttl = $rro->ttl; $this->rdlength = $rro->rdlength; $this->rdata = $rro->rdata; if ($offset) { if ($this->rdlength > 0) { $a = unpack("@{$offset}/norder/npreference", $data); $offset += 4; $packet = new Net_DNS_Packet(); list($flags, $offset) = Net_DNS_Packet::label_extract($data, $offset); list($services, $offset) = Net_DNS_Packet::label_extract($data, $offset); list($regex, $offset) = Net_DNS_Packet::label_extract($data, $offset); list($replacement, $offset) = $packet->dn_expand($data, $offset); $this->order = $a['order']; $this->preference = $a['preference']; $this->flags = $flags; $this->services = $services; $this->regex = $regex; $this->replacement = $replacement; } } elseif (is_array($data)) { $this->order = $data['order']; $this->preference = $data['preference']; $this->flags = $data['flags']; $this->services = $data['services']; $this->regex = $data['regex']; $this->replacement = $data['replacement']; } else { $data = str_replace('\\\\', chr(1) . chr(1), $data); /* disguise escaped backslash */ $data = str_replace('\\"', chr(2) . chr(2), $data); /* disguise \" */ preg_match('/([0-9]+)[ \\t]+([0-9]+)[ \\t]+("[^"]*"|[^ \\t]*)[ \\t]+("[^"]*"|[^ \\t]*)[ \\t]+("[^"]*"|[^ \\t]*)[ \\t]+(.*?)[ \\t]*$/', $data, $regs); $this->preference = $regs[1]; $this->weight = $regs[2]; foreach ($regs as $idx => $value) { $value = str_replace(chr(2) . chr(2), '\\"', $value); $value = str_replace(chr(1) . chr(1), '\\\\', $value); $regs[$idx] = stripslashes($value); } $this->flags = $regs[3]; $this->services = $regs[4]; $this->regex = $regs[5]; $this->replacement = $regs[6]; } }
function __construct(AsyncDNS $owner, Net_DNS_Packet $packet, $nextping = NULL) { parent::__construct($owner); $this->packet = $packet; if (!($sock = $this->connect(AsyncDNS::getNameservers()))) { throw new Exception("Nameservers down"); } if (!$sock->send($packet->data())) { throw new Exception("Send error"); } $sock->onRead(array($this, 'read')); $sock->onError(array($this, 'error')); $sock->onPing(array($this, 'ping'), $nextping); $this->sock = $sock; }
function Net_DNS_RR_SRV(&$rro, $data, $offset = '') { $this->name = $rro->name; $this->type = $rro->type; $this->class = $rro->class; $this->ttl = $rro->ttl; $this->rdlength = $rro->rdlength; $this->rdata = $rro->rdata; if ($offset) { if ($this->rdlength > 0) { $a = unpack("@{$offset}/npreference/nweight/nport", $data); $offset += 6; list($target, $offset) = Net_DNS_Packet::dn_expand($data, $offset); $this->preference = $a['preference']; $this->weight = $a['weight']; $this->port = $a['port']; $this->target = $target; } } else { ereg("([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+(.+)[ \t]*\$", $data, $regs); $this->preference = $regs[1]; $this->weight = $regs[2]; $this->port = $regs[3]; $this->target = ereg_replace('(.*)\\.$', '\\1', $regs[4]); } }
function Net_DNS_RR_TXT(&$rro, $data, $offset = '') { $this->name = $rro->name; $this->type = $rro->type; $this->class = $rro->class; $this->ttl = $rro->ttl; $this->rdlength = $rro->rdlength; $this->rdata = $rro->rdata; if ($offset) { if ($this->rdlength > 0) { $maxoffset = $this->rdlength + $offset; while ($maxoffset > $offset) { list($text, $offset) = Net_DNS_Packet::label_extract($data, $offset); $this->text[] = $text; } } } else { $data = str_replace('\\\\', chr(1) . chr(1), $data); /* disguise escaped backslash */ $data = str_replace('\\"', chr(2) . chr(2), $data); /* disguise \" */ ereg('("[^"]*"|[^ \\t]*)[ \\t]*$', $data, $regs); $regs[1] = str_replace(chr(2) . chr(2), '\\"', $regs[1]); $regs[1] = str_replace(chr(1) . chr(1), '\\\\', $regs[1]); $regs[1] = stripslashes($regs[1]); $this->text = $regs[1]; } }
function Net_DNS_RR_SOA(&$rro, $data, $offset = '') { $this->name = $rro->name; $this->type = $rro->type; $this->class = $rro->class; $this->ttl = $rro->ttl; $this->rdlength = $rro->rdlength; $this->rdata = $rro->rdata; if ($offset) { if ($this->rdlength > 0) { list($mname, $offset) = Net_DNS_Packet::dn_expand($data, $offset); list($rname, $offset) = Net_DNS_Packet::dn_expand($data, $offset); $a = unpack("@{$offset}/N5soavals", $data); $this->mname = $mname; $this->rname = $rname; $this->serial = $a['soavals1']; $this->refresh = $a['soavals2']; $this->retry = $a['soavals3']; $this->expire = $a['soavals4']; $this->minimum = $a['soavals5']; } } else { if (ereg("([^ \t]+)[ \t]+([^ \t]+)[ \t]+([0-9]+)[^ \t]+([0-9]+)[^ \t]+([0-9]+)[^ \t]+([0-9]+)[^ \t]*\$", $string, $regs)) { $this->mname = ereg_replace('(.*)\\.$', '\\1', $regs[1]); $this->rname = ereg_replace('(.*)\\.$', '\\1', $regs[2]); $this->serial = $regs[3]; $this->refresh = $regs[4]; $this->retry = $regs[5]; $this->expire = $regs[6]; $this->minimum = $regs[7]; } } }
function Net_DNS_RR_HINFO(&$rro, $data, $offset = '') { $this->name = $rro->name; $this->type = $rro->type; $this->class = $rro->class; $this->ttl = $rro->ttl; $this->rdlength = $rro->rdlength; $this->rdata = $rro->rdata; if ($offset) { if ($this->rdlength > 0) { list($cpu, $offset) = Net_DNS_Packet::label_extract($data, $offset); list($os, $offset) = Net_DNS_Packet::label_extract($data, $offset); $this->cpu = $cpu; $this->os = $os; } } else { $data = str_replace('\\\\', chr(1) . chr(1), $data); /* disguise escaped backslash */ $data = str_replace('\\"', chr(2) . chr(2), $data); /* disguise \" */ ereg('("[^"]*"|[^ \\t]*)[ \\t]+("[^"]*"|[^ \\t]*)[ \\t]*$', $data, $regs); foreach ($regs as $idx => $value) { $value = str_replace(chr(2) . chr(2), '\\"', $value); $value = str_replace(chr(1) . chr(1), '\\\\', $value); $regs[$idx] = stripslashes($value); } $this->cpu = $regs[1]; $this->os = $regs[2]; } }
function Net_DNS_RR_SOA(&$rro, $data, $offset = '') { $this->name = $rro->name; $this->type = $rro->type; $this->class = $rro->class; $this->ttl = $rro->ttl; $this->rdlength = $rro->rdlength; $this->rdata = $rro->rdata; if ($offset) { if ($this->rdlength > 0) { $packet = new Net_DNS_Packet(); list($mname, $offset) = $packet->dn_expand($data, $offset); list($rname, $offset) = $packet->dn_expand($data, $offset); $a = unpack("@{$offset}/N5soavals", $data); $this->mname = $mname; $this->rname = $rname; $this->serial = $a['soavals1']; $this->refresh = $a['soavals2']; $this->retry = $a['soavals3']; $this->expire = $a['soavals4']; $this->minimum = $a['soavals5']; } } elseif (is_array($data)) { $this->mname = $data['mname']; $this->rname = $data['rname']; $this->serial = $data['serial']; $this->refresh = $data['refresh']; $this->retry = $data['retry']; $this->expire = $data['expire']; $this->minimum = $data['minimum']; } else { if (preg_match("/([^ \t]+)[ \t]+([^ \t]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]*\$/", $data, $regs)) { $this->mname = preg_replace('/(.*)\\.$/', '\\1', $regs[1]); $this->rname = preg_replace('/(.*)\\.$/', '\\1', $regs[2]); $this->serial = $regs[3]; $this->refresh = $regs[4]; $this->retry = $regs[5]; $this->expire = $regs[6]; $this->minimum = $regs[7]; } } }
function Net_DNS_RR_CNAME(&$rro, $data, $offset = '') { $this->name = $rro->name; $this->type = $rro->type; $this->class = $rro->class; $this->ttl = $rro->ttl; $this->rdlength = $rro->rdlength; $this->rdata = $rro->rdata; if ($offset) { if ($this->rdlength > 0) { $packet = new Net_DNS_Packet(); list($cname, $offset) = $packet->dn_expand($data, $offset); $this->cname = $cname; } } elseif (is_array($data)) { $this->cname = $data['cname']; } else { $this->cname = preg_replace("/[ \t]+(.+)[\\. \t]*\$/", '\\1', $data); } }
function Net_DNS_RR_RP(&$rro, $data, $offset = '') { $this->name = $rro->name; $this->type = $rro->type; $this->class = $rro->class; $this->ttl = $rro->ttl; $this->rdlength = $rro->rdlength; $this->rdata = $rro->rdata; if ($offset) { if ($this->rdlength > 0) { $packet = new Net_DNS_Packet(); list($this->mboxdname, $offset) = $packet->dn_expand($data, $offset); list($this->txtdname, $offset) = $packet->dn_expand($data, $offset); } } elseif (is_array($data)) { $this->mboxdname = $data['mboxdname']; $this->txtdname = $data['txtdname']; } else { preg_match("/([^ ]+)\\s+([^ ]+)/", $data, $matches); $this->mboxdname = preg_replace('/\\.$/', '', $matches[1]); $this->txtdname = preg_replace('/\\.$/', '', $matches[2]); } }
function Net_DNS_RR_CNAME(&$rro, $data, $offset = '') { $this->name = $rro->name; $this->type = $rro->type; $this->class = $rro->class; $this->ttl = $rro->ttl; $this->rdlength = $rro->rdlength; $this->rdata = $rro->rdata; if ($offset) { if ($this->rdlength > 0) { list($cname, $offset) = Net_DNS_Packet::dn_expand($data, $offset); $this->cname = $cname; } } else { $this->cname = ereg_replace("[ \t]+(.+)[\\. \t]*\$", '\\1', $data); } }
function Net_DNS_RR_MX(&$rro, $data, $offset = '') { $this->name = $rro->name; $this->type = $rro->type; $this->class = $rro->class; $this->ttl = $rro->ttl; $this->rdlength = $rro->rdlength; $this->rdata = $rro->rdata; if ($offset) { if ($this->rdlength > 0) { $a = unpack("@{$offset}/npreference", $data); $offset += 2; list($exchange, $offset) = Net_DNS_Packet::dn_expand($data, $offset); $this->preference = $a['preference']; $this->exchange = $exchange; } } else { ereg("([0-9]+)[ \t]+(.+)[ \t]*\$", $data, $regs); $this->preference = $regs[1]; $this->exchange = ereg_replace('(.*)\\.$', '\\1', $regs[2]); } }
public function get_from_dns() { global $DB, $error_ar, $DNSSERVER; echo "Fetching 'A' records for accounts from our DNS server ...<br>\n"; $start_time = microtime(1); $account_result = $DB->make_select('Accounts', '*', "`Status`!='Deleted'"); $dns_defaults = array('nameservers' => array($DNSSERVER), 'port' => '53', 'retrans' => 5, 'retry' => 3, 'usevc' => 0, 'stayopen' => 1, 'igntc' => 0, 'recurse' => false, 'debug' => false, 'tcp_timeout' => 5); $dns = new Net_DNS_Client($dns_defaults); while ($account = $DB->row($account_result)) { $domain = $account['domain']; $dns_packet = new Net_DNS_Packet(); $dns_packet->buildQuestion($domain . '.', 'A'); $dns_packet->header->rd = false; $dns_ans = $dns->resolver->send($dns_packet); if (empty($dns_ans->answer[0]->address)) { $ip = ''; $error_ar[] = "dns record for account {$domain} not found"; continue; //raw($domain); } else { $ip = $dns_ans->answer[0]->address; //raw("$ip - $domain"); } //if(empty($ip)) { $error_ar[] = "dns record for account $domain not found"; $ip = '0.0.0.0'; } $server = $this->ip2server($ip); if ($server != $account['ServerID'] && $server != 'none') { $error_ar[] = "Server wrong for domain {$domain} , account on server '{$server}', but was at {$account['ServerID']}"; //$DB->make_update("`Accounts` SET `ServerID`='$server' WHERE `AccountID`=$account[AccountID]"); } elseif ($server == 'none') { $error_ar[] = "IP wrong for domain {$domain} , account on server {$account['ServerID']}, but in DNS at {$ip}"; } //echo "$domain \t $ip \t $server \n"; /* $account_servers = array(); $whmaccount_result = $DB->make_select("SELECT * FROM `whmaccts` WHERE `domain`='$domain' AND `username`='$account[domain]'"); while($whmaccount = $DB->row($whmaccount_result)) $account_servers[] = $whmaccount['server']; if(count($account_servers)==0) $error_ar[] = "account $domain , no found on servers"; elseif(count($account_servers)>1) { foreach($account_servers as $account_server) $error_ar[] = "clone of account $domain on server $account_server"; if(!in_array($server,$account_servers)) { $error_ar[] = "ip wrong for domain $domain , account on server $whmaccount[server], but in DNS $server"; $server = $whmaccount['server']; } } */ //$DB->make_update("Accounts SET ServerID='$server' WHERE AccountID=$account[AccountID]"); } echo "Finished Step. Time " . intval((microtime(1) - $start_time) * 1000) . "ms.\n\n"; }
function rr_rdata($packet, $offset) { $rdata = ''; $sigdata = ''; if (strlen($this->key)) { $key = $this->key; $key = ereg_replace(' ', '', $key); $key = base64_decode($key); $newpacket = $packet; $newoffset = $offset; array_pop($newpacket->additional); $newpacket->header->arcount--; $newpacket->compnames = array(); /* * Add the request MAC if present (used to validate responses). */ if (isset($this->request_mac)) { $sigdata .= pack('H*', $this->request_mac); } $sigdata .= $newpacket->data(); /* * Don't compress the record (key) name. */ $tmppacket = new Net_DNS_Packet(); $sigdata .= $tmppacket->dn_comp(strtolower($this->name), 0); $sigdata .= pack('n', Net_DNS::classesbyname(strtoupper($this->class))); $sigdata .= pack('N', $this->ttl); /* * Don't compress the algorithm name. */ $tmppacket->compnames = array(); $sigdata .= $tmppacket->dn_comp(strtolower($this->algorithm), 0); $sigdata .= pack('nN', 0, $this->time_signed); $sigdata .= pack('n', $this->fudge); $sigdata .= pack('nn', $this->error, $this->other_len); if (strlen($this->other_data)) { $sigdata .= pack('nN', 0, $this->other_data); } $this->mac = mhash(MHASH_MD5, $sigdata, $key); $this->mac_size = strlen($this->mac); /* * Don't compress the algorithm name. */ unset($tmppacket); $tmppacket = new Net_DNS_Packet(); $rdata .= $tmppacket->dn_comp(strtolower($this->algorithm), 0); $rdata .= pack('nN', 0, $this->time_signed); $rdata .= pack('nn', $this->fudge, $this->mac_size); $rdata .= $this->mac; $rdata .= pack('nnn', $packet->header->id, $this->error, $this->other_len); if ($this->other_data) { $rdata .= pack('nN', 0, $this->other_data); } } return $rdata; }
/** * DNS packet decompression method * * Expands the domain name stored at a particular location in a DNS * packet. The first argument is a variable containing the packet * data. The second argument is the offset within the packet where * the (possibly) compressed domain name is stored. * * @param string $packet The packet data * @param integer $offset The location offset in the packet of the * label to decompress. * @return array Returns a list of type array($name, $offset) where * $name is the name of the label which was decompressed * and $offset is the offset of the next field in the * packet. Returns array(null, null) on error */ function dn_expand($packet, $offset) { $packetlen = strlen($packet); $name = ''; while (1) { if ($packetlen < $offset + 1) { return array(null, null); } $a = unpack("@{$offset}/Cchar", $packet); $len = (int) $a['char']; if ($len == 0) { $offset++; break; } else { if (($len & 0xc0) == 0xc0) { if ($packetlen < $offset + Net_DNS_Packet::INT16SZ) { return array(null, null); } $ptr = unpack("@{$offset}/ni", $packet); $ptr = $ptr['i']; $ptr = $ptr & 0x3fff; $name2 = Net_DNS_Packet::dn_expand($packet, $ptr); if (is_null($name2[0])) { return array(null, null); } $name .= $name2[0]; $offset += Net_DNS_Packet::INT16SZ; break; } else { $offset++; if ($packetlen < $offset + $len) { return array(null, null); } $elem = substr($packet, $offset, $len); $name .= $elem . '.'; $offset += $len; } } } $name = preg_replace('/\\.$/', '', $name); return array($name, $offset); }
/** * Requests the next RR from a existing transfer started with axfr_start * * @return object Net_DNS_RR Returns a Net_DNS_RR object of the next RR * from a zone transfer. * @see Net_DNS_Resolver::send_tcp() */ function axfr_next() { if (!count($this->_axfr_rr)) { if (!isset($this->_axfr_sock) || !is_resource($this->_axfr_sock)) { $this->errorstring = 'no zone transfer in progress'; return NULL; } $timeout = $this->tcp_timeout; $buf = $this->read_tcp($this->_axfr_sock, 2, $this->debug); if (!strlen($buf)) { $this->errorstring = 'truncated zone transfer'; return NULL; } $len = unpack('n1len', $buf); $len = $len['len']; if (!$len) { $this->errorstring = 'truncated zone transfer'; return NULL; } $buf = $this->read_tcp($this->_axfr_sock, $len, $this->debug); if ($this->debug) { echo ';; received ' . strlen($buf) . "bytes\n"; } if (strlen($buf) != $len) { $this->errorstring = 'expected ' . $len . ' bytes, received ' . strlen($buf); if ($this->debug) { echo ';; ' . $err . "\n"; } return NULL; } $ans = new Net_DNS_Packet($this->debug); if (!$ans->parse($buf)) { if (!$this->errorstring) { $this->errorstring = 'unknown error during packet parsing'; } return NULL; } if ($ans->header->ancount < 1) { $this->errorstring = 'truncated zone transfer'; return NULL; } if ($ans->header->rcode != 'NOERROR') { $this->errorstring = 'errorcode ' . $ans->header->rcode . ' returned'; return NULL; } foreach ($ans->answer as $rr) { if ($rr->type == 'SOA') { if (++$this->_axfr_soa_count < 2) { array_push($this->_axfr_rr, $rr); } } else { array_push($this->_axfr_rr, $rr); } } if ($this->_axfr_soa_count >= 2) { unset($this->_axfr_sock); } } $rr = array_shift($this->_axfr_rr); return $rr; }