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.

132 lines
3.9 KiB

7 years ago
4 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 may be 'domain.duckdns.org' (if using --domain-alias) or '_acme-challenge.domain.duckdns.org'
  79. # either way, return 'domain'. (duckdns does not allow further subdomains and restricts domains to [a-z0-9-].)
  80. _duckdns_get_domain() {
  81. # We'll extract the domain/username from full domain
  82. _duckdns_domain="$(printf "%s" "$fulldomain" | _lower_case | _egrep_o '^(_acme-challenge\.)?[a-z0-9-]*\.duckdns\.org' | sed 's/^\(_acme-challenge\.\)\{0,1\}\([a-z0-9-]*\)\.duckdns\.org/\2/')"
  83. if [ -z "$_duckdns_domain" ]; then
  84. _err "Error extracting the domain."
  85. return 1
  86. fi
  87. return 0
  88. }
  89. #Usage: method URI
  90. _duckdns_rest() {
  91. method=$1
  92. param="$2"
  93. _debug param "$param"
  94. url="$DuckDNS_API?$param"
  95. if [ "$DEBUG" -gt 0 ]; then
  96. url="$url&verbose=true"
  97. fi
  98. _debug url "$url"
  99. # DuckDNS uses GET to update domain info
  100. if [ "$method" = "GET" ]; then
  101. response="$(_get "$url")"
  102. _debug2 response "$response"
  103. if [ "$DEBUG" -gt 0 ] && _contains "$response" "UPDATED" && _contains "$response" "OK"; then
  104. response="OK"
  105. fi
  106. else
  107. _err "Unsupported method"
  108. return 1
  109. fi
  110. return 0
  111. }