-#!/bin/bash
-set -e
-base=/var/named
-conf=$base/conf
-etcfile=/etc/named/zones-rgc
-
-check=true
-install=false
-hostdelg=false
-hostzone=false
-progress=true
-usage=true
-
-while [ $# -gt 0 ]
-do
- case "$1" in
- -f) usage=false; check=false; install=true ;;
- -y) usage=false; check=true; install=true ;;
- -n) usage=false; check=true; install=false ;;
- -h) hostdelg=true; hostzone=false ;;
- -z) hostdelg=true; hostzone=true ;;
- -q) progress=false ;;
- *) echo >&2 "$0: usage: $0 [-y|-f] [-h|-z] [-q]"; exit 3 ;;
- esac
- shift
-done
+#!/usr/bin/perl -w
-if $usage
-then
- cat <<END >&2
-usage: named-conf-regen -f|-y|-n [-h|-z]
-operation modes:
- -f install without checking } but you must then
- -y check and install } ndc reload
- -n check only
-additional options:
- -h check output from host -C
- -z check output from host -C and host -val
-END
- exit 1
-fi
+use IO::File;
+use Data::Dumper;
-beginfile () {
- if $install
- then
- exec >$conf/$1.new
- else
- exec >/dev/null
- fi
- currentfile=$1
+$etcfile= "/etc/bind/chiark-conf-gen.zones";
+$where= '<built-in>';
+
+$mode= '';
+$verbosity= 1;
+$fromroot= 0;
+
+while (@ARGV && $ARGV[0] =~ m/^\-/) {
+ $_= shift @ARGV;
+ if (s/^\-\-//) {
+ last if m/^$/;
+ if (m/^quiet$/) { $verbosity=0; }
+ elsif (m/^verbose$/) { $verbosity=2; }
+ elsif (m/^root$/) { $fromroot=1; }
+ elsif (m/^(yes|no|force)$/) { m/^./; $mode= $&; }
+ elsif (m/^config$/) { $etcfile= loarg(); $where= '--config option'; }
+ else { usageerr("unknown option --$_"); }
+ } else {
+ s/^\-//;
+ last if m/^$/;
+ while (m/^./) {
+ if (s/^[ynf]//) { $mode=$&; }
+ elsif (s/^r//) { $fromroot=1; }
+ elsif (s/^v//) { $verbosity=2; }
+ elsif (s/^q//) { $verbosity=0; }
+ elsif (s/^C//) { $etcfile= soarg(); $where= '-C option'; }
+ else { usageerr("unknown option -$&"); }
+ }
+ }
}
-endfile () {
- exec >/dev/null
- files="$files $currentfile"
+sub loarg() { usageerr("missing option value") if !@ARGV; return shift @ARGV; }
+sub soarg() { my ($rv); $rv=$_; $_=''; return length $rv ? $rv : loarg(); }
+
+usageerr("must specify either -f|-y|-n or zones (and not both)")
+ if !!$mode == !!@ARGV;
+
+sub usageerr ($) {
+ die
+"$_[0]
+usage: named-conf-regen [-rvq] -f|-y|-n|<zone>...\n".
+"operation modes:\n".
+" -f --force install without checking\n".
+" -y --yes check and install\n".
+" -n --no check only\n".
+"additional options:\n".
+" -r --root check all the way back to the root\n".
+" -q --quiet no output for OK zones\n".
+" -v --verbose extra verbose\n";
}
-installfiles () {
- if $install
- then
- cd $conf
- for f in $files
- do
- mv -f $f.new $f
- done
- fi
+cfg_fail("config filename $etcfile should have been absolute path of a file")
+ unless $etcfile =~ m,^/, && $etcfile !~ m,/$,;
+
+$default_dir= $etcfile;
+$default_dir =~ s,/[^/]+$,,;
+
+$slave_dir= 'slave';
+$slave_prefix= '';
+$slave_suffix= '';
+@self_ns= @self_soa= ();
+%zone_cfg= ();
+@zone_cfg_list= ();
+$output= '';
+$default_output= '';
+%output_contents= ();
+
+read_config($etcfile);
+
+print Dumper(@zone_cfg_list), Dumper(%zone_cfg);
+
+sub cfg_fail ($) { die "$0: $where:\n $_[0]\n"; }
+
+sub read_config ($) {
+ my ($if) = @_;
+ my ($fh,$z,@self, $dir,$prefix,$suffix,$lprefix,$lsuffix);
+ local ($_,$1,$2,$3);
+
+ $fh= new IO::File $if,'r' or cfg_fail("open $if:\n $!");
+ for (;;) {
+ if (!defined($_= <$fh>)) {
+ cfg_fail("read config file $if:\n $!") if $fh->error();
+ last;
+ }
+ $where= "$if:$.";
+ s/^\s+//; chomp; s/\s+$//;
+ next if m/^\#/;
+ last if m/^end$/;
+ next unless m/\S/;
+ if (m/^self(\-ns|\-soa|)\s+(\S.*\S)/) {
+ @self= split /\s+/, $2;
+ @self_ns= @self if $1 ne '-soa';
+ @self_soa= @self if $1 ne '-ns';
+ } elsif (m/^primary\-dir\s+(\S+)((?:\s+(\S+))??:\s+(\S+))?$/) {
+ ($dir, $prefix, $suffix) = (qualify($1),$2,$3);
+ $suffix= '_db' if !length $suffix;
+ opendir D, $dir or cfg_fail("open primary-dir $dir:\n $!");
+ $lprefix= length $prefix; $lsuffix= length $suffix;
+ while ($!=0, $_= readdir D) {
+ next if m/^\./ && !$lprefix;
+ next unless length > $lprefix+$lsuffix;
+ next unless substr($_,0,$lprefix) eq $prefix;
+ next unless substr($_,length($_)-$lsuffix) eq $suffix;
+ $z= substr($_,$lprefix,length($_)-($lprefix+$lsuffix));
+ zone_conf($z,'primary',"$dir/$_");
+ }
+ $! and cfg_fail("read primary-dir $dir:\n $!");
+ closedir D or cfg_fail("close primary-dir $dir:\n $!");
+ } elsif (m/^primary\s+(\S+)\s+(\S+)$/) {
+ zone_conf($1,'primary',qualify($2));
+ } elsif (m/^secondary\s+(\S+)\s+([0-9.\t]+)$/) {
+ zone_conf($1,'secondary','',$2);
+ } elsif (m/^stealth\s+(\S+)\s+([0-9. \t]+)$/) {
+ zone_conf($1,'stealth','',split /\s+/, $2);
+ } elsif (m/^slave\-dir\s+(\S+)((?:\s+(\S+))??:\s+(\S+))?$/) {
+ ($slave_dir, $slave_prefix, $slave_suffix) = (qualify($1),$2,$3);
+ } elsif (m/^output\s+bind8\+(\S+)$/) {
+ cfg_fail("default output may not apply to only some zones")
+ if @zone_cfg_list && length $default_output;
+ set_output(qualify($1));
+ } elsif (m/^include\s+(\S+)$/) {
+ read_config($1);
+ } else {
+ cfg_fail("unknown configuration directive".
+ " or incorrect syntax or arguments");
+ }
+ }
+ $fh->close or cfg_fail("close config file $if:\n $!");
}
-warnings=0
+sub qualify ($) {
+ my ($i) = @_;
+ $i= "$default_dir/$i" unless $i =~ m,^/,;
+}
-warning () {
- echo >&2 "$zone $style: $*"
- warnings=$[$warnings+1]
+sub zone_conf ($$@) {
+ my ($zone,$style,$file,@servers) = @_;
+ $file= qualify("$slave_dir/$slave_prefix".$zone.$slave_suffix)
+ unless length $file;
+ if (!length $output) {
+ $default_output= qualify('chiark-conf-gen.bind8')
+ unless length $default_output;
+ set_output($default_output);
+ }
+ cfg_fail("redefined zone $zone") if exists $zone_cfg{$zone};
+ $zone_cfg{$zone}{'file'}= $file;
+ $zone_cfg{$zone}{'style'}= $style;
+ $zone_cfg{$zone}{'servers'}= @servers;
+ $zone_cfg{$zone}{'output'}= $output;
+ push @zone_cfg_list, $zone;
}
-equlines () {
- if [ "x`echo \" $2\" | wc -l`" != "x`echo \" $3\" | wc -l`" ]
- then
- warning "$1 >$2|$3<"
- fi
+sub set_output($) {
+ my ($newout) = @_;
+ $output= $newout;
+ $output_contents{$output}= '';
}
-checkhostout () {
- set +e
- hostout="`host $1 \"$zone\" 2>&1 >/dev/null $2 | egrep -v \
-'^ \!\!\! .* SOA primary .* is not advertised via NS$'`"
- set -e
- if [ "x$hostout" = x ]; then return; fi
- if $hostfirstwarn
- then
- warning "warnings from host:"
- hostfirstwarn=false
- fi
- echo >&2 "$hostout"
+__DATA__
+
+
+
+sub lookup ($$) {
+ my ($type,$domain) = @_;
+ my ($c,@result);
+ defined($c=open ADH, "-|") or die "$0: fork adnshost:\n $!\n";
+ if (!$c) {
+ exec 'adnshost','+Do','+Dt','+Dc','-Cf',"-t$type",
+ '-',"$domain.";
+ die "$0: exec adnshost:\n $!\n";
+ }
+ @result= <ADH>;
+ chomp @result;
+ $!=0; close ADH;
+ die "$0: lookup -t$type $domain failed $? $!\n" if $? or $!;
+ return @result;
}
-progress () {
- if $progress
- then
- echo -n "$zone $style " >&2
- echo -ne '\r' >&2
- fi
+sub lookup1 ($$) {
+ my ($type,$domain) = @_;
+ my (@result)= lookup($type,$domain);
+ @result==1 or die "$0: lookup -t$type $domain gave more than one RR\n";
+ return $result[0];
}
-cd $base/primary
-zones="*_db"
+sub check () {
+ return unless $check;
+ eval {
+ $soa= lookup1('soa',$zone);
+ $soa_origin=$soa; $soa_origin =~ s/ .*//;
+ $soa_origin_addr= lookup1('a',$soa_origin);
-beginfile primary.zones
-for f in $zones
-do
- zone="`echo $f | sed -e 's/_db$//'`"
- cat <<END
-zone "$zone" {
- type master;
- file "primary/$f";
-};
-END
-done
-endfile
+ @zone_ns= lookup('ns-',$zone);
-myname=''
+ @ok_sources= ($soa_origin_addr);
+ $ok_sources_descr= "SOA ORIGIN $soa_origin [$soa_origin_addr]";
-beginfile secondary.zones
-exec <$etcfile
-while read style zone servers
-do
- names=''
- case "$style" in
- myname)
- myname=$zone
- continue
- ;;
- secondary)
- progress
- file="slave/$zone"
- if $check
- then
- rectype=SOA
- set -e; soa="`host -t soa $zone.`"; set +e
- soaname="`echo \" $soa\" | expand | sed -e '
- s/^ [^ ][^ ]* *SOA *//; s/ .*$//; q
- '`"
- equlines SOA "$soaname" ''
- fi
- ;;
- backup|unoff)
- progress
- rectype=NS
- file="slave/$zone"
- ;;
- '#'|'')
- continue
- ;;
- *)
- echo >&2 "$etcfile: $style"
- exit 3
- esac
-
- if $check
- then
- set -e; ns="`host -t ns $zone.`"; set +e
- nsnames="`echo \" $ns\" | expand | tr A-Z a-z | sed -n '
- 1s/^ //
- s/^[^ ][^ ]* *ns *\([0-9a-z][-.0-9a-z]*\)$/\1/p
- '`"
- equlines NS "$nsnames" "$ns"
- nsnames="`echo $nsnames | tr '
-' ' '`"
-
- if [ "x$myname" != x ]
- then
- meadvert=false
- for ns in $nsnames
- do
- if [ "x$myname" = "x$ns" ]
- then
- meadvert=true
- fi
- done
- fi
+ if ($style eq 'unoff' || $style eq 'backup') {
+ for $zone_ns (@zone_ns) {
+ @zone_ns_addrs= lookup('a',$zone_ns);
+ push @ok_sources, @zone_ns_addrs;
+ $ok_sources_descr.= ", NS $zone_ns [@zone_ns_addrs]";
+ }
+ }
- case "$style" in
- secondary) names="$soaname" ;;
- unoff|backup) names="$nsnames" ;;
- esac
+ for $server (@servers) {
+ grep { $server eq $_ } @ok_sources
+ or warn "secondarying from $server which is not ".
+ "$ok_sources_desc\n";
+ }
+
+ if ($style eq 'secondary') {
+ grep { $zone_ns=$_, grep { $myname eq $_ } @mynames } @zone_n
+ or warn "supposedly published secondary but we ".
+ "(@mynames) are not published ($@zone_ns)\n";
+ }
+ }
+ check_after_eval();
+
+
+# $superzone= $zone; $superzone =~ s/^[^\.]+\.//;
+# @super_ns= lookup('ns-',$zone);
+ }
+
+ eval {
+
+ for $super_ns (@super_ns) {
+ @deleg_ns= ();
+ open DIG, "dig @$super_ns. -t ns +norecurse $zone."
+ or die "$0: fork for dig:\n $!\n";
+ while (<DIG>) {
+
+
+ split /\n/, lookup("
+
case "$style" in
secondary|backup)
- if [ $meadvert = false ]
+ if [ $meadvert = 0 ]
then
warning "$myname unlisted NS $nsnames"
fi
;;
unoff)
- if $meadvert = false
+ if $meadvert = 0
then
warning "$myname advertised NS $nsnames"
fi
echo " $server;"
if $check
then
- notfound=true
+ notfound=1
for addr in $addrs
do
if [ "x$addr" = "x$server" ]
then
- notfound=false
+ notfound=0
fi
done
if $notfound
};
};
END
- hostfirstwarn=true
+ hostfirstwarn=1
if $hostdelg
then
checkhostout -C
done
endfile
+
+chdir "$base/primary" or die "$0: chdir $base/primary:\n $!";
+beginfile('primary.zones');
+
+for $f (<*_db>) {
+ $zone= $f; $zone =~ s/_db$//;
+
+
+for f in $zones
+do
+ zone="`echo $f | sed -e 's/_db$//'`"
+ cat <<END
+zone "$zone" {
+ type master;
+ file "primary/$f";
+};
+END
+done
+endfile
+
+
+
+sub beginfile ($) {
+ $currentfile= $_[0];
+ $currentfile_opened= $install ? "$conf/$currentfile.new" : "/dev/null";
+ open CFF, "> $toopen" or die "$0: begin $currentfile_opened:\n $!\n";
+}
+
+endfile () {
+ close CFF or die "$0: close $currentfile_opened:\n $!\n";
+ push @files, $currentfile;
+}
+
+sub installfiles () {
+ return unless $install;
+ chdir $conf or die "$0: chdir $conf:\n $!\n";
+ for $f (@files) {
+ rename "$f.new", $f or die "$0: install new $f:\n $!\n";
+ }
+}
+
+warning () {
+ echo >&2 "$zone $style: $*"
+ warnings=$[$warnings+1]
+}
+
+equlines () {
+ if [ "x`echo \" $2\" | wc -l`" != "x`echo \" $3\" | wc -l`" ]
+ then
+ warning "$1 >$2|$3<"
+ fi
+}
+
+checkhostout () {
+ set +e
+ hostout="`host $1 \"$zone\" 2>&1 >/dev/null $2 | egrep -v \
+'^ \!\!\! .* SOA primary .* is not advertised via NS$'`"
+ set -e
+ if [ "x$hostout" = x ]; then return; fi
+ if $hostfirstwarn
+ then
+ warning "warnings from host:"
+ hostfirstwarn=0
+ fi
+ echo >&2 "$hostout"
+}
+
+progress () {
+ if $progress
+ then
+ echo -n "$zone $style " >&2
+ echo -ne '\r' >&2
+ fi
+}
+
+myname=''
+
if [ $warnings != 0 ]
then
echo >&2 "$warnings warnings "