2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5
* DNS Library for handling lookups and updates.
9
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
10
* All rights reserved.
12
* Redistribution and use in source and binary forms, with or without
13
* modification, are permitted provided that the following conditions
16
* * Redistributions of source code must retain the above copyright
17
* notice, this list of conditions and the following disclaimer.
19
* * Redistributions in binary form must reproduce the above copyright
20
* notice, this list of conditions and the following disclaimer in
21
* the documentation and/or other materials provided with the
24
* * Neither the name of Mike Pultz nor the names of his contributors
25
* may be used to endorse or promote products derived from this
26
* software without specific prior written permission.
28
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
37
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39
* POSSIBILITY OF SUCH DAMAGE.
41
* @category Networking
43
* @author Mike Pultz <mike@mikepultz.com>
44
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
45
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
46
* @version SVN: $Id: Updater.php 198 2013-05-26 05:05:22Z mike.pultz $
47
* @link http://pear.php.net/package/Net_DNS2
48
* @since File available since Release 0.6.0
53
* The main dynamic DNS updater class.
55
* This class provices functions to handle all defined dynamic DNS update
56
* requests as defined by RFC 2136.
58
* This is separate from the Net_DNS2_Resolver class, as while the underlying
59
* protocol is the same, the functionality is completely different.
61
* Generally, query (recursive) lookups are done against caching server, while
62
* update requests are done against authoratative servers.
64
* @category Networking
66
* @author Mike Pultz <mike@mikepultz.com>
67
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
68
* @link http://pear.php.net/package/Net_DNS2
72
class Net_DNS2_Updater extends Net_DNS2
75
* a Net_DNS2_Packet_Request object used for the update request
80
* Constructor - builds a new Net_DNS2_Updater objected used for doing
83
* @param string $zone the domain name to use for DNS updates
84
* @param mixed $options an array of config options or null
86
* @throws Net_DNS2_Exception
90
public function __construct($zone, array $options = null)
92
parent::__construct($options);
97
$this->_packet = new Net_DNS2_Packet_Request(
98
strtolower(trim($zone, " \n\r\t.")), 'SOA', 'IN'
102
// make sure the opcode on the packet is set to UPDATE
104
$this->_packet->header->opcode = Net_DNS2_Lookups::OPCODE_UPDATE;
108
* checks that the given name matches the name for the zone we're updating
110
* @param string $name The name to be checked.
113
* @throws Net_DNS2_Exception
117
private function _checkName($name)
119
if (!preg_match('/' . $this->_packet->question[0]->qname . '$/', $name)) {
121
throw new Net_DNS2_Exception(
122
'name provided (' . $name . ') does not match zone name (' .
123
$this->_packet->question[0]->qname . ')',
124
Net_DNS2_Lookups::E_PACKET_INVALID
132
* add a signature to the request for authentication
134
* @param string $keyname the key name to use for the TSIG RR
135
* @param string $signature the key to sign the request.
139
* @see Net_DNS2::signTSIG()
140
* @deprecated function deprecated in 1.1.0
143
public function signature($keyname, $signature)
145
return $this->signTSIG($keyname, $signature);
149
* 2.5.1 - Add To An RRset
151
* RRs are added to the Update Section whose NAME, TYPE, TTL, RDLENGTH
152
* and RDATA are those being added, and CLASS is the same as the zone
153
* class. Any duplicate RRs will be silently ignored by the primary
156
* @param Net_DNS2_RR $rr the Net_DNS2_RR object to be added to the zone
159
* @throws Net_DNS2_Exception
163
public function add(Net_DNS2_RR $rr)
165
$this->_checkName($rr->name);
168
// add the RR to the "update" section
170
if (!in_array($rr, $this->_packet->authority)) {
171
$this->_packet->authority[] = $rr;
178
* 2.5.4 - Delete An RR From An RRset
180
* RRs to be deleted are added to the Update Section. The NAME, TYPE,
181
* RDLENGTH and RDATA must match the RR being deleted. TTL must be
182
* specified as zero (0) and will otherwise be ignored by the primary
183
* master. CLASS must be specified as NONE to distinguish this from an
184
* RR addition. If no such RRs exist, then this Update RR will be
185
* silently ignored by the primary master.
187
* @param Net_DNS2_RR $rr the Net_DNS2_RR object to be deleted from the zone
190
* @throws Net_DNS2_Exception
194
public function delete(Net_DNS2_RR $rr)
196
$this->_checkName($rr->name);
202
// add the RR to the "update" section
204
if (!in_array($rr, $this->_packet->authority)) {
205
$this->_packet->authority[] = $rr;
212
* 2.5.2 - Delete An RRset
214
* One RR is added to the Update Section whose NAME and TYPE are those
215
* of the RRset to be deleted. TTL must be specified as zero (0) and is
216
* otherwise not used by the primary master. CLASS must be specified as
217
* ANY. RDLENGTH must be zero (0) and RDATA must therefore be empty.
218
* If no such RRset exists, then this Update RR will be silently ignored
219
* by the primary master
221
* @param string $name the RR name to be removed from the zone
222
* @param string $type the RR type to be removed from the zone
225
* @throws Net_DNS2_Exception
229
public function deleteAny($name, $type)
231
$this->_checkName($name);
233
$class = Net_DNS2_Lookups::$rr_types_id_to_class[
234
Net_DNS2_Lookups::$rr_types_by_name[$type]
236
if (!isset($class)) {
238
throw new Net_DNS2_Exception(
239
'unknown or un-supported resource record type: ' . $type,
240
Net_DNS2_Lookups::E_RR_INVALID
253
// add the RR to the "update" section
255
if (!in_array($rr, $this->_packet->authority)) {
256
$this->_packet->authority[] = $rr;
263
* 2.5.3 - Delete All RRsets From A Name
265
* One RR is added to the Update Section whose NAME is that of the name
266
* to be cleansed of RRsets. TYPE must be specified as ANY. TTL must
267
* be specified as zero (0) and is otherwise not used by the primary
268
* master. CLASS must be specified as ANY. RDLENGTH must be zero (0)
269
* and RDATA must therefore be empty. If no such RRsets exist, then
270
* this Update RR will be silently ignored by the primary master.
272
* @param string $name the RR name to be removed from the zone
275
* @throws Net_DNS2_Exception
279
public function deleteAll($name)
281
$this->_checkName($name);
284
// the Net_DNS2_RR_ANY class is just an empty stub class used for these
287
$rr = new Net_DNS2_RR_ANY;
297
// add the RR to the "update" section
299
if (!in_array($rr, $this->_packet->authority)) {
300
$this->_packet->authority[] = $rr;
307
* 2.4.1 - RRset Exists (Value Independent)
309
* At least one RR with a specified NAME and TYPE (in the zone and class
310
* specified in the Zone Section) must exist.
312
* For this prerequisite, a requestor adds to the section a single RR
313
* whose NAME and TYPE are equal to that of the zone RRset whose
314
* existence is required. RDLENGTH is zero and RDATA is therefore
315
* empty. CLASS must be specified as ANY to differentiate this
316
* condition from that of an actual RR whose RDLENGTH is naturally zero
317
* (0) (e.g., NULL). TTL is specified as zero (0).
319
* @param string $name the RR name for the prerequisite
320
* @param string $type the RR type for the prerequisite
323
* @throws Net_DNS2_Exception
327
public function checkExists($name, $type)
329
$this->_checkName($name);
331
$class = Net_DNS2_Lookups::$rr_types_id_to_class[
332
Net_DNS2_Lookups::$rr_types_by_name[$type]
334
if (!isset($class)) {
336
throw new Net_DNS2_Exception(
337
'unknown or un-supported resource record type: ' . $type,
338
Net_DNS2_Lookups::E_RR_INVALID
351
// add the RR to the "prerequisite" section
353
if (!in_array($rr, $this->_packet->answer)) {
354
$this->_packet->answer[] = $rr;
361
* 2.4.2 - RRset Exists (Value Dependent)
363
* A set of RRs with a specified NAME and TYPE exists and has the same
364
* members with the same RDATAs as the RRset specified here in this
365
* section. While RRset ordering is undefined and therefore not
366
* significant to this comparison, the sets be identical in their
369
* For this prerequisite, a requestor adds to the section an entire
370
* RRset whose preexistence is required. NAME and TYPE are that of the
371
* RRset being denoted. CLASS is that of the zone. TTL must be
372
* specified as zero (0) and is ignored when comparing RRsets for
375
* @param Net_DNS2_RR $rr the RR object to be used as a prerequisite
378
* @throws Net_DNS2_Exception
382
public function checkValueExists(Net_DNS2_RR $rr)
384
$this->_checkName($rr->name);
389
// add the RR to the "prerequisite" section
391
if (!in_array($rr, $this->_packet->answer)) {
392
$this->_packet->answer[] = $rr;
399
* 2.4.3 - RRset Does Not Exist
401
* No RRs with a specified NAME and TYPE (in the zone and class denoted
402
* by the Zone Section) can exist.
404
* For this prerequisite, a requestor adds to the section a single RR
405
* whose NAME and TYPE are equal to that of the RRset whose nonexistence
406
* is required. The RDLENGTH of this record is zero (0), and RDATA
407
* field is therefore empty. CLASS must be specified as NONE in order
408
* to distinguish this condition from a valid RR whose RDLENGTH is
409
* naturally zero (0) (for example, the NULL RR). TTL must be specified
412
* @param string $name the RR name for the prerequisite
413
* @param string $type the RR type for the prerequisite
416
* @throws Net_DNS2_Exception
420
public function checkNotExists($name, $type)
422
$this->_checkName($name);
424
$class = Net_DNS2_Lookups::$rr_types_id_to_class[
425
Net_DNS2_Lookups::$rr_types_by_name[$type]
427
if (!isset($class)) {
429
throw new Net_DNS2_Exception(
430
'unknown or un-supported resource record type: ' . $type,
431
Net_DNS2_Lookups::E_RR_INVALID
444
// add the RR to the "prerequisite" section
446
if (!in_array($rr, $this->_packet->answer)) {
447
$this->_packet->answer[] = $rr;
454
* 2.4.4 - Name Is In Use
456
* Name is in use. At least one RR with a specified NAME (in the zone
457
* and class specified by the Zone Section) must exist. Note that this
458
* prerequisite is NOT satisfied by empty nonterminals.
460
* For this prerequisite, a requestor adds to the section a single RR
461
* whose NAME is equal to that of the name whose ownership of an RR is
462
* required. RDLENGTH is zero and RDATA is therefore empty. CLASS must
463
* be specified as ANY to differentiate this condition from that of an
464
* actual RR whose RDLENGTH is naturally zero (0) (e.g., NULL). TYPE
465
* must be specified as ANY to differentiate this case from that of an
466
* RRset existence test. TTL is specified as zero (0).
468
* @param string $name the RR name for the prerequisite
471
* @throws Net_DNS2_Exception
475
public function checkNameInUse($name)
477
$this->_checkName($name);
480
// the Net_DNS2_RR_ANY class is just an empty stub class used for these
483
$rr = new Net_DNS2_RR_ANY;
493
// add the RR to the "prerequisite" section
495
if (!in_array($rr, $this->_packet->answer)) {
496
$this->_packet->answer[] = $rr;
503
* 2.4.5 - Name Is Not In Use
505
* Name is not in use. No RR of any type is owned by a specified NAME.
506
* Note that this prerequisite IS satisfied by empty nonterminals.
508
* For this prerequisite, a requestor adds to the section a single RR
509
* whose NAME is equal to that of the name whose nonownership of any RRs
510
* is required. RDLENGTH is zero and RDATA is therefore empty. CLASS
511
* must be specified as NONE. TYPE must be specified as ANY. TTL must
512
* be specified as zero (0).
514
* @param string $name the RR name for the prerequisite
517
* @throws Net_DNS2_Exception
521
public function checkNameNotInUse($name)
523
$this->_checkName($name);
526
// the Net_DNS2_RR_ANY class is just an empty stub class used for these
529
$rr = new Net_DNS2_RR_ANY;
539
// add the RR to the "prerequisite" section
541
if (!in_array($rr, $this->_packet->answer)) {
542
$this->_packet->answer[] = $rr;
549
* returns the current internal packet object.
551
* @return Net_DNS2_Packet_Request
555
public function packet()
563
// check for an authentication method; either TSIG or SIG
565
if ( ($this->auth_signature instanceof Net_DNS2_RR_TSIG)
566
|| ($this->auth_signature instanceof Net_DNS2_RR_SIG)
568
$p->additional[] = $this->auth_signature;
574
$p->header->qdcount = count($p->question);
575
$p->header->ancount = count($p->answer);
576
$p->header->nscount = count($p->authority);
577
$p->header->arcount = count($p->additional);
583
* executes the update request with the object informaton
585
* @param Net_DNS2_Packet_Response &$response ref to the response object
588
* @throws Net_DNS2_Exception
592
public function update(&$response = null)
595
// make sure we have some name servers set
597
$this->checkServers(Net_DNS2::RESOLV_CONF);
600
// check for an authentication method; either TSIG or SIG
602
if ( ($this->auth_signature instanceof Net_DNS2_RR_TSIG)
603
|| ($this->auth_signature instanceof Net_DNS2_RR_SIG)
605
$this->_packet->additional[] = $this->auth_signature;
611
$this->_packet->header->qdcount = count($this->_packet->question);
612
$this->_packet->header->ancount = count($this->_packet->answer);
613
$this->_packet->header->nscount = count($this->_packet->authority);
614
$this->_packet->header->arcount = count($this->_packet->additional);
617
// make sure we have some data to send
619
if ( ($this->_packet->header->qdcount == 0)
620
|| ($this->_packet->header->nscount == 0)
622
throw new Net_DNS2_Exception(
623
'empty headers- nothing to send!',
624
Net_DNS2_Lookups::E_PACKET_INVALID
629
// send the packet and get back the response
631
$response = $this->sendPacket($this->_packet, $this->use_tcp);
634
// clear the internal packet so if we make another request, we don't have
635
// old data being sent.
637
$this->_packet->reset();
640
// for updates, we just need to know it worked- we don't actualy need to
641
// return the response object
651
* c-hanging-comment-ender-p: nil