You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.4 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_easydns_info='easyDNS.net
  4. Site: easyDNS.net
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_easydns
  6. Options:
  7. EASYDNS_Token API Token
  8. EASYDNS_Key API Key
  9. Issues: github.com/acmesh-official/acme.sh/issues/2647
  10. Author: Neilpang, wurzelpanzer <wurzelpanzer@maximolider.net>
  11. '
  12. # API Documentation: https://sandbox.rest.easydns.net:3001/
  13. #################### Public functions #################
  14. #EASYDNS_Key="xxxxxxxxxxxxxxxxxxxxxxxx"
  15. #EASYDNS_Token="xxxxxxxxxxxxxxxxxxxxxxxx"
  16. EASYDNS_Api="https://rest.easydns.net"
  17. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  18. dns_easydns_add() {
  19. fulldomain=$1
  20. txtvalue=$2
  21. EASYDNS_Token="${EASYDNS_Token:-$(_readaccountconf_mutable EASYDNS_Token)}"
  22. EASYDNS_Key="${EASYDNS_Key:-$(_readaccountconf_mutable EASYDNS_Key)}"
  23. if [ -z "$EASYDNS_Token" ] || [ -z "$EASYDNS_Key" ]; then
  24. _err "You didn't specify an easydns.net token or api key. Signup at https://cp.easydns.com/manage/security/api/signup.php"
  25. return 1
  26. else
  27. _saveaccountconf_mutable EASYDNS_Token "$EASYDNS_Token"
  28. _saveaccountconf_mutable EASYDNS_Key "$EASYDNS_Key"
  29. fi
  30. _debug "First detect the root zone"
  31. if ! _get_root "$fulldomain"; then
  32. _err "invalid domain"
  33. return 1
  34. fi
  35. _debug _sub_domain "$_sub_domain"
  36. _debug _domain "$_domain"
  37. _debug "Getting txt records"
  38. _EASYDNS_rest GET "zones/records/all/${_domain}/search/${_sub_domain}"
  39. if ! printf "%s" "$response" | grep \"status\":200 >/dev/null; then
  40. _err "Error"
  41. return 1
  42. fi
  43. _info "Adding record"
  44. if _EASYDNS_rest PUT "zones/records/add/$_domain/TXT" "{\"host\":\"$_sub_domain\",\"rdata\":\"$txtvalue\"}"; then
  45. if _contains "$response" "\"status\":201"; then
  46. _info "Added, OK"
  47. return 0
  48. elif _contains "$response" "Record already exists"; then
  49. _info "Already exists, OK"
  50. return 0
  51. else
  52. _err "Add txt record error."
  53. return 1
  54. fi
  55. fi
  56. _err "Add txt record error."
  57. return 1
  58. }
  59. dns_easydns_rm() {
  60. fulldomain=$1
  61. txtvalue=$2
  62. EASYDNS_Token="${EASYDNS_Token:-$(_readaccountconf_mutable EASYDNS_Token)}"
  63. EASYDNS_Key="${EASYDNS_Key:-$(_readaccountconf_mutable EASYDNS_Key)}"
  64. _debug "First detect the root zone"
  65. if ! _get_root "$fulldomain"; then
  66. _err "invalid domain"
  67. return 1
  68. fi
  69. _debug _sub_domain "$_sub_domain"
  70. _debug _domain "$_domain"
  71. _debug "Getting txt records"
  72. _EASYDNS_rest GET "zones/records/all/${_domain}/search/${_sub_domain}"
  73. if ! printf "%s" "$response" | grep \"status\":200 >/dev/null; then
  74. _err "Error"
  75. return 1
  76. fi
  77. count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
  78. _debug count "$count"
  79. if [ "$count" = "0" ]; then
  80. _info "Don't need to remove."
  81. else
  82. record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | head -n 1)
  83. _debug "record_id" "$record_id"
  84. if [ -z "$record_id" ]; then
  85. _err "Can not get record id to remove."
  86. return 1
  87. fi
  88. if ! _EASYDNS_rest DELETE "zones/records/$_domain/$record_id"; then
  89. _err "Delete record error."
  90. return 1
  91. fi
  92. _contains "$response" "\"status\":200"
  93. fi
  94. }
  95. #################### Private functions below ##################################
  96. #_acme-challenge.www.domain.com
  97. #returns
  98. # _sub_domain=_acme-challenge.www
  99. # _domain=domain.com
  100. _get_root() {
  101. domain=$1
  102. i=1
  103. p=1
  104. while true; do
  105. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  106. _debug h "$h"
  107. if [ -z "$h" ]; then
  108. #not valid
  109. return 1
  110. fi
  111. if ! _EASYDNS_rest GET "zones/records/all/$h"; then
  112. return 1
  113. fi
  114. if _contains "$response" "\"status\":200"; then
  115. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  116. _domain=$h
  117. return 0
  118. fi
  119. p=$i
  120. i=$(_math "$i" + 1)
  121. done
  122. return 1
  123. }
  124. _EASYDNS_rest() {
  125. m=$1
  126. ep="$2"
  127. data="$3"
  128. _debug "$ep"
  129. basicauth=$(printf "%s" "$EASYDNS_Token":"$EASYDNS_Key" | _base64)
  130. export _H1="accept: application/json"
  131. if [ "$basicauth" ]; then
  132. export _H2="Authorization: Basic $basicauth"
  133. fi
  134. if [ "$m" != "GET" ]; then
  135. export _H3="Content-Type: application/json"
  136. _debug data "$data"
  137. response="$(_post "$data" "$EASYDNS_Api/$ep" "" "$m")"
  138. else
  139. response="$(_get "$EASYDNS_Api/$ep")"
  140. fi
  141. if [ "$?" != "0" ]; then
  142. _err "error $ep"
  143. return 1
  144. fi
  145. _debug2 response "$response"
  146. return 0
  147. }