-
Notifications
You must be signed in to change notification settings - Fork 4
/
dhclient-exit-hook
89 lines (79 loc) · 2.13 KB
/
dhclient-exit-hook
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/sh
set -e
# allow overriding resolv.conf location for testing
RESOLV_CONF=${RESOLV_CONF:-/etc/resolv.conf}
. /etc/adblock.conf
layout=unknown
case `uname -s` in
FreeBSD )
layout=freebsd ;;
OpenBSD )
layout=openbsd ;;
Linux )
if test -f /etc/debian_version || test -f /etc/devuan_version; then
layout=debian
fi
;;
esac
case $layout in
debian)
NAMED_CONF=${NAMED_ETC}/named.conf.options
;;
freebsd)
NAMED_CONF=${NAMED_ETC}/named.conf
;;
*)
echo "Bad layout: $layout" 1>&2
exit 1
;;
esac
actual_ns=`egrep '^\s*nameserver' $RESOLV_CONF |awk '{print $2}'`
if test -n "$PASSTHROUGH_NAMESERVERS"; then
for ns in $PASSTHROUGH_NAMESERVERS 127.0.0.1; do
for ans in $actual_ns; do
if test "$ans" = "$ns"; then
exit
fi
done
done
fi
inplace_sed() {
if test `uname -s` = Linux; then
sed -i "$@"
else
# BSD
sed -i "" "$@"
fi
}
forwarders() {
printf 'forwarders {'
for ans in $actual_ns; do
printf "$ans; "
done
printf '};'
}
#set -x
case $reason in
"BOUND"|"RENEW"|"REBIND"|"REBOOT")
# use DNS servers obtained by dhclient as forwarders
# don't rely on spaces inside forwarders declaration in case the previous run left a blank forwarders line
# note: -E and -i switches to sed are non-portable and won't work on openbsd, in particular
if egrep -q '^[ \t]*forwarders \{.*\};' $NAMED_CONF; then
inplace_sed -E -e 's/^[ \t]*forwarders \{.*\};/'" `forwarders` "/ $NAMED_CONF
elif egrep -q '^\s*//\s*forwarders\b' $NAMED_CONF; then
inplace_sed -E -e 's:^(\s*//\s*forwarders .*)$:'" `forwarders`"\\n\\1: $NAMED_CONF
else
echo "Warning: couldn't add forwarders statement to $NAMED_CONF" 1>&2
exit 0
fi
# tell name server to use new forwarders
rndc reload
# replace nameserver declarations in resolv.conf
tempfile=`mktemp -t dhclient_hook.XXXXXX`
<$RESOLV_CONF sed -e '/^nameserver/d' > $tempfile
echo nameserver 127.0.0.1 >> $tempfile
# copy instead of moving to preserve resolv.conf's permissions and ownership
cp $tempfile $RESOLV_CONF
rm $tempfile
;;
esac