You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
11 KiB

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