5 kx #!/bin/sh
5 kx #
5 kx # /etc/rc.d/rc.openvpn
5 kx #
5 kx # Start/stop/restart the openvpn daemon.
5 kx #
5 kx # By default, this script will start/stop/restart a daemon for every *.conf
5 kx # file found in /etc/openvpn.
5 kx #
5 kx # To work with a single connection, add the name of the config file:
5 kx # /etc/rc.d/rc.openvpn start configfile.conf
5 kx #
5 kx # You may also use a config file not found in /etc/openvpn by providing a
5 kx # complete path:
5 kx # /etc/rc.d/rc.openvpn start /path/to/some/other/configfile.conf
5 kx #
5 kx # The name of a config file provided with a complete path should not match
5 kx # the name of any config file present in the /etc/openvpn directory.
5 kx
5 kx ovpn_start() {
5 kx if [ -x /usr/sbin/openvpn ]; then
5 kx if [ -z "$1" ]; then # start OpenVPN for all config files:
5 kx if /bin/ls /etc/openvpn/*.conf 1> /dev/null 2> /dev/null ; then
5 kx for config in /etc/openvpn/*.conf ; do
5 kx echo "Starting OpenVPN: /usr/sbin/openvpn --daemon --writepid /run/openvpn/$(basename $config).pid --user nobody --group nobody --config $config"
5 kx /usr/sbin/openvpn --daemon --writepid /run/openvpn/$(basename $config).pid --user nobody --group nobody --config $config
5 kx done
5 kx else
5 kx echo "Unable to start OpenVPN - no .conf files found in /etc/openvpn/."
5 kx fi
5 kx else # start OpenVPN for one config file:
5 kx if [ -r "$1" ]; then
5 kx echo "Starting OpenVPN: /usr/sbin/openvpn --daemon --writepid /run/openvpn/$(basename $1).pid --user nobody --group nobody --config $1"
5 kx /usr/sbin/openvpn --daemon --writepid /run/openvpn/$(basename $1).pid --user nobody --group nobody --config $1
5 kx else # config file is missing:
5 kx echo "Error starting OpenVPN: config file $1 is missing."
5 kx fi
5 kx fi
5 kx fi
5 kx }
5 kx
5 kx ovpn_stop() {
5 kx # Note: OpenVPN has a bad habit of leaving stale pid files around when exiting.
5 kx # Maybe it would be better to just use killall unless called for one config?
5 kx if [ -z "$1" ]; then # stop OpenVPN for all pid files:
5 kx if /bin/ls /run/openvpn/*.pid 1> /dev/null 2> /dev/null ; then
5 kx for pid in /run/openvpn/*.pid ; do
5 kx echo "Stopping OpenVPN for pid file $pid..."
5 kx kill $(cat $pid)
5 kx rm -f $pid
5 kx done
5 kx else
5 kx echo "Warning: no pid files found in /run/openvpn/. Using killall to stop any OpenVPN processes."
5 kx killall openvpn
5 kx fi
5 kx else # stop OpenVPN for one config file:
5 kx if [ -r /run/openvpn/$(basename ${1}).pid ]; then
5 kx echo "Stopping OpenVPN for config file ${1}..."
5 kx kill $(cat /run/openvpn/$(basename ${1}).pid)
5 kx rm -f /run/openvpn/$(basename ${1}).pid
5 kx else
5 kx echo "Error stopping OpenVPN: no such pid file /run/openvpn/$(basename ${1}).pid"
5 kx fi
5 kx fi
5 kx }
5 kx
5 kx ovpn_restart() {
5 kx if [ ! -z "$1" ]; then # restart for all config files:
5 kx ovpn_stop
5 kx sleep 2
5 kx ovpn_start
5 kx else # restart for one config file only:
5 kx ovpn_stop $1
5 kx sleep 2
5 kx ovpn_start $1
5 kx fi
5 kx }
5 kx
5 kx ovpn_status() {
5 kx if /bin/ls /run/openvpn/*.pid 1> /dev/null 2> /dev/null ; then
5 kx echo "Currently running OpenVPN processes according to .pid files in /run/openvpn:"
5 kx for pid in /run/openvpn/*.pid ; do
5 kx echo " $(basename $pid) ($(cat $pid))"
5 kx done
5 kx else
5 kx echo "No .pid files found in /run/openvpn."
5 kx fi
5 kx }
5 kx
5 kx # Create PID directory if it doesn't exist:
5 kx if [ ! -d /run/openvpn ]; then
5 kx mkdir -p /run/openvpn
5 kx fi
5 kx
5 kx case "$1" in
5 kx 'start')
5 kx ovpn_start $2
5 kx ;;
5 kx 'stop')
5 kx ovpn_stop $2
5 kx ;;
5 kx 'restart')
5 kx ovpn_restart $2
5 kx ;;
5 kx 'status')
5 kx ovpn_status
5 kx ;;
5 kx *)
5 kx echo "Usage: $0 {start|stop|restart}"
5 kx esac
5 kx