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