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.

153 lines
5.1 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. # TODO: Does not work with Domains that have double endings like i.e. 'co.uk'
  16. # => Get all root zones and compare once the provider offers that.
  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 or Updating $_fulldomain DNS TXT entry on All-inkl/Kasserver"
  24. _check_and_save
  25. _get_zone "$_fulldomain"
  26. _get_record_name "$_fulldomain"
  27. _get_record_id
  28. _info "Creating TXT DNS record"
  29. params="?kas_login=$KAS_Login"
  30. params="$params&kas_auth_type=$KAS_Authtype"
  31. params="$params&kas_auth_data=$KAS_Authdata"
  32. params="$params&var1=record_name"
  33. params="$params&wert1=$_record_name"
  34. params="$params&var2=record_type"
  35. params="$params&wert2=TXT"
  36. params="$params&var3=record_data"
  37. params="$params&wert3=$_txtvalue"
  38. params="$params&var4=record_aux"
  39. params="$params&wert4=0"
  40. params="$params&kas_action=add_dns_settings"
  41. params="$params&var5=zone_host"
  42. params="$params&wert5=$_zone"
  43. _debug2 "Wait for 10 seconds by default before calling KAS API."
  44. sleep 10
  45. response="$(_get "$KAS_Api$params")"
  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. _check_and_save
  60. _get_zone "$_fulldomain"
  61. _get_record_name "$_fulldomain"
  62. _get_record_id
  63. # If there is a record_id, delete the entry
  64. if [ -n "$_record_id" ]; then
  65. params="?kas_login=$KAS_Login"
  66. params="$params&kas_auth_type=$KAS_Authtype"
  67. params="$params&kas_auth_data=$KAS_Authdata"
  68. params="$params&kas_action=delete_dns_settings"
  69. for i in $_record_id; do
  70. params2="$params&var1=record_id"
  71. params2="$params2&wert1=$i"
  72. _debug2 "Wait for 10 seconds by default before calling KAS API."
  73. sleep 10
  74. response="$(_get "$KAS_Api$params2")"
  75. _debug2 "response" "$response"
  76. if ! _contains "$response" "TRUE"; then
  77. _err "Either the txt record is not found or another error occurred, please check manually."
  78. return 1
  79. fi
  80. done
  81. else # Cannot delete or unkown error
  82. _err "No record_id found that can be deleted. Please check manually."
  83. return 1
  84. fi
  85. return 0
  86. }
  87. ########################## PRIVATE FUNCTIONS ###########################
  88. # Checks for the ENV variables and saves them
  89. _check_and_save() {
  90. KAS_Login="${KAS_Login:-$(_readaccountconf_mutable KAS_Login)}"
  91. KAS_Authtype="${KAS_Authtype:-$(_readaccountconf_mutable KAS_Authtype)}"
  92. KAS_Authdata="${KAS_Authdata:-$(_readaccountconf_mutable KAS_Authdata)}"
  93. if [ -z "$KAS_Login" ] || [ -z "$KAS_Authtype" ] || [ -z "$KAS_Authdata" ]; then
  94. KAS_Login=
  95. KAS_Authtype=
  96. KAS_Authdata=
  97. _err "No auth details provided. Please set user credentials using the \$KAS_Login, \$KAS_Authtype, and \$KAS_Authdata environment variables."
  98. return 1
  99. fi
  100. _saveaccountconf_mutable KAS_Login "$KAS_Login"
  101. _saveaccountconf_mutable KAS_Authtype "$KAS_Authtype"
  102. _saveaccountconf_mutable KAS_Authdata "$KAS_Authdata"
  103. return 0
  104. }
  105. # Gets back the base domain/zone.
  106. # TODO Get a list of all possible root zones and compare (Currently not possible via provider)
  107. # See: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
  108. _get_zone() {
  109. _zone=$(echo "$1" | rev | cut -d . -f1-2 | rev).
  110. return 0
  111. }
  112. # Removes the domain/subdomain from the entry since kasserver
  113. # cannot handle _fulldomain
  114. # TODO Get a list of all possible root zones and compare (Currently not possible via provider)
  115. # See: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
  116. _get_record_name() {
  117. _record_name=$(echo "$1" | rev | cut -d"." -f3- | rev)
  118. return 0
  119. }
  120. # Retrieve the DNS record ID
  121. _get_record_id() {
  122. params="?kas_login=$KAS_Login"
  123. params="$params&kas_auth_type=$KAS_Authtype"
  124. params="$params&kas_auth_data=$KAS_Authdata"
  125. params="$params&kas_action=get_dns_settings"
  126. params="$params&var1=zone_host"
  127. params="$params&wert1=$_zone"
  128. _debug2 "Wait for 10 seconds by default before calling KAS API."
  129. sleep 10
  130. response="$(_get "$KAS_Api$params")"
  131. _debug2 "response" "$response"
  132. _record_id="$(echo "$response" | tr -d "\n\r" | sed "s/=> Array/\n=> Array/g" | tr -d " " | tr '[]' '<>' | grep "=>$_record_name<" | grep '>TXT<' | tr '<' '\n' | grep record_id | cut -d '>' -f 3)"
  133. _debug2 _record_id "$_record_id"
  134. return 0
  135. }