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.

108 lines
3.6 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. _checkKeyFile || return 1
  22. # save the dns server and key to the account conf file.
  23. _saveaccountconf_mutable NSUPDATE_SERVER "${NSUPDATE_SERVER}"
  24. _saveaccountconf_mutable NSUPDATE_SERVER_PORT "${NSUPDATE_SERVER_PORT}"
  25. _saveaccountconf_mutable NSUPDATE_KEY "${NSUPDATE_KEY}"
  26. _saveaccountconf_mutable NSUPDATE_ZONE "${NSUPDATE_ZONE}"
  27. [ -n "${NSUPDATE_SERVER}" ] || NSUPDATE_SERVER="localhost"
  28. [ -n "${NSUPDATE_SERVER_PORT}" ] || NSUPDATE_SERVER_PORT=53
  29. _info "adding ${fulldomain}. 60 in txt \"${txtvalue}\""
  30. [ -n "$DEBUG" ] && [ "$DEBUG" -ge "$DEBUG_LEVEL_1" ] && nsdebug="-d"
  31. [ -n "$DEBUG" ] && [ "$DEBUG" -ge "$DEBUG_LEVEL_2" ] && nsdebug="-D"
  32. if [ -z "${NSUPDATE_ZONE}" ]; then
  33. nsupdate -k "${NSUPDATE_KEY}" $nsdebug <<EOF
  34. server ${NSUPDATE_SERVER} ${NSUPDATE_SERVER_PORT}
  35. update add ${fulldomain}. 60 in txt "${txtvalue}"
  36. send
  37. EOF
  38. else
  39. nsupdate -k "${NSUPDATE_KEY}" $nsdebug <<EOF
  40. server ${NSUPDATE_SERVER} ${NSUPDATE_SERVER_PORT}
  41. zone ${NSUPDATE_ZONE}.
  42. update add ${fulldomain}. 60 in txt "${txtvalue}"
  43. send
  44. EOF
  45. fi
  46. if [ $? -ne 0 ]; then
  47. _err "error updating domain"
  48. return 1
  49. fi
  50. return 0
  51. }
  52. #Usage: dns_nsupdate_rm _acme-challenge.www.domain.com
  53. dns_nsupdate_rm() {
  54. fulldomain=$1
  55. NSUPDATE_SERVER="${NSUPDATE_SERVER:-$(_readaccountconf_mutable NSUPDATE_SERVER)}"
  56. NSUPDATE_SERVER_PORT="${NSUPDATE_SERVER_PORT:-$(_readaccountconf_mutable NSUPDATE_SERVER_PORT)}"
  57. NSUPDATE_KEY="${NSUPDATE_KEY:-$(_readaccountconf_mutable NSUPDATE_KEY)}"
  58. NSUPDATE_ZONE="${NSUPDATE_ZONE:-$(_readaccountconf_mutable NSUPDATE_ZONE)}"
  59. _checkKeyFile || return 1
  60. [ -n "${NSUPDATE_SERVER}" ] || NSUPDATE_SERVER="localhost"
  61. [ -n "${NSUPDATE_SERVER_PORT}" ] || NSUPDATE_SERVER_PORT=53
  62. _info "removing ${fulldomain}. txt"
  63. [ -n "$DEBUG" ] && [ "$DEBUG" -ge "$DEBUG_LEVEL_1" ] && nsdebug="-d"
  64. [ -n "$DEBUG" ] && [ "$DEBUG" -ge "$DEBUG_LEVEL_2" ] && nsdebug="-D"
  65. if [ -z "${NSUPDATE_ZONE}" ]; then
  66. nsupdate -k "${NSUPDATE_KEY}" $nsdebug <<EOF
  67. server ${NSUPDATE_SERVER} ${NSUPDATE_SERVER_PORT}
  68. update delete ${fulldomain}. txt
  69. send
  70. EOF
  71. else
  72. nsupdate -k "${NSUPDATE_KEY}" $nsdebug <<EOF
  73. server ${NSUPDATE_SERVER} ${NSUPDATE_SERVER_PORT}
  74. zone ${NSUPDATE_ZONE}.
  75. update delete ${fulldomain}. txt
  76. send
  77. EOF
  78. fi
  79. if [ $? -ne 0 ]; then
  80. _err "error updating domain"
  81. return 1
  82. fi
  83. return 0
  84. }
  85. #################### Private functions below ##################################
  86. _checkKeyFile() {
  87. if [ -z "${NSUPDATE_KEY}" ]; then
  88. _err "you must specify a path to the nsupdate key file"
  89. return 1
  90. fi
  91. if [ ! -r "${NSUPDATE_KEY}" ]; then
  92. _err "key ${NSUPDATE_KEY} is unreadable"
  93. return 1
  94. fi
  95. }