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