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.

107 lines
2.2 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_knot_info='Knot Server knsupdate
  4. Site: www.knot-dns.cz/docs/2.5/html/man_knsupdate.html
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_knot
  6. Options:
  7. KNOT_SERVER Server hostname. Default: "localhost".
  8. KNOT_KEY File path to TSIG key
  9. '
  10. # See also dns_nsupdate.sh
  11. ######## Public functions #####################
  12. #Usage: dns_knot_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. dns_knot_add() {
  14. fulldomain=$1
  15. txtvalue=$2
  16. _checkKey || return 1
  17. [ -n "${KNOT_SERVER}" ] || KNOT_SERVER="localhost"
  18. # save the dns server and key to the account.conf file.
  19. _saveaccountconf KNOT_SERVER "${KNOT_SERVER}"
  20. _saveaccountconf KNOT_KEY "${KNOT_KEY}"
  21. if ! _get_root "$fulldomain"; then
  22. _err "Domain does not exist."
  23. return 1
  24. fi
  25. _info "Adding ${fulldomain}. 60 TXT \"${txtvalue}\""
  26. knsupdate <<EOF
  27. server ${KNOT_SERVER}
  28. key ${KNOT_KEY}
  29. zone ${_domain}.
  30. update add ${fulldomain}. 60 TXT "${txtvalue}"
  31. send
  32. quit
  33. EOF
  34. if [ $? -ne 0 ]; then
  35. _err "Error updating domain."
  36. return 1
  37. fi
  38. _info "Domain TXT record successfully added."
  39. return 0
  40. }
  41. #Usage: dns_knot_rm _acme-challenge.www.domain.com
  42. dns_knot_rm() {
  43. fulldomain=$1
  44. _checkKey || return 1
  45. [ -n "${KNOT_SERVER}" ] || KNOT_SERVER="localhost"
  46. if ! _get_root "$fulldomain"; then
  47. _err "Domain does not exist."
  48. return 1
  49. fi
  50. _info "Removing ${fulldomain}. TXT"
  51. knsupdate <<EOF
  52. server ${KNOT_SERVER}
  53. key ${KNOT_KEY}
  54. zone ${_domain}.
  55. update del ${fulldomain}. TXT
  56. send
  57. quit
  58. EOF
  59. if [ $? -ne 0 ]; then
  60. _err "error updating domain"
  61. return 1
  62. fi
  63. _info "Domain TXT record successfully deleted."
  64. return 0
  65. }
  66. #################### Private functions below ##################################
  67. # _acme-challenge.www.domain.com
  68. # returns
  69. # _domain=domain.com
  70. _get_root() {
  71. domain=$1
  72. i="$(echo "$fulldomain" | tr '.' ' ' | wc -w)"
  73. i=$(_math "$i" - 1)
  74. while true; do
  75. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  76. if [ -z "$h" ]; then
  77. return 1
  78. fi
  79. _domain="$h"
  80. return 0
  81. done
  82. _debug "$domain not found"
  83. return 1
  84. }
  85. _checkKey() {
  86. if [ -z "${KNOT_KEY}" ]; then
  87. _err "You must specify a TSIG key to authenticate the request."
  88. return 1
  89. fi
  90. }