1## Domain Registry Interface, RRP Host commands 2## 3## Copyright (c) 2005,2006,2008 Patrick Mevzek <netdri@dotandco.com>. All rights reserved. 4## 5## This file is part of Net::DRI 6## 7## Net::DRI is free software; you can redistribute it and/or modify 8## it under the terms of the GNU General Public License as published by 9## the Free Software Foundation; either version 2 of the License, or 10## (at your option) any later version. 11## 12## See the LICENSE file that comes with this distribution for more details. 13# 14# 15# 16######################################################################################### 17 18package Net::DRI::Protocol::RRP::Core::Host; 19 20use strict; 21use Net::DRI::Protocol::RRP; 22use Net::DRI::Data::Hosts; 23use Net::DRI::Util; 24 25our $VERSION=do { my @r=(q$Revision: 1.10 $=~/\d+/g); sprintf("%d".".%02d" x $#r, @r); }; 26 27=pod 28 29=head1 NAME 30 31Net::DRI::Protocol::RRP::Core::Host - RRP Host commands for Net::DRI 32 33=head1 DESCRIPTION 34 35Please see the README file for details. 36 37=head1 SUPPORT 38 39For now, support questions should be sent to: 40 41E<lt>netdri@dotandco.comE<gt> 42 43Please also see the SUPPORT file in the distribution. 44 45=head1 SEE ALSO 46 47E<lt>http://www.dotandco.com/services/software/Net-DRI/E<gt> 48 49=head1 AUTHOR 50 51Patrick Mevzek, E<lt>netdri@dotandco.comE<gt> 52 53=head1 COPYRIGHT 54 55Copyright (c) 2005,2006,2008 Patrick Mevzek <netdri@dotandco.com>. 56All rights reserved. 57 58This program is free software; you can redistribute it and/or modify 59it under the terms of the GNU General Public License as published by 60the Free Software Foundation; either version 2 of the License, or 61(at your option) any later version. 62 63See the LICENSE file that comes with this distribution for more details. 64 65=cut 66 67######################################################################################### 68 69sub register_commands 70{ 71 my ($class,$version)=@_; 72 my %tmp=( create => [ \&add ], 73 check => [ \&check, \&check_parse ], 74 info => [ \&status, \&status_parse ], 75 delete => [ \&del ], 76 update => [ \&mod ], 77 ); 78 79 return { 'host' => \%tmp }; 80} 81 82sub build_msg 83{ 84 my ($msg,$command,$hostname)=@_; 85 ($hostname)=$hostname->get_names(1) if (defined($hostname) && ref($hostname)); 86 Net::DRI::Exception->die(1,'protocol/RRP',3,"Host name needed") unless defined($hostname) && $hostname; 87 Net::DRI::Exception->die(1,'protocol/RRP',10,"Invalid host name") unless ($hostname=~m/^([a-z0-9]([a-z0-9\-]{0,61}[a-z0-9])?\.)*[a-z0-9]([a-z0-9\-]{0,61}[a-z0-9])?\.[a-z0-9]([a-z0-9\-]{0,61}[a-z0-9])?$/i); ## from RRP grammar 88 $msg->command($command) if defined($command); 89 $msg->entities('EntityName','NameServer'); 90 $msg->entities('NameServer',uc($hostname)); 91} 92 93sub add 94{ 95 my ($rrp,$ns)=@_; 96 my $mes=$rrp->message(); 97 build_msg($mes,'add',$ns); 98 add_ip($mes,$ns,$rrp->version()); 99} 100 101sub _basic_command 102{ 103 my ($command,$rrp,$ns)=@_; 104 my $mes=$rrp->message(); 105 build_msg($mes,$command,$ns); 106} 107 108sub check { return _basic_command('check',@_); } 109sub status { return _basic_command('status',@_); } 110sub del { return _basic_command('del',@_); } 111 112sub check_parse 113{ 114 my ($po,$otype,$oaction,$oname,$rinfo)=@_; 115 my $mes=$po->message(); 116 return unless $mes->is_success(); 117 118 $rinfo->{host}->{$oname}->{action}='check'; 119 if ($mes->errcode() == 213) ## nameserver exists 120 { 121 my @ip=$mes->entities('ipaddress'); 122 $rinfo->{host}->{$oname}->{self}=Net::DRI::Data::Hosts->new($oname,\@ip); 123 $rinfo->{host}->{$oname}->{exist}=1; 124 } elsif ($mes->errcode() == 212) ## nameserver available 125 { 126 $rinfo->{host}->{$oname}->{exist}=0; 127 } 128} 129 130sub status_parse 131{ 132 my ($po,$otype,$oaction,$oname,$rinfo)=@_; 133 my $mes=$po->message(); 134 return unless $mes->is_success(); ## if operation succeeds, information should be there 135 136 $rinfo->{host}->{$oname}->{exist}=1; 137 $rinfo->{host}->{$oname}->{action}='info'; 138 while(my ($k,$v)=each(%Net::DRI::Protocol::RRP::DATES)) 139 { 140 my $d=$mes->entities($k); 141 next unless $d; 142 $rinfo->{host}->{$oname}->{$v}=$po->{dt_parse}->parse_datetime($d); 143 } 144 145 while(my ($k,$v)=each(%Net::DRI::Protocol::RRP::IDS)) 146 { 147 my $d=$mes->entities($k); 148 next unless $d; 149 $rinfo->{host}->{$oname}->{$v}=$d; 150 } 151 152 my @ip=$mes->entities('ipaddress'); 153 $rinfo->{host}->{$oname}->{self}=Net::DRI::Data::Hosts->new($oname,\@ip); 154} 155 156sub mod 157{ 158 my ($rrp,$hostname,$todo)=@_; 159 my $mes=$rrp->message(); 160 161 Net::DRI::Exception::usererr_invalid_parameters($todo.' must be a Net::DRI::Data::Changes object') unless Net::DRI::Util::isa_changes($todo); 162 if ((grep { ! /^(?:ip|name)$/ } $todo->types()) || 163 (grep { ! /^(?:add|del)$/ } $todo->types('ip')) || 164 (grep { ! /^(?:set)$/ } $todo->types('name')) 165 ) 166 { 167 Net::DRI::Exception->die(0,'protocol/RRP',11,'Only IP add/del or name set available for host'); 168 } 169 170 my $nsadd=$todo->add('ip'); 171 my $nsdel=$todo->del('ip'); 172 my $newname=$todo->set('name'); 173 174 unless (defined($hostname) && $hostname) 175 { 176 $hostname=$nsadd->get_names(1) if (defined($nsadd) && ref($nsadd) && $nsadd->can('get_names')); 177 $hostname=$nsdel->get_names(1) if (defined($nsdel) && ref($nsdel) && $nsdel->can('get_names')); 178 } 179 build_msg($mes,'mod',$hostname); 180 181 my $version=$rrp->version(); 182 add_ip($mes,$nsadd,$version); 183 add_ip($mes,$nsdel,$version,'='); 184 $mes->entities('NewNameServer',ref($newname)? $newname->get_names(1) : $newname) if (defined($newname) && $newname); 185} 186 187sub add_ip 188{ 189 my ($mes,$ns,$version,$extra)=@_; 190 $extra||=''; 191 return unless (defined($ns) && ref($ns)); 192 my ($name,$r4,$r6)=$ns->get_details(1); 193 my $c=1; 194 foreach my $ip (@$r4) { last if $c++>13; $mes->entities('IPAddress',$_.$extra); }; 195 $c=1; 196 if ($version eq '2.0') { foreach my $ip (@$r6) { last if $c++>13; $mes->entities('IPAddress',$_.$extra); } } 197} 198 199######################################################################################### 2001; 201