Subversion Repositories ALCASAR

Rev

Rev 3219 | 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 3227 2024-10-08 09:35:31Z rexy $
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)
2108 richard 7
# - Ce script prévient les usagers de l'indisponibilité de l'accès Internet
8
# - Il déconnecte les usagers dont les équipements réseau ne répondent plus (leur onglet 'status.php' a été fermé)
9
# - Il deconnecte les usagers dont les adresses MAC sont usurpées
10
#
11
# - This script tells users that Internet access is down
12
# - It logs out users whose PCs are quiet (their status tab is closed)
13
# - It logs out users whose MAC address is used by other systems (usurped)
672 richard 14
 
3219 rexy 15
export LC_ALL=C
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
2474 tom.houday 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}
2884 rexy 21
PRIVATE_IP=`echo $private_ip_mask |cut -d"/" -f1`
22
PRIVATE_IP=${PRIVATE_IP:=192.168.182.1}
3008 rexy 23
MULTIWAN=`grep ^MULTIWAN= $CONF_FILE|cut -d"=" -f2`
2841 rexy 24
current_users_file="/tmp/current_users.txt"		# file containing active users with their "status.php" tab open
316 richard 25
DIR_WEB="/var/www/html"
360 richard 26
Index_Page="$DIR_WEB/index.php"
1472 richard 27
IPTABLES="/sbin/iptables"
2250 tom.houday 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")
2537 tom.houday 37
		logger -t alcasar-watchdog "$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")
2539 tom.houday 42
		logger -t alcasar-watchdog "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
		;;
2864 rexy 46
	"3")
47
		logger -t alcasar-watchdog "can't resolv DNS queries"
48
		echo "can't resolv DNS queries"
49
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"can't resolv DNS queries\";?g" $Index_Page
50
		;;
308 richard 51
	esac
2250 tom.houday 52
	net_pb=`grep "network_pb = true;" $Index_Page|wc -l`
3008 rexy 53
	if [ $net_pb = "0" ] # if previously up
308 richard 54
		then
2250 tom.houday 55
		/bin/sed -i "s?^\$network_pb.*?\$network_pb = true;?g" $Index_Page
1472 richard 56
		$IPTABLES -I PREROUTING -t nat -i $TUNIF -p udp --dport domain -j REDIRECT --to-port 56
308 richard 57
	fi
58
}
59
 
783 richard 60
function lan_test ()
61
# LAN connectiivity testing
62
{
784 richard 63
	watchdog_process=`ps -C alcasar-watchdog.sh|wc -l`
64
	if [[ $(expr $watchdog_process) -gt 3 ]]
65
		then
66
		echo "ALCASAR watchdog is already running"
67
		exit 0
68
	fi
783 richard 69
	# EXTIF testing
70
	LAN_DOWN="0"
1472 richard 71
	if [ `/sbin/ip link | grep $EXTIF|grep "NO-CARRIER" | wc -l` -eq "1" ]
783 richard 72
		then
73
		LAN_DOWN="1"
308 richard 74
	fi
783 richard 75
	# Default GW testing
76
	if [ $LAN_DOWN -eq "0" ]
308 richard 77
		then
2830 rexy 78
		GW_EXIST=`/sbin/ip route list|grep ^default|wc -l`
79
		if [ $GW_EXIST -eq "0" ] # no GW defined !
2376 tom.houday 80
			then
2830 rexy 81
			systemctl restart network
3008 rexy 82
		else
83
			if [ "$MULTIWAN" == "off" ] || [ "$MULTIWAN" == "Off" ]
2830 rexy 84
				then
3227 rexy 85
				IP_GW_count=`/sbin/ip route list|grep ^default|wc -l`
86
				if [ $IP_GW_count -ne 1 ]
87
					then
88
					systemctl restart network
89
				fi
3008 rexy 90
				IP_GW=`/sbin/ip route list|grep ^default|cut -d" " -f3`
3214 rexy 91
				arp_reply=`/usr/sbin/arping -I$EXTIF -c1 $IP_GW|grep response|cut -d" " -f2`
3008 rexy 92
				if [ $arp_reply -eq "0" ]
93
					then
94
					LAN_DOWN="2"
95
				fi
2830 rexy 96
			fi
783 richard 97
		fi
308 richard 98
	fi
2871 rexy 99
	# DNS request testing (twice)
2864 rexy 100
	if [ $LAN_DOWN -eq "0" ]
101
		then
2871 rexy 102
			dns_reply=`/usr/bin/host -W1 www.free.fr|grep SERVFAIL|wc -l`
2864 rexy 103
			if [ $dns_reply -eq "1" ]
104
				then
2871 rexy 105
				dns_reply=`/usr/bin/host -W1 www.startpage.com|grep SERVFAIL|wc -l`
106
				if [ $dns_reply -eq "1" ]
107
					then LAN_DOWN="3"
108
				fi
2864 rexy 109
			fi
110
	fi
783 richard 111
	# if LAN pb detected, users are warned
112
	if [ $LAN_DOWN != "0" ]
113
		then
114
			lan_down_alert
115
	# else switch in normal mode
116
	else
987 richard 117
		echo "Internet access is OK for now"
2250 tom.houday 118
		net_pb=`grep "network_pb = true;" $Index_Page|wc -l`
3008 rexy 119
		if [ $net_pb != "0" ] # if already down
783 richard 120
			then
2250 tom.houday 121
			/bin/sed -i "s?^\$network_pb.*?\$network_pb = false;?g" $Index_Page
1472 richard 122
			$IPTABLES -D PREROUTING -t nat -i $TUNIF -p udp --dport domain -j REDIRECT --to-port 56
783 richard 123
		fi
124
	fi
125
}
126
 
2887 rexy 127
usage="Usage: alcasar-watchdog.sh {-lt --lan_test | --disconnect-permanent-users}"
783 richard 128
case $1 in
129
	-\? | -h* | --h*)
130
		echo "$usage"
131
		exit 0
2967 rexy 132
	;;
783 richard 133
	-lt | --lan_test)
134
		lan_test
135
		exit 0
2967 rexy 136
	;;
2887 rexy 137
	--disconnect-permanent-users)
2967 rexy 138
		/bin/sed -i '/PERM/d' $current_users_file
139
		exit 0
140
	;;
783 richard 141
	*)
142
		lan_test
2108 richard 143
		# We disconnect inactive users (its means that their 'status.php' tab has been closed --> their ip address isn't in $current_users_file)
2886 rexy 144
		# process each equipment known by chilli
2394 tom.houday 145
		for system in `/usr/sbin/chilli_query list | grep -v "0\.0\.0\.0"`
783 richard 146
		do
147
			active_ip=`echo $system |cut -d" " -f2`
148
			active_session=`echo $system |cut -d" " -f5`
149
			active_mac=`echo $system | cut -d" " -f1`
840 richard 150
			active_user=`echo $system |cut -d" " -f6`
2516 rexy 151
			# We check if the user isn't an auth @MAC and if he is still connected
2108 richard 152
			if [ "$active_user" != "$active_mac" ] && [ $(expr $active_session) -eq 1 ]; then
2107 raphael.pi 153
				if [ -e $current_users_file ]; then
154
					# We check if user @IP is in 'current_users.txt'
2394 tom.houday 155
					cmp_user_ok=$(cat $current_users_file | awk -F':' "\$1 == \"$active_ip\" {print \$2}")
2108 richard 156
					# If not we disconnect this user.
2394 tom.houday 157
					if [ -z "$cmp_user_ok" ]; then
2883 rexy 158
						logger -t alcasar-watchdog "$active_ip ($active_mac) doesn't contact ALCASAR any more. We disconnects the user ($active_user)."
2107 raphael.pi 159
						/usr/sbin/chilli_query logout $active_mac
2394 tom.houday 160
					elif [ "$cmp_user_ok" == "TEMP" ]; then
2883 rexy 161
						# Remove the user's IP from 'current_users.txt'. Every user status page need to insert their @IP everytime to prove their connectivity.
162
						# We don't disconnect when $cmp_user_ok == "PERM" (status page not needed)
2394 tom.houday 163
						sed -i "/^$active_ip:$cmp_user_ok\$/d" $current_users_file
2107 raphael.pi 164
					fi
2278 richard 165
				else # "current_user.txt" does not exists. We disconnect every users.
2841 rexy 166
					logger -t alcasar-watchdog "The file /tmp/current_users.txt doesn't' exist. We disconnects the user $active_user"
2107 raphael.pi 167
					/usr/sbin/chilli_query logout $active_mac
168
				fi
169
			fi
2108 richard 170
			# IP usurpation test : process only equipment with an authenticated user
783 richard 171
			if [[ $(expr $active_session) -eq 1 ]]
1157 stephane 172
			then
3214 rexy 173
				arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c1 -w4 $active_ip|grep -c "Unicast reply"`
2278 richard 174
				# disconnect users whose equipement is usurped. For example, if there are 2 same @MAC it will make 2 lines in output.
175
				if [[ $(expr $arp_reply) -gt 1 ]]
2376 tom.houday 176
					then 
2455 tom.houday 177
					echo "[$(date +"%Y-%m-%d %H:%M:%S")] : alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)." >> /var/Save/security/watchdog.log
2537 tom.houday 178
					logger -t alcasar-watchdog "$active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)."
783 richard 179
					/usr/sbin/chilli_query logout $active_mac
1500 richard 180
					chmod 644 /var/Save/security/watchdog.log
783 richard 181
				fi
182
			fi
183
		done
2967 rexy 184
	;;
2454 tom.houday 185
esac
597 richard 186
IFS=$OLDIFS