dns_autodns.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #!/usr/bin/env sh
  2. # -*- mode: sh; tab-width: 2; indent-tabs-mode: s; coding: utf-8 -*-
  3. # This is the InternetX autoDNS xml api wrapper for acme.sh
  4. # Author: auerswald@gmail.com
  5. # Created: 2018-01-14
  6. #
  7. # export AUTODNS_USER="username"
  8. # export AUTODNS_PASSWORD="password"
  9. # export AUTODNS_CONTEXT="context"
  10. #
  11. # Usage:
  12. # acme.sh --issue --dns dns_autodns -d example.com
  13. AUTODNS_API="https://gateway.autodns.com"
  14. # Arguments:
  15. # txtdomain
  16. # txt
  17. dns_autodns_add() {
  18. fulldomain="$1"
  19. txtvalue="$2"
  20. AUTODNS_USER="${AUTODNS_USER:-$(_readaccountconf_mutable AUTODNS_USER)}"
  21. AUTODNS_PASSWORD="${AUTODNS_PASSWORD:-$(_readaccountconf_mutable AUTODNS_PASSWORD)}"
  22. AUTODNS_CONTEXT="${AUTODNS_CONTEXT:-$(_readaccountconf_mutable AUTODNS_CONTEXT)}"
  23. if [ -z "$AUTODNS_USER" ] || [ -z "$AUTODNS_CONTEXT" ] || [ -z "$AUTODNS_PASSWORD" ]; then
  24. _err "You don't specify autodns user, password and context."
  25. return 1
  26. fi
  27. _saveaccountconf_mutable AUTODNS_USER "$AUTODNS_USER"
  28. _saveaccountconf_mutable AUTODNS_PASSWORD "$AUTODNS_PASSWORD"
  29. _saveaccountconf_mutable AUTODNS_CONTEXT "$AUTODNS_CONTEXT"
  30. _debug "First detect the root zone"
  31. if ! _get_autodns_zone "$fulldomain"; then
  32. _err "invalid domain"
  33. return 1
  34. fi
  35. _debug _sub_domain "$_sub_domain"
  36. _debug _zone "$_zone"
  37. _debug _system_ns "$_system_ns"
  38. _info "Adding TXT record"
  39. autodns_response="$(_autodns_zone_update "$_zone" "$_sub_domain" "$txtvalue" "$_system_ns")"
  40. if [ "$?" -eq "0" ]; then
  41. _info "Added, OK"
  42. return 0
  43. fi
  44. return 1
  45. }
  46. # Arguments:
  47. # txtdomain
  48. # txt
  49. dns_autodns_rm() {
  50. fulldomain="$1"
  51. txtvalue="$2"
  52. AUTODNS_USER="${AUTODNS_USER:-$(_readaccountconf_mutable AUTODNS_USER)}"
  53. AUTODNS_PASSWORD="${AUTODNS_PASSWORD:-$(_readaccountconf_mutable AUTODNS_PASSWORD)}"
  54. AUTODNS_CONTEXT="${AUTODNS_CONTEXT:-$(_readaccountconf_mutable AUTODNS_CONTEXT)}"
  55. if [ -z "$AUTODNS_USER" ] || [ -z "$AUTODNS_CONTEXT" ] || [ -z "$AUTODNS_PASSWORD" ]; then
  56. _err "You don't specify autodns user, password and context."
  57. return 1
  58. fi
  59. _debug "First detect the root zone"
  60. if ! _get_autodns_zone "$fulldomain"; then
  61. _err "zone not found"
  62. return 1
  63. fi
  64. _debug _sub_domain "$_sub_domain"
  65. _debug _zone "$_zone"
  66. _debug _system_ns "$_system_ns"
  67. _info "Delete TXT record"
  68. autodns_response="$(_autodns_zone_cleanup "$_zone" "$_sub_domain" "$txtvalue" "$_system_ns")"
  69. if [ "$?" -eq "0" ]; then
  70. _info "Deleted, OK"
  71. return 0
  72. fi
  73. return 1
  74. }
  75. #################### Private functions below ##################################
  76. # Arguments:
  77. # fulldomain
  78. # Returns:
  79. # _sub_domain=_acme-challenge.www
  80. # _zone=domain.com
  81. # _system_ns
  82. _get_autodns_zone() {
  83. domain="$1"
  84. i=2
  85. p=1
  86. while true; do
  87. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  88. _debug h "$h"
  89. if [ -z "$h" ]; then
  90. # not valid
  91. return 1
  92. fi
  93. autodns_response="$(_autodns_zone_inquire "$h")"
  94. if [ "$?" -ne "0" ]; then
  95. _err "invalid domain"
  96. return 1
  97. fi
  98. if _contains "$autodns_response" "<summary>1</summary>" >/dev/null; then
  99. _zone="$(echo "$autodns_response" | _egrep_o '<name>[^<]*</name>' | cut -d '>' -f 2 | cut -d '<' -f 1)"
  100. _system_ns="$(echo "$autodns_response" | _egrep_o '<system_ns>[^<]*</system_ns>' | cut -d '>' -f 2 | cut -d '<' -f 1)"
  101. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  102. return 0
  103. fi
  104. p=$i
  105. i=$(_math "$i" + 1)
  106. done
  107. return 1
  108. }
  109. _build_request_auth_xml() {
  110. printf "<auth>
  111. <user>%s</user>
  112. <password>%s</password>
  113. <context>%s</context>
  114. </auth>" "$AUTODNS_USER" "$AUTODNS_PASSWORD" "$AUTODNS_CONTEXT"
  115. }
  116. # Arguments:
  117. # zone
  118. _build_zone_inquire_xml() {
  119. printf "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  120. <request>
  121. %s
  122. <task>
  123. <code>0205</code>
  124. <view>
  125. <children>1</children>
  126. <limit>1</limit>
  127. </view>
  128. <where>
  129. <key>name</key>
  130. <operator>eq</operator>
  131. <value>%s</value>
  132. </where>
  133. </task>
  134. </request>" "$(_build_request_auth_xml)" "$1"
  135. }
  136. # Arguments:
  137. # zone
  138. # subdomain
  139. # txtvalue
  140. # system_ns
  141. _build_zone_update_xml() {
  142. printf "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  143. <request>
  144. %s
  145. <task>
  146. <code>0202001</code>
  147. <default>
  148. <rr_add>
  149. <name>%s</name>
  150. <ttl>600</ttl>
  151. <type>TXT</type>
  152. <value>%s</value>
  153. </rr_add>
  154. </default>
  155. <zone>
  156. <name>%s</name>
  157. <system_ns>%s</system_ns>
  158. </zone>
  159. </task>
  160. </request>" "$(_build_request_auth_xml)" "$2" "$3" "$1" "$4"
  161. }
  162. # Arguments:
  163. # zone
  164. _autodns_zone_inquire() {
  165. request_data="$(_build_zone_inquire_xml "$1")"
  166. autodns_response="$(_autodns_api_call "$request_data")"
  167. ret="$?"
  168. printf "%s" "$autodns_response"
  169. return "$ret"
  170. }
  171. # Arguments:
  172. # zone
  173. # subdomain
  174. # txtvalue
  175. # system_ns
  176. _autodns_zone_update() {
  177. request_data="$(_build_zone_update_xml "$1" "$2" "$3" "$4")"
  178. autodns_response="$(_autodns_api_call "$request_data")"
  179. ret="$?"
  180. printf "%s" "$autodns_response"
  181. return "$ret"
  182. }
  183. # Arguments:
  184. # zone
  185. # subdomain
  186. # txtvalue
  187. # system_ns
  188. _autodns_zone_cleanup() {
  189. request_data="$(_build_zone_update_xml "$1" "$2" "$3" "$4")"
  190. # replace 'rr_add>' with 'rr_rem>' in request_data
  191. request_data="$(printf -- "%s" "$request_data" | sed 's/rr_add>/rr_rem>/g')"
  192. autodns_response="$(_autodns_api_call "$request_data")"
  193. ret="$?"
  194. printf "%s" "$autodns_response"
  195. return "$ret"
  196. }
  197. # Arguments:
  198. # request_data
  199. _autodns_api_call() {
  200. request_data="$1"
  201. _debug request_data "$request_data"
  202. autodns_response="$(_post "$request_data" "$AUTODNS_API")"
  203. ret="$?"
  204. _debug autodns_response "$autodns_response"
  205. if [ "$ret" -ne "0" ]; then
  206. _err "error"
  207. return 1
  208. fi
  209. if _contains "$autodns_response" "<type>success</type>" >/dev/null; then
  210. _info "success"
  211. printf "%s" "$autodns_response"
  212. return 0
  213. fi
  214. return 1
  215. }