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.0 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. _full_domain=$1
  21. _txt_value=$2
  22. _info "Using DNS-01 All-inkl/Kasserver hook"
  23. _info "Adding or Updating $_full_domain DNS TXT entry on All-inkl/Kasserver"
  24. _check_and_save
  25. _get_zone "$_full_domain"
  26. _get_record_name "$_full_domain"
  27. _get_record_id
  28. params="?kas_login=$KAS_Login"
  29. params="$params&kas_auth_type=$KAS_Authtype"
  30. params="$params&kas_auth_data=$KAS_Authdata"
  31. params="$params&var1=record_name"
  32. params="$params&wert1=$_record_name"
  33. params="$params&var2=record_type"
  34. params="$params&wert2=TXT"
  35. params="$params&var3=record_data"
  36. params="$params&wert3=$_txt_value"
  37. params="$params&var4=record_aux"
  38. params="$params&wert4=0"
  39. # If there is no record_id create the record
  40. if [ -z "$_record_id" ]; then
  41. _info "Creating TXT DNS record"
  42. params="$params&kas_action=add_dns_settings"
  43. params="$params&var5=zone_host"
  44. params="$params&wert5=$_zone"
  45. else # Update the existing record
  46. _info "Updating existing TXT DNS record"
  47. params="$params&kas_action=update_dns_settings"
  48. params="$params&var5=record_id"
  49. params="$params&wert5=$_record_id"
  50. fi
  51. response="$(_get "$KAS_Api$params")"
  52. _debug2 "response" "$response"
  53. if ! _contains "$response" "TRUE"; then
  54. _err "An unkown error occurred, please check manually."
  55. return 1
  56. fi
  57. return 0
  58. }
  59. dns_kas_rm() {
  60. _full_domain=$1
  61. _txt_value=$2
  62. _info "Using DNS-01 All-inkl/Kasserver hook"
  63. _info "Cleaning up after All-inkl/Kasserver hook"
  64. _info "Removing $_full_domain DNS TXT entry on All-inkl/Kasserver"
  65. _check_and_save
  66. _get_zone "$_full_domain"
  67. _get_record_name "$_full_domain"
  68. _get_record_id
  69. # If there is a record_id, delete the entry
  70. if [ -n "$_record_id" ]; then
  71. params="?kas_login=$KAS_Login"
  72. params="$params&kas_auth_type=$KAS_Authtype"
  73. params="$params&kas_auth_data=$KAS_Authdata"
  74. params="$params&kas_action=delete_dns_settings"
  75. params="$params&var1=record_id"
  76. params="$params&wert1=$_record_id"
  77. response="$(_get "$KAS_Api$params")"
  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. else # Cannot delete or unkown error
  84. _err "No record_id found that can be deleted. Please check manually."
  85. return 1
  86. fi
  87. return 0
  88. }
  89. ########################## PRIVATE FUNCTIONS ###########################
  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.
  108. # TODO Get a list of all possible root zones and compare (Currently not possible via provider)
  109. # See: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
  110. _get_zone() {
  111. _zone=$(echo "$1" | rev | cut -d . -f1-2 | rev).
  112. }
  113. # Removes the domain/subdomain from the entry since kasserver
  114. # cannot handle _full_domain
  115. # TODO Get a list of all possible root zones and compare (Currently not possible via provider)
  116. # See: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
  117. _get_record_name() {
  118. _record_name=$(echo "$1" | rev | cut -d"." -f3- | rev)
  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. response="$(_get "$KAS_Api$params")"
  129. _debug2 "response" "$response"
  130. _record_id="$(echo "$response" | grep -A 4 "$_record_name" | grep "record_id" | cut -f2 -d">" | xargs)"
  131. _debug2 _record_id "$_record_id"
  132. return 0
  133. }