]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_openprovider.sh
Add OpenProvider support
[mirror_acme.sh.git] / dnsapi / dns_openprovider.sh
1 #!/usr/bin/env sh
2
3 # This is the OpenProvider API wrapper for acme.sh
4 #
5 # Author: Sylvia van Os
6 # Report Bugs here: https://github.com/Neilpang/acme.sh/issues/2104
7 #
8 # export OPENPROVIDER_USER="username"
9 # export OPENPROVIDER_PASSWORDHASH="hashed_password"
10 #
11 # Usage:
12 # acme.sh --issue --dns dns_openprovider -d example.com
13
14 OPENPROVIDER_API="https://api.openprovider.eu/"
15 #OPENPROVIDER_API="https://api.cte.openprovider.eu/" # Test API
16
17 ######## Public functions #####################
18
19 #Usage: dns_openprovider_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
20 dns_openprovider_add() {
21 fulldomain="$1"
22 txtvalue="$2"
23
24 OPENPROVIDER_USER="${OPENPROVIDER_USER:-$(_readaccountconf_mutable OPENPROVIDER_USER)}"
25 OPENPROVIDER_PASSWORDHASH="${OPENPROVIDER_PASSWORDHASH:-$(_readaccountconf_mutable OPENPROVIDER_PASSWORDHASH)}"
26
27 if [ -z "$OPENPROVIDER_USER" ] || [ -z "$OPENPROVIDER_PASSWORDHASH" ]; then
28 _err "You didn't specify the openprovider user and/or password hash."
29 return 1
30 fi
31
32 # save the username and password to the account conf file.
33 _saveaccountconf_mutable OPENPROVIDER_USER "$OPENPROVIDER_USER"
34 _saveaccountconf_mutable OPENPROVIDER_PASSWORDHASH "$OPENPROVIDER_PASSWORDHASH"
35
36 _debug "First detect the root zone"
37 if ! _get_root "$fulldomain"; then
38 _err "invalid domain"
39 return 1
40 fi
41
42 _debug _domain_name "$_domain_name"
43 _debug _domain_extension "$_domain_extension"
44
45 _debug "Getting current records"
46 existing_items=""
47 results_retrieved=0
48 while true; do
49 _openprovider_request "$(printf '<searchZoneRecordDnsRequest><name>%s.%s</name><offset>%s</offset></searchZoneRecordDnsRequest>' "$_domain_name" "$_domain_extension" "$results_retrieved")"
50
51 items="$response"
52 while true; do
53 item="$(printf '%s' "$items" | _egrep_o '<openXML>.*<\/openXML>' | sed -n -E 's/.*(<item>.*<\/item>).*/\1/p')"
54 _debug existing_items "$existing_items"
55 _debug results_retrieved "$results_retrieved"
56 _debug item "$item"
57
58 if [ -z "$item" ]; then
59 break
60 fi
61
62 items="$(printf '%s' "$items" | sed "s\ 1$item\ 1\ 1")"
63
64 results_retrieved=$((results_retrieved + 1))
65 new_item="$(printf '%s' "$item" | sed -n -E "s/.*<item>.*(<name>(.*)\.$_domain_name\.$_domain_extension<\/name>.*(<type>.*<\/type>).*(<value>.*<\/value>).*(<prio>.*<\/prio>).*(<ttl>.*<\/ttl>)).*<\/item>.*/<item><name>\2<\/name>\3\4\5\6<\/item>/p")"
66 if [ -z "$new_item" ]; then
67 # Base record
68 new_item="$(printf '%s' "$item" | sed -n -E "s/.*<item>.*(<name>(.*)$_domain_name\.$_domain_extension<\/name>.*(<type>.*<\/type>).*(<value>.*<\/value>).*(<prio>.*<\/prio>).*(<ttl>.*<\/ttl>)).*<\/item>.*/<item><name>\2<\/name>\3\4\5\6<\/item>/p")"
69 fi
70
71 if [ -z "$(printf '%s' "$new_item" | _egrep_o ".*<type>(A|AAAA|CNAME|MX|SPF|SRV|TXT|TLSA|SSHFP|CAA)<\/type>.*")" ]; then
72 _debug "not an allowed record type, skipping" "$new_item"
73 continue
74 fi
75
76 existing_items="$(printf '%s%s' "$existing_items" "$new_item")"
77 done
78
79 total="$(printf '%s' "$response" | _egrep_o '<total>.*?<\/total>' | sed -n -E 's/.*<total>(.*)<\/total>.*/\1/p')"
80
81 _debug total "$total"
82 if [ "$results_retrieved" -eq "$total" ]; then
83 break
84 fi
85 done
86
87 _debug "Creating acme record"
88 acme_record="$(printf '%s' "$fulldomain" | sed -e "s/.$_domain_name.$_domain_extension$//")"
89 _openprovider_request "$(printf '<modifyZoneDnsRequest><domain><name>%s</name><extension>%s</extension></domain><type>master</type><records><array>%s<item><name>%s</name><type>TXT</type><value>%s</value><ttl>86400</ttl></item></array></records></modifyZoneDnsRequest>' "$_domain_name" "$_domain_extension" "$existing_items" "$acme_record" "$txtvalue")"
90
91 return 0
92 }
93
94 #Usage: fulldomain txtvalue
95 #Remove the txt record after validation.
96 dns_openprovider_rm() {
97 fulldomain="$1"
98 txtvalue="$2"
99
100 OPENPROVIDER_USER="${OPENPROVIDER_USER:-$(_readaccountconf_mutable OPENPROVIDER_USER)}"
101 OPENPROVIDER_PASSWORDHASH="${OPENPROVIDER_PASSWORDHASH:-$(_readaccountconf_mutable OPENPROVIDER_PASSWORDHASH)}"
102
103 if [ -z "$OPENPROVIDER_USER" ] || [ -z "$OPENPROVIDER_PASSWORDHASH" ]; then
104 _err "You didn't specify the openprovider user and/or password hash."
105 return 1
106 fi
107
108 # save the username and password to the account conf file.
109 _saveaccountconf_mutable OPENPROVIDER_USER "$OPENPROVIDER_USER"
110 _saveaccountconf_mutable OPENPROVIDER_PASSWORDHASH "$OPENPROVIDER_PASSWORDHASH"
111
112 _debug "First detect the root zone"
113 if ! _get_root "$fulldomain"; then
114 _err "invalid domain"
115 return 1
116 fi
117
118 _debug _domain_name "$_domain_name"
119 _debug _domain_extension "$_domain_extension"
120
121 _debug "Getting current records"
122 existing_items=""
123 results_retrieved=0
124 while true; do
125 _openprovider_request "$(printf '<searchZoneRecordDnsRequest><name>%s.%s</name><offset>%s</offset></searchZoneRecordDnsRequest>' "$_domain_name" "$_domain_extension" "$results_retrieved")"
126
127 # Remove acme records from items
128 items="$response"
129 while true; do
130 item="$(printf '%s' "$items" | _egrep_o '<openXML>.*<\/openXML>' | sed -n -E 's/.*(<item>.*<\/item>).*/\1/p')"
131 _debug existing_items "$existing_items"
132 _debug results_retrieved "$results_retrieved"
133 _debug item "$item"
134
135 if [ -z "$item" ]; then
136 break
137 fi
138
139 items="$(printf '%s' "$items" | sed "s\ 1$item\ 1\ 1")"
140
141 results_retrieved=$((results_retrieved + 1))
142 if ! printf '%s' "$item" | grep -v "$fulldomain"; then
143 _debug "acme record, skipping" "$item"
144 continue
145 fi
146
147 new_item="$(printf '%s' "$item" | sed -n -E "s/.*<item>.*(<name>(.*)\.$_domain_name\.$_domain_extension<\/name>.*(<type>.*<\/type>).*(<value>.*<\/value>).*(<prio>.*<\/prio>).*(<ttl>.*<\/ttl>)).*<\/item>.*/<item><name>\2<\/name>\3\4\5\6<\/item>/p")"
148
149 if [ -z "$new_item" ]; then
150 # Base record
151 new_item="$(printf '%s' "$item" | sed -n -E "s/.*<item>.*(<name>(.*)$_domain_name\.$_domain_extension<\/name>.*(<type>.*<\/type>).*(<value>.*<\/value>).*(<prio>.*<\/prio>).*(<ttl>.*<\/ttl>)).*<\/item>.*/<item><name>\2<\/name>\3\4\5\6<\/item>/p")"
152 fi
153
154 if [ -z "$(printf '%s' "$new_item" | _egrep_o ".*<type>(A|AAAA|CNAME|MX|SPF|SRV|TXT|TLSA|SSHFP|CAA)<\/type>.*")" ]; then
155 _debug "not an allowed record type, skipping" "$new_item"
156 continue
157 fi
158
159 existing_items="$(printf '%s%s' "$existing_items" "$new_item")"
160 done
161
162 total="$(printf '%s' "$response" | _egrep_o '<total>.*?<\/total>' | sed -n -E 's/.*<total>(.*)<\/total>.*/\1/p')"
163
164 _debug total "$total"
165
166 if [ "$results_retrieved" -eq "$total" ]; then
167 break
168 fi
169 done
170
171 _debug "Removing acme record"
172 _openprovider_request "$(printf '<modifyZoneDnsRequest><domain><name>%s</name><extension>%s</extension></domain><type>master</type><records><array>%s</array></records></modifyZoneDnsRequest>' "$_domain_name" "$_domain_extension" "$existing_items")"
173
174 return 0
175 }
176
177 #################### Private functions below ##################################
178 #_acme-challenge.www.domain.com
179 #returns
180 # _domain_name=domain
181 # _domain_extension=com
182 _get_root() {
183 domain=$1
184 i=2
185
186 results_retrieved=0
187 while true; do
188 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
189 _debug h "$h"
190 if [ -z "$h" ]; then
191 #not valid
192 return 1
193 fi
194
195 _openprovider_request "$(printf '<searchDomainRequest><domainNamePattern>%s</domainNamePattern><offset>%s</offset></searchDomainRequest>' "$(printf "%s" "$h" | cut -d . -f 1)" "$results_retrieved")"
196
197 items="$response"
198 while true; do
199 item="$(printf '%s' "$items" | _egrep_o '<openXML>.*<\/openXML>' | sed -n -E 's/.*(<domain>.*<\/domain>).*/\1/p')"
200 _debug existing_items "$existing_items"
201 _debug results_retrieved "$results_retrieved"
202 _debug item "$item"
203
204 if [ -z "$item" ]; then
205 break
206 fi
207
208 items="$(printf '%s' "$items" | sed "s\ 1$item\ 1\ 1")"
209
210 results_retrieved=$((results_retrieved + 1))
211
212 _domain_name="$(printf "%s" "$item" | sed -n -E 's/.*<domain>.*<name>(.*)<\/name>.*<\/domain>.*/\1/p')"
213 _domain_extension="$(printf "%s" "$item" | sed -n -E 's/.*<domain>.*<extension>(.*)<\/extension>.*<\/domain>.*/\1/p')"
214 _debug _domain_name "$_domain_name"
215 _debug _domain_extension "$_domain_extension"
216 if [ "$(printf "%s.%s" "$_domain_name" "$_domain_extension")" = "$h" ]; then
217 return 0
218 fi
219 done
220
221 total="$(printf '%s' "$response" | _egrep_o '<total>.*?<\/total>' | sed -n -E 's/.*<total>(.*)<\/total>.*/\1/p')"
222
223 _debug total "$total"
224
225 if [ "$results_retrieved" -eq "$total" ]; then
226 results_retrieved=0
227 i=$(_math "$i" + 1)
228 fi
229 done
230 return 1
231 }
232
233 _openprovider_request() {
234 request_xml=$1
235
236 xml_prefix=$(printf '<?xml version="1.0" encoding="UTF-8"?>')
237 xml_content=$(printf '<openXML><credentials><username>%s</username><hash>%s</hash></credentials>%s</openXML>' "$OPENPROVIDER_USER" "$OPENPROVIDER_PASSWORDHASH" "$request_xml")
238 response="$(_post "$(printf "%s%s" "$xml_prefix" "$xml_content" | tr -d '\n')" "$OPENPROVIDER_API" "" "POST" "application/xml")"
239 _debug response "$response"
240 if ! _contains "$response" "<openXML><reply><code>0</code>.*</reply></openXML>"; then
241 _err "API request failed."
242 return 1
243 fi
244 }