#!/bin/sh

# RedHat section
#
# openvpn       This shell script takes care of starting and stopping
#               openvpn on RedHat and SuSE system.
#
# chkconfig: 345 24 76
#
# description: OpenVPN is a robust and highly flexible tunneling application that
#              uses all of the encryption, authentication, and certification features
#              of the OpenSSL library to securely tunnel IP networks over a single
#              UDP port.
#

# SuSE section
#
### BEGIN INIT INFO
# Provides:                     openvpn
# required-start:               $network
# required-stop:                $network
# Default-Start:                3 5
# Default-Stop:                 0 1 2 6
# Short-Description:            This shell script takes care of starting and stopping OpenVPN.
# Description:                  OpenVPN is a robust and highly flexible tunneling application that uses all of the encryption, authentication, and certification features of the OpenSSL library to securely tunnel IP networks over a single UDP port. 
### END INIT INFO
 
# The init script does the following:
#
# - Starts an openvpn process for each .conf file it finds in
#   /etc/openvpn.
#
# - If /etc/openvpn/xxx.sh exists for a xxx.conf file then it executes
#   it before starting openvpn (useful for doing openvpn --mktun...).
#
# - In addition to start/stop you can do:
#
#   /etc/init.d/openvpn reload - SIGHUP
#   /etc/init.d/openvpn reopen - SIGUSR1
#   /etc/init.d/openvpn status - SIGUSR2

# Location of openvpn binary
openvpn="/usr/sbin/openvpn"

# Lockfile
lock="/var/lock/subsys/openvpn"

# PID directory
piddir="/var/run/openvpn"

# Our working directory
work=/etc/openvpn

if [ -f /etc/rc.d/init.d/functions ]; then
	DISTRO="redhat"
	# Source function library.
	. /etc/rc.d/init.d/functions

	# Source networking configuration.
	. /etc/sysconfig/network
	# Check that networking is up.
	if [ ${NETWORKING} = "no" ]
	then
		echo "Networking is down"
		exit 0
	fi
elif [ -f /etc/rc.status ]; then
	DISTRO="suse"
	# Source rc functions
	. /etc/rc.status
	# Shell functions sourced from /etc/rc.status:
	#      rc_check         check and set local and overall rc status
	#      rc_status        check and set local and overall rc status
	#      rc_status -v     ditto but be verbose in local rc status
	#      rc_status -v -r  ditto and clear the local rc status
	#      rc_failed        set local and overall rc status to failed
	#      rc_reset         clear local rc status (overall remains)
	#      rc_exit          exit appropriate to overall rc status
else
	echo "Can't find function library."
	exit 1
fi

 
# Check that binary exists
if ! [ -f  $openvpn ] 
then
  echo "openvpn binary not found"
  exit 0
fi

# First reset status of this service
[ "$DISTRO" = "suse" ] && rc_reset

# See how we were called.
case "$1" in
  start)
	echo -n $"Starting openvpn: "

	/sbin/modprobe tun >/dev/null 2>&1

	# From a security perspective, I think it makes
	# sense to remove this, and have users who need
	# it explictly enable in their --up scripts or
	# firewall setups.

	#echo 1 > /proc/sys/net/ipv4/ip_forward

	# Run startup script, if defined
	if [ -f $work/openvpn-startup ]; then
	    $work/openvpn-startup
	fi

	if [ ! -d  $piddir ]; then
	    mkdir $piddir
	fi

	if [ -f $lock ]; then
	    # we were not shut down correctly
	    for pidf in `/bin/ls $piddir/*.pid 2>/dev/null`; do
	      if [ -s $pidf ]; then
		kill `cat $pidf` >/dev/null 2>&1
	      fi
	      rm -f $pidf
	    done
	    rm -f $lock
	    sleep 2
	fi

	rm -f $piddir/*.pid
	cd $work

	# Start every .conf in $work and run .sh if exists
	errors=0
	successes=0
	for c in `/bin/ls *.conf 2>/dev/null`; do
	    bn=${c%%.conf}
	    if [ -f "$bn.sh" ]; then
		. $bn.sh
	    fi
	    rm -f $piddir/$bn.pid
	    $openvpn --daemon --writepid $piddir/$bn.pid --config $c --cd $work
	    if [ $? = 0 ]; then
		successes=1
	    else
		errors=1
	    fi
	done

	if [ $successes = 1 ]; then
	    touch $lock
	fi

	if [ "$DISTRO" = "suse" ] ; then
		rc_status -v
	else
		if [ $errors = 1 ]; then
		    failure; echo
		else
		    success; echo
		fi
	fi
	;;
  stop)
	echo -n $"Shutting down openvpn: "
	for pidf in `/bin/ls $piddir/*.pid 2>/dev/null`; do
	  if [ -s $pidf ]; then
	    kill `cat $pidf` >/dev/null 2>&1
	  fi
	  rm -f $pidf
	done

	# Run shutdown script, if defined
	if [ -f $work/openvpn-shutdown ]; then
	    $work/openvpn-shutdown
	fi

	rm -f $lock
	if [ "$DISTRO" = "suse" ] ; then
		rc_status -v
	else
		success; echo
	fi
	;;
  restart)
	$0 stop
	sleep 2
	$0 start

	[ "$DISTRO" = "suse" ] && rc_status
	;;
  reload)
	if [ -f $lock ]; then
	    for pidf in `/bin/ls $piddir/*.pid 2>/dev/null`; do
		if [ -s $pidf ]; then
		    kill -HUP `cat $pidf` >/dev/null 2>&1
		fi
	    done
	else
	    echo "openvpn: service not started"
	    exit 1
	fi

	[ "$DISTRO" = "suse" ] && rc_status -v
	;;
  reopen)
	if [ -f $lock ]; then
	    for pidf in `/bin/ls $piddir/*.pid 2>/dev/null`; do
		if [ -s $pidf ]; then
		    kill -USR1 `cat $pidf` >/dev/null 2>&1
		fi
	    done
	else
	    echo "openvpn: service not started"
	    exit 1
	fi

	[ "$DISTRO" = "suse" ] && rc_status -v
	;;
  condrestart)
	if [ -f $lock ]; then
	    $0 stop
	    # avoid race
	    sleep 2
	    $0 start
	fi

	[ "$DISTRO" = "suse" ] && rc_status
	;;
  status)
	if [ -f $lock ]; then
	    for pidf in `/bin/ls $piddir/*.pid 2>/dev/null`; do
		if [ -s $pidf ]; then
		    kill -USR2 `cat $pidf` >/dev/null 2>&1
		fi
	    done
	    echo "Status written to /var/log/messages"
	else
	    echo "openvpn: service not started"
	    exit 1
	fi

	[ "$DISTRO" = "suse" ] && rc_status -v
        ;;
  *)
	echo "Usage: openvpn {start|stop|restart|condrestart|reload|reopen|status}"
	exit 1
	;;
esac
exit 0
