From 5b6e31a04dcadab3fd87327837eebc66992b1e97 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Thu, 10 Sep 2015 00:29:00 +0100 Subject: [PATCH] bin/update: Check current state before submitting an update. Seems like this might reduce the work done by nameservers if they're not very bright. --- bin/update | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/bin/update b/bin/update index 60cbea0..5ff17fe 100755 --- a/bin/update +++ b/bin/update @@ -28,6 +28,25 @@ EOF getarg='case $# in 0) fail_usage ;; esac; arg=$1; shift' doneargs='case $# in 0) ;; *) fail_usage ;; esac' +check_current_state () { + ck_rrty=$1 ck_name=$2 + set -- $(adnshost --config "nameserver $DYNDNS_SERVER" \ + -Fi -Tt -t$ck_rrty "$ck_name") + case $1,$2,$3,$5 in + ";,failed,permfail,nxdomain" | ";,failed,permfail,nodata") + err=$5 + ;; + ";,failed,"*) + shift 8 + echo >&2 "$0: lookup $ck_name ($ck_rrty) failed: $*" + exit 4 + ;; + *) + err=nil cur_ttl=$2 cur_addr=$4 + ;; + esac +} + checkhost () { host=$1 @@ -47,13 +66,21 @@ checkhost () { doupdate () { cmd=$1 - -nsupdate -k "$DYNDNS_KEY" <&2 "$0: failed to parse new address"; exit 2 ;; esac name=$host.$DYNDNS_ZONE + check_current_state $(echo $rrtype | tr A-Z a-z) "$name" + ttl=${DYNDNS_TTL-14400} + case $err,$cur_ttl,$cur_addr in + nil,$ttl,$addr) echo >&2 "$0: nothing to do"; exit 0 ;; + esac doupdate " update delete $name IN $rrtype - update add $name ${DYNDNS_TTL-14400} IN $rrtype $addr" + update add $name $ttl IN $rrtype $addr" ;; unset) eval $getarg; host=$arg eval $doneargs checkhost "$host" name=$host.$DYNDNS_ZONE + check_current_state a "$name" + case $err in + nxdomain) echo >&2 "$0: nothing to do"; exit 0 ;; + esac doupdate "update delete $name IN" ;; *) -- 2.11.0