-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdhclient-exit-hook
More file actions
125 lines (109 loc) · 2.97 KB
/
dhclient-exit-hook
File metadata and controls
125 lines (109 loc) · 2.97 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/sh
set -e
#set -x
#exec 2>/tmp/deh
# 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}'`
active_ssid() {
active_if=$(ip route get 1.1.1.1 | grep -Po '(?<=dev\s)\w+' | cut -f1 -d ' ')
if echo "$active_if" |grep -q ^wlan; then
ssid=`iwconfig "$active_if" |grep ESSID: |awk -F: '{print $2}' |tr -d '" '`
echo "$ssid"
fi
}
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
if test -n "$PASSTHROUGH_NAMESERVER_SSIDS"; then
ssid=`active_ssid`
if test -n "$ssid"; then
for test_ssid in "$PASSTHROUGH_NAMESERVER_SSIDS"; do
if test "$ssid" = "$test_ssid"; then
exit
fi
done
fi
fi
if test -n "$DIRECT_DNS_SSIDS"; then
ssid=`active_ssid`
if test -n "$ssid"; then
for test_ssid in "$DIRECT_DNS_SSIDS"; do
if test "$ssid" = "$test_ssid"; then
actual_ns=
break
fi
done
fi
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 '};'
}
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
# TODO check that there is exactly one forwarders declaration
# tell name server to use new forwarders
rndc reload || /etc/init.d/named reload || /etc/init.d/named restart
# 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