Subversion Repositories ALCASAR

Rev

Rev 1867 | Rev 2075 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 1867 Rev 2008
Line 1... Line 1...
1
#!/bin/bash
1
#!/bin/bash
2
# $Id: alcasar-watchdog.sh 1867 2016-05-04 12:22:08Z raphael.pion $
2
# $Id: alcasar-watchdog.sh 2008 2016-07-25 09:00:46Z raphael.pion $
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
# Ce script prévient les usagers de l'indisponibilité de l'accès Internet
7
# Ce script prévient les usagers de l'indisponibilité de l'accès Internet
8
# il déconnecte les usagers dont
8
# il déconnecte les usagers dont
9
# - les équipements réseau ne répondent plus
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é)
10
# - les adresses MAC sont usurpées
10
# - les adresses MAC sont usurpées
11
# This script tells users that Internet access is down
11
# This script tells users that Internet access is down
12
# it logs out users whose 
12
# it logs out users whose 
13
# - PCs are quiet
13
# - PCs are quiet (it means that status.php has been closed. We put aside @MAC Authorization )
14
# - MAC address is used by other systems (usurped)
14
# - MAC address is used by other systems (usurped)
15
 
15
 
16
CONF_FILE="/usr/local/etc/alcasar.conf"
16
CONF_FILE="/usr/local/etc/alcasar.conf"
17
EXTIF=`grep ^EXTIF= $CONF_FILE|cut -d"=" -f2`			# EXTernal InterFace
17
EXTIF=`grep ^EXTIF= $CONF_FILE|cut -d"=" -f2`			# EXTernal InterFace
18
INTIF=`grep ^INTIF= $CONF_FILE|cut -d"=" -f2`			# INTernal InterFace
18
INTIF=`grep ^INTIF= $CONF_FILE|cut -d"=" -f2`			# INTernal InterFace
19
private_ip_mask=`grep PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
19
private_ip_mask=`grep PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
20
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
20
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
21
PRIVATE_IP=`echo "$private_ip_mask" |cut -d"/" -f1`      # @ip du portail (côté LAN)
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}
22
PRIVATE_IP=${PRIVATE_IP:=192.168.182.1}
23
tmp_file="/tmp/watchdog.txt"
23
tmp_file="/tmp/watchdog.txt"
-
 
24
tmp_users_file="/tmp/current_users.txt"
24
DIR_WEB="/var/www/html"
25
DIR_WEB="/var/www/html"
25
Index_Page="$DIR_WEB/index.php"
26
Index_Page="$DIR_WEB/index.php"
26
IPTABLES="/sbin/iptables"
27
IPTABLES="/sbin/iptables"
27
TUNIF="tun0"						# listen device for chilli daemon
28
TUNIF="tun0"						# listen device for chilli daemon
28
OLDIFS=$IFS
29
OLDIFS=$IFS
Line 102... Line 103...
102
		lan_test
103
		lan_test
103
		exit 0
104
		exit 0
104
		;;
105
		;;
105
	*)
106
	*)
106
		lan_test
107
		lan_test
107
		# read file that contains IP address of quiet equipments
108
		# read file that contains IP address of user who are not in $tmp_users_file (its means that status.php has been closed)
108
		if [ -e $tmp_file ]; then
109
		if [ -e $tmp_file ]; then
109
			cat $tmp_file | while read noresponse
110
			cat $tmp_file | while read noresponse
110
			do
111
			do
111
				noresponse_ip=`echo $noresponse | cut -d" " -f1`
112
				noresponse_ip=`echo $noresponse | cut -d" " -f1`
112
				noresponse_mac=`echo $noresponse | cut -d" " -f2`
113
				noresponse_mac=`echo $noresponse | cut -d" " -f2`
113
				noresponse_user=`echo $noresponse | cut -d" " -f3`
114
				noresponse_user=`echo $noresponse | cut -d" " -f3`
114
				arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c1 -w4 $noresponse_ip|grep "Unicast reply"|wc -l`
115
				data_user=$(/usr/sbin/chilli_query list | grep -v "\.0\.0\.0" | awk '$5 == 1' | grep $noresponse_ip)
115
				if [[ $(expr $arp_reply) -eq 0 ]]
116
				if [ $(echo $data_users | wc -l) -eq 1 ] #if it still connected ...
116
	       				then
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 
117
					#on vide les ip inactifs de l'ipset not_auth_yet
121
						echo "MAC authentification : $noresponse_mac (keep on connection)"
118
					ipset del not_auth_yet $noresponse_ip
122
					else #we disconnect user because status.php has been closed
119
					logger "alcasar-watchdog $noresponse_ip ($noresponse_mac) can't be contact. Alcasar disconnects the user ($noresponse_user)."
123
						logger "alcasar-watchdog $noresponse_ip ($noresponse_mac) can't be contact. Alcasar disconnects the user ($noresponse_user)."
120
					/usr/sbin/chilli_query logout $noresponse_mac
-
 
121
					if [[ $noresponse_user == $noresponse_mac ]] # for @mac auth equipments, we must remove the arp entry 
124
						echo "Disconnect : $noresponse_ip $noresponse_mac $noresponse_user"
122
						then /usr/sbin/chilli_query dhcp-release $noresponse_mac
125
						/usr/sbin/chilli_query logout $noresponse_mac
123
					fi
126
					fi
124
				fi
127
				fi
-
 
128
                                
125
			done
129
			done
126
			rm $tmp_file
130
			rm $tmp_file
-
 
131
			if [ -e $tmp_users_file ]; then
-
 
132
				rm $tmp_users_file
-
 
133
			fi
-
 
134
 
-
 
135
		fi
-
 
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
127
		fi
164
		fi
-
 
165
 
-
 
166
 
128
# process each equipment known by chilli
167
		# process each equipment known by chilli to check if any equipment is usurped (@MAC)
129
		for system in `/usr/sbin/chilli_query list |grep -v "\.0\.0\.0"`
168
		for system in `/usr/sbin/chilli_query list |grep -v "\.0\.0\.0"`
130
		do
169
		do
131
			active_ip=`echo $system |cut -d" " -f2`
170
			active_ip=`echo $system |cut -d" " -f2`
132
			active_session=`echo $system |cut -d" " -f5`
171
			active_session=`echo $system |cut -d" " -f5`
133
			active_mac=`echo $system | cut -d" " -f1`
172
			active_mac=`echo $system | cut -d" " -f1`
134
			active_user=`echo $system |cut -d" " -f6`
173
			active_user=`echo $system |cut -d" " -f6`
135
# process only equipment with an authenticated user
174
			# process only equipment with an authenticated user
136
			if [[ $(expr $active_session) -eq 1 ]]
175
			if [[ $(expr $active_session) -eq 1 ]]
137
			then
176
			then
138
				arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c2 -w4 $active_ip|grep "Unicast reply"|wc -l`
177
				arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c2 -w4 $active_ip|grep "Unicast reply"|wc -l`
139
# store @IP of quiet equipments
-
 
140
				if [[ $(expr $arp_reply) -eq 0 ]]
-
 
141
   				then		
-
 
142
					echo "$active_ip $active_mac $active_user" >> $tmp_file
-
 
143
					
-
 
144
				fi
-
 
145
# disconnect users whose equipement is usurped (@MAC)
178
				# disconnect users whose equipement is usurped (@MAC). For example, if there are 2 same @MAC it will make 3 lines in output.
146
				if [[ $(expr $arp_reply) -gt 2 ]]
179
				if [[ $(expr $arp_reply) -gt 2 ]]
147
	       				then 
180
	       				then 
148
					echo "$(date "+[%x-%X] : ")alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)." >> /var/Save/security/watchdog.log
181
					echo "$(date "+[%x-%X] : ")alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)." >> /var/Save/security/watchdog.log
149
					logger "alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)."
182
					logger "alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)."
150
					/usr/sbin/chilli_query logout $active_mac
183
					/usr/sbin/chilli_query logout $active_mac