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.

157 lines
4.0 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_zone_info='Zone.eu
  4. Site: Zone.eu
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_zone
  6. Options:
  7. ZONE_Username Username
  8. ZONE_Key API Key
  9. Issues: github.com/acmesh-official/acme.sh/issues/2146
  10. '
  11. # Zone.ee dns API
  12. # https://help.zone.eu/kb/zoneid-api-v2/
  13. ZONE_Api="https://api.zone.eu/v2"
  14. ######## Public functions #####################
  15. #Usage: dns_zone_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  16. dns_zone_add() {
  17. fulldomain=$1
  18. txtvalue=$2
  19. _info "Using zone.ee dns api"
  20. _debug fulldomain "$fulldomain"
  21. _debug txtvalue "$txtvalue"
  22. ZONE_Username="${ZONE_Username:-$(_readaccountconf_mutable ZONE_Username)}"
  23. ZONE_Key="${ZONE_Key:-$(_readaccountconf_mutable ZONE_Key)}"
  24. if [ -z "$ZONE_Username" ] || [ -z "$ZONE_Key" ]; then
  25. ZONE_Username=""
  26. ZONE_Key=""
  27. _err "Zone api key and username must be present."
  28. return 1
  29. fi
  30. _saveaccountconf_mutable ZONE_Username "$ZONE_Username"
  31. _saveaccountconf_mutable ZONE_Key "$ZONE_Key"
  32. _debug "First detect the root zone"
  33. if ! _get_root "$fulldomain"; then
  34. _err "invalid domain"
  35. return 1
  36. fi
  37. _debug "Adding txt record"
  38. if _zone_rest POST "dns/${_domain}/txt" "{\"name\": \"$fulldomain\", \"destination\": \"$txtvalue\"}"; then
  39. if printf -- "%s" "$response" | grep "$fulldomain" >/dev/null; then
  40. _info "Added, OK"
  41. return 0
  42. else
  43. _err "Adding txt record error."
  44. return 1
  45. fi
  46. else
  47. _err "Adding txt record error."
  48. fi
  49. }
  50. #Usage: fulldomain txtvalue
  51. #Remove the txt record after validation.
  52. dns_zone_rm() {
  53. fulldomain=$1
  54. txtvalue=$2
  55. _info "Using zone.ee dns api"
  56. _debug fulldomain "$fulldomain"
  57. _debug txtvalue "$txtvalue"
  58. ZONE_Username="${ZONE_Username:-$(_readaccountconf_mutable ZONE_Username)}"
  59. ZONE_Key="${ZONE_Key:-$(_readaccountconf_mutable ZONE_Key)}"
  60. if [ -z "$ZONE_Username" ] || [ -z "$ZONE_Key" ]; then
  61. ZONE_Username=""
  62. ZONE_Key=""
  63. _err "Zone api key and username must be present."
  64. return 1
  65. fi
  66. _saveaccountconf_mutable ZONE_Username "$ZONE_Username"
  67. _saveaccountconf_mutable ZONE_Key "$ZONE_Key"
  68. _debug "First detect the root zone"
  69. if ! _get_root "$fulldomain"; then
  70. _err "invalid domain"
  71. return 1
  72. fi
  73. _debug "Getting txt records"
  74. _debug _domain "$_domain"
  75. _zone_rest GET "dns/${_domain}/txt"
  76. if printf "%s" "$response" | grep \"error\" >/dev/null; then
  77. _err "Error"
  78. return 1
  79. fi
  80. count=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$fulldomain\"" | wc -l)
  81. _debug count "$count"
  82. if [ "$count" = "0" ]; then
  83. _info "Nothing to remove."
  84. else
  85. record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\",\"resource_url\":\"[^\"]*\",\"name\":\"$fulldomain\"," | cut -d : -f2 | cut -d , -f1 | tr -d \" | _head_n 1)
  86. if [ -z "$record_id" ]; then
  87. _err "No id found to remove."
  88. return 1
  89. fi
  90. if ! _zone_rest DELETE "dns/${_domain}/txt/$record_id"; then
  91. _err "Record deleting error."
  92. return 1
  93. fi
  94. _info "Record deleted"
  95. return 0
  96. fi
  97. }
  98. #################### Private functions below ##################################
  99. _zone_rest() {
  100. m=$1
  101. ep="$2"
  102. data="$3"
  103. _debug "$ep"
  104. realm="$(printf "%s" "$ZONE_Username:$ZONE_Key" | _base64)"
  105. export _H1="Authorization: Basic $realm"
  106. export _H2="Content-Type: application/json"
  107. if [ "$m" != "GET" ]; then
  108. _debug data "$data"
  109. response="$(_post "$data" "$ZONE_Api/$ep" "" "$m")"
  110. else
  111. response="$(_get "$ZONE_Api/$ep")"
  112. fi
  113. if [ "$?" != "0" ]; then
  114. _err "error $ep"
  115. return 1
  116. fi
  117. _debug2 response "$response"
  118. return 0
  119. }
  120. _get_root() {
  121. domain=$1
  122. i=2
  123. while true; do
  124. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  125. _debug h "$h"
  126. if [ -z "$h" ]; then
  127. return 1
  128. fi
  129. if ! _zone_rest GET "dns/$h"; then
  130. return 1
  131. fi
  132. if _contains "$response" "\"identificator\":\"$h\"" >/dev/null; then
  133. _domain=$h
  134. return 0
  135. fi
  136. i=$(_math "$i" + 1)
  137. done
  138. return 0
  139. }