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.

157 lines
5.2 KiB

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