home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / Update.pm < prev    next >
Encoding:
Perl POD Document  |  2004-01-04  |  6.3 KB  |  233 lines

  1. package Net::DNS::Update;
  2. #
  3. # $Id: Update.pm,v 2.101 2004/01/04 04:31:10 ctriv Exp $
  4. #
  5. use strict;
  6. use vars qw($VERSION @ISA);
  7.  
  8. use Net::DNS;
  9.  
  10. @ISA     = qw(Net::DNS::Packet);
  11. $VERSION = (qw$Revision: 2.101 $)[1];
  12.  
  13. =head1 NAME
  14.  
  15. Net::DNS::Update - Create a DNS update packet
  16.  
  17. =head1 SYNOPSIS
  18.  
  19. C<use Net::DNS::Update;>
  20.  
  21. =head1 DESCRIPTION
  22.  
  23. C<Net::DNS::Update> is a subclass of C<Net::DNS::Packet>,
  24. to be used for making DNS dynamic updates.  Programmers
  25. should refer to RFC 2136 for the semantics of dynamic updates.
  26.  
  27. WARNING:  This code is still under development.  Please use with
  28. caution on production nameservers.
  29.  
  30. =head1 METHODS
  31.  
  32. =head2 new
  33.  
  34.     $packet = Net::DNS::Update->new;
  35.     $packet = Net::DNS::Update->new('example.com');
  36.     $packet = Net::DNS::Update->new('example.com', 'HS');
  37.  
  38. Returns a C<Net::DNS::Update> object suitable for performing a DNS
  39. dynamic update.  Specifically, it creates a packet with the header
  40. opcode set to UPDATE and the zone record type to SOA (per RFC 2136,
  41. Section 2.3).
  42.  
  43. Programs must use the C<push> method to add RRs to the prerequisite,
  44. update, and additional sections before performing the update.
  45.  
  46. Arguments are the zone name and the class.  If the zone is omitted,
  47. the default domain will be taken from the resolver configuration.
  48. If the class is omitted, it defaults to IN.
  49.  
  50. Future versions of C<Net::DNS> may provide a simpler interface
  51. for making dynamic updates.
  52.  
  53. =cut
  54.  
  55. sub new {
  56.     my ($package, $zone, $class) = @_;
  57.  
  58.     unless ($zone) {
  59.         my $res = Net::DNS::Resolver->new;
  60.         $zone = ($res->searchlist)[0];
  61.         return unless $zone;
  62.     }
  63.  
  64.     my $type  = 'SOA';
  65.     $class  ||= 'IN';
  66.  
  67.     my $self = $package->SUPER::new($zone, $type, $class) || return;
  68.  
  69.     $self->header->opcode('UPDATE');
  70.     $self->header->rd(0);
  71.  
  72.     $self->{'seen'} = {};
  73.  
  74.  
  75.     return $self;
  76. }
  77.  
  78.  
  79. =head2 safe_push
  80.  
  81.     $update->safe_push(pre        => $rr);
  82.     $update->safe_push(update     => $rr);
  83.     $update->safe_push(additional => $rr);
  84.  
  85. Adds unseen RRs to the specified section of the update. This is useful
  86. to insure that the udpates do not contain redundant RRs in any of the
  87. sections.
  88.  
  89. =cut
  90.  
  91. sub safe_push {
  92.     my ($self, $section, @rrs) = @_;
  93.     
  94.     foreach my $rr (@rrs) {
  95.         next if $self->{'seen'}->{$rr->string};
  96.         
  97.         $self->push($section, $rr);
  98.     }
  99. }
  100.  
  101.  
  102. # overload push to DTRT.  We don't need to worry about populating
  103. # seen inside of new() because you only can use safe_push with
  104. # an update object.
  105.  
  106. sub push {
  107.     my ($self, $section, @rrs) = @_;
  108.     
  109.     $self->{'seen'}{$_->string}++ for @rrs;
  110.  
  111.     return $self->SUPER::push($section, @rrs);
  112. }
  113.  
  114. =head1 EXAMPLES
  115.  
  116. The first example below shows a complete program; subsequent examples
  117. show only the creation of the update packet.
  118.  
  119. =head2 Add a new host
  120.  
  121.  #!/usr/bin/perl -w
  122.  
  123.  use Net::DNS;
  124.  use strict;
  125.  
  126.  # Create the update packet.
  127.  my $update = Net::DNS::Update->new('example.com');
  128.  
  129.  # Prerequisite is that no A records exist for the name.
  130.  $update->push(pre => nxrrset('foo.example.com. A'));
  131.  
  132.  # Add two A records for the name.
  133.  $update->push(update => rr_add('foo.example.com. 86400 A 192.168.1.2'));
  134.  $update->push(update => rr_add('foo.example.com. 86400 A 172.16.3.4'));
  135.  
  136.  # Send the update to the zone's primary master.
  137.  my $res = Net::DNS::Resolver->new;
  138.  $res->nameservers('primary-master.example.com');
  139.  
  140.  my $reply = $res->send($update);
  141.  
  142.  # Did it work?
  143.  if ($reply) {
  144.      if ($reply->header->rcode eq 'NOERROR') {
  145.          print "Update succeeded\n";
  146.      } else {
  147.          print 'Update failed: ', $reply->header->rcode, "\n";
  148.      }
  149.  } else {
  150.      print 'Update failed: ', $res->errorstring, "\n";
  151.  }
  152.  
  153. =head2 Add an MX record for a name that already exists
  154.  
  155.     my $update = Net::DNS::Update->new('example.com');
  156.     $update->push(pre    => yxdomain('example.com'));
  157.     $update->push(update => rr_add('example.com MX 10 mailhost.example.com'));
  158.  
  159. =head2 Add a TXT record for a name that doesn't exist
  160.  
  161.     my $update = Net::DNS::Update->new('example.com');
  162.     $update->push(pre    => nxdomain('info.example.com'));
  163.     $update->push(update => rr_add('info.example.com TXT 'yabba dabba doo''));
  164.  
  165. =head2 Delete all A records for a name
  166.  
  167.     my $update = Net::DNS::Update->new('example.com');
  168.     $update->push(pre    => yxrrset('foo.example.com A'));
  169.     $update->push(update => rr_del('foo.example.com A'));
  170.  
  171. =head2 Delete all RRs for a name
  172.  
  173.     my $update = Net::DNS::Update->new('example.com');
  174.     $update->push(pre    => yxdomain('byebye.example.com'));
  175.     $update->push(update => rr_del('byebye.example.com'));
  176.  
  177. =head2 Perform a signed update
  178.  
  179.     my $key_name = 'tsig-key';
  180.     my $key      = 'awwLOtRfpGE+rRKF2+DEiw==';
  181.  
  182.     my $update = Net::DNS::Update->new('example.com');
  183.     $update->push(update => rr_add('foo.example.com A 10.1.2.3'));
  184.     $update->push(update => rr_add('bar.example.com A 10.4.5.6'));
  185.     $update->sign_tsig($key_name, $key);
  186.  
  187. =head2 Another way to perform a signed update
  188.  
  189.     my $key_name = 'tsig-key';
  190.     my $key      = 'awwLOtRfpGE+rRKF2+DEiw==';
  191.  
  192.     my $update = Net::DNS::Update->new('example.com');
  193.     $update->push(update     => rr_add('foo.example.com A 10.1.2.3'));
  194.     $update->push(update     => rr_add('bar.example.com A 10.4.5.6'));
  195.     $update->push(additional => Net::DNS::RR->new("$key_name TSIG $key"));
  196.  
  197. =head2 Perform a signed update with a customized TSIG record
  198.  
  199.     my $key_name = 'tsig-key';
  200.     my $key      = 'awwLOtRfpGE+rRKF2+DEiw==';
  201.  
  202.     my $tsig = Net::DNS::RR->new("$key_name TSIG $key");
  203.     $tsig->fudge(60);
  204.  
  205.     my $update = Net::DNS::Update->new('example.com');
  206.     $update->push(update     => rr_add('foo.example.com A 10.1.2.3'));
  207.     $update->push(update     => rr_add('bar.example.com A 10.4.5.6'));
  208.     $update->push(additional => $tsig);
  209.  
  210. =head1 BUGS
  211.  
  212. This code is still under development.  Please use with caution on
  213. production nameservers.
  214.  
  215. =head1 COPYRIGHT
  216.  
  217. Copyright (c) 1997-2002 Michael Fuhr. 
  218.  
  219. Portions Copyright (c) 2002-2003 Chris Reinhardt.
  220.  
  221. All rights reserved.  This program is free software; you may redistribute
  222. it and/or modify it under the same terms as Perl itself.
  223.  
  224. =head1 SEE ALSO
  225.  
  226. L<perl(1)>, L<Net::DNS>, L<Net::DNS::Resolver>, L<Net::DNS::Header>,
  227. L<Net::DNS::Packet>, L<Net::DNS::Question>, L<Net::DNS::RR>, RFC 2136,
  228. RFC 2845
  229.  
  230. =cut
  231.  
  232. 1;
  233.