From 4bc0628358e48770e8bc56d44af93282dc6d6e47 Mon Sep 17 00:00:00 2001 From: baerengraben Date: Sat, 18 Nov 2023 00:30:47 +0100 Subject: [PATCH 1/7] Create dns_hosttech.sh Add hosttech API support --- dnsapi/dns_hosttech.sh | 135 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 dnsapi/dns_hosttech.sh diff --git a/dnsapi/dns_hosttech.sh b/dnsapi/dns_hosttech.sh new file mode 100644 index 00000000..85ed1d0a --- /dev/null +++ b/dnsapi/dns_hosttech.sh @@ -0,0 +1,135 @@ +#!/usr/bin/bash + +#Hosttech_Key="abcdefg" + +Hosttech_Api="https://api.ns1.hosttech.eu/api/user/v1" + +######## Public functions ##################### + +#Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" +dns_hosttech_add() { + fulldomain=$1 + txtvalue=$2 + + Hosttech_Key="${Hosttech_Key:-$(_readaccountconf_mutable Hosttech_Key)}" + if [ -z "$Hosttech_Key" ]; then + Hosttech_Key="" + _err "You didn't specify a Hosttech api key." + _err "You can get yours from https://www.myhosttech.eu/user/dns/api" + return 1 + fi + + #save the api key and email to the account conf file. + _saveaccountconf_mutable Hosttech_Key "$Hosttech_Key" + + _debug "First detect the root zone" + if ! _get_root "$fulldomain"; then + _err "invalid domain" + return 1 + fi + _debug _sub_domain "$_sub_domain" + _debug _domain "$_domain" + + _info "Adding record" + if _hosttech_rest POST "zones/$_domain/records" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"text\":\"$txtvalue\",\"ttl\":600}"; then + if _contains "$_response" "$_sub_domain"; then + _debug recordID "$(echo "$_response" | grep -o '"id":[^"]*' | grep -Po "\d+")" + + #save the created recordID to the account conf file, so we can read it back for deleting in dns_hosttech_rm. + _saveaccountconf recordID "$(echo "$_response" | grep -o '"id":[^"]*' | grep -Po "\d+")" + _info "Added, OK" + return 0 + else + _err "Add txt record error." + return 1 + fi + fi + _err "Add txt record error." + return 1 + +} + +#fulldomain txtvalue +dns_hosttech_rm() { + fulldomain=$1 + txtvalue=$2 + + Hosttech_Key="${Hosttech_Key:-$(_readaccountconf_mutable Hosttech_Key)}" + if [ -z "$Hosttech_Key" ]; then + Hosttech_Key="" + _err "You didn't specify a Hosttech api key." + _err "You can get yours from https://www.hosttech.nl/mijn_hosttech" + return 1 + fi + + _debug "First detect the root zone" + if ! _get_root "$fulldomain"; then + _err "invalid domain" + return 1 + fi + + _debug _sub_domain "$_sub_domain" + _debug _domain "$_domain" + + _debug "Removing txt record" + delRecordID="$(_readaccountconf "recordID")" + _hosttech_rest DELETE "zones/$_domain/records/$delRecordID" + + _clearaccountconf recordID +} + +#################### Private functions below ################################## +#_acme-challenge.www.domain.com +#returns +# _sub_domain=_acme-challenge.www +# _domain=domain.com +_get_root() { + domain=$1 + i=2 + p=1 + while true; do + _domain=$(printf "%s" "$domain" | cut -d . -f $i-100) + _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) + _debug _domain "$_domain" + if [ -z "$_domain" ]; then + #not valid + return 1 + fi + + if _hosttech_rest GET "zones?query=${_domain}"; then + if [ "$(echo "$_response" | grep -o '"name":"[^"]*' | cut -d'"' -f4)" = "${_domain}" ]; then + return 0 + fi + else + return 1 + fi + p=$i + i=$(_math "$i" + 1) + done + return 1 +} + +_hosttech_rest() { + m=$1 + ep="$2" + data="$3" + _debug "$ep" + + export _H1="Authorization: Bearer $Hosttech_Key" + export _H2="accept: application/json" + export _H3="Content-Type: application/json" + + _debug data "$data" + _response="$(_post "$data" "$Hosttech_Api/$ep" "" "$m")" + + _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")" + _debug "http response code $_code" + + if [ "$?" != "0" ]; then + _err "error $ep" + return 1 + fi + + _debug2 response "$_response" + return 0 +} From 912307ebb5293e08e55be8ee5fcd662789f344d5 Mon Sep 17 00:00:00 2001 From: baerengraben Date: Sat, 18 Nov 2023 00:43:56 +0100 Subject: [PATCH 2/7] Update dns_hosttech.sh --- dnsapi/dns_hosttech.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dnsapi/dns_hosttech.sh b/dnsapi/dns_hosttech.sh index 85ed1d0a..976db3c3 100644 --- a/dnsapi/dns_hosttech.sh +++ b/dnsapi/dns_hosttech.sh @@ -1,6 +1,6 @@ #!/usr/bin/bash -#Hosttech_Key="abcdefg" +#Hosttech_Key="abcdefghuhu" Hosttech_Api="https://api.ns1.hosttech.eu/api/user/v1" From aa560bd09785790dbeb711a077ef2a10d6294598 Mon Sep 17 00:00:00 2001 From: baerengraben Date: Sat, 18 Nov 2023 12:58:16 +0100 Subject: [PATCH 3/7] Update dns_hosttech.sh --- dnsapi/dns_hosttech.sh | 4 ---- 1 file changed, 4 deletions(-) diff --git a/dnsapi/dns_hosttech.sh b/dnsapi/dns_hosttech.sh index 976db3c3..bba8cfc8 100644 --- a/dnsapi/dns_hosttech.sh +++ b/dnsapi/dns_hosttech.sh @@ -1,7 +1,6 @@ #!/usr/bin/bash #Hosttech_Key="abcdefghuhu" - Hosttech_Api="https://api.ns1.hosttech.eu/api/user/v1" ######## Public functions ##################### @@ -34,8 +33,6 @@ dns_hosttech_add() { if _hosttech_rest POST "zones/$_domain/records" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"text\":\"$txtvalue\",\"ttl\":600}"; then if _contains "$_response" "$_sub_domain"; then _debug recordID "$(echo "$_response" | grep -o '"id":[^"]*' | grep -Po "\d+")" - - #save the created recordID to the account conf file, so we can read it back for deleting in dns_hosttech_rm. _saveaccountconf recordID "$(echo "$_response" | grep -o '"id":[^"]*' | grep -Po "\d+")" _info "Added, OK" return 0 @@ -46,7 +43,6 @@ dns_hosttech_add() { fi _err "Add txt record error." return 1 - } #fulldomain txtvalue From 91cb32eaeb337ac4ab123a7a07a6e3605321a7b2 Mon Sep 17 00:00:00 2001 From: baerengraben Date: Sat, 18 Nov 2023 13:15:24 +0100 Subject: [PATCH 4/7] Update dns_hosttech.sh --- dnsapi/dns_hosttech.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dnsapi/dns_hosttech.sh b/dnsapi/dns_hosttech.sh index bba8cfc8..10902c31 100644 --- a/dnsapi/dns_hosttech.sh +++ b/dnsapi/dns_hosttech.sh @@ -44,13 +44,12 @@ dns_hosttech_add() { _err "Add txt record error." return 1 } - -#fulldomain txtvalue + dns_hosttech_rm() { fulldomain=$1 txtvalue=$2 - Hosttech_Key="${Hosttech_Key:-$(_readaccountconf_mutable Hosttech_Key)}" + if [ -z "$Hosttech_Key" ]; then Hosttech_Key="" _err "You didn't specify a Hosttech api key." From 7c95ae4f967c02403e27081ac642b6f57bf6e6be Mon Sep 17 00:00:00 2001 From: baerengraben Date: Sat, 18 Nov 2023 13:22:06 +0100 Subject: [PATCH 5/7] Update dns_hosttech.sh --- dnsapi/dns_hosttech.sh | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/dnsapi/dns_hosttech.sh b/dnsapi/dns_hosttech.sh index 10902c31..069566b5 100644 --- a/dnsapi/dns_hosttech.sh +++ b/dnsapi/dns_hosttech.sh @@ -1,6 +1,7 @@ #!/usr/bin/bash -#Hosttech_Key="abcdefghuhu" +#Hosttech_Key="asdfasdfawefasdfawefasdafe" + Hosttech_Api="https://api.ns1.hosttech.eu/api/user/v1" ######## Public functions ##################### @@ -29,10 +30,12 @@ dns_hosttech_add() { _debug _sub_domain "$_sub_domain" _debug _domain "$_domain" - _info "Adding record" + _info "Adding record" if _hosttech_rest POST "zones/$_domain/records" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"text\":\"$txtvalue\",\"ttl\":600}"; then if _contains "$_response" "$_sub_domain"; then _debug recordID "$(echo "$_response" | grep -o '"id":[^"]*' | grep -Po "\d+")" + + #save the created recordID to the account conf file, so we can read it back for deleting in dns_hosttech_rm. _saveaccountconf recordID "$(echo "$_response" | grep -o '"id":[^"]*' | grep -Po "\d+")" _info "Added, OK" return 0 @@ -43,13 +46,15 @@ dns_hosttech_add() { fi _err "Add txt record error." return 1 + } +#fulldomain txtvalue dns_hosttech_rm() { fulldomain=$1 txtvalue=$2 + Hosttech_Key="${Hosttech_Key:-$(_readaccountconf_mutable Hosttech_Key)}" - if [ -z "$Hosttech_Key" ]; then Hosttech_Key="" _err "You didn't specify a Hosttech api key." From 49e391555f4edbd02886ee44379a6a70e1a2f80d Mon Sep 17 00:00:00 2001 From: baerengraben Date: Sat, 18 Nov 2023 14:01:31 +0100 Subject: [PATCH 7/7] Update dns_hosttech.sh --- dnsapi/dns_hosttech.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dnsapi/dns_hosttech.sh b/dnsapi/dns_hosttech.sh index 069566b5..05d9c898 100644 --- a/dnsapi/dns_hosttech.sh +++ b/dnsapi/dns_hosttech.sh @@ -14,7 +14,7 @@ dns_hosttech_add() { Hosttech_Key="${Hosttech_Key:-$(_readaccountconf_mutable Hosttech_Key)}" if [ -z "$Hosttech_Key" ]; then Hosttech_Key="" - _err "You didn't specify a Hosttech api key." + _err "You didn't specify a Hosttech api key" _err "You can get yours from https://www.myhosttech.eu/user/dns/api" return 1 fi