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.

258 lines
7.4 KiB

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