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.

94 lines
1.9 KiB

  1. #!/usr/bin/env bash
  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. NSUPDATE_SERVER=${NSUPDATE_SERVER:-localhost}
  9. # save the dns server and key to the account conf file.
  10. _saveaccountconf NSUPDATE_SERVER "${NSUPDATE_SERVER}"
  11. _saveaccountconf NSUPDATE_KEY "${NSUPDATE_KEY}"
  12. tmp=$(mktemp --tmpdir acme_nsupdate.XXXXXX)
  13. cat > ${tmp} <<EOF
  14. server ${NSUPDATE_SERVER}
  15. update add ${fulldomain}. 60 in txt "${txtvalue}"
  16. send
  17. EOF
  18. _info "adding ${fulldomain}. 60 in txt \"${txtvalue}\""
  19. nsupdate -k ${NSUPDATE_KEY} ${tmp}
  20. if [ $? -ne 0 ]; then
  21. _err "error updating domain, see ${tmp} for details"
  22. return 1
  23. fi
  24. rm -f ${tmp}
  25. return 0
  26. }
  27. #Usage: dns_nsupdate_rm _acme-challenge.www.domain.com
  28. dns_nsupdate_rm() {
  29. fulldomain=$1
  30. _checkKeyFile || return 1
  31. NSUPDATE_SERVER=${NSUPDATE_SERVER:-localhost}
  32. tmp=$(mktemp --tmpdir acme_nsupdate.XXXXXX)
  33. cat > ${tmp} <<EOF
  34. server ${NSUPDATE_SERVER}
  35. update delete ${fulldomain}. txt
  36. send
  37. EOF
  38. _info "removing ${fulldomain}. txt"
  39. nsupdate -k ${NSUPDATE_KEY} ${tmp}
  40. if [ $? -ne 0 ]; then
  41. _err "error updating domain, see ${tmp} for details"
  42. return 1
  43. fi
  44. rm -f ${tmp}
  45. return 0
  46. }
  47. #################### Private functions bellow ##################################
  48. _checkKeyFile() {
  49. if [ -z "${NSUPDATE_KEY}" ]; then
  50. _err "you must specify a path to the nsupdate key file"
  51. return 1
  52. fi
  53. if [ ! -r "${NSUPDATE_KEY}" ]; then
  54. _err "key ${NSUPDATE_KEY} is unreadable"
  55. return 1
  56. fi
  57. }
  58. _info() {
  59. if [ -z "$2" ] ; then
  60. echo "[$(date)] $1"
  61. else
  62. echo "[$(date)] $1='$2'"
  63. fi
  64. }
  65. _err() {
  66. _info "$@" >&2
  67. return 1
  68. }
  69. _debug() {
  70. if [ -z "$DEBUG" ] ; then
  71. return
  72. fi
  73. _err "$@"
  74. return 0
  75. }
  76. _debug2() {
  77. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then
  78. _debug "$@"
  79. fi
  80. return
  81. }