#!/bin/bash

# Script called by pppd upon authentication success
# Purpose: get the source IP address of the VPN client and call
#	the logConnection application which resets the failure counter
#	for this IP.

logfile="/var/log/ia_syslog_daemon"
binPath="/opt/itrnet/config/cgi-bin"
doTraces=0

# Params sent by pppd: 
# ifname, peer_name, user_name, devname, speed, pppdPid, serverPid (PPTPd or L2TPd)
# We care only about the two last ones for our purpose
pppdPid=$6
parentPid=$7

# Get the daemon log line containing the pub IP of the client
# Both servers log a line containing: their Pid, the pppd pid
# and the IP address. Hence we are sure to get the right IP.
string=`grep $pppdPid $logfile | grep $parentPid`

# Extract the IP address from this raw line
clientIP=`echo $string | sed 's/^.*[: ]\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\).*$/\1/'`

if [ -z "$clientIP" -o -n "`echo $clientIP | grep [^0-9.]`" ];then
	if [ $doTraces -eq 1 ];then
		logger -p daemon.info -t TESTAUTHSCRIPT "Bad IP"
	fi
	exit 1
fi

if [ $doTraces -eq 1 ];then
	logger -p daemon.info -t TESTAUTHSCRIPT "found client IP $clientIP"
fi

# now call connection manager, action = accept.
$binPath/logConnection --remote $clientIP --action accept

