Fix for multiple TLDs in DNS API

This commit is contained in:
Bjarke Bruun 2022-09-07 03:46:28 +02:00
parent 9fb14eef0e
commit 5c00afc6fe

View File

@ -13,8 +13,8 @@ DNSServices_API=https://dns.services/api
#Usage: dns_dnsservices_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" #Usage: dns_dnsservices_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
dns_dnsservices_add() { dns_dnsservices_add() {
fulldomain=$1 fulldomain="$1"
txtvalue=$2 txtvalue="$2"
_info "Using dns.services to create ACME DNS challenge" _info "Using dns.services to create ACME DNS challenge"
_debug2 add_fulldomain "$fulldomain" _debug2 add_fulldomain "$fulldomain"
@ -61,8 +61,8 @@ dns_dnsservices_add() {
#Usage: fulldomain txtvalue #Usage: fulldomain txtvalue
#Description: Remove the txt record after validation. #Description: Remove the txt record after validation.
dns_dnsservices_rm() { dns_dnsservices_rm() {
fulldomain=$1 fulldomain="$1"
txtvalue=$2 txtvalue="$2"
_info "Using dns.services to remove DNS record $fulldomain TXT $txtvalue" _info "Using dns.services to remove DNS record $fulldomain TXT $txtvalue"
_debug rm_fulldomain "$fulldomain" _debug rm_fulldomain "$fulldomain"
@ -117,36 +117,40 @@ _setup_headers() {
} }
_get_root() { _get_root() {
domain=$1 domain="$1"
_debug2 _get_root "Get the root domain of ${domain} for DNS API" _debug2 _get_root "Get the root domain of ${domain} for DNS API"
# Setup _get() and _post() headers # Setup _get() and _post() headers
#_setup_headers #_setup_headers
result=$(_H1="$_H1" _H2="$_H2" _get "$DNSServices_API/dns") result=$(_H1="$_H1" _H2="$_H2" _get "$DNSServices_API/dns")
result2="$(printf "%s\n" "$result" | tr '[' '\n' | grep '"name"')"
result3="$(printf "%s\n" "$result2" | tr '}' '\n' | grep '"name"' | sed "s,^\,,,g" | sed "s,$,},g")"
useResult=""
_debug2 _get_root "Got the following root domain(s) $result" _debug2 _get_root "Got the following root domain(s) $result"
_debug2 _get_root "- JSON: $result" _debug2 _get_root "- JSON: $result"
if [ "$(echo "$result" | grep -c '"name"')" -gt "1" ]; then if [ "$(printf "%s\n" "$result" | tr '}' '\n' | grep -c '"name"')" -gt "1" ]; then
checkMultiZones="true" checkMultiZones="true"
_debug2 _get_root "- multiple zones found" _debug2 _get_root "- multiple zones found"
else else
checkMultiZones="false" checkMultiZones="false"
_debug2 _get_root "- single zone found"
fi fi
# Find/isolate the root zone to work with in createRecord() and deleteRecord() # Find/isolate the root zone to work with in createRecord() and deleteRecord()
rootZone="" rootZone=""
if [ "$checkMultiZones" = "true" ]; then if [ "$checkMultiZones" = "true" ]; then
rootZone=$(for zone in $(echo "$result" | tr -d '\n' ' '); do #rootZone=$(for x in $(printf "%s" "${result3}" | tr ',' '\n' | sed -n 's/.*"name":"\(.*\)",.*/\1/p'); do if [ "$(echo "$domain" | grep "$x")" != "" ]; then echo "$x"; fi; done)
if [ "$(echo "$domain" | grep "$zone")" != "" ]; then rootZone=$(for x in $(printf "%s\n" "${result3}" | tr ',' '\n' | grep name | cut -d'"' -f4); do if [ "$(echo "$domain" | grep "$x")" != "" ]; then echo "$x"; fi; done)
_debug2 _get_root "- trying to figure out if $zone is in $domain" if [ "$rootZone" != "" ]; then
echo "$zone" _debug2 _rootZone "- root zone for $domain is $rootZone"
break
fi
done)
else else
rootZone=$(echo "$result" | _egrep_o '"name":"[^"]*' | cut -d'"' -f4) _err "Could not find root zone for $domain, is it correctly typed?"
return 1
fi
else
rootZone=$(echo "$result" | tr '}' '\n' | _egrep_o '"name":"[^"]*' | cut -d'"' -f4)
_debug2 _get_root "- only found 1 domain in API: $rootZone" _debug2 _get_root "- only found 1 domain in API: $rootZone"
fi fi
@ -155,14 +159,18 @@ _get_root() {
return 1 return 1
fi fi
# Make sure we use the correct API zone data
useResult="$(printf "%s\n" "${result3}" tr ',' '\n' | grep "$rootZone")"
_debug2 _useResult "useResult=$useResult"
# Setup variables used by other functions to communicate with DNS.Services API # Setup variables used by other functions to communicate with DNS.Services API
#zoneInfo=$(echo "$result" | sed "s,\"zones,\n&,g" | grep zones | cut -d'[' -f2 | cut -d']' -f1 | tr '}' '\n' | grep "\"$rootZone\"") #zoneInfo=$(printf "%s\n" "$useResult" | sed -E 's,.*(zones)(.*),\1\2,g' | sed -E 's,^(.*"name":")([^"]*)"(.*)$,\2,g')
zoneInfo=$(echo "$result" | sed -E 's,.*(zones)(.*),\1\2,g' | sed -E 's,^(.*"name":")([^"]*)"(.*)$,\2,g' | grep "\"$rootZone\"") zoneInfo=$(printf "%s\n" "$useResult" | tr ',' '\n' | grep '"name"' | cut -d'"' -f4)
rootZoneName="$rootZone" rootZoneName="$rootZone"
subDomainName="$(echo "$domain" | sed "s,\.$rootZone,,g")" subDomainName="$(printf "%s\n" "$domain" | sed "s,\.$rootZone,,g")"
subDomainNameClean="$(echo "$domain" | sed "s,_acme-challenge.,,g")" subDomainNameClean="$(printf "%s\n" "$domain" | sed "s,_acme-challenge.,,g")"
rootZoneDomainID=$(echo "$result" | sed -E 's,.*(zones)(.*),\1\2,g' | sed -E 's,^(.*"domain_id":")([^"]*)"(.*)$,\2,g') rootZoneDomainID=$(printf "%s\n" "$useResult" | tr ',' '\n' | grep domain_id | cut -d'"' -f4)
rootZoneServiceID=$(echo "$result" | sed -E 's,.*(zones)(.*),\1\2,g' | sed -E 's,^(.*"service_id":")([^"]*)"(.*)$,\2,g') rootZoneServiceID=$(printf "%s\n" "$useResult" | tr ',' '\n' | grep service_id | cut -d'"' -f4)
_debug2 _zoneInfo "Zone info from API : $zoneInfo" _debug2 _zoneInfo "Zone info from API : $zoneInfo"
_debug2 _get_root "Root zone name : $rootZoneName" _debug2 _get_root "Root zone name : $rootZoneName"
@ -175,13 +183,17 @@ _get_root() {
} }
createRecord() { createRecord() {
fulldomain=$1 fulldomain="$1"
txtvalue="$2" txtvalue="$2"
# Get root domain information - needed for DNS.Services API communication # Get root domain information - needed for DNS.Services API communication
if [ -z "$rootZoneName" ] || [ -z "$rootZoneDomainID" ] || [ -z "$rootZoneServiceID" ]; then if [ -z "$rootZoneName" ] || [ -z "$rootZoneDomainID" ] || [ -z "$rootZoneServiceID" ]; then
_get_root "$fulldomain" _get_root "$fulldomain"
fi fi
if [ -z "$rootZoneName" ] || [ -z "$rootZoneDomainID" ] || [ -z "$rootZoneServiceID" ]; then
_err "Something happend - could not get the API zone information"
return 1
fi
_debug2 createRecord "CNAME TXT value is: $txtvalue" _debug2 createRecord "CNAME TXT value is: $txtvalue"
@ -203,8 +215,8 @@ createRecord() {
} }
deleteRecord() { deleteRecord() {
fulldomain=$1 fulldomain="$1"
txtvalue=$2 txtvalue="$2"
_log deleteRecord "Deleting $fulldomain TXT $txtvalue record" _log deleteRecord "Deleting $fulldomain TXT $txtvalue record"
@ -213,8 +225,10 @@ deleteRecord() {
fi fi
result="$(_H1="$_H1" _H2="$_H2" _get "$DNSServices_API/service/$rootZoneServiceID/dns/$rootZoneDomainID")" result="$(_H1="$_H1" _H2="$_H2" _get "$DNSServices_API/service/$rootZoneServiceID/dns/$rootZoneDomainID")"
recordInfo="$(echo "$result" | sed -e 's/:{/:{\n/g' -e 's/},/\n},\n/g' | grep "${txtvalue}")" #recordInfo="$(echo "$result" | sed -e 's/:{/:{\n/g' -e 's/},/\n},\n/g' | grep "${txtvalue}")"
recordID="$(echo "$recordInfo" | sed -e 's/:{/:{\n/g' -e 's/},/\n},\n/g' | grep "${txtvalue}" | sed -E 's,.*(zones)(.*),\1\2,g' | sed -E 's,^(.*"id":")([^"]*)"(.*)$,\2,g')" #recordID="$(echo "$recordInfo" | sed -e 's/:{/:{\n/g' -e 's/},/\n},\n/g' | grep "${txtvalue}" | sed -E 's,.*(zones)(.*),\1\2,g' | sed -E 's,^(.*"id":")([^"]*)"(.*)$,\2,g')"
recordID="$(printf "%s\n" "$result" | tr '}' '\n' | grep -- "$txtvalue" | tr ',' '\n' | grep '"id"' | cut -d'"' -f4)"
_debug2 _recordID "recordID used for deletion of record: $recordID"
if [ -z "$recordID" ]; then if [ -z "$recordID" ]; then
_info "Record $fulldomain TXT $txtvalue not found or already deleted" _info "Record $fulldomain TXT $txtvalue not found or already deleted"