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.

150 lines
4.9 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. params="$params&var1=record_id"
  70. params="$params&wert1=$_record_id"
  71. _debug2 "Wait for 10 seconds by default before calling KAS API."
  72. sleep 10
  73. response="$(_get "$KAS_Api$params")"
  74. _debug2 "response" "$response"
  75. if ! _contains "$response" "TRUE"; then
  76. _err "Either the txt record is not found or another error occurred, please check manually."
  77. return 1
  78. fi
  79. else # Cannot delete or unkown error
  80. _err "No record_id found that can be deleted. Please check manually."
  81. return 1
  82. fi
  83. return 0
  84. }
  85. ########################## PRIVATE FUNCTIONS ###########################
  86. # Checks for the ENV variables and saves them
  87. _check_and_save() {
  88. KAS_Login="${KAS_Login:-$(_readaccountconf_mutable KAS_Login)}"
  89. KAS_Authtype="${KAS_Authtype:-$(_readaccountconf_mutable KAS_Authtype)}"
  90. KAS_Authdata="${KAS_Authdata:-$(_readaccountconf_mutable KAS_Authdata)}"
  91. if [ -z "$KAS_Login" ] || [ -z "$KAS_Authtype" ] || [ -z "$KAS_Authdata" ]; then
  92. KAS_Login=
  93. KAS_Authtype=
  94. KAS_Authdata=
  95. _err "No auth details provided. Please set user credentials using the \$KAS_Login, \$KAS_Authtype, and \$KAS_Authdata environment variables."
  96. return 1
  97. fi
  98. _saveaccountconf_mutable KAS_Login "$KAS_Login"
  99. _saveaccountconf_mutable KAS_Authtype "$KAS_Authtype"
  100. _saveaccountconf_mutable KAS_Authdata "$KAS_Authdata"
  101. return 0
  102. }
  103. # Gets back the base domain/zone.
  104. # TODO Get a list of all possible root zones and compare (Currently not possible via provider)
  105. # See: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
  106. _get_zone() {
  107. _zone=$(echo "$1" | rev | cut -d . -f1-2 | rev).
  108. return 0
  109. }
  110. # Removes the domain/subdomain from the entry since kasserver
  111. # cannot handle _fulldomain
  112. # TODO Get a list of all possible root zones and compare (Currently not possible via provider)
  113. # See: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
  114. _get_record_name() {
  115. _record_name=$(echo "$1" | rev | cut -d"." -f3- | rev)
  116. return 0
  117. }
  118. # Retrieve the DNS record ID
  119. _get_record_id() {
  120. params="?kas_login=$KAS_Login"
  121. params="$params&kas_auth_type=$KAS_Authtype"
  122. params="$params&kas_auth_data=$KAS_Authdata"
  123. params="$params&kas_action=get_dns_settings"
  124. params="$params&var1=zone_host"
  125. params="$params&wert1=$_zone"
  126. _debug2 "Wait for 10 seconds by default before calling KAS API."
  127. sleep 10
  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. }