Subversion Repositories ALCASAR

Rev

Rev 1867 | Rev 2075 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
1535 richard 1
#!/bin/bash
64 franck 2
# $Id: alcasar-watchdog.sh 2008 2016-07-25 09:00:46Z raphael.pion $
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)
308 richard 7
# Ce script prévient les usagers de l'indisponibilité de l'accès Internet
8
# il déconnecte les usagers dont
2008 raphael.pi 9
# - les équipements réseau ne répondent plus ( si la fenetre status.php a été fermé. Les autorisations par adresse mac sont laissé de côté)
1 root 10
# - les adresses MAC sont usurpées
308 richard 11
# This script tells users that Internet access is down
12
# it logs out users whose 
2008 raphael.pi 13
# - PCs are quiet (it means that status.php has been closed. We put aside @MAC Authorization )
1157 stephane 14
# - MAC address is used by other systems (usurped)
672 richard 15
 
1469 richard 16
CONF_FILE="/usr/local/etc/alcasar.conf"
17
EXTIF=`grep ^EXTIF= $CONF_FILE|cut -d"=" -f2`			# EXTernal InterFace
18
INTIF=`grep ^INTIF= $CONF_FILE|cut -d"=" -f2`			# INTernal InterFace
19
private_ip_mask=`grep PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
786 richard 20
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
1157 stephane 21
PRIVATE_IP=`echo "$private_ip_mask" |cut -d"/" -f1`      # @ip du portail (côté LAN)
22
PRIVATE_IP=${PRIVATE_IP:=192.168.182.1}
1 root 23
tmp_file="/tmp/watchdog.txt"
2008 raphael.pi 24
tmp_users_file="/tmp/current_users.txt"
316 richard 25
DIR_WEB="/var/www/html"
360 richard 26
Index_Page="$DIR_WEB/index.php"
1472 richard 27
IPTABLES="/sbin/iptables"
28
TUNIF="tun0"						# listen device for chilli daemon
597 richard 29
OLDIFS=$IFS
1 root 30
IFS=$'\n'
308 richard 31
 
783 richard 32
function lan_down_alert ()
1157 stephane 33
# users are redirected on ALCASAR IP address if a LAN problem is detected
308 richard 34
{
783 richard 35
	case $LAN_DOWN in
308 richard 36
	"1")
1469 richard 37
		logger "$EXTIF (WAN card) link down"
1474 richard 38
		echo "$EXTIF (WAN card) link down"
1469 richard 39
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"$EXTIF (WAN card) link down\";?g" $Index_Page
308 richard 40
		;;
41
	"2")
42
		logger "can't contact the default router"
987 richard 43
		echo "can't contact the default router"
363 richard 44
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"can't contact the default router\";?g" $Index_Page
308 richard 45
		;;
46
	esac
1472 richard 47
	net_pb=`grep "network_pb = True;" $Index_Page|wc -l`
48
	if [ $net_pb = "0" ] # user alert (only the first time)	
308 richard 49
		then
360 richard 50
		/bin/sed -i "s?^\$network_pb.*?\$network_pb = True;?g" $Index_Page
1472 richard 51
		$IPTABLES -I PREROUTING -t nat -i $TUNIF -p udp --dport domain -j REDIRECT --to-port 56
308 richard 52
	fi
53
}
54
 
783 richard 55
function lan_test ()
56
# LAN connectiivity testing
57
{
784 richard 58
	watchdog_process=`ps -C alcasar-watchdog.sh|wc -l`
59
	if [[ $(expr $watchdog_process) -gt 3 ]]
60
		then
61
		echo "ALCASAR watchdog is already running"
62
		exit 0
63
	fi
783 richard 64
	# EXTIF testing
65
	LAN_DOWN="0"
1472 richard 66
	if [ `/sbin/ip link | grep $EXTIF|grep "NO-CARRIER" | wc -l` -eq "1" ]
783 richard 67
		then
68
		LAN_DOWN="1"
308 richard 69
	fi
783 richard 70
	# Default GW testing
71
	if [ $LAN_DOWN -eq "0" ]
308 richard 72
		then
783 richard 73
		IP_GW=`/sbin/ip route list|grep ^default|cut -d" " -f3`
74
		arp_reply=`/usr/sbin/arping -I$EXTIF -c1 $IP_GW|grep response|cut -d" " -f2`
75
		if [ $arp_reply -eq "0" ]
76
	       		then
77
			LAN_DOWN="2"
78
		fi
308 richard 79
	fi
783 richard 80
	# if LAN pb detected, users are warned
81
	if [ $LAN_DOWN != "0" ]
82
		then
83
			lan_down_alert
84
	# else switch in normal mode
85
	else
987 richard 86
		echo "Internet access is OK for now"
1472 richard 87
		net_pb=`grep "network_pb = True;" $Index_Page|wc -l`
783 richard 88
		if [ $net_pb != "0" ]
89
			then
90
			/bin/sed -i "s?^\$network_pb.*?\$network_pb = False;?g" $Index_Page
1472 richard 91
			$IPTABLES -D PREROUTING -t nat -i $TUNIF -p udp --dport domain -j REDIRECT --to-port 56
783 richard 92
		fi
93
	fi
94
}
95
 
96
usage="Usage: alcasar-watchdog.sh {-lt --lan_test}"
97
case $1 in
98
	-\? | -h* | --h*)
99
		echo "$usage"
100
		exit 0
101
		;;
102
	-lt | --lan_test)
103
		lan_test
104
		exit 0
105
		;;
106
	*)
107
		lan_test
2008 raphael.pi 108
		# read file that contains IP address of user who are not in $tmp_users_file (its means that status.php has been closed)
783 richard 109
		if [ -e $tmp_file ]; then
110
			cat $tmp_file | while read noresponse
111
			do
112
				noresponse_ip=`echo $noresponse | cut -d" " -f1`
113
				noresponse_mac=`echo $noresponse | cut -d" " -f2`
840 richard 114
				noresponse_user=`echo $noresponse | cut -d" " -f3`
2008 raphael.pi 115
				data_user=$(/usr/sbin/chilli_query list | grep -v "\.0\.0\.0" | awk '$5 == 1' | grep $noresponse_ip)
116
				if [ $(echo $data_users | wc -l) -eq 1 ] #if it still connected ...
117
	       			then
118
					if [[ $noresponse_user == $noresponse_mac ]] #we let @mac auth equipments connected
119
						then 
120
						#/usr/sbin/chilli_query dhcp-release $noresponse_mac # for @mac auth equipments, we must remove the arp entry 
121
						echo "MAC authentification : $noresponse_mac (keep on connection)"
122
					else #we disconnect user because status.php has been closed
123
						logger "alcasar-watchdog $noresponse_ip ($noresponse_mac) can't be contact. Alcasar disconnects the user ($noresponse_user)."
124
						echo "Disconnect : $noresponse_ip $noresponse_mac $noresponse_user"
125
						/usr/sbin/chilli_query logout $noresponse_mac
1688 richard 126
					fi
783 richard 127
				fi
2008 raphael.pi 128
 
783 richard 129
			done
130
			rm $tmp_file
2008 raphael.pi 131
			if [ -e $tmp_users_file ]; then
132
				rm $tmp_users_file
133
			fi
134
 
1 root 135
		fi
2008 raphael.pi 136
 
137
		#this temporary file contains every 'user IP address' which are connected to ALCASAR (status.php still open)
138
		if [ -e $tmp_users_file ]; then 
139
 
140
			for ip_user in $(cat $tmp_users_file)
141
			do
142
				#Check if user is still connected
143
				data_user=$(/usr/sbin/chilli_query list | grep -v "\.0\.0\.0" | awk '$5 == 1' | grep $ip_user)
144
				if [ $(echo $data_users | wc -l) -eq 0 ] #if user not in the file $tmp_users_file  (it means that status.php has been closed)
145
				then
146
					#save info in tmp_file
147
					active_ip=`echo $data_user |cut -d" " -f2`
148
		                        active_mac=`echo $data_user | cut -d" " -f1`
149
		                        active_user=`echo $data_user |cut -d" " -f6`
150
 
151
					echo "$active_ip $active_mac $active_user" >> $tmp_file #save info for next time, user will be disconnected.
152
				fi
153
			done
154
			rm $tmp_users_file
155
		else
156
			for system in $(/usr/sbin/chilli_query list |grep -v "\.0\.0\.0" | awk '$5 == 1')
157
			do
158
				#disconnect all users
159
                                active_ip=`echo $system |cut -d" " -f2`
160
                                active_mac=`echo $system | cut -d" " -f1`
161
                                active_user=`echo $system |cut -d" " -f6`
162
                                echo "$active_ip $active_mac $active_user" >> $tmp_file
163
			done
164
		fi
165
 
166
 
167
		# process each equipment known by chilli to check if any equipment is usurped (@MAC)
783 richard 168
		for system in `/usr/sbin/chilli_query list |grep -v "\.0\.0\.0"`
169
		do
170
			active_ip=`echo $system |cut -d" " -f2`
171
			active_session=`echo $system |cut -d" " -f5`
172
			active_mac=`echo $system | cut -d" " -f1`
840 richard 173
			active_user=`echo $system |cut -d" " -f6`
2008 raphael.pi 174
			# process only equipment with an authenticated user
783 richard 175
			if [[ $(expr $active_session) -eq 1 ]]
1157 stephane 176
			then
783 richard 177
				arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c2 -w4 $active_ip|grep "Unicast reply"|wc -l`
2008 raphael.pi 178
				# disconnect users whose equipement is usurped (@MAC). For example, if there are 2 same @MAC it will make 3 lines in output.
783 richard 179
				if [[ $(expr $arp_reply) -gt 2 ]]
1202 crox53 180
	       				then 
1500 richard 181
					echo "$(date "+[%x-%X] : ")alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)." >> /var/Save/security/watchdog.log
840 richard 182
					logger "alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)."
783 richard 183
					/usr/sbin/chilli_query logout $active_mac
1500 richard 184
					chmod 644 /var/Save/security/watchdog.log
783 richard 185
				fi
186
			fi
187
		done
188
		;;
189
esac	
597 richard 190
IFS=$OLDIFS