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.

278 lines
11 KiB

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