PageRenderTime 85ms CodeModel.GetById 77ms app.highlight 6ms RepoModel.GetById 0ms app.codeStats 1ms

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