+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: $!\n";
+ if (!flock($m_lock, LOCK_EX|LOCK_NB)) {
+ <$m_lock> =~ m/^\d+ /;
+ die "$quis: $zone: concurrrency? - flock $&$!\n";
+ $m_lock->close;
+ return 0;
+ }
+ (@s1= $m_lock->stat) 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";
+ return 0;
+ }
+ @m_ok= split /\:/, $2;
+ @m_fail= split /\:/, $3;
+ }
+ } elsif ($! != &ENOENT) {
+ die "$quis: open ${m_base}_info: $!\n";
+ } elsif ($domail ne 'first') {
+ warn "$quis: $zone: first run, but not --mail-first,".
+ " ignoring zone\n";
+ return 0;
+ }
+ 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");
+ return 1;
+}
+
+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_fh;
+
+ if ($domail =~ m/^final/) {
+ if (100*@m_fail <= $$cfg{'mailmwarn'}*(@m_fail + @m_ok)) {
+ printf " %-40s ok\n", $zone or die "$quis: mail ok report: $!\n";
+ } elsif (zone_style('@',0)) {
+ printf " %-40s mail suppressed\n", $zone
+ or die "$quis: mail suppress report: $!\n";
+ } else {
+ mail_zone_mail();
+ }
+ } else {
+ printf " %-40s %d warns. OK %s Fail %s\n",
+ $zone,
+ defined $zone_warnings{$zone} ? $zone_warnings{$zone} : 0,
+ join(',', map { $_ - $m_time } @m_ok),
+ join(',', map { $_ - $m_time } @m_fail)
+ or die "$quis: checking progress report: $!\n";
+ }
+
+ @m_fail= @m_ok= ('','')
+ if $domail =~ m/^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, @soas);
+ $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/[ps]/)) {
+ eval {
+ ($r,@soas)= lookup($zone,'soa','0','problem-addr');
+ @soas==1 or die "multiple soas\n";
+ $soas[0] =~ m/^\S+ (\S.*\@\S+) [0-9 ]+$/ or
+ die "bad soa \`$_'\n";
+ $zone_to= $1;
+ };
+ $zterr= $@;
+ $zterr =~ s/\n$//;
+ }
+ pmail <<END
+From: zone checker <$$cfg{'admin'}>
+Subject: $zone - configuration problems report
+END
+;
+ pmail("To: ");
+ pmail("(testing!) ") if $domail ne 'final';
+ pmail("SOA MNAME for $zone <$zone_to>\nCC: ") if length($zone_to);
+ pmail($$cfg{'admin'}."\n\n");
+ pmail <<END
+You are receiving this mail because your email address is listed
+in the SOA (Start Of Authority) record for $zone
+in the DNS, which means you are supposedly the DNS administrator
+responsible for this zone. This report is generated automatically
+on behalf of $$cfg{'admin'}, who
+is the administrator for a server for the zone. Please contact
+them if you have any problem with this report.
+
+END
+if length $zone_to;
+ pmail <<END
+Sent to $$cfg{'admin'} since SOA MNAME unavailable:
+$zterr
+
+END
+if length $zterr;
+ pmail <<END
+The zone has had configuration errors or persistent operational
+problems during recent checks. See the logs below for details.
+
+Recent check history for $zone:
+END
+;
+ if ($m_lastok ne '-') {
+ pmail(" Last seen to be fine: ".ptime($m_lastok)."\n");
+ } else {
+ pmail(" No record of this zone ever being fine.\n");
+ }
+ for $t (@m_fail) {
+ pmail(" Zone had problems at: ".ptime($t)."\n");
+ }
+ for $t (@m_ok) {
+ pmail(" Everything in order at: ".ptime($t)."\n");
+ }
+ pmail("\n");
+ $log= new IO::File "${m_base}_history",'r'
+ or die "$quis: reopen ${m_base}_history: $!";
+ undef $/;
+ pmail($log->getline);
+ $/= "\n";
+ (!$log->error and $log->close)
+ or die "$quis: reread or close ${m_base}_log: $!\n";
+ $log->eof or die;
+ $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) {
+ defined dup2($m_m->fileno, 0)
+ or die "$quis - sendmail: dup for stdin: $!\n";
+ exec (qw(/usr/sbin/sendmail -odb -oee -oi),
+ ($domail eq 'final' ? '-t' : $$cfg{'admin'}));
+ 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,
+ length $zone_to ? $zone_to : 'reporting to admin'
+ or die "$quis: write mailing report: $!\n";
+}
+