Page 1 of 1

automatic establish and check client connetcion?

Posted: Wed Jul 15, 2015 1:54 pm
by lemussky
Hello forum users!
Now first time not in admin section. ;-)

I've several problems on a client connection.
First of all: the connection itself works fine!

My system is a debian 7.8 with openvpn installed.
It should work als client.

The connection should be established on system start.
As doc. says therefore one have to put the config file in /etc/openvpn.
For me this does NOT work: no syslog entry, no vpn process in "ps aux".
FYI: in /etc/default/openvpn there is "AUTOSTART = all".

Ok, I can do the job in an other way, I know.

Second problem: if I start the connection with

Code: Select all

openvpn myconn.openvpn &
it opens and work.

How can ensure by script that this connection is still valid and not broken?
And if broken, how to automatically re-establish the connection?
How can I clean close the connection?

Is the an othe way than

Code: Select all

kill all <pid>
Many, many questions...

Are there some answers for me?

Thanks in advance,
lemussky

Re: automatic establish and check client connetcion?

Posted: Wed Jul 15, 2015 3:19 pm
by Traffic
lemussky wrote:My system is a debian 7.8 with openvpn installed.
It should work als client.

The connection should be established on system start.
As doc. says therefore one have to put the config file in /etc/openvpn.
For me this does NOT work: no syslog entry, no vpn process in "ps aux".
FYI: in /etc/default/openvpn there is "AUTOSTART = all".
On debian 7 there should be: /etc/init.d/openvpn which will auto start all openvpn config files in /etc/openvpn ...

where did you get openvpn ?

Here is the official repo:
https://community.openvpn.net/openvpn/w ... twareRepos

Re: automatic establish and check client connetcion?

Posted: Thu Jul 16, 2015 9:03 am
by lemussky
Thank you for your reply, Traffic!

I've installed openvpn from official debian distro.

/etc/init.d/openvpn exists and is executable.

Do you think it would be better to use installation
from openvpn repository?

Has somebody an idea how to monitor the connection?

It seems, that checking "tun0" is not reliable.

Re: automatic establish and check client connetcion?

Posted: Thu Jul 16, 2015 9:39 am
by Traffic
lemussky wrote:Do you think it would be better to use installation
from openvpn repository?
I would recommend you use the OpenVPN repo.

Re: automatic establish and check client connetcion?

Posted: Thu Jul 16, 2015 9:41 am
by lemussky
One more info: by setting verbosity I get
the following message in openvpn.log:

Code: Select all

event_wait : Interrupted system call (code=4)

Re: automatic establish and check client connetcion?

Posted: Thu Jul 16, 2015 10:07 am
by Traffic
Please post complete configs and logs.

Re: automatic establish and check client connetcion?

Posted: Thu Jul 16, 2015 11:15 am
by lemussky
Here they are.
Note, that manual start of connection works!

client.ovpn:

Code: Select all

client
dev tun
proto tcp-client
remote 217.x.x.x 443
resolv-retry infinite
nobind
persist-key
persist-tun
ca      /etc/openvpn/cacert.pem
cert    /etc/openvpn/openvpn.pem
key     /etc/openvpn/openvpn.key
log-append      /var/log/openvpn.log
verb 5
mute 20
/etc/default/openvpn:

Code: Select all

# This is the configuration file for /etc/init.d/openvpn

#
# Start only these VPNs automatically via init script.
# Allowed values are "all", "none" or space separated list of
# names of the VPNs. If empty, "all" is assumed.
# The VPN name refers to the VPN configutation file name.
# i.e. "home" would be /etc/openvpn/home.conf
#
#AUTOSTART="all"
#AUTOSTART="none"
#AUTOSTART="home office"
#
# Refresh interval (in seconds) of default status files
# located in /var/run/openvpn.$NAME.status
# Defaults to 10, 0 disables status file generation
#
#STATUSREFRESH=10
#STATUSREFRESH=0
# Optional arguments to openvpn's command line
OPTARGS=""
#
# If you need openvpn running after sendsigs, i.e.
# to let umountnfs work over the vpn, set OMIT_SENDSIGS
# to 1 and include umountnfs as Required-Stop: in openvpn's
# init.d script (remember to run insserv after that)
#
OMIT_SENDSIGS=0
log (openvpn.log) is empty after after system restart @ 11:21:17:
Sorry: my last post was my fault because it belongs to system shutdown:

Code: Select all

Thu Jul 16 11:19:51 2015 us=875268 event_wait : Interrupted system call (code=4)
Thu Jul 16 11:19:51 2015 us=875840 TCP/UDP: Closing socket
Thu Jul 16 11:19:51 2015 us=875989 /sbin/route del -net 10.8.0.1 netmask 255.255.255.255
Thu Jul 16 11:19:51 2015 us=881056 /sbin/route del -net 217.x.x.x netmask 255.255.255.128
Thu Jul 16 11:19:51 2015 us=883121 Closing TUN/TAP interface
Thu Jul 16 11:19:51 2015 us=883220 /sbin/ifconfig tun0 0.0.0.0
Thu Jul 16 11:19:51 2015 us=906367 SIGTERM[hard,] received, process exiting
"messages", "syslog", "dmesg" don't have any information about "vpn" :(

Re: automatic establish and check client connetcion?

Posted: Thu Jul 16, 2015 11:45 am
by Traffic
lemussky wrote:log (openvpn.log) is empty after after system restart
lemussky wrote:"messages", "syslog", "dmesg" don't have any information about "vpn"
Look here:
lemussky wrote:client.ovpn:
Code:
client

# Your log file
log-append /var/log/openvpn.log

# Set verb 4
verb 5

# Remove this
mute 20
And your server config ?

Re: automatic establish and check client connetcion?

Posted: Thu Jul 16, 2015 12:21 pm
by lemussky
Hmmm, still the same:

Zero byte log in /var/log/openvpn.log
(I've created it yesterday with "touch" as root).
Old version in /etc/openvpn.

I don't have access to the server config.

But as I said: manual start with "openvpn client.conf &" works.
Only automatic start with system does not.

Re: automatic establish and check client connetcion?

Posted: Thu Jul 16, 2015 12:59 pm
by Traffic
Install from the OpenVPN repos.

Compare this to yours:

/etc/init.d/openvpn

Code: Select all

#!/bin/sh -e

### BEGIN INIT INFO
# Provides:          openvpn
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Should-Start:      network-manager
# Should-Stop:       network-manager
# X-Start-Before:    $x-display-manager gdm kdm xdm wdm ldm sdm nodm
# X-Interactive:     true
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Openvpn VPN service
### END INIT INFO

# Original version by Robert Leslie
# <rob@mars.org>, edited by iwj and cs
# Modified for openvpn by Alberto Gonzalez Iniesta <agi@inittab.org>
# Modified for restarting / starting / stopping single tunnels by Richard Mueller <mueller@teamix.net>

. /lib/lsb/init-functions

test $DEBIAN_SCRIPT_DEBUG && set -v -x

DAEMON=/usr/local/sbin/openvpn
DESC="virtual private network daemon"
CONFIG_DIR=/etc/openvpn
test -x $DAEMON || exit 0
test -d $CONFIG_DIR || exit 0

# Source defaults file; edit that file to configure this script.
AUTOSTART="all"
STATUSREFRESH=10
if test -e /etc/default/openvpn ; then
  . /etc/default/openvpn
fi

start_vpn () {
    if grep -q '^[	 ]*daemon' $CONFIG_DIR/$NAME.conf ; then
      # daemon already given in config file
      DAEMONARG=
    else
      # need to daemonize
      DAEMONARG="--daemon ovpn-$NAME"
    fi

    if grep -q '^[	 ]*status ' $CONFIG_DIR/$NAME.conf ; then
      # status file already given in config file
      STATUSARG=""
    elif test $STATUSREFRESH -eq 0 ; then
      # default status file disabled in /etc/default/openvpn
      STATUSARG=""
    else
      # prepare default status file
      STATUSARG="--status /var/run/openvpn.$NAME.status $STATUSREFRESH"
    fi

    log_progress_msg "$NAME"
    STATUS=0

    start-stop-daemon --start --quiet --oknodo \
        --pidfile /var/run/openvpn.$NAME.pid \
        --exec $DAEMON -- $OPTARGS --writepid /var/run/openvpn.$NAME.pid \
        $DAEMONARG $STATUSARG --cd $CONFIG_DIR \
        --config $CONFIG_DIR/$NAME.conf || STATUS=1
}
stop_vpn () {
  kill `cat $PIDFILE` || true
  rm -f $PIDFILE
  rm -f /var/run/openvpn.$NAME.status 2> /dev/null
}

case "$1" in
start)
  log_daemon_msg "Starting $DESC"

  # autostart VPNs
  if test -z "$2" ; then
    # check if automatic startup is disabled by AUTOSTART=none
    if test "x$AUTOSTART" = "xnone" -o -z "$AUTOSTART" ; then
      log_warning_msg " Autostart disabled."
      exit 0
    fi
    if test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
      # all VPNs shall be started automatically
      for CONFIG in `cd $CONFIG_DIR; ls *.conf 2> /dev/null`; do
        NAME=${CONFIG%%.conf}
        start_vpn
      done
    else
      # start only specified VPNs
      for NAME in $AUTOSTART ; do
        if test -e $CONFIG_DIR/$NAME.conf ; then
          start_vpn
        else
          log_failure_msg "No such VPN: $NAME"
          STATUS=1
        fi
      done
    fi
  #start VPNs from command line
  else
    while shift ; do
      [ -z "$1" ] && break
      if test -e $CONFIG_DIR/$1.conf ; then
        NAME=$1
        start_vpn
      else
       log_failure_msg " No such VPN: $1"
       STATUS=1
      fi
    done
  fi
  log_end_msg ${STATUS:-0}

  ;;
stop)
  log_daemon_msg "Stopping $DESC"

  if test -z "$2" ; then
    for PIDFILE in `ls /var/run/openvpn.*.pid 2> /dev/null`; do
      NAME=`echo $PIDFILE | cut -c18-`
      NAME=${NAME%%.pid}
      stop_vpn
      log_progress_msg "$NAME"
    done
  else
    while shift ; do
      [ -z "$1" ] && break
      if test -e /var/run/openvpn.$1.pid ; then
        PIDFILE=`ls /var/run/openvpn.$1.pid 2> /dev/null`
        NAME=`echo $PIDFILE | cut -c18-`
        NAME=${NAME%%.pid}
        stop_vpn
        log_progress_msg "$NAME"
      else
        log_failure_msg " (failure: No such VPN is running: $1)"
      fi
    done
  fi
  log_end_msg 0
  ;;
# Only 'reload' running VPNs. New ones will only start with 'start' or 'restart'.
reload|force-reload)
 log_daemon_msg "Reloading $DESC"
  for PIDFILE in `ls /var/run/openvpn.*.pid 2> /dev/null`; do
    NAME=`echo $PIDFILE | cut -c18-`
    NAME=${NAME%%.pid}
# If openvpn if running under a different user than root we'll need to restart
    if egrep '^[[:blank:]]*user[[:blank:]]' $CONFIG_DIR/$NAME.conf > /dev/null 2>&1 ; then
      stop_vpn
      sleep 1
      start_vpn
      log_progress_msg "(restarted)"
    else
      kill -HUP `cat $PIDFILE` || true
    log_progress_msg "$NAME"
    fi
  done
  log_end_msg 0
  ;;

# Only 'soft-restart' running VPNs. New ones will only start with 'start' or 'restart'.
soft-restart)
 log_daemon_msg "$DESC sending SIGUSR1"
  for PIDFILE in `ls /var/run/openvpn.*.pid 2> /dev/null`; do
    NAME=`echo $PIDFILE | cut -c18-`
    NAME=${NAME%%.pid}
    kill -USR1 `cat $PIDFILE` || true
    log_progress_msg "$NAME"
  done
  log_end_msg 0
 ;;

restart)
  shift
  $0 stop ${@}
  sleep 1
  $0 start ${@}
  ;;
cond-restart)
  log_daemon_msg "Restarting $DESC."
  for PIDFILE in `ls /var/run/openvpn.*.pid 2> /dev/null`; do
    NAME=`echo $PIDFILE | cut -c18-`
    NAME=${NAME%%.pid}
    stop_vpn
    sleep 1
    start_vpn
  done
  log_end_msg 0
  ;;
status)
  GLOBAL_STATUS=0
  if test -z "$2" ; then
    # We want status for all defined VPNs.
    # Returns success if all autostarted VPNs are defined and running
    if test "x$AUTOSTART" = "xnone" ; then
      # Consider it a failure if AUTOSTART=none
      log_warning_msg "No VPN autostarted"
      GLOBAL_STATUS=1
    else
      if ! test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
        # Consider it a failure if one of the autostarted VPN is not defined
        for VPN in $AUTOSTART ; do
          if ! test -f $CONFIG_DIR/$VPN.conf ; then
            log_warning_msg "VPN '$VPN' is in AUTOSTART but is not defined"
            GLOBAL_STATUS=1
          fi
        done
      fi
    fi
    for CONFIG in `cd $CONFIG_DIR; ls *.conf 2> /dev/null`; do
      NAME=${CONFIG%%.conf}
      # Is it an autostarted VPN ?
      if test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
        AUTOVPN=1
      else
        if test "x$AUTOSTART" = "xnone" ; then
          AUTOVPN=0
        else
          AUTOVPN=0
          for VPN in $AUTOSTART; do
            if test "x$VPN" = "x$NAME" ; then
              AUTOVPN=1
            fi
          done
        fi
      fi
      if test "x$AUTOVPN" = "x1" ; then
        # If it is autostarted, then it contributes to global status
        status_of_proc -p /var/run/openvpn.${NAME}.pid openvpn "VPN '${NAME}'" || GLOBAL_STATUS=1
      else
        status_of_proc -p /var/run/openvpn.${NAME}.pid openvpn "VPN '${NAME}' (non autostarted)" || true
      fi
    done
  else
    # We just want status for specified VPNs.
    # Returns success if all specified VPNs are defined and running
    while shift ; do
      [ -z "$1" ] && break
      NAME=$1
      if test -e $CONFIG_DIR/$NAME.conf ; then
        # Config exists
        status_of_proc -p /var/run/openvpn.${NAME}.pid openvpn "VPN '${NAME}'" || GLOBAL_STATUS=1
      else
        # Config does not exist
        log_warning_msg "VPN '$NAME': missing $CONFIG_DIR/$NAME.conf file !"
        GLOBAL_STATUS=1
      fi
    done
  fi
  exit $GLOBAL_STATUS
  ;;
*)
  echo "Usage: $0 {start|stop|reload|restart|force-reload|cond-restart|soft-restart|status}" >&2
  exit 1
  ;;
esac

exit 0

# vim:set ai sts=2 sw=2 tw=0:

Re: automatic establish and check client connetcion?

Posted: Thu Jul 16, 2015 2:48 pm
by lemussky
Hello Traffic!

The script is different. I.e. the line "DAEMON="

yours: DAEMON=/usr/local/sbin/openvpn
mine: DAEMON=/usr/sbin/openvpn

Mine is fine there: script is located in "/usr/sbin/openvpn".

There is one more variable initialisation after "STATUSREFRESH=10":
OMIT_SENDSIGS=0

Mine has an additional block in "start_vpn()":

Code: Select all

    # tun using the "subnet" topology confuses the routing code that wrongly
    # emits ICMP redirects for client to client communications
    SAVED_DEFAULT_SEND_REDIRECTS=0
    if grep -q '^[[:space:]]*dev[[:space:]]*tun' $CONFIG_DIR/$NAME.conf && \
       grep -q '^[[:space:]]*topology[[:space:]]*subnet' $CONFIG_DIR/$NAME.conf ; then
        # When using "client-to-client", OpenVPN routes the traffic itself without
        # involving the TUN/TAP interface so no ICMP redirects are sent
        if ! grep -q '^[[:space:]]*client-to-client' $CONFIG_DIR/$NAME.conf ; then
            sysctl -w net.ipv4.conf.all.send_redirects=0 > /dev/null

            # Save the default value for send_redirects before disabling it
            # to make sure the tun device is created with send_redirects disabled
            SAVED_DEFAULT_SEND_REDIRECTS=$(sysctl -n net.ipv4.conf.default.send_redirects)

            if [ "$SAVED_DEFAULT_SEND_REDIRECTS" -ne 0 ]; then
              sysctl -w net.ipv4.conf.default.send_redirects=0 > /dev/null
            fi
        fi
    fi
and at the end of "start_vpn()" an other block:

Code: Select all

    [ "$OMIT_SENDSIGS" -ne 1 ] || ln -s /var/run/openvpn.$NAME.pid /run/sendsigs.omit.d/openvpn.$NAME.pid

    # Set the back the original default value of send_redirects if it was changed
    if [ "$SAVED_DEFAULT_SEND_REDIRECTS" -ne 0 ]; then
      sysctl -w net.ipv4.conf.default.send_redirects=$SAVED_DEFAULT_SEND_REDIRECTS > /dev/null
    fi
That's all.


Very strage too is the following:

I just startet
/etc/init.d/openvpn start

and got

Code: Select all

 [ ok ] Starting virtual private network daemon:.
But I can't find
- any "openvpn.*.pid",
- ps aux | grep vpn
- ps -ef | grep vpn
- and log entries...

May be ist better to "apt-get purge" and install new from openvpn repository(?)