#!/usr/bin/env perl
# Verify existence of SOA records for device's domain and its IP subnets
use strict;
use warnings;
use File::Basename;
use Net::DNS;
use Socket;

die 'Usage: ', basename($0), " <hostname>\n"
        unless scalar @ARGV == 1;
die "Hostname cannot be empty!\n"
        unless length( my $hostname = $ARGV[0] );
die "Hostname without a valid domain!\n"
        unless ( $hostname =~ /^[^.]*\.(.*)/ && length( my $domain = $1 ) );
die "Hostname without a valid IP!\n"
        unless my $ip = inet_aton $hostname;
$ip = inet_ntoa $ip;

my $res = Net::DNS::Resolver->new;
if ( $res->query( $domain, 'SOA' ) ) {
        print "SOA record exists for domain \"$domain\"\n";
}
else {
        print "No SOA record for for domain \"$domain\"\n";
}

my @rsubnet = reverse split '\.', $ip;
my $suffix = '';
for my $netmask ( 24, 16, 8 ) {
        shift @rsubnet;
        $suffix .= '.0';
        if ( $res->query( join( '.', @rsubnet ) . '.in-addr.arpa', 'SOA' ) ) {
                my $subnet = join('.', reverse @rsubnet );
                die "SOA record exists for subnet \"$subnet$suffix/$netmask\"\n";
        }
}

print "No SOA record for any of \"$ip\" subnets!\n";