]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_kas.sh
Merge pull request #4658 from Justman10000/master
[mirror_acme.sh.git] / dnsapi / dns_kas.sh
CommitLineData
2214507d 1#!/usr/bin/env sh
861df496
MK
2########################################################################
3# All-inkl Kasserver hook script for acme.sh
4#
5# Environment variables:
6#
7# - $KAS_Login (Kasserver API login name)
0e8fef73 8# - $KAS_Authtype (Kasserver API auth type. Default: plain)
861df496
MK
9# - $KAS_Authdata (Kasserver API auth data.)
10#
2304f005 11# Last update: squared GmbH <github@squaredgmbh.de>
0e8fef73 12# Credits:
2304f005
HS
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
594b83e7 17########################################################################
0e8fef73
HS
18KAS_Api_GET="$(_get "https://kasapi.kasserver.com/soap/wsdl/KasApi.wsdl")"
19KAS_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
22KAS_Auth_GET="$(_get "https://kasapi.kasserver.com/soap/wsdl/KasAuth.wsdl")"
23KAS_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
26KAS_default_ratelimit=5 # TODO - Every response delivers a ratelimit (seconds) where KASAPI is blocking a request.
27
02461967
M
28######## Public functions #####################
29dns_kas_add() {
26b5180b
MK
30 _fulldomain=$1
31 _txtvalue=$2
aaee0414 32
0e8fef73 33 _info "[KAS] -> Using DNS-01 All-inkl/Kasserver hook"
2304f005
HS
34 _info "[KAS] -> Check and Save Props"
35 _check_and_save
f9c2874c 36
b42532af 37 _info "[KAS] -> Adding $_fulldomain DNS TXT entry on all-inkl.com/Kasserver"
0e8fef73 38 _info "[KAS] -> Retriving Credential Token"
aaee0414
M
39 _get_credential_token
40
0e8fef73 41 _info "[KAS] -> Checking Zone and Record_Name"
594b83e7 42 _get_zone_and_record_name "$_fulldomain"
aaee0414 43
0e8fef73 44 _info "[KAS] -> Checking for existing Record entries"
861df496
MK
45 _get_record_id
46
aaee0414
M
47 # If there is a record_id, delete the entry
48 if [ -n "$_record_id" ]; then
0e8fef73 49 _info "[KAS] -> Existing records found. Now deleting old entries"
aaee0414
M
50 for i in $_record_id; do
51 _delete_RecordByID "$i"
52 done
53 else
0e8fef73 54 _info "[KAS] -> No record found."
aaee0414
M
55 fi
56
0e8fef73 57 _info "[KAS] -> Creating TXT DNS record"
aaee0414 58 action="add_dns_settings"
0e8fef73
HS
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\""
aaee0414 64 response="$(_callAPI "$action" "$kasReqParam")"
0e8fef73 65 _debug2 "[KAS] -> Response" "$response"
aaee0414 66
0e8fef73
HS
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."
861df496
MK
83 return 1
84 fi
85 return 0
86}
87
88dns_kas_rm() {
26b5180b
MK
89 _fulldomain=$1
90 _txtvalue=$2
431c53ef 91
0e8fef73 92 _info "[KAS] -> Using DNS-01 All-inkl/Kasserver hook"
2304f005
HS
93 _info "[KAS] -> Check and Save Props"
94 _check_and_save
95
0e8fef73
HS
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"
aaee0414
M
99 _get_credential_token
100
0e8fef73 101 _info "[KAS] -> Checking Zone and Record_Name"
594b83e7 102 _get_zone_and_record_name "$_fulldomain"
aaee0414 103
0e8fef73 104 _info "[KAS] -> Getting Record ID"
861df496 105 _get_record_id
431c53ef 106
0e8fef73 107 _info "[KAS] -> Removing entries with ID: $_record_id"
ec1f9841 108 # If there is a record_id, delete the entry
861df496 109 if [ -n "$_record_id" ]; then
3ccac629 110 for i in $_record_id; do
aaee0414 111 _delete_RecordByID "$i"
1ef7fd36 112 done
861df496 113 else # Cannot delete or unkown error
0e8fef73 114 _info "[KAS] -> No record_id found that can be deleted. Please check manually."
861df496 115 fi
c641b61b 116 return 0
861df496
MK
117}
118
119########################## PRIVATE FUNCTIONS ###########################
aaee0414
M
120# Delete Record ID
121_delete_RecordByID() {
122 recId=$1
123 action="delete_dns_settings"
0e8fef73 124 kasReqParam="\"record_id\":\"$recId\""
aaee0414 125 response="$(_callAPI "$action" "$kasReqParam")"
0e8fef73
HS
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
aaee0414
M
145 fi
146}
861df496
MK
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=
0e8fef73 157 _err "[KAS] -> No auth details provided. Please set user credentials using the \$KAS_Login, \$KAS_Authtype, and \$KAS_Authdata environment variables."
861df496
MK
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
594b83e7 166# Gets back the base domain/zone and record name.
861df496 167# See: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
a1384254 168_get_zone_and_record_name() {
aaee0414 169 action="get_domains"
0e8fef73
HS
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
aaee0414
M
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}
594b83e7 188 l2=${#i}
aaee0414
M
189 if _endswith "$domain" "$i" && [ "$l1" -ge "$l2" ]; then
190 rootzone="$i"
594b83e7
M
191 fi
192 done
aaee0414
M
193 _zone="${rootzone}."
194 temp_record_name="$(echo "$temp_domain" | sed "s/$rootzone//g")"
195 _record_name="$(echo "$temp_record_name" | sed 's/\.$//')"
0e8fef73
HS
196 _debug "[KAS] -> Zone:" "$_zone"
197 _debug "[KAS] -> Domain:" "$domain"
198 _debug "[KAS] -> Record_Name:" "$_record_name"
11bfb1e5 199 return 0
861df496
MK
200}
201
202# Retrieve the DNS record ID
203_get_record_id() {
aaee0414 204 action="get_dns_settings"
0e8fef73 205 kasReqParam="\"zone_host\":\"$_zone\""
aaee0414 206 response="$(_callAPI "$action" "$kasReqParam")"
0e8fef73
HS
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
aaee0414 217
dde1bab1 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")"
0e8fef73 219 _debug "[KAS] -> Record Id: " "$_record_id"
aaee0414
M
220 return 0
221}
222
223# Retrieve credential token
224_get_credential_token() {
0e8fef73
HS
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\""
aaee0414 230
0e8fef73
HS
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
aaee0414
M
236
237 contentType="text/xml"
0e8fef73 238 export _H1="SOAPAction: urn:xmethodsKasApiAuthentication#KasAuth"
aaee0414 239 response="$(_post "$data" "$KAS_Auth" "" "POST" "$contentType")"
0e8fef73 240 _debug2 "[KAS] -> Response" "$response"
aaee0414 241
2304f005
HS
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
aaee0414 251 _credential_token="$(echo "$response" | tr '\n' ' ' | sed 's/.*return xsi:type="xsd:string">\(.*\)<\/return>/\1/' | sed 's/<\/ns1:KasAuthResponse\(.*\)Envelope>.*//')"
0e8fef73 252 _debug "[KAS] -> Credential Token: " "$_credential_token"
861df496 253 return 0
ec1f9841 254}
aaee0414
M
255
256_callAPI() {
257 kasaction=$1
258 kasReqParams=$2
aaee0414 259
0e8fef73
HS
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
aaee0414
M
275
276 contentType="text/xml"
0e8fef73 277 export _H1="SOAPAction: urn:xmethodsKasApi#KasApi"
aaee0414 278 response="$(_post "$data" "$KAS_Api" "" "POST" "$contentType")"
0e8fef73 279 _debug2 "[KAS] -> Response" "$response"
aaee0414 280 echo "$response"
dea8a08b 281}