Blame | Last modification | View Log | RSS feed
#!/bin/sh## openvpn This shell script takes care of starting and stopping# openvpn on RedHat or other chkconfig-based system.## chkconfig: - 24 76## processname: 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.# Contributed to the OpenVPN project by### BEGIN INIT INFO# Provides: openvpn# Required-Start: $network# Required-Stop: $network# Short-Description: start and stop 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# Douglas Keller <doug@voidstar.dyndns.org># 2002.05.15# To install:# copy this file to /etc/rc.d/init.d/openvpn# shell> chkconfig --add openvpn# shell> mkdir /etc/openvpn# make .conf or .sh files in /etc/openvpn (see below)# To uninstall:# run: chkconfig --del openvpn# Author's Notes:## I have created an /etc/init.d init script and enhanced openvpn.spec to# automatically register the init script. Once the RPM is installed you# can start and stop OpenVPN with "service openvpn start" and "service# openvpn stop".## 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:## service openvpn reload - SIGHUP# service openvpn reopen - SIGUSR1# service openvpn status - SIGUSR2## Modifications:## 2003.05.02# * Changed == to = for sh compliance (Bishop Clark).# * If condrestart|reload|reopen|status, check that we were# actually started (James Yonan).# * Added lock, piddir, and work variables (James Yonan).# * If start is attempted twice, without an intervening stop, or# if start is attempted when previous start was not properly# shut down, then kill any previously started processes, before# commencing new start operation (James Yonan).# * Do a better job of flagging errors on start, and properly# returning success or failure status to caller (James Yonan).## 2005.04.04# * Added openvpn-startup and openvpn-shutdown script calls# (James Yonan).## Location of openvpn binaryopenvpn=""openvpn_locations="/usr/sbin/openvpn /usr/local/sbin/openvpn"for location in $openvpn_locationsdoif [ -f "$location" ]thenopenvpn=$locationfidone# Lockfilelock="/var/lock/subsys/openvpn"# PID directorypiddir="/var/run/openvpn"# Our working directorywork=/etc/openvpn# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.if [ ${NETWORKING} = "no" ]thenecho "Networking is down"exit 0fi# Check that binary existsif ! [ -f $openvpn ]thenecho "openvpn binary not found"exit 0fi# See how we were called.case "$1" instart)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 definedif [ -f $work/openvpn-startup ]; then$work/openvpn-startupfiif [ ! -d $piddir ]; thenmkdir $piddirfiif [ -f $lock ]; then# we were not shut down correctlyfor pidf in `/bin/ls $piddir/*.pid 2>/dev/null`; doif [ -s $pidf ]; thenkill `cat $pidf` >/dev/null 2>&1firm -f $pidfdonerm -f $locksleep 2firm -f $piddir/*.pidcd $work# Start every .conf in $work and run .sh if existserrors=0successes=0for c in `/bin/ls *.conf 2>/dev/null`; dobn=${c%%.conf}if [ -f "$bn.sh" ]; then. ./$bn.shfirm -f $piddir/$bn.pid# Handle backward compatibility, see Red Hat Bugzilla ID #458594script_security=''if [ -z "$( grep '^[[:space:]]*script-security[[:space:]]' $c )" ]; thenscript_security="--script-security 2"fi$openvpn --daemon --writepid $piddir/$bn.pid --cd $work --config $c $script_securityif [ $? = 0 ]; thensuccesses=1elseerrors=1fidoneif [ $errors = 1 ]; thenfailure; echoelsesuccess; echofiif [ $successes = 1 ]; thentouch $lockfi;;stop)echo -n $"Shutting down openvpn: "for pidf in `/bin/ls $piddir/*.pid 2>/dev/null`; doif [ -s $pidf ]; thenkill `cat $pidf` >/dev/null 2>&1firm -f $pidfdone# Run shutdown script, if definedif [ -f $work/openvpn-shutdown ]; then$work/openvpn-shutdownfisuccess; echorm -f $lock;;restart)$0 stopsleep 2$0 start;;reload)if [ -f $lock ]; thenfor pidf in `/bin/ls $piddir/*.pid 2>/dev/null`; doif [ -s $pidf ]; thenkill -HUP `cat $pidf` >/dev/null 2>&1fidoneelseecho "openvpn: service not started"exit 1fi;;reopen)if [ -f $lock ]; thenfor pidf in `/bin/ls $piddir/*.pid 2>/dev/null`; doif [ -s $pidf ]; thenkill -USR1 `cat $pidf` >/dev/null 2>&1fidoneelseecho "openvpn: service not started"exit 1fi;;condrestart)if [ -f $lock ]; then$0 stop# avoid racesleep 2$0 startfi;;status)if [ -f $lock ]; thenfor pidf in `/bin/ls $piddir/*.pid 2>/dev/null`; doif [ -s $pidf ]; thenkill -USR2 `cat $pidf` >/dev/null 2>&1fidoneecho "Status written to /var/log/messages"elseecho "openvpn: service not started"exit 1fi;;*)echo "Usage: openvpn {start|stop|restart|condrestart|reload|reopen|status}"exit 1;;esacexit 0