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.

143 lines
3.4 KiB

  1. #!/usr/bin/env sh
  2. # DNS API for Domain-Offensive / Resellerinterface / Domainrobot
  3. # DO_PID="KD-1234567"
  4. # DO_PW="cdfkjl3n2"
  5. DO_URL="https://soap.resellerinterface.de/"
  6. ######## Public functions #####################
  7. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  8. dns_do_add() {
  9. fulldomain=$1
  10. txtvalue=$2
  11. if _dns_do_authenticate; then
  12. _info "Adding TXT record to ${_domain} as ${fulldomain}"
  13. _dns_do_soap createRR origin "${_domain}" name "${fulldomain}" type TXT data "${txtvalue}" ttl 300
  14. if _contains "${response}" '>success<'; then
  15. return 0
  16. fi
  17. _err "Could not create resource record, check logs"
  18. fi
  19. return 1
  20. }
  21. #fulldomain
  22. dns_do_rm() {
  23. fulldomain=$1
  24. _cookiejar="$(_mktemp)"
  25. if _dns_do_authenticate; then
  26. if _dns_do_list_rrs; then
  27. for _rrid in ${_rr_list}; do
  28. _info "Deleting resource record $_rrid for $_domain"
  29. _dns_do_soap deleteRR origin "${_domain}" rrid "${_rrid}"
  30. if ! _contains "${response}" '>success<'; then
  31. _err "Could not delete resource record for ${_domain}, id ${_rrid}"
  32. fi
  33. done
  34. return 0
  35. fi
  36. fi
  37. return 1
  38. }
  39. #################### Private functions below ##################################
  40. _dns_do_authenticate() {
  41. _info "Authenticating as ${DO_PID}"
  42. _dns_do_soap authPartner partner "${DO_PID}" password "${DO_PW}"
  43. if _contains "${response}" '>success<'; then
  44. _get_root "$fulldomain"
  45. _debug "_domain $_domain"
  46. return 0
  47. else
  48. _err "Authentication failed, are DO_PID and DO_PW set correctly?"
  49. fi
  50. return 1
  51. }
  52. _dns_do_list_rrs() {
  53. _dns_do_soap getRRList origin "${_domain}"
  54. if ! _contains "${response}" 'SOAP-ENC:Array'; then
  55. _err "getRRList origin ${_domain} failed"
  56. return 1
  57. fi
  58. _rr_list="$(echo "${response}" \
  59. | tr -d "\n\r\t" \
  60. | sed -e 's/<item xsi:type="ns2:Map">/\n/g' \
  61. | grep ">$(_regexcape "$fulldomain")</value>" \
  62. | sed -e 's/<\/item>/\n/g' \
  63. | grep '>id</key><value' \
  64. | _egrep_o '>[0-9]{1,16}<' \
  65. | tr -d '><')"
  66. [ "${_rr_list}" ]
  67. }
  68. _dns_do_soap() {
  69. func="$1"
  70. shift
  71. # put the parameters to xml
  72. body="<tns:${func} xmlns:tns=\"${DO_URL}\">"
  73. while [ "$1" ]; do
  74. _k="$1"
  75. shift
  76. _v="$1"
  77. shift
  78. body="$body<$_k>$_v</$_k>"
  79. done
  80. body="$body</tns:${func}>"
  81. _debug2 "SOAP request ${body}"
  82. # build SOAP XML
  83. _xml='<?xml version="1.0" encoding="UTF-8"?>
  84. <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
  85. <env:Body>'"$body"'</env:Body>
  86. </env:Envelope>'
  87. # set SOAP headers
  88. _H1="SOAPAction: ${DO_URL}#${func}"
  89. if ! response="$(_post "${_xml}" "${DO_URL}")"; then
  90. _err "Error <$1>"
  91. return 1
  92. fi
  93. _debug2 "SOAP response $response"
  94. # retrieve cookie header
  95. _H2="$(_egrep_o 'Cookie: [^;]+' <"$HTTP_HEADER" | head -1)"
  96. return 0
  97. }
  98. _get_root() {
  99. domain=$1
  100. i=1
  101. _dns_do_soap getDomainList
  102. _all_domains="/$(echo "${response}" \
  103. | tr -d "\n\r\t " \
  104. | _egrep_o 'domain</key><value[^>]+>[^<]+' \
  105. | sed -e 's/^domain<\/key><value[^>]*>//g')"
  106. while true; do
  107. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  108. if [ -z "$h" ]; then
  109. return 1
  110. fi
  111. if _contains "${_all_domains}" "^$(_regexcape "$h")\$"; then
  112. _domain="$h"
  113. return 0
  114. fi
  115. i=$(_math $i + 1)
  116. done
  117. _debug "$domain not found"
  118. return 1
  119. }
  120. _regexcape() {
  121. echo "$1" | sed -e 's/\([]\.$*^[]\)/\\\1/g'
  122. }