2 # This is chiark-named-conf, which is Copyright 2002 Ian Jackson.
4 # chiark-named-conf and its manpage are free software; you can
5 # redistribute it and/or modify them under the terms of the GNU
6 # General Public License as published by the Free Software Foundation;
7 # either version 2, or (at your option) any later version.
9 # chiark-named-conf and its manpage are distributed in the hope that
10 # it will be useful, but WITHOUT ANY WARRANTY; without even the
11 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 # PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 use Fcntl qw(:DEFAULT :flock);
24 use vars qw($quis $stdout_fh $stderr_fh
27 $debug $needglue $localonly $repeat $verbosity
28 $admin $mail_state_dir $mail_max_warnfreq
29 $progress_fh $warn_fh $modifiers
30 %group2modcmd %group2used);
32 $quis= $0; $quis =~ s,.*/,,;
36 $etcfile= "/etc/bind/chiark-conf-gen.zones";
42 $admin=''; $mail_state_dir=''; $mail_max_warnfreq= 50;
46 $group2modcmd{'foreign'}= '*$!@?';
47 $group2used{'foreign'}= 1;
49 ($progress_fh= $stdout_fh= new_from_fd IO::Handle(1,'w') and
50 $warn_fh= $stderr_fh = new_from_fd IO::Handle(2,'w'))
51 or die "$quis: setup standard filehandles: $!\n";
53 use vars qw($dig_owner $dig_type $dig_rdata);
55 while (@ARGV && $ARGV[0] =~ m/^\-/) {
59 if (m/^(yes|no|force)$/) { m/^./; $mode= $&; $domail=''; }
60 elsif (m/^nothing$/) { $mode= 'x'; $domail=''; }
61 elsif (m/^mail\-(first|middle|final|final\-test)$/) {
65 elsif (m/^all$/) { $doall=1; }
66 elsif (m/^config$/) { $etcfile= loarg(); $where= '--config option'; }
67 elsif (m/^glueless$/) { $needglue=0; }
68 elsif (m/^localonly$/) { $localonly=1; }
69 elsif (m/^quiet$/) { $verbosity--; }
70 elsif (m/^repeat$/) { $repeat=1; }
71 elsif (m/^verbose$/) { $verbosity++; }
72 else { usageerr("unknown option --$_"); }
77 if (s/^[ynf]//) { $mode=$&; $domail=''; }
78 elsif (s/^A//) { $doall=1; }
79 elsif (s/^C//) { $etcfile= soarg(); $where= '-C option'; }
80 elsif (s/^D//) { $debug++; }
81 elsif (s/^g//) { $needglue=0; }
82 elsif (s/^l//) { $localonly=1; }
83 elsif (s/^m(\w+)(\W+)$//) {
86 usageerr("modifiers $m for group $g: $@") if bad_modifiers($m);
88 elsif (s/^q//) { $verbosity--; }
89 elsif (s/^r//) { $repeat=1; }
90 elsif (s/^v//) { $verbosity++; }
91 else { usageerr("unknown option -$&"); }
96 sub loarg() { usageerr("missing option value") if !@ARGV; return shift @ARGV; }
97 sub soarg() { my ($rv); $rv=$_; $_=''; return length $rv ? $rv : loarg(); }
99 usageerr("-q may be specified at most twice") if $verbosity<0;
100 usageerr("-v may be specified at most once") if $verbosity>3;
101 usageerr("-D may be specified at most twice") if $debug>2;
102 usageerr("must specify either -f|-y|-n or zones (and not both)")
103 if !!$mode == !!@ARGV && !$domail;
109 usage: chiark-named-conf [options] -f|-y|-n|<zone>...
111 -f --force install without checking
112 -y --yes check and install
113 -n --no check only (configured zones)
114 --nothing list zones only
115 --mail-* send mail about broken zones (see manpage)
116 <zone> ... check only (specified zones, even unconfigured ones)
118 -A --all report on zones marked ? (ones we know are broken)
119 -D debug $quis (does not help debug your DNS config)
120 -g --glueless do not warn about any glueless referrals (not recommended)
121 -l --localonly full checks only on zones which we primary
122 -q --quiet no output for OK zones
123 -r --repeat repeat warnings for all sources of imperfect data
124 -v --verbose extra verbose info about each zone
125 -C|--config <DIR/FILE use FILE as default config and DIR as default dir
127 chiark-named-conf is Copyright 2002 Ian Jackson. It is Free software, under
128 the GNU General Public License, and you are welcome to change it and/or
129 distribute copies under certain conditions. There is ABSOLUTELY NO WARRANTY.
133 cfg_fail("config filename $etcfile should not be directory")
134 if $etcfile =~ m,/$,;
136 use vars qw($default_dir);
137 $default_dir= $etcfile =~ m,^.*/, ? $& : './';
139 use vars qw($slave_dir $slave_prefix $slave_suffix);
144 use vars qw(@self_ns @self_soa @self_addr @forbid_addr @conv_glueless);
145 @self_ns= @self_soa= @self_addr= @forbid_addr= ();
146 @conv_glueless= qw(in-addr.arpa ip6.arpa ip6.int);
148 use vars qw(%zone_cfg @zone_cfg_list);
152 use vars qw($output $default_output %output_contents);
155 %output_contents= ();
157 use vars qw($check $install);
158 $check= $mode !~ m/^[fx]/;
159 $install= $mode =~ m/^[yf]/;
161 read_config($etcfile);
162 debug_dump('@zone_cfg_list %zone_cfg');
163 process_zones(!@ARGV ? @zone_cfg_list : @ARGV);
164 debug_dump('%output_contents');
165 output_files() if $install;
167 $stdout_fh->close or die "$quis: write messages to stdout: $!\n";
168 $stderr_fh->close or die "$quis: write messages to stderr: $!\n";
171 #-------------------- configuration reading
173 sub cfg_fail ($) { die "$quis: $where:\n $_[0]\n"; }
175 sub read_config ($) {
177 my ($fh,$z,@self,$before,$group,
178 $mod,$dir,$prefix,$suffix,$subfile,$lprefix,$lsuffix,$zf);
181 $fh= new IO::File $if,'r' or cfg_fail("open $if:\n $!");
184 if (!defined($_= <$fh>)) {
185 cfg_fail("read config file $if:\n $!") if $fh->error();
189 if (s/\\$//) { $before.= $_; next; }
197 if (m/^self(\-ns|\-soa|)\s+(\S.*\S)/) {
198 @self= split /\s+/, $2;
199 @self_ns= @self if $1 ne '-soa';
200 @self_soa= @self if $1 ne '-ns';
201 } elsif (m/^serverless\-glueless\s+(\S.*\S)/) {
202 @conv_glueless= split /\s+/, $1;
203 } elsif (m/^self\-addr\s+([0-9. \t]+)/) {
204 @self_addr= split /\s+/, $1;
205 } elsif (m/^forbid\-addr(?:\s+([0-9. \t]+))?/) {
206 @forbid_addr= defined $1 ? split /\s+/, $1 : ();
210 (?: \s+ ([^/ \t]*) (?: (/.+) )?
213 ($mod, $dir, $prefix, $suffix, $subfile) =
214 ($1,qualify($2),$3,$4,$5);
215 cfg_fail("modifiers $mod for directory $dir: $@")
216 if bad_modifiers($mod);
217 $suffix= '' if !defined $suffix;
218 $subfile= '' if !defined $subfile;
219 $suffix= '_db' if !length $suffix && !length $subfile;
220 if (-d "$dir/$prefix") { $dir.='/'; $dir.=$prefix; $prefix=''; }
221 opendir D, $dir or cfg_fail("open primary-dir $dir:\n $!");
222 $lprefix= length $prefix; $lsuffix= length $suffix;
223 while (defined($_= readdir D)) {
224 next if m/^\./ && !$lprefix;
225 next unless length > $lprefix+$lsuffix;
226 next unless substr($_,0,$lprefix) eq $prefix;
227 next unless substr($_,length($_)-$lsuffix) eq $suffix;
228 $z= substr($_,$lprefix,length($_)-($lprefix+$lsuffix));
229 $zf= $dir.'/'.$prefix.$z.$suffix.$subfile;
231 next if length $subfile && $! == &ENOENT;
232 cfg_fail("cannot stat zonefile $zf:\n $!");
234 -f _ or cfg_fail("zonefile $zf is not a plain file");
235 zone_conf($z,'primary','p',$mod,$zf);
237 closedir D or cfg_fail("close primary-dir $dir:\n $!");
238 } elsif (m/^primary(\W*)\s+(\S+)\s+(\S+)$/) {
239 zone_conf($2,'primary','p',$1,qualify($3));
240 } elsif (m/^published(\W*)\s+(\S+)\s+([0-9.\t]+)$/) {
241 zone_conf($2,'published','s',$1,'',$3);
242 } elsif (m/^stealth(\W*)\s+(\S+)\s+([0-9. \t]+)$/) {
243 zone_conf($2,'stealth','u',$1,'',split /\s+/, $3);
244 } elsif (m/^modifiers\s+(\W+)(?:\s+(\w+))$/) {
245 ($mod,$group) = ($1,$2);
246 cfg_fail("modifiers $mod for group $group: $@")
247 if bad_modifiers($mod);
248 if (exists $group2modcmd{$group}) {
249 $mod= $group2modcmd{$group};
250 $group2used{$group}++;
253 } elsif (m/^slave\-dir\s+(\S+)(?:(?:\s+(\S+))\s+(\S+))?$/) {
254 ($slave_dir, $slave_prefix, $slave_suffix) = (qualify($1),$2,$3);
255 $slave_prefix='' if !defined $slave_prefix;
256 $slave_suffix='' if !defined $slave_suffix;
257 } elsif (m/^output\s+bind8\+(\S+)$/) {
258 cfg_fail("default output may not apply to only some zones")
259 if @zone_cfg_list && length $default_output;
260 set_output(qualify($1));
261 } elsif (m/^include\s+(\S+)$/) {
263 } elsif (m/^admin\s+(\S+)$/) {
265 } elsif (m/^mail\-state\-dir\s+(\S+)$/) {
267 } elsif (m/^mail\-max\-warnfreq\s+(\d{1,3}(?:\.\d{0,5})?)$/) {
268 cfg_fail("mail-max-warnfreq must be <=100") if $1>100;
269 $mail_max_warnfreq= $1;
271 cfg_fail("unknown configuration directive".
272 " or incorrect syntax or arguments:\n".
276 foreach $group (keys %group2modcmd) {
277 next if exists $group2used{$group};
278 cfg_fail("command line specifies modifier group $group".
279 " but missing in configuration file");
281 $fh->close or cfg_fail("close config file $if:\n $!");
286 $i= "$default_dir$i" unless $i =~ m,^/,;
290 sub bad_modifiers ($) {
293 die "bad modifier $&" if m/[^!*\$\@~?]/;
294 die "repeated modifier $1" if m/(.).*\1/;
303 sub zone_conf ($$$$$@) {
304 my ($zone,$style,$sabbr,$mod,$file,@servers) = @_;
306 $file= qualify("$slave_dir/$slave_prefix".$zone.$slave_suffix)
308 if (!length $output) {
309 $default_output= qualify('chiark-conf-gen.bind8')
310 unless length $default_output;
311 set_output($default_output);
313 cfg_fail("redefined zone $zone\n".
314 " earlier definition $zone_cfg{$zone}{'where'}")
315 if exists $zone_cfg{$zone};
316 $zone_cfg{$zone}{'where'}= $where;
317 $zone_cfg{$zone}{'file'}= $file;
318 $zone_cfg{$zone}{'style_p'}= $style.$mod;
319 $zone_cfg{$zone}{'s'}= "$sabbr $mod $modifiers";
320 # p)rimary s)econdary u)npub f)oreign
321 # followed by modifiers, first per-zone, then default
322 $zone_cfg{$zone}{'servers'}= [ @servers ];
324 length $admin && length $mail_state_dir or
325 cfg_fail("mailing but failed to specify admin".
326 " or mail-state-dir before zone");
327 $zone_cfg{$zone}{'admin'}= $admin;
328 $zone_cfg{$zone}{'maildir'}= qualify($mail_state_dir);
329 $zone_cfg{$zone}{'mailmwarn'}= $mail_max_warnfreq;
331 foreach $sfx (qw(self_soa self_ns self_addr forbid_addr conv_glueless)) {
332 { no strict 'refs'; $aref= [ @$sfx ]; }
333 @$aref or cfg_fail("failed to specify $sfx before zone")
335 $zone_cfg{$zone}{$sfx}= $aref;
337 foreach $sfx (qw(self_soa self_ns)) {
338 map { s/\*$/$zone/ } @{ $zone_cfg{$zone}{$sfx} };
340 $zone_cfg{$zone}{'output'}= $output;
341 push @zone_cfg_list, $zone;
347 $output_contents{$output}= '';
350 #-------------------- checking
352 use vars qw($zone $cfg $warnings %zone_warnings);
357 return if $verbosity < $minv;
358 print $progress_fh "$m\n" or die "$quis: $zone: write log: $!\n";
361 sub verbose ($) { progress(3, ' ' . $_[0]); }
363 sub process_zones (@) {
367 foreach $zone (@zones) {
368 $cfg= $zone_cfg{$zone} || {
369 'style_p' => 'foreign',
370 's' => "f $group2modcmd{'foreign'}",
374 mail_zone_before() or next
377 progress(1, sprintf "%-20s %s", $zone, $$cfg{'style_p'});
378 if ($check && ($doall || !zone_style('?',0))) {
380 if ($localonly && $cfg->{'s'} =~ m/f/) {
381 zone_warning("foreign zone specified with -l",'');
382 } elsif ($localonly && $cfg->{'s'} !~ m/p/) {
388 zone_warning("checks failed: $@",'') if length $@;
390 $output_contents{$$cfg{'output'}} .= zone_output()
393 mail_zone_after() if $domail;
396 } elsif ($warnings) {
397 printf STDERR ("%s: %d warning(s) in %d zone(s);".
398 " %d zone(s) checked ok.\n",
401 scalar(keys %zone_warnings),
402 scalar(@zones - keys %zone_warnings));
404 progress(1, sprintf "%d zone(s) checked ok", scalar @zones);
408 use vars qw(%delgs); # $delgs{$nameserver_list} = [ $whosaidandwhy ]
409 use vars qw(%auths); # $auths{$nameserver_list} = [ $whosaidandwhy ]
410 use vars qw(%glue); # $glue{$name}{$addr_list} = [ $whosaidandwhy ]
411 use vars qw(%soas); # $soa{"$serial $origin"} = [ $whosaidandwhy ]
412 use vars qw(%addr_is_ok %warned);
413 use vars qw($delg_to_us);
414 use vars qw(@to_check); # ($addr,$whyask,$name_if_auth,$glueless_ok, ...)
415 use vars qw(@to_check_soa); # ($addr,$whyask,$name,$is_ns, ...)
417 sub zone_warning ($$) {
421 return 0 if !$repeat && $warned{$w}++;
426 $w .= " ($o)" if length $o;
427 print $warn_fh "$zone: warning: $w\n" or die $!;
429 $zone_warnings{$zone}++;
433 sub zone_warnmore ($) {
434 print $warn_fh "$zone: $_[0]\n" or die $!;
437 sub zone_check_full () {
444 %delgs= %auths= %glue= %soas= %warned= %addr_is_ok= ();
446 @to_check= @to_check_soa= ();
449 sub zone_investigate() {
450 my ($super_zone, @start_nsnames, $start_ww,
451 $start_ns, @start_ns_addrs, $s, $wa, $name_if_auth,
452 %nsrrset_checked, %soa_checked, $addr, $glueless_ok,
453 $rcode, $name, $is_ns);
455 if (!zone_style('*',0)) {
458 debug_trace("zone $zone superzone $super_zone");
460 and die "no superzone ? ($super_zone)\n";
461 $super_zone =~ s/^[^.]+\.//
463 ($rcode,@start_nsnames)=
464 lookup($super_zone,'ns-','06',"superzone search");
467 $start_ww= "server for $super_zone";
469 ($rcode,@start_nsnames)=
470 lookup($zone,'ns-','0',"initial nameserver search");
471 $start_ww= "nameserver for $zone";
473 for $start_ns (@start_nsnames) {
474 $start_ns= lc $start_ns;
475 ($rcode,@start_ns_addrs)= lookup($start_ns,'a','0',"$start_ww");
476 foreach $addr (@start_ns_addrs) {
477 push @to_check, $addr, "$start_ns, $start_ww", undef, 0;
481 # We do these in order so that we always do NS RRset checks on
482 # nameservers that came from other NS RRsets first; otherwise
483 # we might set nsrrset_checked due to a glueless_ok check,
484 # and then not check for gluefulness later.
485 debug_dump('@to_check @to_check_soa');
486 if (($addr,$wa,$name_if_auth,$glueless_ok,@to_check) = @to_check) {
487 push @to_check_soa, $addr, $wa, $name_if_auth, 1,
488 if defined $name_if_auth;
489 next if $nsrrset_checked{$addr}++;
490 zone_check_nsrrset($addr, $wa, $name_if_auth, $glueless_ok);
491 } elsif (($addr,$wa,$name,$is_ns,@to_check_soa) = @to_check_soa) {
492 next if $soa_checked{$addr}++;
493 zone_check_soa($addr,$wa,$name,$is_ns);
500 sub zone_check_nsrrset ($$$$) {
501 my ($uaddr,$wa, $name_if_auth, $glueless_ok) = @_;
502 my (@s, $s, $a, %s2g, @glue, $glue, $delgs_or_auths, $wwn, $ww);
505 verbose("checking delegation by $ww");
507 if ($dig_type eq 'ns' && $dig_owner eq $zone) {
508 $s2g{lc $dig_rdata} = [ ];
509 } elsif ($dig_type eq 'a' && exists $s2g{$dig_owner}) {
510 $wwn= "in glue from $ww";
511 zone_server_queue($dig_rdata,$dig_owner,$wwn,"NS [$uaddr]",0);
512 push @{ $s2g{$dig_owner} }, $dig_rdata;
517 zone_warning("unable to find NS RRset at [$uaddr]", $wa);
519 } elsif (keys %s2g == 1) {
520 zone_warning("only one nameserver ". (join '', keys %s2g),
525 zone_ns_name($s,$ww);
526 @glue= @{ $s2g{$s} };
528 zone_warning("glueless NS $s", $ww)
529 unless $glueless_ok || zone_style('~',!$needglue) ||
530 grep { has_suffix_of($zone,".$_"); }
531 @{ $cfg->{'conv_glueless'} };
532 ($rcode,@glue)= lookup($s,'a','0',"glueless NS from $ww");
534 $wwn= "glueless NS from $ww";
535 zone_server_queue($a,$s,$wwn,"NS [$uaddr]",0);
538 $glue= join ' ', sort @glue;
539 push @{ $glue{$s}{$glue} }, $ww;
542 $delgs_or_auths= defined($name_if_auth) ? \%auths : \%delgs;
543 push @{ $delgs_or_auths->{$s} }, $ww;
546 sub zone_ns_name ($$) {
549 $delg_to_us=1 if grep { $name eq $_ } @{ $cfg->{'self_ns'} };
550 foreach $cg (@{ $cfg->{'conv_glueless'} }) {
551 zone_warning("nameserver $name in serverless-glueless".
552 " namespace area $cg",
554 if has_suffix_of(".$name",".$cg");
556 zone_warning("published server, as $name, but configured as stealth",
558 if $cfg->{'s'} =~ m/u/ &&
560 @{ $cfg->{'self_ns'} }, @{ $cfg->{'self_soa'} };
563 sub zone_server_queue ($$$$$) {
564 my ($addr,$name,$wwn,$wwq,$is_soa) = @_;
565 zone_server_addr($addr,$name,$wwn,$wwq,$is_soa);
566 push @to_check, $addr, "$name, $wwn", $name, $is_soa;
569 sub zone_server_addr ($$$$$) {
570 my ($addr,$name,$ww,$wwq,$is_soa) = @_;
571 debug_trace("zone_server_addr ".join '|',@_);
572 $addr_is_ok{$addr}= "$name ($wwq)"
573 if $is_soa || $cfg->{'s'} =~ m/u/;
574 zone_warning("forbidden nameserver address [$addr] $name",$ww)
575 if grep { $_ eq $addr } @{ $cfg->{'forbid_addr'} };
577 my ($name_is_self, $addr_is_self);
578 $name_is_self= grep { $_ eq $name }
579 @{ $cfg->{$is_soa ? 'self_soa' : 'self_ns'} };
580 $addr_is_self= grep { $_ eq $addr }
581 @{ $cfg->{'self_addr'} };
582 if ($name_is_self && !$addr_is_self) {
583 zone_warning("our $name supplied with wrong address [$addr]", $ww);
585 if (!$name_is_self && $addr_is_self) {
586 zone_warning("we [$addr] are named in ".
587 ($is_soa ? "SOA" : "NS").
588 " by wrong name $name",
591 if (!$name_is_self && !$addr_is_self &&
592 $is_soa && $cfg->{'s'} =~ m/p/) {
593 zone_warning("SOA ORIGIN $name is not us (".
594 (join ' ', @{ $cfg->{'self_soa'} }).")", $ww);
596 $delg_to_us=1 if $addr_is_self && !$is_soa;
599 sub zone_check_soa ($$$$) {
600 my ($uaddr,$wa,$name,$is_ns) = @_;
601 my ($lame,$serial,$origin,$got,$rcode,@soa_addrs,$soa_addr,$ww,$wwn);
602 verbose("checking service at [$uaddr] $name");
603 $lame= 'dead or lame';
606 if ($dig_type eq 'flags:') {
607 $lame= $dig_rdata =~ m/ aa / ? '' : 'lame';
608 } elsif ($dig_type eq 'soa' && $dig_owner eq $zone && !$lame) {
609 die "several SOAs ? $ww" if defined $origin;
611 $got =~ m/^(\d+) (\S+)$/ or die "$got ?";
612 ($serial,$origin) = ($1,$2);
616 $lame= 'broken' if !$lame && !defined $origin;
617 if ($lame) { zone_warning("$lame server [$uaddr]",$wa); return; }
618 progress(2, sprintf "%-16s %46s has %s%s",
619 $zone, "$name [$uaddr]", $serial, $is_ns ? '' : '*');
620 push @{ $soas{$got} }, $ww;
621 ($rcode,@soa_addrs)= lookup($origin,'a','0',"SOA ORIGIN");
622 $wwn= "SOA ORIGIN from $ww";
623 foreach $soa_addr (@soa_addrs) {
624 zone_server_queue($soa_addr,$origin,$wwn,"SOA [$uaddr]",1);
628 sub zone_consistency() {
629 my ($d, $org_ser, $origin, $a, $h, $self_soa, $wa);
630 zone_consistency_set('delegations',\%delgs);
631 foreach $d (keys %delgs) { delete $auths{$d}; }
632 zone_consistency_set('zone nameserver rrset',\%auths);
633 foreach $h (keys %glue) {
634 zone_consistency_set("glue for $h", $glue{$h});
636 zone_consistency_set("serial number and/or SOA ORIGIN",\%soas);
637 $self_soa= $cfg->{'self_soa'};
640 sub zone_servers_ok () {
644 foreach $a (@{ $cfg->{'servers'} }) {
645 next if exists $addr_is_ok{$a};
646 zone_warning("we slave from [$a]",'')
650 foreach $a (keys %addr_is_ok) {
651 zone_warnmore("permitted master [$a] $addr_is_ok{$a}");
655 if ($cfg->{'s'} =~ m/s/ && !$delg_to_us) {
656 zone_warning("we are supposedly published secondary,".
657 " but not listed as a nameserver",'');
658 map { $fs{$_}=1 } keys(%delgs), keys(%auths);
659 zone_warnmore("servers are: ". join ' ', sort keys %fs);
663 sub zone_consistency_set ($%) {
666 if (keys(%$set) > 1) {
667 zone_warning("inconsistent $msg:",'');
668 foreach $d (keys %$set) {
669 foreach $o (@{ $set->{$d} }) { zone_warnmore(" $d from $o"); }
674 sub zone_check_local () {
675 zone_servers_simplefind();
679 sub zone_servers_simplefind () {
680 my ($rcode,@nsnames,$ns,@soas,$origin);
682 ($rcode,@nsnames)= lookup($zone,'ns-','0',"zone's servers");
683 foreach $ns (@nsnames) {
684 zone_ns_name($ns,"NS");
685 zone_server_simple($ns,'NS',0);
687 $delgs{join ' ', sort @nsnames} = [ "zone's servers" ];
689 ($rcode,@soas)= lookup($zone,'soa','0',"SOA ORIGIN");
690 die "multiple SOA RRs in set! @soas ?" if @soas!=1;
691 $soas[0] =~ m/^(\S+)\s/ or die "SOA ? $_";
692 zone_server_simple(domain_canon($1,"lookup $zone SOA"),'SOA',1);
695 sub zone_server_simple ($$$) {
696 my ($name,$ww,$is_soa) = @_;
697 my ($rcode,@addrs,$addr);
698 ($rcode,@addrs)= lookup($name,'a','0', "server - ".
699 ($is_soa ? "SOA ORIGIN" : "NS"));
700 foreach $addr (@addrs) { zone_server_addr($addr,$name,$ww,$ww,$is_soa); }
703 sub zone_style ($$) {
704 my ($stylechar, $default) = @_;
705 local ($_) = $$cfg{'s'};
706 $stylechar =~ s/\W/\\$&/ or die;
707 return m/(\S*)$stylechar/ ? $1 !~ m/\!/ : $default;
710 #-------------------- mailing
712 use vars qw($m_base $m_lastok @m_ok @m_fail
713 $m_info $m_time $m_lock $m_m);
715 sub mail_zone_before () {
718 $m_base= $$cfg{'maildir'}.'/'.$zone;
724 $m_lock= new IO::File "${m_base}_lock", O_RDWR|O_CREAT, 0600
725 or die "$quis: create lockfile ${m_base}_lock: $!\n";
726 if (!flock($m_lock, LOCK_EX|LOCK_NB)) {
727 <$m_lock> =~ m/^\d+ /;
728 die "$quis: $zone: concurrrency? - flock $&$!\n";
732 (@s1= $m_lock->stat) or die "$quis: fstat ${m_base}_lock: $!\n";
733 (@s2= stat "${m_base}_lock") or
734 die "$quis: stat ${m_base}_lock: $!\n";
735 last if ($s1[0] eq $s2[0] && $s1[1] eq $s2[1]);
738 (print $m_lock "$$ \n" and $m_lock->flush)
739 or die "$quis: write pid to ${m_base}_lock: $!\n";
741 if ($m_info= new IO::File "${m_base}_info", 'r') {
743 $_ =~ m/\n/ or die "$quis: read ${m_base}_info: $!\n";
744 m/^\d+ (\d+|-) ([0-9:]*) ([0-9:]*) / or
745 die "$quis: ${m_base}_info malformed\n";
747 if ($domail ne 'first') {
748 if ($2 eq ':' && $3 eq ':') {
749 warn "$quis: $zone: mid/last run, but last".
750 "run already done, ignoring zone\n";
753 @m_ok= split /\:/, $2;
754 @m_fail= split /\:/, $3;
756 } elsif ($! != &ENOENT) {
757 die "$quis: open ${m_base}_info: $!\n";
758 } elsif ($domail ne 'first') {
759 warn "$quis: $zone: first run, but not --mail-first,".
763 if ($domail eq 'first') {
764 remove "${m_base}_history" or $!==&ENOENT
765 or die "$quis: remove ${m_base}_history: $!\n";
767 $progress_fh= $warn_fh= new IO::File "${m_base}_history",'a',0666
768 or die "$quis: open ${m_base}_history: $!\n";
769 $m_info= new IO::File "${m_base}_info.tmp",'w',0666
770 or die "$quis: open ${m_base}_info.tmp: $!\n";
773 progress(-1, "\n".('-'x70)."\n".ptime($m_time)."\n");
777 sub mail_zone_after () {
778 if ($zone_warnings{$zone}) {
779 push @m_fail, $m_time;
780 progress(-1,"failed - $zone_warnings{$zone} warnings");
784 progress(-1,"everything is fine");
786 close $progress_fh or die "$quis: close ${m_base}_history: $!\n";
787 $progress_fh= $warn_fh= $stderr_fh;
789 if ($domail =~ m/^final/) {
790 if (100*@m_fail <= $$cfg{'mailmwarn'}*(@m_fail + @m_ok)) {
791 printf " %-40s ok\n", $zone or die "$quis: mail ok report: $!\n";
792 } elsif (zone_style('@',0)) {
793 printf " %-40s mail suppressed\n", $zone
794 or die "$quis: mail suppress report: $!\n";
799 printf " %-40s %d warns. OK %s Fail %s\n",
801 defined $zone_warnings{$zone} ? $zone_warnings{$zone} : 0,
802 join(',', map { $_ - $m_time } @m_ok),
803 join(',', map { $_ - $m_time } @m_fail)
804 or die "$quis: checking progress report: $!\n";
807 @m_fail= @m_ok= ('','')
808 if $domail =~ m/^final/;
810 printf $m_info "%s %s %s %s \n",
811 $m_time, $m_lastok, join(':',@m_ok), join(':',@m_fail)
812 or die "$quis: write new ${m_base}_info: $!\n";
813 $m_info->close or die "$quis: close new ${m_base}_info: $!\n";
814 rename "${m_base}_info.tmp", "${m_base}_info"
815 or die "$quis: install new ${m_base}_info: $!\n";
822 or die "$quis: write ${m_base}_mail: $!\n";
826 return gmtime($time)." GMT ($time)";
829 sub mail_zone_mail () {
830 my ($log, $zone_to, $zterr, $c, $r, $t, @soas);
831 $m_m= new IO::File "${m_base}_mail", 'w', 0666
832 or die "$quis: create ${m_base}_mail: $!\n";
833 $zone_to=''; $zterr='';
834 if (!zone_style("\$", $$cfg{'s'} !~ m/[ps]/)) {
836 ($r,@soas)= lookup($zone,'soa','0','problem-addr');
837 @soas==1 or die "multiple soas\n";
838 $soas[0] =~ m/^\S+ (\S.*\@\S+) [0-9 ]+$/ or
839 die "bad soa \`$_'\n";
846 From: zone checker <$$cfg{'admin'}>
847 Subject: $zone - configuration problems report
851 pmail("(testing!) ") if $domail ne 'final';
852 pmail("SOA MNAME for $zone <$zone_to>\nCC: ") if length($zone_to);
853 pmail($$cfg{'admin'}."\n\n");
855 You are receiving this mail because your email address is listed
856 in the SOA (Start Of Authority) record for $zone
857 in the DNS, which means you are supposedly the DNS administrator
858 responsible for this zone. This report is generated automatically
859 on behalf of $$cfg{'admin'}, who
860 is the administrator for a server for the zone. Please contact
861 them if you have any problem with this report.
866 Sent to $$cfg{'admin'} since SOA MNAME unavailable:
872 The zone has had configuration errors or persistent operational
873 problems during recent checks. See the logs below for details.
875 Recent check history for $zone:
878 if ($m_lastok ne '-') {
879 pmail(" Last seen to be fine: ".ptime($m_lastok)."\n");
881 pmail(" No record of this zone ever being fine.\n");
884 pmail(" Zone had problems at: ".ptime($t)."\n");
887 pmail(" Everything in order at: ".ptime($t)."\n");
890 $log= new IO::File "${m_base}_history",'r'
891 or die "$quis: reopen ${m_base}_history: $!";
893 pmail($log->getline);
895 (!$log->error and $log->close)
896 or die "$quis: reread or close ${m_base}_log: $!\n";
898 $m_m->close or die "$quis: close ${m_base}_mail: $!\n";
899 $m_m= new IO::File "${m_base}_mail"
900 or die "$quis: reopen ${m_base}_mail: $!";
902 defined($c= fork) or die "$quis: fork for mail: $!\n";
904 defined dup2($m_m->fileno, 0)
905 or die "$quis - sendmail: dup for stdin: $!\n";
906 exec (qw(/usr/sbin/sendmail -odb -oee -oi),
907 ($domail eq 'final' ? '-t' : $$cfg{'admin'}));
908 die "$quis - sendmail: exec: $!\n";
911 $!=0; $r= waitpid $c,0;
912 $r == $c or die "$quis: waitpid sendmail ($c): $r $!";
913 $? and warn "$quis: sendmail failed: $?\n";
915 printf " %-40s %s\n", $zone,
916 length $zone_to ? $zone_to : 'reporting to admin'
917 or die "$quis: write mailing report: $!\n";
920 #-------------------- outputting
925 $o= "zone \"$zone\" {\n";
926 if ($$cfg{'s'} =~ m/p/) {
927 $o.= " type master;\n";
929 $o.= " type slave;\n".
931 foreach $m (@{ $$cfg{'servers'} }) { $o.= " $m;\n"; }
934 $o.= " file \"$$cfg{'file'}\";\n";
939 sub output_files () {
940 my ($fn,$ofn,$mfn,$l,$dir, $maxmode,$h,@to_install);
942 foreach $ofn (keys %output_contents) {
943 $fn= $ofn; $mfn= "output file $fn";
946 $! == &ENOENT or die "$quis: stat $mfn:\n $!\n";
950 $maxmode= (stat _)[2];
953 defined($l= readlink $fn)
954 or die "$quis: readlink $mfn:\n $!\n";
955 $dir= $fn =~ m,^.*/, ? $& : './';
956 $fn= "$dir$l" unless $l =~ m,^/,;
957 $mfn= "output file $fn (symlink target of $ofn)";
959 die "$quis: output file $mfn exists but is not a file".
960 " (or symlink to one)";
963 unlink "$fn.new" or $! == &ENOENT or
964 die "$quis: cannot clear out old .new version of $mfn:\n $!";
965 $h= new IO::File "$fn.new",'w',$maxmode
966 or die("$quis: create .new version of $mfn:\n $!");
968 "# generated by $quis, do not edit\n",
969 $output_contents{$ofn}
970 or die "$quis: write data to .new version of $mfn:\n $!";
972 or die "$quis: close .new version of $mfn:\n $!";
973 push @to_install, $fn,$mfn;
976 while (($fn,$mfn, @to_install) = @to_install) {
978 or die "$quis: install new version of $mfn:\n $!";
982 #-------------------- general utilities
986 return unless $debug>1;
987 local $Data::Dumper::Terse=1;
988 foreach $vn (split /\s+/, $_[0]) {
989 print "$vn := ", eval "Dumper(\\$vn)";
993 sub debug_trace ($) {
994 return unless $debug;
998 sub has_suffix_of ($$) {
999 my ($whole,$suffix) = @_;
1000 debug_trace("has_suffix_of $whole $suffix");
1001 return 0 if length $whole < length $suffix;
1002 return 0 if substr($whole, length($whole) - length($suffix)) ne $suffix;
1003 debug_trace("has_suffix_of $whole $suffix YES");
1008 my ($domain,$type,$okrcodes,$w) = @_;
1010 debug_trace("lookup ==> (->$okrcodes) $domain $type");
1013 defined($c= open $h, "-|") or die "$quis: fork adnshost:\n $!\n";
1015 exec 'adnshost','-Fi','+Do','+Dt','+Dc','-Cf',"-t$type",
1017 die "$quis: exec adnshost:\n $!\n";
1019 @result= $h->getlines();
1020 $h->error and die "$quis: read from adnshost:\n $!\n";
1023 die "$quis: lookup -t$type $domain $okrcodes ($w) failed $? $! @result\n"
1024 if $! or $?&255 or $?>1536 or index($okrcodes,$?>>8)<0;
1025 debug_trace("lookup <== $? @result");
1026 return ($?,@result);
1030 my ($eachrr, $qowner,$qtype,$qaddr) = @_;
1031 # also pseudo-rr with type `flags:'
1032 my ($h,$inmid,$irdata,$c,$digwhy);
1035 debug_trace("dig ==> \@$qaddr $qowner $qtype");
1038 defined($c= open $h, "-|") or die "$quis: fork dig:\n $!\n";
1040 open STDERR, ">&STDOUT" or die $!;
1042 '+nodef','+nosea','+nodebug','+norecurse',
1043 "\@$qaddr",'-t',$qtype,$qowner);
1044 die "$quis: exec dig:\n $!\n";
1048 if (!defined($_= $h->getline())) {
1049 $h->error() and die "$quis: read from dig:\n $!\n";
1053 if (length $inmid) {
1054 s/^\s+/ / or die "$inmid // $_ ?";
1058 s/$/ \(/ unless s/\s*\)\s*$//;
1060 if (s/\s*\(\s*$//) { $inmid= $_; next; }
1061 $digwhy= "dig $qowner $qtype $qaddr \`$_'";
1062 if (m/^\;\; flags\:( [-0-9a-z ]+)\;/) {
1063 $dig_owner=''; $dig_type='flags:'; $dig_rdata= "$1 ";
1064 debug_trace("dig f: $dig_rdata");
1068 } elsif (m/^([-.0-9a-z]+)\s+\d\w+\s+in\s+([a-z]+)\s+(\S.*)/i) {
1069 $dig_owner=domain_canon($1,$digwhy); $dig_type=lc $2; $irdata=$3;
1070 if ($dig_type eq 'a') {
1071 $irdata =~ m/^[.0-9]+$/ or die "$irdata ?";
1073 } elsif ($dig_type eq 'ns') {
1074 $irdata =~ m/^[-.0-9a-z]+$/i or die "bad nameserver $irdata ?";
1075 $dig_rdata= domain_canon($irdata,$digwhy);
1076 } elsif ($dig_type eq 'soa') {
1077 $irdata =~ m/^([-.0-9a-z]+)\s+.*\s+(\d+)(?:\s+\d\w+){4}$/i
1078 or die "bad SOA $irdata ?";
1079 $dig_rdata= $2.' '.domain_canon($1,$digwhy);
1081 debug_trace("ignoring uknown RR type $dig_type");
1084 debug_trace("dig $dig_owner $dig_type $dig_rdata");
1087 debug_trace("ignoring unknown dig output $_");
1091 debug_trace("dig <== gave $?");
1094 sub domain_canon ($$) {
1097 return '.' if $i eq '.';
1098 die "domain $i ($w) ?" unless $i =~ m/^[0-9a-z]/i;