svn cleanup
[anytun.git] / openvpn / contrib / pull-resolv-conf / client.up
blob48b1a390c2aee38a734f5c82f0d9f701f3c6beb3
1 #!/bin/bash
3 # Copyright (c) 2005 by OpenVPN Solutions LLC
4 # Licensed under the GPL version 2
6 # First version by Jesse Adelman
7 # someone at boldandbusted dink com
8 # http://www.boldandbusted.com/
10 # PURPOSE: This script automatically sets the proper /etc/resolv.conf entries
11 # as pulled down from an OpenVPN server.
13 # INSTALL NOTES:
14 # Place this in /etc/openvpn/client.up
15 # Then, add the following to your /etc/openvpn/<clientconfig>.conf:
16 # client
17 # pull dhcp-options
18 # up /etc/openvpn/client.up
19 # Next, "chmod a+x /etc/openvpn/client.up"
21 # USAGE NOTES:
22 # Note that this script is best served with the companion "client.down"
23 # script.
25 # Only tested on Gentoo Linux 2005.0 with OpenVPN 2.0
26 # It should work with any GNU/Linux with /etc/resolv.conf
28 # This runs with the context of the OpenVPN UID/GID
29 # at the time of execution. This generally means that
30 # the client "up" script will run fine, but the "down" script
31 # will require the use of the OpenVPN "down-root" plugin
32 # which is in the plugins/ directory of the OpenVPN source tree
34 # A horrid work around, from a security perspective,
35 # is to run OpenVPN as root. THIS IS NOT RECOMMENDED. You have
36 # been WARNED.
38 # init variables
40 i=1
41 j=1
42 unset fopt
43 unset dns
44 unset opt
46 # Convert ENVs to an array
48 while fopt=foreign_option_$i; [ -n "${!fopt}" ]; do
50 opt[i-1]=${!fopt}
51 case ${opt[i-1]} in
52 *DOMAIN* ) domain=`echo ${opt[i-1]} | \
53 sed -e 's/dhcp-option DOMAIN //g'` ;;
54 *DNS* ) dns[j-1]=`echo ${opt[i-1]} | \
55 sed -e 's/dhcp-option DNS //g'`
56 let j++ ;;
57 esac
58 let i++
60 done
62 # Now, do the work
64 if [ -n "${dns[*]}" ]; then
65 for i in "${dns[@]}"; do
66 sed -i -e "1,1 i nameserver ${i}" /etc/resolv.conf || die
67 done
70 if [ -n "${domain}" ]; then
71 sed -i -e "$j,1 i search ${domain}" /etc/resolv.conf || die
74 # all done...
75 exit 0