From 4efcb6ccf387a5eebc6103109037097b2f194de3 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Thu, 25 Mar 2021 10:41:11 +0100 Subject: [type/__dtnrch_getssl_script_henet] Add CNAME resolver using nslookup --- .../files/dns/nslookup.sh | 82 ++++++++++++---------- 1 file changed, 43 insertions(+), 39 deletions(-) (limited to 'type') diff --git a/type/__dtnrch_getssl_script_henet/files/dns/nslookup.sh b/type/__dtnrch_getssl_script_henet/files/dns/nslookup.sh index c475e9a..53dbbdf 100644 --- a/type/__dtnrch_getssl_script_henet/files/dns/nslookup.sh +++ b/type/__dtnrch_getssl_script_henet/files/dns/nslookup.sh @@ -1,46 +1,50 @@ -find_auth_dns() ( - domain=${1:?} +if expr "$(ls -l "$(command -v nslookup)")" : '.*-> .*busybox' >/dev/null +then + echo 'busybox nslookup is not supported.' >&2 + exit 1 +fi + +LF=' +' + +find_zone_and_ns() { + zone=${1:?} + zone=${zone%.} - until test -n "${ns-}" || test "${domain}" = "${domain##*.}" + while test -n "${zone}" do - ns=$( - nslookup -debug -type=NS "${domain}" \ - | awk '\ - /^Authoritative answer/ { block = "non-auth" } - /^Non-authoritative answer:$/ { block = "non-auth" } - - debug && /^[ ]{4}([A-Z][A-Z ]*):$/ { - debug_block = substr($0, 5, length - 6) - } - debug_block == "ANSWERS" && /nameserver = / { - key = "nameserver = " - origin = substr($0, index($0, key) + length(key)) - } - END { print origin }') - - domain=${domain#*.} + if ! nslookup -nosearch -class=IN -type=CNAME "${zone}" \ + | grep -q -F 'canonical name = ' + then + # Only if not a CNAME + name_servers=$( + nslookup -nosearch -class=IN -type=NS "${zone}" \ + | sed -n -e 's/^.*[[:space:]]nameserver = \(.*\)$/\1/p') + if test -n "${name_servers}" + then + break + fi + fi + + zone=$(expr "${zone}" : '[^.]\{1,\}\.\(.*\)$') done - test -n "${ns}" || return 2 - - # Check authority - echo $ns - origin=$( - nslookup -debug -type=SOA "$1" "${ns}" \ - | awk '\ - /^-+$/ { debug = !debug; debug_block = "" } - debug && /^[ ]{4}([A-Z][A-Z ]*):$/ { - debug_block = substr($0, 5, length - 6) - } - debug { print debug_block } - debug_block ~ /AUTHORITY RECORDS?/ && /origin = / { - key = "origin = " - print substr($0, index($0, key) + length(key)) - }') - - test -n "${origin}" && echo "${origin}" || return 1 -) + + test -n "${zone}" || return 1 + ns=${name_servers%%${LF}*} + ns=${ns%.} + printf '%s@%s\n' "${zone}" "${ns}" +} resolve_cnames() ( domain=${1:?} - auth_dns + domain=${domain%.} + + zone_and_ns=$(find_zone_and_ns "${domain}") + ns=${zone_and_ns#*@} + + cname=$( + nslookup -nosearch -norecurse -class=IN -type=CNAME "${domain}" "${ns}" \ + | sed -n -e 's/^.*[[:space:]]canonical name = \(.*\)$/\1/p') + + test -n "${cname}" && resolve_cnames "${cname}" || echo "${domain}" ) -- cgit v1.2.3