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.

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