#!/usr/bin/perl
#
# par Julien Wajsberg <flash@minet.net>
#
# License GPL v2
#

use warnings;
use strict;
use Net::DNS;

my $type = "text";
my $header = 0;
my $footer = 0;

my $erreur = 0;

my $domain;

if (@ARGV) {
	while (($_ = shift) and /^-/) {
		$type = "html" if /^-html$/i ;
		$header = 1 if /^-header$/i ;
		$footer = 1 if /^-footer$/i ;
	}
	$domain = $_;
}

my $printer = new Printer($type);

#$printer->print_line("Printer $type correctly initialized");
$printer->print_line("Usage: $0 [ -html ] [ -header ] [ -footer ] <domain.>"), exit 1 unless (defined $domain and $domain =~ /\.$/);

$| = 1;

$printer->print_header($domain) if ($header);
$printer->print_blockheader("Checking domain $domain...");

my $res = Net::DNS::Resolver->new;
my $query = $res->query("$domain", "NS");

if ($query) {
	$res->recurse(0);
	foreach my $rr (grep { $_->type eq 'NS' } $query->answer) {
		my $dns = $rr->nsdname;

		$printer->print_start("$dns :");

		$res->nameservers($dns);
		$query = $res->query("$domain", "SOA");
		if ($query) {
			my @results = $query->answer;
			if ($#results > 0) {
				$printer->print_ko("more than one SOA ?!");
				$erreur = 1;
			} elsif ($#results < 0) {
				$printer->print_ko("no SOA found for $domain");
				$erreur = 1;
			} else {
				$printer->print_ok("ok");
			}
		} else {
			$printer->print_ko("query failed: ". $res->errorstring);
			$erreur = 1;
		}
	}
}
else {
	$printer->print_line("query failed: ". $res->errorstring);
	$erreur = 1;
}
$printer->print_blockfooter();
$printer->print_footer($domain) if ($footer);

exit $erreur;

#-------------------------------------------------------------------
{
	package Printer ;

	sub new {
		my $self = shift;
		my $type = shift;

		for ($type) {
			if (/html/i) { return "Printer::HTML" }
			elsif (/text/i) { return "Printer::Text" }
		}
	}
}

{
	package Printer::HTML ;

	sub print_header($$) {
		my $self = shift;
		my $domain = shift;
		print "<html><head><title>Domain Name Servers check for $domain</title>";
		print "<link type='text/css' rel='stylesheet' href='style.css' />";
		print "</head>\n";
		print "<body>\n";
		print "<h1>Domain Name Servers check for $domain</h1>\n";
	}

	sub print_blockheader($$) {
		my $self = shift;
		my $line = shift;

		print "<div class='domain'>";
		print "<div class='blockheader'>", $line, "</div>\n";
	}

	sub print_blockfooter() {
		print "</div>";
	}

	sub print_footer($) {
		print "</body></html>";
	}
	
	sub print_line($$) {
		my $self = shift;
		my $line = shift;

		print "<div>", $line, "</div>\n";
	}

	sub print_start($$) {
		my $self = shift;
		my $line = shift;

		print "<div><span class='start'>", $line, "</span> ";
	}

	sub print_ok($$) {
		my $self = shift;
		my $line = shift;

		print "<span class='ok'>", $line, "</span></div>\n";
	}
	
	sub print_ko($$) {
		my $self = shift;
		my $line = shift;

		print "<span class='ko'>", $line, "</span></div>\n";
	}

}

{
	package Printer::Text ;

	sub print_line($$) {
		my $self = shift;
		my $line = shift;

		print $line, "\n";
	}
	
	sub print_start($$) {
		my $self = shift;
		my $line = shift;

		print $line, " ";
	}

	sub print_ok($$) {
		&print_line(@_);
	}

	sub print_ko($$) {
		&print_line(@_);
	}

	sub print_header($$) {
	}

	sub print_footer($$) {
	}

	sub print_blockheader() {
		&print_line(@_);
	}

	sub print_blockfooter() {
	}
}

1;

__END__


