]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_autodns.sh
Merge pull request #4658 from Justman10000/master
[mirror_acme.sh.git] / dnsapi / dns_autodns.sh
1 #!/usr/bin/env sh
2 # -*- mode: sh; tab-width: 2; indent-tabs-mode: s; coding: utf-8 -*-
3
4 # This is the InternetX autoDNS xml api wrapper for acme.sh
5 # Author: auerswald@gmail.com
6 # Created: 2018-01-14
7 #
8 # export AUTODNS_USER="username"
9 # export AUTODNS_PASSWORD="password"
10 # export AUTODNS_CONTEXT="context"
11 #
12 # Usage:
13 # acme.sh --issue --dns dns_autodns -d example.com
14
15 AUTODNS_API="https://gateway.autodns.com"
16
17 # Arguments:
18 # txtdomain
19 # txt
20 dns_autodns_add() {
21 fulldomain="$1"
22 txtvalue="$2"
23
24 AUTODNS_USER="${AUTODNS_USER:-$(_readaccountconf_mutable AUTODNS_USER)}"
25 AUTODNS_PASSWORD="${AUTODNS_PASSWORD:-$(_readaccountconf_mutable AUTODNS_PASSWORD)}"
26 AUTODNS_CONTEXT="${AUTODNS_CONTEXT:-$(_readaccountconf_mutable AUTODNS_CONTEXT)}"
27
28 if [ -z "$AUTODNS_USER" ] || [ -z "$AUTODNS_CONTEXT" ] || [ -z "$AUTODNS_PASSWORD" ]; then
29 _err "You don't specify autodns user, password and context."
30 return 1
31 fi
32
33 _saveaccountconf_mutable AUTODNS_USER "$AUTODNS_USER"
34 _saveaccountconf_mutable AUTODNS_PASSWORD "$AUTODNS_PASSWORD"
35 _saveaccountconf_mutable AUTODNS_CONTEXT "$AUTODNS_CONTEXT"
36
37 _debug "First detect the root zone"
38
39 if ! _get_autodns_zone "$fulldomain"; then
40 _err "invalid domain"
41 return 1
42 fi
43
44 _debug _sub_domain "$_sub_domain"
45 _debug _zone "$_zone"
46 _debug _system_ns "$_system_ns"
47
48 _info "Adding TXT record"
49
50 autodns_response="$(_autodns_zone_update "$_zone" "$_sub_domain" "$txtvalue" "$_system_ns")"
51
52 if [ "$?" -eq "0" ]; then
53 _info "Added, OK"
54 return 0
55 fi
56
57 return 1
58 }
59
60 # Arguments:
61 # txtdomain
62 # txt
63 dns_autodns_rm() {
64 fulldomain="$1"
65 txtvalue="$2"
66
67 AUTODNS_USER="${AUTODNS_USER:-$(_readaccountconf_mutable AUTODNS_USER)}"
68 AUTODNS_PASSWORD="${AUTODNS_PASSWORD:-$(_readaccountconf_mutable AUTODNS_PASSWORD)}"
69 AUTODNS_CONTEXT="${AUTODNS_CONTEXT:-$(_readaccountconf_mutable AUTODNS_CONTEXT)}"
70
71 if [ -z "$AUTODNS_USER" ] || [ -z "$AUTODNS_CONTEXT" ] || [ -z "$AUTODNS_PASSWORD" ]; then
72 _err "You don't specify autodns user, password and context."
73 return 1
74 fi
75
76 _debug "First detect the root zone"
77
78 if ! _get_autodns_zone "$fulldomain"; then
79 _err "zone not found"
80 return 1
81 fi
82
83 _debug _sub_domain "$_sub_domain"
84 _debug _zone "$_zone"
85 _debug _system_ns "$_system_ns"
86
87 _info "Delete TXT record"
88
89 autodns_response="$(_autodns_zone_cleanup "$_zone" "$_sub_domain" "$txtvalue" "$_system_ns")"
90
91 if [ "$?" -eq "0" ]; then
92 _info "Deleted, OK"
93 return 0
94 fi
95
96 return 1
97 }
98
99 #################### Private functions below ##################################
100
101 # Arguments:
102 # fulldomain
103 # Returns:
104 # _sub_domain=_acme-challenge.www
105 # _zone=domain.com
106 # _system_ns
107 _get_autodns_zone() {
108 domain="$1"
109
110 i=2
111 p=1
112
113 while true; do
114 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
115 _debug h "$h"
116
117 if [ -z "$h" ]; then
118 # not valid
119 return 1
120 fi
121
122 autodns_response="$(_autodns_zone_inquire "$h")"
123
124 if [ "$?" -ne "0" ]; then
125 _err "invalid domain"
126 return 1
127 fi
128
129 if _contains "$autodns_response" "<summary>1</summary>" >/dev/null; then
130 _zone="$(echo "$autodns_response" | _egrep_o '<name>[^<]*</name>' | cut -d '>' -f 2 | cut -d '<' -f 1)"
131 _system_ns="$(echo "$autodns_response" | _egrep_o '<system_ns>[^<]*</system_ns>' | cut -d '>' -f 2 | cut -d '<' -f 1)"
132 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
133 return 0
134 fi
135
136 p=$i
137 i=$(_math "$i" + 1)
138 done
139
140 return 1
141 }
142
143 _build_request_auth_xml() {
144 printf "<auth>
145 <user>%s</user>
146 <password>%s</password>
147 <context>%s</context>
148 </auth>" "$AUTODNS_USER" "$AUTODNS_PASSWORD" "$AUTODNS_CONTEXT"
149 }
150
151 # Arguments:
152 # zone
153 _build_zone_inquire_xml() {
154 printf "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
155 <request>
156 %s
157 <task>
158 <code>0205</code>
159 <view>
160 <children>1</children>
161 <limit>1</limit>
162 </view>
163 <where>
164 <key>name</key>
165 <operator>eq</operator>
166 <value>%s</value>
167 </where>
168 </task>
169 </request>" "$(_build_request_auth_xml)" "$1"
170 }
171
172 # Arguments:
173 # zone
174 # subdomain
175 # txtvalue
176 # system_ns
177 _build_zone_update_xml() {
178 printf "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
179 <request>
180 %s
181 <task>
182 <code>0202001</code>
183 <default>
184 <rr_add>
185 <name>%s</name>
186 <ttl>600</ttl>
187 <type>TXT</type>
188 <value>%s</value>
189 </rr_add>
190 </default>
191 <zone>
192 <name>%s</name>
193 <system_ns>%s</system_ns>
194 </zone>
195 </task>
196 </request>" "$(_build_request_auth_xml)" "$2" "$3" "$1" "$4"
197 }
198
199 # Arguments:
200 # zone
201 _autodns_zone_inquire() {
202 request_data="$(_build_zone_inquire_xml "$1")"
203 autodns_response="$(_autodns_api_call "$request_data")"
204 ret="$?"
205
206 printf "%s" "$autodns_response"
207 return "$ret"
208 }
209
210 # Arguments:
211 # zone
212 # subdomain
213 # txtvalue
214 # system_ns
215 _autodns_zone_update() {
216 request_data="$(_build_zone_update_xml "$1" "$2" "$3" "$4")"
217 autodns_response="$(_autodns_api_call "$request_data")"
218 ret="$?"
219
220 printf "%s" "$autodns_response"
221 return "$ret"
222 }
223
224 # Arguments:
225 # zone
226 # subdomain
227 # txtvalue
228 # system_ns
229 _autodns_zone_cleanup() {
230 request_data="$(_build_zone_update_xml "$1" "$2" "$3" "$4")"
231 # replace 'rr_add>' with 'rr_rem>' in request_data
232 request_data="$(printf -- "%s" "$request_data" | sed 's/rr_add>/rr_rem>/g')"
233 autodns_response="$(_autodns_api_call "$request_data")"
234 ret="$?"
235
236 printf "%s" "$autodns_response"
237 return "$ret"
238 }
239
240 # Arguments:
241 # request_data
242 _autodns_api_call() {
243 request_data="$1"
244
245 _debug request_data "$request_data"
246
247 autodns_response="$(_post "$request_data" "$AUTODNS_API")"
248 ret="$?"
249
250 _debug autodns_response "$autodns_response"
251
252 if [ "$ret" -ne "0" ]; then
253 _err "error"
254 return 1
255 fi
256
257 if _contains "$autodns_response" "<type>success</type>" >/dev/null; then
258 _info "success"
259 printf "%s" "$autodns_response"
260 return 0
261 fi
262
263 return 1
264 }