Merged in feature/280-dev-dev01 (pull request #21)

auto-patch  280-dev-dev01-2024-01-19T16_41_58

* auto-patch  280-dev-dev01-2024-01-19T16_41_58
This commit is contained in:
Tony Volpe
2024-01-19 16:44:43 +00:00
parent 2699b5437a
commit be83910651
2125 changed files with 179300 additions and 35639 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,142 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*/
/**
* Exception handler used by Net_DNS2
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
*
*/
class Net_DNS2_Exception extends Exception
{
private $_request;
private $_response;
/**
* Constructor - overload the constructor so we can pass in the request
* and response object (when it's available)
*
* @param string $message the exception message
* @param int $code the exception code
* @param object $previous the previous Exception object
* @param object $request the Net_DNS2_Packet_Request object for this request
* @param object $response the Net_DNS2_Packet_Response object for this request
*
* @access public
*
*/
public function __construct(
$message = '',
$code = 0,
$previous = null,
Net_DNS2_Packet_Request $request = null,
Net_DNS2_Packet_Response $response = null
) {
//
// store the request/response objects (if passed)
//
$this->_request = $request;
$this->_response = $response;
//
// call the parent constructor
//
// the "previous" argument was added in PHP 5.3.0
//
// https://code.google.com/p/netdns2/issues/detail?id=25
//
if (version_compare(PHP_VERSION, '5.3.0', '>=') == true) {
parent::__construct($message, $code, $previous);
} else {
parent::__construct($message, $code);
}
}
/**
* returns the Net_DNS2_Packet_Request object (if available)
*
* @return Net_DNS2_Packet_Request object
* @access public
* @since function available since release 1.3.1
*
*/
public function getRequest()
{
return $this->_request;
}
/**
* returns the Net_DNS2_Packet_Response object (if available)
*
* @return Net_DNS2_Packet_Response object
* @access public
* @since function available since release 1.3.1
*
*/
public function getResponse()
{
return $this->_response;
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,277 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
*/
/**
* DNS Packet Header class
*
* This class handles parsing and constructing DNS Packet Headers as defined
* by section 4.1.1 of RFC1035.
*
* DNS header format - RFC1035 section 4.1.1
* DNS header format - RFC4035 section 3.2
*
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | ID |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* |QR| Opcode |AA|TC|RD|RA| Z|AD|CD| RCODE |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | QDCOUNT |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | ANCOUNT |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | NSCOUNT |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | ARCOUNT |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
*
*/
class Net_DNS2_Header
{
public $id; // 16 bit - identifier
public $qr; // 1 bit - 0 = query, 1 = response
public $opcode; // 4 bit - op code
public $aa; // 1 bit - Authoritative Answer
public $tc; // 1 bit - TrunCation
public $rd; // 1 bit - Recursion Desired
public $ra; // 1 bit - Recursion Available
public $z; // 1 bit - Reserved
public $ad; // 1 bit - Authentic Data (RFC4035)
public $cd; // 1 bit - Checking Disabled (RFC4035)
public $rcode; // 4 bit - Response code
public $qdcount; // 16 bit - entries in the question section
public $ancount; // 16 bit - resource records in the answer section
public $nscount; // 16 bit - name server rr in the authority records section
public $arcount; // 16 bit - rr's in the additional records section
/**
* Constructor - builds a new Net_DNS2_Header object
*
* @param mixed &$packet either a Net_DNS2_Packet object or null
*
* @throws Net_DNS2_Exception
* @access public
*
*/
public function __construct(Net_DNS2_Packet &$packet = null)
{
if (!is_null($packet)) {
$this->set($packet);
} else {
$this->id = $this->nextPacketId();
$this->qr = Net_DNS2_Lookups::QR_QUERY;
$this->opcode = Net_DNS2_Lookups::OPCODE_QUERY;
$this->aa = 0;
$this->tc = 0;
$this->rd = 1;
$this->ra = 0;
$this->z = 0;
$this->ad = 0;
$this->cd = 0;
$this->rcode = Net_DNS2_Lookups::RCODE_NOERROR;
$this->qdcount = 1;
$this->ancount = 0;
$this->nscount = 0;
$this->arcount = 0;
}
}
/**
* returns the next available packet id
*
* @return integer
* @access public
*
*/
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;
}
/**
* magic __toString() method to return the header as a string
*
* @return string
* @access public
*
*/
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;
}
/**
* constructs a Net_DNS2_Header from a Net_DNS2_Packet object
*
* @param Net_DNS2_Packet &$packet Object
*
* @return boolean
* @throws Net_DNS2_Exception
* @access public
*
*/
public function set(Net_DNS2_Packet &$packet)
{
//
// the header must be at least 12 bytes long.
//
if ($packet->rdlength < Net_DNS2_Lookups::DNS_HEADER_SIZE) {
throw new Net_DNS2_Exception(
'invalid header data provided; to small',
Net_DNS2_Lookups::E_HEADER_INVALID
);
}
$offset = 0;
//
// parse the values
//
$this->id = ord($packet->rdata[$offset]) << 8 |
ord($packet->rdata[++$offset]);
++$offset;
$this->qr = (ord($packet->rdata[$offset]) >> 7) & 0x1;
$this->opcode = (ord($packet->rdata[$offset]) >> 3) & 0xf;
$this->aa = (ord($packet->rdata[$offset]) >> 2) & 0x1;
$this->tc = (ord($packet->rdata[$offset]) >> 1) & 0x1;
$this->rd = ord($packet->rdata[$offset]) & 0x1;
++$offset;
$this->ra = (ord($packet->rdata[$offset]) >> 7) & 0x1;
$this->z = (ord($packet->rdata[$offset]) >> 6) & 0x1;
$this->ad = (ord($packet->rdata[$offset]) >> 5) & 0x1;
$this->cd = (ord($packet->rdata[$offset]) >> 4) & 0x1;
$this->rcode = ord($packet->rdata[$offset]) & 0xf;
$this->qdcount = ord($packet->rdata[++$offset]) << 8 |
ord($packet->rdata[++$offset]);
$this->ancount = ord($packet->rdata[++$offset]) << 8 |
ord($packet->rdata[++$offset]);
$this->nscount = ord($packet->rdata[++$offset]) << 8 |
ord($packet->rdata[++$offset]);
$this->arcount = ord($packet->rdata[++$offset]) << 8 |
ord($packet->rdata[++$offset]);
//
// increment the internal offset
//
$packet->offset += Net_DNS2_Lookups::DNS_HEADER_SIZE;
return true;
}
/**
* returns a binary packed DNS Header
*
* @param Net_DNS2_Packet &$packet Object
*
* @return string
* @access public
*
*/
public function get(Net_DNS2_Packet &$packet)
{
$packet->offset += Net_DNS2_Lookups::DNS_HEADER_SIZE;
return pack('n', $this->id) .
chr(
($this->qr << 7) | ($this->opcode << 3) |
($this->aa << 2) | ($this->tc << 1) | ($this->rd)
) .
chr(
($this->ra << 7) | ($this->ad << 5) | ($this->cd << 4) | $this->rcode
) .
pack('n4', $this->qdcount, $this->ancount, $this->nscount, $this->arcount);
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,578 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
*/
//
// initalize the packet id value
//
Net_DNS2_Lookups::$next_packet_id = mt_rand(0, 65535);
//
// build the reverse lookup tables; this is just so we don't have to
// have duplicate static content laying around.
//
Net_DNS2_Lookups::$rr_types_by_id = array_flip(Net_DNS2_Lookups::$rr_types_by_name);
Net_DNS2_Lookups::$classes_by_id = array_flip(Net_DNS2_Lookups::$classes_by_name);
Net_DNS2_Lookups::$rr_types_class_to_id = array_flip(Net_DNS2_Lookups::$rr_types_id_to_class);
Net_DNS2_Lookups::$algorithm_name_to_id = array_flip(Net_DNS2_Lookups::$algorithm_id_to_name);
Net_DNS2_Lookups::$digest_name_to_id = array_flip(Net_DNS2_Lookups::$digest_id_to_name);
Net_DNS2_Lookups::$rr_qtypes_by_id = array_flip(Net_DNS2_Lookups::$rr_qtypes_by_name);
Net_DNS2_Lookups::$rr_metatypes_by_id = array_flip(Net_DNS2_Lookups::$rr_metatypes_by_name);
Net_DNS2_Lookups::$protocol_by_id = array_flip(Net_DNS2_Lookups::$protocol_by_name);
/**
* This class provides simple lookups used througout the Net_DNS2 code
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
*
*/
class Net_DNS2_Lookups
{
/*
* size (in bytes) of a header in a standard DNS packet
*/
const DNS_HEADER_SIZE = 12;
/*
* max size of a UDP packet
*/
const DNS_MAX_UDP_SIZE = 512;
/*
* Query/Response flag
*/
const QR_QUERY = 0; // RFC 1035
const QR_RESPONSE = 1; // RFC 1035
/*
* DNS Op Codes
*/
const OPCODE_QUERY = 0; // RFC 1035
const OPCODE_IQUERY = 1; // RFC 1035, RFC 3425
const OPCODE_STATUS = 2; // RFC 1035
const OPCODE_NOTIFY = 4; // RFC 1996
const OPCODE_UPDATE = 5; // RFC 2136
/*
* Resource Record Classes
*/
const RR_CLASS_IN = 1; // RFC 1035
const RR_CLASS_CH = 3; // RFC 1035
const RR_CLASS_HS = 4; // RFC 1035
const RR_CLASS_NONE = 254; // RFC 2136
const RR_CLASS_ANY = 255; // RFC 1035
/*
* DNS Response Codes
*/
const RCODE_NOERROR = 0; // RFC 1035
const RCODE_FORMERR = 1; // RFC 1035
const RCODE_SERVFAIL = 2; // RFC 1035
const RCODE_NXDOMAIN = 3; // RFC 1035
const RCODE_NOTIMP = 4; // RFC 1035
const RCODE_REFUSED = 5; // RFC 1035
const RCODE_YXDOMAIN = 6; // RFC 2136
const RCODE_YXRRSET = 7; // RFC 2136
const RCODE_NXRRSET = 8; // RFC 2136
const RCODE_NOTAUTH = 9; // RFC 2136
const RCODE_NOTZONE = 10; // RFC 2136
// 11-15 reserved
const RCODE_BADSIG = 16; // RFC 2845
const RCODE_BADVERS = 16; // RFC 6891
const RCODE_BADKEY = 17; // RFC 2845
const RCODE_BADTIME = 18; // RFC 2845
const RCODE_BADMODE = 19; // RFC 2930
const RCODE_BADNAME = 20; // RFC 2930
const RCODE_BADALG = 21; // RFC 2930
const RCODE_BADTRUNC = 22; // RFC 4635
const RCODE_BADCOOKIE = 23; // RFC 7873
/*
* internal errors codes returned by the exceptions class
*/
const E_NONE = 0;
const E_DNS_FORMERR = self::RCODE_FORMERR;
const E_DNS_SERVFAIL = self::RCODE_SERVFAIL;
const E_DNS_NXDOMAIN = self::RCODE_NXDOMAIN;
const E_DNS_NOTIMP = self::RCODE_NOTIMP;
const E_DNS_REFUSED = self::RCODE_REFUSED;
const E_DNS_YXDOMAIN = self::RCODE_YXDOMAIN;
const E_DNS_YXRRSET = self::RCODE_YXRRSET;
const E_DNS_NXRRSET = self::RCODE_NXRRSET;
const E_DNS_NOTAUTH = self::RCODE_NOTAUTH;
const E_DNS_NOTZONE = self::RCODE_NOTZONE;
// 11-15 reserved
const E_DNS_BADSIG = self::RCODE_BADSIG;
const E_DNS_BADKEY = self::RCODE_BADKEY;
const E_DNS_BADTIME = self::RCODE_BADTIME;
const E_DNS_BADMODE = self::RCODE_BADMODE;
const E_DNS_BADNAME = self::RCODE_BADNAME;
const E_DNS_BADALG = self::RCODE_BADALG;
const E_DNS_BADTRUNC = self::RCODE_BADTRUNC;
const E_DNS_BADCOOKIE = self::RCODE_BADCOOKIE;
// other error conditions
const E_NS_INVALID_FILE = 200;
const E_NS_INVALID_ENTRY = 201;
const E_NS_FAILED = 202;
const E_NS_SOCKET_FAILED = 203;
const E_NS_INVALID_SOCKET = 204;
const E_PACKET_INVALID = 300;
const E_PARSE_ERROR = 301;
const E_HEADER_INVALID = 302;
const E_QUESTION_INVALID = 303;
const E_RR_INVALID = 304;
const E_OPENSSL_ERROR = 400;
const E_OPENSSL_UNAVAIL = 401;
const E_OPENSSL_INV_PKEY = 402;
const E_OPENSSL_INV_ALGO = 403;
const E_CACHE_UNSUPPORTED = 500;
const E_CACHE_SHM_FILE = 501;
const E_CACHE_SHM_UNAVAIL = 502;
/*
* EDNS0 Option Codes (OPT)
*/
// 0 - Reserved
const EDNS0_OPT_LLQ = 1;
const EDNS0_OPT_UL = 2;
const EDNS0_OPT_NSID = 3;
// 4 - Reserved
const EDNS0_OPT_DAU = 5;
const EDNS0_OPT_DHU = 6;
const EDNS0_OPT_N3U = 7;
const EDNS0_OPT_CLIENT_SUBNET = 8;
const EDNS0_OPT_EXPIRE = 9;
const EDNS0_OPT_COOKIE = 10;
const EDNS0_OPT_TCP_KEEPALIVE = 11;
const EDNS0_OPT_PADDING = 12;
const EDNS0_OPT_CHAIN = 13;
/*
* DNSSEC Algorithms
*/
const DNSSEC_ALGORITHM_RES = 0;
const DNSSEC_ALGORITHM_RSAMD5 = 1;
const DNSSEC_ALGORITHM_DH = 2;
const DNSSEC_ALGORITHM_DSA = 3;
const DNSSEC_ALGORITHM_ECC = 4;
const DNSSEC_ALGORITHM_RSASHA1 = 5;
const DNSSEC_ALGORITHM_DSANSEC3SHA1 = 6;
const DSNSEC_ALGORITHM_RSASHA1NSEC3SHA1 = 7;
const DNSSEC_ALGORITHM_RSASHA256 = 8;
const DNSSEC_ALGORITHM_RSASHA512 = 10;
const DNSSEC_ALGORITHM_ECCGOST = 12;
const DNSSEC_ALGORITHM_INDIRECT = 252;
const DNSSEC_ALGORITHM_PRIVATEDNS = 253;
const DNSSEC_ALGORITHM_PRIVATEOID = 254;
/*
* DNSSEC Digest Types
*/
const DNSSEC_DIGEST_RES = 0;
const DNSSEC_DIGEST_SHA1 = 1;
/*
* The packet id used when sending requests
*/
public static $next_packet_id;
/*
* Used to map resource record types to their id's, and back
*/
public static $rr_types_by_id = array();
public static $rr_types_by_name = array(
'SIG0' => 0, // RFC 2931 pseudo type
'A' => 1, // RFC 1035
'NS' => 2, // RFC 1035
'MD' => 3, // RFC 1035 - obsolete, Not implemented
'MF' => 4, // RFC 1035 - obsolete, Not implemented
'CNAME' => 5, // RFC 1035
'SOA' => 6, // RFC 1035
'MB' => 7, // RFC 1035 - obsolete, Not implemented
'MG' => 8, // RFC 1035 - obsolete, Not implemented
'MR' => 9, // RFC 1035 - obsolete, Not implemented
'NULL' => 10, // RFC 1035 - obsolete, Not implemented
'WKS' => 11, // RFC 1035
'PTR' => 12, // RFC 1035
'HINFO' => 13, // RFC 1035
'MINFO' => 14, // RFC 1035 - obsolete, Not implemented
'MX' => 15, // RFC 1035
'TXT' => 16, // RFC 1035
'RP' => 17, // RFC 1183
'AFSDB' => 18, // RFC 1183
'X25' => 19, // RFC 1183
'ISDN' => 20, // RFC 1183
'RT' => 21, // RFC 1183
'NSAP' => 22, // RFC 1706
'NSAP_PTR' => 23, // RFC 1348 - obsolete, Not implemented
'SIG' => 24, // RFC 2535
'KEY' => 25, // RFC 2535, RFC 2930
'PX' => 26, // RFC 2163
'GPOS' => 27, // RFC 1712 - Not implemented
'AAAA' => 28, // RFC 3596
'LOC' => 29, // RFC 1876
'NXT' => 30, // RFC 2065, obsoleted by by RFC 3755
'EID' => 31, // [Patton][Patton1995]
'NIMLOC' => 32, // [Patton][Patton1995]
'SRV' => 33, // RFC 2782
'ATMA' => 34, // Windows only
'NAPTR' => 35, // RFC 2915
'KX' => 36, // RFC 2230
'CERT' => 37, // RFC 4398
'A6' => 38, // downgraded to experimental by RFC 3363
'DNAME' => 39, // RFC 2672
'SINK' => 40, // Not implemented
'OPT' => 41, // RFC 2671
'APL' => 42, // RFC 3123
'DS' => 43, // RFC 4034
'SSHFP' => 44, // RFC 4255
'IPSECKEY' => 45, // RFC 4025
'RRSIG' => 46, // RFC 4034
'NSEC' => 47, // RFC 4034
'DNSKEY' => 48, // RFC 4034
'DHCID' => 49, // RFC 4701
'NSEC3' => 50, // RFC 5155
'NSEC3PARAM' => 51, // RFC 5155
'TLSA' => 52, // RFC 6698
'SMIMEA' => 53, // draft-ietf-dane-smime-10
// 54 unassigned
'HIP' => 55, // RFC 5205
'NINFO' => 56, // Not implemented
'RKEY' => 57, // Not implemented
'TALINK' => 58, //
'CDS' => 59, // RFC 7344
'CDNSKEY' => 60, // RFC 7344
'OPENPGPKEY' => 61, // RFC 7929
'CSYNC' => 62, // RFC 7477
// 63 - 98 unassigned
'SPF' => 99, // RFC 4408
'UINFO' => 100, // no RFC, Not implemented
'UID' => 101, // no RFC, Not implemented
'GID' => 102, // no RFC, Not implemented
'UNSPEC' => 103, // no RFC, Not implemented
'NID' => 104, // RFC 6742
'L32' => 105, // RFC 6742
'L64' => 106, // RFC 6742
'LP' => 107, // RFC 6742
'EUI48' => 108, // RFC 7043
'EUI64' => 109, // RFC 7043
// 110 - 248 unassigned
'TKEY' => 249, // RFC 2930
'TSIG' => 250, // RFC 2845
'IXFR' => 251, // RFC 1995 - only a full (AXFR) is supported
'AXFR' => 252, // RFC 1035
'MAILB' => 253, // RFC 883, Not implemented
'MAILA' => 254, // RFC 973, Not implemented
'ANY' => 255, // RFC 1035 - we support both 'ANY' and '*'
'URI' => 256, // tools.ietf.org/html/draft-faltstrom-uri-06
'CAA' => 257, // tools.ietf.org/html/draft-ietf-pkix-caa-03
'AVC' => 258, // Application Visibility and Control
// 259 - 32767 unassigned
'TA' => 32768, // same as DS
'DLV' => 32769 // RFC 4431
);
/*
* Qtypes and Metatypes - defined in RFC2929 section 3.1
*/
public static $rr_qtypes_by_id = array();
public static $rr_qtypes_by_name = array(
'IXFR' => 251, // RFC 1995 - only a full (AXFR) is supported
'AXFR' => 252, // RFC 1035
'MAILB' => 253, // RFC 883, Not implemented
'MAILA' => 254, // RFC 973, Not implemented
'ANY' => 255 // RFC 1035 - we support both 'ANY' and '*'
);
public static $rr_metatypes_by_id = array();
public static $rr_metatypes_by_name = array(
'OPT' => 41, // RFC 2671
'TKEY' => 249, // RFC 2930
'TSIG' => 250 // RFC 2845
);
/*
* used to map resource record id's to RR class names
*/
public static $rr_types_class_to_id = array();
public static $rr_types_id_to_class = array(
1 => 'Net_DNS2_RR_A',
2 => 'Net_DNS2_RR_NS',
5 => 'Net_DNS2_RR_CNAME',
6 => 'Net_DNS2_RR_SOA',
11 => 'Net_DNS2_RR_WKS',
12 => 'Net_DNS2_RR_PTR',
13 => 'Net_DNS2_RR_HINFO',
15 => 'Net_DNS2_RR_MX',
16 => 'Net_DNS2_RR_TXT',
17 => 'Net_DNS2_RR_RP',
18 => 'Net_DNS2_RR_AFSDB',
19 => 'Net_DNS2_RR_X25',
20 => 'Net_DNS2_RR_ISDN',
21 => 'Net_DNS2_RR_RT',
22 => 'Net_DNS2_RR_NSAP',
24 => 'Net_DNS2_RR_SIG',
25 => 'Net_DNS2_RR_KEY',
26 => 'Net_DNS2_RR_PX',
28 => 'Net_DNS2_RR_AAAA',
29 => 'Net_DNS2_RR_LOC',
31 => 'Net_DNS2_RR_EID',
32 => 'Net_DNS2_RR_NIMLOC',
33 => 'Net_DNS2_RR_SRV',
34 => 'Net_DNS2_RR_ATMA',
35 => 'Net_DNS2_RR_NAPTR',
36 => 'Net_DNS2_RR_KX',
37 => 'Net_DNS2_RR_CERT',
39 => 'Net_DNS2_RR_DNAME',
41 => 'Net_DNS2_RR_OPT',
42 => 'Net_DNS2_RR_APL',
43 => 'Net_DNS2_RR_DS',
44 => 'Net_DNS2_RR_SSHFP',
45 => 'Net_DNS2_RR_IPSECKEY',
46 => 'Net_DNS2_RR_RRSIG',
47 => 'Net_DNS2_RR_NSEC',
48 => 'Net_DNS2_RR_DNSKEY',
49 => 'Net_DNS2_RR_DHCID',
50 => 'Net_DNS2_RR_NSEC3',
51 => 'Net_DNS2_RR_NSEC3PARAM',
52 => 'Net_DNS2_RR_TLSA',
53 => 'Net_DNS2_RR_SMIMEA',
55 => 'Net_DNS2_RR_HIP',
58 => 'Net_DNS2_RR_TALINK',
59 => 'Net_DNS2_RR_CDS',
60 => 'Net_DNS2_RR_CDNSKEY',
61 => 'Net_DNS2_RR_OPENPGPKEY',
62 => 'Net_DNS2_RR_CSYNC',
99 => 'Net_DNS2_RR_SPF',
104 => 'Net_DNS2_RR_NID',
105 => 'Net_DNS2_RR_L32',
106 => 'Net_DNS2_RR_L64',
107 => 'Net_DNS2_RR_LP',
108 => 'Net_DNS2_RR_EUI48',
109 => 'Net_DNS2_RR_EUI64',
249 => 'Net_DNS2_RR_TKEY',
250 => 'Net_DNS2_RR_TSIG',
// 251 - IXFR - handled as a full zone transfer (252)
// 252 - AXFR - handled as a function call
255 => 'Net_DNS2_RR_ANY',
256 => 'Net_DNS2_RR_URI',
257 => 'Net_DNS2_RR_CAA',
258 => 'Net_DNS2_RR_AVC',
32768 => 'Net_DNS2_RR_TA',
32769 => 'Net_DNS2_RR_DLV'
);
/*
* used to map resource record class names to their id's, and back
*/
public static $classes_by_id = array();
public static $classes_by_name = array(
'IN' => self::RR_CLASS_IN, // RFC 1035
'CH' => self::RR_CLASS_CH, // RFC 1035
'HS' => self::RR_CLASS_HS, // RFC 1035
'NONE' => self::RR_CLASS_NONE, // RFC 2136
'ANY' => self::RR_CLASS_ANY // RFC 1035
);
/*
* maps response codes to error messages
*/
public static $result_code_messages = array(
self::RCODE_NOERROR => 'The request completed successfully.',
self::RCODE_FORMERR => 'The name server was unable to interpret the query.',
self::RCODE_SERVFAIL => 'The name server was unable to process this query due to a problem with the name server.',
self::RCODE_NXDOMAIN => 'The domain name referenced in the query does not exist.',
self::RCODE_NOTIMP => 'The name server does not support the requested kind of query.',
self::RCODE_REFUSED => 'The name server refuses to perform the specified operation for policy reasons.',
self::RCODE_YXDOMAIN => 'Name Exists when it should not.',
self::RCODE_YXRRSET => 'RR Set Exists when it should not.',
self::RCODE_NXRRSET => 'RR Set that should exist does not.',
self::RCODE_NOTAUTH => 'Server Not Authoritative for zone.',
self::RCODE_NOTZONE => 'Name not contained in zone.',
self::RCODE_BADSIG => 'TSIG Signature Failure.',
self::RCODE_BADKEY => 'Key not recognized.',
self::RCODE_BADTIME => 'Signature out of time window.',
self::RCODE_BADMODE => 'Bad TKEY Mode.',
self::RCODE_BADNAME => 'Duplicate key name.',
self::RCODE_BADALG => 'Algorithm not supported.',
self::RCODE_BADTRUNC => 'Bad truncation.'
);
/*
* maps DNS SEC alrorithms to their mnemonics
*/
public static $algorithm_name_to_id = array();
public static $algorithm_id_to_name = array(
self::DNSSEC_ALGORITHM_RES => 'RES',
self::DNSSEC_ALGORITHM_RSAMD5 => 'RSAMD5',
self::DNSSEC_ALGORITHM_DH => 'DH',
self::DNSSEC_ALGORITHM_DSA => 'DSA',
self::DNSSEC_ALGORITHM_ECC => 'ECC',
self::DNSSEC_ALGORITHM_RSASHA1 => 'RSASHA1',
self::DNSSEC_ALGORITHM_DSANSEC3SHA1 => 'DSA-NSEC3-SHA1',
self::DSNSEC_ALGORITHM_RSASHA1NSEC3SHA1 => 'RSASHA1-NSEC3-SHA1',
self::DNSSEC_ALGORITHM_RSASHA256 => 'RSASHA256',
self::DNSSEC_ALGORITHM_RSASHA512 => 'RSASHA512',
self::DNSSEC_ALGORITHM_ECCGOST => 'ECC-GOST',
self::DNSSEC_ALGORITHM_INDIRECT => 'INDIRECT',
self::DNSSEC_ALGORITHM_PRIVATEDNS => 'PRIVATEDNS',
self::DNSSEC_ALGORITHM_PRIVATEOID => 'PRIVATEOID'
);
/*
* maps DNSSEC digest types to their mnemonics
*/
public static $digest_name_to_id = array();
public static $digest_id_to_name = array(
self::DNSSEC_DIGEST_RES => 'RES',
self::DNSSEC_DIGEST_SHA1 => 'SHA-1'
);
/*
* Protocols names - RFC 1010
*/
public static $protocol_by_id = array();
public static $protocol_by_name = array(
'ICMP' => 1,
'IGMP' => 2,
'GGP' => 3,
'ST' => 5,
'TCP' => 6,
'UCL' => 7,
'EGP' => 8,
'IGP' => 9,
'BBN-RCC-MON' => 10,
'NVP-II' => 11,
'PUP' => 12,
'ARGUS' => 13,
'EMCON' => 14,
'XNET' => 15,
'CHAOS' => 16,
'UDP' => 17,
'MUX' => 18,
'DCN-MEAS' => 19,
'HMP' => 20,
'PRM' => 21,
'XNS-IDP' => 22,
'TRUNK-1' => 23,
'TRUNK-2' => 24,
'LEAF-1' => 25,
'LEAF-2' => 26,
'RDP' => 27,
'IRTP' => 28,
'ISO-TP4' => 29,
'NETBLT' => 30,
'MFE-NSP' => 31,
'MERIT-INP' => 32,
'SEP' => 33,
// 34 - 60 - Unassigned
// 61 - any host internal protocol
'CFTP' => 62,
// 63 - any local network
'SAT-EXPAK' => 64,
'MIT-SUBNET' => 65,
'RVD' => 66,
'IPPC' => 67,
// 68 - any distributed file system
'SAT-MON' => 69,
// 70 - Unassigned
'IPCV' => 71,
// 72 - 75 - Unassigned
'BR-SAT-MON' => 76,
// 77 - Unassigned
'WB-MON' => 78,
'WB-EXPAK' => 79
// 80 - 254 - Unassigned
// 255 - Reserved
);
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,434 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
* This file contains code based off the Net::DNS Perl module by
* Michael Fuhr.
*
* This is the copyright notice from the PERL Net::DNS module:
*
* Copyright (c) 1997-2000 Michael Fuhr. All rights reserved. This
* program is free software; you can redistribute it and/or modify it
* under the same terms as Perl itself.
*
*/
/**
* This is the base class that holds a standard DNS packet.
*
* The Net_DNS2_Packet_Request and Net_DNS2_Packet_Response classes extend this
* class.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
* @see Net_DNS2_Packet_Request, Net_DNS2_Packet_Response
*
*/
class Net_DNS2_Packet
{
/*
* the full binary data and length for this packet
*/
public $rdata;
public $rdlength;
/*
* the offset pointer used when building/parsing packets
*/
public $offset = 0;
/*
* Net_DNS2_Header object with the DNS packet header
*/
public $header;
/*
* array of Net_DNS2_Question objects
*
* used as "zone" for updates per RFC2136
*
*/
public $question = array();
/*
* array of Net_DNS2_RR Objects for Answers
*
* used as "prerequisite" for updates per RFC2136
*
*/
public $answer = array();
/*
* array of Net_DNS2_RR Objects for Authority
*
* used as "update" for updates per RFC2136
*
*/
public $authority = array();
/*
* array of Net_DNS2_RR Objects for Addtitional
*/
public $additional = array();
/*
* array of compressed labeles
*/
private $_compressed = array();
/**
* magic __toString() method to return the Net_DNS2_Packet as a string
*
* @return string
* @access public
*
*/
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;
}
/**
* returns a full binary DNS packet
*
* @return string
* @throws Net_DNS2_Exception
* @access public
*
*/
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;
}
/**
* applies a standard DNS name compression on the given name/offset
*
* This logic was based on the Net::DNS::Packet::dn_comp() function
* by Michanel Fuhr
*
* @param string $name the name to be compressed
* @param integer &$offset the offset into the given packet object
*
* @return string
* @access public
*
*/
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;
}
//
// truncate see RFC1035 2.3.1
//
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;
}
/**
* applies a standard DNS name compression on the given name/offset
*
* This logic was based on the Net::DNS::Packet::dn_comp() function
* by Michanel Fuhr
*
* @param string $name the name to be compressed
*
* @return string
* @access public
*
*/
public static function pack($name)
{
$offset = 0;
$names = explode('.', $name);
$compname = '';
while (!empty($names)) {
$first = array_shift($names);
$length = strlen($first);
$compname .= pack('Ca*', $length, $first);
$offset += $length + 1;
}
$compname .= "\0";
$offset++;
return $compname;
}
/**
* expands the domain name stored at a given offset in a DNS Packet
*
* This logic was based on the Net::DNS::Packet::dn_expand() function
* by Michanel Fuhr
*
* @param Net_DNS2_Packet &$packet the DNS packet to look in for the domain name
* @param integer &$offset the offset into the given packet object
*
* @return mixed either the domain name or null if it's not found.
* @access public
*
*/
public static function expand(Net_DNS2_Packet &$packet, &$offset)
{
$name = '';
while (1) {
if ($packet->rdlength < ($offset + 1)) {
return null;
}
$xlen = ord($packet->rdata[$offset]);
if ($xlen == 0) {
++$offset;
break;
} else if (($xlen & 0xc0) == 0xc0) {
if ($packet->rdlength < ($offset + 2)) {
return null;
}
$ptr = ord($packet->rdata[$offset]) << 8 |
ord($packet->rdata[$offset+1]);
$ptr = $ptr & 0x3fff;
$name2 = Net_DNS2_Packet::expand($packet, $ptr);
if (is_null($name2)) {
return null;
}
$name .= $name2;
$offset += 2;
break;
} else {
++$offset;
if ($packet->rdlength < ($offset + $xlen)) {
return null;
}
$elem = '';
$elem = substr($packet->rdata, $offset, $xlen);
$name .= $elem . '.';
$offset += $xlen;
}
}
return trim($name, '.');
}
/**
* parses a domain label from a DNS Packet at the given offset
*
* @param Net_DNS2_Packet &$packet the DNS packet to look in for the domain name
* @param integer &$offset the offset into the given packet object
*
* @return mixed either the domain name or null if it's not found.
* @access public
*
*/
public static function label(Net_DNS2_Packet &$packet, &$offset)
{
$name = '';
if ($packet->rdlength < ($offset + 1)) {
return null;
}
$xlen = ord($packet->rdata[$offset]);
++$offset;
if (($xlen + $offset) > $packet->rdlength) {
$name = substr($packet->rdata, $offset);
$offset = $packet->rdlength;
} else {
$name = substr($packet->rdata, $offset, $xlen);
$offset += $xlen;
}
return $name;
}
/**
* copies the contents of the given packet, to the local packet object. this
* function intentionally ignores some of the packet data.
*
* @param Net_DNS2_Packet $packet the DNS packet to copy the data from
*
* @return boolean
* @access public
*
*/
public function copy(Net_DNS2_Packet $packet)
{
$this->header = $packet->header;
$this->question = $packet->question;
$this->answer = $packet->answer;
$this->authority = $packet->authority;
$this->additional = $packet->additional;
return true;
}
/**
* resets the values in the current packet object
*
* @return boolean
* @access public
*
*/
public function reset()
{
$this->header->id = $this->header->nextPacketId();
$this->rdata = '';
$this->rdlength = 0;
$this->offset = 0;
$this->answer = array();
$this->authority = array();
$this->additional = array();
$this->_compressed = array();
return true;
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,217 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
*/
/**
* This class handles building new DNS request packets; packets used for DNS
* queries and updates.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
* @see Net_DNS2_Packet
*
*/
class Net_DNS2_Packet_Request extends Net_DNS2_Packet
{
/**
* Constructor - builds a new Net_DNS2_Packet_Request object
*
* @param string $name the domain name for the packet
* @param string $type the DNS RR type for the packet
* @param string $class the DNS class for the packet
*
* @throws Net_DNS2_Exception
* @access public
*
*/
public function __construct($name, $type = null, $class = null)
{
$this->set($name, $type, $class);
}
/**
* builds a new Net_DNS2_Packet_Request object
*
* @param string $name the domain name for the packet
* @param string $type the DNS RR type for the packet
* @param string $class the DNS class for the packet
*
* @return boolean
* @throws Net_DNS2_Exception
* @access public
*
*/
public function set($name, $type = 'A', $class = 'IN')
{
//
// generate a new header
//
$this->header = new Net_DNS2_Header;
//
// add a new question
//
$q = new Net_DNS2_Question();
//
// allow queries directly to . for the root name servers
//
if ($name != '.') {
$name = trim(strtolower($name), " \t\n\r\0\x0B.");
}
$type = strtoupper(trim($type));
$class = strtoupper(trim($class));
//
// check that the input string has some data in it
//
if (empty($name)) {
throw new Net_DNS2_Exception(
'empty query string provided',
Net_DNS2_Lookups::E_PACKET_INVALID
);
}
//
// if the type is "*", rename it to "ANY"- both are acceptable.
//
if ($type == '*') {
$type = 'ANY';
}
//
// check that the type and class are valid
//
if ( (!isset(Net_DNS2_Lookups::$rr_types_by_name[$type]))
|| (!isset(Net_DNS2_Lookups::$classes_by_name[$class]))
) {
throw new Net_DNS2_Exception(
'invalid type (' . $type . ') or class (' . $class . ') specified.',
Net_DNS2_Lookups::E_PACKET_INVALID
);
}
if ($type == 'PTR') {
//
// if it's a PTR request for an IP address, then make sure we tack on
// the arpa domain.
//
// there are other types of PTR requests, so if an IP adress doesn't match,
// then just let it flow through and assume it's a hostname
//
if (Net_DNS2::isIPv4($name) == true) {
//
// IPv4
//
$name = implode('.', array_reverse(explode('.', $name)));
$name .= '.in-addr.arpa';
} else if (Net_DNS2::isIPv6($name) == true) {
//
// IPv6
//
$e = Net_DNS2::expandIPv6($name);
if ($e !== false) {
$name = implode(
'.', array_reverse(str_split(str_replace(':', '', $e)))
);
$name .= '.ip6.arpa';
} else {
throw new Net_DNS2_Exception(
'unsupported PTR value: ' . $name,
Net_DNS2_Lookups::E_PACKET_INVALID
);
}
}
}
//
// store the data
//
$q->qname = $name;
$q->qtype = $type;
$q->qclass = $class;
$this->question[] = $q;
//
// the answer, authority and additional are empty; they can be modified
// after the request is created for UPDATE requests if needed.
//
$this->answer = array();
$this->authority = array();
$this->additional = array();
return true;
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,194 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
*/
/**
* This class handles building new DNS response packets; it parses binary packed
* packets that come off the wire
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
* @see Net_DNS2_Packet
*
*/
class Net_DNS2_Packet_Response extends Net_DNS2_Packet
{
/*
* The name servers that this response came from
*/
public $answer_from;
/*
* The socket type the answer came from (TCP/UDP)
*/
public $answer_socket_type;
/*
* The query response time in microseconds
*/
public $response_time = 0;
/**
* Constructor - builds a new Net_DNS2_Packet_Response object
*
* @param string $data binary DNS packet
* @param integer $size the length of the DNS packet
*
* @throws Net_DNS2_Exception
* @access public
*
*/
public function __construct($data, $size)
{
$this->set($data, $size);
}
/**
* builds a new Net_DNS2_Packet_Response object
*
* @param string $data binary DNS packet
* @param integer $size the length of the DNS packet
*
* @return boolean
* @throws Net_DNS2_Exception
* @access public
*
*/
public function set($data, $size)
{
//
// store the full packet
//
$this->rdata = $data;
$this->rdlength = $size;
//
// parse the header
//
// we don't bother checking the size earlier, because the first thing the
// header class does, is check the size and throw and exception if it's
// invalid.
//
$this->header = new Net_DNS2_Header($this);
//
// if the truncation bit is set, then just return right here, because the
// rest of the packet is probably empty; and there's no point in processing
// anything else.
//
// we also don't need to worry about checking to see if the the header is
// null or not, since the Net_DNS2_Header() constructor will throw an
// exception if the packet is invalid.
//
if ($this->header->tc == 1) {
return false;
}
//
// parse the questions
//
for ($x = 0; $x < $this->header->qdcount; ++$x) {
$this->question[$x] = new Net_DNS2_Question($this);
}
//
// parse the answers
//
for ($x = 0; $x < $this->header->ancount; ++$x) {
$o = Net_DNS2_RR::parse($this);
if (!is_null($o)) {
$this->answer[] = $o;
}
}
//
// parse the authority section
//
for ($x = 0; $x < $this->header->nscount; ++$x) {
$o = Net_DNS2_RR::parse($this);
if (!is_null($o)) {
$this->authority[] = $o;
}
}
//
// parse the additional section
//
for ($x = 0; $x < $this->header->arcount; ++$x) {
$o = Net_DNS2_RR::parse($this);
if (!is_null($o)) {
$this->additional[] = $o;
}
}
return true;
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,244 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
*/
/**
* This class handles parsing and constructing the question sectino of DNS
* packets.
*
* This is referred to as the "zone" for update per RFC2136
*
* DNS question format - RFC1035 section 4.1.2
*
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | |
* / QNAME /
* / /
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | QTYPE |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | QCLASS |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
* @see Net_DNS2_Packet
*
*/
class Net_DNS2_Question
{
/*
* The name of the question
*
* referred to as "zname" for updates per RFC2136
*
*/
public $qname;
/*
* The RR type for the questino
*
* referred to as "ztype" for updates per RFC2136
*
*/
public $qtype;
/*
* The RR class for the questino
*
* referred to as "zclass" for updates per RFC2136
*
*/
public $qclass;
/**
* Constructor - builds a new Net_DNS2_Question object
*
* @param mixed &$packet either a Net_DNS2_Packet object, or null to
* build an empty object
*
* @throws Net_DNS2_Exception
* @access public
*
*/
public function __construct(Net_DNS2_Packet &$packet = null)
{
if (!is_null($packet)) {
$this->set($packet);
} else {
$this->qname = '';
$this->qtype = 'A';
$this->qclass = 'IN';
}
}
/**
* magic __toString() function to return the Net_DNS2_Question object as a string
*
* @return string
* @access public
*
*/
public function __toString()
{
return ";;\n;; Question:\n;;\t " . $this->qname . '. ' .
$this->qtype . ' ' . $this->qclass . "\n";
}
/**
* builds a new Net_DNS2_Header object from a Net_DNS2_Packet object
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet object
*
* @return boolean
* @throws Net_DNS2_Exception
* @access public
*
*/
public function set(Net_DNS2_Packet &$packet)
{
//
// expand the name
//
$this->qname = $packet->expand($packet, $packet->offset);
if ($packet->rdlength < ($packet->offset + 4)) {
throw new Net_DNS2_Exception(
'invalid question section: to small',
Net_DNS2_Lookups::E_QUESTION_INVALID
);
}
//
// unpack the type and class
//
$type = ord($packet->rdata[$packet->offset++]) << 8 |
ord($packet->rdata[$packet->offset++]);
$class = ord($packet->rdata[$packet->offset++]) << 8 |
ord($packet->rdata[$packet->offset++]);
//
// validate it
//
$type_name = Net_DNS2_Lookups::$rr_types_by_id[$type];
$class_name = Net_DNS2_Lookups::$classes_by_id[$class];
if ( (!isset($type_name)) || (!isset($class_name)) ) {
throw new Net_DNS2_Exception(
'invalid question section: invalid type (' . $type .
') or class (' . $class . ') specified.',
Net_DNS2_Lookups::E_QUESTION_INVALID
);
}
//
// store it
//
$this->qtype = $type_name;
$this->qclass = $class_name;
return true;
}
/**
* returns a binary packed Net_DNS2_Question object
*
* @param Net_DNS2_Packet &$packet the Net_DNS2_Packet object this question is
* part of. This needs to be passed in so that
* the compressed qname value can be packed in
* with the names of the other parts of the
* packet.
*
* @return string
* @throws Net_DNS2_Exception
* @access public
*
*/
public function get(Net_DNS2_Packet &$packet)
{
//
// validate the type and class
//
$type = Net_DNS2_Lookups::$rr_types_by_name[$this->qtype];
$class = Net_DNS2_Lookups::$classes_by_name[$this->qclass];
if ( (!isset($type)) || (!isset($class)) ) {
throw new Net_DNS2_Exception(
'invalid question section: invalid type (' . $this->qtype .
') or class (' . $this->qclass . ') specified.',
Net_DNS2_Lookups::E_QUESTION_INVALID
);
}
$data = $packet->compress($this->qname, $packet->offset);
$data .= chr($type >> 8) . chr($type) . chr($class >> 8) . chr($class);
$packet->offset += 4;
return $data;
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,644 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
*/
/**
* This is the base class for DNS Resource Records
*
* Each resource record type (defined in RR/*.php) extends this class for
* base functionality.
*
* This class handles parsing and constructing the common parts of the DNS
* resource records, while the RR specific functionality is handled in each
* child class.
*
* DNS resource record format - RFC1035 section 4.1.3
*
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | |
* / /
* / NAME /
* | |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | TYPE |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | CLASS |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | TTL |
* | |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | RDLENGTH |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
* / RDATA /
* / /
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
*
*/
abstract class Net_DNS2_RR
{
/*
* The name of the resource record
*/
public $name;
/*
* The resource record type
*/
public $type;
/*
* The resouce record class
*/
public $class;
/*
* The time to live for this resource record
*/
public $ttl;
/*
* The length of the rdata field
*/
public $rdlength;
/*
* The resource record specific data as a packed binary string
*/
public $rdata;
/**
* abstract definition - method to return a RR as a string; not to
* be confused with the __toString() magic method.
*
* @return string
* @access protected
*
*/
abstract protected function rrToString();
/**
* abstract definition - parses a RR from a standard DNS config line
*
* @param array $rdata a string split line of values for the rdata
*
* @return boolean
* @access protected
*
*/
abstract protected function rrFromString(array $rdata);
/**
* abstract definition - sets a Net_DNS2_RR from a Net_DNS2_Packet object
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
*
* @return boolean
* @access protected
*
*/
abstract protected function rrSet(Net_DNS2_Packet &$packet);
/**
* abstract definition - returns a binary packet DNS RR object
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet use for
* compressed names
*
* @return mixed either returns a binary packed string or
* null on failure
* @access protected
*
*/
abstract protected function rrGet(Net_DNS2_Packet &$packet);
/**
* Constructor - builds a new Net_DNS2_RR object
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet or null to create
* an empty object
* @param array $rr an array with RR parse values or null to
* create an empty object
*
* @throws Net_DNS2_Exception
* @access public
*
*/
public function __construct(Net_DNS2_Packet &$packet = null, array $rr = null)
{
if ( (!is_null($packet)) && (!is_null($rr)) ) {
if ($this->set($packet, $rr) == false) {
throw new Net_DNS2_Exception(
'failed to generate resource record',
Net_DNS2_Lookups::E_RR_INVALID
);
}
} else {
$class = Net_DNS2_Lookups::$rr_types_class_to_id[get_class($this)];
if (isset($class)) {
$this->type = Net_DNS2_Lookups::$rr_types_by_id[$class];
}
$this->class = 'IN';
$this->ttl = 86400;
}
}
/**
* magic __toString() method to return the Net_DNS2_RR object object as a string
*
* @return string
* @access public
*
*/
public function __toString()
{
return $this->name . '. ' . $this->ttl . ' ' . $this->class .
' ' . $this->type . ' ' . $this->rrToString();
}
/**
* return a formatted string; if a string has spaces in it, then return
* it with double quotes around it, otherwise, return it as it was passed in.
*
* @param string $string the string to format
*
* @return string
* @access protected
*
*/
protected function formatString($string)
{
return '"' . str_replace('"', '\"', trim($string, '"')) . '"';
}
/**
* builds an array of strings from an array of chunks of text split by spaces
*
* @param array $chunks an array of chunks of text split by spaces
*
* @return array
* @access protected
*
*/
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 == true) {
$data[$c] .= ' ' . $r;
} else {
$data[$c++] = $r;
}
}
}
foreach ($data as $index => $string) {
$data[$index] = str_replace('\"', '"', trim($string, '"'));
}
return $data;
}
/**
* builds a new Net_DNS2_RR object
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet or null to create
* an empty object
* @param array $rr an array with RR parse values or null to
* create an empty object
*
* @return boolean
* @throws Net_DNS2_Exception
* @access public
*
*/
public function set(Net_DNS2_Packet &$packet, array $rr)
{
$this->name = $rr['name'];
$this->type = Net_DNS2_Lookups::$rr_types_by_id[$rr['type']];
//
// for RR OPT (41), the class value includes the requestors UDP payload size,
// and not a class value
//
if ($this->type == 'OPT') {
$this->class = $rr['class'];
} else {
$this->class = Net_DNS2_Lookups::$classes_by_id[$rr['class']];
}
$this->ttl = $rr['ttl'];
$this->rdlength = $rr['rdlength'];
$this->rdata = substr($packet->rdata, $packet->offset, $rr['rdlength']);
return $this->rrSet($packet);
}
/**
* returns a binary packed DNS RR object
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet used for
* compressing names
*
* @return string
* @throws Net_DNS2_Exception
* @access public
*
*/
public function get(Net_DNS2_Packet &$packet)
{
$data = '';
$rdata = '';
//
// pack the name
//
$data = $packet->compress($this->name, $packet->offset);
//
// pack the main values
//
if ($this->type == 'OPT') {
//
// pre-build the TTL value
//
$this->preBuild();
//
// the class value is different for OPT types
//
$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
);
}
//
// increase the offset, and allow for the rdlength
//
$packet->offset += 10;
//
// get the RR specific details
//
if ($this->rdlength != -1) {
$rdata = $this->rrGet($packet);
}
//
// add the RR
//
$data .= pack('n', strlen($rdata)) . $rdata;
return $data;
}
/**
* parses a binary packet, and returns the appropriate Net_DNS2_RR object,
* based on the RR type of the binary content.
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet used for
* decompressing names
*
* @return mixed returns a new Net_DNS2_RR_* object for
* the given RR
* @throws Net_DNS2_Exception
* @access public
*
*/
public static function parse(Net_DNS2_Packet &$packet)
{
$object = array();
//
// expand the name
//
$object['name'] = $packet->expand($packet, $packet->offset);
if (is_null($object['name'])) {
throw new Net_DNS2_Exception(
'failed to parse resource record: failed to expand name.',
Net_DNS2_Lookups::E_PARSE_ERROR
);
}
if ($packet->rdlength < ($packet->offset + 10)) {
throw new Net_DNS2_Exception(
'failed to parse resource record: packet too small.',
Net_DNS2_Lookups::E_PARSE_ERROR
);
}
//
// unpack the RR details
//
$object['type'] = ord($packet->rdata[$packet->offset++]) << 8 |
ord($packet->rdata[$packet->offset++]);
$object['class'] = ord($packet->rdata[$packet->offset++]) << 8 |
ord($packet->rdata[$packet->offset++]);
$object['ttl'] = ord($packet->rdata[$packet->offset++]) << 24 |
ord($packet->rdata[$packet->offset++]) << 16 |
ord($packet->rdata[$packet->offset++]) << 8 |
ord($packet->rdata[$packet->offset++]);
$object['rdlength'] = ord($packet->rdata[$packet->offset++]) << 8 |
ord($packet->rdata[$packet->offset++]);
if ($packet->rdlength < ($packet->offset + $object['rdlength'])) {
return null;
}
//
// lookup the class to use
//
$o = null;
$class = Net_DNS2_Lookups::$rr_types_id_to_class[$object['type']];
if (isset($class)) {
$o = new $class($packet, $object);
if ($o) {
$packet->offset += $object['rdlength'];
}
} else {
throw new Net_DNS2_Exception(
'un-implemented resource record type: ' . $object['type'],
Net_DNS2_Lookups::E_RR_INVALID
);
}
return $o;
}
/**
* cleans up some RR data
*
* @param string $data the text string to clean
*
* @return string returns the cleaned string
*
* @access public
*
*/
public function cleanString($data)
{
return strtolower(rtrim($data, '.'));
}
/**
* parses a standard RR format lines, as defined by rfc1035 (kinda)
*
* In our implementation, the domain *must* be specified- format must be
*
* <name> [<ttl>] [<class>] <type> <rdata>
* or
* <name> [<class>] [<ttl>] <type> <rdata>
*
* name, title, class and type are parsed by this function, rdata is passed
* to the RR specific classes for parsing.
*
* @param string $line a standard DNS config line
*
* @return mixed returns a new Net_DNS2_RR_* object for the given RR
* @throws Net_DNS2_Exception
* @access public
*
*/
public static function fromString($line)
{
if (strlen($line) == 0) {
throw new Net_DNS2_Exception(
'empty config line provided.',
Net_DNS2_Lookups::E_PARSE_ERROR
);
}
$name = '';
$type = '';
$class = 'IN';
$ttl = 86400;
//
// split the line by spaces
//
$values = preg_split('/[\s]+/', $line);
if (count($values) < 3) {
throw new Net_DNS2_Exception(
'failed to parse config: minimum of name, type and rdata required.',
Net_DNS2_Lookups::E_PARSE_ERROR
);
}
//
// assume the first value is the name
//
$name = trim(strtolower(array_shift($values)), '.');
//
// The next value is either a TTL, Class or Type
//
foreach ($values as $value) {
switch(true) {
case is_numeric($value):
$ttl = array_shift($values);
break;
//
// this is here because of a bug in is_numeric() in certain versions of
// PHP on windows.
//
case ($value === 0):
$ttl = array_shift($values);
break;
case isset(Net_DNS2_Lookups::$classes_by_name[strtoupper($value)]):
$class = strtoupper(array_shift($values));
break;
case isset(Net_DNS2_Lookups::$rr_types_by_name[strtoupper($value)]):
$type = strtoupper(array_shift($values));
break 2;
break;
default:
throw new Net_DNS2_Exception(
'invalid config line provided: unknown file: ' . $value,
Net_DNS2_Lookups::E_PARSE_ERROR
);
}
}
//
// lookup the class to use
//
$o = null;
$class_name = Net_DNS2_Lookups::$rr_types_id_to_class[
Net_DNS2_Lookups::$rr_types_by_name[$type]
];
if (isset($class_name)) {
$o = new $class_name;
if (!is_null($o)) {
//
// set the parsed values
//
$o->name = $name;
$o->class = $class;
$o->ttl = $ttl;
//
// parse the rdata
//
if ($o->rrFromString($values) === false) {
throw new Net_DNS2_Exception(
'failed to parse rdata for config: ' . $line,
Net_DNS2_Lookups::E_PARSE_ERROR
);
}
} else {
throw new Net_DNS2_Exception(
'failed to create new RR record for type: ' . $type,
Net_DNS2_Lookups::E_RR_INVALID
);
}
} else {
throw new Net_DNS2_Exception(
'un-implemented resource record type: '. $type,
Net_DNS2_Lookups::E_RR_INVALID
);
}
return $o;
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,153 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
*/
/**
* CNAME Resource Record - RFC1035 section 3.3.1
*
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* / CNAME /
* / /
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
* @see Net_DNS2_RR
*
*/
class Net_DNS2_RR_CNAME extends Net_DNS2_RR
{
/*
* The canonical name
*/
public $cname;
/**
* method to return the rdata portion of the packet as a string
*
* @return string
* @access protected
*
*/
protected function rrToString()
{
return $this->cleanString($this->cname) . '.';
}
/**
* parses the rdata portion from a standard DNS config line
*
* @param array $rdata a string split line of values for the rdata
*
* @return boolean
* @access protected
*
*/
protected function rrFromString(array $rdata)
{
$this->cname = $this->cleanString(array_shift($rdata));
return true;
}
/**
* parses the rdata of the Net_DNS2_Packet object
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
*
* @return boolean
* @access protected
*
*/
protected function rrSet(Net_DNS2_Packet &$packet)
{
if ($this->rdlength > 0) {
$offset = $packet->offset;
$this->cname = Net_DNS2_Packet::expand($packet, $offset);
return true;
}
return false;
}
/**
* returns the rdata portion of the DNS packet
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet use for
* compressed names
*
* @return mixed either returns a binary packed
* string or null on failure
* @access protected
*
*/
protected function rrGet(Net_DNS2_Packet &$packet)
{
if (strlen($this->cname) > 0) {
return $packet->compress($this->cname, $packet->offset);
}
return null;
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,292 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 1.0.0
*
*/
/**
* OPT Resource Record - RFC2929 section 3.1
*
* +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
* | OPTION-CODE |
* +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
* | OPTION-LENGTH |
* +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
* | |
* / OPTION-DATA /
* / /
* +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
* @see Net_DNS2_RR
*
*/
class Net_DNS2_RR_OPT extends Net_DNS2_RR
{
/*
* option code - assigned by IANA
*/
public $option_code;
/*
* the length of the option data
*/
public $option_length;
/*
* the option data
*/
public $option_data;
/*
* the extended response code stored in the TTL
*/
public $extended_rcode;
/*
* the implementation level
*/
public $version;
/*
* the DO bit used for DNSSEC - RFC3225
*/
public $do;
/*
* the extended flags
*/
public $z;
/**
* Constructor - builds a new Net_DNS2_RR_OPT object; normally you wouldn't call
* this directly, but OPT RR's are a little different
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet or null to create
* an empty object
* @param array $rr an array with RR parse values or null to
* create an empty object
*
* @throws Net_DNS2_Exception
* @access public
*
*/
public function __construct(Net_DNS2_Packet &$packet = null, array $rr = null)
{
//
// this is for when we're manually building an OPT RR object; we aren't
// passing in binary data to parse, we just want a clean/empty object.
//
$this->type = 'OPT';
$this->rdlength = 0;
$this->option_length = 0;
$this->extended_rcode = 0;
$this->version = 0;
$this->do = 0;
$this->z = 0;
//
// everthing else gets passed through to the parent.
//
if ( (!is_null($packet)) && (!is_null($rr)) ) {
parent::__construct($packet, $rr);
}
}
/**
* method to return the rdata portion of the packet as a string. There is no
* defintion for returning an OPT RR by string- this is just here to validate
* the binary parsing / building routines.
*
* @return string
* @access protected
*
*/
protected function rrToString()
{
return $this->option_code . ' ' . $this->option_data;
}
/**
* parses the rdata portion from a standard DNS config line. There is no
* definition for parsing a OPT RR by string- this is just here to validate
* the binary parsing / building routines.
*
* @param array $rdata a string split line of values for the rdata
*
* @return boolean
* @access protected
*
*/
protected function rrFromString(array $rdata)
{
$this->option_code = array_shift($rdata);
$this->option_data = array_shift($rdata);
$this->option_length = strlen($this->option_data);
$x = unpack('Cextended/Cversion/Cdo/Cz', pack('N', $this->ttl));
$this->extended_rcode = $x['extended'];
$this->version = $x['version'];
$this->do = ($x['do'] >> 7);
$this->z = $x['z'];
return true;
}
/**
* parses the rdata of the Net_DNS2_Packet object
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
*
* @return boolean
* @access protected
*
*/
protected function rrSet(Net_DNS2_Packet &$packet)
{
//
// parse out the TTL value
//
$x = unpack('Cextended/Cversion/Cdo/Cz', pack('N', $this->ttl));
$this->extended_rcode = $x['extended'];
$this->version = $x['version'];
$this->do = ($x['do'] >> 7);
$this->z = $x['z'];
//
// parse the data, if there is any
//
if ($this->rdlength > 0) {
//
// unpack the code and length
//
$x = unpack('noption_code/noption_length', $this->rdata);
$this->option_code = $x['option_code'];
$this->option_length = $x['option_length'];
//
// copy out the data based on the length
//
$this->option_data = substr($this->rdata, 4);
}
return true;
}
/**
* pre-builds the TTL value for this record; we needed to separate this out
* from the rrGet() function, as the logic in the Net_DNS2_RR packs the TTL
* value before it builds the rdata value.
*
* @return void
* @access protected
*
*/
protected function preBuild()
{
//
// build the TTL value based on the local values
//
$ttl = unpack(
'N',
pack('CCCC', $this->extended_rcode, $this->version, ($this->do << 7), 0)
);
$this->ttl = $ttl[1];
return;
}
/**
* returns the rdata portion of the DNS packet
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet use for
* compressed names
*
* @return mixed either returns a binary packed
* string or null on failure
* @access protected
*
*/
protected function rrGet(Net_DNS2_Packet &$packet)
{
//
// if there is an option code, then pack that data too
//
if ($this->option_code) {
$data = pack('nn', $this->option_code, $this->option_length) .
$this->option_data;
$packet->offset += strlen($data);
return $data;
}
return null;
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,152 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
*/
/**
* PTR Resource Record - RFC1035 section 3.3.12
*
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* / PTRDNAME /
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
* @see Net_DNS2_RR
*
*/
class Net_DNS2_RR_PTR extends Net_DNS2_RR
{
/*
* the hostname of the PTR entry
*/
public $ptrdname;
/**
* method to return the rdata portion of the packet as a string
*
* @return string
* @access protected
*
*/
protected function rrToString()
{
return rtrim($this->ptrdname, '.') . '.';
}
/**
* parses the rdata portion from a standard DNS config line
*
* @param array $rdata a string split line of values for the rdata
*
* @return boolean
* @access protected
*
*/
protected function rrFromString(array $rdata)
{
$this->ptrdname = rtrim(implode(' ', $rdata), '.');
return true;
}
/**
* parses the rdata of the Net_DNS2_Packet object
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
*
* @return boolean
* @access protected
*
*/
protected function rrSet(Net_DNS2_Packet &$packet)
{
if ($this->rdlength > 0) {
$offset = $packet->offset;
$this->ptrdname = Net_DNS2_Packet::expand($packet, $offset);
return true;
}
return false;
}
/**
* returns the rdata portion of the DNS packet
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet use for
* compressed names
*
* @return mixed either returns a binary packed
* string or null on failure
* @access protected
*
*/
protected function rrGet(Net_DNS2_Packet &$packet)
{
if (strlen($this->ptrdname) > 0) {
return $packet->compress($this->ptrdname, $packet->offset);
}
return null;
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,459 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
* This file contains code based off the Net::DNS::SEC Perl module by
* Olaf M. Kolkman
*
* This is the copyright notice from the PERL Net::DNS::SEC module:
*
* Copyright (c) 2001 - 2005 RIPE NCC. Author Olaf M. Kolkman
* Copyright (c) 2007 - 2008 NLnet Labs. Author Olaf M. Kolkman
* <olaf@net-dns.org>
*
* All Rights Reserved
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation, and that the name of the author not be
* used in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
*
* THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
* AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
* DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
/**
* SIG Resource Record - RFC2535 section 4.1
*
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Type Covered | Algorithm | Labels |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Original TTL |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Signature Expiration |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Signature Inception |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Key Tag | /
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Signer's Name /
* / /
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* / /
* / Signature /
* / /
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
* @see Net_DNS2_RR
*
*/
class Net_DNS2_RR_SIG extends Net_DNS2_RR
{
/*
* and instance of a Net_DNS2_PrivateKey object
*/
public $private_key = null;
/*
* the RR type covered by this signature
*/
public $typecovered;
/*
* the algorithm used for the signature
*/
public $algorithm;
/*
* the number of labels in the name
*/
public $labels;
/*
* the original TTL
*/
public $origttl;
/*
* the signature expiration
*/
public $sigexp;
/*
* the inception of the signature
*/
public $sigincep;
/*
* the keytag used
*/
public $keytag;
/*
* the signer's name
*/
public $signname;
/*
* the signature
*/
public $signature;
/**
* method to return the rdata portion of the packet as a string
*
* @return string
* @access protected
*
*/
protected function rrToString()
{
return $this->typecovered . ' ' . $this->algorithm . ' ' .
$this->labels . ' ' . $this->origttl . ' ' .
$this->sigexp . ' ' . $this->sigincep . ' ' .
$this->keytag . ' ' . $this->cleanString($this->signname) . '. ' .
$this->signature;
}
/**
* parses the rdata portion from a standard DNS config line
*
* @param array $rdata a string split line of values for the rdata
*
* @return boolean
* @access protected
*
*/
protected function rrFromString(array $rdata)
{
$this->typecovered = strtoupper(array_shift($rdata));
$this->algorithm = array_shift($rdata);
$this->labels = array_shift($rdata);
$this->origttl = array_shift($rdata);
$this->sigexp = array_shift($rdata);
$this->sigincep = array_shift($rdata);
$this->keytag = array_shift($rdata);
$this->signname = $this->cleanString(array_shift($rdata));
foreach ($rdata as $line) {
$this->signature .= $line;
}
$this->signature = trim($this->signature);
return true;
}
/**
* parses the rdata of the Net_DNS2_Packet object
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
*
* @return boolean
* @access protected
*
*/
protected function rrSet(Net_DNS2_Packet &$packet)
{
if ($this->rdlength > 0) {
//
// unpack
//
$x = unpack(
'ntc/Calgorithm/Clabels/Norigttl/Nsigexp/Nsigincep/nkeytag',
$this->rdata
);
$this->typecovered = Net_DNS2_Lookups::$rr_types_by_id[$x['tc']];
$this->algorithm = $x['algorithm'];
$this->labels = $x['labels'];
$this->origttl = Net_DNS2::expandUint32($x['origttl']);
//
// the dates are in GM time
//
$this->sigexp = gmdate('YmdHis', $x['sigexp']);
$this->sigincep = gmdate('YmdHis', $x['sigincep']);
//
// get the keytag
//
$this->keytag = $x['keytag'];
//
// get teh signers name and signature
//
$offset = $packet->offset + 18;
$sigoffset = $offset;
$this->signname = strtolower(
Net_DNS2_Packet::expand($packet, $sigoffset)
);
$this->signature = base64_encode(
substr($this->rdata, 18 + ($sigoffset - $offset))
);
return true;
}
return false;
}
/**
* returns the rdata portion of the DNS packet
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet use for
* compressed names
*
* @return mixed either returns a binary packed
* string or null on failure
* @access protected
*
*/
protected function rrGet(Net_DNS2_Packet &$packet)
{
//
// parse the values out of the dates
//
preg_match(
'/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/', $this->sigexp, $e
);
preg_match(
'/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/', $this->sigincep, $i
);
//
// pack the value
//
$data = pack(
'nCCNNNn',
Net_DNS2_Lookups::$rr_types_by_name[$this->typecovered],
$this->algorithm,
$this->labels,
$this->origttl,
gmmktime($e[4], $e[5], $e[6], $e[2], $e[3], $e[1]),
gmmktime($i[4], $i[5], $i[6], $i[2], $i[3], $i[1]),
$this->keytag
);
//
// the signer name is special; it's not allowed to be compressed
// (see section 3.1.7)
//
$names = explode('.', strtolower($this->signname));
foreach ($names as $name) {
$data .= chr(strlen($name));
$data .= $name;
}
$data .= chr('0');
//
// if the signature is empty, and $this->private_key is an instance of a
// private key object, and we have access to openssl, then assume this
// is a SIG(0), and generate a new signature
//
if ( (strlen($this->signature) == 0)
&& ($this->private_key instanceof Net_DNS2_PrivateKey)
&& (extension_loaded('openssl') === true)
) {
//
// create a new packet for the signature-
//
$new_packet = new Net_DNS2_Packet_Request('example.com', 'SOA', 'IN');
//
// copy the packet data over
//
$new_packet->copy($packet);
//
// remove the SIG object from the additional list
//
array_pop($new_packet->additional);
$new_packet->header->arcount = count($new_packet->additional);
//
// copy out the data
//
$sigdata = $data . $new_packet->get();
//
// based on the algorithm
//
$algorithm = 0;
switch($this->algorithm) {
//
// MD5
//
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_RSAMD5:
$algorithm = OPENSSL_ALGO_MD5;
break;
//
// SHA1
//
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_RSASHA1:
$algorithm = OPENSSL_ALGO_SHA1;
break;
//
// SHA256 (PHP 5.4.8 or higher)
//
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_RSASHA256:
if (version_compare(PHP_VERSION, '5.4.8', '<') == true) {
throw new Net_DNS2_Exception(
'SHA256 support is only available in PHP >= 5.4.8',
Net_DNS2_Lookups::E_OPENSSL_INV_ALGO
);
}
$algorithm = OPENSSL_ALGO_SHA256;
break;
//
// SHA512 (PHP 5.4.8 or higher)
//
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_RSASHA512:
if (version_compare(PHP_VERSION, '5.4.8', '<') == true) {
throw new Net_DNS2_Exception(
'SHA512 support is only available in PHP >= 5.4.8',
Net_DNS2_Lookups::E_OPENSSL_INV_ALGO
);
}
$algorithm = OPENSSL_ALGO_SHA512;
break;
//
// unsupported at the moment
//
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_DSA:
case Net_DNS2_Lookups::DSNSEC_ALGORITHM_RSASHA1NSEC3SHA1:
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_DSANSEC3SHA1:
default:
throw new Net_DNS2_Exception(
'invalid or unsupported algorithm',
Net_DNS2_Lookups::E_OPENSSL_INV_ALGO
);
break;
}
//
// sign the data
//
if (openssl_sign($sigdata, $this->signature, $this->private_key->instance, $algorithm) == false) {
throw new Net_DNS2_Exception(
openssl_error_string(),
Net_DNS2_Lookups::E_OPENSSL_ERROR
);
}
//
// build the signature value based
//
switch($this->algorithm) {
//
// RSA- add it directly
//
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_RSAMD5:
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_RSASHA1:
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_RSASHA256:
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_RSASHA512:
$this->signature = base64_encode($this->signature);
break;
}
}
//
// add the signature
//
$data .= base64_decode($this->signature);
$packet->offset += strlen($data);
return $data;
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,240 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
*/
/**
* SOA Resource Record - RFC1035 section 3.3.13
*
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* / MNAME /
* / /
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* / RNAME /
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | SERIAL |
* | |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | REFRESH |
* | |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | RETRY |
* | |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | EXPIRE |
* | |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | MINIMUM |
* | |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
* @see Net_DNS2_RR
*
*/
class Net_DNS2_RR_SOA extends Net_DNS2_RR
{
/*
* The master DNS server
*/
public $mname;
/*
* mailbox of the responsible person
*/
public $rname;
/*
* serial number
*/
public $serial;
/*
* refresh time
*/
public $refresh;
/*
* retry interval
*/
public $retry;
/*
* expire time
*/
public $expire;
/*
* minimum TTL for any RR in this zone
*/
public $minimum;
/**
* method to return the rdata portion of the packet as a string
*
* @return string
* @access protected
*
*/
protected function rrToString()
{
return $this->cleanString($this->mname) . '. ' .
$this->cleanString($this->rname) . '. ' .
$this->serial . ' ' . $this->refresh . ' ' . $this->retry . ' ' .
$this->expire . ' ' . $this->minimum;
}
/**
* parses the rdata portion from a standard DNS config line
*
* @param array $rdata a string split line of values for the rdata
*
* @return boolean
* @access protected
*
*/
protected function rrFromString(array $rdata)
{
$this->mname = $this->cleanString($rdata[0]);
$this->rname = $this->cleanString($rdata[1]);
$this->serial = $rdata[2];
$this->refresh = $rdata[3];
$this->retry = $rdata[4];
$this->expire = $rdata[5];
$this->minimum = $rdata[6];
return true;
}
/**
* parses the rdata of the Net_DNS2_Packet object
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
*
* @return boolean
* @access protected
*
*/
protected function rrSet(Net_DNS2_Packet &$packet)
{
if ($this->rdlength > 0) {
//
// parse the
//
$offset = $packet->offset;
$this->mname = Net_DNS2_Packet::expand($packet, $offset);
$this->rname = Net_DNS2_Packet::expand($packet, $offset);
//
// get the SOA values
//
$x = unpack(
'@' . $offset . '/Nserial/Nrefresh/Nretry/Nexpire/Nminimum/',
$packet->rdata
);
$this->serial = Net_DNS2::expandUint32($x['serial']);
$this->refresh = Net_DNS2::expandUint32($x['refresh']);
$this->retry = Net_DNS2::expandUint32($x['retry']);
$this->expire = Net_DNS2::expandUint32($x['expire']);
$this->minimum = Net_DNS2::expandUint32($x['minimum']);
return true;
}
return false;
}
/**
* returns the rdata portion of the DNS packet
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet use for
* compressed names
*
* @return mixed either returns a binary packed
* string or null on failure
* @access protected
*
*/
protected function rrGet(Net_DNS2_Packet &$packet)
{
if (strlen($this->mname) > 0) {
$data = $packet->compress($this->mname, $packet->offset);
$data .= $packet->compress($this->rname, $packet->offset);
$data .= pack(
'N5', $this->serial, $this->refresh, $this->retry,
$this->expire, $this->minimum
);
$packet->offset += 20;
return $data;
}
return null;
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,504 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
*/
/**
* TSIG Resource Record - RFC 2845
*
* 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* / algorithm /
* / /
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | time signed |
* | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | | fudge |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | mac size | /
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /
* / mac /
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | original id | error |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | other length | /
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /
* / other data /
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
* @see Net_DNS2_RR
*
*/
class Net_DNS2_RR_TSIG extends Net_DNS2_RR
{
/*
* TSIG Algorithm Identifiers
*/
const HMAC_MD5 = 'hmac-md5.sig-alg.reg.int'; // RFC 2845, required
const GSS_TSIG = 'gss-tsig'; // unsupported, optional
const HMAC_SHA1 = 'hmac-sha1'; // RFC 4635, required
const HMAC_SHA224 = 'hmac-sha224'; // RFC 4635, optional
const HMAC_SHA256 = 'hmac-sha256'; // RFC 4635, required
const HMAC_SHA384 = 'hmac-sha384'; // RFC 4635, optional
const HMAC_SHA512 = 'hmac-sha512'; // RFC 4635, optional
/*
* the map of hash values to names
*/
public static $hash_algorithms = array(
self::HMAC_MD5 => 'md5',
self::HMAC_SHA1 => 'sha1',
self::HMAC_SHA224 => 'sha224',
self::HMAC_SHA256 => 'sha256',
self::HMAC_SHA384 => 'sha384',
self::HMAC_SHA512 => 'sha512'
);
/*
* algorithm used; only supports HMAC-MD5
*/
public $algorithm;
/*
* The time it was signed
*/
public $time_signed;
/*
* fudge- allowed offset from the time signed
*/
public $fudge;
/*
* size of the digest
*/
public $mac_size;
/*
* the digest data
*/
public $mac;
/*
* the original id of the request
*/
public $original_id;
/*
* additional error code
*/
public $error;
/*
* length of the "other" data, should only ever be 0 when there is
* no error, or 6 when there is the error RCODE_BADTIME
*/
public $other_length;
/*
* the other data; should only ever be a timestamp when there is the
* error RCODE_BADTIME
*/
public $other_data;
/*
* the key to use for signing - passed in, not included in the rdata
*/
public $key;
/**
* method to return the rdata portion of the packet as a string
*
* @return string
* @access protected
*
*/
protected function rrToString()
{
$out = $this->cleanString($this->algorithm) . '. ' .
$this->time_signed . ' ' .
$this->fudge . ' ' . $this->mac_size . ' ' .
base64_encode($this->mac) . ' ' . $this->original_id . ' ' .
$this->error . ' '. $this->other_length;
if ($this->other_length > 0) {
$out .= ' ' . $this->other_data;
}
return $out;
}
/**
* parses the rdata portion from a standard DNS config line
*
* @param array $rdata a string split line of values for the rdata
*
* @return boolean
* @access protected
*
*/
protected function rrFromString(array $rdata)
{
//
// the only value passed in is the key-
//
// this assumes it's passed in base64 encoded.
//
$this->key = preg_replace('/\s+/', '', array_shift($rdata));
//
// the rest of the data is set to default
//
$this->algorithm = self::HMAC_MD5;
$this->time_signed = time();
$this->fudge = 300;
$this->mac_size = 0;
$this->mac = '';
$this->original_id = 0;
$this->error = 0;
$this->other_length = 0;
$this->other_data = '';
//
// per RFC 2845 section 2.3
//
$this->class = 'ANY';
$this->ttl = 0;
return true;
}
/**
* parses the rdata of the Net_DNS2_Packet object
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
*
* @return boolean
* @access protected
*
*/
protected function rrSet(Net_DNS2_Packet &$packet)
{
if ($this->rdlength > 0) {
//
// expand the algorithm
//
$newoffset = $packet->offset;
$this->algorithm = Net_DNS2_Packet::expand($packet, $newoffset);
$offset = $newoffset - $packet->offset;
//
// unpack time, fudge and mac_size
//
$x = unpack(
'@' . $offset . '/ntime_high/Ntime_low/nfudge/nmac_size',
$this->rdata
);
$this->time_signed = Net_DNS2::expandUint32($x['time_low']);
$this->fudge = $x['fudge'];
$this->mac_size = $x['mac_size'];
$offset += 10;
//
// copy out the mac
//
if ($this->mac_size > 0) {
$this->mac = substr($this->rdata, $offset, $this->mac_size);
$offset += $this->mac_size;
}
//
// unpack the original id, error, and other_length values
//
$x = unpack(
'@' . $offset . '/noriginal_id/nerror/nother_length',
$this->rdata
);
$this->original_id = $x['original_id'];
$this->error = $x['error'];
$this->other_length = $x['other_length'];
//
// the only time there is actually any "other data", is when there's
// a BADTIME error code.
//
// The other length should be 6, and the other data field includes the
// servers current time - per RFC 2845 section 4.5.2
//
if ($this->error == Net_DNS2_Lookups::RCODE_BADTIME) {
if ($this->other_length != 6) {
return false;
}
//
// other data is a 48bit timestamp
//
$x = unpack(
'nhigh/nlow',
substr($this->rdata, $offset + 6, $this->other_length)
);
$this->other_data = $x['low'];
}
return true;
}
return false;
}
/**
* returns the rdata portion of the DNS packet
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet use for
* compressed names
*
* @return mixed either returns a binary packed
* string or null on failure
* @access protected
*
*/
protected function rrGet(Net_DNS2_Packet &$packet)
{
if (strlen($this->key) > 0) {
//
// create a new packet for the signature-
//
$new_packet = new Net_DNS2_Packet_Request('example.com', 'SOA', 'IN');
//
// copy the packet data over
//
$new_packet->copy($packet);
//
// remove the TSIG object from the additional list
//
array_pop($new_packet->additional);
$new_packet->header->arcount = count($new_packet->additional);
//
// copy out the data
//
$sig_data = $new_packet->get();
//
// add the name without compressing
//
$sig_data .= Net_DNS2_Packet::pack($this->name);
//
// add the class and TTL
//
$sig_data .= pack(
'nN', Net_DNS2_Lookups::$classes_by_name[$this->class], $this->ttl
);
//
// add the algorithm name without compression
//
$sig_data .= Net_DNS2_Packet::pack(strtolower($this->algorithm));
//
// add the rest of the values
//
$sig_data .= pack(
'nNnnn', 0, $this->time_signed, $this->fudge,
$this->error, $this->other_length
);
if ($this->other_length > 0) {
$sig_data .= pack('nN', 0, $this->other_data);
}
//
// sign the data
//
$this->mac = $this->_signHMAC(
$sig_data, base64_decode($this->key), $this->algorithm
);
$this->mac_size = strlen($this->mac);
//
// compress the algorithm
//
$data = Net_DNS2_Packet::pack(strtolower($this->algorithm));
//
// pack the time, fudge and mac size
//
$data .= pack(
'nNnn', 0, $this->time_signed, $this->fudge, $this->mac_size
);
$data .= $this->mac;
//
// check the error and other_length
//
if ($this->error == Net_DNS2_Lookups::RCODE_BADTIME) {
$this->other_length = strlen($this->other_data);
if ($this->other_length != 6) {
return null;
}
} else {
$this->other_length = 0;
$this->other_data = '';
}
//
// pack the id, error and other_length
//
$data .= pack(
'nnn', $packet->header->id, $this->error, $this->other_length
);
if ($this->other_length > 0) {
$data .= pack('nN', 0, $this->other_data);
}
$packet->offset += strlen($data);
return $data;
}
return null;
}
/**
* signs the given data with the given key, and returns the result
*
* @param string $data the data to sign
* @param string $key key to use for signing
* @param string $algorithm the algorithm to use; defaults to MD5
*
* @return string the signed digest
* @throws Net_DNS2_Exception
* @access private
*
*/
private function _signHMAC($data, $key = null, $algorithm = self::HMAC_MD5)
{
//
// use the hash extension; this is included by default in >= 5.1.2 which
// is our dependent version anyway- so it's easy to switch to it.
//
if (extension_loaded('hash')) {
if (!isset(self::$hash_algorithms[$algorithm])) {
throw new Net_DNS2_Exception(
'invalid or unsupported algorithm',
Net_DNS2_Lookups::E_PARSE_ERROR
);
}
return hash_hmac(self::$hash_algorithms[$algorithm], $data, $key, true);
}
//
// if the hash extension isn't loaded, and they selected something other
// than MD5, throw an exception
//
if ($algorithm != self::HMAC_MD5) {
throw new Net_DNS2_Exception(
'only HMAC-MD5 supported. please install the php-extension ' .
'"hash" in order to use the sha-family',
Net_DNS2_Lookups::E_PARSE_ERROR
);
}
//
// otherwise, do it ourselves
//
if (is_null($key)) {
return pack('H*', md5($data));
}
$key = str_pad($key, 64, chr(0x00));
if (strlen($key) > 64) {
$key = pack('H*', md5($key));
}
$k_ipad = $key ^ str_repeat(chr(0x36), 64);
$k_opad = $key ^ str_repeat(chr(0x5c), 64);
return $this->_signHMAC(
$k_opad . pack('H*', md5($k_ipad . $data)), null, $algorithm
);
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,332 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
*/
/**
* This is the main resolver class, providing DNS query functions.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
* @see Net_DNS2
*
*/
class Net_DNS2_Resolver extends Net_DNS2
{
/**
* Constructor - creates a new Net_DNS2_Resolver object
*
* @param mixed $options either an array with options or null
*
* @access public
*
*/
public function __construct(array $options = null)
{
parent::__construct($options);
}
/**
* does a basic DNS lookup query
*
* @param string $name the DNS name to loookup
* @param string $type the name of the RR type to lookup
* @param string $class the name of the RR class to lookup
*
* @return Net_DNS2_RR object
* @throws Net_DNS2_Exception
* @access public
*
*/
public function query($name, $type = 'A', $class = 'IN')
{
//
// make sure we have some name servers set
//
$this->checkServers(Net_DNS2::RESOLV_CONF);
//
// we dont' support incremental zone tranfers; so if it's requested, a full
// zone transfer can be returned
//
if ($type == 'IXFR') {
$type = 'AXFR';
}
//
// if the name *looks* too short, then append the domain from the config
//
if ( (strpos($name, '.') === false) && ($type != 'PTR') ) {
$name .= '.' . strtolower($this->domain);
}
//
// create a new packet based on the input
//
$packet = new Net_DNS2_Packet_Request($name, $type, $class);
//
// check for an authentication method; either TSIG or SIG
//
if ( ($this->auth_signature instanceof Net_DNS2_RR_TSIG)
|| ($this->auth_signature instanceof Net_DNS2_RR_SIG)
) {
$packet->additional[] = $this->auth_signature;
$packet->header->arcount = count($packet->additional);
}
//
// check for the DNSSEC flag, and if it's true, then add an OPT
// RR to the additional section, and set the DO flag to 1.
//
if ($this->dnssec == true) {
//
// create a new OPT RR
//
$opt = new Net_DNS2_RR_OPT();
//
// set the DO flag, and the other values
//
$opt->do = 1;
$opt->class = $this->dnssec_payload_size;
//
// add the RR to the additional section.
//
$packet->additional[] = $opt;
$packet->header->arcount = count($packet->additional);
}
//
// set the DNSSEC AD or CD bits
//
if ($this->dnssec_ad_flag == true) {
$packet->header->ad = 1;
}
if ($this->dnssec_cd_flag == true) {
$packet->header->cd = 1;
}
//
// if caching is turned on, then check then hash the question, and
// do a cache lookup.
//
// don't use the cache for zone transfers
//
$packet_hash = '';
if ( ($this->use_cache == true) && ($this->cacheable($type) == true) ) {
//
// open the cache
//
$this->cache->open(
$this->cache_file, $this->cache_size, $this->cache_serializer
);
//
// build the key and check for it in the cache.
//
$packet_hash = md5(
$packet->question[0]->qname . '|' . $packet->question[0]->qtype
);
if ($this->cache->has($packet_hash)) {
return $this->cache->get($packet_hash);
}
}
//
// set the RD (recursion desired) bit to 1 / 0 depending on the config
// setting.
//
if ($this->recurse == false) {
$packet->header->rd = 0;
} else {
$packet->header->rd = 1;
}
//
// send the packet and get back the response
//
// *always* use TCP for zone transfers- does this cause any problems?
//
$response = $this->sendPacket(
$packet, ($type == 'AXFR') ? true : $this->use_tcp
);
//
// if strict mode is enabled, then make sure that the name that was
// looked up is *actually* in the response object.
//
// only do this is strict_query_mode is turned on, AND we've received
// some answers; no point doing any else if there were no answers.
//
if ( ($this->strict_query_mode == true)
&& ($response->header->ancount > 0)
) {
$found = false;
//
// look for the requested name/type/class
//
foreach ($response->answer as $index => $object) {
if ( (strcasecmp(trim($object->name, '.'), trim($packet->question[0]->qname, '.')) == 0)
&& ($object->type == $packet->question[0]->qtype)
&& ($object->class == $packet->question[0]->qclass)
) {
$found = true;
break;
}
}
//
// if it's not found, then unset the answer section; it's not correct to
// throw an exception here; if the hostname didn't exist, then
// sendPacket() would have already thrown an NXDOMAIN error- so the host
// *exists*, but just not the request type/class.
//
// the correct response in this case, is an empty answer section; the
// authority section may still have usual information, like a SOA record.
//
if ($found == false) {
$response->answer = array();
$response->header->ancount = 0;
}
}
//
// cache the response object
//
if ( ($this->use_cache == true) && ($this->cacheable($type) == true) ) {
$this->cache->put($packet_hash, $response);
}
return $response;
}
/**
* does an inverse query for the given RR; most DNS servers do not implement
* inverse queries, but they should be able to return "not implemented"
*
* @param Net_DNS2_RR $rr the RR object to lookup
*
* @return Net_DNS2_RR object
* @throws Net_DNS2_Exception
* @access public
*
*/
public function iquery(Net_DNS2_RR $rr)
{
//
// make sure we have some name servers set
//
$this->checkServers(Net_DNS2::RESOLV_CONF);
//
// create an empty packet
//
$packet = new Net_DNS2_Packet_Request($rr->name, 'A', 'IN');
//
// unset the question
//
$packet->question = array();
$packet->header->qdcount = 0;
//
// set the opcode to IQUERY
//
$packet->header->opcode = Net_DNS2_Lookups::OPCODE_IQUERY;
//
// add the given RR as the answer
//
$packet->answer[] = $rr;
$packet->header->ancount = 1;
//
// check for an authentication method; either TSIG or SIG
//
if ( ($this->auth_signature instanceof Net_DNS2_RR_TSIG)
|| ($this->auth_signature instanceof Net_DNS2_RR_SIG)
) {
$packet->additional[] = $this->auth_signature;
$packet->header->arcount = count($packet->additional);
}
//
// send the packet and get back the response
//
return $this->sendPacket($packet, $this->use_tcp);
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,190 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
*/
/*
* check to see if the socket defines exist; if they don't, then define them
*/
if (defined('SOCK_STREAM') == false) {
define('SOCK_STREAM', 1);
}
if (defined('SOCK_DGRAM') == false) {
define('SOCK_DGRAM', 2);
}
/**
* This is the abstract base class for the two sockets classes; this simply
* provides the class definition for the two sockets classes.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
* @see Net_DNS2_Socket_Sockets, Net_DNS2_Socket_Streams
*
*/
abstract class Net_DNS2_Socket
{
protected $sock;
protected $type;
protected $host;
protected $port;
protected $timeout;
protected $local_host;
protected $local_port;
public $last_error;
/*
* type of sockets
*/
const SOCK_STREAM = SOCK_STREAM;
const SOCK_DGRAM = SOCK_DGRAM;
/**
* constructor - set the port details
*
* @param integer $type the socket type
* @param string $host the IP address of the DNS server to connect to
* @param integer $port the port of the DNS server to connect to
* @param integer $timeout the timeout value to use for socket functions
*
* @access public
*
*/
public function __construct($type, $host, $port, $timeout)
{
$this->type = $type;
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;
}
/**
* destructor
*
* @access public
*/
public function __destruct()
{
$this->close();
}
/**
* sets the local address/port for the socket to bind to
*
* @param string $address the local IP address to bind to
* @param mixed $port the local port to bind to, or 0 to let the socket
* function select a port
*
* @return boolean
* @access public
*
*/
public function bindAddress($address, $port = 0)
{
$this->local_host = $address;
$this->local_port = $port;
return true;
}
/**
* opens a socket connection to the DNS server
*
* @return boolean
* @access public
*
*/
abstract public function open();
/**
* closes a socket connection to the DNS server
*
* @return boolean
* @access public
*
*/
abstract public function close();
/**
* writes the given string to the DNS server socket
*
* @param string $data a binary packed DNS packet
*
* @return boolean
* @access public
*
*/
abstract public function write($data);
/**
* reads a response from a DNS server
*
* @param integer &$size the size of the DNS packet read is passed back
* @param integer $max_size the max data size returned.
*
* @return mixed returns the data on success and false on error
* @access public
*
*/
abstract public function read(&$size, $max_size);
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,364 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
*/
/**
* Socket handling class using the PHP sockets extension
*
* The sockets extension is faster than the stream functions in PHP, but it's
* not standard. So if the extension is loaded, then this class is used, if
* it's not, then the Net_DNS2_Socket_Streams class is used.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
* @see Net_DNS2_Socket
*
*/
class Net_DNS2_Socket_Sockets extends Net_DNS2_Socket
{
/**
* opens a socket connection to the DNS server
*
* @return boolean
* @access public
*
*/
public function open()
{
//
// create the socket
//
if (Net_DNS2::isIPv4($this->host) == true) {
$this->sock = @socket_create(
AF_INET, $this->type,
($this->type == Net_DNS2_Socket::SOCK_STREAM) ? SOL_TCP : SOL_UDP
);
} else if (Net_DNS2::isIPv6($this->host) == true) {
$this->sock = @socket_create(
AF_INET6, $this->type,
($this->type == Net_DNS2_Socket::SOCK_STREAM) ? SOL_TCP : SOL_UDP
);
} else {
$this->last_error = 'invalid address type: ' . $this->host;
return false;
}
if ($this->sock === false) {
$this->last_error = socket_strerror(socket_last_error());
return false;
}
@socket_set_option($this->sock, SOL_SOCKET, SO_REUSEADDR, 1);
//
// bind to a local IP/port if it's set
//
if (strlen($this->local_host) > 0) {
$result = @socket_bind(
$this->sock, $this->local_host,
($this->local_port > 0) ? $this->local_port : null
);
if ($result === false) {
$this->last_error = socket_strerror(socket_last_error());
return false;
}
}
//
// mark the socket as non-blocking
//
if (@socket_set_nonblock($this->sock) === false) {
$this->last_error = socket_strerror(socket_last_error());
return false;
}
//
// connect to the socket; don't check for status here, we'll check it on the
// socket_select() call so we can handle timeouts properly
//
@socket_connect($this->sock, $this->host, $this->port);
$read = null;
$write = array($this->sock);
$except = null;
//
// select on write to check if the call to connect worked
//
$result = @socket_select($read, $write, $except, $this->timeout);
if ($result === false) {
$this->last_error = socket_strerror(socket_last_error());
return false;
} else if ($result == 0) {
$this->last_error = 'timeout on write select for connect()';
return false;
}
return true;
}
/**
* closes a socket connection to the DNS server
*
* @return boolean
* @access public
*
*/
public function close()
{
if (is_resource($this->sock) === true) {
@socket_close($this->sock);
}
return true;
}
/**
* writes the given string to the DNS server socket
*
* @param string $data a binary packed DNS packet
*
* @return boolean
* @access public
*
*/
public function write($data)
{
$length = strlen($data);
if ($length == 0) {
$this->last_error = 'empty data on write()';
return false;
}
$read = null;
$write = array($this->sock);
$except = null;
//
// select on write
//
$result = @socket_select($read, $write, $except, $this->timeout);
if ($result === false) {
$this->last_error = socket_strerror(socket_last_error());
return false;
} else if ($result == 0) {
$this->last_error = 'timeout on write select()';
return false;
}
//
// if it's a TCP socket, then we need to packet and send the length of the
// data as the first 16bit of data.
//
if ($this->type == Net_DNS2_Socket::SOCK_STREAM) {
$s = chr($length >> 8) . chr($length);
if (@socket_write($this->sock, $s) === false) {
$this->last_error = socket_strerror(socket_last_error());
return false;
}
}
//
// write the data to the socket
//
$size = @socket_write($this->sock, $data);
if ( ($size === false) || ($size != $length) ) {
$this->last_error = socket_strerror(socket_last_error());
return false;
}
return true;
}
/**
* reads a response from a DNS server
*
* @param integer &$size the size of the DNS packet read is passed back
*
* @return mixed returns the data on success and false on error
* @access public
*
*/
public function read(&$size, $max_size)
{
$read = array($this->sock);
$write = null;
$except = null;
//
// make sure our socket is non-blocking
//
if (@socket_set_nonblock($this->sock) === false) {
$this->last_error = socket_strerror(socket_last_error());
return false;
}
//
// select on read
//
$result = @socket_select($read, $write, $except, $this->timeout);
if ($result === false) {
$this->last_error = socket_strerror(socket_last_error());
return false;
} else if ($result == 0) {
$this->last_error = 'timeout on read select()';
return false;
}
$data = '';
$length = $max_size;
//
// if it's a TCP socket, then the first two bytes is the length of the DNS
// packet- we need to read that off first, then use that value for the
// packet read.
//
if ($this->type == Net_DNS2_Socket::SOCK_STREAM) {
if (($size = @socket_recv($this->sock, $data, 2, 0)) === false) {
$this->last_error = socket_strerror(socket_last_error());
return false;
}
$length = ord($data[0]) << 8 | ord($data[1]);
if ($length < Net_DNS2_Lookups::DNS_HEADER_SIZE) {
return false;
}
}
//
// at this point, we know that there is data on the socket to be read,
// because we've already extracted the length from the first two bytes.
//
// so the easiest thing to do, is just turn off socket blocking, and
// wait for the data.
//
if (@socket_set_block($this->sock) === false) {
$this->last_error = socket_strerror(socket_last_error());
return false;
}
//
// read the data from the socket
//
// loop while reading since some OS's (specifically Win < 2003) don't support
// MSG_WAITALL properly, so they may return with less data than is available.
//
// According to M$, XP and below don't support MSG_WAITALL at all; and there
// also seems to be some issue in 2003 and 2008 where the MSG_WAITALL is
// defined as 0, but if you actually pass 8 (which is the correct defined
// value), it works as it's supposed to- so in these cases, it's just the
// define that's incorrect- this is likely a PHP issue.
//
$data = '';
$size = 0;
while (1) {
$chunk_size = @socket_recv($this->sock, $chunk, $length, MSG_WAITALL);
if ($chunk_size === false) {
$size = $chunk_size;
$this->last_error = socket_strerror(socket_last_error());
return false;
}
$data .= $chunk;
$size += $chunk_size;
$length -= $chunk_size;
if ( ($length <= 0) || ($this->type == Net_DNS2_Socket::SOCK_DGRAM) ) {
break;
}
}
return $data;
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,389 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
*/
/**
* Socket handling class using the PHP Streams
*
* The sockets extension is faster than the stream functions in PHP, but it's
* not standard. So if the extension is loaded, then the Net_DNS2_Socket_Sockets
* class it used, otherwise, this class it used.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
* @see Net_DNS2_Socket
*
*/
class Net_DNS2_Socket_Streams extends Net_DNS2_Socket
{
private $_context;
/**
* opens a socket connection to the DNS server
*
* @return boolean
* @access public
*
*/
public function open()
{
//
// create a list of options for the context
//
$opts = array('socket' => array());
//
// bind to a local IP/port if it's set
//
if (strlen($this->local_host) > 0) {
$opts['socket']['bindto'] = $this->local_host;
if ($this->local_port > 0) {
$opts['socket']['bindto'] .= ':' . $this->local_port;
}
}
//
// create the context
//
$this->_context = @stream_context_create($opts);
//
// create socket
//
$errno;
$errstr;
switch($this->type) {
case Net_DNS2_Socket::SOCK_STREAM:
if (Net_DNS2::isIPv4($this->host) == true) {
$this->sock = @stream_socket_client(
'tcp://' . $this->host . ':' . $this->port,
$errno, $errstr, $this->timeout,
STREAM_CLIENT_CONNECT, $this->_context
);
} else if (Net_DNS2::isIPv6($this->host) == true) {
$this->sock = @stream_socket_client(
'tcp://[' . $this->host . ']:' . $this->port,
$errno, $errstr, $this->timeout,
STREAM_CLIENT_CONNECT, $this->_context
);
} else {
$this->last_error = 'invalid address type: ' . $this->host;
return false;
}
break;
case Net_DNS2_Socket::SOCK_DGRAM:
if (Net_DNS2::isIPv4($this->host) == true) {
$this->sock = @stream_socket_client(
'udp://' . $this->host . ':' . $this->port,
$errno, $errstr, $this->timeout,
STREAM_CLIENT_CONNECT, $this->_context
);
} else if (Net_DNS2::isIPv6($this->host) == true) {
$this->sock = @stream_socket_client(
'udp://[' . $this->host . ']:' . $this->port,
$errno, $errstr, $this->timeout,
STREAM_CLIENT_CONNECT, $this->_context
);
} else {
$this->last_error = 'invalid address type: ' . $this->host;
return false;
}
break;
default:
$this->last_error = 'Invalid socket type: ' . $this->type;
return false;
}
if ($this->sock === false) {
$this->last_error = $errstr;
return false;
}
//
// set it to non-blocking and set the timeout
//
@stream_set_blocking($this->sock, 0);
@stream_set_timeout($this->sock, $this->timeout);
return true;
}
/**
* closes a socket connection to the DNS server
*
* @return boolean
* @access public
*
*/
public function close()
{
if (is_resource($this->sock) === true) {
@fclose($this->sock);
}
return true;
}
/**
* writes the given string to the DNS server socket
*
* @param string $data a binary packed DNS packet
*
* @return boolean
* @access public
*
*/
public function write($data)
{
$length = strlen($data);
if ($length == 0) {
$this->last_error = 'empty data on write()';
return false;
}
$read = null;
$write = array($this->sock);
$except = null;
//
// select on write
//
$result = stream_select($read, $write, $except, $this->timeout);
if ($result === false) {
$this->last_error = 'failed on write select()';
return false;
} else if ($result == 0) {
$this->last_error = 'timeout on write select()';
return false;
}
//
// if it's a TCP socket, then we need to packet and send the length of the
// data as the first 16bit of data.
//
if ($this->type == Net_DNS2_Socket::SOCK_STREAM) {
$s = chr($length >> 8) . chr($length);
if (@fwrite($this->sock, $s) === false) {
$this->last_error = 'failed to fwrite() 16bit length';
return false;
}
}
//
// write the data to the socket
//
$size = @fwrite($this->sock, $data);
if ( ($size === false) || ($size != $length) ) {
$this->last_error = 'failed to fwrite() packet';
return false;
}
return true;
}
/**
* reads a response from a DNS server
*
* @param integer &$size the size of the DNS packet read is passed back
*
* @return mixed returns the data on success and false on error
* @access public
*
*/
public function read(&$size, $max_size)
{
$read = array($this->sock);
$write = null;
$except = null;
//
// make sure our socket is non-blocking
//
@stream_set_blocking($this->sock, 0);
//
// select on read
//
$result = stream_select($read, $write, $except, $this->timeout);
if ($result === false) {
$this->last_error = 'error on read select()';
return false;
} else if ($result == 0) {
$this->last_error = 'timeout on read select()';
return false;
}
$data = '';
$length = $max_size;
//
// if it's a TCP socket, then the first two bytes is the length of the DNS
// packet- we need to read that off first, then use that value for the
// packet read.
//
if ($this->type == Net_DNS2_Socket::SOCK_STREAM) {
if (($data = fread($this->sock, 2)) === false) {
$this->last_error = 'failed on fread() for data length';
return false;
}
$length = ord($data[0]) << 8 | ord($data[1]);
if ($length < Net_DNS2_Lookups::DNS_HEADER_SIZE) {
return false;
}
}
//
// at this point, we know that there is data on the socket to be read,
// because we've already extracted the length from the first two bytes.
//
// so the easiest thing to do, is just turn off socket blocking, and
// wait for the data.
//
@stream_set_blocking($this->sock, 1);
//
// read the data from the socket
//
$data = '';
//
// the streams socket is weird for TCP sockets; it doesn't seem to always
// return all the data properly; but the looping code I added broke UDP
// packets- my fault-
//
// the sockets library works much better.
//
if ($this->type == Net_DNS2_Socket::SOCK_STREAM) {
$chunk = '';
$chunk_size = $length;
//
// loop so we make sure we read all the data
//
while (1) {
$chunk = fread($this->sock, $chunk_size);
if ($chunk === false) {
$this->last_error = 'failed on fread() for data';
return false;
}
$data .= $chunk;
$chunk_size -= strlen($chunk);
if (strlen($data) >= $length) {
break;
}
}
} else {
//
// if it's UDP, it's a single fixed-size frame, and the streams library
// doesn't seem to have a problem reading it.
//
$data = fread($this->sock, $length);
if ($length === false) {
$this->last_error = 'failed on fread() for data';
return false;
}
}
$size = strlen($data);
return $data;
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>

View File

@@ -0,0 +1,16 @@
<?php
/**
* Class to provide IPv4 calculations
*
* PHP versions 4, 5 and 7
*
* @link https://stackoverflow.com/questions/594112/matching-an-ip-to-a-cidr-mask-in-php-5#answer-594134
*/
class Net_IPv4 {
public static function ipInNetwork( $ip, $cidr ) {
list( $subnet, $bitmask ) = explode( '/', $cidr );
$bitmask = -1 << ( 32 - $bitmask );
return ( ip2long( $ip ) & $bitmask ) === ( ip2long( $subnet ) & $bitmask );
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,33 @@
Net_DNS2 - DNS Library for handling lookups and updates.
Copyright (c) 2010-2013, Mike Pultz <mike@mikepultz.com>.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Mike Pultz nor the names of his contributors
may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

View File

@@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2015 Paragon Initiative Enterprises
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,195 @@
<?php
/**
* Random_* Compatibility Library
* for using the new PHP 7 random_* API in PHP 5 projects
*
* The MIT License (MIT)
*
* Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
if (!is_callable('RandomCompat_strlen')) {
if (
defined('MB_OVERLOAD_STRING')
&& /* @codingStandardsIgnoreLine */ // phpcs:ignore
((int) ini_get('mbstring.func_overload')) & MB_OVERLOAD_STRING
) {
/**
* strlen() implementation that isn't brittle to mbstring.func_overload
*
* This version uses mb_strlen() in '8bit' mode to treat strings as raw
* binary rather than UTF-8, ISO-8859-1, etc
*
* @param string $binary_string
*
* @throws TypeError
*
* @return int
*/
function RandomCompat_strlen($binary_string)
{
if (!is_string($binary_string)) {
throw new TypeError(
'RandomCompat_strlen() expects a string'
);
}
return (int) mb_strlen($binary_string, '8bit');
}
} else {
/**
* strlen() implementation that isn't brittle to mbstring.func_overload
*
* This version just used the default strlen()
*
* @param string $binary_string
*
* @throws TypeError
*
* @return int
*/
function RandomCompat_strlen($binary_string)
{
if (!is_string($binary_string)) {
throw new TypeError(
'RandomCompat_strlen() expects a string'
);
}
return (int) strlen($binary_string);
}
}
}
if (!is_callable('RandomCompat_substr')) {
if (
defined('MB_OVERLOAD_STRING')
&& /* @codingStandardsIgnoreLine */ // phpcs:ignore
((int) ini_get('mbstring.func_overload')) & MB_OVERLOAD_STRING
) {
/**
* substr() implementation that isn't brittle to mbstring.func_overload
*
* This version uses mb_substr() in '8bit' mode to treat strings as raw
* binary rather than UTF-8, ISO-8859-1, etc
*
* @param string $binary_string
* @param int $start
* @param int|null $length (optional)
*
* @throws TypeError
*
* @return string
*/
function RandomCompat_substr($binary_string, $start, $length = null)
{
if (!is_string($binary_string)) {
throw new TypeError(
'RandomCompat_substr(): First argument should be a string'
);
}
if (!is_int($start)) {
throw new TypeError(
'RandomCompat_substr(): Second argument should be an integer'
);
}
if ($length === null) {
/**
* mb_substr($str, 0, NULL, '8bit') returns an empty string on
* PHP 5.3, so we have to find the length ourselves.
*/
/** @var int $length */
$length = RandomCompat_strlen($binary_string) - $start;
} elseif (!is_int($length)) {
throw new TypeError(
'RandomCompat_substr(): Third argument should be an integer, or omitted'
);
}
// Consistency with PHP's behavior
if ($start === RandomCompat_strlen($binary_string) && $length === 0) {
return '';
}
if ($start > RandomCompat_strlen($binary_string)) {
return '';
}
return (string) mb_substr(
(string) $binary_string,
(int) $start,
(int) $length,
'8bit'
);
}
} else {
/**
* substr() implementation that isn't brittle to mbstring.func_overload
*
* This version just uses the default substr()
*
* @param string $binary_string
* @param int $start
* @param int|null $length (optional)
*
* @throws TypeError
*
* @return string
*/
function RandomCompat_substr($binary_string, $start, $length = null)
{
if (!is_string($binary_string)) {
throw new TypeError(
'RandomCompat_substr(): First argument should be a string'
);
}
if (!is_int($start)) {
throw new TypeError(
'RandomCompat_substr(): Second argument should be an integer'
);
}
if ($length !== null) {
if (!is_int($length)) {
throw new TypeError(
'RandomCompat_substr(): Third argument should be an integer, or omitted'
);
}
return (string) substr(
(string )$binary_string,
(int) $start,
(int) $length
);
}
return (string) substr(
(string) $binary_string,
(int) $start
);
}
}
}

View File

@@ -0,0 +1,77 @@
<?php
/**
* Random_* Compatibility Library
* for using the new PHP 7 random_* API in PHP 5 projects
*
* The MIT License (MIT)
*
* Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
if (!is_callable('RandomCompat_intval')) {
/**
* Cast to an integer if we can, safely.
*
* If you pass it a float in the range (~PHP_INT_MAX, PHP_INT_MAX)
* (non-inclusive), it will sanely cast it to an int. If you it's equal to
* ~PHP_INT_MAX or PHP_INT_MAX, we let it fail as not an integer. Floats
* lose precision, so the <= and => operators might accidentally let a float
* through.
*
* @param int|float $number The number we want to convert to an int
* @param bool $fail_open Set to true to not throw an exception
*
* @return float|int
* @psalm-suppress InvalidReturnType
*
* @throws TypeError
*/
function RandomCompat_intval($number, $fail_open = false)
{
if (is_int($number) || is_float($number)) {
$number += 0;
} elseif (is_numeric($number)) {
/** @psalm-suppress InvalidOperand */
$number += 0;
}
/** @var int|float $number */
if (
is_float($number)
&&
$number > ~PHP_INT_MAX
&&
$number < PHP_INT_MAX
) {
$number = (int) $number;
}
if (is_int($number)) {
return (int) $number;
} elseif (!$fail_open) {
throw new TypeError(
'Expected an integer.'
);
}
return $number;
}
}

View File

@@ -0,0 +1,49 @@
<?php
/**
* Random_* Compatibility Library
* for using the new PHP 7 random_* API in PHP 5 projects
*
* The MIT License (MIT)
*
* Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
if (!class_exists('Error', false)) {
// We can't really avoid making this extend Exception in PHP 5.
class Error extends Exception
{
}
}
if (!class_exists('TypeError', false)) {
if (is_subclass_of('Error', 'Exception')) {
class TypeError extends Error
{
}
} else {
class TypeError extends Exception
{
}
}
}

View File

@@ -0,0 +1,225 @@
<?php
/**
* Random_* Compatibility Library
* for using the new PHP 7 random_* API in PHP 5 projects
*
* @version 2.0.17
* @released 2018-07-04
*
* The MIT License (MIT)
*
* Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
if (!defined('PHP_VERSION_ID')) {
// This constant was introduced in PHP 5.2.7
$RandomCompatversion = array_map('intval', explode('.', PHP_VERSION));
define(
'PHP_VERSION_ID',
$RandomCompatversion[0] * 10000
+ $RandomCompatversion[1] * 100
+ $RandomCompatversion[2]
);
$RandomCompatversion = null;
}
/**
* PHP 7.0.0 and newer have these functions natively.
*/
if (PHP_VERSION_ID >= 70000) {
return;
}
if (!defined('RANDOM_COMPAT_READ_BUFFER')) {
define('RANDOM_COMPAT_READ_BUFFER', 8);
}
$RandomCompatDIR = dirname(__FILE__);
require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'byte_safe_strings.php';
require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'cast_to_int.php';
require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'error_polyfill.php';
if (!is_callable('random_bytes')) {
/**
* PHP 5.2.0 - 5.6.x way to implement random_bytes()
*
* We use conditional statements here to define the function in accordance
* to the operating environment. It's a micro-optimization.
*
* In order of preference:
* 1. Use libsodium if available.
* 2. fread() /dev/urandom if available (never on Windows)
* 3. mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM)
* 4. COM('CAPICOM.Utilities.1')->GetRandom()
*
* See RATIONALE.md for our reasoning behind this particular order
*/
if (extension_loaded('libsodium')) {
// See random_bytes_libsodium.php
if (PHP_VERSION_ID >= 50300 && is_callable('\\Sodium\\randombytes_buf')) {
require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'random_bytes_libsodium.php';
} elseif (method_exists('Sodium', 'randombytes_buf')) {
require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'random_bytes_libsodium_legacy.php';
}
}
/**
* Reading directly from /dev/urandom:
*/
if (DIRECTORY_SEPARATOR === '/') {
// DIRECTORY_SEPARATOR === '/' on Unix-like OSes -- this is a fast
// way to exclude Windows.
$RandomCompatUrandom = true;
$RandomCompat_basedir = ini_get('open_basedir');
if (!empty($RandomCompat_basedir)) {
$RandomCompat_open_basedir = explode(
PATH_SEPARATOR,
strtolower($RandomCompat_basedir)
);
$RandomCompatUrandom = (array() !== array_intersect(
array('/dev', '/dev/', '/dev/urandom'),
$RandomCompat_open_basedir
));
$RandomCompat_open_basedir = null;
}
if (
!is_callable('random_bytes')
&&
$RandomCompatUrandom
&&
@is_readable('/dev/urandom')
) {
// Error suppression on is_readable() in case of an open_basedir
// or safe_mode failure. All we care about is whether or not we
// can read it at this point. If the PHP environment is going to
// panic over trying to see if the file can be read in the first
// place, that is not helpful to us here.
// See random_bytes_dev_urandom.php
require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'random_bytes_dev_urandom.php';
}
// Unset variables after use
$RandomCompat_basedir = null;
} else {
$RandomCompatUrandom = false;
}
/**
* mcrypt_create_iv()
*
* We only want to use mcypt_create_iv() if:
*
* - random_bytes() hasn't already been defined
* - the mcrypt extensions is loaded
* - One of these two conditions is true:
* - We're on Windows (DIRECTORY_SEPARATOR !== '/')
* - We're not on Windows and /dev/urandom is readabale
* (i.e. we're not in a chroot jail)
* - Special case:
* - If we're not on Windows, but the PHP version is between
* 5.6.10 and 5.6.12, we don't want to use mcrypt. It will
* hang indefinitely. This is bad.
* - If we're on Windows, we want to use PHP >= 5.3.7 or else
* we get insufficient entropy errors.
*/
if (
!is_callable('random_bytes')
&&
// Windows on PHP < 5.3.7 is broken, but non-Windows is not known to be.
(DIRECTORY_SEPARATOR === '/' || PHP_VERSION_ID >= 50307)
&&
// Prevent this code from hanging indefinitely on non-Windows;
// see https://bugs.php.net/bug.php?id=69833
(
DIRECTORY_SEPARATOR !== '/' ||
(PHP_VERSION_ID <= 50609 || PHP_VERSION_ID >= 50613)
)
&&
extension_loaded('mcrypt')
) {
// See random_bytes_mcrypt.php
require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'random_bytes_mcrypt.php';
}
$RandomCompatUrandom = null;
/**
* This is a Windows-specific fallback, for when the mcrypt extension
* isn't loaded.
*/
if (
!is_callable('random_bytes')
&&
extension_loaded('com_dotnet')
&&
class_exists('COM')
) {
$RandomCompat_disabled_classes = preg_split(
'#\s*,\s*#',
strtolower(ini_get('disable_classes'))
);
if (!in_array('com', $RandomCompat_disabled_classes)) {
try {
$RandomCompatCOMtest = new COM('CAPICOM.Utilities.1');
if (method_exists($RandomCompatCOMtest, 'GetRandom')) {
// See random_bytes_com_dotnet.php
require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'random_bytes_com_dotnet.php';
}
} catch (com_exception $e) {
// Don't try to use it.
}
}
$RandomCompat_disabled_classes = null;
$RandomCompatCOMtest = null;
}
/**
* throw new Exception
*/
if (!is_callable('random_bytes')) {
/**
* We don't have any more options, so let's throw an exception right now
* and hope the developer won't let it fail silently.
*
* @param mixed $length
* @psalm-suppress InvalidReturnType
* @throws Exception
* @return string
*/
function random_bytes($length)
{
unset($length); // Suppress "variable not used" warnings.
throw new Exception(
'There is no suitable CSPRNG installed on your system'
);
return '';
}
}
}
if (!is_callable('random_int')) {
require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'random_int.php';
}
$RandomCompatDIR = null;

View File

@@ -0,0 +1,91 @@
<?php
/**
* Random_* Compatibility Library
* for using the new PHP 7 random_* API in PHP 5 projects
*
* The MIT License (MIT)
*
* Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
if (!is_callable('random_bytes')) {
/**
* Windows with PHP < 5.3.0 will not have the function
* openssl_random_pseudo_bytes() available, so let's use
* CAPICOM to work around this deficiency.
*
* @param int $bytes
*
* @throws Exception
*
* @return string
*/
function random_bytes($bytes)
{
try {
/** @var int $bytes */
$bytes = RandomCompat_intval($bytes);
} catch (TypeError $ex) {
throw new TypeError(
'random_bytes(): $bytes must be an integer'
);
}
if ($bytes < 1) {
throw new Error(
'Length must be greater than 0'
);
}
/** @var string $buf */
$buf = '';
if (!class_exists('COM')) {
throw new Error(
'COM does not exist'
);
}
/** @var COM $util */
$util = new COM('CAPICOM.Utilities.1');
$execCount = 0;
/**
* Let's not let it loop forever. If we run N times and fail to
* get N bytes of random data, then CAPICOM has failed us.
*/
do {
$buf .= base64_decode((string) $util->GetRandom($bytes, 0));
if (RandomCompat_strlen($buf) >= $bytes) {
/**
* Return our random entropy buffer here:
*/
return (string) RandomCompat_substr($buf, 0, $bytes);
}
++$execCount;
} while ($execCount < $bytes);
/**
* If we reach here, PHP has failed us.
*/
throw new Exception(
'Could not gather sufficient random data'
);
}
}

View File

@@ -0,0 +1,190 @@
<?php
/**
* Random_* Compatibility Library
* for using the new PHP 7 random_* API in PHP 5 projects
*
* The MIT License (MIT)
*
* Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
if (!defined('RANDOM_COMPAT_READ_BUFFER')) {
define('RANDOM_COMPAT_READ_BUFFER', 8);
}
if (!is_callable('random_bytes')) {
/**
* Unless open_basedir is enabled, use /dev/urandom for
* random numbers in accordance with best practices
*
* Why we use /dev/urandom and not /dev/random
* @ref https://www.2uo.de/myths-about-urandom
* @ref http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers
*
* @param int $bytes
*
* @throws Exception
*
* @return string
*/
function random_bytes($bytes)
{
/** @var resource $fp */
static $fp = null;
/**
* This block should only be run once
*/
if (empty($fp)) {
/**
* We don't want to ever read C:\dev\random, only /dev/urandom on
* Unix-like operating systems. While we guard against this
* condition in random.php, it doesn't hurt to be defensive in depth
* here.
*
* To that end, we only try to open /dev/urandom if we're on a Unix-
* like operating system (which means the directory separator is set
* to "/" not "\".
*/
if (DIRECTORY_SEPARATOR === '/') {
if (!is_readable('/dev/urandom')) {
throw new Exception(
'Environment misconfiguration: ' .
'/dev/urandom cannot be read.'
);
}
/**
* We use /dev/urandom if it is a char device.
* We never fall back to /dev/random
*/
/** @var resource|bool $fp */
$fp = fopen('/dev/urandom', 'rb');
if (is_resource($fp)) {
/** @var array<string, int> $st */
$st = fstat($fp);
if (($st['mode'] & 0170000) !== 020000) {
fclose($fp);
$fp = false;
}
}
}
if (is_resource($fp)) {
/**
* stream_set_read_buffer() does not exist in HHVM
*
* If we don't set the stream's read buffer to 0, PHP will
* internally buffer 8192 bytes, which can waste entropy
*
* stream_set_read_buffer returns 0 on success
*/
if (is_callable('stream_set_read_buffer')) {
stream_set_read_buffer($fp, RANDOM_COMPAT_READ_BUFFER);
}
if (is_callable('stream_set_chunk_size')) {
stream_set_chunk_size($fp, RANDOM_COMPAT_READ_BUFFER);
}
}
}
try {
/** @var int $bytes */
$bytes = RandomCompat_intval($bytes);
} catch (TypeError $ex) {
throw new TypeError(
'random_bytes(): $bytes must be an integer'
);
}
if ($bytes < 1) {
throw new Error(
'Length must be greater than 0'
);
}
/**
* This if() block only runs if we managed to open a file handle
*
* It does not belong in an else {} block, because the above
* if (empty($fp)) line is logic that should only be run once per
* page load.
*/
if (is_resource($fp)) {
/**
* @var int
*/
$remaining = $bytes;
/**
* @var string|bool
*/
$buf = '';
/**
* We use fread() in a loop to protect against partial reads
*/
do {
/**
* @var string|bool
*/
$read = fread($fp, $remaining);
if (!is_string($read)) {
/**
* We cannot safely read from the file. Exit the
* do-while loop and trigger the exception condition
*
* @var string|bool
*/
$buf = false;
break;
}
/**
* Decrease the number of bytes returned from remaining
*/
$remaining -= RandomCompat_strlen($read);
/**
* @var string $buf
*/
$buf .= $read;
} while ($remaining > 0);
/**
* Is our result valid?
* @var string|bool $buf
*/
if (is_string($buf)) {
if (RandomCompat_strlen($buf) === $bytes) {
/**
* Return our random entropy buffer here:
*/
return $buf;
}
}
}
/**
* If we reach here, PHP has failed us.
*/
throw new Exception(
'Error reading from source device'
);
}
}

View File

@@ -0,0 +1,91 @@
<?php
/**
* Random_* Compatibility Library
* for using the new PHP 7 random_* API in PHP 5 projects
*
* The MIT License (MIT)
*
* Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
if (!is_callable('random_bytes')) {
/**
* If the libsodium PHP extension is loaded, we'll use it above any other
* solution.
*
* libsodium-php project:
* @ref https://github.com/jedisct1/libsodium-php
*
* @param int $bytes
*
* @throws Exception
*
* @return string
*/
function random_bytes($bytes)
{
try {
/** @var int $bytes */
$bytes = RandomCompat_intval($bytes);
} catch (TypeError $ex) {
throw new TypeError(
'random_bytes(): $bytes must be an integer'
);
}
if ($bytes < 1) {
throw new Error(
'Length must be greater than 0'
);
}
/**
* \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be
* generated in one invocation.
*/
/** @var string|bool $buf */
if ($bytes > 2147483647) {
$buf = '';
for ($i = 0; $i < $bytes; $i += 1073741824) {
$n = ($bytes - $i) > 1073741824
? 1073741824
: $bytes - $i;
$buf .= \Sodium\randombytes_buf($n);
}
} else {
/** @var string|bool $buf */
$buf = \Sodium\randombytes_buf($bytes);
}
if (is_string($buf)) {
if (RandomCompat_strlen($buf) === $bytes) {
return $buf;
}
}
/**
* If we reach here, PHP has failed us.
*/
throw new Exception(
'Could not gather sufficient random data'
);
}
}

View File

@@ -0,0 +1,93 @@
<?php
/**
* Random_* Compatibility Library
* for using the new PHP 7 random_* API in PHP 5 projects
*
* The MIT License (MIT)
*
* Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
if (!is_callable('random_bytes')) {
/**
* If the libsodium PHP extension is loaded, we'll use it above any other
* solution.
*
* libsodium-php project:
* @ref https://github.com/jedisct1/libsodium-php
*
* @param int $bytes
*
* @throws Exception
*
* @return string
*/
function random_bytes($bytes)
{
try {
/** @var int $bytes */
$bytes = RandomCompat_intval($bytes);
} catch (TypeError $ex) {
throw new TypeError(
'random_bytes(): $bytes must be an integer'
);
}
if ($bytes < 1) {
throw new Error(
'Length must be greater than 0'
);
}
/**
* @var string
*/
$buf = '';
/**
* \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be
* generated in one invocation.
*/
if ($bytes > 2147483647) {
for ($i = 0; $i < $bytes; $i += 1073741824) {
$n = ($bytes - $i) > 1073741824
? 1073741824
: $bytes - $i;
$buf .= Sodium::randombytes_buf((int) $n);
}
} else {
$buf .= Sodium::randombytes_buf((int) $bytes);
}
if (is_string($buf)) {
if (RandomCompat_strlen($buf) === $bytes) {
return $buf;
}
}
/**
* If we reach here, PHP has failed us.
*/
throw new Exception(
'Could not gather sufficient random data'
);
}
}

View File

@@ -0,0 +1,79 @@
<?php
/**
* Random_* Compatibility Library
* for using the new PHP 7 random_* API in PHP 5 projects
*
* The MIT License (MIT)
*
* Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
if (!is_callable('random_bytes')) {
/**
* Powered by ext/mcrypt (and thankfully NOT libmcrypt)
*
* @ref https://bugs.php.net/bug.php?id=55169
* @ref https://github.com/php/php-src/blob/c568ffe5171d942161fc8dda066bce844bdef676/ext/mcrypt/mcrypt.c#L1321-L1386
*
* @param int $bytes
*
* @throws Exception
*
* @return string
*/
function random_bytes($bytes)
{
try {
/** @var int $bytes */
$bytes = RandomCompat_intval($bytes);
} catch (TypeError $ex) {
throw new TypeError(
'random_bytes(): $bytes must be an integer'
);
}
if ($bytes < 1) {
throw new Error(
'Length must be greater than 0'
);
}
/** @var string|bool $buf */ /* @codingStandardsIgnoreLine */ // phpcs:ignore
$buf = @mcrypt_create_iv((int) $bytes, (int) MCRYPT_DEV_URANDOM);
if (
is_string($buf)
&&
RandomCompat_strlen($buf) === $bytes
) {
/**
* Return our random entropy buffer here:
*/
return $buf;
}
/**
* If we reach here, PHP has failed us.
*/
throw new Exception(
'Could not gather sufficient random data'
);
}
}

View File

@@ -0,0 +1,204 @@
<?php
if (!is_callable('random_int')) {
/**
* Random_* Compatibility Library
* for using the new PHP 7 random_* API in PHP 5 projects
*
* The MIT License (MIT)
*
* Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Fetch a random integer between $min and $max inclusive
*
* @param int $min
* @param int $max
*
* @throws Exception
*
* @return int
*/
function random_int($min, $max)
{
/**
* Type and input logic checks
*
* If you pass it a float in the range (~PHP_INT_MAX, PHP_INT_MAX)
* (non-inclusive), it will sanely cast it to an int. If you it's equal to
* ~PHP_INT_MAX or PHP_INT_MAX, we let it fail as not an integer. Floats
* lose precision, so the <= and => operators might accidentally let a float
* through.
*/
try {
/** @var int $min */
$min = RandomCompat_intval($min);
} catch (TypeError $ex) {
throw new TypeError(
'random_int(): $min must be an integer'
);
}
try {
/** @var int $max */
$max = RandomCompat_intval($max);
} catch (TypeError $ex) {
throw new TypeError(
'random_int(): $max must be an integer'
);
}
/**
* Now that we've verified our weak typing system has given us an integer,
* let's validate the logic then we can move forward with generating random
* integers along a given range.
*/
if ($min > $max) {
throw new Error(
'Minimum value must be less than or equal to the maximum value'
);
}
if ($max === $min) {
return (int) $min;
}
/**
* Initialize variables to 0
*
* We want to store:
* $bytes => the number of random bytes we need
* $mask => an integer bitmask (for use with the &) operator
* so we can minimize the number of discards
*/
$attempts = $bits = $bytes = $mask = $valueShift = 0;
/** @var int $attempts */
/** @var int $bits */
/** @var int $bytes */
/** @var int $mask */
/** @var int $valueShift */
/**
* At this point, $range is a positive number greater than 0. It might
* overflow, however, if $max - $min > PHP_INT_MAX. PHP will cast it to
* a float and we will lose some precision.
*
* @var int|float $range
*/
$range = $max - $min;
/**
* Test for integer overflow:
*/
if (!is_int($range)) {
/**
* Still safely calculate wider ranges.
* Provided by @CodesInChaos, @oittaa
*
* @ref https://gist.github.com/CodesInChaos/03f9ea0b58e8b2b8d435
*
* We use ~0 as a mask in this case because it generates all 1s
*
* @ref https://eval.in/400356 (32-bit)
* @ref http://3v4l.org/XX9r5 (64-bit)
*/
$bytes = PHP_INT_SIZE;
/** @var int $mask */
$mask = ~0;
} else {
/**
* $bits is effectively ceil(log($range, 2)) without dealing with
* type juggling
*/
while ($range > 0) {
if ($bits % 8 === 0) {
++$bytes;
}
++$bits;
$range >>= 1;
/** @var int $mask */
$mask = $mask << 1 | 1;
}
$valueShift = $min;
}
/** @var int $val */
$val = 0;
/**
* Now that we have our parameters set up, let's begin generating
* random integers until one falls between $min and $max
*/
/** @psalm-suppress RedundantCondition */
do {
/**
* The rejection probability is at most 0.5, so this corresponds
* to a failure probability of 2^-128 for a working RNG
*/
if ($attempts > 128) {
throw new Exception(
'random_int: RNG is broken - too many rejections'
);
}
/**
* Let's grab the necessary number of random bytes
*/
$randomByteString = random_bytes($bytes);
/**
* Let's turn $randomByteString into an integer
*
* This uses bitwise operators (<< and |) to build an integer
* out of the values extracted from ord()
*
* Example: [9F] | [6D] | [32] | [0C] =>
* 159 + 27904 + 3276800 + 201326592 =>
* 204631455
*/
$val &= 0;
for ($i = 0; $i < $bytes; ++$i) {
$val |= ord($randomByteString[$i]) << ($i * 8);
}
/** @var int $val */
/**
* Apply mask
*/
$val &= $mask;
$val += $valueShift;
++$attempts;
/**
* If $val overflows to a floating point number,
* ... or is larger than $max,
* ... or smaller than $min,
* then try again.
*/
} while (!is_int($val) || $val > $max || $val < $min);
return (int) $val;
}
}