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.

200 lines
7.6 KiB

  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: sha1)
  9. # - $KAS_Authdata (Kasserver API auth data.)
  10. #
  11. # Author: Martin Kammerlander, Phlegx Systems OG <martin.kammerlander@phlegx.com>
  12. # Updated by: Marc-Oliver Lange <git@die-lang.es>
  13. # Credits: Inspired by dns_he.sh. Thanks a lot man!
  14. # Git repo: https://github.com/phlegx/acme.sh
  15. # TODO: Better Error handling
  16. ########################################################################
  17. KAS_Api="https://kasapi.kasserver.com/soap/KasApi.php"
  18. KAS_Auth="https://kasapi.kasserver.com/soap/KasAuth.php"
  19. ######## Public functions #####################
  20. dns_kas_add() {
  21. _fulldomain=$1
  22. _txtvalue=$2
  23. _info "### -> Using DNS-01 All-inkl/Kasserver hook"
  24. _info "### -> Adding $_fulldomain DNS TXT entry on All-inkl/Kasserver"
  25. _info "### -> Retriving Credential Token"
  26. _get_credential_token
  27. _info "### -> Check and Save Props"
  28. _check_and_save
  29. _info "### -> Checking Zone and Record_Name"
  30. _get_zone_and_record_name "$_fulldomain"
  31. _info "### -> Checking for existing Record entries"
  32. _get_record_id
  33. # If there is a record_id, delete the entry
  34. if [ -n "$_record_id" ]; then
  35. _info "Existing records found. Now deleting old entries"
  36. for i in $_record_id; do
  37. _delete_RecordByID "$i"
  38. done
  39. else
  40. _info "No record found."
  41. fi
  42. _info "### -> Creating TXT DNS record"
  43. action="add_dns_settings"
  44. kasReqParam="{\"record_name\":\"$_record_name\",\"record_type\":\"TXT\",\"record_data\":\"$_txtvalue\",\"record_aux\":\"0\",\"zone_host\":\"$_zone\"}"
  45. response="$(_callAPI "$action" "$kasReqParam")"
  46. _debug2 "Response" "$response"
  47. if ! _contains "$response" "TRUE"; then
  48. _err "An unkown error occurred, please check manually."
  49. return 1
  50. fi
  51. return 0
  52. }
  53. dns_kas_rm() {
  54. _fulldomain=$1
  55. _txtvalue=$2
  56. _info "### -> Using DNS-01 All-inkl/Kasserver hook"
  57. _info "### -> Cleaning up after All-inkl/Kasserver hook"
  58. _info "### -> Removing $_fulldomain DNS TXT entry on All-inkl/Kasserver"
  59. _info "### -> Retriving Credential Token"
  60. _get_credential_token
  61. _info "### -> Check and Save Props"
  62. _check_and_save
  63. _info "### -> Checking Zone and Record_Name"
  64. _get_zone_and_record_name "$_fulldomain"
  65. _info "### -> Getting Record ID"
  66. _get_record_id
  67. _info "### -> Removing entries with ID: $_record_id"
  68. # If there is a record_id, delete the entry
  69. if [ -n "$_record_id" ]; then
  70. for i in $_record_id; do
  71. _delete_RecordByID "$i"
  72. done
  73. else # Cannot delete or unkown error
  74. _info "No record_id found that can be deleted. Please check manually."
  75. fi
  76. return 0
  77. }
  78. ########################## PRIVATE FUNCTIONS ###########################
  79. # Delete Record ID
  80. _delete_RecordByID() {
  81. recId=$1
  82. action="delete_dns_settings"
  83. kasReqParam="{\"record_id\":\"$recId\"}"
  84. response="$(_callAPI "$action" "$kasReqParam")"
  85. _debug2 "Response" "$response"
  86. if ! _contains "$response" "TRUE"; then
  87. _info "Either the txt record is not found or another error occurred, please check manually."
  88. fi
  89. }
  90. # Checks for the ENV variables and saves them
  91. _check_and_save() {
  92. KAS_Login="${KAS_Login:-$(_readaccountconf_mutable KAS_Login)}"
  93. KAS_Authtype="${KAS_Authtype:-$(_readaccountconf_mutable KAS_Authtype)}"
  94. KAS_Authdata="${KAS_Authdata:-$(_readaccountconf_mutable KAS_Authdata)}"
  95. if [ -z "$KAS_Login" ] || [ -z "$KAS_Authtype" ] || [ -z "$KAS_Authdata" ]; then
  96. KAS_Login=
  97. KAS_Authtype=
  98. KAS_Authdata=
  99. _err "No auth details provided. Please set user credentials using the \$KAS_Login, \$KAS_Authtype, and \$KAS_Authdata environment variables."
  100. return 1
  101. fi
  102. _saveaccountconf_mutable KAS_Login "$KAS_Login"
  103. _saveaccountconf_mutable KAS_Authtype "$KAS_Authtype"
  104. _saveaccountconf_mutable KAS_Authdata "$KAS_Authdata"
  105. return 0
  106. }
  107. # Gets back the base domain/zone and record name.
  108. # See: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
  109. _get_zone_and_record_name() {
  110. action="get_domains"
  111. kasReqParam="[]"
  112. response="$(_callAPI "$action" "$kasReqParam")"
  113. _debug2 "Response" "$response"
  114. 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')"
  115. domain="$1"
  116. temp_domain="$(echo "$1" | sed 's/\.$//')"
  117. rootzone="$domain"
  118. for i in $zonen; do
  119. l1=${#rootzone}
  120. l2=${#i}
  121. if _endswith "$domain" "$i" && [ "$l1" -ge "$l2" ]; then
  122. rootzone="$i"
  123. fi
  124. done
  125. _zone="${rootzone}."
  126. temp_record_name="$(echo "$temp_domain" | sed "s/$rootzone//g")"
  127. _record_name="$(echo "$temp_record_name" | sed 's/\.$//')"
  128. _debug "Zone:" "$_zone"
  129. _debug "Domain:" "$domain"
  130. _debug "Record_Name:" "$_record_name"
  131. return 0
  132. }
  133. # Retrieve the DNS record ID
  134. _get_record_id() {
  135. action="get_dns_settings"
  136. kasReqParam="{\"zone_host\":\"$_zone\",\"nameserver\":\"ns5.kasserver.com\"}"
  137. response="$(_callAPI "$action" "$kasReqParam")"
  138. _debug2 "Response" "$response"
  139. _record_id="$(echo "$response" | sed 's/<item xsi:type="ns2:Map">/\n/g' | sed -n -e "/^.*$_record_name.*/Ip" | sed -n -e "/^.*$_txtvalue.*/Ip" | sed -r 's/(.*record_id<\/key><value xsi:type="xsd:string">)([0-9]+)(<\/value.*)/\2/')"
  140. _debug "Record Id: " "$_record_id"
  141. return 0
  142. }
  143. # Retrieve credential token
  144. _get_credential_token() {
  145. data="<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>"
  146. data="$data<Params xsi:type=\"xsd:string\">{\"kas_login\":\"$KAS_Login\",\"kas_auth_type\":\"$KAS_Authtype\",\"kas_auth_data\":\"$KAS_Authdata\",\"session_lifetime\":600,\"session_update_lifetime\":\"Y\",\"session_2fa\":123456}</Params>"
  147. data="$data</ns1:KasAuth></SOAP-ENV:Body></SOAP-ENV:Envelope>"
  148. _debug "Be frindly and wait 10 seconds by default before calling KAS API."
  149. _sleep 10
  150. contentType="text/xml"
  151. export _H1="SOAPAction: ns1:KasAuth"
  152. response="$(_post "$data" "$KAS_Auth" "" "POST" "$contentType")"
  153. _debug2 "Response" "$response"
  154. _credential_token="$(echo "$response" | tr '\n' ' ' | sed 's/.*return xsi:type="xsd:string">\(.*\)<\/return>/\1/' | sed 's/<\/ns1:KasAuthResponse\(.*\)Envelope>.*//')"
  155. _debug "Credential Token: " "$_credential_token"
  156. return 0
  157. }
  158. _callAPI() {
  159. kasaction=$1
  160. kasReqParams=$2
  161. baseParam="<Params xsi:type=\"xsd:string\">{\"kas_login\":\"$KAS_Login\",\"kas_auth_type\":\"session\",\"kas_auth_data\":\"$_credential_token\",\"kas_action\":\"$kasaction\",\"KasRequestParams\":$kasReqParams"
  162. baseParamClosing="}</Params>"
  163. data="<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>"
  164. data="$data$baseParam$baseParamClosing"
  165. data="$data</ns1:KasApi></SOAP-ENV:Body></SOAP-ENV:Envelope>"
  166. _debug2 "Request" "$data"
  167. _debug "Be frindly and wait 10 seconds by default before calling KAS API."
  168. _sleep 10
  169. contentType="text/xml"
  170. export _H1="SOAPAction: ns1:KasApi"
  171. response="$(_post "$data" "$KAS_Api" "" "POST" "$contentType")"
  172. _debug2 "Response" "$response"
  173. echo "$response"
  174. }