contrib/pull-resolv-conf/client.up
5c9f1d2e
 #!/bin/sh
6fbf66fa
 
49979459
 # Copyright (c) 2005-2018 OpenVPN Inc
6fbf66fa
 # Licensed under the GPL version 2
 
 # First version by Jesse Adelman
 # someone at boldandbusted dink com
 # http://www.boldandbusted.com/
 
 # PURPOSE: This script automatically sets the proper /etc/resolv.conf entries
 # as pulled down from an OpenVPN server.
 
 # INSTALL NOTES:
 # Place this in /etc/openvpn/client.up
 # Then, add the following to your /etc/openvpn/<clientconfig>.conf:
 #   client
 #   up /etc/openvpn/client.up
 # Next, "chmod a+x /etc/openvpn/client.up"
 
 # USAGE NOTES:
 # Note that this script is best served with the companion "client.down"
 # script.
 
5c9f1d2e
 # Tested under Debian lenny with OpenVPN 2.1_rc11
 # It should work with any UNIX with a POSIX sh, /etc/resolv.conf or resolvconf
6fbf66fa
 
 # This runs with the context of the OpenVPN UID/GID 
 # at the time of execution. This generally means that
 # the client "up" script will run fine, but the "down" script
 # will require the use of the OpenVPN "down-root" plugin
 # which is in the plugins/ directory of the OpenVPN source tree
 
 # A horrid work around, from a security perspective,
 # is to run OpenVPN as root. THIS IS NOT RECOMMENDED. You have
 # been WARNED.
3adf2f55
 PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin
6fbf66fa
 
 # init variables
 
 i=1
5c9f1d2e
 domains=
 fopt=
 ndoms=0
 nns=0
 nl='
 '
 
 # $foreign_option_<n> is something like
 # "dhcp-option DOMAIN example.com" (multiple allowed)
 # or
 # "dhcp-option DNS 10.10.10.10" (multiple allowed)
 
4a506b9c
 # each DNS option becomes a "nameserver" option in resolv.conf
5c9f1d2e
 # if we get one DOMAIN, that becomes "domain" in resolv.conf
 # if we get multiple DOMAINS, those become "search" lines in resolv.conf
4a506b9c
 # if we get no DOMAINS, then don't use either domain or search.
5c9f1d2e
 
 while true; do
   eval fopt=\$foreign_option_${i}
   [ -z "${fopt}" ] && break
 
   case ${fopt} in
 		dhcp-option\ DOMAIN\ *)
            ndoms=$((ndoms + 1))
            domains="${domains} ${fopt#dhcp-option DOMAIN }"
            ;;
 		dhcp-option\ DNS\ *)
            nns=$((nns + 1))
            if [ $nns -le 3 ]; then
              dns="${dns}${dns:+$nl}nameserver ${fopt#dhcp-option DNS }"
            else
              printf "%s\n" "Too many nameservers - ignoring after third" >&2
            fi
            ;;
         *)
            printf "%s\n" "Unknown option \"${fopt}\" - ignored" >&2
            ;;
6fbf66fa
 	esac
5c9f1d2e
   i=$((i + 1))
6fbf66fa
 done
 
4a506b9c
 ds=""
 if [ $ndoms -eq 1 ]; then
   ds="${nl}domain"
 elif [ $ndoms -gt 1 ]; then
   ds="${nl}search"
6fbf66fa
 fi
 
5c9f1d2e
 # This is the complete file - "$domains" has a leading space already
94c1ce22
 out="# resolv.conf autogenerated by ${0} (${dev})${nl}${dns}${ds}${domains}"
5c9f1d2e
 
 # use resolvconf if it's available
3adf2f55
 if type resolvconf >/dev/null 2>&1; then
94c1ce22
   printf "%s\n" "${out}" | resolvconf -p -a "${dev}"
5c9f1d2e
 else
   # Preserve the existing resolv.conf
   if [ -e /etc/resolv.conf ] ; then
     cp /etc/resolv.conf /etc/resolv.conf.ovpnsave
   fi
   printf "%s\n" "${out}" > /etc/resolv.conf
   chmod 644 /etc/resolv.conf
6fbf66fa
 fi
 
 exit 0