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.

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