Advertisement
Guest User

openvpn24 init script ubuntu 16.10

a guest
Apr 3rd, 2017
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.25 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 2.4 service
  14. # Description: This script will start OpenVPN tunnels as specified
  15. #              in /etc/default/openvpn and /etc/openvpn/*.conf
  16. ### END INIT INFO
  17.  
  18. # Original version by Robert Leslie
  19. # <rob@mars.org>, edited by iwj and cs
  20. # Modified for openvpn by Alberto Gonzalez Iniesta <agi@inittab.org>
  21. # Modified for restarting / starting / stopping single tunnels by Richard Mueller <mueller@teamix.net>
  22.  
  23. . /lib/lsb/init-functions
  24.  
  25. test $DEBIAN_SCRIPT_DEBUG && set -v -x
  26.  
  27. DAEMON=/opt/openvpn/sbin/openvpn
  28. DESC="virtual private network daemon"
  29. CONFIG_DIR=/opt/openvpn/etc/openvpn
  30. test -x $DAEMON || exit 0
  31. test -d $CONFIG_DIR || exit 0
  32.  
  33. # Source defaults file; edit that file to configure this script.
  34. AUTOSTART="all"
  35. STATUSREFRESH=10
  36. OMIT_SENDSIGS=0
  37. if test -e /etc/default/openvpn ; then
  38.   . /etc/default/openvpn
  39. fi
  40.  
  41. start_vpn () {
  42.     if grep -q '^[       ]*daemon' $CONFIG_DIR/$NAME.conf ; then
  43.       # daemon already given in config file
  44.       DAEMONARG=
  45.     else
  46.       # need to daemonize
  47.       DAEMONARG="--daemon ovpn-$NAME"
  48.     fi
  49.  
  50.     if grep -q '^[       ]*status ' $CONFIG_DIR/$NAME.conf ; then
  51.       # status file already given in config file
  52.       STATUSARG=""
  53.     elif test $STATUSREFRESH -eq 0 ; then
  54.       # default status file disabled in /etc/default/openvpn
  55.       STATUSARG=""
  56.     else
  57.       # prepare default status file
  58.       STATUSARG="--status /run/openvpn/$NAME.status $STATUSREFRESH"
  59.     fi
  60.  
  61.     # tun using the "subnet" topology confuses the routing code that wrongly
  62.     # emits ICMP redirects for client to client communications
  63.     SAVED_DEFAULT_SEND_REDIRECTS=0
  64.     if grep -q '^[[:space:]]*dev[[:space:]]*tun' $CONFIG_DIR/$NAME.conf && \
  65.        grep -q '^[[:space:]]*topology[[:space:]]*subnet' $CONFIG_DIR/$NAME.conf ; then
  66.         # When using "client-to-client", OpenVPN routes the traffic itself without
  67.         # involving the TUN/TAP interface so no ICMP redirects are sent
  68.         if ! grep -q '^[[:space:]]*client-to-client' $CONFIG_DIR/$NAME.conf ; then
  69.             sysctl -w net.ipv4.conf.all.send_redirects=0 > /dev/null
  70.  
  71.             # Save the default value for send_redirects before disabling it
  72.             # to make sure the tun device is created with send_redirects disabled
  73.             SAVED_DEFAULT_SEND_REDIRECTS=$(sysctl -n net.ipv4.conf.default.send_redirects)
  74.  
  75.             if [ "$SAVED_DEFAULT_SEND_REDIRECTS" -ne 0 ]; then
  76.               sysctl -w net.ipv4.conf.default.send_redirects=0 > /dev/null
  77.             fi
  78.         fi
  79.     fi
  80.  
  81.     log_progress_msg "$NAME"
  82.     STATUS=0
  83.  
  84.     start-stop-daemon --start --quiet --oknodo \
  85.         --pidfile /run/openvpn/$NAME.pid \
  86.         --exec $DAEMON -- $OPTARGS --writepid /run/openvpn/$NAME.pid \
  87.         $DAEMONARG $STATUSARG --cd $CONFIG_DIR \
  88.         --config $CONFIG_DIR/$NAME.conf || STATUS=1
  89.  
  90.     [ "$OMIT_SENDSIGS" -ne 1 ] || ln -s /run/openvpn/$NAME.pid /run/sendsigs.omit.d/openvpn.$NAME.pid
  91.  
  92.     # Set the back the original default value of send_redirects if it was changed
  93.     if [ "$SAVED_DEFAULT_SEND_REDIRECTS" -ne 0 ]; then
  94.       sysctl -w net.ipv4.conf.default.send_redirects=$SAVED_DEFAULT_SEND_REDIRECTS > /dev/null
  95.     fi
  96. }
  97. stop_vpn () {
  98.   start-stop-daemon --stop --quiet --oknodo \
  99.       --pidfile $PIDFILE --exec $DAEMON --retry 10
  100.   if [ "$?" -eq 0 ]; then
  101.     rm -f $PIDFILE
  102.     [ "$OMIT_SENDSIGS" -ne 1 ] || rm -f /run/sendsigs.omit.d/openvpn.$NAME.pid
  103.     rm -f /run/openvpn/$NAME.status 2> /dev/null
  104.   fi
  105. }
  106.  
  107. case "$1" in
  108. start)
  109.   log_daemon_msg "Starting $DESC"
  110.  
  111.   # first create /run directory so it's present even
  112.   # when no VPN are autostarted by this script, but later
  113.   # by systemd openvpn@.service
  114.   mkdir -p /run/openvpn
  115.  
  116.   # autostart VPNs
  117.   if test -z "$2" ; then
  118.     # check if automatic startup is disabled by AUTOSTART=none
  119.     if test "x$AUTOSTART" = "xnone" -o -z "$AUTOSTART" ; then
  120.       log_warning_msg " Autostart disabled."
  121.       exit 0
  122.     fi
  123.     if test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
  124.       # all VPNs shall be started automatically
  125.       for CONFIG in `cd $CONFIG_DIR; ls *.conf 2> /dev/null`; do
  126.         NAME=${CONFIG%%.conf}
  127.         start_vpn
  128.       done
  129.     else
  130.       # start only specified VPNs
  131.       for NAME in $AUTOSTART ; do
  132.         if test -e $CONFIG_DIR/$NAME.conf ; then
  133.           start_vpn
  134.         else
  135.           log_failure_msg "No such VPN: $NAME"
  136.           STATUS=1
  137.         fi
  138.       done
  139.     fi
  140.   #start VPNs from command line
  141.   else
  142.     while shift ; do
  143.       [ -z "$1" ] && break
  144.       if test -e $CONFIG_DIR/$1.conf ; then
  145.         NAME=$1
  146.         start_vpn
  147.       else
  148.        log_failure_msg " No such VPN: $1"
  149.        STATUS=1
  150.       fi
  151.     done
  152.   fi
  153.   log_end_msg ${STATUS:-0}
  154.  
  155.   ;;
  156. stop)
  157.   log_daemon_msg "Stopping $DESC"
  158.  
  159.   if test -z "$2" ; then
  160.     for PIDFILE in `ls /run/openvpn/*.pid 2> /dev/null`; do
  161.       NAME=`echo $PIDFILE | cut -c14-`
  162.       NAME=${NAME%%.pid}
  163.       stop_vpn
  164.       log_progress_msg "$NAME"
  165.     done
  166.   else
  167.     while shift ; do
  168.       [ -z "$1" ] && break
  169.       if test -e /run/openvpn/$1.pid ; then
  170.         PIDFILE=`ls /run/openvpn/$1.pid 2> /dev/null`
  171.         NAME=`echo $PIDFILE | cut -c14-`
  172.         NAME=${NAME%%.pid}
  173.         stop_vpn
  174.         log_progress_msg "$NAME"
  175.       else
  176.         log_failure_msg " (failure: No such VPN is running: $1)"
  177.       fi
  178.     done
  179.   fi
  180.   log_end_msg 0
  181.   ;;
  182. # Only 'reload' running VPNs. New ones will only start with 'start' or 'restart'.
  183. reload|force-reload)
  184.  log_daemon_msg "Reloading $DESC"
  185.   for PIDFILE in `ls /run/openvpn/*.pid 2> /dev/null`; do
  186.     NAME=`echo $PIDFILE | cut -c14-`
  187.     NAME=${NAME%%.pid}
  188. # If openvpn if running under a different user than root we'll need to restart
  189.     if egrep '^[[:blank:]]*user[[:blank:]]' $CONFIG_DIR/$NAME.conf > /dev/null 2>&1 ; then
  190.       stop_vpn
  191.       start_vpn
  192.       log_progress_msg "(restarted)"
  193.     else
  194.       kill -HUP `cat $PIDFILE` || true
  195.     log_progress_msg "$NAME"
  196.     fi
  197.   done
  198.   log_end_msg 0
  199.   ;;
  200.  
  201. # Only 'soft-restart' running VPNs. New ones will only start with 'start' or 'restart'.
  202. soft-restart)
  203.  log_daemon_msg "$DESC sending SIGUSR1"
  204.   for PIDFILE in `ls /run/openvpn/*.pid 2> /dev/null`; do
  205.     NAME=`echo $PIDFILE | cut -c14-`
  206.     NAME=${NAME%%.pid}
  207.     kill -USR1 `cat $PIDFILE` || true
  208.     log_progress_msg "$NAME"
  209.   done
  210.   log_end_msg 0
  211.  ;;
  212.  
  213. restart)
  214.   shift
  215.   $0 stop ${@}
  216.   $0 start ${@}
  217.   ;;
  218. cond-restart)
  219.   log_daemon_msg "Restarting $DESC."
  220.   for PIDFILE in `ls /run/openvpn/*.pid 2> /dev/null`; do
  221.     NAME=`echo $PIDFILE | cut -c14-`
  222.     NAME=${NAME%%.pid}
  223.     stop_vpn
  224.     start_vpn
  225.   done
  226.   log_end_msg 0
  227.   ;;
  228. status)
  229.   GLOBAL_STATUS=0
  230.   if test -z "$2" ; then
  231.     # We want status for all defined VPNs.
  232.     # Returns success if all autostarted VPNs are defined and running
  233.     if test "x$AUTOSTART" = "xnone" ; then
  234.       # Consider it a failure if AUTOSTART=none
  235.       log_warning_msg "No VPN autostarted"
  236.       GLOBAL_STATUS=1
  237.     else
  238.       if ! test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
  239.         # Consider it a failure if one of the autostarted VPN is not defined
  240.         for VPN in $AUTOSTART ; do
  241.           if ! test -f $CONFIG_DIR/$VPN.conf ; then
  242.             log_warning_msg "VPN '$VPN' is in AUTOSTART but is not defined"
  243.             GLOBAL_STATUS=1
  244.           fi
  245.         done
  246.       fi
  247.     fi
  248.     for CONFIG in `cd $CONFIG_DIR; ls *.conf 2> /dev/null`; do
  249.       NAME=${CONFIG%%.conf}
  250.       # Is it an autostarted VPN ?
  251.       if test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
  252.         AUTOVPN=1
  253.       else
  254.         if test "x$AUTOSTART" = "xnone" ; then
  255.           AUTOVPN=0
  256.         else
  257.           AUTOVPN=0
  258.           for VPN in $AUTOSTART; do
  259.             if test "x$VPN" = "x$NAME" ; then
  260.               AUTOVPN=1
  261.             fi
  262.           done
  263.         fi
  264.       fi
  265.       if test "x$AUTOVPN" = "x1" ; then
  266.         # If it is autostarted, then it contributes to global status
  267.         status_of_proc -p /run/openvpn/${NAME}.pid openvpn "VPN '${NAME}'" || GLOBAL_STATUS=1
  268.       else
  269.         status_of_proc -p /run/openvpn/${NAME}.pid openvpn "VPN '${NAME}' (non autostarted)" || true
  270.       fi
  271.     done
  272.   else
  273.     # We just want status for specified VPNs.
  274.     # Returns success if all specified VPNs are defined and running
  275.     while shift ; do
  276.       [ -z "$1" ] && break
  277.       NAME=$1
  278.       if test -e $CONFIG_DIR/$NAME.conf ; then
  279.         # Config exists
  280.         status_of_proc -p /run/openvpn/${NAME}.pid openvpn "VPN '${NAME}'" || GLOBAL_STATUS=1
  281.       else
  282.         # Config does not exist
  283.         log_warning_msg "VPN '$NAME': missing $CONFIG_DIR/$NAME.conf file !"
  284.         GLOBAL_STATUS=1
  285.       fi
  286.     done
  287.   fi
  288.   exit $GLOBAL_STATUS
  289.   ;;
  290. *)
  291.   echo "Usage: $0 {start|stop|reload|restart|force-reload|cond-restart|soft-restart|status}" >&2
  292.   exit 1
  293.   ;;
  294. esac
  295.  
  296. exit 0
  297.  
  298. # vim:set ai sts=2 sw=2 tw=0:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement