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.

204 lines
5.2 KiB

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