dns_schlundtech.sh 5.6 KB

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