File client-netconfig.up of Package openvpn

#!/bin/bash

# Copyright (c) 2010 Andreas Schneider <anschneider@exsuse.de>
#                    Marius Tomaschewski <mt@suse.de>
# Licensed under the GPL version 2

# PURPOSE: This script sets the proper /etc/resolv.conf and another
#          settings as pulled down from an OpenVPN server using a
#          netconfig modify call.

# INSTALL NOTES:
# Place this file in /etc/openvpn/client.up
# Then, add the following to your /etc/openvpn/<clientconfig>.conf:
#   client
#   pull dhcp-options
#   up   /etc/openvpn/client.up
#   down /etc/openvpn/client.down
# Finally, "chmod +x /etc/openvpn/client.{up,down}"

# disable pathname expansion
shopt -o -s noglob

# --up/--down patametes are:
#
#   tun_dev tun_mtu link_mtu ifconfig_local_ip
#           ifconfig_remote_ip [ init | restart ]
#
#   tap_dev tap_mtu link_mtu ifconfig_local_ip
#           ifconfig_netmask [ init | restart ]
dev=$1

# skip, when there is no interface parameter or netconfig
if test -x /sbin/netconfig -a "x${dev}" != x ; then

	# init variables
	dns_domain=()
	dns_server=()
	ntp_server=()
	wins_server=()
	nbdd_server=()
	nb_typeid=""
	nb_scopeid=""
	nb_disable=""

	# collect settings data
	for fopt in ${!foreign_option_*} ; do
		test "x${!fopt}" != x || continue
		data=(${!fopt})
		test "x${data[0]}" = "xdhcp-option" && \
		case "${data[1]}" in
		  DOMAIN)       dns_domain+=("${data[2]}")  ;;
		  DNS)          dns_server+=("${data[2]}")  ;;
		  NTP)          ntp_server+=("${data[2]}")  ;;
		  WINS)         wins_server+=("${data[2]}") ;;
		  NBDD)         nbdd_server+=("${data[2]}") ;;
		  NBT)          nb_typeid="${data[2]}"      ;;
		  NBS)          nb_scopeid="${data[2]}"     ;;
		  DISABLE-NBT)  nb_disable="yes"            ;;
		esac
	done

	# call netconfig modify
	{
		echo "DNSSEARCH='${dns_domain[*]}'"
		echo "DNSSERVERS='${dns_server[*]}'"
		echo "NTPSERVERS='${ntp_server[*]}'"
		# currently unused / no netconfig module for:
		echo "NETBIOSNAMESERVER='${wins_server[*]}'"
		echo "NETBIOSDDSERVER='${nbdd_server[*]}'"
		echo "NETBIOSNODETYPE='$nb_typeid'"
		echo "NETBIOSSCOPE='$nb_scopeid'"
		# nb_disable ?
	} | /sbin/netconfig modify -s openvpn -i "$dev"
fi

# all done...
exit 0
openSUSE Build Service is sponsored by