#!/bin/bash

# Script called by pppd upon authentication failure
# Purpose: get the source IP address of the VPN client and call
#	the logConnection application which keeps track of possible
#	attacks and possibly prevents this IP from connecting to OXO.

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

# Params: pppdPid, serverPid (PPTPd or L2TPd)
pppdPid=$1
parentPid=$2

# 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

# ensure ipcheck file can be written also when called from tkt-login
if [ ! -e $ipFile ];then
	touch $ipFile
	chown root.webadmin $ipFile
	chmod 660 $ipFile
fi

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

