Subversion Repositories ALCASAR

Rev

Rev 790 | Rev 845 | 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 840 2012-03-16 14:15:41Z richard $
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"
363 richard 35
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"eth0 link down\";?g" $Index_Page
308 richard 36
		;;
37
	"2")
38
		logger "can't contact the default router"
363 richard 39
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"can't contact the default router\";?g" $Index_Page
308 richard 40
		;;
41
	esac
520 richard 42
	net_pb=`cat /etc/dnsmasq.conf|grep "address=/#/"|wc -l`
43
	if [ $net_pb = "0" ] # on alerte les usagers (si ce n'est pas déjà le cas).
308 richard 44
		then
360 richard 45
		/bin/sed -i "s?^\$network_pb.*?\$network_pb = True;?g" $Index_Page
520 richard 46
		/bin/sed -i "s?^conf-dir=.*?address=\/#\/$PRIVATE_IP?g" /etc/dnsmasq-blackhole.conf
47
		/bin/sed -i "1i\address=\/#\/$PRIVATE_IP" /etc/dnsmasq.conf
308 richard 48
		/etc/init.d/dnsmasq restart
49
	fi
50
}
51
 
783 richard 52
function lan_test ()
53
# LAN connectiivity testing
54
{
784 richard 55
	watchdog_process=`ps -C alcasar-watchdog.sh|wc -l`
56
	if [[ $(expr $watchdog_process) -gt 3 ]]
57
		then
58
		echo "ALCASAR watchdog is already running"
59
		exit 0
60
	fi
783 richard 61
	# EXTIF testing
62
	LAN_DOWN="0"
63
	if [ "`/usr/sbin/ethtool $EXTIF|grep Link|cut -d' ' -f3`" != "yes" ]
64
		then
65
		LAN_DOWN="1"
308 richard 66
	fi
783 richard 67
	# Default GW testing
68
	if [ $LAN_DOWN -eq "0" ]
308 richard 69
		then
783 richard 70
		IP_GW=`/sbin/ip route list|grep ^default|cut -d" " -f3`
71
		arp_reply=`/usr/sbin/arping -I$EXTIF -c1 $IP_GW|grep response|cut -d" " -f2`
72
		if [ $arp_reply -eq "0" ]
73
	       		then
74
			LAN_DOWN="2"
75
		fi
308 richard 76
	fi
783 richard 77
	# if LAN pb detected, users are warned
78
	if [ $LAN_DOWN != "0" ]
79
		then
80
			lan_down_alert
81
	# else switch in normal mode
82
	else
83
		net_pb=`cat /etc/dnsmasq.conf|grep "address=/#/"|wc -l`
84
		if [ $net_pb != "0" ]
85
			then
86
			/bin/sed -i "s?^\$network_pb.*?\$network_pb = False;?g" $Index_Page
87
			/bin/sed -i "s?^address=\/#\/.*?conf-dir=/usr/local/etc/alcasar-dnsfilter-enabled?g" /etc/dnsmasq-blackhole.conf
88
			/bin/sed -i "/^address=/d" /etc/dnsmasq.conf
89
			/etc/init.d/dnsmasq restart
90
		fi
91
	fi
92
}
93
 
94
usage="Usage: alcasar-watchdog.sh {-lt --lan_test}"
95
case $1 in
96
	-\? | -h* | --h*)
97
		echo "$usage"
98
		exit 0
99
		;;
100
	-lt | --lan_test)
101
		lan_test
102
		exit 0
103
		;;
104
	*)
105
		lan_test
840 richard 106
		# read file that contains IP address of quiet equipments
783 richard 107
		if [ -e $tmp_file ]; then
108
			cat $tmp_file | while read noresponse
109
			do
110
				noresponse_ip=`echo $noresponse | cut -d" " -f1`
111
				noresponse_mac=`echo $noresponse | cut -d" " -f2`
840 richard 112
				noresponse_user=`echo $noresponse | cut -d" " -f3`
787 richard 113
				arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c1 -w4 $noresponse_ip|grep "Unicast reply"|wc -l`
783 richard 114
				if [[ $(expr $arp_reply) -eq 0 ]]
115
	       				then
840 richard 116
					logger "alcasar-watchdog $noresponse_ip ($noresponse_mac) can't be contact. Alcasar disconnects the user ($noresponce_user)."
117
					/usr/sbin/chilli_query logout $noresponse_mac
118
					/usr/sbin/chilli_query dhcp-release $noresponse_mac  # release dhcp for mac_auth equipment 
783 richard 119
				fi
120
			done
121
			rm $tmp_file
1 root 122
		fi
123
# on traite chaque équipements connus de chilli
783 richard 124
		for system in `/usr/sbin/chilli_query list |grep -v "\.0\.0\.0"`
125
		do
126
			active_ip=`echo $system |cut -d" " -f2`
127
			active_session=`echo $system |cut -d" " -f5`
128
			active_mac=`echo $system | cut -d" " -f1`
840 richard 129
			active_user=`echo $system |cut -d" " -f6`
109 richard 130
# on ne traite que les équipements exploitées par un usager authentifié (test de 2 réponses en 4 secondes)
783 richard 131
			if [[ $(expr $active_session) -eq 1 ]]
132
				then
133
				arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c2 -w4 $active_ip|grep "Unicast reply"|wc -l`
1 root 134
# on stocke les adresses IP des stations muettes
783 richard 135
				if [[ $(expr $arp_reply) -eq 0 ]]
136
	       				then
840 richard 137
					echo "$active_ip $active_mac $active_user" >> $tmp_file
783 richard 138
				fi
1 root 139
# on deconnecte l'usager d'une stations usurpée (@MAC)
783 richard 140
				if [[ $(expr $arp_reply) -gt 2 ]]
141
	       				then
840 richard 142
					echo "alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)." >> /var/Save/logs/security/watchdog.log
143
					logger "alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)."
783 richard 144
					/usr/sbin/chilli_query logout $active_mac
145
				fi
146
			fi
147
		done
148
		;;
149
esac	
597 richard 150
IFS=$OLDIFS