summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Camera <dennis.camera@ssrq-sds-fds.ch>2021-03-25 10:41:11 +0100
committerDennis Camera <dennis.camera@ssrq-sds-fds.ch>2021-03-25 10:41:11 +0100
commit4efcb6ccf387a5eebc6103109037097b2f194de3 (patch)
treeaa71be0e6ddd9338e81fd3519cbed70a3b05695c
parent06bc944b3628a0bd9c2bfe8029330bc1726a2766 (diff)
download__dtnrch_getssl-4efcb6ccf387a5eebc6103109037097b2f194de3.tar.gz
__dtnrch_getssl-4efcb6ccf387a5eebc6103109037097b2f194de3.zip
[type/__dtnrch_getssl_script_henet] Add CNAME resolver using nslookup
-rw-r--r--type/__dtnrch_getssl_script_henet/files/dns/nslookup.sh82
1 files changed, 43 insertions, 39 deletions
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}"
)