2010-07-21 20:46:37 +00:00
package xCAT_plugin::ddns ;
2010-03-17 14:03:21 +00:00
use strict ;
2010-03-12 22:06:59 +00:00
use Getopt::Long ;
use Net::DNS ;
2010-12-01 13:00:18 +00:00
use File::Path ;
2010-03-12 22:06:59 +00:00
use xCAT::Table ;
2010-03-15 21:11:51 +00:00
use Sys::Hostname ;
2012-08-09 04:07:40 +00:00
use xCAT::TableUtils ;
2011-01-26 16:07:01 +00:00
use xCAT::NetworkUtils qw/getipaddr/ ;
use Math::BigInt ;
2010-03-22 13:18:34 +00:00
use MIME::Base64 ;
2010-08-06 15:29:07 +00:00
use xCAT::SvrUtils ;
2010-03-12 22:06:59 +00:00
use Socket ;
2010-03-17 14:03:21 +00:00
use Fcntl qw/:flock/ ;
2013-01-18 15:22:04 +00:00
# This is a rewrite of DNS management using nsupdate rather than
# direct zone mangling
2010-03-12 22:06:59 +00:00
my $ callback ;
2011-08-15 00:30:40 +00:00
my $ distro = xCAT::Utils - > osver ( ) ;
my $ service = "named" ;
2013-01-18 15:22:04 +00:00
2011-08-15 00:30:40 +00:00
# is this ubuntu ?
2013-01-07 07:22:27 +00:00
if ( $ distro =~ /ubuntu.*/i || $ distro =~ /debian.*/i ) {
2011-10-06 17:45:08 +00:00
$ service = "bind9" ;
2011-08-15 00:30:40 +00:00
}
2010-03-12 22:06:59 +00:00
2010-03-16 20:40:19 +00:00
sub handled_commands
{
2012-08-09 04:07:40 +00:00
my @ entries = xCAT::TableUtils - > get_site_attribute ( "dnshandler" ) ;
2012-05-22 05:58:08 +00:00
my $ site_entry = $ entries [ 0 ] ;
unless ( defined ( $ site_entry ) ) {
2011-11-14 09:02:42 +00:00
return { "makedns" = > "ddns" } ;
}
2010-07-21 20:39:51 +00:00
return { "makedns" = > "site:dnshandler" } ;
2010-03-16 20:40:19 +00:00
}
2010-06-16 17:59:42 +00:00
sub getzonesfornet {
2011-02-15 20:54:40 +00:00
my $ netent = shift ;
my $ net = $ netent - > { net } ;
my $ mask = $ netent - > { mask } ;
2010-06-16 17:59:42 +00:00
my @ zones = ( ) ;
2011-02-15 20:54:40 +00:00
if ( $ netent - > { ddnsdomain } ) {
push @ zones , $ netent - > { ddnsdomain } ;
}
2011-01-26 18:19:45 +00:00
if ( $ net =~ /:/ ) { #ipv6, for now do the simple stuff under the assumption we won't have a mask indivisible by 4
$ net =~ s/\/(.*)// ;
my $ maskbits = $ 1 ;
2012-02-15 05:52:55 +00:00
if ( $ maskbits % 4 ) {
2011-01-26 18:19:45 +00:00
die "Not supporting having a mask like $mask on an ipv6 network like $net" ;
}
my $ netnum = getipaddr ( $ net , GetNumber = > 1 ) ;
2011-06-16 13:45:21 +00:00
unless ( $ netnum ) { return ( ) ; }
2011-01-26 18:19:45 +00:00
$ netnum - > brsft ( 128 - $ maskbits ) ;
my $ prefix = $ netnum - > as_hex ( ) ;
my $ nibbs = $ maskbits / 4 ;
$ prefix =~ s/^0x// ;
my $ rev ;
foreach ( reverse ( split // , $ prefix ) ) {
$ rev . = $ _ . "." ;
$ nibbs - - ;
}
while ( $ nibbs ) {
$ rev . = "0." ;
$ nibbs - - ;
}
$ rev . = "ip6.arpa." ;
2011-02-15 20:54:40 +00:00
push @ zones , $ rev ;
return @ zones ;
2011-01-26 18:19:45 +00:00
}
2010-06-16 17:59:42 +00:00
#return all in-addr reverse zones for a given mask and net
#for class a,b,c, the answer is easy
#for classless, identify the partial byte, do $netbyte | (0xff&~$maskbyte) to get the highest value
#return sequence from $net to value calculated above
#since old bind.pm only went as far as class c, we will carry that over for now (more people with smaller than class c complained
#and none hit the theoretical conflict. FYI, the 'official' method in RFC 2317 seems cumbersome, but maybe one day it makes sense
#since this is dhcpv4 for now, we'll use the inet_aton, ntop functions to generate the answers (dhcpv6 omapi would be nice...)
my $ netn = inet_aton ( $ net ) ;
my $ maskn = inet_aton ( $ mask ) ;
unless ( $ netn and $ mask ) { return ( ) ; }
my $ netnum = unpack ( 'N' , $ netn ) ;
my $ masknum = unpack ( 'N' , $ maskn ) ;
if ( $ masknum >= 0xffffff00 ) { #treat all netmasks higher than 255.255.255.0 as class C
$ netnum = $ netnum & 0xffffff00 ;
$ netn = pack ( 'N' , $ netnum ) ;
$ net = inet_ntoa ( $ netn ) ;
2010-06-16 18:23:30 +00:00
$ net =~ s/\.[^\.]*$// ;
2010-06-16 17:59:42 +00:00
return ( join ( '.' , reverse ( split ( '\.' , $ net ) ) ) . '.IN-ADDR.ARPA.' ) ;
2010-06-16 18:14:47 +00:00
} elsif ( $ masknum > 0xffff0000 ) { #(/17) to /23
2010-06-16 17:59:42 +00:00
my $ tempnumber = ( $ netnum >> 8 ) ;
$ masknum = $ masknum >> 8 ;
my $ highnet = $ tempnumber | ( 0xffffff & ~ $ masknum ) ;
foreach ( $ tempnumber .. $ highnet ) {
$ netnum = $ _ << 8 ;
$ net = inet_ntoa ( pack ( 'N' , $ netnum ) ) ;
2010-06-16 18:23:30 +00:00
$ net =~ s/\.[^\.]*$// ;
2010-06-16 17:59:42 +00:00
push @ zones , join ( '.' , reverse ( split ( '\.' , $ net ) ) ) . '.IN-ADDR.ARPA.' ;
}
return @ zones ;
2010-06-16 18:14:47 +00:00
} elsif ( $ masknum > 0xff000000 ) { # (/9) to class b /16, could have made it more flexible, for for only two cases, not worth in
2010-06-16 17:59:42 +00:00
my $ tempnumber = ( $ netnum >> 16 ) ; #the last two bytes are insignificant, shift them off to make math easier
$ masknum = $ masknum >> 16 ;
my $ highnet = $ tempnumber | ( 0xffff & ~ $ masknum ) ;
foreach ( $ tempnumber .. $ highnet ) {
$ netnum = $ _ < < 16 ; #convert back to the real network value
$ net = inet_ntoa ( pack ( 'N' , $ netnum ) ) ;
2010-06-16 18:23:30 +00:00
$ net =~ s/\.[^\.]*$// ;
$ net =~ s/\.[^\.]*$// ;
2010-06-16 17:59:42 +00:00
push @ zones , join ( '.' , reverse ( split ( '\.' , $ net ) ) ) . '.IN-ADDR.ARPA.' ;
}
return @ zones ;
2010-06-16 18:14:47 +00:00
} else { #class a (theoretically larger, but those shouldn't exist)
my $ tempnumber = ( $ netnum >> 24 ) ; #the last two bytes are insignificant, shift them off to make math easier
$ masknum = $ masknum >> 24 ;
my $ highnet = $ tempnumber | ( 0xff & ~ $ masknum ) ;
foreach ( $ tempnumber .. $ highnet ) {
$ netnum = $ _ << 24 ; #convert back to the real network value
$ net = inet_ntoa ( pack ( 'N' , $ netnum ) ) ;
2010-06-16 18:23:30 +00:00
$ net =~ s/\.[^\.]*$// ;
$ net =~ s/\.[^\.]*$// ;
$ net =~ s/\.[^\.]*$// ;
2010-06-16 18:14:47 +00:00
push @ zones , join ( '.' , reverse ( split ( '\.' , $ net ) ) ) . '.IN-ADDR.ARPA.' ;
}
return @ zones ;
}
2010-06-16 17:59:42 +00:00
}
2011-01-26 16:07:01 +00:00
sub get_reverse_zones_for_entity {
2010-03-12 22:06:59 +00:00
my $ ctx = shift ;
my $ node = shift ;
my $ net ;
2011-04-12 11:22:43 +00:00
if ( ( $ node =~ /loopback/ ) || ( $ node =~ /localhost/ ) )
{
# do not use DNS to resolve localhsot
return ;
}
2010-03-12 22:06:59 +00:00
if ( $ ctx - > { hoststab } and $ ctx - > { hoststab } - > { $ node } and $ ctx - > { hoststab } - > { $ node } - > [ 0 ] - > { ip } ) {
$ node = $ ctx - > { hoststab } - > { $ node } - > [ 0 ] - > { ip } ;
}
2011-01-26 16:07:01 +00:00
my @ tvars = getipaddr ( $ node , GetNumber = > 1 , GetAllAddresses = > 1 ) ;
2010-03-12 22:06:59 +00:00
my $ tvar ;
2011-01-26 16:07:01 +00:00
my @ revs ;
foreach $ tvar ( @ tvars ) {
2010-03-12 22:06:59 +00:00
foreach my $ net ( keys % { $ ctx - > { nets } } ) {
if ( $ ctx - > { nets } - > { $ net } - > { netn } == ( $ tvar & $ ctx - > { nets } - > { $ net } - > { mask } ) ) {
2011-01-26 16:07:01 +00:00
if ( $ net =~ /\./ ) { #IPv4/IN-ADDR.ARPA case.
my $ maskstr = unpack ( "B32" , pack ( "N" , $ ctx - > { nets } - > { $ net } - > { mask } ) ) ;
my $ maskcount = ( $ maskstr =~ tr /1/ / ) ;
2011-04-13 02:55:22 +00:00
if ( $ maskcount >= 24 )
{
$ maskcount -= ( $ maskcount % 8 ) ; #e.g. treat the 27bit netmask as 24bit
}
else
{
$ maskcount += ( ( 8 - ( $ maskcount % 8 ) ) % 8 ) ; #round to the next octet
}
2011-01-26 16:07:01 +00:00
my $ newmask = 2 ** $ maskcount - 1 << ( 32 - $ maskcount ) ;
my $ rev = inet_ntoa ( pack ( "N" , ( $ tvar & $ newmask ) ) ) ;
my @ zone ;
my @ orig = split /\./ , $ rev ;
while ( $ maskcount ) {
$ maskcount -= 8 ;
unshift ( @ zone , ( shift @ orig ) ) ;
}
$ rev = join ( '.' , @ zone ) ;
$ rev . = '.IN-ADDR.ARPA.' ;
push @ revs , $ rev ;
} elsif ( $ net =~ /:/ ) { #v6/ip6.arpa case
$ net =~ /\/(.*)/ ;
my $ maskbits = $ 1 ;
unless ( $ maskbits and ( ( $ maskbits % 4 ) == 0 ) ) {
die "Never expected this, $net should have had CIDR / notation... and the mask should be a factor of 4, if not, need work..."
}
my $ netnum = Math::BigInt - > new ( $ ctx - > { nets } - > { $ net } - > { netn } ) ;
$ netnum - > brsft ( 128 - $ maskbits ) ;
my $ prefix = $ netnum - > as_hex ( ) ;
my $ nibbs = $ maskbits / 4 ;
$ prefix =~ s/^0x// ;
my $ rev ;
foreach ( reverse ( split // , $ prefix ) ) {
$ rev . = $ _ . "." ;
$ nibbs - - ;
}
while ( $ nibbs ) {
$ rev . = "0." ;
$ nibbs - - ;
}
$ rev . = "ip6.arpa." ;
push @ revs , $ rev ;
2010-03-12 22:06:59 +00:00
}
}
}
}
2011-01-26 16:07:01 +00:00
return @ revs ;
2010-03-12 22:06:59 +00:00
}
sub process_request {
my $ request = shift ;
2010-03-16 21:38:40 +00:00
$ callback = shift ;
2012-09-25 09:19:21 +00:00
my $ oldmask = umask ( 0007 ) ;
2010-03-12 22:06:59 +00:00
my $ ctx = { } ;
2010-03-16 21:38:40 +00:00
my @ nodes = ( ) ;
2010-03-12 22:06:59 +00:00
my $ hadargs = 0 ;
my $ allnodes ;
my $ zapfiles ;
2011-04-14 02:38:36 +00:00
my $ help ;
2010-08-25 13:13:44 +00:00
my $ deletemode = 0 ;
2012-02-20 04:26:38 +00:00
my $ external = 0 ;
2013-12-18 06:35:15 +00:00
my $ slave = 0 ;
2010-03-12 22:06:59 +00:00
if ( $ request - > { arg } ) {
$ hadargs = 1 ;
@ ARGV = @ { $ request - > { arg } } ;
2011-04-14 02:38:36 +00:00
Getopt::Long:: Configure ( "no_pass_through" ) ;
2011-05-11 06:48:21 +00:00
Getopt::Long:: Configure ( "bundling" ) ;
2010-03-12 22:06:59 +00:00
if ( ! GetOptions (
'a|all' = > \ $ allnodes ,
'n|new' = > \ $ zapfiles ,
2010-08-25 13:13:44 +00:00
'd|delete' = > \ $ deletemode ,
2012-02-20 04:26:38 +00:00
'e|external' = > \ $ external ,
2013-12-18 06:35:15 +00:00
's|slave' = > \ $ slave ,
2011-04-14 02:38:36 +00:00
'h|help' = > \ $ help ,
2010-03-12 22:06:59 +00:00
) ) {
2011-04-14 02:38:36 +00:00
#xCAT::SvrUtils::sendmsg([1,"TODO: makedns Usage message"], $callback);
makedns_usage ( $ callback ) ;
2012-09-25 09:19:21 +00:00
umask ( $ oldmask ) ;
2010-03-12 22:06:59 +00:00
return ;
}
}
2011-04-14 02:38:36 +00:00
if ( $ help )
{
makedns_usage ( $ callback ) ;
2012-09-25 09:19:21 +00:00
umask ( $ oldmask ) ;
2011-06-22 03:33:51 +00:00
return ;
2011-04-14 02:38:36 +00:00
}
2011-06-28 09:07:53 +00:00
if ( $ deletemode && ( ! $ request - > { node } - > [ 0 ] ) )
{
makedns_usage ( $ callback ) ;
2012-09-25 09:19:21 +00:00
umask ( $ oldmask ) ;
2011-06-28 09:07:53 +00:00
return ;
}
2011-04-14 02:38:36 +00:00
2010-08-25 13:13:44 +00:00
$ ctx - > { deletemode } = $ deletemode ;
2013-01-18 15:22:04 +00:00
2011-08-07 11:24:49 +00:00
# check for site.domain
2012-08-09 04:07:40 +00:00
my @ entries = xCAT::TableUtils - > get_site_attribute ( "domain" ) ;
2012-05-22 05:58:08 +00:00
my $ site_entry = $ entries [ 0 ] ;
unless ( defined ( $ site_entry ) ) {
2010-08-06 15:29:07 +00:00
xCAT::SvrUtils:: sendmsg ( [ 1 , "domain not defined in site table" ] , $ callback ) ;
2012-09-25 09:19:21 +00:00
umask ( $ oldmask ) ;
2010-03-16 21:38:40 +00:00
return ;
}
2012-05-22 05:58:08 +00:00
$ ctx - > { domain } = $ site_entry ;
2011-08-09 16:39:47 +00:00
2012-02-20 04:26:38 +00:00
if ( $ external ) #need to check if /etc/resolv.conf existing
2011-08-09 16:39:47 +00:00
{
2012-02-20 04:26:38 +00:00
my $ resolv = "/etc/resolv.conf" ;
my $ cmd = "egrep '^nameserver|^search' $resolv" ;
my $ acmd = "egrep '^nameserver|^domain' $resolv" ;
if ( xCAT::Utils - > isAIX ( ) )
{
$ cmd = $ acmd ;
2011-08-09 16:39:47 +00:00
}
2012-02-20 04:26:38 +00:00
my @ output = xCAT::Utils - > runcmd ( $ cmd , 0 ) ;
if ( $ ::RUNCMD_RC != 0 )
{
xCAT::SvrUtils:: sendmsg ( [ 1 , "You are using -e flag to update DNS records to an external DNS server, please ensure /etc/resolv.conf existing and pointed to this external DNS server." ] , $ callback ) ;
2012-09-25 09:19:21 +00:00
umask ( $ oldmask ) ;
2012-02-20 04:26:38 +00:00
return ;
2011-08-18 17:38:53 +00:00
}
2011-08-09 16:39:47 +00:00
}
2012-02-20 04:26:38 +00:00
2011-08-19 11:55:15 +00:00
# check for selinux disabled
2011-08-22 12:02:08 +00:00
my $ rc = xCAT::Utils - > isSELINUX ( ) ;
if ( $ rc == 0 )
2011-08-19 11:55:15 +00:00
{
xCAT::SvrUtils:: sendmsg ( [ 0 , "Warning:SELINUX is not disabled. The makedns command will not be able to generate a complete DNS setup. Disable SELINUX and run the command again." ] , $ callback ) ;
2011-08-22 12:02:08 +00:00
}
2013-06-08 07:18:19 +00:00
my @ entries = xCAT::TableUtils - > get_site_attribute ( "nameservers" ) ;
my $ sitens = $ entries [ 0 ] ;
unless ( defined ( $ site_entry ) ) {
xCAT::SvrUtils:: sendmsg ( [ 1 , "nameservers not defined in site table" ] , $ callback ) ;
umask ( $ oldmask ) ;
return ;
}
2011-08-22 12:02:08 +00:00
2011-06-23 07:10:36 +00:00
my $ networkstab = xCAT::Table - > new ( 'networks' , - create = > 0 ) ;
unless ( $ networkstab ) { xCAT::SvrUtils:: sendmsg ( [ 1 , 'Unable to enumerate networks, try to run makenetworks' ] , $ callback ) ; }
2013-01-18 15:22:04 +00:00
2013-06-08 07:18:19 +00:00
my @ networks = $ networkstab - > getAllAttribs ( 'net' , 'mask' , 'ddnsdomain' , 'domain' , 'nameservers' ) ;
# exclude the net if it is using an external dns server.
2013-10-24 08:13:55 +00:00
foreach my $ net ( @ networks )
2013-06-08 07:18:19 +00:00
{
2013-10-24 08:13:55 +00:00
if ( $ net and $ net - > { nameservers } )
2013-06-08 07:18:19 +00:00
{
2013-10-24 08:13:55 +00:00
my $ valid = 0 ;
my $ myip = xCAT::NetworkUtils - > my_ip_facing ( $ net - > { net } ) ;
foreach ( split /,/ , $ net - > { nameservers } )
2013-06-08 07:18:19 +00:00
{
2013-10-24 08:13:55 +00:00
chomp $ _ ;
if ( ( $ _ eq $ myip ) || ( $ _ eq '<xcatmaster>' ) || ( $ _ eq $ sitens ) )
{
$ valid += 1 ;
}
}
unless ( $ valid > 0 )
{
$ net = undef ;
2013-06-08 07:18:19 +00:00
}
}
}
2011-06-23 07:10:36 +00:00
2013-04-22 19:09:36 +00:00
# if ($request->{node}) { #we have a noderange to process
# @nodes = @{$request->{node}};
#} elsif ($allnodes) {
if ( $ allnodes ) {
2010-03-12 22:06:59 +00:00
#read all nodelist specified nodes
2010-03-15 21:11:51 +00:00
} else {
2013-04-22 19:09:36 +00:00
if ( not $ request - > { node } and $ deletemode ) {
2011-06-29 17:57:25 +00:00
#when this was permitted, it really ruined peoples' days
xCAT::SvrUtils:: sendmsg ( [ 1 , "makedns -d without noderange or -a is not supported" ] , $ callback ) ;
2012-09-25 09:19:21 +00:00
umask ( $ oldmask ) ;
2011-06-29 17:57:25 +00:00
return ;
}
2013-04-22 19:09:36 +00:00
my @ contents ;
2013-01-18 15:22:04 +00:00
my $ domain ;
2013-04-22 19:09:36 +00:00
if ( $ request - > { node } ) { #leverage makehosts code to flesh out the options
require xCAT_plugin::hosts ;
2013-06-04 08:20:52 +00:00
my @ content1 ;
my @ content2 ;
xCAT_plugin::hosts:: add_hosts_content ( nodelist = > $ request - > { node } , callback = > $ callback , hostsref = > \ @ content1 ) ;
xCAT_plugin::hosts:: donics ( nodes = > $ request - > { node } , callback = > $ callback , hostsref = > \ @ content2 ) ;
@ contents = ( @ content1 , @ content2 ) ;
2013-04-22 19:09:36 +00:00
} else {
#legacy behavior, read from /etc/hosts
my $ hostsfile ;
open ( $ hostsfile , "<" , "/etc/hosts" ) ;
flock ( $ hostsfile , LOCK_SH ) ;
@ contents = <$hostsfile> ;
flock ( $ hostsfile , LOCK_UN ) ;
close ( $ hostsfile ) ;
}
2010-03-15 21:11:51 +00:00
my $ addr ;
my $ name ;
my $ canonical ;
my $ aliasstr ;
my @ aliases ;
my $ names ;
2013-01-18 15:22:04 +00:00
my @ hosts ;
my % nodehash ;
2010-03-15 21:11:51 +00:00
foreach ( @ contents ) {
2010-03-16 21:38:40 +00:00
chomp ; #no newline
2010-03-15 21:11:51 +00:00
s/#.*// ; #strip comments;
2010-03-16 21:38:40 +00:00
s/^[ \t\n]*// ; #remove leading whitespace
2010-03-15 21:11:51 +00:00
next unless ( $ _ ) ; #skip empty lines
( $ addr , $ names ) = split /[ \t]+/ , $ _ , 2 ;
2013-01-18 15:22:04 +00:00
2011-02-15 20:54:40 +00:00
if ( $ addr !~ /^\d+\.\d+\.\d+\.\d+$/ and $ addr !~ /^[abcdef0123456789:]+$/ ) {
xCAT::SvrUtils:: sendmsg ( ":Ignoring line $_ in /etc/hosts, address seems malformed." , $ callback ) ;
2010-03-15 21:11:51 +00:00
next ;
}
2010-03-16 21:38:40 +00:00
unless ( $ names =~ /^[a-z0-9\. \t\n-]+$/i ) {
2010-08-06 15:29:07 +00:00
xCAT::SvrUtils:: sendmsg ( ":Ignoring line $_ in /etc/hosts, names $names contain invalid characters (valid characters include a through z, numbers and the '-', but not '_'" , $ callback ) ;
2010-03-15 21:11:51 +00:00
next ;
}
2013-01-18 15:22:04 +00:00
my ( $ host , $ ip ) = xCAT::NetworkUtils - > gethostnameandip ( $ addr ) ;
push @ hosts , $ host ;
$ nodehash { $ addr } { names } = $ names ;
$ nodehash { $ addr } { host } = $ host ;
}
# get the domains for each node - one call for all nodes in hosts file
my $ nd = xCAT::NetworkUtils - > getNodeDomains ( \ @ hosts ) ;
my % nodedomains = %$ nd ;
foreach my $ n ( keys % nodehash ) {
$ addr = $ n ;
$ names = $ nodehash { $ n } { names } ;
# - need domain for this node
my $ host = $ nodehash { $ n } { host } ;
$ domain = $ nodedomains { $ host } ;
unless ( $ domain =~ /^\./ ) { $ domain = '.' . $ domain ; }
2010-03-15 21:11:51 +00:00
( $ canonical , $ aliasstr ) = split /[ \t]+/ , $ names , 2 ;
if ( $ aliasstr ) {
@ aliases = split /[ \t]+/ , $ aliasstr ;
} else {
@ aliases = ( ) ;
}
2010-03-16 21:38:40 +00:00
my % names = ( ) ;
my $ node = $ canonical ;
2011-07-20 06:45:30 +00:00
xCAT::SvrUtils:: sendmsg ( ":Handling $node in /etc/hosts." , $ callback ) ;
2010-03-16 21:38:40 +00:00
unless ( $ canonical =~ /$domain/ ) {
$ canonical . = $ domain ;
}
2013-01-18 15:22:04 +00:00
# for only the sake of comparison, ensure consistant dot suffix
unless ( $ canonical =~ /\.\z/ ) { $ canonical . = '.' }
2010-03-16 21:38:40 +00:00
foreach my $ alias ( @ aliases ) {
unless ( $ alias =~ /$domain/ ) {
$ alias . = $ domain ;
}
unless ( $ alias =~ /\.\z/ ) {
$ alias . = '.' ;
}
if ( $ alias eq $ canonical ) {
next ;
}
2013-01-18 15:22:04 +00:00
# remember alias for CNAM records later
$ ctx - > { aliases } - > { $ node } - > { $ alias } = 1 ;
2010-03-16 21:38:40 +00:00
}
2011-06-23 07:10:36 +00:00
# exclude the nodes not belong to any nets defined in networks table
2013-01-18 15:22:04 +00:00
# because only the nets defined in networks table will be add
# zones later.
2011-06-23 07:10:36 +00:00
my $ found = 0 ;
foreach ( @ networks )
{
if ( xCAT::NetworkUtils - > ishostinsubnet ( $ addr , $ _ - > { mask } , $ _ - > { net } ) )
{
$ found = 1 ;
}
}
if ( $ found )
{
push @ nodes , $ node ;
$ ctx - > { nodeips } - > { $ node } - > { $ addr } = 1 ;
}
else
{
2011-06-23 07:39:43 +00:00
unless ( $ node =~ /localhost/ )
{
2013-06-08 07:18:19 +00:00
xCAT::SvrUtils:: sendmsg ( ":Ignoring host $node, it does not belong to any nets defined in networks table or the net it belongs to is configured to use an external nameserver." , $ callback ) ;
2011-06-23 07:39:43 +00:00
}
2011-06-23 07:10:36 +00:00
}
2010-03-15 21:11:51 +00:00
}
2010-03-12 22:06:59 +00:00
}
2013-01-18 15:22:04 +00:00
2010-03-12 22:06:59 +00:00
my $ hoststab = xCAT::Table - > new ( 'hosts' , - create = > 0 ) ;
if ( $ hoststab ) {
$ ctx - > { hoststab } = $ hoststab - > getNodesAttribs ( \ @ nodes , [ 'ip' ] ) ;
}
2010-03-15 21:11:51 +00:00
$ ctx - > { nodes } = \ @ nodes ;
2011-06-23 07:10:36 +00:00
2010-03-12 22:06:59 +00:00
foreach ( @ networks ) {
2011-01-26 16:07:01 +00:00
my $ maskn ;
if ( $ _ - > { mask } ) { #better be IPv4, we only do CIDR for v6, use the v4/v6 agnostic just in case
$ maskn = getipaddr ( $ _ - > { mask } , GetNumber = > 1 ) ; #pack("N",inet_aton($_->{mask}));
} elsif ( $ _ - > { net } =~ /\/(.*)/ ) { #CIDR
my $ maskbits = $ 1 ;
my $ numbits ;
if ( $ _ - > { net } =~ /:/ ) { #v6
$ numbits = 128 ;
} elsif ( $ _ - > { net } =~ /\./ ) {
$ numbits = 32 ;
} else {
2012-09-25 09:19:21 +00:00
umask ( $ oldmask ) ;
2011-01-26 16:07:01 +00:00
die "Network " . $ _ - > { net } . " appears to be malformed in networks table" ;
}
$ maskn = Math::BigInt - > new ( "0b" . ( "1" x $ maskbits ) . ( "0" x ( $ numbits - $ maskbits ) ) ) ;
}
2010-03-12 22:06:59 +00:00
$ ctx - > { nets } - > { $ _ - > { net } } - > { mask } = $ maskn ;
2013-01-18 15:22:04 +00:00
2011-01-26 16:07:01 +00:00
my $ net = $ _ - > { net } ;
$ net =~ s/\/.*// ;
$ ctx - > { nets } - > { $ _ - > { net } } - > { netn } = getipaddr ( $ net , GetNumber = > 1 ) ;
2010-06-16 17:59:42 +00:00
my $ currzone ;
2011-02-15 20:54:40 +00:00
foreach $ currzone ( getzonesfornet ( $ _ ) ) {
2010-06-16 17:59:42 +00:00
$ ctx - > { zonestotouch } - > { $ currzone } = 1 ;
}
2010-03-12 22:06:59 +00:00
}
my $ passtab = xCAT::Table - > new ( 'passwd' ) ;
my $ pent = $ passtab - > getAttribs ( { key = > 'omapi' , username = > 'xcat_key' } , [ 'password' ] ) ;
if ( $ pent and $ pent - > { password } ) {
$ ctx - > { privkey } = $ pent - > { password } ;
} #do not warn/error here yet, if we can't generate or extract, we'll know later
2011-05-11 06:48:21 +00:00
2012-08-09 04:07:40 +00:00
my @ entries = xCAT::TableUtils - > get_site_attribute ( "forwarders" ) ;
2012-05-22 05:58:08 +00:00
my $ site_entry = $ entries [ 0 ] ;
if ( defined ( $ site_entry ) ) {
my @ forwarders = split /[ ,]/ , $ site_entry ;
2011-05-12 07:59:51 +00:00
$ ctx - > { forwarders } = \ @ forwarders ;
2010-03-12 22:06:59 +00:00
}
2011-06-23 07:10:36 +00:00
2013-12-18 06:35:15 +00:00
my @ slave_ips ;
my $ dns_slaves = get_dns_slave ( ) ;
if ( scalar @$ dns_slaves ) {
foreach my $ slave_hn ( @$ dns_slaves ) {
my $ slave_ip = xCAT::NetworkUtils - > getipaddr ( $ slave_hn ) ;
push @ slave_ips , $ slave_ip ;
}
$ ctx - > { slaves } = \ @ slave_ips ;
}
2013-01-18 15:22:04 +00:00
$ ctx - > { zonestotouch } - > { $ ctx - > { domain } } = 1 ;
foreach ( @ networks ) {
if ( $ _ - > { domain } ) {
$ ctx - > { zonestotouch } - > { $ _ - > { domain } } = 1 ;
}
}
2012-06-15 05:18:56 +00:00
xCAT::SvrUtils:: sendmsg ( "Getting reverse zones, this may take several minutes for a large cluster." , $ callback ) ;
2011-06-23 07:10:36 +00:00
2010-03-12 22:06:59 +00:00
foreach ( @ nodes ) {
2011-01-26 16:07:01 +00:00
my @ revzones = get_reverse_zones_for_entity ( $ ctx , $ _ ) ; ;
unless ( @ revzones ) { next ; }
$ ctx - > { revzones } - > { $ _ } = \ @ revzones ;
foreach ( @ revzones ) {
$ ctx - > { zonestotouch } - > { $ _ } = 1 ;
}
2010-03-12 22:06:59 +00:00
}
2011-06-23 07:10:36 +00:00
xCAT::SvrUtils:: sendmsg ( "Completed getting reverse zones." , $ callback ) ;
2013-01-18 15:22:04 +00:00
if ( 1 ) {
#TODO: function to detect and return 1 if the master server is
# DNS SOA for all the zones we care about here, we are examining
# files to assure that our key is in named.conf, the zones we
# care about are there, and that if active directory is in use,
# allow the domain controllers to update specific zones
2012-08-09 04:07:40 +00:00
@ entries = xCAT::TableUtils - > get_site_attribute ( "directoryprovider" ) ;
2012-05-22 05:58:08 +00:00
$ site_entry = $ entries [ 0 ] ;
if ( defined ( $ site_entry ) and $ site_entry eq 'activedirectory' ) {
2012-08-09 04:07:40 +00:00
@ entries = xCAT::TableUtils - > get_site_attribute ( "directoryservers" ) ;
2012-05-22 05:58:08 +00:00
$ site_entry = $ entries [ 0 ] ;
if ( defined ( $ site_entry ) ) {
my @ dservers = split /[ ,]/ , $ site_entry ;
2010-03-12 22:06:59 +00:00
$ ctx - > { adservers } = \ @ dservers ;
2013-01-18 15:22:04 +00:00
############################
# - should this include all domains?
# - multi-domains not supported with activedirectory
# - TODO in future release
###################
2010-03-15 21:11:51 +00:00
$ ctx - > { adzones } = {
"_msdcs." . $ ctx - > { domain } = > 1 ,
"_sites." . $ ctx - > { domain } = > 1 ,
"_tcp." . $ ctx - > { domain } = > 1 ,
"_udp." . $ ctx - > { domain } = > 1 ,
} ;
2010-03-12 22:06:59 +00:00
}
}
2013-01-18 15:22:04 +00:00
2012-08-09 04:07:40 +00:00
@ entries = xCAT::TableUtils - > get_site_attribute ( "dnsupdaters" ) ;
2012-05-22 05:58:08 +00:00
$ site_entry = $ entries [ 0 ] ;
if ( defined ( $ site_entry ) ) {
my @ nservers = split /[ ,]/ , $ site_entry ;
2010-03-12 22:06:59 +00:00
$ ctx - > { dnsupdaters } = \ @ nservers ;
}
if ( $ zapfiles ) { #here, we unlink all the existing files to start fresh
2011-04-13 08:08:32 +00:00
if ( xCAT::Utils - > isAIX ( ) )
{
system ( "/usr/bin/stopsrc -s $service" ) ;
}
else
{
2011-08-15 00:30:40 +00:00
system ( "service $service stop" ) ; #named may otherwise hold on to stale journal filehandles
2011-04-13 08:08:32 +00:00
}
2010-12-01 13:00:18 +00:00
my $ conf = get_conf ( ) ;
unlink $ conf ;
my $ DBDir = get_dbdir ( ) ;
foreach ( <$DBDir/db.*> ) {
2010-03-17 14:03:21 +00:00
unlink $ _ ;
}
2010-03-12 22:06:59 +00:00
}
#We manipulate local namedconf
2010-03-15 21:11:51 +00:00
$ ctx - > { dbdir } = get_dbdir ( ) ;
2010-12-01 13:00:18 +00:00
$ ctx - > { zonesdir } = get_zonesdir ( ) ;
2010-04-01 19:19:04 +00:00
chmod 0775 , $ ctx - > { dbdir } ; # assure dynamic dns can actually execute against the directory
2013-01-18 15:22:04 +00:00
2013-12-18 06:35:15 +00:00
update_namedconf ( $ ctx , $ slave ) ;
unless ( $ slave )
{
update_zones ( $ ctx ) ;
}
2013-04-27 06:04:50 +00:00
2010-03-15 21:11:51 +00:00
if ( $ ctx - > { restartneeded } ) {
2010-12-01 13:00:18 +00:00
xCAT::SvrUtils:: sendmsg ( "Restarting $service" , $ callback ) ;
2011-04-12 11:22:43 +00:00
2013-09-11 09:24:57 +00:00
if ( xCAT::Utils - > isAIX ( ) )
{
2013-11-13 06:58:44 +00:00
#try to stop named
2013-09-11 09:24:57 +00:00
my $ cmd = "/usr/bin/stopsrc -s $service" ;
my @ output = xCAT::Utils - > runcmd ( $ cmd , 0 ) ;
$ cmd = "/usr/bin/startsrc -s $service" ;
@ output = xCAT::Utils - > runcmd ( $ cmd , 0 ) ;
2013-11-13 06:58:44 +00:00
my $ outp = join ( '' , @ output ) ;
2013-09-11 09:24:57 +00:00
if ( $ ::RUNCMD_RC != 0 )
{
my $ rsp = { } ;
$ rsp - > { data } - > [ 0 ] = "Command failed: $cmd. Error message: $outp.\n" ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ callback ) ;
return ;
}
}
else
{
my $ cmd = "service $service stop" ;
my @ output = xCAT::Utils - > runcmd ( $ cmd , 0 ) ;
my $ outp = join ( '' , @ output ) ;
if ( $ ::RUNCMD_RC != 0 )
{
my $ rsp = { } ;
$ rsp - > { data } - > [ 0 ] = "Command failed: $cmd. Error message: $outp.\n" ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ callback ) ;
return ;
}
$ cmd = "service $service start" ;
@ output = xCAT::Utils - > runcmd ( $ cmd , 0 ) ;
$ outp = join ( '' , @ output ) ;
if ( $ ::RUNCMD_RC != 0 )
{
my $ rsp = { } ;
$ rsp - > { data } - > [ 0 ] = "Command failed: $cmd. Error message: $outp.\n" ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ callback ) ;
return ;
}
}
2010-08-06 15:29:07 +00:00
xCAT::SvrUtils:: sendmsg ( "Restarting named complete" , $ callback ) ;
2010-03-15 21:11:51 +00:00
}
2010-03-12 22:06:59 +00:00
} else {
unless ( $ ctx - > { privkey } ) {
2010-08-06 15:29:07 +00:00
xCAT::SvrUtils:: sendmsg ( [ 1 , "Unable to update DNS due to lack of credentials in passwd to communicate with remote server" ] , $ callback ) ;
2010-03-12 22:06:59 +00:00
}
}
2013-04-27 06:04:50 +00:00
2013-12-18 06:35:15 +00:00
if ( $ slave )
{
return ;
}
2013-04-27 06:04:50 +00:00
# check if named is active before update dns records.
if ( xCAT::Utils - > isAIX ( ) )
{
my $ cmd = "/usr/bin/lssrc -s $service |grep active" ;
my @ output = xCAT::Utils - > runcmd ( $ cmd , 0 ) ;
if ( $ ::RUNCMD_RC != 0 )
{
2013-09-11 09:24:57 +00:00
$ cmd = "/usr/bin/startsrc -s $service" ;
@ output = xCAT::Utils - > runcmd ( $ cmd , 0 ) ;
my $ outp = join ( '' , @ output ) ;
if ( $ ::RUNCMD_RC != 0 )
{
my $ rsp = { } ;
$ rsp - > { data } - > [ 0 ] = "Command failed: $cmd. Error message: $outp.\n" ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ callback ) ;
return ;
}
2013-04-27 06:04:50 +00:00
}
}
else
{
my $ cmd = "service $service status|grep running" ;
my @ output = xCAT::Utils - > runcmd ( $ cmd , 0 ) ;
if ( $ ::RUNCMD_RC != 0 )
{
2013-09-11 09:24:57 +00:00
$ cmd = "service $service start" ;
@ output = xCAT::Utils - > runcmd ( $ cmd , 0 ) ;
my $ outp = join ( '' , @ output ) ;
if ( $ ::RUNCMD_RC != 0 )
{
my $ rsp = { } ;
$ rsp - > { data } - > [ 0 ] = "Command failed: $cmd. Error message: $outp.\n" ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ callback ) ;
return ;
}
2013-04-27 06:04:50 +00:00
}
2013-09-11 09:24:57 +00:00
}
2010-03-12 22:06:59 +00:00
#now we stick to Net::DNS style updates, with TSIG if possible. TODO: kerberized (i.e. Windows) DNS server support, maybe needing to use nsupdate -g....
2012-02-20 04:26:38 +00:00
if ( $ external )
{
2013-01-18 15:22:04 +00:00
# based on /etc/resolv.conf
$ ctx - > { resolver } = Net::DNS::Resolver - > new ( ) ;
2012-02-20 04:26:38 +00:00
}
else
{
2013-01-18 15:22:04 +00:00
# default to localhost
$ ctx - > { resolver } = Net::DNS::Resolver - > new ( nameservers = > [ '127.0.0.1' ] ) ;
2012-02-20 04:26:38 +00:00
}
2013-01-18 15:22:04 +00:00
2013-09-04 09:46:22 +00:00
my $ ret = add_or_delete_records ( $ ctx ) ;
unless ( $ ret ) {
xCAT::SvrUtils:: sendmsg ( "DNS setup is completed" , $ callback ) ;
}
2012-02-21 02:04:35 +00:00
2012-09-25 09:19:21 +00:00
umask ( $ oldmask ) ;
2010-03-12 22:06:59 +00:00
}
2010-12-01 13:00:18 +00:00
sub get_zonesdir {
my $ ZonesDir = get_dbdir ( ) ;
2012-08-09 04:07:40 +00:00
my @ entries = xCAT::TableUtils - > get_site_attribute ( "bindzones" ) ;
2012-05-22 05:58:08 +00:00
my $ site_entry = $ entries [ 0 ] ;
2010-12-01 13:00:18 +00:00
2013-01-18 15:22:04 +00:00
if ( defined ( $ site_entry ) ) {
$ ZonesDir = $ site_entry ;
}
2010-12-01 13:00:18 +00:00
return "$ZonesDir" ;
}
sub get_conf {
my $ conf = "/etc/named.conf" ;
2013-01-18 15:22:04 +00:00
2011-10-06 17:45:08 +00:00
# is this ubuntu ?
2013-01-18 15:22:04 +00:00
if ( $ distro =~ /ubuntu.*/i || $ distro =~ /debian.*/i ) {
2011-10-06 17:45:08 +00:00
$ conf = "/etc/bind/named.conf" ;
}
2012-08-09 04:07:40 +00:00
my @ entries = xCAT::TableUtils - > get_site_attribute ( "bindconf" ) ;
2012-05-22 05:58:08 +00:00
my $ site_entry = $ entries [ 0 ] ;
2010-12-01 13:00:18 +00:00
2013-01-18 15:22:04 +00:00
if ( defined ( $ site_entry ) ) {
$ conf = $ site_entry ;
}
2010-12-01 13:00:18 +00:00
return "$conf" ;
}
2010-03-15 21:11:51 +00:00
sub get_dbdir {
2010-12-01 13:00:18 +00:00
my $ DBDir ;
2012-08-09 04:07:40 +00:00
my @ entries = xCAT::TableUtils - > get_site_attribute ( "binddir" ) ;
2012-05-22 05:58:08 +00:00
my $ site_entry = $ entries [ 0 ] ;
2013-01-18 15:22:04 +00:00
if ( defined ( $ site_entry ) ) {
$ DBDir = $ site_entry ;
}
2010-12-01 13:00:18 +00:00
if ( - d "$DBDir" ) {
return "$DBDir"
} elsif ( - d "/var/named" ) {
2010-03-15 21:11:51 +00:00
return "/var/named/" ;
} elsif ( - d "/var/lib/named" ) {
2011-07-19 06:26:52 +00:00
# Temp fix for bugzilla 73119
chown ( scalar ( getpwnam ( 'root' ) ) , scalar ( getgrnam ( 'named' ) ) , "/var/lib/named" ) ;
2010-03-15 21:11:51 +00:00
return "/var/lib/named/" ;
2011-08-15 00:30:40 +00:00
}
elsif ( - d "/var/lib/bind" ) {
return "/var/lib/bind/" ;
}
else {
2010-03-15 21:11:51 +00:00
mkpath "/var/named/" ;
2010-03-17 14:03:21 +00:00
chown ( scalar ( getpwnam ( 'named' ) ) , scalar ( getgrnam ( 'named' ) ) , "/var/named" ) ;
2010-03-15 21:11:51 +00:00
return "/var/named/" ;
}
}
2010-05-27 15:57:56 +00:00
sub isvalidip {
#inet_pton/ntop good for ensuring an ip looks like an ip? or do string compare manually?
#for now, do string analysis, one problem with pton/ntop is that 010.1.1.1 would look diff from 10.1.1.1)
my $ candidate = shift ;
if ( $ candidate =~ /^(\d+)\.(\d+)\.(\d+).(\d+)\z/ ) {
return (
$ 1 >= 0 and $ 1 <= 255 and
$ 2 >= 0 and $ 2 <= 255 and
$ 3 >= 0 and $ 3 <= 255 and
$ 4 >= 0 and $ 4 <= 255
) ;
}
}
2010-03-15 21:11:51 +00:00
sub update_zones {
my $ ctx = shift ;
my $ currzone ;
my $ dbdir = $ ctx - > { dbdir } ;
my $ name = hostname ;
2010-03-16 20:36:25 +00:00
my $ node = $ name ;
2011-06-23 07:10:36 +00:00
2013-01-18 15:22:04 +00:00
# get the domain for the node - which is the local hostname
my ( $ host , $ nip ) = xCAT::NetworkUtils - > gethostnameandip ( $ node ) ;
my @ hosts ;
push ( @ hosts , $ host ) ;
my $ nd = xCAT::NetworkUtils - > getNodeDomains ( \ @ hosts ) ;
my % nodedomains = %$ nd ;
my $ domain = $ nodedomains { $ host } ;
2011-06-23 07:10:36 +00:00
xCAT::SvrUtils:: sendmsg ( "Updating zones." , $ callback ) ;
2013-01-18 15:22:04 +00:00
2010-03-16 20:36:25 +00:00
unless ( $ domain =~ /^\./ ) {
$ domain = '.' . $ domain ;
}
2010-03-15 21:11:51 +00:00
unless ( $ name =~ /\./ ) {
$ name . = $ domain ;
}
unless ( $ name =~ /\.\z/ ) {
$ name . = '.' ;
}
2010-03-16 20:36:25 +00:00
my $ ip = $ node ;
if ( $ ctx - > { hoststab } and $ ctx - > { hoststab } - > { $ node } and $ ctx - > { hoststab } - > { $ node } - > [ 0 ] - > { ip } ) {
$ ip = $ ctx - > { hoststab } - > { $ node } - > [ 0 ] - > { ip } ;
2010-05-27 15:57:56 +00:00
unless ( isvalidip ( $ ip ) ) {
2010-08-06 15:29:07 +00:00
xCAT::SvrUtils:: sendmsg ( [ 1 , "The hosts table entry for $node indicates $ip as an ip address, which is not a valid address" ] , $ callback ) ;
2010-05-27 15:57:56 +00:00
next ;
}
2010-03-16 20:36:25 +00:00
} else {
unless ( $ ip = inet_aton ( $ ip ) ) {
print "Unable to find an IP for $node in hosts table or via system lookup (i.e. /etc/hosts" ;
2010-08-06 15:29:07 +00:00
xCAT::SvrUtils:: sendmsg ( [ 1 , "Unable to find an IP for $node in hosts table or via system lookup (i.e. /etc/hosts" ] , $ callback ) ;
2010-03-16 20:36:25 +00:00
next ;
}
$ ip = inet_ntoa ( $ ip ) ;
}
2010-03-15 21:11:51 +00:00
my @ neededzones = keys % { $ ctx - > { zonestotouch } } ;
push @ neededzones , keys % { $ ctx - > { adzones } } ;
my ( $ sec , $ min , $ hour , $ mday , $ mon , $ year , $ rest ) = localtime ( time ) ;
my $ serial = ( $ mday * 100 ) + ( ( $ mon + 1 ) * 10000 ) + ( ( $ year + 1900 ) * 1000000 ) ;
2013-01-18 15:22:04 +00:00
2010-03-15 21:11:51 +00:00
foreach $ currzone ( @ neededzones ) {
2013-01-18 15:22:04 +00:00
my $ zonefilename = $ currzone ;
2010-03-15 21:11:51 +00:00
if ( $ currzone =~ /IN-ADDR\.ARPA/ ) {
$ currzone =~ s/\.IN-ADDR\.ARPA.*// ;
my @ octets = split /\./ , $ currzone ;
$ currzone = join ( '.' , reverse ( @ octets ) ) ;
2010-04-02 18:09:58 +00:00
$ zonefilename = $ currzone ;
2010-06-08 19:14:48 +00:00
#If needed, the below, but it was a fairly painfully restricted paradigm for zonefile names...
#} elsif (not $zonefilename =~ /_/) {
# $zonefilename =~ s/\..*//; #compatible with bind.pm
2010-03-15 21:11:51 +00:00
}
2010-04-01 19:19:04 +00:00
unless ( - f $ dbdir . "/db.$zonefilename" ) {
2010-03-15 21:11:51 +00:00
my $ zonehdl ;
2010-04-01 19:19:04 +00:00
open ( $ zonehdl , ">>" , $ dbdir . "/db.$zonefilename" ) ;
2010-03-17 14:03:21 +00:00
flock ( $ zonehdl , LOCK_EX ) ;
seek ( $ zonehdl , 0 , 0 ) ;
truncate ( $ zonehdl , 0 ) ;
2010-03-15 21:11:51 +00:00
print $ zonehdl '$TTL 86400' . "\n" ;
print $ zonehdl '@ IN SOA ' . $ name . " root.$name ( $serial 10800 3600 604800 86400 )\n" ;
print $ zonehdl " IN NS $name\n" ;
2010-03-16 20:36:25 +00:00
if ( $ name =~ /$currzone/ ) { #Must guarantee an A record for the DNS server
print $ zonehdl "$name IN A $ip\n" ;
}
2010-03-17 14:03:21 +00:00
flock ( $ zonehdl , LOCK_UN ) ;
2010-03-15 21:11:51 +00:00
close ( $ zonehdl ) ;
2013-01-18 15:22:04 +00:00
if ( $ distro =~ /ubuntu.*/i || $ distro =~ /debian.*/i ) {
2012-12-13 06:05:46 +00:00
chown ( scalar ( getpwnam ( 'root' ) ) , scalar ( getgrnam ( 'bind' ) ) , $ dbdir . "/db.$zonefilename" ) ;
}
else {
chown ( scalar ( getpwnam ( 'named' ) ) , scalar ( getgrnam ( 'named' ) ) , $ dbdir . "/db.$zonefilename" ) ;
}
2010-03-15 21:11:51 +00:00
$ ctx - > { restartneeded } = 1 ;
}
}
2011-06-23 07:10:36 +00:00
xCAT::SvrUtils:: sendmsg ( "Completed updating zones." , $ callback ) ;
2010-03-15 21:11:51 +00:00
}
2010-03-12 22:06:59 +00:00
sub update_namedconf {
my $ ctx = shift ;
2013-12-18 06:35:15 +00:00
my $ slave = shift ;
2010-12-01 13:00:18 +00:00
my $ namedlocation = get_conf ( ) ;
2010-03-12 22:06:59 +00:00
my $ nameconf ;
my @ newnamed ;
my $ gotoptions = 0 ;
my $ gotkey = 0 ;
my % didzones ;
2013-01-18 15:22:04 +00:00
2010-03-12 22:06:59 +00:00
if ( - r $ namedlocation ) {
my @ currnamed = ( ) ;
open ( $ nameconf , "<" , $ namedlocation ) ;
2010-03-17 14:03:21 +00:00
flock ( $ nameconf , LOCK_SH ) ;
2010-03-12 22:06:59 +00:00
@ currnamed = <$nameconf> ;
2010-03-17 14:03:21 +00:00
flock ( $ nameconf , LOCK_UN ) ;
2010-03-12 22:06:59 +00:00
close ( $ nameconf ) ;
my $ i = 0 ;
for ( $ i = 0 ; $ i < scalar ( @ currnamed ) ; $ i + + ) {
my $ line = $ currnamed [ $ i ] ;
if ( $ line =~ /^options +\{/ ) {
$ gotoptions = 1 ;
my $ skip = 0 ;
do {
2011-08-03 19:57:41 +00:00
#push @newnamed,"\t\t//listen-on-v6 { any; };\n";
2010-03-12 22:06:59 +00:00
if ( $ ctx - > { forwarders } and $ line =~ /forwarders {/ ) {
2010-03-15 21:11:51 +00:00
push @ newnamed , "\tforwarders \{\n" ;
2010-03-12 22:06:59 +00:00
$ skip = 1 ;
foreach ( @ { $ ctx - > { forwarders } } ) {
2010-03-15 21:11:51 +00:00
push @ newnamed , "\t\t" . $ _ . ";\n" ;
2010-03-12 22:06:59 +00:00
}
2010-03-15 21:11:51 +00:00
push @ newnamed , "\t};\n" ;
2013-12-18 06:35:15 +00:00
} elsif ( $ ctx - > { slaves } and $ line =~ /allow-transfer {/ ) {
push @ newnamed , "\tallow-transfer \{\n" ;
$ skip = 1 ;
foreach ( @ { $ ctx - > { slaves } } ) {
push @ newnamed , "\t\t" . $ _ . ";\n" ;
}
push @ newnamed , "\t};\n" ;
} elsif ( $ ctx - > { slaves } and $ line =~ /also-notify {/ ) {
push @ newnamed , "\talso-notify \{\n" ;
$ skip = 1 ;
foreach ( @ { $ ctx - > { slaves } } ) {
push @ newnamed , "\t\t" . $ _ . ";\n" ;
}
push @ newnamed , "\t};\n" ;
2010-03-12 22:06:59 +00:00
} elsif ( $ skip ) {
2010-03-15 21:11:51 +00:00
if ( $ line =~ /};/ ) {
2010-03-12 22:06:59 +00:00
$ skip = 0 ;
}
} else {
push @ newnamed , $ line ;
}
$ i + + ;
$ line = $ currnamed [ $ i ] ;
} while ( $ line !~ /^\};/ ) ;
2010-03-15 21:11:51 +00:00
push @ newnamed , $ line ;
2010-03-12 22:06:59 +00:00
} elsif ( $ line =~ /^zone "([^"]*)" in \{/ ) {
my $ currzone = $ 1 ;
if ( $ ctx - > { zonestotouch } - > { $ currzone } or $ ctx - > { adzones } - > { $ currzone } ) {
$ didzones { $ currzone } = 1 ;
2010-03-15 21:11:51 +00:00
my @ candidate = ( $ line ) ;
my $ needreplace = 1 ;
do {
$ i + + ;
$ line = $ currnamed [ $ i ] ;
push @ candidate , $ line ;
if ( $ line =~ /key xcat_key/ ) {
$ needreplace = 0 ;
}
} while ( $ line !~ /^\};/ ) ; #skip the old file zone
unless ( $ needreplace ) {
push @ newnamed , @ candidate ;
next ;
}
$ ctx - > { restartneeded } = 1 ;
2010-03-12 22:06:59 +00:00
push @ newnamed , "zone \"$currzone\" in {\n" , "\ttype master;\n" , "\tallow-update {\n" , "\t\tkey xcat_key;\n" ;
my @ list ;
2010-03-15 21:11:51 +00:00
if ( not $ ctx - > { adzones } - > { $ currzone } ) {
if ( $ ctx - > { dnsupdaters } ) {
@ list = @ { $ ctx - > { dnsupdaters } } ;
}
2010-03-12 22:06:59 +00:00
} else {
2010-03-15 21:11:51 +00:00
if ( $ ctx - > { adservers } ) {
@ list = @ { $ ctx - > { adservers } } ;
}
2010-03-12 22:06:59 +00:00
}
foreach ( @ list ) {
2010-03-15 21:11:51 +00:00
push @ newnamed , "\t\t$_;\n" ;
2010-03-12 22:06:59 +00:00
}
2010-03-15 21:11:51 +00:00
if ( $ currzone =~ /IN-ADDR\.ARPA/ ) {
my $ net = $ currzone ;
$ net =~ s/.IN-ADDR\.ARPA.*// ;
my @ octets = split /\./ , $ net ;
$ net = join ( '.' , reverse ( @ octets ) ) ;
push @ newnamed , "\t};\n" , "\tfile \"db.$net\";\n" , "};\n" ;
} else {
2010-04-01 19:19:04 +00:00
my $ zfilename = $ currzone ;
2010-06-09 18:53:46 +00:00
#$zfilename =~ s/\..*//;
2010-04-01 19:19:04 +00:00
push @ newnamed , "\t};\n" , "\tfile \"db.$zfilename\";\n" , "};\n" ;
2010-03-12 22:06:59 +00:00
}
} else {
push @ newnamed , $ line ;
2010-03-15 21:11:51 +00:00
do {
$ i + + ;
$ line = $ currnamed [ $ i ] ;
push @ newnamed , $ line ;
} while ( $ line !~ /^\};/ ) ;
2010-03-12 22:06:59 +00:00
}
2010-03-15 21:11:51 +00:00
2010-03-12 22:06:59 +00:00
} elsif ( $ line =~ /^key xcat_key/ ) {
$ gotkey = 1 ;
if ( $ ctx - > { privkey } ) {
2010-03-15 21:11:51 +00:00
#for now, assume the field is correct
#push @newnamed,"key xcat_key {\n","\talgorithm hmac-md5;\n","\tsecret \"".$ctx->{privkey}."\";\n","};\n\n";
push @ newnamed , $ line ;
do {
$ i + + ;
$ line = $ currnamed [ $ i ] ;
push @ newnamed , $ line ;
} while ( $ line !~ /^\};/ ) ;
2010-03-12 22:06:59 +00:00
} else {
2010-03-15 21:11:51 +00:00
push @ newnamed , $ line ;
2010-03-12 22:06:59 +00:00
while ( $ line !~ /^\};/ ) { #skip the old file zone
if ( $ line =~ /secret \"([^"]*)\"/ ) {
my $ passtab = xCAT::Table - > new ( "passwd" , - create = > 1 ) ;
2010-04-01 14:09:42 +00:00
$ passtab - > setAttribs ( { key = > "omapi" , username = > "xcat_key" } , { password = > $ 1 } ) ;
2010-03-12 22:06:59 +00:00
}
$ i + + ;
$ line = $ currnamed [ $ i ] ;
2010-03-15 21:11:51 +00:00
push @ newnamed , $ line ;
2010-03-12 22:06:59 +00:00
}
}
2010-03-15 21:11:51 +00:00
} else {
push @ newnamed , $ line ;
2010-03-12 22:06:59 +00:00
}
}
}
unless ( $ gotoptions ) {
2010-12-01 13:00:18 +00:00
push @ newnamed , "options {\n" , "\tdirectory \"" . $ ctx - > { zonesdir } . "\";\n" ;
2011-07-21 14:37:54 +00:00
push @ newnamed , "\t\t//listen-on-v6 { any; };\n" ;
2010-03-12 22:06:59 +00:00
if ( $ ctx - > { forwarders } ) {
push @ newnamed , "\tforwarders {\n" ;
foreach ( @ { $ ctx - > { forwarders } } ) {
push @ newnamed , "\t\t$_;\n" ;
}
push @ newnamed , "\t};\n" ;
}
2013-12-18 06:35:15 +00:00
if ( $ slave ) {
push @ newnamed , "\tallow-transfer { any; };\n" ;
} else {
if ( $ ctx - > { slaves } ) {
push @ newnamed , "\tnotify yes;\n" ;
push @ newnamed , "\tallow-transfer {\n" ;
foreach ( @ { $ ctx - > { slaves } } ) {
push @ newnamed , "\t\t$_;\n" ;
}
push @ newnamed , "\t};\n" ;
push @ newnamed , "\talso-notify {\n" ;
foreach ( @ { $ ctx - > { slaves } } ) {
push @ newnamed , "\t\t$_;\n" ;
}
push @ newnamed , "\t};\n" ;
}
}
2010-03-15 21:11:51 +00:00
push @ newnamed , "};\n\n" ;
2010-03-12 22:06:59 +00:00
}
2013-12-18 06:35:15 +00:00
unless ( $ slave ) {
unless ( $ gotkey ) {
unless ( $ ctx - > { privkey } ) { #need to generate one
$ ctx - > { privkey } = encode_base64 ( genpassword ( 32 ) ) ;
chomp ( $ ctx - > { privkey } ) ;
}
push @ newnamed , "key xcat_key {\n" , "\talgorithm hmac-md5;\n" , "\tsecret \"" . $ ctx - > { privkey } . "\";\n" , "};\n\n" ;
$ ctx - > { restartneeded } = 1 ;
}
2010-03-12 22:06:59 +00:00
}
2013-12-18 06:35:15 +00:00
my $ cmd = "grep '^nameserver' /etc/resolv.conf | awk '{print $2}'" ;
my @ output = xCAT::Utils - > runcmd ( $ cmd , 0 ) ;
2010-03-12 22:06:59 +00:00
my $ zone ;
foreach $ zone ( keys % { $ ctx - > { zonestotouch } } ) {
if ( $ didzones { $ zone } ) { next ; }
2010-03-15 21:11:51 +00:00
$ ctx - > { restartneeded } = 1 ; #have to add a zone, a restart will be needed
2013-12-18 06:35:15 +00:00
push @ newnamed , "zone \"$zone\" in {\n" ;
if ( $ slave ) {
push @ newnamed , "\ttype slave;\n" ;
push @ newnamed , "\tmasters { $output[0]; };\n" ;
} else {
push @ newnamed , "\ttype master;\n" , "\tallow-update {\n" , "\t\tkey xcat_key;\n" ;
foreach ( @ { $ ctx - > { dnsupdaters } } ) {
push @ newnamed , "\t\t$_;\n" ;
}
2010-03-15 21:11:51 +00:00
}
if ( $ zone =~ /IN-ADDR\.ARPA/ ) {
my $ net = $ zone ;
$ net =~ s/.IN-ADDR\.ARPA.*// ;
my @ octets = split /\./ , $ net ;
$ net = join ( '.' , reverse ( @ octets ) ) ;
push @ newnamed , "\t};\n" , "\tfile \"db.$net\";\n" , "};\n" ;
} else {
2010-04-01 19:19:04 +00:00
my $ zfilename = $ zone ;
2010-06-09 18:53:46 +00:00
#$zfilename =~ s/\..*//;
2010-04-01 19:19:04 +00:00
push @ newnamed , "\t};\n" , "\tfile \"db.$zfilename\";\n" , "};\n" ;
2010-03-12 22:06:59 +00:00
}
}
foreach $ zone ( keys % { $ ctx - > { adzones } } ) {
if ( $ didzones { $ zone } ) { next ; }
2010-03-15 21:11:51 +00:00
$ ctx - > { restartneeded } = 1 ; #have to add a zone, a restart will be needed
2013-12-18 06:35:15 +00:00
push @ newnamed , "zone \"$zone\" in {\n" ;
if ( $ slave ) {
push @ newnamed , "\ttype slave;\n" ;
push @ newnamed , "\tmasters { $output[0]; };\n" ;
} else {
push @ newnamed , "\ttype master;\n" , "\tallow-update {\n" , "\t\tkey xcat_key;\n" ;
foreach ( @ { $ ctx - > { adservers } } ) {
push @ newnamed , "\t\t$_;\n" ;
}
2010-03-12 22:06:59 +00:00
}
2010-04-01 19:19:04 +00:00
my $ zfilename = $ zone ;
2010-06-09 18:53:46 +00:00
#$zfilename =~ s/\..*//;
2010-04-01 19:19:04 +00:00
push @ newnamed , "\t};\n" , "\tfile \"db.$zfilename\";\n" , "};\n\n" ;
2010-03-12 22:06:59 +00:00
}
2011-07-20 06:45:30 +00:00
# For AIX, add a hint zone
if ( xCAT::Utils - > isAIX ( ) )
{
unless ( grep ( /hint/ , @ newnamed ) )
{
push @ newnamed , "zone \"\.\" in {\n" , "\ttype hint;\n" , "\tfile \"db\.cache\";\n" , "};\n\n" ;
# Toutch the stub zone file
system ( "/usr/bin/touch $ctx->{dbdir}.'/db.cache'" ) ;
$ ctx - > { restartneeded } = 1 ;
2011-09-13 02:29:29 +00:00
if ( $ ctx - > { forwarders } )
{
my $ dbcachefile = $ ctx - > { dbdir } . '/db.cache' ;
my $ cmd = qq~dig @"$ctx->{forwarders}[0]" . ns >> $dbcachefile~ ;
my $ outref = xCAT::Utils - > runcmd ( "$cmd" , 0 ) ;
if ( $ ::RUNCMD_RC != 0 )
{
my $ rsp = { } ;
$ rsp - > { data } - > [ 0 ] = "Failed to run command: $cmd.\n" ;
xCAT::MsgUtils - > message ( "W" , $ rsp , $ callback , 1 ) ;
}
}
2011-07-20 06:45:30 +00:00
}
}
2013-01-18 15:22:04 +00:00
2010-03-15 21:11:51 +00:00
my $ newnameconf ;
2010-03-17 14:03:21 +00:00
open ( $ newnameconf , ">>" , $ namedlocation ) ;
flock ( $ newnameconf , LOCK_EX ) ;
seek ( $ newnameconf , 0 , 0 ) ;
truncate ( $ newnameconf , 0 ) ;
2010-03-15 21:11:51 +00:00
for my $ l ( @ newnamed ) { print $ newnameconf $ l ; }
2010-03-17 14:03:21 +00:00
flock ( $ newnameconf , LOCK_UN ) ;
2010-03-15 21:11:51 +00:00
close ( $ newnameconf ) ;
2013-01-18 15:22:04 +00:00
if ( $ distro =~ /ubuntu.*/i || $ distro =~ /debian.*/i ) {
2012-11-28 03:23:12 +00:00
chown ( scalar ( getpwnam ( 'root' ) ) , scalar ( getgrnam ( 'bind' ) ) , $ namedlocation ) ;
}
else {
chown ( scalar ( getpwnam ( 'root' ) ) , scalar ( getgrnam ( 'named' ) ) , $ namedlocation ) ;
}
2010-03-12 22:06:59 +00:00
}
2010-08-25 13:13:44 +00:00
sub add_or_delete_records {
2010-03-15 21:11:51 +00:00
my $ ctx = shift ;
2011-06-23 07:10:36 +00:00
2012-06-15 05:18:56 +00:00
xCAT::SvrUtils:: sendmsg ( "Updating DNS records, this may take several minutes for a large cluster." , $ callback ) ;
2011-06-23 07:10:36 +00:00
2010-03-15 21:11:51 +00:00
unless ( $ ctx - > { privkey } ) {
my $ passtab = xCAT::Table - > new ( 'passwd' ) ;
my $ pent = $ passtab - > getAttribs ( { key = > 'omapi' , username = > 'xcat_key' } , [ 'password' ] ) ;
if ( $ pent and $ pent - > { password } ) {
$ ctx - > { privkey } = $ pent - > { password } ;
} else {
2010-08-06 15:29:07 +00:00
xCAT::SvrUtils:: sendmsg ( [ 1 , "Unable to find omapi key in passwd table" ] , $ callback ) ;
2010-03-15 21:11:51 +00:00
}
}
my $ node ;
2011-01-26 18:19:45 +00:00
my @ ips ;
2013-01-18 15:22:04 +00:00
2010-03-16 20:36:25 +00:00
$ ctx - > { nsmap } = { } ; #will store a map to known NS records to avoid needless redundant queries to sort nodes into domains
$ ctx - > { updatesbyzone } = { } ; #sort all updates into their respective zones for bulk update for fewer DNS transactions
2013-01-18 15:22:04 +00:00
# get node domains
my $ nd = xCAT::NetworkUtils - > getNodeDomains ( \ @ { $ ctx - > { nodes } } ) ;
my % nodedomains = % { $ nd } ;
2010-03-15 21:11:51 +00:00
foreach $ node ( @ { $ ctx - > { nodes } } ) {
2010-03-16 20:36:25 +00:00
my $ name = $ node ;
2011-04-12 11:22:43 +00:00
if ( ( $ name =~ /loopback/ ) || ( $ name =~ /localhost/ ) )
{
next ;
}
2013-01-18 15:22:04 +00:00
my $ domain = $ nodedomains { $ node } ;
unless ( $ domain =~ /^\./ ) { $ domain = '.' . $ domain ; }
2010-03-16 20:36:25 +00:00
unless ( $ name =~ /$domain/ ) { $ name . = $ domain } # $name needs to represent fqdn, but must preserve $node as a nodename for cfg lookup
2013-01-18 15:22:04 +00:00
2010-03-15 21:11:51 +00:00
if ( $ ctx - > { hoststab } and $ ctx - > { hoststab } - > { $ node } and $ ctx - > { hoststab } - > { $ node } - > [ 0 ] - > { ip } ) {
2011-01-26 18:19:45 +00:00
@ ips = ( $ ctx - > { hoststab } - > { $ node } - > [ 0 ] - > { ip } ) ;
2010-03-15 21:11:51 +00:00
} else {
2011-01-26 18:19:45 +00:00
@ ips = getipaddr ( $ node , GetAllAddresses = > 1 ) ;
2013-04-22 19:09:36 +00:00
if ( not @ ips and keys % { $ ctx - > { nodeips } - > { $ node } } ) {
@ ips = keys % { $ ctx - > { nodeips } - > { $ node } } ;
}
2012-02-03 09:39:54 +00:00
if ( ! defined ( $ ips [ 0 ] ) ) {
xCAT::SvrUtils:: sendmsg ( [ 1 , "Unable to find an IP for $node in hosts table or via system lookup (i.e. /etc/hosts)" ] , $ callback ) ;
2010-03-15 21:11:51 +00:00
next ;
}
2011-01-26 18:19:45 +00:00
}
foreach my $ ip ( @ ips ) {
$ ctx - > { currip } = $ ip ;
#time to update, A and PTR records, IPv6 still TODO
if ( $ ip =~ /\./ ) { #v4
$ ip = join ( '.' , reverse ( split ( /\./ , $ ip ) ) ) ;
$ ip . = '.IN-ADDR.ARPA.' ;
} elsif ( $ ip =~ /:/ ) { #v6
$ ip = getipaddr ( $ ip , GetNumber = > 1 ) ;
$ ip = $ ip - > as_hex ( ) ;
$ ip =~ s/^0x// ;
$ ip = join ( '.' , reverse ( split ( // , $ ip ) ) ) ;
$ ip . = '.ip6.arpa.' ;
} else {
die "ddns did not understand $ip result of lookup" ;
}
#ok, now it is time to identify which zones should actually hold the forward (A) and reverse (PTR) records and a nameserver to handle the request
my $ revzone = $ ip ;
$ ctx - > { currnode } = $ node ;
$ ctx - > { currname } = $ name ;
$ ctx - > { currrevname } = $ ip ;
2011-08-12 11:17:36 +00:00
my $ tmpdm ;
unless ( $ domain =~ /\.$/ ) { $ tmpdm = $ domain . '.' ; } #example.com becomes example.com.
2013-01-18 15:22:04 +00:00
2011-01-26 18:19:45 +00:00
find_nameserver_for_dns ( $ ctx , $ revzone ) ;
2011-08-12 11:17:36 +00:00
find_nameserver_for_dns ( $ ctx , $ tmpdm ) ;
2011-01-26 18:19:45 +00:00
}
2010-03-15 21:11:51 +00:00
}
2010-03-16 20:36:25 +00:00
my $ zone ;
foreach $ zone ( keys % { $ ctx - > { updatesbyzone } } ) {
2013-09-04 09:46:22 +00:00
my $ ip = xCAT::NetworkUtils - > getipaddr ( $ ctx - > { nsmap } - > { $ zone } ) ;
if ( ! defined $ ip ) {
xCAT::SvrUtils:: sendmsg ( [ 1 , "Please make sure $ctx->{nsmap}->{$zone} exist in /etc/hosts or DNS." ] , $ callback ) ;
return 1 ;
}
2012-11-26 08:12:39 +00:00
my $ resolver = Net::DNS::Resolver - > new ( nameservers = > [ $ ip ] ) ;
2010-03-17 14:03:21 +00:00
my $ entry ;
2010-04-05 19:39:51 +00:00
my $ numreqs = 300 ; # limit to 300 updates in a payload, something broke at 644 on a certain sample, choosing 300 for now
2010-03-16 20:36:25 +00:00
my $ update = Net::DNS::Update - > new ( $ zone ) ;
2010-03-17 14:03:21 +00:00
foreach $ entry ( @ { $ ctx - > { updatesbyzone } - > { $ zone } } ) {
2010-08-25 13:13:44 +00:00
if ( $ ctx - > { deletemode } ) {
$ update - > push ( update = > rr_del ( $ entry ) ) ;
} else {
$ update - > push ( update = > rr_add ( $ entry ) ) ;
}
2010-04-05 19:39:51 +00:00
$ numreqs -= 1 ;
if ( $ numreqs == 0 ) {
$ update - > sign_tsig ( "xcat_key" , $ ctx - > { privkey } ) ;
$ numreqs = 300 ;
my $ reply = $ resolver - > send ( $ update ) ;
2012-11-29 05:59:36 +00:00
if ( $ reply )
{
if ( $ reply - > header - > rcode ne 'NOERROR' )
{
2013-09-11 09:24:57 +00:00
xCAT::SvrUtils:: sendmsg ( [ 1 , "Failure encountered updating $zone, error was " . $ reply - > header - > rcode . ". See more details in system log." ] , $ callback ) ;
2012-11-29 05:59:36 +00:00
}
2010-04-05 19:39:51 +00:00
}
2012-11-29 05:59:36 +00:00
else
{
xCAT::SvrUtils:: sendmsg ( [ 1 , "No reply received when sending DNS update to zone $zone" ] , $ callback ) ;
}
2010-04-05 19:39:51 +00:00
$ update = Net::DNS::Update - > new ( $ zone ) ; #new empty request
}
}
2010-04-05 20:12:16 +00:00
if ( $ numreqs != 300 ) { #either no entries at all to begin with or a perfect multiple of 300
2010-04-05 19:39:51 +00:00
$ update - > sign_tsig ( "xcat_key" , $ ctx - > { privkey } ) ;
my $ reply = $ resolver - > send ( $ update ) ;
2012-11-29 05:59:36 +00:00
if ( $ reply )
{
if ( $ reply - > header - > rcode ne 'NOERROR' )
{
2013-09-11 09:24:57 +00:00
xCAT::SvrUtils:: sendmsg ( [ 1 , "Failure encountered updating $zone, error was " . $ reply - > header - > rcode . ". See more details in system log." ] , $ callback ) ;
2012-11-29 05:59:36 +00:00
}
}
else
{
xCAT::SvrUtils:: sendmsg ( [ 1 , "No reply received when sending DNS update to zone $zone" ] , $ callback ) ;
}
2011-08-19 11:32:43 +00:00
2011-05-13 07:31:18 +00:00
# sometimes resolver does not work if the update zone request sent so quick
sleep 1 ;
2010-03-16 20:36:25 +00:00
}
}
2011-06-23 07:10:36 +00:00
xCAT::SvrUtils:: sendmsg ( "Completed updating DNS records." , $ callback ) ;
2010-03-16 20:36:25 +00:00
}
sub find_nameserver_for_dns {
my $ ctx = shift ;
my $ zone = shift ;
my $ node = $ ctx - > { currnode } ;
my $ ip = $ ctx - > { currip } ;
my $ rname = $ ctx - > { currrevname } ;
my $ name = $ ctx - > { currname } ;
unless ( $ name =~ /\.\z/ ) { $ name . = '.' }
2011-01-26 18:19:45 +00:00
my @ rrcontent ;
if ( $ ip =~ /:/ ) {
@ rrcontent = ( "$name IN AAAA $ip" ) ;
} else {
@ rrcontent = ( "$name IN A $ip" ) ;
}
2010-03-19 13:23:17 +00:00
foreach ( keys % { $ ctx - > { nodeips } - > { $ node } } ) {
unless ( $ _ eq $ ip ) {
2011-01-26 18:19:45 +00:00
if ( $ _ =~ /:/ ) {
push @ rrcontent , "$name IN AAAA $_" ;
} else {
push @ rrcontent , "$name IN A $_" ;
}
2010-03-19 13:23:17 +00:00
}
}
2013-01-18 15:22:04 +00:00
2012-02-16 02:38:43 +00:00
if ( defined $ ctx - > { aliases } - > { $ node } ) {
foreach ( keys % { $ ctx - > { aliases } - > { $ node } } ) {
push @ rrcontent , "$_ IN CNAME $name" ;
}
}
2010-09-27 20:21:39 +00:00
if ( $ ctx - > { deletemode } ) {
push @ rrcontent , "$name TXT" ;
push @ rrcontent , "$name A" ;
}
2011-01-26 18:19:45 +00:00
if ( $ zone =~ /IN-ADDR.ARPA/ or $ zone =~ /ip6.arpa/ ) { #reverse style
2010-03-19 13:23:17 +00:00
@ rrcontent = ( "$rname IN PTR $name" ) ;
2010-03-16 20:36:25 +00:00
}
while ( $ zone ) {
unless ( defined $ ctx - > { nsmap } - > { $ zone } ) { #ok, we already thought about this zone and made a decision
2010-03-17 14:03:21 +00:00
if ( $ zone =~ /^\.*192.IN-ADDR.ARPA\.*/ or $ zone =~ /^\.*172.IN-ADDR.ARPA\.*/ or $ zone =~ /127.IN-ADDR.ARPA\.*/ or $ zone =~ /^\.*IN-ADDR.ARPA\.*/ or $ zone =~ /^\.*ARPA\.*/ ) {
$ ctx - > { nsmap } - > { $ zone } = 0 ; #ignore zones that are likely to appear, but probably not ours
2011-07-12 20:18:36 +00:00
} elsif ( $ ::XCATSITEVALS { ddnsserver } ) {
$ ctx - > { nsmap } - > { $ zone } = $ ::XCATSITEVALS { ddnsserver } ;
2010-03-17 14:03:21 +00:00
} else {
my $ reply = $ ctx - > { resolver } - > query ( $ zone , 'NS' ) ;
if ( $ reply ) {
2011-08-19 11:32:43 +00:00
if ( $ reply - > header - > rcode ne 'NOERROR' ) {
xCAT::SvrUtils:: sendmsg ( [ 1 , "Failure encountered querying $zone, error was " . $ reply - > header - > rcode ] , $ callback ) ;
}
2010-03-17 14:03:21 +00:00
foreach my $ record ( $ reply - > answer ) {
if ( $ record - > nsdname =~ /blackhole.*\.iana\.org/ ) {
$ ctx - > { nsmap } - > { $ zone } = 0 ;
} else {
$ ctx - > { nsmap } - > { $ zone } = $ record - > nsdname ;
}
}
} else {
$ ctx - > { nsmap } - > { $ zone } = 0 ;
}
2010-03-16 20:36:25 +00:00
}
}
2013-01-18 15:22:04 +00:00
2010-03-16 20:36:25 +00:00
if ( $ ctx - > { nsmap } - > { $ zone } ) { #we have a nameserver for this zone, therefore this zone is one to update
2010-03-19 13:23:17 +00:00
push @ { $ ctx - > { updatesbyzone } - > { $ zone } } , @ rrcontent ;
2010-03-16 20:36:25 +00:00
last ;
} else { #we have it defined, but zero, means search higher domains. Possible to shortcut further by pointing to the right domain, maybe later
2010-03-17 14:03:21 +00:00
if ( $ zone !~ /\./ ) {
2010-08-06 15:29:07 +00:00
xCAT::SvrUtils:: sendmsg ( [ 1 , "Unable to find reverse zone to hold $node" ] , $ callback , $ node ) ;
2010-03-17 14:03:21 +00:00
last ;
}
2010-03-16 20:36:25 +00:00
$ zone =~ s/^[^\.]*\.// ; #strip all up to and including first dot
unless ( $ zone ) {
2010-08-06 15:29:07 +00:00
xCAT::SvrUtils:: sendmsg ( [ 1 , "Unable to find zone to hold $node" ] , $ callback , $ node ) ;
2010-03-16 20:36:25 +00:00
last ;
}
}
}
}
2010-03-18 14:02:12 +00:00
sub genpassword
{
#Generate a pseudo-random password of specified length
my $ length = shift ;
my $ password = '' ;
my $ characters =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890' ;
srand ; #have to reseed, rand is not rand otherwise
while ( length ( $ password ) < $ length )
{
$ password . = substr ( $ characters , int ( rand 63 ) , 1 ) ;
}
return $ password ;
}
2011-04-14 02:38:36 +00:00
sub makedns_usage
{
my $ callback = shift ;
my $ rsp ;
push @ { $ rsp - > { data } } ,
"\n makedns - sets up domain name services (DNS)." ;
push @ { $ rsp - > { data } } , " Usage: " ;
push @ { $ rsp - > { data } } , "\tmakedns [-h|--help ]" ;
2012-02-20 04:26:38 +00:00
push @ { $ rsp - > { data } } , "\tmakedns [-e|--external] [-n|--new ] [noderange]" ;
push @ { $ rsp - > { data } } , "\tmakedns [-e|--external] [-d|--delete noderange]" ;
2011-04-14 02:38:36 +00:00
push @ { $ rsp - > { data } } , "\n" ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ callback ) ;
return 0 ;
}
2013-12-18 06:35:15 +00:00
sub get_dns_slave
{
# get all service nodes with servicenode.nameserver=2
my @ sns ;
my @ slaves ;
my $ sntab = xCAT::Table - > new ( 'servicenode' ) ;
my @ ents = $ sntab - > getAllAttribs ( 'node' , 'nameserver' ) ;
foreach my $ sn ( @ ents )
{
if ( $ sn - > { 'nameserver' } == 2 )
{
push @ sns , $ sn - > { 'node' } ;
}
}
@ slaves = xCAT::NodeRange:: noderange ( join ( ',' , @ sns ) ) ;
return \ @ slaves ;
}
2010-03-16 21:38:40 +00:00
1 ;