]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_duckdns.sh
Merge pull request #973 from mvanini/master
[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=_acme-challenge.domain.duckdns.org
95 #returns
96 # _duckdns_domain=domain
97 _duckdns_get_domain() {
98
99 # We'll extract the domain/username from full domain
100 _duckdns_domain="$(printf "%s" "$fulldomain" | _lower_case | _egrep_o '[.][^.][^.]*[.]duckdns.org' | cut -d . -f 2)"
101
102 if [ -z "$_duckdns_domain" ]; then
103 _err "Error extracting the domain."
104 return 1
105 fi
106
107 return 0
108 }
109
110 #Usage: method URI
111 _duckdns_rest() {
112 method=$1
113 param="$2"
114 _debug param "$param"
115 url="$DuckDNS_API?$param"
116 _debug url "$url"
117
118 # DuckDNS uses GET to update domain info
119 if [ "$method" = "GET" ]; then
120 response="$(_get "$url")"
121 else
122 _err "Unsupported method"
123 return 1
124 fi
125
126 _debug2 response "$response"
127 return 0
128 }