#!/usr/bin/perl
#
# Check the freshness of slave zones.
#
# Just verify the files are reasonably new. Do not do anything clever
# with SOA records. Do not ensure that all slave zones are present
# (assume the admin will have spotted complete absence of the zone).
#
# $Cambridge: hermes/conf/bind/bin/checkslave,v 1.1 2011/07/19 14:08:27 fanf2 Exp $

use warnings;
use strict;

sub uid2name {
    my $uid = shift;
    my @pwent = getpwuid $uid;
    return $pwent[0] or $uid;
}

my $zs = "/spool/bind/zs";
opendir my $dir, $zs
    or die "could not read slave zone directory $zs: $!\n";
my @dir = sort readdir $dir;
closedir $dir
    or die "failed to read slave zone directory $zs: $!\n";

my @warn;
sub bleat {
    my ($name,$type,$value,$expected) = @_;
    push @warn, sprintf "%s\tunexpected %s %s\n",
	$name, $type, $value
	    if $value ne $expected;
}

for my $f (@dir) {
    next if $f =~ m{^[.]|[.]jnl$};
    my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
	$atime,$mtime,$ctime,$blksize,$blocks) = stat "$zs/$f";
    my $perms = sprintf "%04o", $mode & 07777;
    my $owner = uid2name $uid;
    my $age = sprintf "%d days", (time - $mtime) / 86400;
    bleat $f, "permissions", $perms, "0644";
    bleat $f, "owner", $owner, "named";
    bleat $f, "age", $age, "0 days";
}

if (@warn) {
    my $hostname = qx{hostname};
    chomp $hostname;
    print <<EOF;
##
## $hostname slave zone check failed !!!
##

@warn
EOF
    exit 1;
} else {
    exit 0;
}

# eof
