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.

160 lines
4.1 KiB

  1. #!/usr/bin/env sh
  2. # united-domains Reselling (https://www.ud-reselling.com/) DNS API
  3. # Author: Andreas Scherer (https://github.com/andischerer)
  4. # Created: 2021-02-01
  5. #
  6. # Set the environment variables as below:
  7. #
  8. # export UDR_USER="your_username_goes_here"
  9. # export UDR_PASS="some_password_goes_here"
  10. #
  11. UDR_API="https://api.domainreselling.de/api/call.cgi"
  12. UDR_TTL="30"
  13. ######## Public functions #####################
  14. #Usage: add _acme-challenge.www.domain.com "some_long_string_of_characters_go_here_from_lets_encrypt"
  15. dns_udr_add() {
  16. fulldomain=$1
  17. txtvalue=$2
  18. UDR_USER="${UDR_USER:-$(_readaccountconf_mutable UDR_USER)}"
  19. UDR_PASS="${UDR_PASS:-$(_readaccountconf_mutable UDR_PASS)}"
  20. if [ -z "$UDR_USER" ] || [ -z "$UDR_PASS" ]; then
  21. UDR_USER=""
  22. UDR_PASS=""
  23. _err "You didn't specify an UD-Reselling username and password yet"
  24. return 1
  25. fi
  26. # save the username and password to the account conf file.
  27. _saveaccountconf_mutable UDR_USER "$UDR_USER"
  28. _saveaccountconf_mutable UDR_PASS "$UDR_PASS"
  29. _debug "First detect the root zone"
  30. if ! _get_root "$fulldomain"; then
  31. _err "invalid domain"
  32. return 1
  33. fi
  34. _debug _dnszone "${_dnszone}"
  35. _debug "Getting txt records"
  36. if ! _udr_rest "QueryDNSZoneRRList" "dnszone=${_dnszone}"; then
  37. return 1
  38. fi
  39. rr="${fulldomain}. ${UDR_TTL} IN TXT ${txtvalue}"
  40. _debug resource_record "${rr}"
  41. if _contains "$response" "$rr" >/dev/null; then
  42. _err "Error, it would appear that this record already exists. Please review existing TXT records for this domain."
  43. return 1
  44. fi
  45. _info "Adding record"
  46. if ! _udr_rest "UpdateDNSZone" "dnszone=${_dnszone}&addrr0=${rr}"; then
  47. _err "Adding the record did not succeed, please verify/check."
  48. return 1
  49. fi
  50. _info "Added, OK"
  51. return 0
  52. }
  53. dns_udr_rm() {
  54. fulldomain=$1
  55. txtvalue=$2
  56. UDR_USER="${UDR_USER:-$(_readaccountconf_mutable UDR_USER)}"
  57. UDR_PASS="${UDR_PASS:-$(_readaccountconf_mutable UDR_PASS)}"
  58. if [ -z "$UDR_USER" ] || [ -z "$UDR_PASS" ]; then
  59. UDR_USER=""
  60. UDR_PASS=""
  61. _err "You didn't specify an UD-Reselling username and password yet"
  62. return 1
  63. fi
  64. _debug "First detect the root zone"
  65. if ! _get_root "$fulldomain"; then
  66. _err "invalid domain"
  67. return 1
  68. fi
  69. _debug _dnszone "${_dnszone}"
  70. _debug "Getting txt records"
  71. if ! _udr_rest "QueryDNSZoneRRList" "dnszone=${_dnszone}"; then
  72. return 1
  73. fi
  74. rr="${fulldomain}. ${UDR_TTL} IN TXT ${txtvalue}"
  75. _debug resource_record "${rr}"
  76. if _contains "$response" "$rr" >/dev/null; then
  77. if ! _udr_rest "UpdateDNSZone" "dnszone=${_dnszone}&delrr0=${rr}"; then
  78. _err "Deleting the record did not succeed, please verify/check."
  79. return 1
  80. fi
  81. _info "Removed, OK"
  82. return 0
  83. else
  84. _info "Text record is not present, will not delete anything."
  85. return 0
  86. fi
  87. }
  88. #################### Private functions below ##################################
  89. #_acme-challenge.www.domain.com
  90. #returns
  91. # _sub_domain=_acme-challenge.www
  92. # _domain=domain.com
  93. _get_root() {
  94. domain=$1
  95. i=1
  96. if ! _udr_rest "QueryDNSZoneList" ""; then
  97. return 1
  98. fi
  99. while true; do
  100. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  101. _debug h "$h"
  102. if [ -z "$h" ]; then
  103. #not valid
  104. return 1
  105. fi
  106. if _contains "${response}" "${h}." >/dev/null; then
  107. _dnszone=$(echo "$response" | _egrep_o "${h}")
  108. if [ "$_dnszone" ]; then
  109. return 0
  110. fi
  111. return 1
  112. fi
  113. i=$(_math "$i" + 1)
  114. done
  115. return 1
  116. }
  117. _udr_rest() {
  118. if [ -n "$2" ]; then
  119. data="command=$1&$2"
  120. else
  121. data="command=$1"
  122. fi
  123. _debug data "${data}"
  124. response="$(_post "${data}" "${UDR_API}?s_login=${UDR_USER}&s_pw=${UDR_PASS}" "" "POST")"
  125. _code=$(echo "$response" | _egrep_o "code = ([0-9]+)" | _head_n 1 | cut -d = -f 2 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  126. _description=$(echo "$response" | _egrep_o "description = .*" | _head_n 1 | cut -d = -f 2 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  127. _debug response_code "$_code"
  128. _debug response_description "$_description"
  129. if [ ! "$_code" = "200" ]; then
  130. _err "DNS-API-Error: $_description"
  131. return 1
  132. fi
  133. return 0
  134. }