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
2.5 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_nsd_info='NLnetLabs NSD Server
  4. Site: github.com/NLnetLabs/nsd
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#nsd
  6. Options:
  7. Nsd_ZoneFile Zone File path. E.g. "/etc/nsd/zones/example.com.zone"
  8. Nsd_Command Command. E.g. "sudo nsd-control reload"
  9. Issues: github.com/acmesh-official/acme.sh/issues/2245
  10. '
  11. # args: zonefile
  12. _local_nsd_get_serial()
  13. {
  14. local _zone_file="$1"
  15. cat "$_zone_file" | \
  16. sed -n '/IN[ \t]*SOA.*/,/[)]/p' | \
  17. sed 's/\([^;]\);\(.*\)/\1/g' | \
  18. sed -z 's/\n//g' | \
  19. sed 's/\([^(]*\)[(]\([^)]*\)[)]/\2/g' | \
  20. sed 's/\([ \t]*\)\([0-9]*\)\(.*\)/\2/g'
  21. }
  22. # args: fulldomain txtvalue
  23. dns_nsd_add() {
  24. fulldomain=$1
  25. txtvalue=$2
  26. ttlvalue=300
  27. Nsd_ZoneFile="${Nsd_ZoneFile:-$(_readdomainconf Nsd_ZoneFile)}"
  28. Nsd_Command="${Nsd_Command:-$(_readdomainconf Nsd_Command)}"
  29. # Arg checks
  30. if [ -z "$Nsd_ZoneFile" ] || [ -z "$Nsd_Command" ]; then
  31. Nsd_ZoneFile=""
  32. Nsd_Command=""
  33. _err "Specify ENV vars Nsd_ZoneFile and Nsd_Command"
  34. return 1
  35. fi
  36. if [ ! -f "$Nsd_ZoneFile" ]; then
  37. Nsd_ZoneFile=""
  38. Nsd_Command=""
  39. _err "No such file: $Nsd_ZoneFile"
  40. return 1
  41. fi
  42. _savedomainconf Nsd_ZoneFile "$Nsd_ZoneFile"
  43. _savedomainconf Nsd_Command "$Nsd_Command"
  44. echo "$fulldomain. $ttlvalue IN TXT \"$txtvalue\"" >>"$Nsd_ZoneFile"
  45. # Updating serial. The idea is that we'll parse out the old serial first,
  46. # generate a new one by incrementing, then sed-replace the old by the new one.
  47. local zone_serial=$(_local_nsd_get_serial "$Nsd_ZoneFile")
  48. local zone_serial_next=$[$zone_serial+1]
  49. local tmp_zonefile=$(mktemp)
  50. cat "$Nsd_ZoneFile" | sed "s/$zone_serial/$zone_serial_next/" > "$tmp_zonefile"
  51. cat "$tmp_zonefile" > "$Nsd_ZoneFile"
  52. rm -rf "$tmp_zonefile"
  53. _info "Added TXT record for $fulldomain"
  54. _debug "Running $Nsd_Command"
  55. if eval "$Nsd_Command"; then
  56. _info "Successfully updated the zone"
  57. return 0
  58. else
  59. _err "Problem updating the zone"
  60. return 1
  61. fi
  62. }
  63. # args: fulldomain txtvalue
  64. dns_nsd_rm() {
  65. fulldomain=$1
  66. txtvalue=$2
  67. ttlvalue=300
  68. Nsd_ZoneFile="${Nsd_ZoneFile:-$(_readdomainconf Nsd_ZoneFile)}"
  69. Nsd_Command="${Nsd_Command:-$(_readdomainconf Nsd_Command)}"
  70. _sed_i "/$fulldomain. $ttlvalue IN TXT \"$txtvalue\"/d" "$Nsd_ZoneFile"
  71. _info "Removed TXT record for $fulldomain"
  72. _debug "Running $Nsd_Command"
  73. if eval "$Nsd_Command"; then
  74. _info "Successfully reloaded NSD "
  75. return 0
  76. else
  77. _err "Problem reloading NSD"
  78. return 1
  79. fi
  80. }