]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_dyn.sh
Merge pull request #4658 from Justman10000/master
[mirror_acme.sh.git] / dnsapi / dns_dyn.sh
1 #!/usr/bin/env sh
2 #
3 # Dyn.com Domain API
4 #
5 # Author: Gerd Naschenweng
6 # https://github.com/magicdude4eva
7 #
8 # Dyn Managed DNS API
9 # https://help.dyn.com/dns-api-knowledge-base/
10 #
11 # It is recommended to add a "Dyn Managed DNS" user specific for API access.
12 # The "Zones & Records Permissions" required by this script are:
13 # --
14 # RecordAdd
15 # RecordUpdate
16 # RecordDelete
17 # RecordGet
18 # ZoneGet
19 # ZoneAddNode
20 # ZoneRemoveNode
21 # ZonePublish
22 # --
23 #
24 # Pass credentials before "acme.sh --issue --dns dns_dyn ..."
25 # --
26 # export DYN_Customer="customer"
27 # export DYN_Username="apiuser"
28 # export DYN_Password="secret"
29 # --
30
31 DYN_API="https://api.dynect.net/REST"
32
33 #REST_API
34 ######## Public functions #####################
35
36 #Usage: add _acme-challenge.www.domain.com "Challenge-code"
37 dns_dyn_add() {
38 fulldomain="$1"
39 txtvalue="$2"
40
41 DYN_Customer="${DYN_Customer:-$(_readaccountconf_mutable DYN_Customer)}"
42 DYN_Username="${DYN_Username:-$(_readaccountconf_mutable DYN_Username)}"
43 DYN_Password="${DYN_Password:-$(_readaccountconf_mutable DYN_Password)}"
44 if [ -z "$DYN_Customer" ] || [ -z "$DYN_Username" ] || [ -z "$DYN_Password" ]; then
45 DYN_Customer=""
46 DYN_Username=""
47 DYN_Password=""
48 _err "You must export variables: DYN_Customer, DYN_Username and DYN_Password"
49 return 1
50 fi
51
52 #save the config variables to the account conf file.
53 _saveaccountconf_mutable DYN_Customer "$DYN_Customer"
54 _saveaccountconf_mutable DYN_Username "$DYN_Username"
55 _saveaccountconf_mutable DYN_Password "$DYN_Password"
56
57 if ! _dyn_get_authtoken; then
58 return 1
59 fi
60
61 if [ -z "$_dyn_authtoken" ]; then
62 _dyn_end_session
63 return 1
64 fi
65
66 if ! _dyn_get_zone; then
67 _dyn_end_session
68 return 1
69 fi
70
71 if ! _dyn_add_record; then
72 _dyn_end_session
73 return 1
74 fi
75
76 if ! _dyn_publish_zone; then
77 _dyn_end_session
78 return 1
79 fi
80
81 _dyn_end_session
82
83 return 0
84 }
85
86 #Usage: fulldomain txtvalue
87 #Remove the txt record after validation.
88 dns_dyn_rm() {
89 fulldomain="$1"
90 txtvalue="$2"
91
92 DYN_Customer="${DYN_Customer:-$(_readaccountconf_mutable DYN_Customer)}"
93 DYN_Username="${DYN_Username:-$(_readaccountconf_mutable DYN_Username)}"
94 DYN_Password="${DYN_Password:-$(_readaccountconf_mutable DYN_Password)}"
95 if [ -z "$DYN_Customer" ] || [ -z "$DYN_Username" ] || [ -z "$DYN_Password" ]; then
96 DYN_Customer=""
97 DYN_Username=""
98 DYN_Password=""
99 _err "You must export variables: DYN_Customer, DYN_Username and DYN_Password"
100 return 1
101 fi
102
103 if ! _dyn_get_authtoken; then
104 return 1
105 fi
106
107 if [ -z "$_dyn_authtoken" ]; then
108 _dyn_end_session
109 return 1
110 fi
111
112 if ! _dyn_get_zone; then
113 _dyn_end_session
114 return 1
115 fi
116
117 if ! _dyn_get_record_id; then
118 _dyn_end_session
119 return 1
120 fi
121
122 if [ -z "$_dyn_record_id" ]; then
123 _dyn_end_session
124 return 1
125 fi
126
127 if ! _dyn_rm_record; then
128 _dyn_end_session
129 return 1
130 fi
131
132 if ! _dyn_publish_zone; then
133 _dyn_end_session
134 return 1
135 fi
136
137 _dyn_end_session
138
139 return 0
140 }
141
142 #################### Private functions below ##################################
143
144 #get Auth-Token
145 _dyn_get_authtoken() {
146
147 _info "Start Dyn API Session"
148
149 data="{\"customer_name\":\"$DYN_Customer\", \"user_name\":\"$DYN_Username\", \"password\":\"$DYN_Password\"}"
150 dyn_url="$DYN_API/Session/"
151 method="POST"
152
153 _debug data "$data"
154 _debug dyn_url "$dyn_url"
155
156 export _H1="Content-Type: application/json"
157
158 response="$(_post "$data" "$dyn_url" "" "$method")"
159 sessionstatus="$(printf "%s\n" "$response" | _egrep_o '"status" *: *"[^"]*' | _head_n 1 | sed 's#^"status" *: *"##')"
160
161 _debug response "$response"
162 _debug sessionstatus "$sessionstatus"
163
164 if [ "$sessionstatus" = "success" ]; then
165 _dyn_authtoken="$(printf "%s\n" "$response" | _egrep_o '"token" *: *"[^"]*' | _head_n 1 | sed 's#^"token" *: *"##')"
166 _info "Token received"
167 _debug _dyn_authtoken "$_dyn_authtoken"
168 return 0
169 fi
170
171 _dyn_authtoken=""
172 _err "get token failed"
173 return 1
174 }
175
176 #fulldomain=_acme-challenge.www.domain.com
177 #returns
178 # _dyn_zone=domain.com
179 _dyn_get_zone() {
180 i=2
181 while true; do
182 domain="$(printf "%s" "$fulldomain" | cut -d . -f "$i-100")"
183 if [ -z "$domain" ]; then
184 break
185 fi
186
187 dyn_url="$DYN_API/Zone/$domain/"
188
189 export _H1="Auth-Token: $_dyn_authtoken"
190 export _H2="Content-Type: application/json"
191
192 response="$(_get "$dyn_url" "" "")"
193 sessionstatus="$(printf "%s\n" "$response" | _egrep_o '"status" *: *"[^"]*' | _head_n 1 | sed 's#^"status" *: *"##')"
194
195 _debug dyn_url "$dyn_url"
196 _debug response "$response"
197 _debug sessionstatus "$sessionstatus"
198
199 if [ "$sessionstatus" = "success" ]; then
200 _dyn_zone="$domain"
201 return 0
202 fi
203 i=$(_math "$i" + 1)
204 done
205
206 _dyn_zone=""
207 _err "get zone failed"
208 return 1
209 }
210
211 #add TXT record
212 _dyn_add_record() {
213
214 _info "Adding TXT record"
215
216 data="{\"rdata\":{\"txtdata\":\"$txtvalue\"},\"ttl\":\"300\"}"
217 dyn_url="$DYN_API/TXTRecord/$_dyn_zone/$fulldomain/"
218 method="POST"
219
220 export _H1="Auth-Token: $_dyn_authtoken"
221 export _H2="Content-Type: application/json"
222
223 response="$(_post "$data" "$dyn_url" "" "$method")"
224 sessionstatus="$(printf "%s\n" "$response" | _egrep_o '"status" *: *"[^"]*' | _head_n 1 | sed 's#^"status" *: *"##')"
225
226 _debug response "$response"
227 _debug sessionstatus "$sessionstatus"
228
229 if [ "$sessionstatus" = "success" ]; then
230 _info "TXT Record successfully added"
231 return 0
232 fi
233
234 _err "add TXT record failed"
235 return 1
236 }
237
238 #publish the zone
239 _dyn_publish_zone() {
240
241 _info "Publishing zone"
242
243 data="{\"publish\":\"true\"}"
244 dyn_url="$DYN_API/Zone/$_dyn_zone/"
245 method="PUT"
246
247 export _H1="Auth-Token: $_dyn_authtoken"
248 export _H2="Content-Type: application/json"
249
250 response="$(_post "$data" "$dyn_url" "" "$method")"
251 sessionstatus="$(printf "%s\n" "$response" | _egrep_o '"status" *: *"[^"]*' | _head_n 1 | sed 's#^"status" *: *"##')"
252
253 _debug response "$response"
254 _debug sessionstatus "$sessionstatus"
255
256 if [ "$sessionstatus" = "success" ]; then
257 _info "Zone published"
258 return 0
259 fi
260
261 _err "publish zone failed"
262 return 1
263 }
264
265 #get record_id of TXT record so we can delete the record
266 _dyn_get_record_id() {
267
268 _info "Getting record_id of TXT record"
269
270 dyn_url="$DYN_API/TXTRecord/$_dyn_zone/$fulldomain/"
271
272 export _H1="Auth-Token: $_dyn_authtoken"
273 export _H2="Content-Type: application/json"
274
275 response="$(_get "$dyn_url" "" "")"
276 sessionstatus="$(printf "%s\n" "$response" | _egrep_o '"status" *: *"[^"]*' | _head_n 1 | sed 's#^"status" *: *"##')"
277
278 _debug response "$response"
279 _debug sessionstatus "$sessionstatus"
280
281 if [ "$sessionstatus" = "success" ]; then
282 _dyn_record_id="$(printf "%s\n" "$response" | _egrep_o "\"data\" *: *\[\"/REST/TXTRecord/$_dyn_zone/$fulldomain/[^\"]*" | _head_n 1 | sed "s#^\"data\" *: *\[\"/REST/TXTRecord/$_dyn_zone/$fulldomain/##")"
283 _debug _dyn_record_id "$_dyn_record_id"
284 return 0
285 fi
286
287 _dyn_record_id=""
288 _err "getting record_id failed"
289 return 1
290 }
291
292 #delete TXT record
293 _dyn_rm_record() {
294
295 _info "Deleting TXT record"
296
297 dyn_url="$DYN_API/TXTRecord/$_dyn_zone/$fulldomain/$_dyn_record_id/"
298 method="DELETE"
299
300 _debug dyn_url "$dyn_url"
301
302 export _H1="Auth-Token: $_dyn_authtoken"
303 export _H2="Content-Type: application/json"
304
305 response="$(_post "" "$dyn_url" "" "$method")"
306 sessionstatus="$(printf "%s\n" "$response" | _egrep_o '"status" *: *"[^"]*' | _head_n 1 | sed 's#^"status" *: *"##')"
307
308 _debug response "$response"
309 _debug sessionstatus "$sessionstatus"
310
311 if [ "$sessionstatus" = "success" ]; then
312 _info "TXT record successfully deleted"
313 return 0
314 fi
315
316 _err "delete TXT record failed"
317 return 1
318 }
319
320 #logout
321 _dyn_end_session() {
322
323 _info "End Dyn API Session"
324
325 dyn_url="$DYN_API/Session/"
326 method="DELETE"
327
328 _debug dyn_url "$dyn_url"
329
330 export _H1="Auth-Token: $_dyn_authtoken"
331 export _H2="Content-Type: application/json"
332
333 response="$(_post "" "$dyn_url" "" "$method")"
334
335 _debug response "$response"
336
337 _dyn_authtoken=""
338 return 0
339 }