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.

259 lines
5.6 KiB

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