#!/bin/sh
#
# $Id: domainchk,v 0.9 2009/07/20 20:39:07 stef Exp $
#
# Simple script that gathers information from the DNS about domain names
# and outputs it in a pretty format.
#
# Prerequisites: awk, cut, date, dig, sed, tr
#
# Copyright (c) 2009, stef, http://www.bigpointyteeth.se/
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# - forward and reverse on top domain?
# - reverse on a couple of the names returned?
# - forward on a non-existant subdomain?
# - forward on all domain named found?

PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"

me=$(basename $0)

usage() {
    echo "Usage: (1) $me <domain>[,domain]"
    echo "       (2) $me <domains file>"
    exit 1
}

if test -z $1; then
    usage
fi

# is the positional argument a file?
if test -f $1; then
    domains=$(cat $1)
else
    # if not we assume it is a domain
    domains=$(echo $1 | tr "," " ")
fi

# TODO we should do a check of $domains here

for domain in $domains; do
    echo -n $domain
    ipaddr="\t($(host $domain | grep "has address" | awk '{print $4}'))"
    if test $? -eq 0; then
        echo -e $ipaddr
    else
        echo " ()"
    fi

    # make DNS queries and remove first column
    output=$( (dig $domain soa; dig $domain ns;\
               dig $domain mx; dig $domain txt;\
               dig $domain spf; dig $domain srv) |\
               grep -v ";" | grep $domain | cut -f 2- | sed 's/^[ \t]*//' | sort -u)

    # extract names of authorative DNS servers
    nservers=$(echo "$output" | grep NS | awk '{print $4}')

    if test -n "$output"; then
        echo "$output"
    else
        echo "-"
    fi
    echo ""
done

exit 0

# eof
