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.

167 lines
5.7 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. # Credits: Inspired by dns_he.sh. Thanks a lot man!
  13. # Git repo: https://github.com/phlegx/acme.sh
  14. # TODO: Better Error handling
  15. ########################################################################
  16. KAS_Api="https://kasapi.kasserver.com/dokumentation/formular.php"
  17. ######## Public functions #####################
  18. dns_kas_add(){
  19. _fulldomain=$1
  20. _txtvalue=$2
  21. _info "Using DNS-01 All-inkl/Kasserver hook"
  22. _info "Adding or Updating $_fulldomain DNS TXT entry on All-inkl/Kasserver"
  23. _info "Check and Save Props"
  24. _check_and_save
  25. _info "Checking Zone and Record_Name"
  26. _get_zone_and_record_name "$_fulldomain"
  27. _info "Getting Record ID"
  28. _get_record_id
  29. _info "Creating TXT DNS record"
  30. params="?kas_login=$KAS_Login"
  31. params="$params&kas_auth_type=$KAS_Authtype"
  32. params="$params&kas_auth_data=$KAS_Authdata"
  33. params="$params&var1=record_name"
  34. params="$params&wert1=$_record_name"
  35. params="$params&var2=record_type"
  36. params="$params&wert2=TXT"
  37. params="$params&var3=record_data"
  38. params="$params&wert3=$_txtvalue"
  39. params="$params&var4=record_aux"
  40. params="$params&wert4=0"
  41. params="$params&kas_action=add_dns_settings"
  42. params="$params&var5=zone_host"
  43. params="$params&wert5=$_zone"
  44. _debug2 "Wait for 10 seconds by default before calling KAS API."
  45. sleep 10
  46. response="$(_get "$KAS_Api$params")"
  47. _debug2 "response" "$response"
  48. if ! _contains "$response" "TRUE"; then
  49. _err "An unkown error occurred, please check manually."
  50. return 1
  51. fi
  52. return 0
  53. }
  54. dns_kas_rm() {
  55. _fulldomain=$1
  56. _txtvalue=$2
  57. _info "Using DNS-01 All-inkl/Kasserver hook"
  58. _info "Cleaning up after All-inkl/Kasserver hook"
  59. _info "Removing $_fulldomain DNS TXT entry on All-inkl/Kasserver"
  60. _info "Check and Save Props"
  61. _check_and_save
  62. _info "Checking Zone and Record_Name"
  63. _get_zone_and_record_name "$_fulldomain"
  64. _info "Getting Record ID"
  65. _get_record_id
  66. # If there is a record_id, delete the entry
  67. if [ -n "$_record_id" ]; then
  68. params="?kas_login=$KAS_Login"
  69. params="$params&kas_auth_type=$KAS_Authtype"
  70. params="$params&kas_auth_data=$KAS_Authdata"
  71. params="$params&kas_action=delete_dns_settings"
  72. for i in $_record_id; do
  73. params2="$params&var1=record_id"
  74. params2="$params2&wert1=$i"
  75. _debug2 "Wait for 10 seconds by default before calling KAS API."
  76. sleep 10
  77. response="$(_get "$KAS_Api$params2")"
  78. _debug2 "response" "$response"
  79. if ! _contains "$response" "TRUE"; then
  80. _err "Either the txt record is not found or another error occurred, please check manually."
  81. return 1
  82. fi
  83. done
  84. else # Cannot delete or unkown error
  85. _err "No record_id found that can be deleted. Please check manually."
  86. return 1
  87. fi
  88. return 0
  89. }
  90. ########################## PRIVATE FUNCTIONS ###########################
  91. # Checks for the ENV variables and saves them
  92. _check_and_save() {
  93. KAS_Login="${KAS_Login:-$(_readaccountconf_mutable KAS_Login)}"
  94. KAS_Authtype="${KAS_Authtype:-$(_readaccountconf_mutable KAS_Authtype)}"
  95. KAS_Authdata="${KAS_Authdata:-$(_readaccountconf_mutable KAS_Authdata)}"
  96. if [ -z "$KAS_Login" ] || [ -z "$KAS_Authtype" ] || [ -z "$KAS_Authdata" ]; then
  97. KAS_Login=
  98. KAS_Authtype=
  99. KAS_Authdata=
  100. _err "No auth details provided. Please set user credentials using the \$KAS_Login, \$KAS_Authtype, and \$KAS_Authdata environment variables."
  101. return 1
  102. fi
  103. _saveaccountconf_mutable KAS_Login "$KAS_Login"
  104. _saveaccountconf_mutable KAS_Authtype "$KAS_Authtype"
  105. _saveaccountconf_mutable KAS_Authdata "$KAS_Authdata"
  106. return 0
  107. }
  108. # Gets back the base domain/zone and record name.
  109. # See: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
  110. _get_zone_and_record_name() {
  111. params="?kas_login=$KAS_Login"
  112. params="$params&kas_auth_type=$KAS_Authtype"
  113. params="$params&kas_auth_data=$KAS_Authdata"
  114. params="$params&kas_action=get_domains"
  115. _debug2 "Wait for 10 seconds by default before calling KAS API."
  116. sleep 10
  117. response="$(_get "$KAS_Api$params")"
  118. _debug2 "response" "$response"
  119. _zonen="$( echo "$response" | tr -d "\n\r" | tr -d " " | tr '[]' '<>' | sed "s/=>Array/\n=> Array/g" | tr ' ' '\n' | grep "domain_name" | tr '<' '\n' | grep "domain_name" | sed "s/domain_name>=>//g")"
  120. _domain="$1"
  121. _temp_domain="$(echo "$1" | sed 's/\.$//')"
  122. _rootzone="$_domain"
  123. for i in $_zonen; do
  124. l1=${#_rootzone}
  125. l2=${#i}
  126. if _endswith "$_domain" "$i" && [ "$l1" -ge "$l2" ]; then
  127. _rootzone="$i"
  128. fi
  129. done
  130. _zone="${_rootzone}."
  131. _temp_record_name="$(echo "$_temp_domain" | sed "s/"$_rootzone"//g")"
  132. _record_name="$(echo "$_temp_record_name" | sed 's/\.$//')"
  133. _debug2 "Zone:" "$_zone"
  134. _debug2 "Domain:" "$_domain"
  135. _debug2 "Record_Name:" "$_record_name"
  136. return 0
  137. }
  138. # Retrieve the DNS record ID
  139. _get_record_id() {
  140. params="?kas_login=$KAS_Login"
  141. params="$params&kas_auth_type=$KAS_Authtype"
  142. params="$params&kas_auth_data=$KAS_Authdata"
  143. params="$params&kas_action=get_dns_settings"
  144. params="$params&var1=zone_host"
  145. params="$params&wert1=$_zone"
  146. _debug2 "Wait for 10 seconds by default before calling KAS API."
  147. sleep 10
  148. response="$(_get "$KAS_Api$params")"
  149. _debug2 "response" "$response"
  150. _record_id="$(echo "$response" | tr -d "\n\r" | tr -d " " | tr '[]' '<>' | sed "s/=>Array/\n=> Array/g" | tr ' ' '\n' | grep "=>$_record_name<" | grep '>TXT<' | tr '<' '\n' | grep record_id | sed "s/record_id>=>//g")"
  151. _debug2 _record_id "$_record_id"
  152. return 0
  153. }