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.

84 lines
2.5 KiB

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