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.

128 lines
3.5 KiB

8 years ago
  1. #!/usr/bin/env sh
  2. #Created by RaidenII, to use DuckDNS's API to add/remove text records
  3. #06/27/2017
  4. # Pass credentials before "acme.sh --issue --dns dns_duckdns ..."
  5. # --
  6. # export DuckDNS_Token="aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
  7. # --
  8. #
  9. # Due to the fact that DuckDNS uses StartSSL as cert provider, --insecure may need to be used with acme.sh
  10. DuckDNS_API="https://www.duckdns.org/update"
  11. ######## Public functions #####################
  12. #Usage: dns_duckdns_add _acme-challenge.domain.duckdns.org "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. dns_duckdns_add() {
  14. fulldomain=$1
  15. txtvalue=$2
  16. DuckDNS_Token="${DuckDNS_Token:-$(_readaccountconf_mutable DuckDNS_Token)}"
  17. if [ -z "$DuckDNS_Token" ]; then
  18. _err "You must export variable: DuckDNS_Token"
  19. _err "The token for your DuckDNS account is necessary."
  20. _err "You can look it up in your DuckDNS account."
  21. return 1
  22. fi
  23. # Now save the credentials.
  24. _saveaccountconf_mutable DuckDNS_Token "$DuckDNS_Token"
  25. # Unfortunately, DuckDNS does not seems to support lookup domain through API
  26. # So I assume your credentials (which are your domain and token) are correct
  27. # If something goes wrong, we will get a KO response from DuckDNS
  28. if ! _duckdns_get_domain; then
  29. return 1
  30. fi
  31. # Now add the TXT record to DuckDNS
  32. _info "Trying to add TXT record"
  33. if _duckdns_rest GET "domains=$_duckdns_domain&token=$DuckDNS_Token&txt=$txtvalue"; then
  34. if [ "$response" = "OK" ]; then
  35. _info "TXT record has been successfully added to your DuckDNS domain."
  36. _info "Note that all subdomains under this domain uses the same TXT record."
  37. return 0
  38. else
  39. _err "Errors happened during adding the TXT record, response=$response"
  40. return 1
  41. fi
  42. else
  43. _err "Errors happened during adding the TXT record."
  44. return 1
  45. fi
  46. }
  47. #Usage: fulldomain txtvalue
  48. #Remove the txt record after validation.
  49. dns_duckdns_rm() {
  50. fulldomain=$1
  51. txtvalue=$2
  52. DuckDNS_Token="${DuckDNS_Token:-$(_readaccountconf_mutable DuckDNS_Token)}"
  53. if [ -z "$DuckDNS_Token" ]; then
  54. _err "You must export variable: DuckDNS_Token"
  55. _err "The token for your DuckDNS account is necessary."
  56. _err "You can look it up in your DuckDNS account."
  57. return 1
  58. fi
  59. if ! _duckdns_get_domain; then
  60. return 1
  61. fi
  62. # Now remove the TXT record from DuckDNS
  63. _info "Trying to remove TXT record"
  64. if _duckdns_rest GET "domains=$_duckdns_domain&token=$DuckDNS_Token&txt=&clear=true"; then
  65. if [ "$response" = "OK" ]; then
  66. _info "TXT record has been successfully removed from your DuckDNS domain."
  67. return 0
  68. else
  69. _err "Errors happened during removing the TXT record, response=$response"
  70. return 1
  71. fi
  72. else
  73. _err "Errors happened during removing the TXT record."
  74. return 1
  75. fi
  76. }
  77. #################### Private functions below ##################################
  78. #fulldomain=_acme-challenge.domain.duckdns.org
  79. #returns
  80. # _duckdns_domain=domain
  81. _duckdns_get_domain() {
  82. # We'll extract the domain/username from full domain
  83. _duckdns_domain="$(printf "%s" "$fulldomain" | _lower_case | _egrep_o '[.][^.][^.]*[.]duckdns.org' | cut -d . -f 2)"
  84. if [ -z "$_duckdns_domain" ]; then
  85. _err "Error extracting the domain."
  86. return 1
  87. fi
  88. return 0
  89. }
  90. #Usage: method URI
  91. _duckdns_rest() {
  92. method=$1
  93. param="$2"
  94. _debug param "$param"
  95. url="$DuckDNS_API?$param"
  96. _debug url "$url"
  97. # DuckDNS uses GET to update domain info
  98. if [ "$method" = "GET" ]; then
  99. response="$(_get "$url")"
  100. else
  101. _err "Unsupported method"
  102. return 1
  103. fi
  104. _debug2 response "$response"
  105. return 0
  106. }