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.

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