X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-utils.git;a=blobdiff_plain;f=scripts%2Fnamed-conf;h=1520fd8a7da7276339da345e3050e4f36ce2fc2f;hp=a43e5ab8d151d985e2577cc97498fc609c3ad643;hb=5812c849817258ecde6763c216e952d40d261c1d;hpb=e168daee94305e0678e0b9b03ccced26fb39e0e9 diff --git a/scripts/named-conf b/scripts/named-conf index a43e5ab..1520fd8 100755 --- a/scripts/named-conf +++ b/scripts/named-conf @@ -4,11 +4,15 @@ use strict; use IO::File; use Data::Dumper; use POSIX; +use Fcntl qw(:DEFAULT :flock); use vars qw($quis - $mode $doall + $mode $doall $domail $etcfile $where - $debug $needglue $localonly $repeat $verbosity); + $debug $needglue $localonly $repeat $verbosity + $admin $mail_state_dir $mail_max_warnfreq + $progress_fh $warn_fh $modifiers + %group2modcmd %group2used); $quis= $0; $quis =~ s,.*/,,; @@ -20,7 +24,12 @@ $debug= 0; $needglue= 1; $localonly= 0; $verbosity= 2; +$admin=''; $mail_state_dir=''; $mail_max_warnfreq= 50; $repeat= 0; +$domail= ''; +$progress_fh= 'STDOUT'; +$warn_fh= 'STDERR'; +$modifiers= ''; use vars qw($dig_owner $dig_type $dig_rdata); @@ -28,7 +37,9 @@ while (@ARGV && $ARGV[0] =~ m/^\-/) { $_= shift @ARGV; if (s/^\-\-//) { last if m/^$/; - if (m/^(yes|no|force)$/) { m/^./; $mode= $&; } + if (m/^(yes|no|force)$/) { m/^./; $mode= $&; $domail=''; } + elsif (m/^nothing$/) { $mode= 'x'; $domail=''; } + elsif (m/^mail\-(first|middle|final)/) { $mode='n'; $domail=$1; } elsif (m/^all$/) { $doall=1; } elsif (m/^config$/) { $etcfile= loarg(); $where= '--config option'; } elsif (m/^glueless$/) { $needglue=0; } @@ -41,12 +52,17 @@ while (@ARGV && $ARGV[0] =~ m/^\-/) { s/^\-//; last if m/^$/; while (m/^./) { - if (s/^[ynf]//) { $mode=$&; } + if (s/^[ynf]//) { $mode=$&; $domail=''; } elsif (s/^A//) { $doall=1; } elsif (s/^C//) { $etcfile= soarg(); $where= '-C option'; } elsif (s/^D//) { $debug++; } elsif (s/^g//) { $needglue=0; } elsif (s/^l//) { $localonly=1; } + elsif (s/^m(\w+)(\W+)$//) { + my ($g,$m) = ($1,$2); + $group2modcmd{$g}=$m; + usageerr("modifiers $m for group $g: $@") if bad_modifiers($m); + } elsif (s/^q//) { $verbosity--; } elsif (s/^r//) { $repeat=1; } elsif (s/^v//) { $verbosity++; } @@ -73,6 +89,8 @@ operation modes: -f --force install without checking -y --yes check and install -n --no check only (configured zones) + --nothing list zones only + --mail-* send mail about broken zones (see manpage) ... check only (specified zones, even unconfigured ones) additional options: -A --all report on zones marked ? (ones we know are broken) @@ -115,7 +133,7 @@ $default_output= ''; %output_contents= (); use vars qw($check $install); -$check= $mode !~ m/^f/; +$check= $mode !~ m/^[fx]/; $install= $mode =~ m/^[yf]/; read_config($etcfile); @@ -132,7 +150,7 @@ sub cfg_fail ($) { die "$quis: $where:\n $_[0]\n"; } sub read_config ($) { my ($if) = @_; - my ($fh,$z,@self,$before, + my ($fh,$z,@self,$before,$group, $mod,$dir,$prefix,$suffix,$subfile,$lprefix,$lsuffix,$zf); local ($_); @@ -163,13 +181,15 @@ sub read_config ($) { } elsif (m/^forbid\-addr(?:\s+([0-9. \t]+))?/) { @forbid_addr= defined $1 ? split /\s+/, $1 : (); } elsif (m,^ - primary\-dir ([*?]?) + primary\-dir (\W*) \s+ (\S+)/([^/ \t]*) (?: \s+ ([^/ \t]*) (?: (/.+) )? )? $,x) { ($mod, $dir, $prefix, $suffix, $subfile) = ($1,qualify($2),$3,$4,$5); + cfg_fail("modifiers $mod for directory $dir: $@") + if bad_modifiers($mod); $suffix= '' if !defined $suffix; $subfile= '' if !defined $subfile; $suffix= '_db' if !length $suffix && !length $subfile; @@ -191,12 +211,21 @@ sub read_config ($) { zone_conf($z,'primary','p',$mod,$zf); } closedir D or cfg_fail("close primary-dir $dir:\n $!"); - } elsif (m/^primary([*?]?)\s+(\S+)\s+(\S+)$/) { + } elsif (m/^primary(\W*)\s+(\S+)\s+(\S+)$/) { zone_conf($2,'primary','p',$1,qualify($3)); - } elsif (m/^published([*?]?)\s+(\S+)\s+([0-9.\t]+)$/) { + } elsif (m/^published(\W*)\s+(\S+)\s+([0-9.\t]+)$/) { zone_conf($2,'published','s',$1,'',$3); - } elsif (m/^stealth([*?]?)\s+(\S+)\s+([0-9. \t]+)$/) { + } elsif (m/^stealth(\W*)\s+(\S+)\s+([0-9. \t]+)$/) { zone_conf($2,'stealth','u',$1,'',split /\s+/, $3); + } elsif (m/^modifiers\s+(\W+)(?:\s+(\w+))$/) { + ($mod,$group) = ($1,$2); + cfg_fail("modifiers $mod for group $group: $@") + if bad_modifiers($mod); + if (exists $group2modcmd{$group}) { + $mod= $group2modcmd{$group}; + $group2used{$group}++; + } + $modifiers= $mod; } elsif (m/^slave\-dir\s+(\S+)(?:(?:\s+(\S+))\s+(\S+))?$/) { ($slave_dir, $slave_prefix, $slave_suffix) = (qualify($1),$2,$3); $slave_prefix='' if !defined $slave_prefix; @@ -207,12 +236,24 @@ sub read_config ($) { set_output(qualify($1)); } elsif (m/^include\s+(\S+)$/) { read_config($1); + } elsif (m/^admin\s+(\S+)$/) { + $admin=$1; + } elsif (m/^mail\-state\-dir\s+(\S+)$/) { + $mail_state_dir= $1; + } elsif (m/^mail\-max\-warnfreq\s+(\d{1,3}(?:\.\d{0,5})?)$/) { + cfg_fail("mail-max-warnfreq must be <=100") if $1>100; + $mail_max_warnfreq= $1; } else { cfg_fail("unknown configuration directive". " or incorrect syntax or arguments:\n". " \`$_'"); } } + foreach $group (keys %group2modcmd) { + next if exists $group2used{$group}; + cfg_fail("command line specifies modifier group $group". + " but missing in configuration file"); + } $fh->close or cfg_fail("close config file $if:\n $!"); } @@ -222,6 +263,19 @@ sub qualify ($) { return $i; } +sub bad_modifiers ($) { + local ($_) = @_; + if (!eval { + die "bad modifier $&" if m/[^!*\$\@~?]/; + die "repeated modifier $1" if m/(.).*$1/; + 1; + }) { + $@ =~ s/\n//; + return 1; + } + return 0; +} + sub zone_conf ($$$$$@) { my ($zone,$style,$sabbr,$mod,$file,@servers) = @_; my ($sfx,$aref); @@ -238,14 +292,27 @@ sub zone_conf ($$$$$@) { $zone_cfg{$zone}{'where'}= $where; $zone_cfg{$zone}{'file'}= $file; $zone_cfg{$zone}{'style_p'}= $style.$mod; - $zone_cfg{$zone}{'s'}= $sabbr.$mod; # p)rimary s)econdary u)npub f)oreign + $zone_cfg{$zone}{'s'}= "$sabbr $mod $modifiers"; + # p)rimary s)econdary u)npub f)oreign + # followed by modifiers, first per-zone, then default $zone_cfg{$zone}{'servers'}= [ @servers ]; + if ($domail) { + length $admin && length $mail_state_dir or + cfg_fail("mailing but failed to specify admin". + " or mail-state-dir before zone"); + $zone_cfg{$zone}{'admin'}= $admin; + $zone_cfg{$zone}{'maildir'}= qualify($mail_state_dir); + $zone_cfg{$zone}{'mailmwarn'}= $mail_max_warnfreq; + } foreach $sfx (qw(self_soa self_ns self_addr forbid_addr conv_glueless)) { { no strict 'refs'; $aref= [ @$sfx ]; } @$aref or cfg_fail("failed to specify $sfx before zone") if $sfx =~ m/^self/; $zone_cfg{$zone}{$sfx}= $aref; } + foreach $sfx (qw(self_soa self_ns)) { + $zone_cfg{$zone}{$sfx} =~ s/\*$/$zone/; + } $zone_cfg{$zone}{'output'}= $output; push @zone_cfg_list, $zone; } @@ -256,7 +323,6 @@ sub set_output($) { $output_contents{$output}= ''; } - #-------------------- checking use vars qw($zone $cfg $warnings %zone_warnings); @@ -265,7 +331,7 @@ $warnings= 0; sub progress ($$) { my ($minv,$m) = @_; return if $verbosity < $minv; - print "$m\n"; + print $progress_fh "$m\n" or die "$quis: $zone: write log: $!\n"; } sub verbose ($) { progress(3, ' ' . $_[0]); } @@ -275,14 +341,16 @@ sub process_zones (@) { local ($zone,$cfg); foreach $zone (@zones) { - zone_reset(); $cfg= $zone_cfg{$zone} || { 'style_p' => 'foreign', 's' => 'f', 'servers' => [ ], }; + + mail_zone_before() if $domail; + zone_reset(); progress(1, sprintf "%-20s %s", $zone, $$cfg{'style_p'}); - if ($check && ($doall || $cfg->{'s'} !~ m/\?/)) { + if ($check && ($doall || !zone_style('?',0))) { eval { if ($localonly && $cfg->{'s'} =~ m/f/) { zone_warning("foreign zone specified with -l",''); @@ -296,8 +364,11 @@ sub process_zones (@) { } $output_contents{$$cfg{'output'}} .= zone_output() if $install; + + mail_zone_after() if $domail; } - if ($warnings) { + if ($domail) { + } elsif ($warnings) { printf STDERR ("%s: %d warning(s) in %d zone(s);". " %d zone(s) checked ok.\n", $quis, @@ -328,14 +399,14 @@ sub zone_warning ($$) { $w =~ s,\n, // ,g; $w .= " ($o)" if length $o; - print STDERR "$zone: warning: $w\n" or die $!; + print $warn_fh "$zone: warning: $w\n" or die $!; $warnings++; $zone_warnings{$zone}++; return 1; } sub zone_warnmore ($) { - print STDERR "$zone: $_[0]\n" or die $!; + print $warn_fh "$zone: $_[0]\n" or die $!; } sub zone_check_full () { @@ -356,12 +427,14 @@ sub zone_investigate() { %nsrrset_checked, %soa_checked, $addr, $glueless_ok, $rcode, $name, $is_ns); - if ($cfg->{'s'} !~ m/\*/) { + if (!zone_style('*',0)) { $super_zone= $zone; for (;;) { debug_trace("zone $zone superzone $super_zone"); + $super_zone eq '.' + and die "no superzone ? ($super_zone)\n"; $super_zone =~ s/^[^.]+\.// - or die "no superzone ? ($super_zone)\n"; + or $super_zone= '.'; ($rcode,@start_nsnames)= lookup($super_zone,'ns-','06',"superzone search"); last if !$rcode; @@ -428,7 +501,7 @@ sub zone_check_nsrrset ($$$$) { @glue= @{ $s2g{$s} }; if (!@glue) { zone_warning("glueless NS $s", $ww) - unless $glueless_ok || !$needglue || + unless $glueless_ok || zone_style('~',!$needglue) || grep { has_suffix_of($zone,".$_"); } @{ $cfg->{'conv_glueless'} }; ($rcode,@glue)= lookup($s,'a','0',"glueless NS from $ww"); @@ -602,6 +675,212 @@ sub zone_server_simple ($$$) { foreach $addr (@addrs) { zone_server_addr($addr,$name,$ww,$ww,$is_soa); } } +sub zone_style ($$) { + my ($stylechar, $default) = @_; + local ($_) = $$cfg{'s'}; + $stylechar =~ s/\W/\\$&/ or die; + return m/(\S*)$stylechar/ ? $1 !~ m/\!/ : $default; +} + +#-------------------- mailing + +use vars qw($m_base $m_lastok @m_ok @m_fail + $m_info $m_time $m_lock $m_m); + +sub mail_zone_before () { + my (@s1,@s2); + + $m_base= $$cfg{'maildir'}.'/'.$zone; + $m_lastok= '-'; + @m_ok= (); + @m_fail= (); + + for (;;) { + $m_lock= new IO::File "${m_base}_lock", O_RDWR|O_CREAT, 0600 + or die "$quis: create lockfile ${m_base}_lock"; + if (!flock($m_lock, LOCK_EX|LOCK_NB)) { + <$m_lock> =~ m/^\d+ /; + die "$quis: $zone: concurrrency? - flock $&$!\n"; + $m_lock->close; + next; + } + (@s1= $m_lock->fstat) or die "$quis: fstat ${m_base}_lock: $!\n"; + (@s2= stat "${m_base}_lock") or + die "$quis: stat ${m_base}_lock: $!\n"; + last if ($s1[0] eq $s2[0] && $s1[1] eq $s2[1]); + $m_lock->close; + } + (print $m_lock "$$ \n" and $m_lock->flush) + or die "$quis: write pid to ${m_base}_lock: $!\n"; + + if ($m_info= new IO::File "${m_base}_info", 'r') { + $!=0; $_= <$m_info>; + $_ =~ m/\n/ or die "$quis: read ${m_base}_info: $!\n"; + m/^\d+ (\d+|-) ([0-9:]*) ([0-9:]*) / or + die "$quis: ${m_base}_info malformed\n"; + $m_lastok= $1; + if ($domail ne 'first') { + if ($2 eq ':' && $3 eq ':') { + warn "$quis: $zone: mid/last run, but last". + "run already done, ignoring zone\n"; + next; + } + @m_ok= split /\:/, $2; + @m_fail= split /\:/, $3; + } + } elsif ($! != &ENOENT) { + die "$quis: open ${m_base}_info: $!\n"; + } elsif ($domail eq 'first') { + warn "$quis: $zone: first run, but not --mail-first,". + " ignoring zone\n"; + next; + } + if ($domail eq 'first') { + remove "${m_base}_history" or $!==&ENOENT + or die "$quis: remove ${m_base}_history: $!\n"; + } + $progress_fh= $warn_fh= new IO::File "${m_base}_history",'a',0666 + or die "$quis: open ${m_base}_history: $!\n"; + $m_info= new IO::File "${m_base}_info.tmp",'w',0666 + or die "$quis: open ${m_base}_info.tmp: $!\n"; + + $m_time= time; + progress(-1, "\n".('-'x70)."\n".ptime($m_time)."\n"); +} + +sub mail_zone_after () { + if ($zone_warnings{$zone}) { + push @m_fail, $m_time; + progress(-1,"failed - $zone_warnings{$zone} warnings"); + } else { + push @m_ok, $m_time; + $m_lastok= $m_time; + progress(-1,"everything is fine"); + } + close $progress_fh or die "$quis: close ${m_base}_history: $!\n"; + $progress_fh= $warn_fh= 'STDERR'; + + if ($domail eq 'final') { + if (100*@m_fail <= $$cfg{'mailmwarn'}*(@m_fail + @m_ok)) { + printf " %-40s ok\n" or die "$quis: mail ok report: $!\n"; + } elsif (zone_style('@',0)) { + printf " %-40s mail suppressed\n" + or die "$quis: mail suppress report: $!\n"; + } else { + mail_zone_mail(); + } + } + + @m_fail= @m_ok= ('','') + if $domail eq 'final'; + + printf $m_info "%s %s %s %s \n", + $m_time, $m_lastok, join(':',@m_ok), join(':',@m_fail) + or die "$quis: write new ${m_base}_info: $!\n"; + $m_info->close or die "$quis: close new ${m_base}_info: $!\n"; + rename "${m_base}_info.tmp", "${m_base}_info" + or die "$quis: install new ${m_base}_info: $!\n"; + $m_lock->close; +} + + +sub pmail ($) { + print $m_m $_[0] + or die "$quis: write ${m_base}_mail: $!\n"; +} +sub ptime ($) { + my ($time) = @_; + return gmtime($time)." GMT ($time)"; +} + +sub mail_zone_mail () { + my ($log, $zone_to, $zterr, $c, $r, $t); + $m_m= new IO::File "${m_base}_mail", 'w', 0666 + or die "$quis: create ${m_base}_mail: $!\n"; + $zone_to=''; $zterr=''; + if (!zone_style("\$",$$cfg{'s'} =~ m/u/)) { + eval { + $_= lookup($zone,'soa','0','problem-addr'); + m/\n\n/ and die "multiple soas\n"; + m/^\S+ (\S.*\@\S+) [0-9 ]+$/ or + die "bad soa \`$_'\n"; + $zone_to= $1; + }; + $zterr= $@; + $zterr =~ s/\n$//; + } + pmail < +Subject: $zone - configuration problems report +END +; + pmail("To: "); + pmail("SOA MNAME for $zone <$zone_to>\nCC: ") + if length($zone_to); + pmail($$cfg{'admin'}."\n\n"); + pmail <error && $log->close + or die "$quis: reread or close ${m_base}_log: $!"; + $m_m->close or die "$quis: close ${m_base}_mail: $!\n"; + $m_m= new IO::File "${m_base}_mail" + or die "$quis: reopen ${m_base}_mail: $!"; + + defined($c= fork) or die "$quis: fork for mail: $!\n"; + if (!$c) { + open STDIN, "<& ${m_m}" or die "$quis - sendmail: dup for stdin: $!\n"; + exec '/usr/sbin/sendmail','-odq','-oee','-oi', + 'ijackson@chiark.greenend.org.uk'; # should be -t + die "$quis - sendmail: exec: $!\n"; + } + $m_m->close; + $!=0; $r= waitpid $c,0; + $r == $c or die "$quis: waitpid sendmail ($c): $r $!"; + $? and warn "$quis: sendmail failed: $?\n"; + + printf " %-40s %s\n", $zone, $zone_to or + die "$quis: write mailing report: $!\n"; +} + #-------------------- outputting sub zone_output () { @@ -698,7 +977,7 @@ sub lookup ($$$$) { defined($c= open $h, "-|") or die "$quis: fork adnshost:\n $!\n"; if (!$c) { exec 'adnshost','-Fi','+Do','+Dt','+Dc','-Cf',"-t$type", - '-',"$domain."; + '-',"$domain"; die "$quis: exec adnshost:\n $!\n"; } @result= $h->getlines();