#!/bin/sh -e
#
# Test if the DNS server works.

. /usr/share/debian-edu-config/testsuite-lib.sh

# Ensure constent output when testing
LC_ALL=C
export LC_ALL

if test -r /etc/debian-edu/config ; then
    . /etc/debian-edu/config
fi

check_file_perm /etc/resolv.conf 644

# In case we are a standalone profile, just exit gracefully
if echo "$PROFILE" | grep -q Standalone ; then
	exit 0
fi

# The internal DNS server is called 'domain'
dnsserver='domain'

if echo "$PROFILE" | grep -q Main-Server ; then
    dnsserver=localhost
fi

hostvariant="$(dpkg -S /usr/bin/host |cut -d":" -f1)"

test_addr() {
    severity=$1
    hostinfo=$2
    host=`echo $hostinfo | cut -d: -f1`
    ip=`echo $hostinfo | cut -d: -f2`

   #echo "Testing '$host' '$ip'"
   case "$hostvariant" in
      #bind9-host
      bind9-host)
          forwardip=`host  "$host" "$dnsserver" 2>/dev/null | grep -v 'CNAME' | awk '/has address/ { print $4; }'`
          reversename=`host  "$ip" "$dnsserver" 2>/dev/null | grep 'pointer' | awk '{ print $5; }' | sed -e 's/\.$//g' `
	  ;;
      #default to old way
      host | *) 
          forwardip=`host -q  "$host" "$dnsserver" 2>/dev/null | grep -v 'CNAME' | awk '{ print $3; }'`
          reversename=`host -q  "$ip" "$dnsserver" 2>/dev/null | grep '^Name:' | awk '{ print $2; }'`
	  ;;
   esac


    if test "$ip" = "$forwardip" ; then
	echo "success: $0: DNS lookup for '$host' on server '$dnsserver' works ('$forwardip')."
    else
	echo "$severity: $0: Unable to look up '$host' on server '$dnsserver' ('$forwardip')."
    fi

    # Use grep to handle multiple DNS names in reverse.
    if echo "$reversename" | grep -qw "$host" ; then
	echo "success: $0: DNS lookup for '$ip' on server '$dnsserver' works ('$reversename')."
    else
	shortname=`echo $reversename | cut -d. -f1`
	if test "$host" = "$shortname" ; then
	    echo "success: $0: DNS lookup for '$ip' on server '$dnsserver' works ('$reversename')."
	else
	    echo "$severity: $0: Unable to look up '$ip' on server '$dnsserver' ('$reversename' != '$host')."
	fi
    fi
}


# Check if we have internet connectivity
if ping -c2 158.36.191.137 > /dev/null 2>&1 ; then
    echo "success: $0: Got internet connectivity, testing global DNS."
    for hostinfo in \
	maintainer.skolelinux.no:158.36.191.137
      do
      test_addr info $hostinfo
    done
else
    echo "warning: $0: No internet connectivity (ping 158.36.191.137 failed)."
fi

# Check if forward and reverse is correct
for hostinfo in \
    gateway:10.0.0.1 \
    tjener:10.0.2.2 \
    tjener.intern:10.0.2.2 \
    ltspserver00.subnet00.intern:192.168.0.254 \
    ltspserver01.subnet01.intern:192.168.1.254 \
  ; do
    test_addr error $hostinfo
done

# Check the presense of the CNAME records we expect
for cname in \
    backup \
    ntp \
    sitesummary \
    syslog \
    webcache \
    wpad \
    www \
    ldap \
    kerberos \
    postoffice \
    domain \
  ; do
    if host -t cname "$cname" "$dnsserver" | grep -q SRV ; then
        echo "error: $0: Failed to find CNAME record for $cname on server $dnsserver"
    else
        echo "success: $0: Found CNAME record for $cname on server $dnsserver"
    fi
done

# Check the presense of the SRV records we use.
for srv in \
    _ldap._tcp \
    _syslog._udp \
    _kerberos._udp \
    _kpasswd._tcp \
  ; do
    if host -N 2 -t srv $srv "$dnsserver" | grep -q SRV ; then
        echo "success: $0: Found SRV record for $srv on server $dnsserver"
    else
        echo "error: $0: Failed to find SRV record for $srv on server $dnsserver"
    fi
done

if [ -e /var/mail/root ] && \
    grep -q 'Reloading the zone .* was successful' /var/mail/root ; then
    echo "error: $0: ldap2zone sending email every hour (BTS #653053)."
else
    echo "success: $0: ldap2zone not sending email every hour (BTS #653053)."
fi
