| 1 |
#!/usr/bin/perl -w |
| 2 |
# This is chiark-named-conf, which is Copyright 2002 Ian Jackson. |
| 3 |
# |
| 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 3, or (at your option) any later version. |
| 8 |
# |
| 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. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License along |
| 15 |
# with this program; if not, consult the Free Software Foundation's |
| 16 |
# website at www.fsf.org, or the GNU Project website at www.gnu.org. |
| 17 |
|
| 18 |
use strict; |
| 19 |
use IO::File; |
| 20 |
use Data::Dumper; |
| 21 |
use POSIX; |
| 22 |
use Fcntl qw(:DEFAULT :flock); |
| 23 |
|
| 24 |
# bastard Perl wants me to do this now ! |
| 25 |
sub loarg(); |
| 26 |
sub soarg(); |
| 27 |
sub usageerr ($); |
| 28 |
sub cfg_fail ($); |
| 29 |
sub read_config ($); |
| 30 |
sub qualify ($); |
| 31 |
sub bad_modifiers ($); |
| 32 |
sub zone_conf ($$$$$@); |
| 33 |
sub set_output($); |
| 34 |
sub progress ($$); |
| 35 |
sub verbose ($); |
| 36 |
sub process_zones (@); |
| 37 |
sub zone_warning ($$); |
| 38 |
sub zone_warnmore ($); |
| 39 |
sub zone_check_full (); |
| 40 |
sub zone_reset(); |
| 41 |
sub zone_investigate(); |
| 42 |
sub zone_check_nsrrset ($$$$); |
| 43 |
sub zone_ns_name ($$); |
| 44 |
sub zone_server_queue ($$$$$); |
| 45 |
sub zone_server_addr ($$$$$); |
| 46 |
sub zone_check_soa ($$$$); |
| 47 |
sub zone_consistency(); |
| 48 |
sub zone_servers_ok (); |
| 49 |
sub zone_consistency_set ($%); |
| 50 |
sub zone_check_local (); |
| 51 |
sub zone_servers_simplefind (); |
| 52 |
sub zone_server_simple ($$$); |
| 53 |
sub zone_style ($$); |
| 54 |
sub mail_zone_before (); |
| 55 |
sub mail_zone_after (); |
| 56 |
sub pmail ($); |
| 57 |
sub ptime ($); |
| 58 |
sub mail_zone_mail (); |
| 59 |
sub zone_output (); |
| 60 |
sub output_files (); |
| 61 |
sub debug_dump ($); |
| 62 |
sub debug_trace ($); |
| 63 |
sub has_suffix_of ($$); |
| 64 |
sub lookup ($$$$); |
| 65 |
sub dig (&$$$); |
| 66 |
sub domain_canon ($$); |
| 67 |
|
| 68 |
use vars qw($quis $stdout_fh $stderr_fh |
| 69 |
$mode $doall $domail |
| 70 |
$etcfile $where |
| 71 |
$debug $needglue $localonly $repeat $verbosity |
| 72 |
$admin $mail_state_dir $mail_max_warnfreq |
| 73 |
$progress_fh $warn_fh $modifiers |
| 74 |
%group2modcmd %group2used); |
| 75 |
|
| 76 |
$|=1; |
| 77 |
$quis= $0; $quis =~ s,.*/,,; |
| 78 |
|
| 79 |
$mode= ''; |
| 80 |
$doall= 0; |
| 81 |
$etcfile= "/etc/bind/chiark-conf-gen.zones"; |
| 82 |
$where= '<built-in>'; |
| 83 |
$debug= 0; |
| 84 |
$needglue= 1; |
| 85 |
$localonly= 0; |
| 86 |
$verbosity= 2; |
| 87 |
$admin=''; $mail_state_dir=''; $mail_max_warnfreq= 50; |
| 88 |
$repeat= 0; |
| 89 |
$domail= ''; |
| 90 |
$modifiers= ''; |
| 91 |
$group2modcmd{'foreign'}= '$!*@?'; |
| 92 |
$group2used{'foreign'}= 1; |
| 93 |
|
| 94 |
($progress_fh= $stdout_fh= new_from_fd IO::Handle(1,'w') and |
| 95 |
$warn_fh= $stderr_fh = new_from_fd IO::Handle(2,'w')) |
| 96 |
or die "$quis: setup standard filehandles: $!\n"; |
| 97 |
|
| 98 |
use vars qw($dig_owner $dig_type $dig_rdata); |
| 99 |
|
| 100 |
while (@ARGV && $ARGV[0] =~ m/^\-/) { |
| 101 |
$_= shift @ARGV; |
| 102 |
if (s/^\-\-//) { |
| 103 |
last if m/^$/; |
| 104 |
if (m/^(yes|no|force)$/) { m/^./; $mode= $&; $domail=''; } |
| 105 |
elsif (m/^nothing$/) { $mode= 'x'; $domail=''; } |
| 106 |
elsif (m/^mail\-(first|middle|final|final\-test)$/) { |
| 107 |
$mode='n'; |
| 108 |
$domail=$1; |
| 109 |
} |
| 110 |
elsif (m/^all$/) { $doall=1; } |
| 111 |
elsif (m/^config$/) { $etcfile= loarg(); $where= '--config option'; } |
| 112 |
elsif (m/^glueless$/) { $needglue=0; } |
| 113 |
elsif (m/^localonly$/) { $localonly=1; } |
| 114 |
elsif (m/^quiet$/) { $verbosity--; } |
| 115 |
elsif (m/^repeat$/) { $repeat=1; } |
| 116 |
elsif (m/^verbose$/) { $verbosity++; } |
| 117 |
else { usageerr("unknown option --$_"); } |
| 118 |
} else { |
| 119 |
s/^\-//; |
| 120 |
last if m/^$/; |
| 121 |
while (m/^./) { |
| 122 |
if (s/^[ynf]//) { $mode=$&; $domail=''; } |
| 123 |
elsif (s/^A//) { $doall=1; } |
| 124 |
elsif (s/^C//) { $etcfile= soarg(); $where= '-C option'; } |
| 125 |
elsif (s/^D//) { $debug++; } |
| 126 |
elsif (s/^g//) { $needglue=0; } |
| 127 |
elsif (s/^l//) { $localonly=1; } |
| 128 |
elsif (s/^m(\w+)(\W+)$//) { |
| 129 |
my ($g,$m) = ($1,$2); |
| 130 |
$group2modcmd{$g}=$m; |
| 131 |
usageerr("modifiers $m for group $g: $@") if bad_modifiers($m); |
| 132 |
} |
| 133 |
elsif (s/^q//) { $verbosity--; } |
| 134 |
elsif (s/^r//) { $repeat=1; } |
| 135 |
elsif (s/^v//) { $verbosity++; } |
| 136 |
else { usageerr("unknown option -$&"); } |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
sub loarg() { usageerr("missing option value") if !@ARGV; return shift @ARGV; } |
| 142 |
sub soarg() { my ($rv); $rv=$_; $_=''; return length $rv ? $rv : loarg(); } |
| 143 |
|
| 144 |
usageerr("-q may be specified at most twice") if $verbosity<0; |
| 145 |
usageerr("-v may be specified at most once") if $verbosity>3; |
| 146 |
usageerr("-D may be specified at most twice") if $debug>2; |
| 147 |
usageerr("must specify either -f|-y|-n or zones (and not both)") |
| 148 |
if !!$mode == !!@ARGV && !$domail; |
| 149 |
|
| 150 |
sub usageerr ($) { |
| 151 |
die <<END; |
| 152 |
$_[0] |
| 153 |
|
| 154 |
usage: chiark-named-conf [options] -f|-y|-n|<zone>... |
| 155 |
operation modes: |
| 156 |
-f --force install without checking |
| 157 |
-y --yes check and install |
| 158 |
-n --no check only (configured zones) |
| 159 |
--nothing list zones only |
| 160 |
--mail-* send mail about broken zones (see manpage) |
| 161 |
<zone> ... check only (specified zones, even unconfigured ones) |
| 162 |
additional options: |
| 163 |
-A --all report on zones marked ? (ones we know are broken) |
| 164 |
-D debug $quis (does not help debug your DNS config) |
| 165 |
-g --glueless do not warn about any glueless referrals (not recommended) |
| 166 |
-l --localonly full checks only on zones which we primary |
| 167 |
-q --quiet no output for OK zones |
| 168 |
-r --repeat repeat warnings for all sources of imperfect data |
| 169 |
-v --verbose extra verbose info about each zone |
| 170 |
-C|--config <DIR/FILE use FILE as default config and DIR as default dir |
| 171 |
|
| 172 |
chiark-named-conf is Copyright 2002 Ian Jackson. It is Free software, under |
| 173 |
the GNU General Public License, and you are welcome to change it and/or |
| 174 |
distribute copies under certain conditions. There is ABSOLUTELY NO WARRANTY. |
| 175 |
END |
| 176 |
} |
| 177 |
|
| 178 |
cfg_fail("config filename $etcfile should not be directory") |
| 179 |
if $etcfile =~ m,/$,; |
| 180 |
|
| 181 |
use vars qw($default_dir); |
| 182 |
$default_dir= $etcfile =~ m,^.*/, ? $& : './'; |
| 183 |
|
| 184 |
use vars qw($slave_dir $slave_prefix $slave_suffix); |
| 185 |
$slave_dir= 'slave'; |
| 186 |
$slave_prefix= ''; |
| 187 |
$slave_suffix= ''; |
| 188 |
|
| 189 |
use vars qw(@self_ns @self_soa @self_addr @forbid_addr @forbid_slave |
| 190 |
@conv_glueless @indirect_glue); |
| 191 |
@self_ns= @self_soa= @self_addr= @forbid_addr= @forbid_slave= @indirect_glue= (); |
| 192 |
@conv_glueless= qw(in-addr.arpa ip6.arpa ip6.int); |
| 193 |
|
| 194 |
use vars qw(%zone_cfg @zone_cfg_list); |
| 195 |
%zone_cfg= (); |
| 196 |
@zone_cfg_list= (); |
| 197 |
|
| 198 |
use vars qw($output $default_output %output_contents); |
| 199 |
$output= ''; |
| 200 |
$default_output= ''; |
| 201 |
%output_contents= (); |
| 202 |
|
| 203 |
use vars qw($check $install); |
| 204 |
$check= $mode !~ m/^[fx]/; |
| 205 |
$install= $mode =~ m/^[yf]/; |
| 206 |
|
| 207 |
read_config($etcfile); |
| 208 |
debug_dump('@zone_cfg_list %zone_cfg'); |
| 209 |
process_zones(!@ARGV ? @zone_cfg_list : @ARGV); |
| 210 |
debug_dump('%output_contents'); |
| 211 |
output_files() if $install; |
| 212 |
|
| 213 |
$stdout_fh->close or die "$quis: write messages to stdout: $!\n"; |
| 214 |
$stderr_fh->close or die "$quis: write messages to stderr: $!\n"; |
| 215 |
exit 0; |
| 216 |
|
| 217 |
#-------------------- configuration reading |
| 218 |
|
| 219 |
sub cfg_fail ($) { die "$quis: $where:\n $_[0]\n"; } |
| 220 |
|
| 221 |
sub read_config ($) { |
| 222 |
my ($if) = @_; |
| 223 |
my ($fh,$z,@self,$before,$group, |
| 224 |
$mod,$dir,$prefix,$suffix,$subfile,$lprefix,$lsuffix,$zf); |
| 225 |
local ($_); |
| 226 |
|
| 227 |
$fh= new IO::File $if,'r'; |
| 228 |
unless ($fh) { |
| 229 |
return if $! == &ENOENT; |
| 230 |
cfg_fail("open $if:\n $!"); |
| 231 |
} |
| 232 |
$before= ''; |
| 233 |
for (;;) { |
| 234 |
if (!defined($_= <$fh>)) { |
| 235 |
cfg_fail("read config file $if:\n $!") if $fh->error(); |
| 236 |
last; |
| 237 |
} |
| 238 |
chomp; s/\s+$//; |
| 239 |
if (s/\\$//) { $before.= $_; next; } |
| 240 |
$_= $before.$_; |
| 241 |
$before= ''; |
| 242 |
s/^\s+//; |
| 243 |
$where= "$if:$."; |
| 244 |
next if m/^\#/; |
| 245 |
last if m/^end$/; |
| 246 |
next unless m/\S/; |
| 247 |
if (m/^self(\-ns|\-soa|)\s+(\S.*\S)/) { |
| 248 |
@self= split /\s+/, $2; |
| 249 |
@self_ns= @self if $1 ne '-soa'; |
| 250 |
@self_soa= @self if $1 ne '-ns'; |
| 251 |
} elsif (m/^serverless\-glueless\s+(\S.*\S)/) { |
| 252 |
@conv_glueless= split /\s+/, $1; |
| 253 |
} elsif (m/^allow\-indirect\-glue\s+(\S.*\S)/) { |
| 254 |
@indirect_glue= split /\s+/, $1; |
| 255 |
} elsif (m/^self\-addr\s+([0-9. \t]+)/) { |
| 256 |
@self_addr= split /\s+/, $1; |
| 257 |
} elsif (m/^forbid\-addr(?:\s+([0-9. \t]+))?/) { |
| 258 |
@forbid_addr= defined $1 ? split /\s+/, $1 : (); |
| 259 |
} elsif (m/^forbid\-slave(?:\s+([0-9. \t]+))?/) { |
| 260 |
@forbid_slave= defined $1 ? split /\s+/, $1 : (); |
| 261 |
} elsif (m,^ |
| 262 |
primary\-dir (\W*) |
| 263 |
\s+ (\S+)/([^/ \t]*) |
| 264 |
(?: \s+ ([^/ \t]*) (?: (/.+) )? |
| 265 |
)? |
| 266 |
$,x) { |
| 267 |
($mod, $dir, $prefix, $suffix, $subfile) = |
| 268 |
($1,qualify($2),$3,$4,$5); |
| 269 |
cfg_fail("modifiers $mod for directory $dir: $@") |
| 270 |
if bad_modifiers($mod); |
| 271 |
$suffix= '' if !defined $suffix; |
| 272 |
$subfile= '' if !defined $subfile; |
| 273 |
$suffix= '_db' if !length $suffix && !length $subfile; |
| 274 |
if (-d "$dir/$prefix") { $dir.='/'; $dir.=$prefix; $prefix=''; } |
| 275 |
opendir D, $dir or cfg_fail("open primary-dir $dir:\n $!"); |
| 276 |
$lprefix= length $prefix; $lsuffix= length $suffix; |
| 277 |
while (defined($_= readdir D)) { |
| 278 |
next if m/^\./ && !$lprefix; |
| 279 |
next unless length > $lprefix+$lsuffix; |
| 280 |
next unless substr($_,0,$lprefix) eq $prefix; |
| 281 |
next unless substr($_,length($_)-$lsuffix) eq $suffix; |
| 282 |
$z= substr($_,$lprefix,length($_)-($lprefix+$lsuffix)); |
| 283 |
$zf= $dir.'/'.$prefix.$z.$suffix.$subfile; |
| 284 |
if (!stat $zf) { |
| 285 |
next if length $subfile && $! == &ENOENT; |
| 286 |
cfg_fail("cannot stat zonefile $zf:\n $!"); |
| 287 |
} |
| 288 |
-f _ or cfg_fail("zonefile $zf is not a plain file"); |
| 289 |
zone_conf($z,'primary','p',$mod,$zf); |
| 290 |
} |
| 291 |
closedir D or cfg_fail("close primary-dir $dir:\n $!"); |
| 292 |
} elsif (m/^primary(\W*)\s+(\S+)\s+(\S+)$/) { |
| 293 |
zone_conf($2,'primary','p',$1,qualify($3)); |
| 294 |
} elsif (m/^published(\W*)\s+(\S+)\s+([0-9.\t]+)$/) { |
| 295 |
zone_conf($2,'published','s',$1,'',$3); |
| 296 |
} elsif (m/^stealth(\W*)\s+(\S+)\s+([0-9. \t]+)$/) { |
| 297 |
zone_conf($2,'stealth','u',$1,'',split /\s+/, $3); |
| 298 |
} elsif (m/^modifiers\s+(\W+)(?:\s+(\w+))$/) { |
| 299 |
($mod,$group) = ($1,$2); |
| 300 |
cfg_fail("modifiers $mod for group $group: $@") |
| 301 |
if bad_modifiers($mod); |
| 302 |
if (exists $group2modcmd{$group}) { |
| 303 |
$mod= $group2modcmd{$group}; |
| 304 |
$group2used{$group}++; |
| 305 |
} |
| 306 |
$modifiers= $mod; |
| 307 |
} elsif (m/^slave\-dir\s+(\S+)(?:(?:\s+(\S+))\s+(\S+))?$/) { |
| 308 |
($slave_dir, $slave_prefix, $slave_suffix) = (qualify($1),$2,$3); |
| 309 |
$slave_prefix='' if !defined $slave_prefix; |
| 310 |
$slave_suffix='' if !defined $slave_suffix; |
| 311 |
} elsif (m/^output\s+bind8\+(\S+)$/) { |
| 312 |
cfg_fail("default output may not apply to only some zones") |
| 313 |
if @zone_cfg_list && length $default_output; |
| 314 |
set_output(qualify($1)); |
| 315 |
} elsif (m/^include\s+(\S+)$/) { |
| 316 |
read_config($1); |
| 317 |
} elsif (m/^admin\s+(\S+)$/) { |
| 318 |
$admin=$1; |
| 319 |
} elsif (m/^mail\-state\-dir\s+(\S+)$/) { |
| 320 |
$mail_state_dir= $1; |
| 321 |
} elsif (m/^mail\-max\-warnfreq\s+(\d{1,3}(?:\.\d{0,5})?)$/) { |
| 322 |
cfg_fail("mail-max-warnfreq must be <=100") if $1>100; |
| 323 |
$mail_max_warnfreq= $1; |
| 324 |
} else { |
| 325 |
cfg_fail("unknown configuration directive". |
| 326 |
" or incorrect syntax or arguments:\n". |
| 327 |
" \`$_'"); |
| 328 |
} |
| 329 |
} |
| 330 |
foreach $group (keys %group2modcmd) { |
| 331 |
next if exists $group2used{$group}; |
| 332 |
cfg_fail("command line specifies modifier group $group". |
| 333 |
" but missing in configuration file"); |
| 334 |
} |
| 335 |
$fh->close or cfg_fail("close config file $if:\n $!"); |
| 336 |
} |
| 337 |
|
| 338 |
sub qualify ($) { |
| 339 |
my ($i) = @_; |
| 340 |
$i= "$default_dir$i" unless $i =~ m,^/,; |
| 341 |
return $i; |
| 342 |
} |
| 343 |
|
| 344 |
sub bad_modifiers ($) { |
| 345 |
local ($_) = @_; |
| 346 |
if (!eval { |
| 347 |
die "bad modifier $&" if m/[^!*\$\@~?]/; |
| 348 |
die "repeated modifier $1" if m/(.).*\1/; |
| 349 |
1; |
| 350 |
}) { |
| 351 |
$@ =~ s/\n//; |
| 352 |
return 1; |
| 353 |
} |
| 354 |
return 0; |
| 355 |
} |
| 356 |
|
| 357 |
sub zone_conf_settings ($$) { |
| 358 |
my ($cfg,$zone) = @_; |
| 359 |
my ($sfx,$aref); |
| 360 |
foreach $sfx (qw(self_soa self_ns self_addr forbid_addr forbid_slave |
| 361 |
conv_glueless indirect_glue)) { |
| 362 |
{ no strict 'refs'; $aref= [ @$sfx ]; } |
| 363 |
@$aref or cfg_fail("failed to specify $sfx before zone") |
| 364 |
if $sfx =~ m/^self/; |
| 365 |
$cfg->{$sfx}= $aref; |
| 366 |
} |
| 367 |
foreach $sfx (qw(self_soa self_ns)) { |
| 368 |
map { s/\*$/$zone/ } @{ $zone_cfg{$zone}{$sfx} }; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
sub zone_conf ($$$$$@) { |
| 373 |
my ($zone,$style,$sabbr,$mod,$file,@servers) = @_; |
| 374 |
$file= qualify("$slave_dir/$slave_prefix".$zone.$slave_suffix) |
| 375 |
unless length $file; |
| 376 |
if (!length $output) { |
| 377 |
$default_output= qualify('chiark-conf-gen.bind8') |
| 378 |
unless length $default_output; |
| 379 |
set_output($default_output); |
| 380 |
} |
| 381 |
cfg_fail("redefined zone $zone\n". |
| 382 |
" earlier definition $zone_cfg{$zone}{'where'}") |
| 383 |
if exists $zone_cfg{$zone}; |
| 384 |
$zone_cfg{$zone}{'where'}= $where; |
| 385 |
$zone_cfg{$zone}{'file'}= $file; |
| 386 |
$zone_cfg{$zone}{'style_p'}= $style.$mod; |
| 387 |
$zone_cfg{$zone}{'s'}= "$sabbr $mod $modifiers"; |
| 388 |
# p)rimary s)econdary u)npub f)oreign |
| 389 |
# followed by modifiers, first per-zone, then default |
| 390 |
$zone_cfg{$zone}{'servers'}= [ @servers ]; |
| 391 |
if ($domail) { |
| 392 |
length $admin && length $mail_state_dir or |
| 393 |
cfg_fail("mailing but failed to specify admin". |
| 394 |
" or mail-state-dir before zone"); |
| 395 |
$zone_cfg{$zone}{'admin'}= $admin; |
| 396 |
$zone_cfg{$zone}{'maildir'}= qualify($mail_state_dir); |
| 397 |
$zone_cfg{$zone}{'mailmwarn'}= $mail_max_warnfreq; |
| 398 |
} |
| 399 |
zone_conf_settings($zone_cfg{$zone}, $zone); |
| 400 |
$zone_cfg{$zone}{'output'}= $output; |
| 401 |
push @zone_cfg_list, $zone; |
| 402 |
} |
| 403 |
|
| 404 |
sub set_output($) { |
| 405 |
my ($newout) = @_; |
| 406 |
$output= $newout; |
| 407 |
$output_contents{$output}= ''; |
| 408 |
} |
| 409 |
|
| 410 |
#-------------------- checking |
| 411 |
|
| 412 |
use vars qw($zone $cfg $warnings %zone_warnings); |
| 413 |
$warnings= 0; |
| 414 |
|
| 415 |
sub progress ($$) { |
| 416 |
my ($minv,$m) = @_; |
| 417 |
return if $verbosity < $minv; |
| 418 |
print $progress_fh "$m\n" or die "$quis: $zone: write log: $!\n"; |
| 419 |
} |
| 420 |
|
| 421 |
sub verbose ($) { progress(3, ' ' . $_[0]); } |
| 422 |
|
| 423 |
sub process_zones (@) { |
| 424 |
my (@zones) = @_; |
| 425 |
local ($zone,$cfg); |
| 426 |
|
| 427 |
foreach $zone (@zones) { |
| 428 |
if ($zone =~ m/\.$/) { |
| 429 |
zone_warning("zone specified with trailing dot -". |
| 430 |
" will not work", ''); |
| 431 |
} |
| 432 |
|
| 433 |
$cfg= $zone_cfg{$zone}; |
| 434 |
if (!$cfg) { |
| 435 |
$cfg= { |
| 436 |
'style_p' => 'foreign', |
| 437 |
's' => "f $group2modcmd{'foreign'}", |
| 438 |
'servers' => [ ], |
| 439 |
}; |
| 440 |
zone_conf_settings($cfg, $zone); |
| 441 |
} |
| 442 |
|
| 443 |
mail_zone_before() or next |
| 444 |
if $domail; |
| 445 |
zone_reset(); |
| 446 |
progress(1, sprintf "%-20s %s", $zone, $$cfg{'style_p'}); |
| 447 |
if ($check && ($doall || !zone_style('?',0))) { |
| 448 |
eval { |
| 449 |
if ($localonly && $cfg->{'s'} =~ m/f/) { |
| 450 |
zone_warning("foreign zone specified with -l",''); |
| 451 |
} elsif ($localonly && $cfg->{'s'} !~ m/p/) { |
| 452 |
zone_check_local(); |
| 453 |
} else { |
| 454 |
zone_check_full(); |
| 455 |
} |
| 456 |
}; |
| 457 |
zone_warning("checks failed: $@",'') if length $@; |
| 458 |
} |
| 459 |
$output_contents{$$cfg{'output'}} .= zone_output() |
| 460 |
if $install; |
| 461 |
|
| 462 |
mail_zone_after() if $domail; |
| 463 |
} |
| 464 |
if ($domail) { |
| 465 |
} elsif ($warnings) { |
| 466 |
printf STDERR ("%s: %d warning(s) in %d zone(s);". |
| 467 |
" %d zone(s) checked ok.\n", |
| 468 |
$quis, |
| 469 |
$warnings, |
| 470 |
scalar(keys %zone_warnings), |
| 471 |
scalar(@zones - keys %zone_warnings)); |
| 472 |
} else { |
| 473 |
progress(1, sprintf "%d zone(s) checked ok", scalar @zones); |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
use vars qw(%delgs); # $delgs{$nameserver_list} = [ $whosaidandwhy ] |
| 478 |
use vars qw(%auths); # $auths{$nameserver_list} = [ $whosaidandwhy ] |
| 479 |
use vars qw(%glue); # $glue{$name}{$addr_list} = [ $whosaidandwhy ] |
| 480 |
use vars qw(%soas); # $soa{"$serial $mname"} = [ $whosaidandwhy ] |
| 481 |
use vars qw(%addr_is_ok %warned); |
| 482 |
use vars qw($delg_to_us); |
| 483 |
use vars qw(@to_check); # ($addr,$whyask,$name_if_auth,$glueless_ok, ...) |
| 484 |
use vars qw(@to_check_soa); # ($addr,$whyask,$name,$is_ns, ...) |
| 485 |
|
| 486 |
sub zone_warning ($$) { |
| 487 |
my ($w,$o) = @_; |
| 488 |
my ($wk); |
| 489 |
|
| 490 |
return 0 if !$repeat && $warned{$w}++; |
| 491 |
|
| 492 |
$w =~ s/\n$//; |
| 493 |
$w =~ s,\n, // ,g; |
| 494 |
|
| 495 |
$w .= " ($o)" if length $o; |
| 496 |
print $warn_fh "$zone: warning: $w\n" or die $!; |
| 497 |
$warnings++; |
| 498 |
$zone_warnings{$zone}++; |
| 499 |
return 1; |
| 500 |
} |
| 501 |
|
| 502 |
sub zone_warnmore ($) { |
| 503 |
print $warn_fh "$zone: $_[0]\n" or die $!; |
| 504 |
} |
| 505 |
|
| 506 |
sub zone_check_full () { |
| 507 |
zone_investigate(); |
| 508 |
zone_consistency(); |
| 509 |
zone_servers_ok(); |
| 510 |
} |
| 511 |
|
| 512 |
sub zone_reset() { |
| 513 |
%delgs= %auths= %glue= %soas= %warned= %addr_is_ok= (); |
| 514 |
$delg_to_us= 0; |
| 515 |
@to_check= @to_check_soa= (); |
| 516 |
} |
| 517 |
|
| 518 |
sub zone_investigate() { |
| 519 |
my ($super_zone, @start_nsnames, $start_ww, |
| 520 |
$start_ns, @start_ns_addrs, $s, $wa, $name_if_auth, |
| 521 |
%nsrrset_checked, %soa_checked, $addr, $glueless_ok, |
| 522 |
$rcode, $name, $is_ns); |
| 523 |
|
| 524 |
if (!zone_style('*',0)) { |
| 525 |
$super_zone= $zone; |
| 526 |
for (;;) { |
| 527 |
debug_trace("zone $zone superzone $super_zone"); |
| 528 |
$super_zone eq '.' |
| 529 |
and die "no superzone ? ($super_zone)\n"; |
| 530 |
$super_zone =~ s/^[^.]+\.// |
| 531 |
or $super_zone= '.'; |
| 532 |
($rcode,@start_nsnames)= |
| 533 |
lookup($super_zone,'ns-','06',"superzone search"); |
| 534 |
last if !$rcode; |
| 535 |
} |
| 536 |
$start_ww= "server for $super_zone"; |
| 537 |
} else { |
| 538 |
($rcode,@start_nsnames)= |
| 539 |
lookup($zone,'ns-','0',"initial nameserver search"); |
| 540 |
$start_ww= "nameserver for $zone"; |
| 541 |
} |
| 542 |
for $start_ns (@start_nsnames) { |
| 543 |
$start_ns= lc $start_ns; |
| 544 |
($rcode,@start_ns_addrs)= lookup($start_ns,'a','0',"$start_ww"); |
| 545 |
foreach $addr (@start_ns_addrs) { |
| 546 |
push @to_check, $addr, "$start_ns, $start_ww", undef, 0; |
| 547 |
} |
| 548 |
} |
| 549 |
for (;;) { |
| 550 |
# We do these in order so that we always do NS RRset checks on |
| 551 |
# nameservers that came from other NS RRsets first; otherwise |
| 552 |
# we might set nsrrset_checked due to a glueless_ok check, |
| 553 |
# and then not check for gluefulness later. |
| 554 |
debug_dump('@to_check @to_check_soa'); |
| 555 |
if (($addr,$wa,$name_if_auth,$glueless_ok,@to_check) = @to_check) { |
| 556 |
push @to_check_soa, $addr, $wa, $name_if_auth, 1, |
| 557 |
if defined $name_if_auth; |
| 558 |
next if $nsrrset_checked{$addr}++; |
| 559 |
zone_check_nsrrset($addr, $wa, $name_if_auth, $glueless_ok); |
| 560 |
} elsif (($addr,$wa,$name,$is_ns,@to_check_soa) = @to_check_soa) { |
| 561 |
next if $soa_checked{$addr}++; |
| 562 |
zone_check_soa($addr,$wa,$name,$is_ns); |
| 563 |
} else { |
| 564 |
last; |
| 565 |
} |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
sub zone_check_nsrrset ($$$$) { |
| 570 |
my ($uaddr,$wa, $name_if_auth, $glueless_ok) = @_; |
| 571 |
my (@s, $s, $a, %s2g, @glue, $glue, $delgs_or_auths, $wwn, $ww, $cg); |
| 572 |
my ($rcode); |
| 573 |
$ww= "[$uaddr] $wa"; |
| 574 |
verbose("checking delegation by $ww"); |
| 575 |
dig(sub { |
| 576 |
if ($dig_type eq 'ns' && $dig_owner eq $zone) { |
| 577 |
$s2g{lc $dig_rdata} = [ ]; |
| 578 |
} elsif ($dig_type eq 'a' && exists $s2g{$dig_owner}) { |
| 579 |
$wwn= "in glue from $ww"; |
| 580 |
zone_server_queue($dig_rdata,$dig_owner,$wwn,"NS [$uaddr]",0); |
| 581 |
push @{ $s2g{$dig_owner} }, $dig_rdata; |
| 582 |
} |
| 583 |
}, |
| 584 |
$zone,'ns',$uaddr); |
| 585 |
if (!%s2g) { |
| 586 |
zone_warning("unable to find NS RRset at [$uaddr]", $wa); |
| 587 |
return; |
| 588 |
} elsif (keys %s2g == 1) { |
| 589 |
zone_warning("only one nameserver ". (join '', keys %s2g), |
| 590 |
$ww); |
| 591 |
} |
| 592 |
@s= sort keys %s2g; |
| 593 |
foreach $s (@s) { |
| 594 |
zone_ns_name($s,$ww); |
| 595 |
@glue= @{ $s2g{$s} }; |
| 596 |
if (!@glue) { |
| 597 |
zone_warning("glueless NS $s", $ww) |
| 598 |
unless $glueless_ok |
| 599 |
or zone_style('~',!$needglue) |
| 600 |
or grep { has_suffix_of($zone,".$_"); } |
| 601 |
@{ $cfg->{'conv_glueless'} } |
| 602 |
or ((grep { has_suffix_of($s,".$_"); } |
| 603 |
@{ $cfg->{'indirect_glue'} }) and |
| 604 |
!(grep { has_suffix_of($zone,".$_"); } |
| 605 |
@{ $cfg->{'indirect_glue'} })); |
| 606 |
foreach $cg (@{ $cfg->{'conv_glueless'} }) { |
| 607 |
zone_warning("nameserver $s (glueless) in". |
| 608 |
" serverless-glueless namespace area $cg", $ww) |
| 609 |
if has_suffix_of(".$s",".$cg"); |
| 610 |
} |
| 611 |
($rcode,@glue)= lookup($s,'a','0',"glueless NS from $ww"); |
| 612 |
foreach $a (@glue) { |
| 613 |
$wwn= "glueless NS from $ww"; |
| 614 |
zone_server_queue($a,$s,$wwn,"NS [$uaddr]",0); |
| 615 |
} |
| 616 |
} |
| 617 |
$glue= join ' ', sort @glue; |
| 618 |
push @{ $glue{$s}{$glue} }, $ww; |
| 619 |
} |
| 620 |
$s= join ' ', @s; |
| 621 |
$delgs_or_auths= defined($name_if_auth) ? \%auths : \%delgs; |
| 622 |
push @{ $delgs_or_auths->{$s} }, $ww; |
| 623 |
} |
| 624 |
|
| 625 |
sub zone_ns_name ($$) { |
| 626 |
my ($name,$ww) = @_; |
| 627 |
$delg_to_us=1 if grep { $name eq $_ } @{ $cfg->{'self_ns'} }; |
| 628 |
zone_warning("published server, as $name, but configured as stealth", |
| 629 |
$ww) |
| 630 |
if $cfg->{'s'} =~ m/u/ && |
| 631 |
grep { $_ eq $name } |
| 632 |
@{ $cfg->{'self_ns'} }, @{ $cfg->{'self_soa'} }; |
| 633 |
} |
| 634 |
|
| 635 |
sub zone_server_queue ($$$$$) { |
| 636 |
my ($addr,$name,$wwn,$wwq,$is_soa) = @_; |
| 637 |
zone_server_addr($addr,$name,$wwn,$wwq,$is_soa); |
| 638 |
push @to_check, $addr, "$name, $wwn", $name, $is_soa; |
| 639 |
} |
| 640 |
|
| 641 |
sub zone_server_addr ($$$$$) { |
| 642 |
my ($addr,$name,$ww,$wwq,$is_soa) = @_; |
| 643 |
debug_trace("zone_server_addr ".join('|',@_)); |
| 644 |
$addr_is_ok{$addr}= "$name ($wwq)" |
| 645 |
if $is_soa || $cfg->{'s'} =~ m/u/; |
| 646 |
zone_warning("forbidden nameserver address [$addr] $name",$ww) |
| 647 |
if grep { $_ eq $addr } @{ $cfg->{'forbid_addr'} }; |
| 648 |
zone_warning("forbidden server address for our slave [$addr] $name",$ww) |
| 649 |
if $cfg->{'s'} =~ m/p/ and |
| 650 |
grep { $_ eq $addr } @{ $cfg->{'forbid_slave'} }; |
| 651 |
|
| 652 |
my ($name_is_self, $addr_is_self); |
| 653 |
$name_is_self= grep { $_ eq $name } |
| 654 |
@{ $cfg->{$is_soa ? 'self_soa' : 'self_ns'} }; |
| 655 |
$addr_is_self= grep { $_ eq $addr } |
| 656 |
@{ $cfg->{'self_addr'} }; |
| 657 |
if ($name_is_self && !$addr_is_self) { |
| 658 |
zone_warning("our $name supplied with wrong address [$addr]", $ww); |
| 659 |
} |
| 660 |
if (!$name_is_self && $addr_is_self) { |
| 661 |
zone_warning("we [$addr] are named in ". |
| 662 |
($is_soa ? "SOA" : "NS"). |
| 663 |
" by wrong name $name", |
| 664 |
$ww); |
| 665 |
} |
| 666 |
if (!$name_is_self && !$addr_is_self && |
| 667 |
$is_soa && $cfg->{'s'} =~ m/p/) { |
| 668 |
zone_warning("SOA MNAME $name is not us (". |
| 669 |
(join ' ', @{ $cfg->{'self_soa'} }).")", $ww); |
| 670 |
} |
| 671 |
$delg_to_us=1 if $addr_is_self && !$is_soa; |
| 672 |
} |
| 673 |
|
| 674 |
sub zone_check_soa ($$$$) { |
| 675 |
my ($uaddr,$wa,$name,$is_ns) = @_; |
| 676 |
my ($lame,$serial,$mname,$got,$rcode,@soa_addrs,$soa_addr,$ww,$wwn); |
| 677 |
verbose("checking service at [$uaddr] $name"); |
| 678 |
$lame= 'dead or lame'; |
| 679 |
$ww= "[$uaddr] $wa"; |
| 680 |
dig(sub { |
| 681 |
if ($dig_type eq 'flags:') { |
| 682 |
$lame= $dig_rdata =~ m/ aa / ? '' : 'lame'; |
| 683 |
} elsif ($dig_type eq 'soa' && $dig_owner eq $zone && !$lame) { |
| 684 |
die "several SOAs ? $ww" if defined $mname; |
| 685 |
$got= $dig_rdata; |
| 686 |
$got =~ m/^(\d+) (\S+)$/ or die "$got ?"; |
| 687 |
($serial,$mname) = ($1,$2); |
| 688 |
} |
| 689 |
}, |
| 690 |
$zone,'soa',$uaddr); |
| 691 |
$lame= 'broken' if !$lame && !defined $mname; |
| 692 |
if ($lame) { zone_warning("$lame server [$uaddr]",$wa); return; } |
| 693 |
progress(2, sprintf "%-16s %46s has %s%s", |
| 694 |
$zone, "$name [$uaddr]", $serial, $is_ns ? '' : '*'); |
| 695 |
push @{ $soas{$got} }, $ww; |
| 696 |
($rcode,@soa_addrs)= lookup($mname,'a','0',"SOA MNAME"); |
| 697 |
$wwn= "SOA MNAME from $ww"; |
| 698 |
foreach $soa_addr (@soa_addrs) { |
| 699 |
zone_server_queue($soa_addr,$mname,$wwn,"SOA [$uaddr]",1); |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
sub zone_consistency() { |
| 704 |
my ($d, $org_ser, $mname, $a, $h, $self_soa, $wa); |
| 705 |
zone_consistency_set('delegations',\%delgs); |
| 706 |
foreach $d (keys %delgs) { delete $auths{$d}; } |
| 707 |
zone_consistency_set('zone nameserver rrset',\%auths); |
| 708 |
foreach $h (keys %glue) { |
| 709 |
zone_consistency_set("glue for $h", $glue{$h}); |
| 710 |
} |
| 711 |
zone_consistency_set("serial number and/or SOA MNAME",\%soas); |
| 712 |
$self_soa= $cfg->{'self_soa'}; |
| 713 |
} |
| 714 |
|
| 715 |
sub zone_servers_ok () { |
| 716 |
my ($showok,%fs); |
| 717 |
if (%addr_is_ok) { |
| 718 |
$showok= 0; |
| 719 |
foreach $a (@{ $cfg->{'servers'} }) { |
| 720 |
next if exists $addr_is_ok{$a}; |
| 721 |
zone_warning("we slave from [$a]",'') |
| 722 |
and $showok=1; |
| 723 |
} |
| 724 |
if ($showok) { |
| 725 |
foreach $a (keys %addr_is_ok) { |
| 726 |
zone_warnmore("permitted master [$a] $addr_is_ok{$a}"); |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
if ($cfg->{'s'} =~ m/s/ && !$delg_to_us) { |
| 731 |
zone_warning("we are supposedly published secondary,". |
| 732 |
" but not listed as a nameserver",''); |
| 733 |
map { $fs{$_}=1 } keys(%delgs), keys(%auths); |
| 734 |
zone_warnmore("servers are: ". join ' ', sort keys %fs); |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
sub zone_consistency_set ($%) { |
| 739 |
my ($msg,$set) = @_; |
| 740 |
my ($d,$o); |
| 741 |
if (keys(%$set) > 1) { |
| 742 |
zone_warning("inconsistent $msg:",''); |
| 743 |
foreach $d (keys %$set) { |
| 744 |
foreach $o (@{ $set->{$d} }) { zone_warnmore(" $d from $o"); } |
| 745 |
} |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
sub zone_check_local () { |
| 750 |
zone_servers_simplefind(); |
| 751 |
zone_servers_ok(); |
| 752 |
} |
| 753 |
|
| 754 |
sub zone_servers_simplefind () { |
| 755 |
my ($rcode,@nsnames,$ns,@soas,$mname); |
| 756 |
|
| 757 |
($rcode,@nsnames)= lookup($zone,'ns-','0',"zone's servers"); |
| 758 |
foreach $ns (@nsnames) { |
| 759 |
zone_ns_name($ns,"NS"); |
| 760 |
zone_server_simple($ns,'NS',0); |
| 761 |
} |
| 762 |
$delgs{join ' ', sort @nsnames} = [ "zone's servers" ]; |
| 763 |
|
| 764 |
($rcode,@soas)= lookup($zone,'soa','0',"SOA MNAME"); |
| 765 |
die "multiple SOA RRs in set! @soas ?" if @soas!=1; |
| 766 |
$soas[0] =~ m/^(\S+)\s/ or die "SOA ? $_"; |
| 767 |
zone_server_simple(domain_canon($1,"lookup $zone SOA"),'SOA',1); |
| 768 |
} |
| 769 |
|
| 770 |
sub zone_server_simple ($$$) { |
| 771 |
my ($name,$ww,$is_soa) = @_; |
| 772 |
my ($rcode,@addrs,$addr); |
| 773 |
($rcode,@addrs)= lookup($name,'a','0', "server - ". |
| 774 |
($is_soa ? "SOA MNAME" : "NS")); |
| 775 |
foreach $addr (@addrs) { zone_server_addr($addr,$name,$ww,$ww,$is_soa); } |
| 776 |
} |
| 777 |
|
| 778 |
sub zone_style ($$) { |
| 779 |
my ($stylechar, $default) = @_; |
| 780 |
local ($_) = $$cfg{'s'}; |
| 781 |
$stylechar =~ s/\W/\\$&/ or die; |
| 782 |
return m/(\S*)$stylechar/ ? $1 !~ m/\!/ : $default; |
| 783 |
} |
| 784 |
|
| 785 |
#-------------------- mailing |
| 786 |
|
| 787 |
use vars qw($m_base $m_lastok @m_ok @m_fail |
| 788 |
$m_info $m_time $m_lock $m_m); |
| 789 |
|
| 790 |
sub mail_zone_before () { |
| 791 |
my (@s1,@s2); |
| 792 |
|
| 793 |
$m_base= $$cfg{'maildir'}.'/'.$zone; |
| 794 |
$m_lastok= '-'; |
| 795 |
@m_ok= (); |
| 796 |
@m_fail= (); |
| 797 |
|
| 798 |
for (;;) { |
| 799 |
$m_lock= new IO::File "${m_base}_lock", O_RDWR|O_CREAT, 0600 |
| 800 |
or die "$quis: create lockfile ${m_base}_lock: $!\n"; |
| 801 |
if (!flock($m_lock, LOCK_EX|LOCK_NB)) { |
| 802 |
<$m_lock> =~ m/^\d+ /; |
| 803 |
die "$quis: $zone: concurrrency? - flock $&$!\n"; |
| 804 |
$m_lock->close; |
| 805 |
return 0; |
| 806 |
} |
| 807 |
(@s1= $m_lock->stat) or die "$quis: fstat ${m_base}_lock: $!\n"; |
| 808 |
(@s2= stat "${m_base}_lock") or |
| 809 |
die "$quis: stat ${m_base}_lock: $!\n"; |
| 810 |
last if ($s1[0] eq $s2[0] && $s1[1] eq $s2[1]); |
| 811 |
$m_lock->close; |
| 812 |
} |
| 813 |
(print $m_lock "$$ \n" and $m_lock->flush) |
| 814 |
or die "$quis: write pid to ${m_base}_lock: $!\n"; |
| 815 |
|
| 816 |
if ($m_info= new IO::File "${m_base}_info", 'r') { |
| 817 |
$!=0; $_= <$m_info>; |
| 818 |
$_ =~ m/\n/ or die "$quis: read ${m_base}_info: $!\n"; |
| 819 |
m/^\d+ (\d+|-) ([0-9:]*) ([0-9:]*) / or |
| 820 |
die "$quis: ${m_base}_info malformed\n"; |
| 821 |
$m_lastok= $1; |
| 822 |
if ($domail ne 'first') { |
| 823 |
if ($2 eq ':' && $3 eq ':') { |
| 824 |
warn "$quis: $zone: mid/last run, but last". |
| 825 |
"run already done, ignoring zone\n"; |
| 826 |
return 0; |
| 827 |
} |
| 828 |
@m_ok= split /\:/, $2; |
| 829 |
@m_fail= split /\:/, $3; |
| 830 |
} |
| 831 |
} elsif ($! != &ENOENT) { |
| 832 |
die "$quis: open ${m_base}_info: $!\n"; |
| 833 |
} elsif ($domail ne 'first') { |
| 834 |
warn "$quis: $zone: first run, but not --mail-first,". |
| 835 |
" ignoring zone\n"; |
| 836 |
return 0; |
| 837 |
} |
| 838 |
if ($domail eq 'first') { |
| 839 |
remove "${m_base}_history" or $!==&ENOENT |
| 840 |
or die "$quis: remove ${m_base}_history: $!\n"; |
| 841 |
} |
| 842 |
$progress_fh= $warn_fh= new IO::File "${m_base}_history",'a',0666 |
| 843 |
or die "$quis: open ${m_base}_history: $!\n"; |
| 844 |
$m_info= new IO::File "${m_base}_info.tmp",'w',0666 |
| 845 |
or die "$quis: open ${m_base}_info.tmp: $!\n"; |
| 846 |
|
| 847 |
$m_time= time; |
| 848 |
progress(-1, "\n".('-'x70)."\n".ptime($m_time)."\n"); |
| 849 |
return 1; |
| 850 |
} |
| 851 |
|
| 852 |
sub mail_zone_after () { |
| 853 |
if ($zone_warnings{$zone}) { |
| 854 |
push @m_fail, $m_time; |
| 855 |
progress(-1,"failed - $zone_warnings{$zone} warnings"); |
| 856 |
} else { |
| 857 |
push @m_ok, $m_time; |
| 858 |
$m_lastok= $m_time; |
| 859 |
progress(-1,"everything is fine"); |
| 860 |
} |
| 861 |
close $progress_fh or die "$quis: close ${m_base}_history: $!\n"; |
| 862 |
$progress_fh= $warn_fh= $stderr_fh; |
| 863 |
|
| 864 |
if ($domail =~ m/^final/) { |
| 865 |
if (100*@m_fail <= $$cfg{'mailmwarn'}*(@m_fail + @m_ok)) { |
| 866 |
printf " %-40s ok\n", $zone or die "$quis: mail ok report: $!\n"; |
| 867 |
} elsif (zone_style('@',0)) { |
| 868 |
printf " %-40s mail suppressed\n", $zone |
| 869 |
or die "$quis: mail suppress report: $!\n"; |
| 870 |
} else { |
| 871 |
mail_zone_mail(); |
| 872 |
} |
| 873 |
} else { |
| 874 |
printf " %-40s %d warns. OK %s Fail %s\n", |
| 875 |
$zone, |
| 876 |
defined $zone_warnings{$zone} ? $zone_warnings{$zone} : 0, |
| 877 |
join(',', map { $_ - $m_time } @m_ok), |
| 878 |
join(',', map { $_ - $m_time } @m_fail) |
| 879 |
or die "$quis: checking progress report: $!\n"; |
| 880 |
} |
| 881 |
|
| 882 |
@m_fail= @m_ok= ('','') |
| 883 |
if $domail =~ m/^final/; |
| 884 |
|
| 885 |
printf $m_info "%s %s %s %s \n", |
| 886 |
$m_time, $m_lastok, join(':',@m_ok), join(':',@m_fail) |
| 887 |
or die "$quis: write new ${m_base}_info: $!\n"; |
| 888 |
$m_info->close or die "$quis: close new ${m_base}_info: $!\n"; |
| 889 |
rename "${m_base}_info.tmp", "${m_base}_info" |
| 890 |
or die "$quis: install new ${m_base}_info: $!\n"; |
| 891 |
$m_lock->close; |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
sub pmail ($) { |
| 896 |
print $m_m $_[0] |
| 897 |
or die "$quis: write ${m_base}_mail: $!\n"; |
| 898 |
} |
| 899 |
sub ptime ($) { |
| 900 |
my ($time) = @_; |
| 901 |
return gmtime($time)." GMT ($time)"; |
| 902 |
} |
| 903 |
|
| 904 |
sub mail_zone_mail () { |
| 905 |
my ($log, $zone_to, $zterr, $c, $r, $t, @soas); |
| 906 |
$m_m= new IO::File "${m_base}_mail", 'w', 0666 |
| 907 |
or die "$quis: create ${m_base}_mail: $!\n"; |
| 908 |
$zone_to=''; $zterr=''; |
| 909 |
if (!zone_style("\$", $$cfg{'s'} !~ m/[ps]/)) { |
| 910 |
eval { |
| 911 |
($r,@soas)= lookup($zone,'soa','0','problem-addr'); |
| 912 |
@soas==1 or die "multiple soas\n"; |
| 913 |
$soas[0] =~ m/^\S+ (\S.*\@\S+) [0-9 ]+$/ or |
| 914 |
die "bad soa \`$_'\n"; |
| 915 |
$zone_to= $1; |
| 916 |
}; |
| 917 |
$zterr= $@; |
| 918 |
$zterr =~ s/\n$//; |
| 919 |
} |
| 920 |
pmail <<END |
| 921 |
From: zone checker <$$cfg{'admin'}> |
| 922 |
Subject: $zone - configuration problems report |
| 923 |
END |
| 924 |
; |
| 925 |
pmail("To: "); |
| 926 |
pmail("(testing!) ") if $domail ne 'final'; |
| 927 |
pmail("SOA MNAME for $zone <$zone_to>\nCC: ") if length($zone_to); |
| 928 |
pmail($$cfg{'admin'}."\n\n"); |
| 929 |
pmail <<END |
| 930 |
You are receiving this mail because your email address is listed |
| 931 |
in the SOA (Start Of Authority) record for $zone |
| 932 |
in the DNS, which means you are supposedly the DNS administrator |
| 933 |
responsible for this zone. This report is generated automatically |
| 934 |
on behalf of $$cfg{'admin'}, who |
| 935 |
is the administrator for a server for the zone. Please contact |
| 936 |
them if you have any problem with this report. |
| 937 |
|
| 938 |
END |
| 939 |
if length $zone_to; |
| 940 |
pmail <<END |
| 941 |
Sent to $$cfg{'admin'} since SOA MNAME unavailable: |
| 942 |
$zterr |
| 943 |
|
| 944 |
END |
| 945 |
if length $zterr; |
| 946 |
pmail <<END |
| 947 |
The zone has had configuration errors or persistent operational |
| 948 |
problems during recent checks. See the logs below for details. |
| 949 |
|
| 950 |
Recent check history for $zone: |
| 951 |
END |
| 952 |
; |
| 953 |
if ($m_lastok ne '-') { |
| 954 |
pmail(" Last seen to be fine: ".ptime($m_lastok)."\n"); |
| 955 |
} else { |
| 956 |
pmail(" No record of this zone ever being fine.\n"); |
| 957 |
} |
| 958 |
for $t (@m_fail) { |
| 959 |
pmail(" Zone had problems at: ".ptime($t)."\n"); |
| 960 |
} |
| 961 |
for $t (@m_ok) { |
| 962 |
pmail(" Everything in order at: ".ptime($t)."\n"); |
| 963 |
} |
| 964 |
pmail("\n"); |
| 965 |
$log= new IO::File "${m_base}_history",'r' |
| 966 |
or die "$quis: reopen ${m_base}_history: $!"; |
| 967 |
undef $/; |
| 968 |
pmail($log->getline); |
| 969 |
$/= "\n"; |
| 970 |
(!$log->error and $log->close) |
| 971 |
or die "$quis: reread or close ${m_base}_log: $!\n"; |
| 972 |
$log->eof or die; |
| 973 |
$m_m->close or die "$quis: close ${m_base}_mail: $!\n"; |
| 974 |
$m_m= new IO::File "${m_base}_mail" |
| 975 |
or die "$quis: reopen ${m_base}_mail: $!"; |
| 976 |
|
| 977 |
defined($c= fork) or die "$quis: fork for mail: $!\n"; |
| 978 |
if (!$c) { |
| 979 |
defined dup2($m_m->fileno, 0) |
| 980 |
or die "$quis - sendmail: dup for stdin: $!\n"; |
| 981 |
exec (qw(/usr/sbin/sendmail -odb -oee -oi), |
| 982 |
($domail eq 'final' ? '-t' : $$cfg{'admin'})); |
| 983 |
die "$quis - sendmail: exec: $!\n"; |
| 984 |
} |
| 985 |
$m_m->close; |
| 986 |
$!=0; $r= waitpid $c,0; |
| 987 |
$r == $c or die "$quis: waitpid sendmail ($c): $r $!"; |
| 988 |
$? and warn "$quis: sendmail failed: $?\n"; |
| 989 |
|
| 990 |
printf " %-40s %s\n", $zone, |
| 991 |
length $zone_to ? $zone_to : 'reporting to admin' |
| 992 |
or die "$quis: write mailing report: $!\n"; |
| 993 |
} |
| 994 |
|
| 995 |
#-------------------- outputting |
| 996 |
|
| 997 |
sub zone_output () { |
| 998 |
my ($o,$m); |
| 999 |
|
| 1000 |
$o= "zone \"$zone\" {\n"; |
| 1001 |
if ($$cfg{'s'} =~ m/p/) { |
| 1002 |
$o.= " type master;\n"; |
| 1003 |
} else { |
| 1004 |
$o.= " type slave;\n". |
| 1005 |
" masters {\n"; |
| 1006 |
foreach $m (@{ $$cfg{'servers'} }) { $o.= " $m;\n"; } |
| 1007 |
$o.= " };\n"; |
| 1008 |
} |
| 1009 |
$o.= " file \"$$cfg{'file'}\";\n"; |
| 1010 |
$o.= "};\n"; |
| 1011 |
return $o; |
| 1012 |
} |
| 1013 |
|
| 1014 |
sub output_files () { |
| 1015 |
my ($fn,$ofn,$mfn,$l,$dir, $maxmode,$h,@to_install); |
| 1016 |
|
| 1017 |
foreach $ofn (keys %output_contents) { |
| 1018 |
$fn= $ofn; $mfn= "output file $fn"; |
| 1019 |
for (;;) { |
| 1020 |
if (!lstat $fn) { |
| 1021 |
$! == &ENOENT or die "$quis: stat $mfn:\n $!\n"; |
| 1022 |
$maxmode= 0666; |
| 1023 |
last; |
| 1024 |
} elsif (-f _) { |
| 1025 |
$maxmode= (stat _)[2]; |
| 1026 |
last; |
| 1027 |
} elsif (-l _) { |
| 1028 |
defined($l= readlink $fn) |
| 1029 |
or die "$quis: readlink $mfn:\n $!\n"; |
| 1030 |
$dir= $fn =~ m,^.*/, ? $& : './'; |
| 1031 |
$fn= "$dir$l" unless $l =~ m,^/,; |
| 1032 |
$mfn= "output file $fn (symlink target of $ofn)"; |
| 1033 |
} else { |
| 1034 |
die "$quis: output file $mfn exists but is not a file". |
| 1035 |
" (or symlink to one)"; |
| 1036 |
} |
| 1037 |
} |
| 1038 |
unlink "$fn.new" or $! == &ENOENT or |
| 1039 |
die "$quis: cannot clear out old .new version of $mfn:\n $!"; |
| 1040 |
$h= new IO::File "$fn.new",'w',$maxmode |
| 1041 |
or die("$quis: create .new version of $mfn:\n $!"); |
| 1042 |
print $h |
| 1043 |
"# generated by $quis, do not edit\n", |
| 1044 |
$output_contents{$ofn} |
| 1045 |
or die "$quis: write data to .new version of $mfn:\n $!"; |
| 1046 |
$h->close |
| 1047 |
or die "$quis: close .new version of $mfn:\n $!"; |
| 1048 |
push @to_install, $fn,$mfn; |
| 1049 |
} |
| 1050 |
|
| 1051 |
while (($fn,$mfn, @to_install) = @to_install) { |
| 1052 |
rename "$fn.new",$fn |
| 1053 |
or die "$quis: install new version of $mfn:\n $!"; |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
#-------------------- general utilities |
| 1058 |
|
| 1059 |
sub debug_dump ($) { |
| 1060 |
my ($vn); |
| 1061 |
return unless $debug>1; |
| 1062 |
local $Data::Dumper::Terse=1; |
| 1063 |
foreach $vn (split /\s+/, $_[0]) { |
| 1064 |
print "$vn := ", eval "Dumper(\\$vn)"; |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
sub debug_trace ($) { |
| 1069 |
return unless $debug; |
| 1070 |
print "D $_[0]\n"; |
| 1071 |
} |
| 1072 |
|
| 1073 |
sub has_suffix_of ($$) { |
| 1074 |
my ($whole,$suffix) = @_; |
| 1075 |
debug_trace("has_suffix_of $whole $suffix"); |
| 1076 |
return 0 if length $whole < length $suffix; |
| 1077 |
return 0 if substr($whole, length($whole) - length($suffix)) ne $suffix; |
| 1078 |
debug_trace("has_suffix_of $whole $suffix YES"); |
| 1079 |
return 1; |
| 1080 |
} |
| 1081 |
|
| 1082 |
sub lookup ($$$$) { |
| 1083 |
my ($domain,$type,$okrcodes,$w) = @_; |
| 1084 |
my ($c,$h,@result); |
| 1085 |
debug_trace("lookup ==> (->$okrcodes) $domain $type"); |
| 1086 |
$h= new IO::Handle; |
| 1087 |
|
| 1088 |
defined($c= open $h, "-|") or die "$quis: fork adnshost:\n $!\n"; |
| 1089 |
if (!$c) { |
| 1090 |
exec 'adnshost','-Fi','+Do','+Dt','+Dc','-Cf',"-t$type", |
| 1091 |
'-',"$domain"; |
| 1092 |
die "$quis: exec adnshost:\n $!\n"; |
| 1093 |
} |
| 1094 |
@result= $h->getlines(); |
| 1095 |
$h->error and die "$quis: read from adnshost:\n $!\n"; |
| 1096 |
chomp @result; |
| 1097 |
$!=0; $h->close; |
| 1098 |
die "$quis: lookup -t$type $domain $okrcodes ($w) failed $? $! @result\n" |
| 1099 |
if $! or $?&255 or $?>1536 or index($okrcodes,$?>>8)<0; |
| 1100 |
debug_trace("lookup <== $? @result"); |
| 1101 |
return ($?,@result); |
| 1102 |
} |
| 1103 |
|
| 1104 |
sub dig (&$$$) { |
| 1105 |
my ($eachrr, $qowner,$qtype,$qaddr) = @_; |
| 1106 |
# also pseudo-rr with type `flags:' |
| 1107 |
my ($h,$inmid,$irdata,$c,$digwhy); |
| 1108 |
local ($_); |
| 1109 |
|
| 1110 |
debug_trace("dig ==> \@$qaddr $qowner $qtype"); |
| 1111 |
|
| 1112 |
$h= new IO::Handle; |
| 1113 |
defined($c= open $h, "-|") or die "$quis: fork dig:\n $!\n"; |
| 1114 |
if (!$c) { |
| 1115 |
open STDERR, ">&STDOUT" or die $!; |
| 1116 |
exec ('dig', |
| 1117 |
'+nodef','+nosea','+norecurse', |
| 1118 |
"\@$qaddr",'-t',$qtype,$qowner); |
| 1119 |
die "$quis: exec dig:\n $!\n"; |
| 1120 |
} |
| 1121 |
$inmid=''; |
| 1122 |
for (;;) { |
| 1123 |
if (!defined($_= $h->getline())) { |
| 1124 |
$h->error() and die "$quis: read from dig:\n $!\n"; |
| 1125 |
last; |
| 1126 |
} |
| 1127 |
chomp; |
| 1128 |
if (length $inmid) { |
| 1129 |
s/^\s+/ / or die "$inmid // $_ ?"; |
| 1130 |
s/\;.*$//; |
| 1131 |
$_= $inmid.$_; |
| 1132 |
$inmid=''; |
| 1133 |
s/$/ \(/ unless s/\s*\)\s*$//; |
| 1134 |
} |
| 1135 |
if (s/\s*\(\s*$//) { $inmid= $_; next; } |
| 1136 |
$digwhy= "dig $qowner $qtype $qaddr \`$_'"; |
| 1137 |
if (m/^\;\; flags\:( [-0-9a-z ]+)\;/) { |
| 1138 |
$dig_owner=''; $dig_type='flags:'; $dig_rdata= "$1 "; |
| 1139 |
debug_trace("dig f: $dig_rdata"); |
| 1140 |
&$eachrr; |
| 1141 |
} elsif (m/^\;/) { |
| 1142 |
} elsif (!m/\S/) { |
| 1143 |
} elsif (m/^([-.0-9a-z]+)\s+\d\w+\s+in\s+([a-z]+)\s+(\S.*)/i) { |
| 1144 |
$dig_owner=domain_canon($1,$digwhy); $dig_type=lc $2; $irdata=$3; |
| 1145 |
if ($dig_type eq 'a') { |
| 1146 |
$irdata =~ m/^[.0-9]+$/ or die "$irdata ?"; |
| 1147 |
$dig_rdata= $&; |
| 1148 |
} elsif ($dig_type eq 'ns') { |
| 1149 |
$irdata =~ m/^[-.0-9a-z]+$/i or die "bad nameserver $irdata ?"; |
| 1150 |
$dig_rdata= domain_canon($irdata,$digwhy); |
| 1151 |
} elsif ($dig_type eq 'soa') { |
| 1152 |
$irdata =~ m/^([-.0-9a-z]+)\s+.*\s+(\d+)(?:\s+\d\w+){4}$/i |
| 1153 |
or die "bad SOA $irdata ?"; |
| 1154 |
$dig_rdata= $2.' '.domain_canon($1,$digwhy); |
| 1155 |
} else { |
| 1156 |
debug_trace("ignoring uknown RR type $dig_type"); |
| 1157 |
next; |
| 1158 |
} |
| 1159 |
debug_trace("dig $dig_owner $dig_type $dig_rdata"); |
| 1160 |
&$eachrr; |
| 1161 |
} else { |
| 1162 |
debug_trace("ignoring unknown dig output $_"); |
| 1163 |
} |
| 1164 |
} |
| 1165 |
$h->close; |
| 1166 |
debug_trace("dig <== gave $?"); |
| 1167 |
} |
| 1168 |
|
| 1169 |
sub domain_canon ($$) { |
| 1170 |
my ($i,$w) = @_; |
| 1171 |
$i =~ s/(.)\.$/$1/; |
| 1172 |
return '.' if $i eq '.'; |
| 1173 |
die "domain $i ($w) ?" unless $i =~ m/^[0-9a-z]/i; |
| 1174 |
return lc $i; |
| 1175 |
} |