/resolvconf/update.d/bind

http://github.com/brinkman83/bashrc · #! · 102 lines · 88 code · 14 blank · 0 comment · 0 complexity · d8834d78eb75ae2f7a149ed5a7bc4124 MD5 · raw file

  1. #!/bin/bash
  2. # Need bash because we use ${foo//bar/baz}
  3. #
  4. # Script to update the named options file
  5. #
  6. # Resolvconf may run us even if named is not running.
  7. # If a bind package is installed then we go ahead and update
  8. # the named configuration in case named is started later.
  9. #
  10. # Assumption: On entry, PWD contains the resolv.conf-type files
  11. #
  12. # Licensed under the GNU GPL. See /usr/share/doc/resolvconf/copyright.
  13. #
  14. # Written by Thomas Hood <jdthood@yahoo.co.uk>
  15. set -e
  16. PATH=/sbin:/bin
  17. [ -x /usr/sbin/named ] || exit 0
  18. [ -x /lib/resolvconf/list-records ] || exit 1
  19. [ -f /etc/bind/named.conf.options ] || exit 0
  20. OPTS_FILE=named.options
  21. RUN_DIR=/var/run/bind
  22. [ -d "$RUN_DIR" ] || mkdir --parents --mode=0755 "$RUN_DIR"
  23. # Stores arguments (minus duplicates) in RSLT, separated by spaces
  24. # Doesn't work properly if an argument itself contain whitespace
  25. uniquify()
  26. {
  27. RSLT=""
  28. while [ "$1" ] ; do
  29. for E in $RSLT ; do
  30. [ "$1" = "$E" ] && { shift ; continue 2 ; }
  31. done
  32. RSLT="${RSLT:+$RSLT }$1"
  33. shift
  34. done
  35. }
  36. # Get list of records, excluding all those for the loopback interface
  37. RSLVCNFFILES="$(/lib/resolvconf/list-records | sed -e '/^lo$/d' -e '/^lo[.]/d')"
  38. ### Compile semicolon-separated list nameservers ###
  39. NMSRVRS=""
  40. if [ "$RSLVCNFFILES" ] ; then
  41. uniquify $(sed -n -e 's/^[[:space:]]*nameserver[[:space:]]\+//p' $RSLVCNFFILES)
  42. [ "$RSLT" ] && NMSRVRS="${RSLT// /; }; "
  43. fi
  44. # N.B.: After changing directory we no longer have access to the resolv.conf-type files
  45. cd "$RUN_DIR"
  46. TMP_FILE="${OPTS_FILE}_new.$$"
  47. clean_up() { rm -f "${RUN_DIR}/$TMP_FILE" ; }
  48. trap clean_up EXIT
  49. rm -f "$TMP_FILE"
  50. # We want to process named.conf.options such that the new forwarders
  51. # statement gets inserted but nothing else is corrupted in the process.
  52. # We want to do this using only commands available in /bin and /sbin, i.e.,
  53. # with sh, sed and/or grep. Sed can be made to work -- with difficulty.
  54. # Even so, the following script does not work properly if comment
  55. # delimiters of one style of commenting appear inside another kind of
  56. # comment. (Named supports C, C++ and sh comment styles.)
  57. #
  58. # First, we do our best to delete all and only comments.
  59. # Then we delete any existing forwarders statement, taking into account
  60. # the fact that these can span several lines. Then we add a new
  61. # forwarders statement at the beginning of the options statement.
  62. #
  63. echo "// named.conf fragment automatically generated by $0" > "$TMP_FILE"
  64. echo "// DO NOT EDIT THIS FILE. Instead edit /etc/bind/named.conf.options ." >> "$TMP_FILE"
  65. cat /etc/bind/named.conf.options \
  66. | sed -e 's%\*/%\*/\
  67. %g' \
  68. | sed -e '\%/\*%{ :x ; s%\*/%\*/% ; t y ; N ; b x ; :y ; s%/\*.*\*/%% ; }' \
  69. | sed -e 's%//.*%%' -e 's%#.*%%' \
  70. | sed -e '/forwarders/{ :x ; s/}/}/ ; t y ; N ; b x ; :y ; s/}[[:space:]]*;/};/ ; t z ; N ; b y ; :z s/forwarders[[:space:]]*{[^}]*};// ; }' \
  71. | sed -e 's/options[[:space:]]*{/options {\
  72. forwarders { '"${NMSRVRS}"'};/' | sed -e '/^[[:space:]]*$/{ d ; }' \
  73. >> "$TMP_FILE"
  74. # bind version 8 does not create a "bind" group
  75. chown root:bind "$TMP_FILE" > /dev/null 2>&1 || :
  76. if [ "$1" = "-i" ] ; then
  77. mv -f "$TMP_FILE" "$OPTS_FILE"
  78. exit 0
  79. fi
  80. # Reload named unless we know its options haven't changed
  81. if [ -x /usr/bin/diff ] && [ -f "$OPTS_FILE" ] && /usr/bin/diff -q "$OPTS_FILE" "$TMP_FILE" > /dev/null ; then
  82. # No change
  83. rm -f "$TMP_FILE"
  84. else
  85. mv -f "$TMP_FILE" "$OPTS_FILE"
  86. [ -x /etc/init.d/bind9 ] && /etc/init.d/bind9 reload > /dev/null 2>&1 || :
  87. [ -x /etc/init.d/bind ] && /etc/init.d/bind reload > /dev/null 2>&1 || :
  88. fi