#!/bin/sh
test -z "$(which pwgen)" && { echo '"pwgen" not found in $PATH' ; exit 23 ; }
rand=$(pwgen 20 1)
test_domain="test.nanl.de"
monitoring_domain="nanl.de"
refresh_time_internal=15
refresh_time_external=0 # if the external server doesn't have a higher TTL than the internal servers, this should be set to 0
poweradmin_url="https://dns.nanl.de"
poweradmin_username="monitoring"
poweradmin_password="duaLeiw2"
poweradmin_key="/etc/ssl/private/nanl_clients.key"
poweradmin_crt="/etc/ssl/certs/nanl_clients.crt"
poweradmin_ca="/etc/ssl/certs/nanl_services.ca"
args="-O - --quiet --load-cookies /tmp/dns_${monitoring_domain}.cookie"
test -z "${poweradmin_ca}" || args="$args --ca-certificate ${poweradmin_ca}"
test -z "${poweradmin_key}" || args="$args --private-key ${poweradmin_key}"
test -z "${poweradmin_crt}" || args="$args --certificate ${poweradmin_crt}"
output=$(wget $args --keep-session-cookies --save-cookies /tmp/dns_${monitoring_domain}.cookie --post-data "username=${poweradmin_username}&password=${poweradmin_password}&authenticate=+Go+" "${poweradmin_url}")
output=$(wget ${args} "${poweradmin_url}/list_zones.php")
id=$(echo "$output" | grep "${domain}" | grep -o -E "id=[[:digit:]]+" | head -n 1)
output=$(wget ${args} --post-data "record%5B253%5D%5Brid%5D=253&record%5B253%5D%5Bzid%5D=18&record%5B253%5D%5Bname%5D=${test_domain}&record%5B253%5D%5Btype%5D=TXT&record%5B253%5D%5Bcontent%5D=${rand}&record%5B253%5D%5Bprio%5D=0&record%5B253%5D%5Bttl%5D=1&commit=Commit+changes" ${poweradmin_url}/edit.php?${id})
sleep ${refresh_time_internal}
failed=
nameservers=$(nslookup -type=ns "${monitoring_domain}" | grep -E -io '(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])\.$')
for ns in $nameservers; do
res="$(nslookup -type=txt "${test_domain}" "${ns}" | grep -E -o '[[:alnum:]]{20}')"
if [ "$res" != "$rand" ]; then
echo "${ns} failed ($rand (local) != $res (server))"
failed=1
fi
done
test -z $failed || exit 1
sleep ${refresh_time_external}
res="$(nslookup -type=txt ${test_domain} ${ns} | grep -E -o '[[:alnum:]]{20}')"
if [ "$res" != "$rand" ]; then
echo "propagation failed"
exit 2
fi
exit 0