Subversion Repositories ALCASAR

Rev

Rev 790 | Rev 845 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

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