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.

145 lines
4.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. # 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. _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=$_txt_value"
  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. 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. _full_domain=$1
  53. _txt_value=$2
  54. _info "Using DNS-01 All-inkl/Kasserver hook"
  55. _info "Cleaning up after All-inkl/Kasserver hook"
  56. _info "Removing $_full_domain DNS TXT entry on All-inkl/Kasserver"
  57. _check_and_save
  58. _get_zone "$_full_domain"
  59. _get_record_name "$_full_domain"
  60. _get_record_id
  61. # If there is a record_id, delete the entry
  62. if [ -n "$_record_id" ]; then
  63. params="?kas_login=$KAS_Login"
  64. params="$params&kas_auth_type=$KAS_Authtype"
  65. params="$params&kas_auth_data=$KAS_Authdata"
  66. params="$params&kas_action=delete_dns_settings"
  67. params="$params&var1=record_id"
  68. params="$params&wert1=$_record_id"
  69. response="$(_get "$KAS_Api$params")"
  70. _debug2 "response" "$response"
  71. if ! _contains "$response" "TRUE"; then
  72. _err "Either the txt record is not found or another error occurred, please check manually."
  73. return 1
  74. fi
  75. else # Cannot delete or unkown error
  76. _err "No record_id found that can be deleted. Please check manually."
  77. return 1
  78. fi
  79. return 0
  80. }
  81. ########################## PRIVATE FUNCTIONS ###########################
  82. # Checks for the ENV variables and saves them
  83. _check_and_save() {
  84. KAS_Login="${KAS_Login:-$(_readaccountconf_mutable KAS_Login)}"
  85. KAS_Authtype="${KAS_Authtype:-$(_readaccountconf_mutable KAS_Authtype)}"
  86. KAS_Authdata="${KAS_Authdata:-$(_readaccountconf_mutable KAS_Authdata)}"
  87. if [ -z "$KAS_Login" ] || [ -z "$KAS_Authtype" ] || [ -z "$KAS_Authdata" ]; then
  88. KAS_Login=
  89. KAS_Authtype=
  90. KAS_Authdata=
  91. _err "No auth details provided. Please set user credentials using the \$KAS_Login, \$KAS_Authtype, and \$KAS_Authdata environment variables."
  92. return 1
  93. fi
  94. _saveaccountconf_mutable KAS_Login "$KAS_Login"
  95. _saveaccountconf_mutable KAS_Authtype "$KAS_Authtype"
  96. _saveaccountconf_mutable KAS_Authdata "$KAS_Authdata"
  97. return 0
  98. }
  99. # Gets back the base domain/zone.
  100. # TODO Get a list of all possible root zones and compare (Currently not possible via provider)
  101. # See: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
  102. _get_zone() {
  103. _zone=$(echo "$1" | rev | cut -d . -f1-2 | rev).
  104. }
  105. # Removes the domain/subdomain from the entry since kasserver
  106. # cannot handle _full_domain
  107. # TODO Get a list of all possible root zones and compare (Currently not possible via provider)
  108. # See: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
  109. _get_record_name() {
  110. _record_name=$(echo "$1" | rev | cut -d"." -f3- | rev)
  111. }
  112. # Retrieve the DNS record ID
  113. _get_record_id() {
  114. params="?kas_login=$KAS_Login"
  115. params="$params&kas_auth_type=$KAS_Authtype"
  116. params="$params&kas_auth_data=$KAS_Authdata"
  117. params="$params&kas_action=get_dns_settings"
  118. params="$params&var1=zone_host"
  119. params="$params&wert1=$_zone"
  120. response="$(_get "$KAS_Api$params")"
  121. _debug2 "response" "$response"
  122. _record_id="$(echo "$response" | grep -A 4 "$_record_name" | grep "record_id" | cut -f2 -d">" | xargs)"
  123. _debug2 _record_id "$_record_id"
  124. return 0
  125. }