Subversion Repositories ALCASAR

Rev

Rev 987 | Rev 1056 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
672 richard 1
#!/bin/bash
64 franck 2
# $Id: alcasar-watchdog.sh 988 2012-08-20 21:33:01Z franck $
672 richard 3
 
4
# alcasar-watchdog.sh
790 richard 5
# by Rexy
672 richard 6
# This script is distributed under the Gnu General Public License (GPL)
7
 
308 richard 8
# Ce script prévient les usagers de l'indisponibilité de l'accès Internet
9
# il déconnecte les usagers dont
376 franck 10
# - les équipements réseau ne répondent plus
1 root 11
# - les adresses MAC sont usurpées
308 richard 12
# This script tells users that Internet access is down
13
# it logs out users whose 
1 root 14
# - PCs are quiet
15
# - MAC address are in used by other systems (usurped)
672 richard 16
 
308 richard 17
EXTIF="eth0"
1 root 18
INTIF="eth1"
786 richard 19
conf_file="/usr/local/etc/alcasar.conf"
20
private_ip_mask=`grep PRIVATE_IP= $conf_file|cut -d"=" -f2`
21
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
22
PRIVATE_IP=`echo $private_ip_mask | cut -d"/" -f1`			# ALCASAR LAN IP address
1 root 23
tmp_file="/tmp/watchdog.txt"
316 richard 24
DIR_WEB="/var/www/html"
360 richard 25
Index_Page="$DIR_WEB/index.php"
597 richard 26
OLDIFS=$IFS
1 root 27
IFS=$'\n'
308 richard 28
 
783 richard 29
function lan_down_alert ()
30
# users are redirected on ALCASAR IP address if LAN Pb detected
308 richard 31
{
783 richard 32
	case $LAN_DOWN in
308 richard 33
	"1")
34
		logger "eth0 link down"
987 richard 35
		echo "eth0 is down"
363 richard 36
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"eth0 link down\";?g" $Index_Page
308 richard 37
		;;
38
	"2")
39
		logger "can't contact the default router"
987 richard 40
		echo "can't contact the default router"
363 richard 41
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"can't contact the default router\";?g" $Index_Page
308 richard 42
		;;
43
	esac
520 richard 44
	net_pb=`cat /etc/dnsmasq.conf|grep "address=/#/"|wc -l`
45
	if [ $net_pb = "0" ] # on alerte les usagers (si ce n'est pas déjà le cas).
308 richard 46
		then
360 richard 47
		/bin/sed -i "s?^\$network_pb.*?\$network_pb = True;?g" $Index_Page
520 richard 48
		/bin/sed -i "s?^conf-dir=.*?address=\/#\/$PRIVATE_IP?g" /etc/dnsmasq-blackhole.conf
49
		/bin/sed -i "1i\address=\/#\/$PRIVATE_IP" /etc/dnsmasq.conf
308 richard 50
		/etc/init.d/dnsmasq restart
51
	fi
52
}
53
 
783 richard 54
function lan_test ()
55
# LAN connectiivity testing
56
{
784 richard 57
	watchdog_process=`ps -C alcasar-watchdog.sh|wc -l`
58
	if [[ $(expr $watchdog_process) -gt 3 ]]
59
		then
60
		echo "ALCASAR watchdog is already running"
61
		exit 0
62
	fi
783 richard 63
	# EXTIF testing
64
	LAN_DOWN="0"
988 franck 65
	if [ "`/usr/sbin/ethtool $EXTIF|grep Link|cut -d' ' -f3`" != "yes" ] && [ "`/sbin/mii-tool $EXTIF | grep -i link | awk '{print $NF}'`" != "ok" ]
783 richard 66
		then
67
		LAN_DOWN="1"
308 richard 68
	fi
783 richard 69
	# Default GW testing
70
	if [ $LAN_DOWN -eq "0" ]
308 richard 71
		then
783 richard 72
		IP_GW=`/sbin/ip route list|grep ^default|cut -d" " -f3`
73
		arp_reply=`/usr/sbin/arping -I$EXTIF -c1 $IP_GW|grep response|cut -d" " -f2`
74
		if [ $arp_reply -eq "0" ]
75
	       		then
76
			LAN_DOWN="2"
77
		fi
308 richard 78
	fi
783 richard 79
	# if LAN pb detected, users are warned
80
	if [ $LAN_DOWN != "0" ]
81
		then
82
			lan_down_alert
83
	# else switch in normal mode
84
	else
987 richard 85
		echo "Internet access is OK for now"
783 richard 86
		net_pb=`cat /etc/dnsmasq.conf|grep "address=/#/"|wc -l`
87
		if [ $net_pb != "0" ]
88
			then
89
			/bin/sed -i "s?^\$network_pb.*?\$network_pb = False;?g" $Index_Page
90
			/bin/sed -i "s?^address=\/#\/.*?conf-dir=/usr/local/etc/alcasar-dnsfilter-enabled?g" /etc/dnsmasq-blackhole.conf
91
			/bin/sed -i "/^address=/d" /etc/dnsmasq.conf
92
			/etc/init.d/dnsmasq restart
93
		fi
94
	fi
95
}
96
 
97
usage="Usage: alcasar-watchdog.sh {-lt --lan_test}"
98
case $1 in
99
	-\? | -h* | --h*)
100
		echo "$usage"
101
		exit 0
102
		;;
103
	-lt | --lan_test)
104
		lan_test
105
		exit 0
106
		;;
107
	*)
108
		lan_test
840 richard 109
		# read file that contains IP address of quiet equipments
783 richard 110
		if [ -e $tmp_file ]; then
111
			cat $tmp_file | while read noresponse
112
			do
113
				noresponse_ip=`echo $noresponse | cut -d" " -f1`
114
				noresponse_mac=`echo $noresponse | cut -d" " -f2`
840 richard 115
				noresponse_user=`echo $noresponse | cut -d" " -f3`
787 richard 116
				arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c1 -w4 $noresponse_ip|grep "Unicast reply"|wc -l`
783 richard 117
				if [[ $(expr $arp_reply) -eq 0 ]]
118
	       				then
845 richard 119
					logger "alcasar-watchdog $noresponse_ip ($noresponse_mac) can't be contact. Alcasar disconnects the user ($noresponse_user)."
840 richard 120
					/usr/sbin/chilli_query logout $noresponse_mac
121
					/usr/sbin/chilli_query dhcp-release $noresponse_mac  # release dhcp for mac_auth equipment 
783 richard 122
				fi
123
			done
124
			rm $tmp_file
1 root 125
		fi
126
# on traite chaque équipements connus de chilli
783 richard 127
		for system in `/usr/sbin/chilli_query list |grep -v "\.0\.0\.0"`
128
		do
129
			active_ip=`echo $system |cut -d" " -f2`
130
			active_session=`echo $system |cut -d" " -f5`
131
			active_mac=`echo $system | cut -d" " -f1`
840 richard 132
			active_user=`echo $system |cut -d" " -f6`
109 richard 133
# on ne traite que les équipements exploitées par un usager authentifié (test de 2 réponses en 4 secondes)
783 richard 134
			if [[ $(expr $active_session) -eq 1 ]]
135
				then
136
				arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c2 -w4 $active_ip|grep "Unicast reply"|wc -l`
1 root 137
# on stocke les adresses IP des stations muettes
783 richard 138
				if [[ $(expr $arp_reply) -eq 0 ]]
139
	       				then
840 richard 140
					echo "$active_ip $active_mac $active_user" >> $tmp_file
783 richard 141
				fi
1 root 142
# on deconnecte l'usager d'une stations usurpée (@MAC)
783 richard 143
				if [[ $(expr $arp_reply) -gt 2 ]]
144
	       				then
840 richard 145
					echo "alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)." >> /var/Save/logs/security/watchdog.log
146
					logger "alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)."
783 richard 147
					/usr/sbin/chilli_query logout $active_mac
148
				fi
149
			fi
150
		done
151
		;;
152
esac	
597 richard 153
IFS=$OLDIFS