dns_active24.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. # TODO: web can also add content $txtvalue to filter and then get the id from response
  42. _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}\"}}"
  43. #_active24_rest GET "/v2/service/$_service_id/dns/record?rowsPerPage=100"
  44. if _contains "$response" "error"; then
  45. _err "Error"
  46. return 1
  47. fi
  48. # Note: it might never be more than one record actually, NEEDS more INVESTIGATION
  49. record_ids=$(printf "%s" "$response" | _egrep_o "[^{]+${txtvalue}[^}]+" | _egrep_o '"id" *: *[^,]+' | cut -d ':' -f 2)
  50. _debug2 record_ids "$record_ids"
  51. for redord_id in $record_ids; do
  52. _debug "Removing record_id" "$redord_id"
  53. _debug "txtvalue" "$txtvalue"
  54. if _active24_rest DELETE "/v2/service/$_service_id/dns/record/$redord_id" ""; then
  55. if _contains "$response" "error"; then
  56. _err "Unable to remove txt record."
  57. return 1
  58. else
  59. _info "Removed txt record."
  60. return 0
  61. fi
  62. fi
  63. done
  64. _err "No txt records found."
  65. return 1
  66. }
  67. _get_root() {
  68. domain=$1
  69. i=1
  70. p=1
  71. if ! _active24_rest GET "/v1/user/self/service"; then
  72. return 1
  73. fi
  74. while true; do
  75. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  76. _debug "h" "$h"
  77. if [ -z "$h" ]; then
  78. #not valid
  79. return 1
  80. fi
  81. if _contains "$response" "\"$h\"" >/dev/null; then
  82. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  83. _domain=$h
  84. return 0
  85. fi
  86. p=$i
  87. i=$(_math "$i" + 1)
  88. done
  89. return 1
  90. }
  91. _active24_init() {
  92. Active24_ApiKey="${Active24_ApiKey:-$(_readaccountconf_mutable Active24_ApiKey)}"
  93. Active24_ApiSecret="${Active24_ApiSecret:-$(_readaccountconf_mutable Active24_ApiSecret)}"
  94. #Active24_ServiceId="${Active24_ServiceId:-$(_readaccountconf_mutable Active24_ServiceId)}"
  95. if [ -z "$Active24_ApiKey" ] || [ -z "$Active24_ApiSecret" ]; then
  96. Active24_ApiKey=""
  97. Active24_ApiSecret=""
  98. _err "You don't specify Active24 api key and ApiSecret yet."
  99. _err "Please create your key and try again."
  100. return 1
  101. fi
  102. #save the credentials to the account conf file.
  103. _saveaccountconf_mutable Active24_ApiKey "$Active24_ApiKey"
  104. _saveaccountconf_mutable Active24_ApiSecret "$Active24_ApiSecret"
  105. _debug "A24 API CHECK"
  106. if ! _active24_rest GET "/v2/check"; then
  107. _err "A24 API check failed with: $response"
  108. return 1
  109. fi
  110. if ! echo "$response" | tr -d " " | grep \"verified\":true >/dev/null; then
  111. _err "A24 API check failed with: $response"
  112. return 1
  113. fi
  114. _debug "First detect the root zone"
  115. if ! _get_root "$fulldomain"; then
  116. _err "invalid domain"
  117. return 1
  118. fi
  119. _debug _sub_domain "$_sub_domain"
  120. _debug _domain "$_domain"
  121. _active24_get_service_id "$_domain"
  122. _debug _service_id "$_service_id"
  123. }
  124. _active24_get_service_id() {
  125. _d=$1
  126. if ! _active24_rest GET "/v1/user/self/zone/${_d}"; then
  127. return 1
  128. else
  129. response=$(echo "$response" | _json_decode)
  130. _service_id=$(echo "$response" | _egrep_o '"id" *: *[^,]+' | cut -d ':' -f 2)
  131. fi
  132. }
  133. _active24_rest() {
  134. m=$1
  135. ep_qs=$2 # with query string
  136. # ep=$2
  137. ep=$(printf "%s" "$ep_qs" | cut -d '?' -f1) # no query string
  138. data="$3"
  139. _debug "A24 $ep"
  140. _debug "A24 $Active24_ApiKey"
  141. _debug "A24 $Active24_ApiSecret"
  142. timestamp=$(_time)
  143. datez=$(date -u +"%Y%m%dT%H%M%SZ")
  144. canonicalRequest="${m} ${ep} ${timestamp}"
  145. signature=$(printf "%s" "$canonicalRequest" | _hmac sha1 "$(printf "%s" "$Active24_ApiSecret" | _hex_dump | tr -d " ")" hex)
  146. authorization64="$(printf "%s:%s" "$Active24_ApiKey" "$signature" | _base64)"
  147. export _H1="Date: ${datez}"
  148. export _H2="Accept: application/json"
  149. export _H3="Content-Type: application/json"
  150. export _H4="Authorization: Basic ${authorization64}"
  151. _debug2 H1 "$_H1"
  152. _debug2 H2 "$_H2"
  153. _debug2 H3 "$_H3"
  154. _debug2 H4 "$_H4"
  155. # _sleep 1
  156. if [ "$m" != "GET" ]; then
  157. _debug2 "${m} $Active24_Api${ep_qs}"
  158. _debug "data" "$data"
  159. response="$(_post "$data" "$Active24_Api${ep_qs}" "" "$m" "application/json")"
  160. else
  161. if [ -z "$data" ]; then
  162. _debug2 "GET $Active24_Api${ep_qs}"
  163. response="$(_get "$Active24_Api${ep_qs}")"
  164. else
  165. _debug2 "GET $Active24_Api${ep_qs} with data: ${data}"
  166. response="$(_post "$data" "$Active24_Api${ep_qs}" "" "$m" "application/json")"
  167. fi
  168. fi
  169. if [ "$?" != "0" ]; then
  170. _err "error $ep"
  171. return 1
  172. fi
  173. _debug2 response "$response"
  174. return 0
  175. }