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.

208 lines
5.4 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_azion_info='Azion.om
  4. Site: Azion.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_azion
  6. Options:
  7. AZION_Email Email
  8. AZION_Password Password
  9. Issues: github.com/acmesh-official/acme.sh/issues/3555
  10. '
  11. AZION_Api="https://api.azionapi.net"
  12. ######## Public functions ########
  13. # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. # Used to add txt record
  15. dns_azion_add() {
  16. fulldomain=$1
  17. txtvalue=$2
  18. _debug "Detect the root zone"
  19. if ! _get_root "$fulldomain"; then
  20. _err "Domain not found"
  21. return 1
  22. fi
  23. _debug _sub_domain "$_sub_domain"
  24. _debug _domain "$_domain"
  25. _debug _domain_id "$_domain_id"
  26. _info "Add or update record"
  27. _get_record "$_domain_id" "$_sub_domain"
  28. if [ "$record_id" ]; then
  29. _payload="{\"record_type\": \"TXT\", \"entry\": \"$_sub_domain\", \"answers_list\": [$answers_list, \"$txtvalue\"], \"ttl\": 20}"
  30. if _azion_rest PUT "intelligent_dns/$_domain_id/records/$record_id" "$_payload"; then
  31. if _contains "$response" "$txtvalue"; then
  32. _info "Record updated."
  33. return 0
  34. fi
  35. fi
  36. else
  37. _payload="{\"record_type\": \"TXT\", \"entry\": \"$_sub_domain\", \"answers_list\": [\"$txtvalue\"], \"ttl\": 20}"
  38. if _azion_rest POST "intelligent_dns/$_domain_id/records" "$_payload"; then
  39. if _contains "$response" "$txtvalue"; then
  40. _info "Record added."
  41. return 0
  42. fi
  43. fi
  44. fi
  45. _err "Failed to add or update record."
  46. return 1
  47. }
  48. # Usage: fulldomain txtvalue
  49. # Used to remove the txt record after validation
  50. dns_azion_rm() {
  51. fulldomain=$1
  52. txtvalue=$2
  53. _debug "Detect the root zone"
  54. if ! _get_root "$fulldomain"; then
  55. _err "Domain not found"
  56. return 1
  57. fi
  58. _debug _sub_domain "$_sub_domain"
  59. _debug _domain "$_domain"
  60. _debug _domain_id "$_domain_id"
  61. _info "Removing record"
  62. _get_record "$_domain_id" "$_sub_domain"
  63. if [ "$record_id" ]; then
  64. if _azion_rest DELETE "intelligent_dns/$_domain_id/records/$record_id"; then
  65. _info "Record removed."
  66. return 0
  67. else
  68. _err "Failed to remove record."
  69. return 1
  70. fi
  71. else
  72. _info "Record not found or already removed."
  73. return 0
  74. fi
  75. }
  76. #################### Private functions below ##################################
  77. # Usage: _acme-challenge.www.domain.com
  78. # returns
  79. # _sub_domain=_acme-challenge.www
  80. # _domain=domain.com
  81. # _domain_id=sdjkglgdfewsdfg
  82. _get_root() {
  83. domain=$1
  84. i=1
  85. p=1
  86. if ! _azion_rest GET "intelligent_dns"; then
  87. return 1
  88. fi
  89. while true; do
  90. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  91. _debug h "$h"
  92. if [ -z "$h" ]; then
  93. # not valid
  94. return 1
  95. fi
  96. if _contains "$response" "\"domain\":\"$h\""; then
  97. _domain_id=$(echo "$response" | tr '{' "\n" | grep "\"domain\":\"$h\"" | _egrep_o "\"id\":[0-9]*" | _head_n 1 | cut -d : -f 2 | tr -d \")
  98. _debug _domain_id "$_domain_id"
  99. if [ "$_domain_id" ]; then
  100. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  101. _domain=$h
  102. return 0
  103. fi
  104. return 1
  105. fi
  106. p=$i
  107. i=$(_math "$i" + 1)
  108. done
  109. return 1
  110. }
  111. _get_record() {
  112. _domain_id=$1
  113. _record=$2
  114. if ! _azion_rest GET "intelligent_dns/$_domain_id/records"; then
  115. return 1
  116. fi
  117. if _contains "$response" "\"entry\":\"$_record\""; then
  118. _json_record=$(echo "$response" | tr '{' "\n" | grep "\"entry\":\"$_record\"")
  119. if [ "$_json_record" ]; then
  120. record_id=$(echo "$_json_record" | _egrep_o "\"record_id\":[0-9]*" | _head_n 1 | cut -d : -f 2 | tr -d \")
  121. answers_list=$(echo "$_json_record" | _egrep_o "\"answers_list\":\[.*\]" | _head_n 1 | cut -d : -f 2 | tr -d \[\])
  122. return 0
  123. fi
  124. return 1
  125. fi
  126. return 1
  127. }
  128. _get_token() {
  129. AZION_Email="${AZION_Email:-$(_readaccountconf_mutable AZION_Email)}"
  130. AZION_Password="${AZION_Password:-$(_readaccountconf_mutable AZION_Password)}"
  131. if ! _contains "$AZION_Email" "@"; then
  132. _err "It seems that the AZION_Email is not a valid email address. Revalidate your environments."
  133. return 1
  134. fi
  135. if [ -z "$AZION_Email" ] || [ -z "$AZION_Password" ]; then
  136. _err "You didn't specified a AZION_Email/AZION_Password to generate Azion token."
  137. return 1
  138. fi
  139. _saveaccountconf_mutable AZION_Email "$AZION_Email"
  140. _saveaccountconf_mutable AZION_Password "$AZION_Password"
  141. _basic_auth=$(printf "%s:%s" "$AZION_Email" "$AZION_Password" | _base64)
  142. _debug _basic_auth "$_basic_auth"
  143. export _H1="Accept: application/json; version=3"
  144. export _H2="Content-Type: application/json"
  145. export _H3="Authorization: Basic $_basic_auth"
  146. response="$(_post "" "$AZION_Api/tokens" "" "POST")"
  147. if _contains "$response" "\"token\":\"" >/dev/null; then
  148. _azion_token=$(echo "$response" | _egrep_o "\"token\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")
  149. export AZION_Token="$_azion_token"
  150. else
  151. _err "Failed to generate Azion token"
  152. return 1
  153. fi
  154. }
  155. _azion_rest() {
  156. _method=$1
  157. _uri="$2"
  158. _data="$3"
  159. if [ -z "$AZION_Token" ]; then
  160. _get_token
  161. fi
  162. _debug2 token "$AZION_Token"
  163. export _H1="Accept: application/json; version=3"
  164. export _H2="Content-Type: application/json"
  165. export _H3="Authorization: token $AZION_Token"
  166. if [ "$_method" != "GET" ]; then
  167. _debug _data "$_data"
  168. response="$(_post "$_data" "$AZION_Api/$_uri" "" "$_method")"
  169. else
  170. response="$(_get "$AZION_Api/$_uri")"
  171. fi
  172. _debug2 response "$response"
  173. if [ "$?" != "0" ]; then
  174. _err "error $_method $_uri $_data"
  175. return 1
  176. fi
  177. return 0
  178. }