Advertisement
scramblevpn

/etc/init.d/openvpn

Sep 22nd, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.41 KB | None | 0 0
  1. #!/bin/sh -e
  2.  
  3. ### BEGIN INIT INFO
  4. # Provides:          openvpn
  5. # Required-Start:    $network $remote_fs $syslog
  6. # Required-Stop:     $network $remote_fs $syslog
  7. # Should-Start:      network-manager
  8. # Should-Stop:       network-manager
  9. # X-Start-Before:    $x-display-manager gdm kdm xdm wdm ldm sdm nodm
  10. # X-Interactive:     true
  11. # Default-Start:     2 3 4 5
  12. # Default-Stop:      0 1 6
  13. # Short-Description: Openvpn VPN service
  14. ### END INIT INFO
  15.  
  16. # Original version by Robert Leslie
  17. # <rob@mars.org>, edited by iwj and cs
  18. # Modified for openvpn by Alberto Gonzalez Iniesta <agi@inittab.org>
  19. # Modified for restarting / starting / stopping single tunnels by Richard Mueller <mueller@teamix.net>
  20.  
  21. . /lib/lsb/init-functions
  22.  
  23. test $DEBIAN_SCRIPT_DEBUG && set -v -x
  24.  
  25. DAEMON=/usr/sbin/openvpn
  26. DESC="virtual private network daemon"
  27. CONFIG_DIR=/etc/openvpn
  28. test -x $DAEMON || exit 0
  29. test -d $CONFIG_DIR || exit 0
  30.  
  31. # Source defaults file; edit that file to configure this script.
  32. AUTOSTART="all"
  33. STATUSREFRESH=10
  34. if test -e /etc/default/openvpn ; then
  35.   . /etc/default/openvpn
  36. fi
  37.  
  38. start_vpn () {
  39.     if grep -q '^[    ]*daemon' $CONFIG_DIR/$NAME.conf ; then
  40.       # daemon already given in config file
  41.       DAEMONARG=
  42.     else
  43.       # need to daemonize
  44.       DAEMONARG="--daemon ovpn-$NAME"
  45.     fi
  46.  
  47.     if grep -q '^[    ]*status ' $CONFIG_DIR/$NAME.conf ; then
  48.       # status file already given in config file
  49.       STATUSARG=""
  50.     elif test $STATUSREFRESH -eq 0 ; then
  51.       # default status file disabled in /etc/default/openvpn
  52.       STATUSARG=""
  53.     else
  54.       # prepare default status file
  55.       STATUSARG="--status /var/run/openvpn.$NAME.status $STATUSREFRESH"
  56.     fi
  57.  
  58.     log_progress_msg "$NAME"
  59.     STATUS=0
  60.  
  61.     start-stop-daemon --start --quiet --oknodo \
  62.         --pidfile /var/run/openvpn.$NAME.pid \
  63.         --exec $DAEMON -- $OPTARGS --writepid /var/run/openvpn.$NAME.pid \
  64.         $DAEMONARG $STATUSARG --cd $CONFIG_DIR \
  65.         --config $CONFIG_DIR/$NAME.conf || STATUS=1
  66. }
  67. stop_vpn () {
  68.   kill `cat $PIDFILE` || true
  69.   rm -f $PIDFILE
  70.   rm -f /var/run/openvpn.$NAME.status 2> /dev/null
  71. }
  72.  
  73. case "$1" in
  74. start)
  75.   log_daemon_msg "Starting $DESC"
  76.  
  77.   # autostart VPNs
  78.   if test -z "$2" ; then
  79.     # check if automatic startup is disabled by AUTOSTART=none
  80.     if test "x$AUTOSTART" = "xnone" -o -z "$AUTOSTART" ; then
  81.       log_warning_msg " Autostart disabled."
  82.       exit 0
  83.     fi
  84.     if test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
  85.       # all VPNs shall be started automatically
  86.       for CONFIG in `cd $CONFIG_DIR; ls *.conf 2> /dev/null`; do
  87.         NAME=${CONFIG%%.conf}
  88.         start_vpn
  89.       done
  90.     else
  91.       # start only specified VPNs
  92.       for NAME in $AUTOSTART ; do
  93.         if test -e $CONFIG_DIR/$NAME.conf ; then
  94.           start_vpn
  95.         else
  96.           log_failure_msg "No such VPN: $NAME"
  97.           STATUS=1
  98.         fi
  99.       done
  100.     fi
  101.   #start VPNs from command line
  102.   else
  103.     while shift ; do
  104.       [ -z "$1" ] && break
  105.       if test -e $CONFIG_DIR/$1.conf ; then
  106.         NAME=$1
  107.         start_vpn
  108.       else
  109.        log_failure_msg " No such VPN: $1"
  110.        STATUS=1
  111.       fi
  112.     done
  113.   fi
  114.   log_end_msg ${STATUS:-0}
  115.  
  116.   ;;
  117. stop)
  118.   log_daemon_msg "Stopping $DESC"
  119.  
  120.   if test -z "$2" ; then
  121.     for PIDFILE in `ls /var/run/openvpn.*.pid 2> /dev/null`; do
  122.       NAME=`echo $PIDFILE | cut -c18-`
  123.       NAME=${NAME%%.pid}
  124.       stop_vpn
  125.       log_progress_msg "$NAME"
  126.     done
  127.   else
  128.     while shift ; do
  129.       [ -z "$1" ] && break
  130.       if test -e /var/run/openvpn.$1.pid ; then
  131.         PIDFILE=`ls /var/run/openvpn.$1.pid 2> /dev/null`
  132.         NAME=`echo $PIDFILE | cut -c18-`
  133.         NAME=${NAME%%.pid}
  134.         stop_vpn
  135.         log_progress_msg "$NAME"
  136.       else
  137.         log_failure_msg " (failure: No such VPN is running: $1)"
  138.       fi
  139.     done
  140.   fi
  141.   log_end_msg 0
  142.   ;;
  143. # Only 'reload' running VPNs. New ones will only start with 'start' or 'restart'.
  144. reload|force-reload)
  145.  log_daemon_msg "Reloading $DESC"
  146.   for PIDFILE in `ls /var/run/openvpn.*.pid 2> /dev/null`; do
  147.     NAME=`echo $PIDFILE | cut -c18-`
  148.     NAME=${NAME%%.pid}
  149. # If openvpn if running under a different user than root we'll need to restart
  150.     if egrep '^[[:blank:]]*user[[:blank:]]' $CONFIG_DIR/$NAME.conf > /dev/null 2>&1 ; then
  151.       stop_vpn
  152.       sleep 1
  153.       start_vpn
  154.       log_progress_msg "(restarted)"
  155.     else
  156.       kill -HUP `cat $PIDFILE` || true
  157.     log_progress_msg "$NAME"
  158.     fi
  159.   done
  160.   log_end_msg 0
  161.   ;;
  162.  
  163. # Only 'soft-restart' running VPNs. New ones will only start with 'start' or 'restart'.
  164. soft-restart)
  165.  log_daemon_msg "$DESC sending SIGUSR1"
  166.   for PIDFILE in `ls /var/run/openvpn.*.pid 2> /dev/null`; do
  167.     NAME=`echo $PIDFILE | cut -c18-`
  168.     NAME=${NAME%%.pid}
  169.     kill -USR1 `cat $PIDFILE` || true
  170.     log_progress_msg "$NAME"
  171.   done
  172.   log_end_msg 0
  173.  ;;
  174.  
  175. restart)
  176.   shift
  177.   $0 stop ${@}
  178.   sleep 1
  179.   $0 start ${@}
  180.   ;;
  181. cond-restart)
  182.   log_daemon_msg "Restarting $DESC."
  183.   for PIDFILE in `ls /var/run/openvpn.*.pid 2> /dev/null`; do
  184.     NAME=`echo $PIDFILE | cut -c18-`
  185.     NAME=${NAME%%.pid}
  186.     stop_vpn
  187.     sleep 1
  188.     start_vpn
  189.   done
  190.   log_end_msg 0
  191.   ;;
  192. status)
  193.   GLOBAL_STATUS=0
  194.   if test -z "$2" ; then
  195.     # We want status for all defined VPNs.
  196.     # Returns success if all autostarted VPNs are defined and running
  197.     if test "x$AUTOSTART" = "xnone" ; then
  198.       # Consider it a failure if AUTOSTART=none
  199.       log_warning_msg "No VPN autostarted"
  200.       GLOBAL_STATUS=1
  201.     else
  202.       if ! test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
  203.         # Consider it a failure if one of the autostarted VPN is not defined
  204.         for VPN in $AUTOSTART ; do
  205.           if ! test -f $CONFIG_DIR/$VPN.conf ; then
  206.             log_warning_msg "VPN '$VPN' is in AUTOSTART but is not defined"
  207.             GLOBAL_STATUS=1
  208.           fi
  209.         done
  210.       fi
  211.     fi
  212.     for CONFIG in `cd $CONFIG_DIR; ls *.conf 2> /dev/null`; do
  213.       NAME=${CONFIG%%.conf}
  214.       # Is it an autostarted VPN ?
  215.       if test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
  216.         AUTOVPN=1
  217.       else
  218.         if test "x$AUTOSTART" = "xnone" ; then
  219.           AUTOVPN=0
  220.         else
  221.           AUTOVPN=0
  222.           for VPN in $AUTOSTART; do
  223.             if test "x$VPN" = "x$NAME" ; then
  224.               AUTOVPN=1
  225.             fi
  226.           done
  227.         fi
  228.       fi
  229.       if test "x$AUTOVPN" = "x1" ; then
  230.         # If it is autostarted, then it contributes to global status
  231.         status_of_proc -p /var/run/openvpn.${NAME}.pid openvpn "VPN '${NAME}'" || GLOBAL_STATUS=1
  232.       else
  233.         status_of_proc -p /var/run/openvpn.${NAME}.pid openvpn "VPN '${NAME}' (non autostarted)" || true
  234.       fi
  235.     done
  236.   else
  237.     # We just want status for specified VPNs.
  238.     # Returns success if all specified VPNs are defined and running
  239.     while shift ; do
  240.       [ -z "$1" ] && break
  241.       NAME=$1
  242.       if test -e $CONFIG_DIR/$NAME.conf ; then
  243.         # Config exists
  244.         status_of_proc -p /var/run/openvpn.${NAME}.pid openvpn "VPN '${NAME}'" || GLOBAL_STATUS=1
  245.       else
  246.         # Config does not exist
  247.         log_warning_msg "VPN '$NAME': missing $CONFIG_DIR/$NAME.conf file !"
  248.         GLOBAL_STATUS=1
  249.       fi
  250.     done
  251.   fi
  252.   exit $GLOBAL_STATUS
  253.   ;;
  254. *)
  255.   echo "Usage: $0 {start|stop|reload|restart|force-reload|cond-restart|soft-restart|status}" >&2
  256.   exit 1
  257.   ;;
  258. esac
  259.  
  260. exit 0
  261.  
  262. # vim:set ai sts=2 sw=2 tw=0:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement