rdnss/merge-hook.in | 77 ++++++++++++++++++++++++++++-------------------------
1 file changed, 41 insertions(+), 36 deletions(-)
diff --git a/rdnss/merge-hook.in b/rdnss/merge-hook.in
index 00ddcb2..ee65667 100644
--- a/rdnss/merge-hook.in
+++ b/rdnss/merge-hook.in
@@ -17,52 +17,57 @@
# * along with this program. If not, see . *
# *************************************************************************
-set -e
-
PATH=/sbin:/bin
+RESOLVCONF_TOOL=/sbin/resolvconf
+RDNSSD_RESOLV_CONF=@LOCALSTATEDIR@/run/rdnssd/resolv.conf
-# Max number of nameserver options taken into account. Should be as
-# defined in
-MAXNS=3
+merge_resolv_conf()
+{
+ set -e
-# This script tries to share available nameserver slots with IPv4
-# entries, for example to allow fallback to IPv4 if IPv6 fails. If
-# there is not enough room for all IPv6 and IPv4 entries, this script
-# will limit the IPv6 entries it adds to $RDNSS_LIMIT only.
-RDNSS_LIMIT=$(($MAXNS - 1))
+ # Max number of nameserver options taken into account. Should be as
+ # defined in
+ MAXNS=3
-sysconfdir='@SYSCONFDIR@'
-localstatedir='@LOCALSTATEDIR@'
-resolvconf="$sysconfdir/resolv.conf"
-myresolvconf="$localstatedir/run/rdnssd/resolv.conf"
+ # This script tries to share available nameserver slots with IPv4
+ # entries, for example to allow fallback to IPv4 if IPv6 fails. If
+ # there is not enough room for all IPv6 and IPv4 entries, this script
+ # will limit the IPv6 entries it adds to $RDNSS_LIMIT only.
+ RDNSS_LIMIT=$(($MAXNS - 1))
-# These should be POSIX-compliant BREs
-RE_NSV4='^nameserver *\([0-9]\{1,3\}\.\)\{3,3\}[0-9]\{1,3\} *$'
-RE_NSV4OR6='^nameserver *[a-fA-F0-9:\.]\{1,46\}\(%[a-zA-Z0-9]\{1,\}\)\{0,1\} *$'
+ sysconfdir='@SYSCONFDIR@'
+ resolvconf="$sysconfdir/resolv.conf"
-# Count how many IPv6 nameservers we can fit
+ # These should be POSIX-compliant BREs
+ RE_NSV4='^nameserver *\([0-9]\{1,3\}\.\)\{3,3\}[0-9]\{1,3\} *$'
+ RE_NSV4OR6='^nameserver *[a-fA-F0-9:\.]\{1,46\}\(%[a-zA-Z0-9]\{1,\}\)\{0,1\} *$'
-limit=$RDNSS_LIMIT
+ # Count how many IPv6 nameservers we can fit
-nnsv4=`grep -c "$RE_NSV4" $resolvconf || [ $? -le 1 ]`
-room=$(($MAXNS - $nnsv4))
+ limit=$RDNSS_LIMIT
-if [ $limit -lt $room ]; then
- limit=$room
-fi
+ nnsv4=`grep -c "$RE_NSV4" $resolvconf || [ $? -le 1 ]`
+ room=$(($MAXNS - $nnsv4))
-# Merge and write the result. Let rdnssd assume ownership of all IPv6
-# nameservers, and remove extraneous IPv6 entries as expired. However
-# DHCPv4 most often sets up search list entries, and rdnssd cannot
-# clobber these lest it causes counterintuitive breakage. There is no
-# easy way to properly merge and manage DNSSL entries here, so just drop
-# them.
+ if [ $limit -lt $room ]; then
+ limit=$room
+ fi
-{
- sed -e "/$RE_NSV4OR6/d" < $resolvconf
- grep -m $limit "$RE_NSV4OR6" < $myresolvconf || [ $? -le 1 ]
- sed -ne "/$RE_NSV4/p" < $resolvconf
-} > $resolvconf.tmp
+ # Merge and write the result
-mv -f $resolvconf.tmp $resolvconf
+ {
+ sed -e "/$RE_NSV4OR6/d" < $resolvconf
+ grep -m $limit "$RE_NSV4OR6" < $RDNSSD_RESOLV_CONF || [ $? -le 1 ]
+ sed -ne "/$RE_NSV4/p" < $resolvconf
+ } | awk '!a[$0]++' > $resolvconf.tmp
+ mv -f $resolvconf.tmp $resolvconf
+}
+
+[ -f "$RDNSSD_RESOLV_CONF" ] || exit 0
+
+if [ -x "$RESOLVCONF_TOOL" ]; then
+ $RESOLVCONF_TOOL -a 000.rdnssd < "$RDNSSD_RESOLV_CONF"
+else
+ merge_resolv_conf
+fi