]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_dynu.sh
Merge pull request #4542 from alexleigh/master
[mirror_acme.sh.git] / dnsapi / dns_dynu.sh
1 #!/usr/bin/env sh
2
3 #Client ID
4 #Dynu_ClientId="0b71cae7-a099-4f6b-8ddf-94571cdb760d"
5 #
6 #Secret
7 #Dynu_Secret="aCUEY4BDCV45KI8CSIC3sp2LKQ9"
8 #
9 #Token
10 Dynu_Token=""
11 #
12 #Endpoint
13 Dynu_EndPoint="https://api.dynu.com/v2"
14 #
15 #Author: Dynu Systems, Inc.
16 #Report Bugs here: https://github.com/shar0119/acme.sh
17 #
18 ######## Public functions #####################
19
20 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
21 dns_dynu_add() {
22 fulldomain=$1
23 txtvalue=$2
24
25 if [ -z "$Dynu_ClientId" ] || [ -z "$Dynu_Secret" ]; then
26 Dynu_ClientId=""
27 Dynu_Secret=""
28 _err "Dynu client id and secret is not specified."
29 _err "Please create you API client id and secret and try again."
30 return 1
31 fi
32
33 #save the client id and secret to the account conf file.
34 _saveaccountconf Dynu_ClientId "$Dynu_ClientId"
35 _saveaccountconf Dynu_Secret "$Dynu_Secret"
36
37 if [ -z "$Dynu_Token" ]; then
38 _info "Getting Dynu token."
39 if ! _dynu_authentication; then
40 _err "Can not get token."
41 fi
42 fi
43
44 _debug "Detect root zone"
45 if ! _get_root "$fulldomain"; then
46 _err "Invalid domain."
47 return 1
48 fi
49
50 _debug _node "$_node"
51 _debug _domain_name "$_domain_name"
52
53 _info "Creating TXT record."
54 if ! _dynu_rest POST "dns/$dnsId/record" "{\"domainId\":\"$dnsId\",\"nodeName\":\"$_node\",\"recordType\":\"TXT\",\"textData\":\"$txtvalue\",\"state\":true,\"ttl\":90}"; then
55 return 1
56 fi
57
58 if ! _contains "$response" "200"; then
59 _err "Could not add TXT record."
60 return 1
61 fi
62
63 return 0
64 }
65
66 #Usage: rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
67 dns_dynu_rm() {
68 fulldomain=$1
69 txtvalue=$2
70
71 if [ -z "$Dynu_ClientId" ] || [ -z "$Dynu_Secret" ]; then
72 Dynu_ClientId=""
73 Dynu_Secret=""
74 _err "Dynu client id and secret is not specified."
75 _err "Please create you API client id and secret and try again."
76 return 1
77 fi
78
79 #save the client id and secret to the account conf file.
80 _saveaccountconf Dynu_ClientId "$Dynu_ClientId"
81 _saveaccountconf Dynu_Secret "$Dynu_Secret"
82
83 if [ -z "$Dynu_Token" ]; then
84 _info "Getting Dynu token."
85 if ! _dynu_authentication; then
86 _err "Can not get token."
87 fi
88 fi
89
90 _debug "Detect root zone."
91 if ! _get_root "$fulldomain"; then
92 _err "Invalid domain."
93 return 1
94 fi
95
96 _debug _node "$_node"
97 _debug _domain_name "$_domain_name"
98
99 _info "Checking for TXT record."
100 if ! _get_recordid "$fulldomain" "$txtvalue"; then
101 _err "Could not get TXT record id."
102 return 1
103 fi
104
105 if [ "$_dns_record_id" = "" ]; then
106 _err "TXT record not found."
107 return 1
108 fi
109
110 _info "Removing TXT record."
111 if ! _delete_txt_record "$_dns_record_id"; then
112 _err "Could not remove TXT record $_dns_record_id."
113 fi
114
115 return 0
116 }
117
118 ######## Private functions below ##################################
119 #_acme-challenge.www.domain.com
120 #returns
121 # _node=_acme-challenge.www
122 # _domain_name=domain.com
123 _get_root() {
124 domain=$1
125 i=2
126 p=1
127 while true; do
128 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
129 _debug h "$h"
130 if [ -z "$h" ]; then
131 #not valid
132 return 1
133 fi
134
135 if ! _dynu_rest GET "dns/getroot/$h"; then
136 return 1
137 fi
138
139 if _contains "$response" "\"domainName\":\"$h\"" >/dev/null; then
140 dnsId=$(printf "%s" "$response" | tr -d "{}" | cut -d , -f 2 | cut -d : -f 2)
141 _domain_name=$h
142 _node=$(printf "%s" "$domain" | cut -d . -f 1-$p)
143 return 0
144 fi
145 p=$i
146 i=$(_math "$i" + 1)
147 done
148 return 1
149
150 }
151
152 _get_recordid() {
153 fulldomain=$1
154 txtvalue=$2
155
156 if ! _dynu_rest GET "dns/$dnsId/record"; then
157 return 1
158 fi
159
160 if ! _contains "$response" "$txtvalue"; then
161 _dns_record_id=0
162 return 0
163 fi
164
165 _dns_record_id=$(printf "%s" "$response" | sed -e 's/[^{]*\({[^}]*}\)[^{]*/\1\n/g' | grep "\"textData\":\"$txtvalue\"" | sed -e 's/.*"id":\([^,]*\).*/\1/')
166 return 0
167 }
168
169 _delete_txt_record() {
170 _dns_record_id=$1
171
172 if ! _dynu_rest DELETE "dns/$dnsId/record/$_dns_record_id"; then
173 return 1
174 fi
175
176 if ! _contains "$response" "200"; then
177 return 1
178 fi
179
180 return 0
181 }
182
183 _dynu_rest() {
184 m=$1
185 ep="$2"
186 data="$3"
187 _debug "$ep"
188
189 export _H1="Authorization: Bearer $Dynu_Token"
190 export _H2="Content-Type: application/json"
191
192 if [ "$data" ] || [ "$m" = "DELETE" ]; then
193 _debug data "$data"
194 response="$(_post "$data" "$Dynu_EndPoint/$ep" "" "$m")"
195 else
196 _info "Getting $Dynu_EndPoint/$ep"
197 response="$(_get "$Dynu_EndPoint/$ep")"
198 fi
199
200 if [ "$?" != "0" ]; then
201 _err "error $ep"
202 return 1
203 fi
204 _debug2 response "$response"
205 return 0
206 }
207
208 _dynu_authentication() {
209 realm="$(printf "%s" "$Dynu_ClientId:$Dynu_Secret" | _base64)"
210
211 export _H1="Authorization: Basic $realm"
212 export _H2="Content-Type: application/json"
213
214 response="$(_get "$Dynu_EndPoint/oauth2/token")"
215 if [ "$?" != "0" ]; then
216 _err "Authentication failed."
217 return 1
218 fi
219 if _contains "$response" "Authentication Exception"; then
220 _err "Authentication failed."
221 return 1
222 fi
223 if _contains "$response" "access_token"; then
224 Dynu_Token=$(printf "%s" "$response" | tr -d "{}" | cut -d , -f 1 | cut -d : -f 2 | cut -d '"' -f 2)
225 fi
226 if _contains "$Dynu_Token" "null"; then
227 Dynu_Token=""
228 fi
229
230 _debug2 response "$response"
231 return 0
232 }