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