]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_inwx.sh
Merge pull request #2802 from luoch/patch-1
[mirror_acme.sh.git] / dnsapi / dns_inwx.sh
1 #!/usr/bin/env sh
2
3 #
4 #INWX_User="username"
5 #
6 #INWX_Password="password"
7 #
8 # Dependencies:
9 # -------------
10 # - oathtool (When using 2 Factor Authentication)
11
12 INWX_Api="https://api.domrobot.com/xmlrpc/"
13
14 ######## Public functions #####################
15
16 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
17 dns_inwx_add() {
18 fulldomain=$1
19 txtvalue=$2
20
21 INWX_User="${INWX_User:-$(_readaccountconf_mutable INWX_User)}"
22 INWX_Password="${INWX_Password:-$(_readaccountconf_mutable INWX_Password)}"
23 INWX_Shared_Secret="${INWX_Shared_Secret:-$(_readaccountconf_mutable INWX_Shared_Secret)}"
24 if [ -z "$INWX_User" ] || [ -z "$INWX_Password" ]; then
25 INWX_User=""
26 INWX_Password=""
27 _err "You don't specify inwx user and password yet."
28 _err "Please create you key and try again."
29 return 1
30 fi
31
32 #save the api key and email to the account conf file.
33 _saveaccountconf_mutable INWX_User "$INWX_User"
34 _saveaccountconf_mutable INWX_Password "$INWX_Password"
35 _saveaccountconf_mutable INWX_Shared_Secret "$INWX_Shared_Secret"
36
37 _debug "First detect the root zone"
38 if ! _get_root "$fulldomain"; then
39 _err "invalid domain"
40 return 1
41 fi
42 _debug _sub_domain "$_sub_domain"
43 _debug _domain "$_domain"
44
45 _info "Adding record"
46 _inwx_add_record "$_domain" "$_sub_domain" "$txtvalue"
47
48 }
49
50 #fulldomain txtvalue
51 dns_inwx_rm() {
52
53 fulldomain=$1
54 txtvalue=$2
55
56 INWX_User="${INWX_User:-$(_readaccountconf_mutable INWX_User)}"
57 INWX_Password="${INWX_Password:-$(_readaccountconf_mutable INWX_Password)}"
58 INWX_Shared_Secret="${INWX_Shared_Secret:-$(_readaccountconf_mutable INWX_Shared_Secret)}"
59 if [ -z "$INWX_User" ] || [ -z "$INWX_Password" ]; then
60 INWX_User=""
61 INWX_Password=""
62 _err "You don't specify inwx user and password yet."
63 _err "Please create you key and try again."
64 return 1
65 fi
66
67 _debug "First detect the root zone"
68 if ! _get_root "$fulldomain"; then
69 _err "invalid domain"
70 return 1
71 fi
72 _debug _sub_domain "$_sub_domain"
73 _debug _domain "$_domain"
74
75 _debug "Getting txt records"
76
77 xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
78 <methodCall>
79 <methodName>nameserver.info</methodName>
80 <params>
81 <param>
82 <value>
83 <struct>
84 <member>
85 <name>domain</name>
86 <value>
87 <string>%s</string>
88 </value>
89 </member>
90 <member>
91 <name>type</name>
92 <value>
93 <string>TXT</string>
94 </value>
95 </member>
96 <member>
97 <name>name</name>
98 <value>
99 <string>%s</string>
100 </value>
101 </member>
102 </struct>
103 </value>
104 </param>
105 </params>
106 </methodCall>' "$_domain" "$_sub_domain")
107 response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
108
109 if ! _contains "$response" "Command completed successfully"; then
110 _err "Error could not get txt records"
111 return 1
112 fi
113
114 if ! printf "%s" "$response" | grep "count" >/dev/null; then
115 _info "Do not need to delete record"
116 else
117 _record_id=$(printf '%s' "$response" | _egrep_o '.*(<member><name>record){1}(.*)([0-9]+){1}' | _egrep_o '<name>id<\/name><value><int>[0-9]+' | _egrep_o '[0-9]+')
118 _info "Deleting record"
119 _inwx_delete_record "$_record_id"
120 fi
121
122 }
123
124 #################### Private functions below ##################################
125
126 _inwx_login() {
127
128 xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
129 <methodCall>
130 <methodName>account.login</methodName>
131 <params>
132 <param>
133 <value>
134 <struct>
135 <member>
136 <name>user</name>
137 <value>
138 <string>%s</string>
139 </value>
140 </member>
141 <member>
142 <name>pass</name>
143 <value>
144 <string>%s</string>
145 </value>
146 </member>
147 </struct>
148 </value>
149 </param>
150 </params>
151 </methodCall>' $INWX_User $INWX_Password)
152
153 response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
154 _H1=$(printf "Cookie: %s" "$(grep "domrobot=" "$HTTP_HEADER" | grep "^Set-Cookie:" | _tail_n 1 | _egrep_o 'domrobot=[^;]*;' | tr -d ';')")
155 export _H1
156
157 #https://github.com/inwx/php-client/blob/master/INWX/Domrobot.php#L71
158 if _contains "$response" "<member><name>code</name><value><int>1000</int></value></member>" \
159 && _contains "$response" "<member><name>tfa</name><value><string>GOOGLE-AUTH</string></value></member>"; then
160 if [ -z "$INWX_Shared_Secret" ]; then
161 _err "Mobile TAN detected."
162 _err "Please define a shared secret."
163 return 1
164 fi
165
166 if ! _exists oathtool; then
167 _err "Please install oathtool to use 2 Factor Authentication."
168 _err ""
169 return 1
170 fi
171
172 tan="$(oathtool --base32 --totp "${INWX_Shared_Secret}" 2>/dev/null)"
173
174 xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
175 <methodCall>
176 <methodName>account.unlock</methodName>
177 <params>
178 <param>
179 <value>
180 <struct>
181 <member>
182 <name>tan</name>
183 <value>
184 <string>%s</string>
185 </value>
186 </member>
187 </struct>
188 </value>
189 </param>
190 </params>
191 </methodCall>' "$tan")
192
193 response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
194 fi
195
196 }
197
198 _get_root() {
199 domain=$1
200 _debug "get root"
201
202 domain=$1
203 i=2
204 p=1
205
206 _inwx_login
207
208 xml_content='<?xml version="1.0" encoding="UTF-8"?>
209 <methodCall>
210 <methodName>nameserver.list</methodName>
211 </methodCall>'
212
213 response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
214 while true; do
215 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
216 _debug h "$h"
217 if [ -z "$h" ]; then
218 #not valid
219 return 1
220 fi
221
222 if _contains "$response" "$h"; then
223 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
224 _domain="$h"
225 return 0
226 fi
227 p=$i
228 i=$(_math "$i" + 1)
229 done
230 return 1
231
232 }
233
234 _inwx_delete_record() {
235 record_id=$1
236 xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
237 <methodCall>
238 <methodName>nameserver.deleteRecord</methodName>
239 <params>
240 <param>
241 <value>
242 <struct>
243 <member>
244 <name>id</name>
245 <value>
246 <int>%s</int>
247 </value>
248 </member>
249 </struct>
250 </value>
251 </param>
252 </params>
253 </methodCall>' "$record_id")
254
255 response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
256
257 if ! printf "%s" "$response" | grep "Command completed successfully" >/dev/null; then
258 _err "Error"
259 return 1
260 fi
261 return 0
262
263 }
264
265 _inwx_update_record() {
266 record_id=$1
267 txtval=$2
268 xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
269 <methodCall>
270 <methodName>nameserver.updateRecord</methodName>
271 <params>
272 <param>
273 <value>
274 <struct>
275 <member>
276 <name>content</name>
277 <value>
278 <string>%s</string>
279 </value>
280 </member>
281 <member>
282 <name>id</name>
283 <value>
284 <int>%s</int>
285 </value>
286 </member>
287 </struct>
288 </value>
289 </param>
290 </params>
291 </methodCall>' "$txtval" "$record_id")
292
293 response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
294
295 if ! printf "%s" "$response" | grep "Command completed successfully" >/dev/null; then
296 _err "Error"
297 return 1
298 fi
299 return 0
300
301 }
302
303 _inwx_add_record() {
304
305 domain=$1
306 sub_domain=$2
307 txtval=$3
308
309 xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
310 <methodCall>
311 <methodName>nameserver.createRecord</methodName>
312 <params>
313 <param>
314 <value>
315 <struct>
316 <member>
317 <name>domain</name>
318 <value>
319 <string>%s</string>
320 </value>
321 </member>
322 <member>
323 <name>type</name>
324 <value>
325 <string>TXT</string>
326 </value>
327 </member>
328 <member>
329 <name>content</name>
330 <value>
331 <string>%s</string>
332 </value>
333 </member>
334 <member>
335 <name>name</name>
336 <value>
337 <string>%s</string>
338 </value>
339 </member>
340 </struct>
341 </value>
342 </param>
343 </params>
344 </methodCall>' "$domain" "$txtval" "$sub_domain")
345
346 response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
347
348 if ! printf "%s" "$response" | grep "Command completed successfully" >/dev/null; then
349 _err "Error"
350 return 1
351 fi
352 return 0
353 }