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.

254 lines
5.2 KiB

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