Hi Frank,
I modified the script but I think I miss something ?
#!/bin/bash
# I modified the script according to my need. Thanks to Frank Uittenbosch (OpenVPN Community)
# FileName connect
LOG=/var/log/openvpn-log
OPENVPN=/var/log/openvpn/openvpn.log
DATE=`date`
ALLOWIPFILE="/etc/openvpn/firewall/allowed-ip"
ADMINS="
kashif@domain.com"
function ip () {
IP=`tail -4000 $OPENVPN |grep -i $USERO|awk '{print $6}' |tail -1|cut -d "/" -f2|cut -d ":" -f1`
USER=`tail -4000 $OPENVPN |grep -i $USERO|awk '{print $6}' |tail -1|cut -d "/" -f1|cut -d "." -f1`
}
function checkip () {
egrep -i $USERO $ALLOWIPFILE && CHECK=1 || CHECK=0
if [ "$CHECK" == "1" ];then
ALLOWEDIP=`grep -i $USERO $ALLOWIPFILE |cut -d "," -f1`
ALLOWEDUS=`grep -i $USERO $ALLOWIPFILE |cut -d "," -f2`
if [ "$ALLOWEDIP" == "$IP" ];then
echo "$ALLOWEDUS is allowed from $IP" >>$LOG
elif [ "$ALLOWEDIP" != "$IP" ];then
echo "$USERO is NOT allowed from ip-address:$IP! only from: $ALLOWEDIP" >>$LOG
echo "According to $ALLOWIPFILE, $USERO came from $IP, But is only allowed from $ALLOWEDIP" |mail $ADMINS -s "OpenVPN: Unauthorized connection attempt
by $USERO"
else
echo "wtf is going on ? $ALLOWEDIP $ALLOWEDUS $IP" >>$LOG
fi
else
echo "$USERO is not in the allowed list so always allowed." >>$LOG
fi
}
if [ "$1" == "add" ]
then
USERO=`echo $3 |cut -d "." -f1`
ip
echo "$DATE $USERO CONNECTED to openvpn from $IP" >>$LOG
checkip
elif [ "$1" == "delete" ]
then
echo "$DATE $2 $3 DISCONNECTED from openvpn " >>$LOG
elif [ "$1" == "update" ]
then
USERO=`echo $3 |cut -d "." -f1`
ip
echo "$DATE $USERO CONNECTED to openvpn from $IP " >>$LOG
checkip
else
echo ERROR, ERROR, DOES NOT COMPUTE !! $1 $2 $3 >> $LOG
fi
#END
OpenVPN Log
------------------
ay 15 13:35:44 local 95.x.x.22:15365 LZO compression initialized
May 15 13:35:45 local 95.x.x.22:15365 [user1] Peer Connection Initiated with 95.x.x.22:15365 (via 192.x.x.2)
May 15 13:35:45 local user1/95.x.x.22:15365 client-connect command failed: external program exited with error status: 1
May 15 13:35:45 local 46.x.x.148:61291 Re-using SSL/TLS context
May 15 13:35:45 local 46.x.x.148:61291 LZO compression initialized
May 15 13:35:46 local 46.x.x.148:1195 Re-using SSL/TLS context
May 15 13:35:46 local 46.x.x.148:1195 LZO compression initialized
May 15 13:35:47 local 46.x.x.148:61291 [user2] Peer Connection Initiated with 46.x.x.148:61291 (via 192.168.110.2)
May 15 13:35:47 local user2/46.x.x.148:61291 client-connect command failed: external program exited with error status: 1
May 15 13:35:55 local 77.x.x.249:51276 [user3] Peer Connection Initiated with 77.x.x.249:51276 (via 192.x.x.2)
May 15 13:35:55 local user3/77.x.x.249:51276 client-connect command failed: external program exited with error status: 1
May 15 13:36:01 local read UDPv4 [CMSG=8|ECONNREFUSED]: Connection refused (code=111)
May 15 13:36:14 local OpenVPN CLIENT LIST
May 15 13:36:14 local Updated,Tue May 15 13:36:14 2012
May 15 13:36:14 local Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
May 15 13:36:14 local ROUTING TABLE
May 15 13:36:14 local Virtual Address,Common Name,Real Address,Last Ref
May 15 13:36:14 local GLOBAL STATS
May 15 13:36:14 local Max bcast/mcast queue length,0
May 15 13:36:14 local END
May 15 13:36:14 local event_wait : Interrupted system call (code=4)
May 15 13:36:14 local OpenVPN CLIENT LIST
May 15 13:36:14 local Updated,Tue May 15 13:36:14 2012
May 15 13:36:14 local Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
May 15 13:36:14 local ROUTING TABLE
May 15 13:36:14 local Virtual Address,Common Name,Real Address,Last Ref
May 15 13:36:14 local GLOBAL STATS
May 15 13:36:14 local Max bcast/mcast queue length,0
May 15 13:36:14 local END
May 15 13:36:15 local event_wait : Interrupted system call (code=4)
allowed-ip file content
---------------------------
77.x.x.249,user3
77.x.x.249,user4
Added in /etc/openvpn/openvpn.conf
----------------------------------------------
client-connect "/etc/openvpn/scripts/connect"
Note: There was already a client-connect/client-disconnect option (/usr/local/bin/openvpn-client-connect, /usr/local/bin/openvpn-client-disconnect) in the openvpn configuration and I disabled both but the same result. That file has the following code.
import os
import sys
from endian.job.engine_control import send_cmd_to_engine
if len(sys.argv) < 2 or not 'common_name' in os.environ or not 'ifconfig_pool_remote_ip' in os.environ:
print "Invalid usage - this script must be triggered by OpenVPN daemon"
sys.exit(2)
print send_cmd_to_engine("call openvpnjob.client_connect",
options={'config_pathname': os.path.abspath(sys.argv[1]),
'common_name': os.environ['common_name'],
'ifconfig_pool_remote_ip': os.environ['ifconfig_pool_remote_ip']})