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.

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