From 8eca7f1a12a16c582c7df17c7d0386250459a9c6 Mon Sep 17 00:00:00 2001 From: Ruud de Jong Date: Thu, 30 Apr 2020 21:44:33 +0200 Subject: [PATCH] Adding initial TransIP support --- dnsapi/dns_transip.sh | 109 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 dnsapi/dns_transip.sh diff --git a/dnsapi/dns_transip.sh b/dnsapi/dns_transip.sh new file mode 100644 index 00000000..e4cf571b --- /dev/null +++ b/dnsapi/dns_transip.sh @@ -0,0 +1,109 @@ +#!/usr/bin/env sh + +TRANSIP_Token="ey..." +TRANSIP_DomainName="domain.com" + +TRANSIP_Api="https://api.transip.nl/v6" + +######## Public functions ##################### + +#Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" +dns_transip_add() { + fulldomain=$1 + txtvalue=$2 + + TRANSIP_Token="${TRANSIP_Token:-$(_readaccountconf_mutable TRANSIP_Token)}" + TRANSIP_DomainName="${TRANSIP_DomainName:-$(_readaccountconf_mutable TRANSIP_DomainName)}" + + if [ "$TRANSIP_Token" ]; then + _saveaccountconf_mutable TRANSIP_Token "$TRANSIP_Token" + else + _err "You didn't specify a TransIP api access token yet." + return 1 + fi + if [ "$TRANSIP_DomainName" ]; then + _saveaccountconf_mutable TRANSIP_DomainName "$TRANSIP_DomainName" + else + _err "You didn't specify a TransIP domainname yet." + return 1 + fi + + _debug "First detect the root zone" + if ! _transip_get_root "$fulldomain"; then + _err "invalid domain" + return 1 + fi + + _debug _domain "$_domain" + + _info "Adding record" + if _transip_rest POST "domains/$TRANSIP_DomainName/dns" "{\"dnsEntry\": {\"name\": \"$_sub_domain\",\"expire\": 300,\"type\": \"TXT\", \"content\": \"$txtvalue\"}}"; then + _info "Added, OK" + return 0 + fi + _err "Add txt record error." + return 1 + +} + +#fulldomain txtvalue +dns_transip_rm() { + fulldomain=$1 + txtvalue=$2 + + TRANSIP_Token="${TRANSIP_Token:-$(_readaccountconf_mutable TRANSIP_Token)}" + TRANSIP_DomainName="${TRANSIP_DomainName:-$(_readaccountconf_mutable TRANSIP_DomainName)}" + + _debug "First detect the root zone" + if ! _transip_get_root "$fulldomain"; then + _err "invalid domain" + return 1 + fi + + _debug _domain "$_domain" + + if _transip_rest DELETE "domains/$TRANSIP_DomainName/dns" "{\"dnsEntry\": {\"name\": \"$_sub_domain\",\"expire\": 300,\"type\": \"TXT\", \"content\": \"$txtvalue\"}}"; then + _info "Removed, OK" + return 0 + fi + +} + +#################### Private functions below ################################## +#_acme-challenge.www.domain.com +#returns +# _sub_domain=_acme-challenge.www + +_transip_get_root() { + domain=$1 + _cutlength=$((${#domain} - ${#TRANSIP_DomainName} - 1)) + _sub_domain=$(printf "%s" "$domain" | cut -c "1-$_cutlength") + _debug _sub_domain "$_sub_domain" + return 0 +} + +_transip_rest() { + m=$1 + ep="$2" + data="$3" + _debug "$ep" + + token_trimmed=$(echo "$TRANSIP_Token" | tr -d '"') + + export _H1="Content-Type: application/json" + export _H2="Authorization: Bearer $token_trimmed" + + if [ "$m" != "GET" ]; then + _debug data "$data" + response="$(_post "$data" "$TRANSIP_Api/$ep" "" "$m")" + else + response="$(_get "$TRANSIP_Api/$ep")" + fi + + if [ "$?" != "0" ]; then + _err "error $ep" + return 1 + fi + _debug2 response "$response" + return 0 +}