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.

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