]> git.proxmox.com Git - mirror_acme.sh.git/blobdiff - dnsapi/dns_curanet.sh
Replace some functions.
[mirror_acme.sh.git] / dnsapi / dns_curanet.sh
index 92147bc78a427aeabc52aef2897be8596346e249..4b39f36508a3ec0be6e9aa0cf9a4043627b30f79 100644 (file)
@@ -3,6 +3,7 @@
 #Script to use with curanet.dk, scannet.dk, wannafind.dk, dandomain.dk DNS management.
 #Requires api credentials with scope: dns
 #Author: Peter L. Hansen <peter@r12.dk>
+#Version 1.0
 
 CURANET_REST_URL="https://api.curanet.dk/dns/v1/Domains"
 CURANET_AUTH_URL="https://apiauth.dk.team.blue/auth/realms/Curanet/protocol/openid-connect/token"
@@ -32,10 +33,16 @@ dns_curanet_add() {
   _saveaccountconf_mutable CURANET_AUTHCLIENTID "$CURANET_AUTHCLIENTID"
   _saveaccountconf_mutable CURANET_AUTHSECRET "$CURANET_AUTHSECRET"
 
-  gettoken
+  if ! _get_token; then
+    _err "Unable to get token"
+    return 1
+  fi
+
+  if ! _get_root "$fulldomain"; then
+    _err "Invalid domain"
+    return 1
+  fi
 
-  _get_root "$fulldomain"
-  
   export _H1="Content-Type: application/json-patch+json"
   export _H2="Accept: application/json"
   export _H3="Authorization: Bearer $CURANET_ACCESS_TOKEN"
@@ -43,7 +50,7 @@ dns_curanet_add() {
   response="$(_post "$data" "$CURANET_REST_URL/${_domain}/Records" "" "")"
 
   if _contains "$response" "$txtvalue"; then
-      _debug "TXT record added OK"
+    _debug "TXT record added OK"
   else
     _err "Unable to add TXT record"
     return 1
@@ -60,14 +67,20 @@ dns_curanet_rm() {
   _info "Using curanet"
   _debug fulldomain "$fulldomain"
   _debug txtvalue "$txtvalue"
-  
+
   CURANET_AUTHCLIENTID="${CURANET_AUTHCLIENTID:-$(_readaccountconf_mutable CURANET_AUTHCLIENTID)}"
   CURANET_AUTHSECRET="${CURANET_AUTHSECRET:-$(_readaccountconf_mutable CURANET_AUTHSECRET)}"
 
-  gettoken
+  if ! _get_token; then
+    _err "Unable to get token"
+    return 1
+  fi
+
+  if ! _get_root "$fulldomain"; then
+    _err "Invalid domain"
+    return 1
+  fi
 
-  _get_root "$fulldomain"
-  
   _debug "Getting current record list to identify TXT to delete"
 
   export _H1="Content-Type: application/json"
@@ -81,41 +94,46 @@ dns_curanet_rm() {
     return 1
   fi
 
-  recordid=$(echo "$response" | _egrep_o "{\"id\":[0-9]+,\"name\":\"$fulldomain\"" | _egrep_o "id\":[0-9]+" | cut -c 5-)
+  recordid=$(echo "$response" | _egrep_o "{\"id\":[0-9]+,\"name\":\"$fulldomain\",\"type\":\"TXT\",\"ttl\":60,\"priority\":0,\"data\":\"..$txtvalue" | _egrep_o "id\":[0-9]+" | cut -c 5-)
+
+  if [ -z "$recordid" ]; then
+    _err "Unable to get recordid"
+    _debug "regex {\"id\":[0-9]+,\"name\":\"$fulldomain\",\"type\":\"TXT\",\"ttl\":60,\"priority\":0,\"data\":\"..$txtvalue"
+    _debug "response $response"
+    return 1
+  fi
 
   _debug "Deleting recordID $recordid"
-  
   response="$(_post "" "$CURANET_REST_URL/${_domain}/Records/$recordid" "" "DELETE")"
-
-  return 0;
-  
+  return 0
 }
 
 ####################  Private functions below ##################################
 
-gettoken() {
-  
+_get_token() {
   response="$(_post "grant_type=client_credentials&client_id=$CURANET_AUTHCLIENTID&client_secret=$CURANET_AUTHSECRET&scope=dns" "$CURANET_AUTH_URL" "" "")"
-
   if ! _contains "$response" "access_token"; then
     _err "Unable get access token"
     return 1
   fi
-
   CURANET_ACCESS_TOKEN=$(echo "$response" | _egrep_o "\"access_token\":\"[^\"]+" | cut -c 17-)
 
-}
+  if [ -z "$CURANET_ACCESS_TOKEN" ]; then
+    _err "Unable to get token"
+    return 1
+  fi
+
+  return 0
 
+}
 
 #_acme-challenge.www.domain.com
 #returns
-# _sub_domain=_acme-challenge.www
 # _domain=domain.com
 # _domain_id=sdjkglgdfewsdfg
 _get_root() {
   domain=$1
   i=1
-  p=1
 
   while true; do
     h=$(printf "%s" "$domain" | cut -d . -f $i-100)
@@ -131,14 +149,11 @@ _get_root() {
     response="$(_get "$CURANET_REST_URL/$h/Records" "" "")"
 
     if [ ! "$(echo "$response" | _egrep_o "Entity not found")" ]; then
-      _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
       _domain=$h
       return 0
     fi
-    
-    p=$i
+
     i=$(_math "$i" + 1)
   done
   return 1
 }
-