Subversion Repositories ALCASAR

Rev

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

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