dns_subreg.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_subreg_info='Subreg.cz
  4. Site: subreg.cz
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_subreg
  6. Options:
  7. SUBREG_API_USERNAME API username
  8. SUBREG_API_PASSWORD API password
  9. Issues: github.com/acmesh-official/acme.sh/issues/6835
  10. Author: Tomas Pavlic <https://github.com/tomaspavlic>
  11. '
  12. # Subreg SOAP API
  13. # https://subreg.cz/manual/
  14. SUBREG_API_URL="https://soap.subreg.cz/cmd.php"
  15. ######## Public functions #####################
  16. # Usage: dns_subreg_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  17. dns_subreg_add() {
  18. fulldomain=$1
  19. txtvalue=$2
  20. SUBREG_API_USERNAME="${SUBREG_API_USERNAME:-$(_readaccountconf_mutable SUBREG_API_USERNAME)}"
  21. SUBREG_API_PASSWORD="${SUBREG_API_PASSWORD:-$(_readaccountconf_mutable SUBREG_API_PASSWORD)}"
  22. if [ -z "$SUBREG_API_USERNAME" ] || [ -z "$SUBREG_API_PASSWORD" ]; then
  23. _err "SUBREG_API_USERNAME and SUBREG_API_PASSWORD are not set."
  24. return 1
  25. fi
  26. _saveaccountconf_mutable SUBREG_API_USERNAME "$SUBREG_API_USERNAME"
  27. _saveaccountconf_mutable SUBREG_API_PASSWORD "$SUBREG_API_PASSWORD"
  28. if ! _subreg_login; then
  29. return 1
  30. fi
  31. if ! _get_root "$fulldomain"; then
  32. _err "Cannot determine root domain for: $fulldomain"
  33. return 1
  34. fi
  35. _debug _sub_domain "$_sub_domain"
  36. _debug _domain "$_domain"
  37. _subreg_soap "Add_DNS_Record" "<domain>$_domain</domain><record><name>$_sub_domain</name><type>TXT</type><content>$txtvalue</content><prio>0</prio><ttl>120</ttl></record>"
  38. if _subreg_ok; then
  39. _record_id="$(_subreg_map_get record_id)"
  40. if [ -z "$_record_id" ]; then
  41. _err "Subreg API did not return a record_id for TXT record on $fulldomain"
  42. _err "$response"
  43. return 1
  44. fi
  45. _savedomainconf "$(_subreg_record_id_key "$txtvalue")" "$_record_id"
  46. return 0
  47. fi
  48. _err "Failed to add TXT record."
  49. _err "$response"
  50. return 1
  51. }
  52. # Usage: dns_subreg_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  53. dns_subreg_rm() {
  54. fulldomain=$1
  55. txtvalue=$2
  56. SUBREG_API_USERNAME="${SUBREG_API_USERNAME:-$(_readaccountconf_mutable SUBREG_API_USERNAME)}"
  57. SUBREG_API_PASSWORD="${SUBREG_API_PASSWORD:-$(_readaccountconf_mutable SUBREG_API_PASSWORD)}"
  58. if [ -z "$SUBREG_API_USERNAME" ] || [ -z "$SUBREG_API_PASSWORD" ]; then
  59. _err "SUBREG_API_USERNAME and SUBREG_API_PASSWORD are not set."
  60. return 1
  61. fi
  62. if ! _subreg_login; then
  63. return 1
  64. fi
  65. if ! _get_root "$fulldomain"; then
  66. _err "Cannot determine root domain for: $fulldomain"
  67. return 1
  68. fi
  69. _debug _sub_domain "$_sub_domain"
  70. _debug _domain "$_domain"
  71. _record_id="$(_readdomainconf "$(_subreg_record_id_key "$txtvalue")")"
  72. if [ -z "$_record_id" ]; then
  73. _err "Could not find saved record ID for $fulldomain"
  74. return 1
  75. fi
  76. _debug "Deleting record ID: $_record_id"
  77. _subreg_soap "Delete_DNS_Record" "<domain>$_domain</domain><record><id>$_record_id</id></record>"
  78. if _subreg_ok; then
  79. _cleardomainconf "$(_subreg_record_id_key "$txtvalue")"
  80. return 0
  81. fi
  82. _err "Failed to delete TXT record."
  83. _err "$response"
  84. return 1
  85. }
  86. #################### Private functions #####################
  87. # Build a domain-conf key for storing the record ID of a given TXT value.
  88. # Base64url chars include '-' which is invalid in shell variable names, so replace with '_'.
  89. _subreg_record_id_key() {
  90. printf 'SUBREG_RECORD_ID_%s' "$(printf '%s' "$1" | tr '-' '_')"
  91. }
  92. # Check if the current $response contains a successful status in the ns2:Map format:
  93. # <item><key ...>status</key><value ...>ok</value></item>
  94. _subreg_ok() {
  95. [ "$(_subreg_map_get status)" = "ok" ]
  96. }
  97. # Extract the value for a given key from the ns2:Map response.
  98. # Usage: _subreg_map_get keyname
  99. # Reads from $response
  100. _subreg_map_get() {
  101. _key="$1"
  102. echo "$response" | tr -d '\n\r' | _egrep_o ">${_key}</key><value[^>]*>[^<]*</value>" | sed 's/.*<value[^>]*>//;s/<\/value>//'
  103. }
  104. # Login and store session token in _subreg_ssid
  105. _subreg_login() {
  106. _debug "Logging in to Subreg API as $SUBREG_API_USERNAME"
  107. _subreg_soap_noauth "Login" "<login>$SUBREG_API_USERNAME</login><password>$SUBREG_API_PASSWORD</password>"
  108. if ! _subreg_ok; then
  109. _err "Subreg login failed."
  110. _err "$response"
  111. return 1
  112. fi
  113. _subreg_ssid="$(_subreg_map_get ssid)"
  114. if [ -z "$_subreg_ssid" ]; then
  115. _err "Subreg login: could not extract session token (ssid)."
  116. return 1
  117. fi
  118. _debug "Subreg login: session token (ssid) obtained"
  119. return 0
  120. }
  121. # _get_root _acme-challenge.www.domain.com
  122. # returns _sub_domain and _domain
  123. _get_root() {
  124. domain=$1
  125. i=1
  126. p=1
  127. while true; do
  128. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  129. if [ -z "$h" ]; then
  130. _err "Unable to retrieve DNS zone matching domain: $domain"
  131. return 1
  132. fi
  133. _subreg_soap "Get_DNS_Zone" "<domain>$h</domain>"
  134. if _subreg_ok; then
  135. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  136. _domain="$h"
  137. return 0
  138. fi
  139. p=$i
  140. i=$(_math "$i" + 1)
  141. done
  142. }
  143. # Send a SOAP request without authentication (used for Login)
  144. # _subreg_soap_noauth command inner_xml
  145. _subreg_build_soap() {
  146. _cmd="$1"
  147. _data_inner="$2"
  148. _soap_body="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  149. <SOAP-ENV:Envelope
  150. xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"
  151. xmlns:ns1=\"http://soap.subreg.cz/soap\"
  152. xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
  153. xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
  154. xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"
  155. SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">
  156. <SOAP-ENV:Body>
  157. <ns1:${_cmd}>
  158. <data>
  159. ${_data_inner}
  160. </data>
  161. </ns1:${_cmd}>
  162. </SOAP-ENV:Body>
  163. </SOAP-ENV:Envelope>"
  164. export _H1="Content-Type: text/xml"
  165. export _H2="SOAPAction: http://soap.subreg.cz/soap#${_cmd}"
  166. response="$(_post "$_soap_body" "$SUBREG_API_URL" "" "POST" "text/xml")"
  167. }
  168. # Send an authenticated SOAP request (requires _subreg_ssid to be set)
  169. # _subreg_soap command inner_xml
  170. _subreg_soap_noauth() {
  171. _cmd="$1"
  172. _inner="$2"
  173. _subreg_build_soap "$_cmd" "$_inner"
  174. }
  175. # Send an authenticated SOAP request (requires _subreg_ssid to be set)
  176. # _subreg_soap command inner_xml
  177. _subreg_soap() {
  178. _cmd="$1"
  179. _inner="$2"
  180. _inner_with_ssid="<ssid>${_subreg_ssid}</ssid>${_inner}"
  181. _subreg_build_soap "$_cmd" "$_inner_with_ssid"
  182. }