#!/usr/bin/perl -w # 2005/06/15 # Copyright (C) 2005, Guy Antony Halse, All Rights Reserved. use strict; use POSIX; require 5.6.0; # check we know the name of the zone if ($#ARGV != 2) { print STDERR "usage: $0 \n"; exit 1; } my ($domain, $soa, $netlogin) = @ARGV; # get the date for use in a zone file my ($date) = strftime('%Y%m%d', localtime()); # active directory requires several subdomains which it stores in different # zone files. These domains are listed here. my (@subdomain) = qw(_msdcs _sites _tcp _udp); my (%sdhandle); # open the zone files, and create empty journals for each one foreach my $sd (@subdomain) { open $sdhandle{"${sd}.${domain}"}, ">${sd}.${domain}" or die "error writing ${sd}.${domain}: $!"; # print the header information in the zone file print {$sdhandle{"${sd}.${domain}"}} << "EOM"; \$ORIGIN . \$TTL 3600 ; 1 hour ${sd}.${domain} IN SOA $soa. hostmaster.${soa}. ( ${date}00 ; serial 30 ; refresh (30 seconds) 900 ; retry (15 minutes) 120 ; expire (2 minutes) 30 ; minimum (30 seconds) ) NS ${soa}. \$ORIGIN ${sd}.${domain}. EOM # create an empty journal file open SD, ">$sd.$domain.jnl" or die "error creating journal for ${sd}.${domain}: $!"; close SD; } # open a file handle for the main (non-dynamic) zone open $sdhandle{".${domain}"}, ">${domain}" or die "error opening $domain: $!"; my (%netlogin); # start parsing netlogin.dns open NETLOGIN, "<$netlogin" or die "error reading netlogin: $!"; while () { chomp(); my ($host, $ttl, $entry) = split /\s+/, $_, 3; $entry =~ s/^IN\s+//; # work out which sub zone this should be in my ($zone) = ''; foreach my $sd (@subdomain) { if ($host =~ m/^.+\.${sd}\.${domain}\.$/) { $zone = $sd; last; } } push @{$netlogin{"${zone}.${domain}"}}, { -host => $host, -entry => $entry, -ttl => $ttl }; } # write zone files foreach my $zone (sort { $a cmp $b } keys %netlogin) { my ($ttl) = 0; foreach my $entry (0 .. $#{$netlogin{$zone}}) { if ($netlogin{$zone}[$entry]{"-ttl"} != $ttl) { $ttl = $netlogin{$zone}[$entry]{"-ttl"}; print {$sdhandle{$zone}} "\$TTL $ttl\n"; } my ($host) = $netlogin{$zone}[$entry]{"-host"} =~ m/^(.+)\.${zone}\./; $host = $netlogin{$zone}[$entry]{"-host"} if ! defined ($host); print {$sdhandle{$zone}} $host . (' ' x (60 - length($host))) . ' ' . $netlogin{$zone}[$entry]{"-entry"} . "\n"; } } # close the file handles foreach (keys %sdhandle) { close ($sdhandle{$_}); }