update-resolv-conf 1.27 KB
Newer Older
1 2 3 4 5 6 7
#!/bin/bash
# 
# Parses DHCP options from openvpn to update resolv.conf
# To use set as 'up' and 'down' script in your openvpn *.conf:
# up /etc/openvpn/update-resolv-conf
# down /etc/openvpn/update-resolv-conf
#
8
# Used snippets of resolvconf script by Thomas Hood and Chris Hanson.
9 10 11
# Licensed under the GNU GPL.  See /usr/share/common-licenses/GPL. 
# 
# Example envs set from openvpn:
12 13 14 15 16
#
#     foreign_option_1='dhcp-option DNS 193.43.27.132'
#     foreign_option_2='dhcp-option DNS 193.43.27.133'
#     foreign_option_3='dhcp-option DOMAIN be.bnc.ch'
#
17 18

[ -x /sbin/resolvconf ] || exit 0
19 20
[ "$script_type" ] || exit 0
[ "$dev" ] || exit 0
21

22 23 24 25 26 27
split_into_parts()
{
	part1="$1"
	part2="$2"
	part3="$3"
}
28

29 30 31 32 33 34 35 36 37 38 39 40 41
case "$script_type" in
  up)
	NMSRVRS=""
	SRCHS=""
	for optionvarname in ${!foreign_option_*} ; do
		option="${!optionvarname}"
		echo "$option"
		split_into_parts $option
		if [ "$part1" = "dhcp-option" ] ; then
			if [ "$part2" = "DNS" ] ; then
				NMSRVRS="${NMSRVRS:+$NMSRVRS }$part3"
			elif [ "$part2" = "DOMAIN" ] ; then
				SRCHS="${SRCHS:+$SRCHS }$part3"
42 43 44 45
			fi
		fi
	done
	R=""
46
	[ "$SRCHS" ] && R="search $SRCHS
47
"
48
	for NS in $NMSRVRS ; do
49 50 51
        	R="${R}nameserver $NS
"
	done
52
	echo -n "$R" | /sbin/resolvconf -a "${dev}.openvpn"
53
	;;
54 55
  down)
	/sbin/resolvconf -d "${dev}.openvpn"
56 57 58
	;;
esac