]> git.proxmox.com Git - mirror_acme.sh.git/blobdiff - dnsapi/dns_netcup.sh
Merge pull request #3734 from acmesh-official/dev
[mirror_acme.sh.git] / dnsapi / dns_netcup.sh
index 7a8002a732245cb1f9421bfa1f628f34da3e1bee..776fa02d93c2d4fd196e243d3c259948dd11e02a 100644 (file)
@@ -1,6 +1,4 @@
 #!/usr/bin/env sh
-
-#Requirments: jq
 #developed by linux-insideDE
 
 NC_Apikey="${NC_Apikey:-$(_readaccountconf_mutable NC_Apikey)}"
@@ -10,137 +8,127 @@ end="https://ccp.netcup.net/run/webservice/servers/endpoint.php?JSON"
 client=""
 
 dns_netcup_add() {
-       login
-       if [ "$NC_Apikey" = "" ] || [ "$NC_Apipw" = "" ] || [ "$NC_CID" = "" ]; then
-               _err "No Credentials given"
-               return 1
-       fi
-       _saveaccountconf_mutable NC_Apikey  "$NC_Apikey"
-       _saveaccountconf_mutable NC_Apipw  "$NC_Apipw"
-       _saveaccountconf_mutable NC_CID  "$NC_CID"      
-       fulldomain=$1
-       txtvalue=$2
-       tld=""
-       domain=""
-       exit=0  
-       i=20    
-       while [ "$i" -gt 0 ];
-       do 
-               tmp=$(echo "$fulldomain" | cut -d'.' -f$i)              
-               if [ "$tmp" != "" ]; then
-                       if [ "$tld" = "" ]; then
-                               tld=$tmp                                                
-                       else
-                               domain=$tmp
-                               exit=$i
-                               break;
-                       fi
-               fi              
-               i=$((i - 1))
-       done    
-       inc=""
-       i=1     
-       while [ "$i" -lt "$exit" ];
-       do
-               if [ "$((exit-1))" = "$i" ]; then
-                       inc="$inc$i"
-                       break;
-               else
-                       if [ "$inc" = "" ]; then
-                               inc="$i,"
-                       else
-                               inc="$inc$i,"                   
-                       fi                      
-               fi      
-               i=$((i + 1))
-       done
-       
-       tmp=$(echo "$fulldomain" | cut -d'.' -f$inc)
-       msg=$(_post "{\"action\": \"updateDnsRecords\", \"param\": {\"apikey\": \"$NC_Apikey\", \"apisessionid\": \"$sid\", \"customernumber\": \"$NC_CID\",\"clientrequestid\": \"$client\" , \"domainname\": \"$domain.$tld\", \"dnsrecordset\": { \"dnsrecords\": [ {\"id\": \"\", \"hostname\": \"$tmp\", \"type\": \"TXT\", \"priority\": \"\", \"destination\": \"$txtvalue\", \"deleterecord\": \"false\", \"state\": \"yes\"} ]}}}" "$end" "" "POST")
-       _debug "$msg"
-       if [ "$(echo "$msg" | jq -r .status)" != "success" ]; then
-               _err "$msg"
-               return 1
-       fi
-       logout
+  _debug NC_Apikey "$NC_Apikey"
+  login
+  if [ "$NC_Apikey" = "" ] || [ "$NC_Apipw" = "" ] || [ "$NC_CID" = "" ]; then
+    _err "No Credentials given"
+    return 1
+  fi
+  _saveaccountconf_mutable NC_Apikey "$NC_Apikey"
+  _saveaccountconf_mutable NC_Apipw "$NC_Apipw"
+  _saveaccountconf_mutable NC_CID "$NC_CID"
+  fulldomain=$1
+  txtvalue=$2
+  domain=""
+  exit=$(echo "$fulldomain" | tr -dc '.' | wc -c)
+  exit=$(_math "$exit" + 1)
+  i=$exit
+
+  while
+    [ "$exit" -gt 0 ]
+  do
+    tmp=$(echo "$fulldomain" | cut -d'.' -f"$exit")
+    if [ "$(_math "$i" - "$exit")" -eq 0 ]; then
+      domain="$tmp"
+    else
+      domain="$tmp.$domain"
+    fi
+    if [ "$(_math "$i" - "$exit")" -ge 1 ]; then
+      msg=$(_post "{\"action\": \"updateDnsRecords\", \"param\": {\"apikey\": \"$NC_Apikey\", \"apisessionid\": \"$sid\", \"customernumber\": \"$NC_CID\",\"clientrequestid\": \"$client\" , \"domainname\": \"$domain\", \"dnsrecordset\": { \"dnsrecords\": [ {\"id\": \"\", \"hostname\": \"$fulldomain.\", \"type\": \"TXT\", \"priority\": \"\", \"destination\": \"$txtvalue\", \"deleterecord\": \"false\", \"state\": \"yes\"} ]}}}" "$end" "" "POST")
+      _debug "$msg"
+      if [ "$(_getfield "$msg" "5" | sed 's/"statuscode"://g')" != 5028 ]; then
+        if [ "$(_getfield "$msg" "4" | sed s/\"status\":\"//g | sed s/\"//g)" != "success" ]; then
+          _err "$msg"
+          return 1
+        else
+          break
+        fi
+      fi
+    fi
+    exit=$(_math "$exit" - 1)
+  done
+  logout
 }
 
 dns_netcup_rm() {
-       login
-       fulldomain=$1
-       txtvalue=$2
-       tld=""
-       domain=""
-       exit=0  
-       i=20
-       while [ "$i" -gt 0 ];
-       do
-               tmp=$(echo "$fulldomain" | cut -d'.' -f$i)              
-               if [ "$tmp" != "" ]; then
-                       if [ "$tld" = "" ]; then
-                               tld=$tmp                                                
-                       else
-                               domain=$tmp
-                               exit=$i
-                               break;
-                       fi
-               fi
-               i=$((i - 1))
-       done
-       inc=""  
-       i=1     
-       while [ "$i" -lt "$exit" ];
-       do
-               if [ "$((exit-1))" = "$i" ]; then
-                       inc="$inc$i"
-                       break;
-               else
-                       if [ "$inc" = "" ]; then
-                               inc="$i,"
-                       else
-                               inc="$inc$i,"
-                       fi
-               fi
-               i=$((i + 1))
-       done
-       tmp=$(echo "$fulldomain" | cut -d'.' -f$inc)    
-       doma="$domain.$tld"
-       rec=$(getRecords "$doma")
-       ids=$(echo "$rec" | jq -r ".[]|select(.destination==\"$txtvalue\")|.id")
-       msg=$(_post "{\"action\": \"updateDnsRecords\", \"param\": {\"apikey\": \"$NC_Apikey\", \"apisessionid\": \"$sid\", \"customernumber\": \"$NC_CID\",\"clientrequestid\": \"$client\" , \"domainname\": \"$doma\", \"dnsrecordset\": { \"dnsrecords\": [ {\"id\": \"$ids\", \"hostname\": \"$tmp\", \"type\": \"TXT\", \"priority\": \"\", \"destination\": \"$txtvalue\", \"deleterecord\": \"TRUE\", \"state\": \"yes\"} ]}}}" "$end" "" "POST")
-       _debug "$msg"
-       if [ "$(echo "$msg" | jq -r .status)" != "success" ]; then
-               _err "$msg"
-               return 1
-       fi
-       logout
+  login
+  fulldomain=$1
+  txtvalue=$2
+
+  domain=""
+  exit=$(echo "$fulldomain" | tr -dc '.' | wc -c)
+  exit=$(_math "$exit" + 1)
+  i=$exit
+  rec=""
+
+  while
+    [ "$exit" -gt 0 ]
+  do
+    tmp=$(echo "$fulldomain" | cut -d'.' -f"$exit")
+    if [ "$(_math "$i" - "$exit")" -eq 0 ]; then
+      domain="$tmp"
+    else
+      domain="$tmp.$domain"
+    fi
+    if [ "$(_math "$i" - "$exit")" -ge 1 ]; then
+      msg=$(_post "{\"action\": \"infoDnsRecords\", \"param\": {\"apikey\": \"$NC_Apikey\", \"apisessionid\": \"$sid\", \"customernumber\": \"$NC_CID\", \"domainname\": \"$domain\"}}" "$end" "" "POST")
+      rec=$(echo "$msg" | sed 's/\[//g' | sed 's/\]//g' | sed 's/{\"serverrequestid\".*\"dnsrecords\"://g' | sed 's/},{/};{/g' | sed 's/{//g' | sed 's/}//g')
+      _debug "$msg"
+      if [ "$(_getfield "$msg" "5" | sed 's/"statuscode"://g')" != 5028 ]; then
+        if [ "$(_getfield "$msg" "4" | sed s/\"status\":\"//g | sed s/\"//g)" != "success" ]; then
+          _err "$msg"
+          return 1
+        else
+          break
+        fi
+      fi
+    fi
+    exit=$(_math "$exit" - 1)
+  done
+
+  ida=0000
+  idv=0001
+  ids=0000000000
+  i=1
+  while
+    [ "$i" -ne 0 ]
+  do
+    specrec=$(_getfield "$rec" "$i" ";")
+    idv="$ida"
+    ida=$(_getfield "$specrec" "1" "," | sed 's/\"id\":\"//g' | sed 's/\"//g')
+    txtv=$(_getfield "$specrec" "5" "," | sed 's/\"destination\":\"//g' | sed 's/\"//g')
+    i=$(_math "$i" + 1)
+    if [ "$txtvalue" = "$txtv" ]; then
+      i=0
+      ids="$ida"
+    fi
+    if [ "$ida" = "$idv" ]; then
+      i=0
+    fi
+  done
+  msg=$(_post "{\"action\": \"updateDnsRecords\", \"param\": {\"apikey\": \"$NC_Apikey\", \"apisessionid\": \"$sid\", \"customernumber\": \"$NC_CID\",\"clientrequestid\": \"$client\" , \"domainname\": \"$domain\", \"dnsrecordset\": { \"dnsrecords\": [ {\"id\": \"$ids\", \"hostname\": \"$fulldomain.\", \"type\": \"TXT\", \"priority\": \"\", \"destination\": \"$txtvalue\", \"deleterecord\": \"TRUE\", \"state\": \"yes\"} ]}}}" "$end" "" "POST")
+  _debug "$msg"
+  if [ "$(_getfield "$msg" "4" | sed s/\"status\":\"//g | sed s/\"//g)" != "success" ]; then
+    _err "$msg"
+    return 1
+  fi
+  logout
 }
 
 login() {
-       tmp=$(_post "{\"action\": \"login\", \"param\": {\"apikey\": \"$NC_Apikey\", \"apipassword\": \"$NC_Apipw\", \"customernumber\": \"$NC_CID\"}}" "$end" "" "POST")
-       sid=$(echo "$tmp" | jq -r .responsedata.apisessionid)
-       _debug "$tmp"
-       if [ "$(echo "$tmp" | jq -r .status)" != "success" ]; then
-               _err "$tmp"
-               return 1
-       fi
+  tmp=$(_post "{\"action\": \"login\", \"param\": {\"apikey\": \"$NC_Apikey\", \"apipassword\": \"$NC_Apipw\", \"customernumber\": \"$NC_CID\"}}" "$end" "" "POST")
+  sid=$(echo "$tmp" | tr '{}' '\n' | grep apisessionid | cut -d '"' -f 4)
+  _debug "$tmp"
+  if [ "$(_getfield "$tmp" "4" | sed s/\"status\":\"//g | sed s/\"//g)" != "success" ]; then
+    _err "$tmp"
+    return 1
+  fi
 }
 logout() {
-       tmp=$(_post "{\"action\": \"logout\", \"param\": {\"apikey\": \"$NC_Apikey\", \"apisessionid\": \"$sid\", \"customernumber\": \"$NC_CID\"}}" "$end" "" "POST")
-       _debug "$tmp"
-       if [ "$(echo "$tmp" | jq -r .status)" != "success" ]; then
-               _err "$tmp"
-               return 1
-       fi
-}
-getRecords() { 
-       tmp2=$(_post "{\"action\": \"infoDnsRecords\", \"param\": {\"apikey\": \"$NC_Apikey\", \"apisessionid\": \"$sid\", \"customernumber\": \"$NC_CID\", \"domainname\": \"$1\"}}" "$end" "" "POST")
-       xxd=$(echo "$tmp2" | jq -r ".responsedata.dnsrecords" | tr '[' ' ' | tr ']' ' ')
-       xcd=$(echo "$xxd" | sed 's/}\s{/},{/g')
-       echo "[ $xcd ]"
-       _debug "$tmp2"
-       if [ "$(echo "$tmp2" | jq -r .status)" != "success" ]; then
-               _err "$tmp2"
-               return 1
-       fi
+  tmp=$(_post "{\"action\": \"logout\", \"param\": {\"apikey\": \"$NC_Apikey\", \"apisessionid\": \"$sid\", \"customernumber\": \"$NC_CID\"}}" "$end" "" "POST")
+  _debug "$tmp"
+  if [ "$(_getfield "$tmp" "4" | sed s/\"status\":\"//g | sed s/\"//g)" != "success" ]; then
+    _err "$tmp"
+    return 1
+  fi
 }