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.

79 lines
2.5 KiB

8 years ago
  1. #!/usr/bin/env sh
  2. ######## Public functions #####################
  3. ECHO=$(command -v echo)
  4. NSUPDATE=$(command -v nsupdate)
  5. NSUPDATE_COMMANDS_FILE="/tmp/nsupdate"
  6. #Usage: dns_nsupdate_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  7. dns_nsupdate_add() {
  8. fulldomain=$1
  9. txtvalue=$2
  10. _checkKeyFile || return 1
  11. [ -n "${NSUPDATE_SERVER}" ] || NSUPDATE_SERVER="localhost"
  12. [ -n "${NSUPDATE_SERVER_PORT}" ] || NSUPDATE_SERVER_PORT=53
  13. # save the dns server and key to the account conf file.
  14. _saveaccountconf NSUPDATE_SERVER "${NSUPDATE_SERVER}"
  15. _saveaccountconf NSUPDATE_SERVER_PORT "${NSUPDATE_SERVER_PORT}"
  16. _saveaccountconf NSUPDATE_KEY "${NSUPDATE_KEY}"
  17. if ! [ -z "$NSUPDATE_ZONE" ]; then
  18. _saveaccountconf NSUPDATE_ZONE "${NSUPDATE_ZONE}"
  19. fi
  20. _info "adding ${fulldomain}. 60 in txt \"${txtvalue}\""
  21. $ECHO "server ${NSUPDATE_SERVER} ${NSUPDATE_SERVER_PORT}" >${NSUPDATE_COMMANDS_FILE}
  22. if ! [ -z "$NSUPDATE_ZONE" ]; then
  23. $ECHO "zone ${NSUPDATE_ZONE}" >>${NSUPDATE_COMMANDS_FILE}
  24. fi
  25. $ECHO "update add ${fulldomain}. 60 in txt \"${txtvalue}\"" >>${NSUPDATE_COMMANDS_FILE}
  26. $ECHO "send" >>${NSUPDATE_COMMANDS_FILE}
  27. _debug "$(cat ${NSUPDATE_COMMANDS_FILE})"
  28. if ! $NSUPDATE -k "${NSUPDATE_KEY}" -v ${NSUPDATE_COMMANDS_FILE}; then
  29. _err "error updating domain"
  30. rm ${NSUPDATE_COMMANDS_FILE}
  31. return 1
  32. fi
  33. rm ${NSUPDATE_COMMANDS_FILE}
  34. return 0
  35. }
  36. #Usage: dns_nsupdate_rm _acme-challenge.www.domain.com
  37. dns_nsupdate_rm() {
  38. fulldomain=$1
  39. _checkKeyFile || return 1
  40. [ -n "${NSUPDATE_SERVER}" ] || NSUPDATE_SERVER="localhost"
  41. [ -n "${NSUPDATE_SERVER_PORT}" ] || NSUPDATE_SERVER_PORT=53
  42. _info "removing ${fulldomain}. txt"
  43. $ECHO "server ${NSUPDATE_SERVER} ${NSUPDATE_SERVER_PORT}" >${NSUPDATE_COMMANDS_FILE}
  44. if ! [ -z "$NSUPDATE_ZONE" ]; then
  45. $ECHO "zone ${NSUPDATE_ZONE}" >>${NSUPDATE_COMMANDS_FILE}
  46. fi
  47. $ECHO "update delete ${fulldomain}. txt" >>${NSUPDATE_COMMANDS_FILE}
  48. $ECHO "send" >>${NSUPDATE_COMMANDS_FILE}
  49. _debug "$(cat ${NSUPDATE_COMMANDS_FILE})"
  50. if ! $NSUPDATE -k "${NSUPDATE_KEY}" -v ${NSUPDATE_COMMANDS_FILE}; then
  51. _err "error updating domain"
  52. rm ${NSUPDATE_COMMANDS_FILE}
  53. return 1
  54. fi
  55. rm ${NSUPDATE_COMMANDS_FILE}
  56. return 0
  57. }
  58. #################### Private functions below ##################################
  59. _checkKeyFile() {
  60. if [ -z "${NSUPDATE_KEY}" ]; then
  61. _err "you must specify a path to the nsupdate key file"
  62. return 1
  63. fi
  64. if [ ! -r "${NSUPDATE_KEY}" ]; then
  65. _err "key ${NSUPDATE_KEY} is unreadable"
  66. return 1
  67. fi
  68. }