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.

133 lines
3.7 KiB

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