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.

149 lines
4.5 KiB

1 month ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_zoneedit_info='ZoneEdit.com
  4. Site: ZoneEdit.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_zoneedit
  6. Options:
  7. ZONEEDIT_ID ID
  8. ZONEEDIT_Token API Token
  9. Issues: github.com/acmesh-official/acme.sh/issues/6135
  10. '
  11. # https://github.com/blueslow/sslcertzoneedit
  12. ######## Public functions #####################
  13. # Usage: dns_zoneedit_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_zoneedit_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. _info "Using ZoneEdit"
  18. _debug fulldomain "$fulldomain"
  19. _debug txtvalue "$txtvalue"
  20. # Load the credentials from the account conf file
  21. ZONEEDIT_ID="${ZONEEDIT_ID:-$(_readaccountconf_mutable ZONEEDIT_ID)}"
  22. ZONEEDIT_Token="${ZONEEDIT_Token:-$(_readaccountconf_mutable ZONEEDIT_Token)}"
  23. if [ -z "$ZONEEDIT_ID" ] || [ -z "$ZONEEDIT_Token" ]; then
  24. ZONEEDIT_ID=""
  25. ZONEEDIT_Token=""
  26. _err "Please specify ZONEEDIT_ID and _Token."
  27. _err "Please export as ZONEEDIT_ID and ZONEEDIT_Token then try again."
  28. return 1
  29. fi
  30. # Save the credentials to the account conf file
  31. _saveaccountconf_mutable ZONEEDIT_ID "$ZONEEDIT_ID"
  32. _saveaccountconf_mutable ZONEEDIT_Token "$ZONEEDIT_Token"
  33. if _zoneedit_api "CREATE" "$fulldomain" "$txtvalue"; then
  34. _info "Added, OK"
  35. return 0
  36. else
  37. _err "Add txt record error."
  38. return 1
  39. fi
  40. }
  41. # Usage: dns_zoneedit_rm fulldomain txtvalue
  42. dns_zoneedit_rm() {
  43. fulldomain=$1
  44. txtvalue=$2
  45. _info "Using ZoneEdit"
  46. _debug fulldomain "$fulldomain"
  47. _debug txtvalue "$txtvalue"
  48. # Load the credentials from the account conf file
  49. ZONEEDIT_ID="${ZONEEDIT_ID:-$(_readaccountconf_mutable ZONEEDIT_ID)}"
  50. ZONEEDIT_Token="${ZONEEDIT_Token:-$(_readaccountconf_mutable ZONEEDIT_Token)}"
  51. if [ -z "$ZONEEDIT_ID" ] || [ -z "$ZONEEDIT_Token" ]; then
  52. ZONEEDIT_ID=""
  53. ZONEEDIT_Token=""
  54. _err "Please specify ZONEEDIT_ID and _Token."
  55. _err "Please export as ZONEEDIT_ID and ZONEEDIT_Token then try again."
  56. return 1
  57. fi
  58. if _zoneedit_api "DELETE" "$fulldomain" "$txtvalue"; then
  59. _info "Deleted, OK"
  60. return 0
  61. else
  62. _err "Delete txt record error."
  63. return 1
  64. fi
  65. }
  66. #################### Private functions below ##################################
  67. #Usage: _zoneedit_api <CREATE|DELETE> fulldomain txtvalue
  68. _zoneedit_api() {
  69. cmd=$1
  70. fulldomain=$2
  71. txtvalue=$3
  72. # Construct basic authorization header
  73. credentials=$(printf "%s:%s" "$ZONEEDIT_ID" "$ZONEEDIT_Token" | _base64)
  74. export _H1="Authorization: Basic ${credentials}"
  75. # Generate request URL
  76. case "$cmd" in
  77. "CREATE")
  78. # https://dynamic.zoneedit.com/txt-create.php?host=_acme-challenge.example.com&rdata=depE1VF_xshMm1IVY1Y56Kk9Zb_7jA2VFkP65WuNgu8W
  79. geturl="https://dynamic.zoneedit.com/txt-create.php?host=${fulldomain}&rdata=${txtvalue}"
  80. ;;
  81. "DELETE")
  82. # https://dynamic.zoneedit.com/txt-delete.php?host=_acme-challenge.example.com&rdata=depE1VF_xshMm1IVY1Y56Kk9Zb_7jA2VFkP65WuNgu8W
  83. geturl="https://dynamic.zoneedit.com/txt-delete.php?host=${fulldomain}&rdata=${txtvalue}"
  84. ze_sleep=2
  85. ;;
  86. *)
  87. _err "Unknown parameter : $cmd"
  88. return 1
  89. ;;
  90. esac
  91. # Execute request
  92. i=3 # Tries
  93. while [ "$i" -gt 0 ]; do
  94. i=$(_math "$i" - 1)
  95. if ! response=$(_get "$geturl"); then
  96. _err "_get() failed ($response)"
  97. return 1
  98. fi
  99. _debug2 response "$response"
  100. if _contains "$response" "SUCCESS.*200"; then
  101. # Sleep (when needed) to work around a Zonedit API bug
  102. # https://forum.zoneedit.com/threads/automating-changes-of-txt-records-in-dns.7394/page-2#post-23855
  103. if [ "$ze_sleep" ]; then _sleep "$ze_sleep"; fi
  104. return 0
  105. elif _contains "$response" "ERROR.*Minimum.*seconds"; then
  106. _info "ZoneEdit responded with a rate limit of..."
  107. ze_ratelimit=$(echo "$response" | sed -n 's/.*Minimum \([0-9]\+\) seconds.*/\1/p')
  108. if [ "$ze_ratelimit" ] && [ ! "$(echo "$ze_ratelimit" | tr -d '0-9')" ]; then
  109. _info "$ze_ratelimit seconds."
  110. else
  111. _err "$response"
  112. _err "not a number, or blank ($ze_ratelimit), API change?"
  113. unset ze_ratelimit
  114. fi
  115. else
  116. _err "$response"
  117. _err "Unknown response, API change?"
  118. fi
  119. # Retry
  120. if [ "$i" -lt 1 ]; then
  121. _err "Tries exceeded, giving up."
  122. return 1
  123. fi
  124. if [ "$ze_ratelimit" ]; then
  125. _info "Waiting $ze_ratelimit seconds..."
  126. _sleep "$ze_ratelimit"
  127. else
  128. _err "Going to retry after 10 seconds..."
  129. _sleep 10
  130. fi
  131. done
  132. return 1
  133. }