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.

176 lines
4.6 KiB

  1. #!/usr/bin/env sh
  2. #DD_API_User="xxxxx"
  3. #DD_API_Key="xxxxxx"
  4. _DD_BASE="https://durabledns.com/services/dns"
  5. ######## Public functions #####################
  6. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  7. dns_durabledns_add() {
  8. fulldomain=$1
  9. txtvalue=$2
  10. DD_API_User="${DD_API_User:-$(_readaccountconf_mutable DD_API_User)}"
  11. DD_API_Key="${DD_API_Key:-$(_readaccountconf_mutable DD_API_Key)}"
  12. if [ -z "$DD_API_User" ] || [ -z "$DD_API_Key" ]; then
  13. DD_API_User=""
  14. DD_API_Key=""
  15. _err "You didn't specify a durabledns api user or key yet."
  16. _err "You can get yours from here https://durabledns.com/dashboard/index.php"
  17. return 1
  18. fi
  19. #save the api key and email to the account conf file.
  20. _saveaccountconf_mutable DD_API_User "$DD_API_User"
  21. _saveaccountconf_mutable DD_API_Key "$DD_API_Key"
  22. _debug "First detect the root zone"
  23. if ! _get_root "$fulldomain"; then
  24. _err "invalid domain"
  25. return 1
  26. fi
  27. _debug _sub_domain "$_sub_domain"
  28. _debug _domain "$_domain"
  29. _dd_soap createRecord string zonename "$_domain." string name "$_sub_domain" string type "TXT" string data "$txtvalue" int aux 0 int ttl 10 string ddns_enabled N
  30. _contains "$response" "createRecordResponse"
  31. }
  32. dns_durabledns_rm() {
  33. fulldomain=$1
  34. txtvalue=$2
  35. DD_API_User="${DD_API_User:-$(_readaccountconf_mutable DD_API_User)}"
  36. DD_API_Key="${DD_API_Key:-$(_readaccountconf_mutable DD_API_Key)}"
  37. if [ -z "$DD_API_User" ] || [ -z "$DD_API_Key" ]; then
  38. DD_API_User=""
  39. DD_API_Key=""
  40. _err "You didn't specify a durabledns api user or key yet."
  41. _err "You can get yours from here https://durabledns.com/dashboard/index.php"
  42. return 1
  43. fi
  44. _debug "First detect the root zone"
  45. if ! _get_root "$fulldomain"; then
  46. _err "invalid domain"
  47. return 1
  48. fi
  49. _debug _sub_domain "$_sub_domain"
  50. _debug _domain "$_domain"
  51. _debug "Find record id"
  52. if ! _dd_soap listRecords string zonename "$_domain."; then
  53. _err "can not listRecords"
  54. return 1
  55. fi
  56. subtxt="$(echo "$txtvalue" | cut -c 1-30)"
  57. record="$(echo "$response" | sed 's/<item\>/#<item>/g' | tr '#' '\n' | grep ">$subtxt")"
  58. _debug record "$record"
  59. if [ -z "$record" ]; then
  60. _err "can not find record for txtvalue" "$txtvalue"
  61. _err "$response"
  62. return 1
  63. fi
  64. recordid="$(echo "$record" | _egrep_o '<id xsi:type="xsd:int">[0-9]*</id>' | cut -d '>' -f 2 | cut -d '<' -f 1)"
  65. _debug recordid "$recordid"
  66. if [ -z "$recordid" ]; then
  67. _err "can not find record id"
  68. return 1
  69. fi
  70. if ! _dd_soap deleteRecord string zonename "$_domain." int id "$recordid"; then
  71. _err "delete error"
  72. return 1
  73. fi
  74. _contains "$response" "Success"
  75. }
  76. #_acme-challenge.www.domain.com
  77. #returns
  78. # _sub_domain=_acme-challenge.www
  79. # _domain=domain.com
  80. _get_root() {
  81. domain=$1
  82. if ! _dd_soap "listZones"; then
  83. return 1
  84. fi
  85. i=1
  86. p=1
  87. while true; do
  88. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  89. _debug h "$h"
  90. if [ -z "$h" ]; then
  91. #not valid
  92. return 1
  93. fi
  94. if _contains "$response" ">$h.</origin>"; then
  95. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  96. _domain=$h
  97. return 0
  98. fi
  99. p=$i
  100. i=$(_math "$i" + 1)
  101. done
  102. return 1
  103. }
  104. #method
  105. _dd_soap() {
  106. _method="$1"
  107. shift
  108. _urn="${_method}wsdl"
  109. # put the parameters to xml
  110. body="<tns:$_method>
  111. <apiuser xsi:type=\"xsd:string\">$DD_API_User</apiuser>
  112. <apikey xsi:type=\"xsd:string\">$DD_API_Key</apikey>
  113. "
  114. while [ "$1" ]; do
  115. _t="$1"
  116. shift
  117. _k="$1"
  118. shift
  119. _v="$1"
  120. shift
  121. body="$body<$_k xsi:type=\"xsd:$_t\">$_v</$_k>"
  122. done
  123. body="$body</tns:$_method>"
  124. _debug2 "SOAP request ${body}"
  125. # build SOAP XML
  126. _xml='<?xml version="1.0" encoding="utf-8"?>
  127. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  128. xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
  129. xmlns:tns="urn:'$_urn'"
  130. xmlns:types="urn:'$_urn'/encodedTypes"
  131. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  132. xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  133. <soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'"$body"'</soap:Body>
  134. </soap:Envelope>'
  135. _debug2 _xml "$_xml"
  136. # set SOAP headers
  137. _action="SOAPAction: \"urn:$_urn#$_method\""
  138. _debug2 "_action" "$_action"
  139. export _H1="$_action"
  140. export _H2="Content-Type: text/xml; charset=utf-8"
  141. _url="$_DD_BASE/$_method.php"
  142. _debug "_url" "$_url"
  143. if ! response="$(_post "${_xml}" "${_url}")"; then
  144. _err "Error <$1>"
  145. return 1
  146. fi
  147. _debug2 "response" "$response"
  148. response="$(echo "$response" | tr -d "\r\n" | _egrep_o ":${_method}Response .*:${_method}Response><")"
  149. _debug2 "response" "$response"
  150. return 0
  151. }