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.

145 lines
4.4 KiB

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