dns_ddnss.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env sh
  2. #Created by RaidenII, to use DuckDNS's API to add/remove text records
  3. #modified by helbgd @ 03/13/2018 to support ddnss.de
  4. #modified by mod242 @ 04/24/2018 to support different ddnss domains
  5. #Please note: the Wildcard Feature must be turned on for the Host record
  6. #and the checkbox for TXT needs to be enabled
  7. # Pass credentials before "acme.sh --issue --dns dns_ddnss ..."
  8. # --
  9. # export DDNSS_Token="aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
  10. # --
  11. #
  12. DDNSS_DNS_API="https://ddnss.de/upd.php"
  13. ######## Public functions #####################
  14. #Usage: dns_ddnss_add _acme-challenge.domain.ddnss.de "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  15. dns_ddnss_add() {
  16. fulldomain=$1
  17. txtvalue=$2
  18. DDNSS_Token="${DDNSS_Token:-$(_readaccountconf_mutable DDNSS_Token)}"
  19. if [ -z "$DDNSS_Token" ]; then
  20. _err "You must export variable: DDNSS_Token"
  21. _err "The token for your DDNSS account is necessary."
  22. _err "You can look it up in your DDNSS account."
  23. return 1
  24. fi
  25. # Now save the credentials.
  26. _saveaccountconf_mutable DDNSS_Token "$DDNSS_Token"
  27. # Unfortunately, DDNSS does not seems to support lookup domain through API
  28. # So I assume your credentials (which are your domain and token) are correct
  29. # If something goes wrong, we will get a KO response from DDNSS
  30. if ! _ddnss_get_domain; then
  31. return 1
  32. fi
  33. # Now add the TXT record to DDNSS DNS
  34. _info "Trying to add TXT record"
  35. if _ddnss_rest GET "key=$DDNSS_Token&host=$_ddnss_domain&txtm=1&txt=$txtvalue"; then
  36. if [ "$response" = "Updated 1 hostname." ]; then
  37. _info "TXT record has been successfully added to your DDNSS domain."
  38. _info "Note that all subdomains under this domain uses the same TXT record."
  39. return 0
  40. else
  41. _err "Errors happened during adding the TXT record, response=$response"
  42. return 1
  43. fi
  44. else
  45. _err "Errors happened during adding the TXT record."
  46. return 1
  47. fi
  48. }
  49. #Usage: fulldomain txtvalue
  50. #Remove the txt record after validation.
  51. dns_ddnss_rm() {
  52. fulldomain=$1
  53. txtvalue=$2
  54. DDNSS_Token="${DDNSS_Token:-$(_readaccountconf_mutable DDNSS_Token)}"
  55. if [ -z "$DDNSS_Token" ]; then
  56. _err "You must export variable: DDNSS_Token"
  57. _err "The token for your DDNSS account is necessary."
  58. _err "You can look it up in your DDNSS account."
  59. return 1
  60. fi
  61. if ! _ddnss_get_domain; then
  62. return 1
  63. fi
  64. # Now remove the TXT record from DDNS DNS
  65. _info "Trying to remove TXT record"
  66. if _ddnss_rest GET "key=$DDNSS_Token&host=$_ddnss_domain&txtm=1&txt=."; then
  67. if [ "$response" = "Updated 1 hostname." ]; then
  68. _info "TXT record has been successfully removed from your DDNSS domain."
  69. return 0
  70. else
  71. _err "Errors happened during removing the TXT record, response=$response"
  72. return 1
  73. fi
  74. else
  75. _err "Errors happened during removing the TXT record."
  76. return 1
  77. fi
  78. }
  79. #################### Private functions below ##################################
  80. #fulldomain=_acme-challenge.domain.ddnss.de
  81. #returns
  82. # _ddnss_domain=domain
  83. _ddnss_get_domain() {
  84. # We'll extract the domain/username from full domain
  85. _ddnss_domain="$(echo "$fulldomain" | _lower_case | _egrep_o '[.][^.][^.]*[.](ddnss|dyn-ip24|dyndns|dyn|dyndns1|home-webserver|myhome-server|dynip)\..*' | cut -d . -f 2-)"
  86. if [ -z "$_ddnss_domain" ]; then
  87. _err "Error extracting the domain."
  88. return 1
  89. fi
  90. return 0
  91. }
  92. #Usage: method URI
  93. _ddnss_rest() {
  94. method=$1
  95. param="$2"
  96. _debug param "$param"
  97. url="$DDNSS_DNS_API?$param"
  98. _debug url "$url"
  99. # DDNSS uses GET to update domain info
  100. if [ "$method" = "GET" ]; then
  101. response="$(_get "$url" | sed 's/<[a-zA-Z\/][^>]*>//g' | _tail_n 1)"
  102. else
  103. _err "Unsupported method"
  104. return 1
  105. fi
  106. _debug2 response "$response"
  107. return 0
  108. }