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.

167 lines
3.7 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. | grep -F ">${fulldomain}</value>" \
  63. | sed -e 's/<item>/\n\0/g' \
  64. | grep -F '>id</key>' \
  65. | sed -re 's/.*<value[^>]*>([^<]+)<\/value>.*/\1/')"
  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. # add cookie header if present
  90. [ -s "${_cookiejar}" ] && _H2="$(cat "${_cookiejar}")"
  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. grep -F 'Set-Cookie:' "$HTTP_HEADER" | sed -re 's/^Set-(Cookie: [^;]+).*/\1/' | head -1 > "${_cookiejar}"
  98. return 0
  99. }
  100. _get_root() {
  101. domain=$1
  102. i=1
  103. _all_domains="$(_mktemp)"
  104. _dns_do_soap getDomainList
  105. echo "${response}" | tr -d "\n\r\t " | grep -Eo 'domain</key><value[^>]+>[^<]+' | sed -re 's/^domain<\/key><value[^>]+>//g' > "${_all_domains}"
  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 grep -qF "$h" "${_all_domains}"; 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. _info() {
  121. if [ -z "$2" ]; then
  122. echo "[$(date)] $1"
  123. else
  124. echo "[$(date)] $1='$2'"
  125. fi
  126. }
  127. _err() {
  128. _info "$@" >&2
  129. return 1
  130. }
  131. _debug() {
  132. if [ -z "$DEBUG" ]; then
  133. return
  134. fi
  135. _err "$@"
  136. return 0
  137. }
  138. _debug2() {
  139. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  140. _debug "$@"
  141. fi
  142. return
  143. }