]>
Commit | Line | Data |
---|---|---|
fa98d72f R |
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 | ||
96801e34 LA |
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 | |
fa98d72f | 12 | |
e7dff475 | 13 | DuckDNS_API="https://www.duckdns.org/update" |
fa98d72f R |
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 | ||
96801e34 | 22 | DuckDNS_Token="${DuckDNS_Token:-$(_readaccountconf_mutable DuckDNS_Token)}" |
63c6ed3f | 23 | if [ -z "$DuckDNS_Token" ]; then |
96801e34 | 24 | _err "You must export variable: DuckDNS_Token" |
fa98d72f R |
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. | |
96801e34 | 31 | _saveaccountconf_mutable DuckDNS_Token "$DuckDNS_Token" |
fa98d72f R |
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 | ||
96801e34 LA |
37 | if ! _duckdns_get_domain; then |
38 | return 1 | |
39 | fi | |
40 | ||
fa98d72f R |
41 | # Now add the TXT record to DuckDNS |
42 | _info "Trying to add TXT record" | |
96801e34 LA |
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 | |
fa98d72f R |
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 | ||
96801e34 LA |
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 | ||
fa98d72f | 76 | # Now remove the TXT record from DuckDNS |
e7dff475 | 77 | _info "Trying to remove TXT record" |
96801e34 LA |
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 | |
fa98d72f R |
86 | else |
87 | _err "Errors happened during removing the TXT record." | |
88 | return 1 | |
89 | fi | |
90 | } | |
91 | ||
92 | #################### Private functions below ################################## | |
93 | ||
4539d236 M |
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-].) | |
96801e34 LA |
96 | _duckdns_get_domain() { |
97 | ||
98 | # We'll extract the domain/username from full domain | |
21718a69 | 99 | _duckdns_domain="$(printf "%s" "$fulldomain" | _lower_case | _egrep_o '^(_acme-challenge\.)?[a-z0-9-]*\.duckdns\.org' | sed 's/^\(_acme-challenge\.\)\?\([a-z0-9-]*\)\.duckdns\.org/\2/')" |
96801e34 LA |
100 | |
101 | if [ -z "$_duckdns_domain" ]; then | |
102 | _err "Error extracting the domain." | |
103 | return 1 | |
104 | fi | |
105 | ||
106 | return 0 | |
107 | } | |
108 | ||
e7dff475 | 109 | #Usage: method URI |
fa98d72f R |
110 | _duckdns_rest() { |
111 | method=$1 | |
112 | param="$2" | |
113 | _debug param "$param" | |
114 | url="$DuckDNS_API?$param" | |
5e3aa2db | 115 | if [ "$DEBUG" -gt 0 ]; then |
116 | url="$url&verbose=true" | |
117 | fi | |
fa98d72f R |
118 | _debug url "$url" |
119 | ||
120 | # DuckDNS uses GET to update domain info | |
d0f5aece | 121 | if [ "$method" = "GET" ]; then |
fa98d72f | 122 | response="$(_get "$url")" |
db24ca3d | 123 | _debug2 response "$response" |
124 | if [ "$DEBUG" -gt 0 ] && _contains "$response" "UPDATED" && _contains "$response" "OK"; then | |
125 | response="OK" | |
126 | fi | |
fa98d72f R |
127 | else |
128 | _err "Unsupported method" | |
129 | return 1 | |
130 | fi | |
fa98d72f R |
131 | return 0 |
132 | } |