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