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.

112 lines
3.9 KiB

8 years ago
8 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_nsupdate_info='nsupdate RFC 2136 DynDNS client
  4. Site: bind9.readthedocs.io/en/v9.18.19/manpages.html#nsupdate-dynamic-dns-update-utility
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_nsupdate
  6. Options:
  7. NSUPDATE_SERVER Server hostname. Default: "localhost".
  8. NSUPDATE_SERVER_PORT Server port. Default: "53".
  9. NSUPDATE_KEY File path to TSIG key.
  10. NSUPDATE_ZONE Domain zone to update. Optional.
  11. '
  12. ######## Public functions #####################
  13. #Usage: dns_nsupdate_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_nsupdate_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. NSUPDATE_SERVER="${NSUPDATE_SERVER:-$(_readaccountconf_mutable NSUPDATE_SERVER)}"
  18. NSUPDATE_SERVER_PORT="${NSUPDATE_SERVER_PORT:-$(_readaccountconf_mutable NSUPDATE_SERVER_PORT)}"
  19. NSUPDATE_KEY="${NSUPDATE_KEY:-$(_readaccountconf_mutable NSUPDATE_KEY)}"
  20. NSUPDATE_ZONE="${NSUPDATE_ZONE:-$(_readaccountconf_mutable NSUPDATE_ZONE)}"
  21. NSUPDATE_OPT="${NSUPDATE_OPT:-$(_readaccountconf_mutable NSUPDATE_OPT)}"
  22. _checkKeyFile || return 1
  23. # save the dns server and key to the account conf file.
  24. _saveaccountconf_mutable NSUPDATE_SERVER "${NSUPDATE_SERVER}"
  25. _saveaccountconf_mutable NSUPDATE_SERVER_PORT "${NSUPDATE_SERVER_PORT}"
  26. _saveaccountconf_mutable NSUPDATE_KEY "${NSUPDATE_KEY}"
  27. _saveaccountconf_mutable NSUPDATE_ZONE "${NSUPDATE_ZONE}"
  28. _saveaccountconf_mutable NSUPDATE_OPT "${NSUPDATE_OPT}"
  29. [ -n "${NSUPDATE_SERVER}" ] || NSUPDATE_SERVER="localhost"
  30. [ -n "${NSUPDATE_SERVER_PORT}" ] || NSUPDATE_SERVER_PORT=53
  31. [ -n "${NSUPDATE_OPT}" ] || NSUPDATE_OPT=""
  32. _info "adding ${fulldomain}. 60 in txt \"${txtvalue}\""
  33. [ -n "$DEBUG" ] && [ "$DEBUG" -ge "$DEBUG_LEVEL_1" ] && nsdebug="-d"
  34. [ -n "$DEBUG" ] && [ "$DEBUG" -ge "$DEBUG_LEVEL_2" ] && nsdebug="-D"
  35. if [ -z "${NSUPDATE_ZONE}" ]; then
  36. nsupdate -k "${NSUPDATE_KEY}" $nsdebug "${NSUPDATE_OPT}" <<EOF
  37. server ${NSUPDATE_SERVER} ${NSUPDATE_SERVER_PORT}
  38. update add ${fulldomain}. 60 in txt "${txtvalue}"
  39. send
  40. EOF
  41. else
  42. nsupdate -k "${NSUPDATE_KEY}" $nsdebug "${NSUPDATE_OPT}" <<EOF
  43. server ${NSUPDATE_SERVER} ${NSUPDATE_SERVER_PORT}
  44. zone ${NSUPDATE_ZONE}.
  45. update add ${fulldomain}. 60 in txt "${txtvalue}"
  46. send
  47. EOF
  48. fi
  49. if [ $? -ne 0 ]; then
  50. _err "error updating domain"
  51. return 1
  52. fi
  53. return 0
  54. }
  55. #Usage: dns_nsupdate_rm _acme-challenge.www.domain.com
  56. dns_nsupdate_rm() {
  57. fulldomain=$1
  58. NSUPDATE_SERVER="${NSUPDATE_SERVER:-$(_readaccountconf_mutable NSUPDATE_SERVER)}"
  59. NSUPDATE_SERVER_PORT="${NSUPDATE_SERVER_PORT:-$(_readaccountconf_mutable NSUPDATE_SERVER_PORT)}"
  60. NSUPDATE_KEY="${NSUPDATE_KEY:-$(_readaccountconf_mutable NSUPDATE_KEY)}"
  61. NSUPDATE_ZONE="${NSUPDATE_ZONE:-$(_readaccountconf_mutable NSUPDATE_ZONE)}"
  62. NSUPDATE_OPT="${NSUPDATE_OPT:-$(_readaccountconf_mutable NSUPDATE_OPT)}"
  63. _checkKeyFile || return 1
  64. [ -n "${NSUPDATE_SERVER}" ] || NSUPDATE_SERVER="localhost"
  65. [ -n "${NSUPDATE_SERVER_PORT}" ] || NSUPDATE_SERVER_PORT=53
  66. _info "removing ${fulldomain}. txt"
  67. [ -n "$DEBUG" ] && [ "$DEBUG" -ge "$DEBUG_LEVEL_1" ] && nsdebug="-d"
  68. [ -n "$DEBUG" ] && [ "$DEBUG" -ge "$DEBUG_LEVEL_2" ] && nsdebug="-D"
  69. if [ -z "${NSUPDATE_ZONE}" ]; then
  70. nsupdate -k "${NSUPDATE_KEY}" $nsdebug "${NSUPDATE_OPT}" <<EOF
  71. server ${NSUPDATE_SERVER} ${NSUPDATE_SERVER_PORT}
  72. update delete ${fulldomain}. txt
  73. send
  74. EOF
  75. else
  76. nsupdate -k "${NSUPDATE_KEY}" $nsdebug "${NSUPDATE_OPT}" <<EOF
  77. server ${NSUPDATE_SERVER} ${NSUPDATE_SERVER_PORT}
  78. zone ${NSUPDATE_ZONE}.
  79. update delete ${fulldomain}. txt
  80. send
  81. EOF
  82. fi
  83. if [ $? -ne 0 ]; then
  84. _err "error updating domain"
  85. return 1
  86. fi
  87. return 0
  88. }
  89. #################### Private functions below ##################################
  90. _checkKeyFile() {
  91. if [ -z "${NSUPDATE_KEY}" ]; then
  92. _err "you must specify a path to the nsupdate key file"
  93. return 1
  94. fi
  95. if [ ! -r "${NSUPDATE_KEY}" ]; then
  96. _err "key ${NSUPDATE_KEY} is unreadable"
  97. return 1
  98. fi
  99. }