]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_openprovider.sh
Merge pull request #4531 from NCDGHA/bugfix/issue_4530_fix_http_status_503
[mirror_acme.sh.git] / dnsapi / dns_openprovider.sh
CommitLineData
1bfd0f01 1#!/usr/bin/env sh
04eaf7f1
SO
2
3# This is the OpenProvider API wrapper for acme.sh
4#
5# Author: Sylvia van Os
d795fac3 6# Report Bugs here: https://github.com/acmesh-official/acme.sh/issues/2104
04eaf7f1
SO
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
14OPENPROVIDER_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"
20dns_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
7decce97 53 item="$(echo "$items" | _egrep_o '<openXML>.*<\/openXML>' | sed -n 's/.*\(<item>.*<\/item>\).*/\1/p')"
04eaf7f1
SO
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
3ff48b85
BR
62 tmpitem="$(echo "$item" | sed 's/\*/\\*/g')"
63 items="$(echo "$items" | sed "s|${tmpitem}||")"
04eaf7f1 64
7decce97
SO
65 results_retrieved="$(_math "$results_retrieved" + 1)"
66 new_item="$(echo "$item" | sed -n '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')"
04eaf7f1 67 if [ -z "$new_item" ]; then
3ff48b85 68 # Domain apex
7decce97 69 new_item="$(echo "$item" | sed -n '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')"
04eaf7f1
SO
70 fi
71
3ff48b85 72 if [ -z "$(echo "$new_item" | _egrep_o ".*<type>(A|AAAA|CNAME|MX|SPF|SRV|TXT|TLSA|SSHFP|CAA|NS)<\/type>.*")" ]; then
04eaf7f1
SO
73 _debug "not an allowed record type, skipping" "$new_item"
74 continue
75 fi
76
7decce97 77 existing_items="$existing_items$new_item"
04eaf7f1
SO
78 done
79
7decce97 80 total="$(echo "$response" | _egrep_o '<total>.*?<\/total>' | sed -n 's/.*<total>\(.*\)<\/total>.*/\1/p')"
04eaf7f1
SO
81
82 _debug total "$total"
83 if [ "$results_retrieved" -eq "$total" ]; then
84 break
85 fi
86 done
87
88 _debug "Creating acme record"
7decce97 89 acme_record="$(echo "$fulldomain" | sed -e "s/.$_domain_name.$_domain_extension$//")"
3ff48b85 90 _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>600</ttl></item></array></records></modifyZoneDnsRequest>' "$_domain_name" "$_domain_extension" "$existing_items" "$acme_record" "$txtvalue")"
04eaf7f1
SO
91
92 return 0
93}
94
95#Usage: fulldomain txtvalue
96#Remove the txt record after validation.
97dns_openprovider_rm() {
98 fulldomain="$1"
99 txtvalue="$2"
100
101 OPENPROVIDER_USER="${OPENPROVIDER_USER:-$(_readaccountconf_mutable OPENPROVIDER_USER)}"
102 OPENPROVIDER_PASSWORDHASH="${OPENPROVIDER_PASSWORDHASH:-$(_readaccountconf_mutable OPENPROVIDER_PASSWORDHASH)}"
103
104 if [ -z "$OPENPROVIDER_USER" ] || [ -z "$OPENPROVIDER_PASSWORDHASH" ]; then
105 _err "You didn't specify the openprovider user and/or password hash."
106 return 1
107 fi
108
109 # save the username and password to the account conf file.
110 _saveaccountconf_mutable OPENPROVIDER_USER "$OPENPROVIDER_USER"
111 _saveaccountconf_mutable OPENPROVIDER_PASSWORDHASH "$OPENPROVIDER_PASSWORDHASH"
112
113 _debug "First detect the root zone"
114 if ! _get_root "$fulldomain"; then
115 _err "invalid domain"
116 return 1
117 fi
118
119 _debug _domain_name "$_domain_name"
120 _debug _domain_extension "$_domain_extension"
121
122 _debug "Getting current records"
123 existing_items=""
124 results_retrieved=0
125 while true; do
126 _openprovider_request "$(printf '<searchZoneRecordDnsRequest><name>%s.%s</name><offset>%s</offset></searchZoneRecordDnsRequest>' "$_domain_name" "$_domain_extension" "$results_retrieved")"
127
128 # Remove acme records from items
129 items="$response"
130 while true; do
7decce97 131 item="$(echo "$items" | _egrep_o '<openXML>.*<\/openXML>' | sed -n 's/.*\(<item>.*<\/item>\).*/\1/p')"
04eaf7f1
SO
132 _debug existing_items "$existing_items"
133 _debug results_retrieved "$results_retrieved"
134 _debug item "$item"
135
136 if [ -z "$item" ]; then
137 break
138 fi
139
3ff48b85
BR
140 tmpitem="$(echo "$item" | sed 's/\*/\\*/g')"
141 items="$(echo "$items" | sed "s|${tmpitem}||")"
04eaf7f1 142
7decce97
SO
143 results_retrieved="$(_math "$results_retrieved" + 1)"
144 if ! echo "$item" | grep -v "$fulldomain"; then
04eaf7f1
SO
145 _debug "acme record, skipping" "$item"
146 continue
147 fi
148
7decce97 149 new_item="$(echo "$item" | sed -n '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')"
04eaf7f1
SO
150
151 if [ -z "$new_item" ]; then
3ff48b85 152 # domain apex
7decce97 153 new_item="$(echo "$item" | sed -n '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')"
04eaf7f1
SO
154 fi
155
3ff48b85 156 if [ -z "$(echo "$new_item" | _egrep_o ".*<type>(A|AAAA|CNAME|MX|SPF|SRV|TXT|TLSA|SSHFP|CAA|NS)<\/type>.*")" ]; then
04eaf7f1
SO
157 _debug "not an allowed record type, skipping" "$new_item"
158 continue
159 fi
160
7decce97 161 existing_items="$existing_items$new_item"
04eaf7f1
SO
162 done
163
7decce97 164 total="$(echo "$response" | _egrep_o '<total>.*?<\/total>' | sed -n 's/.*<total>\(.*\)<\/total>.*/\1/p')"
04eaf7f1
SO
165
166 _debug total "$total"
167
168 if [ "$results_retrieved" -eq "$total" ]; then
169 break
170 fi
171 done
172
173 _debug "Removing acme record"
174 _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")"
175
176 return 0
177}
178
179#################### Private functions below ##################################
180#_acme-challenge.www.domain.com
181#returns
182# _domain_name=domain
183# _domain_extension=com
184_get_root() {
185 domain=$1
186 i=2
187
188 results_retrieved=0
189 while true; do
7decce97 190 h=$(echo "$domain" | cut -d . -f $i-100)
04eaf7f1
SO
191 _debug h "$h"
192 if [ -z "$h" ]; then
193 #not valid
194 return 1
195 fi
196
7decce97 197 _openprovider_request "$(printf '<searchDomainRequest><domainNamePattern>%s</domainNamePattern><offset>%s</offset></searchDomainRequest>' "$(echo "$h" | cut -d . -f 1)" "$results_retrieved")"
04eaf7f1
SO
198
199 items="$response"
200 while true; do
7decce97 201 item="$(echo "$items" | _egrep_o '<openXML>.*<\/openXML>' | sed -n 's/.*\(<domain>.*<\/domain>\).*/\1/p')"
04eaf7f1
SO
202 _debug existing_items "$existing_items"
203 _debug results_retrieved "$results_retrieved"
204 _debug item "$item"
205
206 if [ -z "$item" ]; then
207 break
208 fi
209
3ff48b85
BR
210 tmpitem="$(echo "$item" | sed 's/\*/\\*/g')"
211 items="$(echo "$items" | sed "s|${tmpitem}||")"
04eaf7f1 212
7decce97 213 results_retrieved="$(_math "$results_retrieved" + 1)"
04eaf7f1 214
7decce97
SO
215 _domain_name="$(echo "$item" | sed -n 's/.*<domain>.*<name>\(.*\)<\/name>.*<\/domain>.*/\1/p')"
216 _domain_extension="$(echo "$item" | sed -n 's/.*<domain>.*<extension>\(.*\)<\/extension>.*<\/domain>.*/\1/p')"
04eaf7f1
SO
217 _debug _domain_name "$_domain_name"
218 _debug _domain_extension "$_domain_extension"
7decce97 219 if [ "$_domain_name.$_domain_extension" = "$h" ]; then
04eaf7f1
SO
220 return 0
221 fi
222 done
223
7decce97 224 total="$(echo "$response" | _egrep_o '<total>.*?<\/total>' | sed -n 's/.*<total>\(.*\)<\/total>.*/\1/p')"
04eaf7f1
SO
225
226 _debug total "$total"
227
228 if [ "$results_retrieved" -eq "$total" ]; then
229 results_retrieved=0
7decce97 230 i="$(_math "$i" + 1)"
04eaf7f1
SO
231 fi
232 done
233 return 1
234}
235
236_openprovider_request() {
237 request_xml=$1
238
71cfd874 239 xml_prefix='<?xml version="1.0" encoding="UTF-8"?>'
04eaf7f1 240 xml_content=$(printf '<openXML><credentials><username>%s</username><hash>%s</hash></credentials>%s</openXML>' "$OPENPROVIDER_USER" "$OPENPROVIDER_PASSWORDHASH" "$request_xml")
7decce97 241 response="$(_post "$(echo "$xml_prefix$xml_content" | tr -d '\n')" "$OPENPROVIDER_API" "" "POST" "application/xml")"
04eaf7f1
SO
242 _debug response "$response"
243 if ! _contains "$response" "<openXML><reply><code>0</code>.*</reply></openXML>"; then
244 _err "API request failed."
245 return 1
246 fi
247}