#!/usr/bin/perl -wT --

#
# Perl command line -application to check for DNS servers that serve recursive queries
#
# Copyright (c) 2013 JaTu
# * This program is free software; you can redistribute it and/or
# * modify it under the terms of the GNU General Public License
# * as published by the Free Software Foundation; either version 2
# * of the License, or (at your option) any later version.
#
# Version history:
# 0.10	1st Apr 2013	Initial version
#

use POSIX qw(locale_h);
use Net::IP;

use lib "./";
use DNSTester;

use strict;
use utf8;
use 5.010;

#
# Begin script
#
	$ENV {"PATH"} = "/bin:/sbin:/usr/bin";

	my $dnsServerToCheck;
	my $dnsServerToDisplay;
	my $messageToDisplay;
	my $responseTimeOut = 10;

    die "DNS-server IP-address missing!\n" if ($#ARGV < 0);

    $dnsServerToDisplay = $ARGV[0];
    $dnsServerToDisplay =~ s/^\s+//s;
    $dnsServerToDisplay =~ s/\s+$//s;
    if (Net::IP::ip_is_ipv4($dnsServerToDisplay) ||
        Net::IP::ip_is_ipv6($dnsServerToDisplay)) {
        $dnsServerToCheck = $dnsServerToDisplay;
    } else {
        die "Invalid IP-address '${dnsServerToDisplay}'!\n";
    }

    my $stat = check_DNS_server($dnsServerToCheck, $responseTimeOut);
    given ($stat) {
        when (1) {
            print "The DNS server in IP-address ${dnsServerToCheck} is configured correctly. It properly refused to process a recursive query.\n";
        }
        when (3) {
            print "There was no response from IP-address ${dnsServerToCheck} in ${responseTimeOut} seconds.\n";
            print "Most likely the server is configured not to respond to any requests from this server's network or there is no functional DNS-server in the given address.\n";
        }
        when (4) {
            print "Inconclusive: The DNS server in IP-address ${dnsServerToCheck} is responding to a query, but the response is nonsense.\n";
            print "Most likely this is not a working DNS-server.\n";
        }
        when (5) {
            print "Inconclusive: The DNS server in IP-address ${dnsServerToCheck} is responding, but the response is malformed.\n";
            print "Most likely this is not a working DNS-server.\n";
        }
        when (6) {
            print "The DNS server in IP-address ${dnsServerToCheck} is configured incorrectly. Contact the administrators and inform that they are a potential source of DDOS-attack.\n";
        }
    }

# End-of-script
