]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_kas.sh
Merge pull request #4764 from sebastianas/inwx
[mirror_acme.sh.git] / dnsapi / dns_kas.sh
1 #!/usr/bin/env sh
2 ########################################################################
3 # All-inkl Kasserver hook script for acme.sh
4 #
5 # Environment variables:
6 #
7 # - $KAS_Login (Kasserver API login name)
8 # - $KAS_Authtype (Kasserver API auth type. Default: plain)
9 # - $KAS_Authdata (Kasserver API auth data.)
10 #
11 # Last update: squared GmbH <github@squaredgmbh.de>
12 # Credits:
13 # - dns_he.sh. Thanks a lot man!
14 # - Martin Kammerlander, Phlegx Systems OG <martin.kammerlander@phlegx.com>
15 # - Marc-Oliver Lange <git@die-lang.es>
16 # - https://github.com/o1oo11oo/kasapi.sh
17 ########################################################################
18 KAS_Api_GET="$(_get "https://kasapi.kasserver.com/soap/wsdl/KasApi.wsdl")"
19 KAS_Api="$(echo "$KAS_Api_GET" | tr -d ' ' | grep -i "<soap:addresslocation=" | sed "s/='/\n/g" | grep -i "http" | sed "s/'\/>//g")"
20 _info "[KAS] -> API URL $KAS_Api"
21
22 KAS_Auth_GET="$(_get "https://kasapi.kasserver.com/soap/wsdl/KasAuth.wsdl")"
23 KAS_Auth="$(echo "$KAS_Auth_GET" | tr -d ' ' | grep -i "<soap:addresslocation=" | sed "s/='/\n/g" | grep -i "http" | sed "s/'\/>//g")"
24 _info "[KAS] -> AUTH URL $KAS_Auth"
25
26 KAS_default_ratelimit=5 # TODO - Every response delivers a ratelimit (seconds) where KASAPI is blocking a request.
27
28 ######## Public functions #####################
29 dns_kas_add() {
30 _fulldomain=$1
31 _txtvalue=$2
32
33 _info "[KAS] -> Using DNS-01 All-inkl/Kasserver hook"
34 _info "[KAS] -> Check and Save Props"
35 _check_and_save
36
37 _info "[KAS] -> Adding $_fulldomain DNS TXT entry on all-inkl.com/Kasserver"
38 _info "[KAS] -> Retriving Credential Token"
39 _get_credential_token
40
41 _info "[KAS] -> Checking Zone and Record_Name"
42 _get_zone_and_record_name "$_fulldomain"
43
44 _info "[KAS] -> Checking for existing Record entries"
45 _get_record_id
46
47 # If there is a record_id, delete the entry
48 if [ -n "$_record_id" ]; then
49 _info "[KAS] -> Existing records found. Now deleting old entries"
50 for i in $_record_id; do
51 _delete_RecordByID "$i"
52 done
53 else
54 _info "[KAS] -> No record found."
55 fi
56
57 _info "[KAS] -> Creating TXT DNS record"
58 action="add_dns_settings"
59 kasReqParam="\"record_name\":\"$_record_name\""
60 kasReqParam="$kasReqParam,\"record_type\":\"TXT\""
61 kasReqParam="$kasReqParam,\"record_data\":\"$_txtvalue\""
62 kasReqParam="$kasReqParam,\"record_aux\":\"0\""
63 kasReqParam="$kasReqParam,\"zone_host\":\"$_zone\""
64 response="$(_callAPI "$action" "$kasReqParam")"
65 _debug2 "[KAS] -> Response" "$response"
66
67 if [ -z "$response" ]; then
68 _info "[KAS] -> Response was empty, please check manually."
69 return 1
70 elif _contains "$response" "<SOAP-ENV:Fault>"; then
71 faultstring="$(echo "$response" | tr -d '\n\r' | sed "s/<faultstring>/\n=> /g" | sed "s/<\/faultstring>/\n/g" | grep "=>" | sed "s/=> //g")"
72 case "${faultstring}" in
73 "record_already_exists")
74 _info "[KAS] -> The record already exists, which must not be a problem. Please check manually."
75 ;;
76 *)
77 _err "[KAS] -> An error =>$faultstring<= occurred, please check manually."
78 return 1
79 ;;
80 esac
81 elif ! _contains "$response" "<item><key xsi:type=\"xsd:string\">ReturnString</key><value xsi:type=\"xsd:string\">TRUE</value></item>"; then
82 _err "[KAS] -> An unknown error occurred, please check manually."
83 return 1
84 fi
85 return 0
86 }
87
88 dns_kas_rm() {
89 _fulldomain=$1
90 _txtvalue=$2
91
92 _info "[KAS] -> Using DNS-01 All-inkl/Kasserver hook"
93 _info "[KAS] -> Check and Save Props"
94 _check_and_save
95
96 _info "[KAS] -> Cleaning up after All-inkl/Kasserver hook"
97 _info "[KAS] -> Removing $_fulldomain DNS TXT entry on All-inkl/Kasserver"
98 _info "[KAS] -> Retriving Credential Token"
99 _get_credential_token
100
101 _info "[KAS] -> Checking Zone and Record_Name"
102 _get_zone_and_record_name "$_fulldomain"
103
104 _info "[KAS] -> Getting Record ID"
105 _get_record_id
106
107 _info "[KAS] -> Removing entries with ID: $_record_id"
108 # If there is a record_id, delete the entry
109 if [ -n "$_record_id" ]; then
110 for i in $_record_id; do
111 _delete_RecordByID "$i"
112 done
113 else # Cannot delete or unkown error
114 _info "[KAS] -> No record_id found that can be deleted. Please check manually."
115 fi
116 return 0
117 }
118
119 ########################## PRIVATE FUNCTIONS ###########################
120 # Delete Record ID
121 _delete_RecordByID() {
122 recId=$1
123 action="delete_dns_settings"
124 kasReqParam="\"record_id\":\"$recId\""
125 response="$(_callAPI "$action" "$kasReqParam")"
126 _debug2 "[KAS] -> Response" "$response"
127
128 if [ -z "$response" ]; then
129 _info "[KAS] -> Response was empty, please check manually."
130 return 1
131 elif _contains "$response" "<SOAP-ENV:Fault>"; then
132 faultstring="$(echo "$response" | tr -d '\n\r' | sed "s/<faultstring>/\n=> /g" | sed "s/<\/faultstring>/\n/g" | grep "=>" | sed "s/=> //g")"
133 case "${faultstring}" in
134 "record_id_not_found")
135 _info "[KAS] -> The record was not found, which perhaps is not a problem. Please check manually."
136 ;;
137 *)
138 _err "[KAS] -> An error =>$faultstring<= occurred, please check manually."
139 return 1
140 ;;
141 esac
142 elif ! _contains "$response" "<item><key xsi:type=\"xsd:string\">ReturnString</key><value xsi:type=\"xsd:string\">TRUE</value></item>"; then
143 _err "[KAS] -> An unknown error occurred, please check manually."
144 return 1
145 fi
146 }
147 # Checks for the ENV variables and saves them
148 _check_and_save() {
149 KAS_Login="${KAS_Login:-$(_readaccountconf_mutable KAS_Login)}"
150 KAS_Authtype="${KAS_Authtype:-$(_readaccountconf_mutable KAS_Authtype)}"
151 KAS_Authdata="${KAS_Authdata:-$(_readaccountconf_mutable KAS_Authdata)}"
152
153 if [ -z "$KAS_Login" ] || [ -z "$KAS_Authtype" ] || [ -z "$KAS_Authdata" ]; then
154 KAS_Login=
155 KAS_Authtype=
156 KAS_Authdata=
157 _err "[KAS] -> No auth details provided. Please set user credentials using the \$KAS_Login, \$KAS_Authtype, and \$KAS_Authdata environment variables."
158 return 1
159 fi
160 _saveaccountconf_mutable KAS_Login "$KAS_Login"
161 _saveaccountconf_mutable KAS_Authtype "$KAS_Authtype"
162 _saveaccountconf_mutable KAS_Authdata "$KAS_Authdata"
163 return 0
164 }
165
166 # Gets back the base domain/zone and record name.
167 # See: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
168 _get_zone_and_record_name() {
169 action="get_domains"
170 response="$(_callAPI "$action")"
171 _debug2 "[KAS] -> Response" "$response"
172
173 if [ -z "$response" ]; then
174 _info "[KAS] -> Response was empty, please check manually."
175 return 1
176 elif _contains "$response" "<SOAP-ENV:Fault>"; then
177 faultstring="$(echo "$response" | tr -d '\n\r' | sed "s/<faultstring>/\n=> /g" | sed "s/<\/faultstring>/\n/g" | grep "=>" | sed "s/=> //g")"
178 _err "[KAS] -> Either no domains were found or another error =>$faultstring<= occurred, please check manually."
179 return 1
180 fi
181
182 zonen="$(echo "$response" | sed 's/<item>/\n/g' | sed -r 's/(.*<key xsi:type="xsd:string">domain_name<\/key><value xsi:type="xsd:string">)(.*)(<\/value.*)/\2/' | sed '/^</d')"
183 domain="$1"
184 temp_domain="$(echo "$1" | sed 's/\.$//')"
185 rootzone="$domain"
186 for i in $zonen; do
187 l1=${#rootzone}
188 l2=${#i}
189 if _endswith "$domain" "$i" && [ "$l1" -ge "$l2" ]; then
190 rootzone="$i"
191 fi
192 done
193 _zone="${rootzone}."
194 temp_record_name="$(echo "$temp_domain" | sed "s/$rootzone//g")"
195 _record_name="$(echo "$temp_record_name" | sed 's/\.$//')"
196 _debug "[KAS] -> Zone:" "$_zone"
197 _debug "[KAS] -> Domain:" "$domain"
198 _debug "[KAS] -> Record_Name:" "$_record_name"
199 return 0
200 }
201
202 # Retrieve the DNS record ID
203 _get_record_id() {
204 action="get_dns_settings"
205 kasReqParam="\"zone_host\":\"$_zone\""
206 response="$(_callAPI "$action" "$kasReqParam")"
207 _debug2 "[KAS] -> Response" "$response"
208
209 if [ -z "$response" ]; then
210 _info "[KAS] -> Response was empty, please check manually."
211 return 1
212 elif _contains "$response" "<SOAP-ENV:Fault>"; then
213 faultstring="$(echo "$response" | tr -d '\n\r' | sed "s/<faultstring>/\n=> /g" | sed "s/<\/faultstring>/\n/g" | grep "=>" | sed "s/=> //g")"
214 _err "[KAS] -> Either no domains were found or another error =>$faultstring<= occurred, please check manually."
215 return 1
216 fi
217
218 _record_id="$(echo "$response" | tr -d '\n\r' | sed "s/<item xsi:type=\"ns2:Map\">/\n/g" | grep -i "$_record_name" | grep -i ">TXT<" | sed "s/<item><key xsi:type=\"xsd:string\">record_id<\/key><value xsi:type=\"xsd:string\">/=>/g" | grep -i "$_txtvalue" | sed "s/<\/value><\/item>/\n/g" | grep "=>" | sed "s/=>//g")"
219 _debug "[KAS] -> Record Id: " "$_record_id"
220 return 0
221 }
222
223 # Retrieve credential token
224 _get_credential_token() {
225 baseParamAuth="\"kas_login\":\"$KAS_Login\""
226 baseParamAuth="$baseParamAuth,\"kas_auth_type\":\"$KAS_Authtype\""
227 baseParamAuth="$baseParamAuth,\"kas_auth_data\":\"$KAS_Authdata\""
228 baseParamAuth="$baseParamAuth,\"session_lifetime\":600"
229 baseParamAuth="$baseParamAuth,\"session_update_lifetime\":\"Y\""
230
231 data='<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:xmethodsKasApiAuthentication" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:KasAuth><Params xsi:type="xsd:string">{'
232 data="$data$baseParamAuth}</Params></ns1:KasAuth></SOAP-ENV:Body></SOAP-ENV:Envelope>"
233
234 _debug "[KAS] -> Be friendly and wait $KAS_default_ratelimit seconds by default before calling KAS API."
235 _sleep $KAS_default_ratelimit
236
237 contentType="text/xml"
238 export _H1="SOAPAction: urn:xmethodsKasApiAuthentication#KasAuth"
239 response="$(_post "$data" "$KAS_Auth" "" "POST" "$contentType")"
240 _debug2 "[KAS] -> Response" "$response"
241
242 if [ -z "$response" ]; then
243 _info "[KAS] -> Response was empty, please check manually."
244 return 1
245 elif _contains "$response" "<SOAP-ENV:Fault>"; then
246 faultstring="$(echo "$response" | tr -d '\n\r' | sed "s/<faultstring>/\n=> /g" | sed "s/<\/faultstring>/\n/g" | grep "=>" | sed "s/=> //g")"
247 _err "[KAS] -> Could not retrieve login token or antoher error =>$faultstring<= occurred, please check manually."
248 return 1
249 fi
250
251 _credential_token="$(echo "$response" | tr '\n' ' ' | sed 's/.*return xsi:type="xsd:string">\(.*\)<\/return>/\1/' | sed 's/<\/ns1:KasAuthResponse\(.*\)Envelope>.*//')"
252 _debug "[KAS] -> Credential Token: " "$_credential_token"
253 return 0
254 }
255
256 _callAPI() {
257 kasaction=$1
258 kasReqParams=$2
259
260 baseParamAuth="\"kas_login\":\"$KAS_Login\""
261 baseParamAuth="$baseParamAuth,\"kas_auth_type\":\"session\""
262 baseParamAuth="$baseParamAuth,\"kas_auth_data\":\"$_credential_token\""
263
264 data='<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:xmethodsKasApi" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:KasApi><Params xsi:type="xsd:string">{'
265 data="$data$baseParamAuth,\"kas_action\":\"$kasaction\""
266 if [ -n "$kasReqParams" ]; then
267 data="$data,\"KasRequestParams\":{$kasReqParams}"
268 fi
269 data="$data}</Params></ns1:KasApi></SOAP-ENV:Body></SOAP-ENV:Envelope>"
270
271 _debug2 "[KAS] -> Request" "$data"
272
273 _debug "[KAS] -> Be friendly and wait $KAS_default_ratelimit seconds by default before calling KAS API."
274 _sleep $KAS_default_ratelimit
275
276 contentType="text/xml"
277 export _H1="SOAPAction: urn:xmethodsKasApi#KasApi"
278 response="$(_post "$data" "$KAS_Api" "" "POST" "$contentType")"
279 _debug2 "[KAS] -> Response" "$response"
280 echo "$response"
281 }