205 lines
5.7 KiB

5 months ago
5 months ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_active24_info='Active24.cz
  4. Site: Active24.cz
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_active24
  6. Options:
  7. Active24_ApiKey API Key. Called "Identifier" in the Active24 Admin
  8. Active24_ApiSecret API Secret. Called "Secret key" in the Active24 Admin
  9. Issues: github.com/acmesh-official/acme.sh/issues/2059
  10. '
  11. Active24_Api="https://rest.active24.cz"
  12. # export Active24_ApiKey=ak48l3h7-ak5d-qn4t-p8gc-b6fs8c3l
  13. # export Active24_ApiSecret=ajvkeo3y82ndsu2smvxy3o36496dcascksldncsq
  14. # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  15. # Used to add txt record
  16. dns_active24_add() {
  17. fulldomain=$1
  18. txtvalue=$2
  19. _active24_init
  20. _info "Adding txt record"
  21. if _active24_rest POST "/v2/service/$_service_id/dns/record" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":300}"; then
  22. if _contains "$response" "error"; then
  23. _err "Add txt record error."
  24. return 1
  25. else
  26. _info "Added, OK"
  27. return 0
  28. fi
  29. fi
  30. _err "Add txt record error."
  31. return 1
  32. }
  33. # Usage: fulldomain txtvalue
  34. # Used to remove the txt record after validation
  35. dns_active24_rm() {
  36. fulldomain=$1
  37. txtvalue=$2
  38. _active24_init
  39. _debug "Getting txt records"
  40. # The API needs to send data in body in order the filter to work
  41. _active24_rest GET "/v2/service/$_service_id/dns/record" "{\"page\":1,\"descending\":true,\"sortBy\":\"name\",\"rowsPerPage\":100,\"totalRecords\":0,\"filters\":{\"type\":[\"TXT\"],\"name\":\"${_sub_domain}\"}}"
  42. #_active24_rest GET "/v2/service/$_service_id/dns/record?rowsPerPage=100"
  43. if _contains "$response" "error"; then
  44. _err "Error"
  45. return 1
  46. fi
  47. # Note: it might never be more than one record actually, NEEDS more INVESTIGATION
  48. record_ids=$(printf "%s" "$response" | _egrep_o "[^{]+${txtvalue}[^}]+" | _egrep_o '"id" *: *[^,]+' | cut -d ':' -f 2)
  49. _debug2 record_ids "$record_ids"
  50. for redord_id in $record_ids; do
  51. _debug "Removing record_id" "$redord_id"
  52. _debug "txtvalue" "$txtvalue"
  53. if _active24_rest DELETE "/v2/service/$_service_id/dns/record/$redord_id" ""; then
  54. if _contains "$response" "error"; then
  55. _err "Unable to remove txt record."
  56. return 1
  57. else
  58. _info "Removed txt record."
  59. return 0
  60. fi
  61. fi
  62. done
  63. _err "No txt records found."
  64. return 1
  65. }
  66. _get_root() {
  67. domain=$1
  68. i=1
  69. p=1
  70. if ! _active24_rest GET "/v1/user/self/service"; then
  71. return 1
  72. fi
  73. while true; do
  74. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  75. _debug "h" "$h"
  76. if [ -z "$h" ]; then
  77. #not valid
  78. return 1
  79. fi
  80. if _contains "$response" "\"$h\"" >/dev/null; then
  81. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  82. _domain=$h
  83. return 0
  84. fi
  85. p=$i
  86. i=$(_math "$i" + 1)
  87. done
  88. return 1
  89. }
  90. _active24_init() {
  91. Active24_ApiKey="${Active24_ApiKey:-$(_readaccountconf_mutable Active24_ApiKey)}"
  92. Active24_ApiSecret="${Active24_ApiSecret:-$(_readaccountconf_mutable Active24_ApiSecret)}"
  93. #Active24_ServiceId="${Active24_ServiceId:-$(_readaccountconf_mutable Active24_ServiceId)}"
  94. if [ -z "$Active24_ApiKey" ] || [ -z "$Active24_ApiSecret" ]; then
  95. Active24_ApiKey=""
  96. Active24_ApiSecret=""
  97. _err "You don't specify Active24 api key and ApiSecret yet."
  98. _err "Please create your key and try again."
  99. return 1
  100. fi
  101. #save the credentials to the account conf file.
  102. _saveaccountconf_mutable Active24_ApiKey "$Active24_ApiKey"
  103. _saveaccountconf_mutable Active24_ApiSecret "$Active24_ApiSecret"
  104. _debug "A24 API CHECK"
  105. if ! _active24_rest GET "/v2/check"; then
  106. _err "A24 API check failed with: $response"
  107. return 1
  108. fi
  109. if ! echo "$response" | tr -d " " | grep \"verified\":true >/dev/null; then
  110. _err "A24 API check failed with: $response"
  111. return 1
  112. fi
  113. _debug "First detect the root zone"
  114. if ! _get_root "$fulldomain"; then
  115. _err "invalid domain"
  116. return 1
  117. fi
  118. _debug _sub_domain "$_sub_domain"
  119. _debug _domain "$_domain"
  120. _active24_get_service_id "$_domain"
  121. _debug _service_id "$_service_id"
  122. }
  123. _active24_get_service_id() {
  124. _d=$1
  125. if ! _active24_rest GET "/v1/user/self/zone/${_d}"; then
  126. return 1
  127. else
  128. response=$(echo "$response" | _json_decode)
  129. _service_id=$(echo "$response" | _egrep_o '"id" *: *[^,]+' | cut -d ':' -f 2)
  130. fi
  131. }
  132. _active24_rest() {
  133. m=$1
  134. ep_qs=$2 # with query string
  135. # ep=$2
  136. ep=$(printf "%s" "$ep_qs" | cut -d '?' -f1) # no query string
  137. data="$3"
  138. _debug "A24 $ep"
  139. _debug "A24 $Active24_ApiKey"
  140. _debug "A24 $Active24_ApiSecret"
  141. timestamp=$(_time)
  142. datez=$(date -u +"%Y%m%dT%H%M%SZ")
  143. canonicalRequest="${m} ${ep} ${timestamp}"
  144. signature=$(printf "%s" "$canonicalRequest" | _hmac sha1 "$(printf "%s" "$Active24_ApiSecret" | _hex_dump | tr -d " ")" hex)
  145. authorization64="$(printf "%s:%s" "$Active24_ApiKey" "$signature" | _base64)"
  146. export _H1="Date: ${datez}"
  147. export _H2="Accept: application/json"
  148. export _H3="Content-Type: application/json"
  149. export _H4="Authorization: Basic ${authorization64}"
  150. _debug2 H1 "$_H1"
  151. _debug2 H2 "$_H2"
  152. _debug2 H3 "$_H3"
  153. _debug2 H4 "$_H4"
  154. # _sleep 1
  155. if [ "$m" != "GET" ]; then
  156. _debug2 "${m} $Active24_Api${ep_qs}"
  157. _debug "data" "$data"
  158. response="$(_post "$data" "$Active24_Api${ep_qs}" "" "$m" "application/json")"
  159. else
  160. if [ -z "$data" ]; then
  161. _debug2 "GET $Active24_Api${ep_qs}"
  162. response="$(_get "$Active24_Api${ep_qs}")"
  163. else
  164. _debug2 "GET $Active24_Api${ep_qs} with data: ${data}"
  165. response="$(_post "$data" "$Active24_Api${ep_qs}" "" "$m" "application/json")"
  166. fi
  167. fi
  168. if [ "$?" != "0" ]; then
  169. _err "error $ep"
  170. return 1
  171. fi
  172. _debug2 response "$response"
  173. return 0
  174. }