2019-12-29 09:59:28 +00:00
#!/usr/bin/env sh
2018-03-09 15:29:47 +00:00
########################################################################
# All-inkl Kasserver hook script for acme.sh
#
# Environment variables:
#
# - $KAS_Login (Kasserver API login name)
2022-08-17 17:43:30 +00:00
# - $KAS_Authtype (Kasserver API auth type. Default: plain)
2018-03-09 15:29:47 +00:00
# - $KAS_Authdata (Kasserver API auth data.)
#
2022-08-23 10:41:42 +00:00
# Last update: squared GmbH <github@squaredgmbh.de>
2022-08-17 17:43:30 +00:00
# Credits:
2022-08-23 10:41:42 +00:00
# - dns_he.sh. Thanks a lot man!
# - Martin Kammerlander, Phlegx Systems OG <martin.kammerlander@phlegx.com>
# - Marc-Oliver Lange <git@die-lang.es>
# - https://github.com/o1oo11oo/kasapi.sh
2019-12-28 10:58:21 +00:00
########################################################################
2022-08-17 17:43:30 +00:00
KAS_Api_GET = " $( _get "https://kasapi.kasserver.com/soap/wsdl/KasApi.wsdl" ) "
KAS_Api = " $( echo " $KAS_Api_GET " | tr -d ' ' | grep -i "<soap:addresslocation=" | sed "s/='/\n/g" | grep -i "http" | sed "s/'\/>//g" ) "
_info " [KAS] -> API URL $KAS_Api "
KAS_Auth_GET = " $( _get "https://kasapi.kasserver.com/soap/wsdl/KasAuth.wsdl" ) "
KAS_Auth = " $( echo " $KAS_Auth_GET " | tr -d ' ' | grep -i "<soap:addresslocation=" | sed "s/='/\n/g" | grep -i "http" | sed "s/'\/>//g" ) "
_info " [KAS] -> AUTH URL $KAS_Auth "
KAS_default_ratelimit = 5 # TODO - Every response delivers a ratelimit (seconds) where KASAPI is blocking a request.
2020-01-15 12:56:01 +00:00
######## Public functions #####################
dns_kas_add( ) {
2018-03-16 14:49:40 +00:00
_fulldomain = $1
_txtvalue = $2
2022-08-04 07:44:35 +00:00
2022-08-17 17:43:30 +00:00
_info "[KAS] -> Using DNS-01 All-inkl/Kasserver hook"
2022-08-23 10:41:42 +00:00
_info "[KAS] -> Check and Save Props"
_check_and_save
2022-08-23 11:30:04 +00:00
2022-08-17 17:58:34 +00:00
_info " [KAS] -> Adding $_fulldomain DNS TXT entry on all-inkl.com/Kasserver "
2022-08-17 17:43:30 +00:00
_info "[KAS] -> Retriving Credential Token"
2022-08-04 07:44:35 +00:00
_get_credential_token
2022-08-17 17:43:30 +00:00
_info "[KAS] -> Checking Zone and Record_Name"
2019-12-28 10:58:21 +00:00
_get_zone_and_record_name " $_fulldomain "
2022-08-04 07:44:35 +00:00
2022-08-17 17:43:30 +00:00
_info "[KAS] -> Checking for existing Record entries"
2018-03-09 15:29:47 +00:00
_get_record_id
2022-08-04 07:44:35 +00:00
# If there is a record_id, delete the entry
if [ -n " $_record_id " ] ; then
2022-08-17 17:43:30 +00:00
_info "[KAS] -> Existing records found. Now deleting old entries"
2022-08-04 07:44:35 +00:00
for i in $_record_id ; do
_delete_RecordByID " $i "
done
else
2022-08-17 17:43:30 +00:00
_info "[KAS] -> No record found."
2022-08-04 07:44:35 +00:00
fi
2022-08-17 17:43:30 +00:00
_info "[KAS] -> Creating TXT DNS record"
2022-08-04 07:44:35 +00:00
action = "add_dns_settings"
2022-08-17 17:43:30 +00:00
kasReqParam = " \"record_name\":\" $_record_name \" "
kasReqParam = " $kasReqParam ,\"record_type\":\"TXT\" "
kasReqParam = " $kasReqParam ,\"record_data\":\" $_txtvalue \" "
kasReqParam = " $kasReqParam ,\"record_aux\":\"0\" "
kasReqParam = " $kasReqParam ,\"zone_host\":\" $_zone \" "
2022-08-04 07:44:35 +00:00
response = " $( _callAPI " $action " " $kasReqParam " ) "
2022-08-17 17:43:30 +00:00
_debug2 "[KAS] -> Response" " $response "
2022-08-04 07:44:35 +00:00
2022-08-17 17:43:30 +00:00
if [ -z " $response " ] ; then
_info "[KAS] -> Response was empty, please check manually."
return 1
elif _contains " $response " "<SOAP-ENV:Fault>" ; then
faultstring = " $( echo " $response " | tr -d '\n\r' | sed "s/<faultstring>/\n=> /g" | sed "s/<\/faultstring>/\n/g" | grep "=>" | sed "s/=> //g" ) "
case " ${ faultstring } " in
"record_already_exists" )
_info "[KAS] -> The record already exists, which must not be a problem. Please check manually."
; ;
*)
_err " [KAS] -> An error => $faultstring <= occurred, please check manually. "
return 1
; ;
esac
elif ! _contains " $response " "<item><key xsi:type=\"xsd:string\">ReturnString</key><value xsi:type=\"xsd:string\">TRUE</value></item>" ; then
_err "[KAS] -> An unknown error occurred, please check manually."
2018-03-09 15:29:47 +00:00
return 1
fi
return 0
}
dns_kas_rm( ) {
2018-03-16 14:49:40 +00:00
_fulldomain = $1
_txtvalue = $2
2020-01-15 16:48:30 +00:00
2022-08-17 17:43:30 +00:00
_info "[KAS] -> Using DNS-01 All-inkl/Kasserver hook"
2022-08-23 10:41:42 +00:00
_info "[KAS] -> Check and Save Props"
_check_and_save
2022-08-17 17:43:30 +00:00
_info "[KAS] -> Cleaning up after All-inkl/Kasserver hook"
_info " [KAS] -> Removing $_fulldomain DNS TXT entry on All-inkl/Kasserver "
_info "[KAS] -> Retriving Credential Token"
2022-08-04 07:44:35 +00:00
_get_credential_token
2022-08-17 17:43:30 +00:00
_info "[KAS] -> Checking Zone and Record_Name"
2019-12-28 10:58:21 +00:00
_get_zone_and_record_name " $_fulldomain "
2022-08-04 07:44:35 +00:00
2022-08-17 17:43:30 +00:00
_info "[KAS] -> Getting Record ID"
2018-03-09 15:29:47 +00:00
_get_record_id
2020-01-15 16:48:30 +00:00
2022-08-17 17:43:30 +00:00
_info " [KAS] -> Removing entries with ID: $_record_id "
2019-11-29 21:22:26 +00:00
# If there is a record_id, delete the entry
2018-03-09 15:29:47 +00:00
if [ -n " $_record_id " ] ; then
2019-12-12 15:23:42 +00:00
for i in $_record_id ; do
2022-08-04 07:44:35 +00:00
_delete_RecordByID " $i "
2019-06-05 09:38:41 +00:00
done
2018-03-09 15:29:47 +00:00
else # Cannot delete or unkown error
2022-08-17 17:43:30 +00:00
_info "[KAS] -> No record_id found that can be deleted. Please check manually."
2018-03-09 15:29:47 +00:00
fi
2019-11-29 21:46:44 +00:00
return 0
2018-03-09 15:29:47 +00:00
}
########################## PRIVATE FUNCTIONS ###########################
2022-08-04 07:44:35 +00:00
# Delete Record ID
_delete_RecordByID( ) {
recId = $1
action = "delete_dns_settings"
2022-08-17 17:43:30 +00:00
kasReqParam = " \"record_id\":\" $recId \" "
2022-08-04 07:44:35 +00:00
response = " $( _callAPI " $action " " $kasReqParam " ) "
2022-08-17 17:43:30 +00:00
_debug2 "[KAS] -> Response" " $response "
if [ -z " $response " ] ; then
_info "[KAS] -> Response was empty, please check manually."
return 1
elif _contains " $response " "<SOAP-ENV:Fault>" ; then
faultstring = " $( echo " $response " | tr -d '\n\r' | sed "s/<faultstring>/\n=> /g" | sed "s/<\/faultstring>/\n/g" | grep "=>" | sed "s/=> //g" ) "
case " ${ faultstring } " in
"record_id_not_found" )
_info "[KAS] -> The record was not found, which perhaps is not a problem. Please check manually."
; ;
*)
_err " [KAS] -> An error => $faultstring <= occurred, please check manually. "
return 1
; ;
esac
elif ! _contains " $response " "<item><key xsi:type=\"xsd:string\">ReturnString</key><value xsi:type=\"xsd:string\">TRUE</value></item>" ; then
_err "[KAS] -> An unknown error occurred, please check manually."
return 1
2022-08-04 07:44:35 +00:00
fi
}
2018-03-09 15:29:47 +00:00
# Checks for the ENV variables and saves them
_check_and_save( ) {
KAS_Login = " ${ KAS_Login :- $( _readaccountconf_mutable KAS_Login) } "
KAS_Authtype = " ${ KAS_Authtype :- $( _readaccountconf_mutable KAS_Authtype) } "
KAS_Authdata = " ${ KAS_Authdata :- $( _readaccountconf_mutable KAS_Authdata) } "
if [ -z " $KAS_Login " ] || [ -z " $KAS_Authtype " ] || [ -z " $KAS_Authdata " ] ; then
KAS_Login =
KAS_Authtype =
KAS_Authdata =
2022-08-17 17:43:30 +00:00
_err "[KAS] -> No auth details provided. Please set user credentials using the \$KAS_Login, \$KAS_Authtype, and \$KAS_Authdata environment variables."
2018-03-09 15:29:47 +00:00
return 1
fi
_saveaccountconf_mutable KAS_Login " $KAS_Login "
_saveaccountconf_mutable KAS_Authtype " $KAS_Authtype "
_saveaccountconf_mutable KAS_Authdata " $KAS_Authdata "
return 0
}
2019-12-28 10:58:21 +00:00
# Gets back the base domain/zone and record name.
2018-03-09 15:29:47 +00:00
# See: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
2019-12-28 22:42:46 +00:00
_get_zone_and_record_name( ) {
2022-08-04 07:44:35 +00:00
action = "get_domains"
2022-08-17 17:43:30 +00:00
response = " $( _callAPI " $action " ) "
_debug2 "[KAS] -> Response" " $response "
if [ -z " $response " ] ; then
_info " [KAS] -> Response was empty, please check manually. "
return 1
elif _contains " $response " "<SOAP-ENV:Fault>" ; then
faultstring = " $( echo " $response " | tr -d '\n\r' | sed "s/<faultstring>/\n=> /g" | sed "s/<\/faultstring>/\n/g" | grep "=>" | sed "s/=> //g" ) "
_err " [KAS] -> Either no domains were found or another error => $faultstring <= occurred, please check manually. "
return 1
fi
2022-08-04 07:44:35 +00:00
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' ) "
domain = " $1 "
temp_domain = " $( echo " $1 " | sed 's/\.$//' ) "
rootzone = " $domain "
for i in $zonen ; do
l1 = ${# rootzone }
2019-12-28 10:58:21 +00:00
l2 = ${# i }
2022-08-04 07:44:35 +00:00
if _endswith " $domain " " $i " && [ " $l1 " -ge " $l2 " ] ; then
rootzone = " $i "
2019-12-28 10:58:21 +00:00
fi
done
2022-08-04 07:44:35 +00:00
_zone = " ${ rootzone } . "
temp_record_name = " $( echo " $temp_domain " | sed " s/ $rootzone //g " ) "
_record_name = " $( echo " $temp_record_name " | sed 's/\.$//' ) "
2022-08-17 17:43:30 +00:00
_debug "[KAS] -> Zone:" " $_zone "
_debug "[KAS] -> Domain:" " $domain "
_debug "[KAS] -> Record_Name:" " $_record_name "
2018-03-16 14:02:47 +00:00
return 0
2018-03-09 15:29:47 +00:00
}
# Retrieve the DNS record ID
_get_record_id( ) {
2022-08-04 07:44:35 +00:00
action = "get_dns_settings"
2022-08-17 17:43:30 +00:00
kasReqParam = " \"zone_host\":\" $_zone \" "
2022-08-04 07:44:35 +00:00
response = " $( _callAPI " $action " " $kasReqParam " ) "
2022-08-17 17:43:30 +00:00
_debug2 "[KAS] -> Response" " $response "
if [ -z " $response " ] ; then
_info "[KAS] -> Response was empty, please check manually."
return 1
elif _contains " $response " "<SOAP-ENV:Fault>" ; then
faultstring = " $( echo " $response " | tr -d '\n\r' | sed "s/<faultstring>/\n=> /g" | sed "s/<\/faultstring>/\n/g" | grep "=>" | sed "s/=> //g" ) "
_err " [KAS] -> Either no domains were found or another error => $faultstring <= occurred, please check manually. "
return 1
fi
2022-08-04 07:44:35 +00:00
2023-03-06 19:18:15 +00:00
_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" ) "
2022-08-17 17:43:30 +00:00
_debug "[KAS] -> Record Id: " " $_record_id "
2022-08-04 07:44:35 +00:00
return 0
}
# Retrieve credential token
_get_credential_token( ) {
2022-08-17 17:43:30 +00:00
baseParamAuth = " \"kas_login\":\" $KAS_Login \" "
baseParamAuth = " $baseParamAuth ,\"kas_auth_type\":\" $KAS_Authtype \" "
baseParamAuth = " $baseParamAuth ,\"kas_auth_data\":\" $KAS_Authdata \" "
baseParamAuth = " $baseParamAuth ,\"session_lifetime\":600 "
baseParamAuth = " $baseParamAuth ,\"session_update_lifetime\":\"Y\" "
2022-08-04 07:44:35 +00:00
2022-08-17 17:43:30 +00:00
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">{'
data = " $data $baseParamAuth }</Params></ns1:KasAuth></SOAP-ENV:Body></SOAP-ENV:Envelope> "
_debug " [KAS] -> Be friendly and wait $KAS_default_ratelimit seconds by default before calling KAS API. "
_sleep $KAS_default_ratelimit
2022-08-04 07:44:35 +00:00
contentType = "text/xml"
2022-08-17 17:43:30 +00:00
export _H1 = "SOAPAction: urn:xmethodsKasApiAuthentication#KasAuth"
2022-08-04 07:44:35 +00:00
response = " $( _post " $data " " $KAS_Auth " "" "POST" " $contentType " ) "
2022-08-17 17:43:30 +00:00
_debug2 "[KAS] -> Response" " $response "
2022-08-04 07:44:35 +00:00
2022-08-23 10:41:42 +00:00
if [ -z " $response " ] ; then
_info "[KAS] -> Response was empty, please check manually."
return 1
elif _contains " $response " "<SOAP-ENV:Fault>" ; then
faultstring = " $( echo " $response " | tr -d '\n\r' | sed "s/<faultstring>/\n=> /g" | sed "s/<\/faultstring>/\n/g" | grep "=>" | sed "s/=> //g" ) "
_err " [KAS] -> Could not retrieve login token or antoher error => $faultstring <= occurred, please check manually. "
return 1
fi
2022-08-04 07:44:35 +00:00
_credential_token = " $( echo " $response " | tr '\n' ' ' | sed 's/.*return xsi:type="xsd:string">\(.*\)<\/return>/\1/' | sed 's/<\/ns1:KasAuthResponse\(.*\)Envelope>.*//' ) "
2022-08-17 17:43:30 +00:00
_debug "[KAS] -> Credential Token: " " $_credential_token "
2018-03-09 15:29:47 +00:00
return 0
2019-11-29 21:22:26 +00:00
}
2022-08-04 07:44:35 +00:00
_callAPI( ) {
kasaction = $1
kasReqParams = $2
2022-08-17 17:43:30 +00:00
baseParamAuth = " \"kas_login\":\" $KAS_Login \" "
baseParamAuth = " $baseParamAuth ,\"kas_auth_type\":\"session\" "
baseParamAuth = " $baseParamAuth ,\"kas_auth_data\":\" $_credential_token \" "
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">{'
data = " $data $baseParamAuth ,\"kas_action\":\" $kasaction \" "
if [ -n " $kasReqParams " ] ; then
data = " $data ,\"KasRequestParams\":{ $kasReqParams } "
fi
data = " $data }</Params></ns1:KasApi></SOAP-ENV:Body></SOAP-ENV:Envelope> "
_debug2 "[KAS] -> Request" " $data "
_debug " [KAS] -> Be friendly and wait $KAS_default_ratelimit seconds by default before calling KAS API. "
_sleep $KAS_default_ratelimit
2022-08-04 07:44:35 +00:00
contentType = "text/xml"
2022-08-17 17:43:30 +00:00
export _H1 = "SOAPAction: urn:xmethodsKasApi#KasApi"
2022-08-04 07:44:35 +00:00
response = " $( _post " $data " " $KAS_Api " "" "POST" " $contentType " ) "
2022-08-17 17:43:30 +00:00
_debug2 "[KAS] -> Response" " $response "
2022-08-04 07:44:35 +00:00
echo " $response "
2023-03-06 19:36:53 +00:00
}