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.

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