Subversion Repositories ALCASAR

Rev

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

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