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.

97 lines
1.9 KiB

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