Subversion Repositories ALCASAR

Rev

Rev 2841 | Rev 2871 | 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 2864 2020-10-18 09:06:17Z 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
 
1469 richard 15
CONF_FILE="/usr/local/etc/alcasar.conf"
16
EXTIF=`grep ^EXTIF= $CONF_FILE|cut -d"=" -f2`			# EXTernal InterFace
17
INTIF=`grep ^INTIF= $CONF_FILE|cut -d"=" -f2`			# INTernal InterFace
2474 tom.houday 18
private_ip_mask=`grep ^PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
786 richard 19
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
2864 rexy 20
PRIVATE_IP="192.168.182.1"
21
PRIVATE_IP="192.168.182.1"
2841 rexy 22
current_users_file="/tmp/current_users.txt"		# file containing active users with their "status.php" tab open
316 richard 23
DIR_WEB="/var/www/html"
360 richard 24
Index_Page="$DIR_WEB/index.php"
1472 richard 25
IPTABLES="/sbin/iptables"
2250 tom.houday 26
TUNIF="tun0"							# listen device for chilli daemon
597 richard 27
OLDIFS=$IFS
1 root 28
IFS=$'\n'
308 richard 29
 
783 richard 30
function lan_down_alert ()
1157 stephane 31
# users are redirected on ALCASAR IP address if a LAN problem is detected
308 richard 32
{
783 richard 33
	case $LAN_DOWN in
308 richard 34
	"1")
2537 tom.houday 35
		logger -t alcasar-watchdog "$EXTIF (WAN card) link down"
1474 richard 36
		echo "$EXTIF (WAN card) link down"
1469 richard 37
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"$EXTIF (WAN card) link down\";?g" $Index_Page
308 richard 38
		;;
39
	"2")
2539 tom.houday 40
		logger -t alcasar-watchdog "can't contact the default router"
987 richard 41
		echo "can't contact the default router"
363 richard 42
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"can't contact the default router\";?g" $Index_Page
308 richard 43
		;;
2864 rexy 44
	"3")
45
		logger -t alcasar-watchdog "can't resolv DNS queries"
46
		echo "can't resolv DNS queries"
47
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"can't resolv DNS queries\";?g" $Index_Page
48
		;;
308 richard 49
	esac
2250 tom.houday 50
	net_pb=`grep "network_pb = true;" $Index_Page|wc -l`
2454 tom.houday 51
	if [ $net_pb = "0" ] # user alert (only the first time)
308 richard 52
		then
2250 tom.houday 53
		/bin/sed -i "s?^\$network_pb.*?\$network_pb = true;?g" $Index_Page
1472 richard 54
		$IPTABLES -I PREROUTING -t nat -i $TUNIF -p udp --dport domain -j REDIRECT --to-port 56
308 richard 55
	fi
56
}
57
 
783 richard 58
function lan_test ()
59
# LAN connectiivity testing
60
{
784 richard 61
	watchdog_process=`ps -C alcasar-watchdog.sh|wc -l`
62
	if [[ $(expr $watchdog_process) -gt 3 ]]
63
		then
64
		echo "ALCASAR watchdog is already running"
65
		exit 0
66
	fi
783 richard 67
	# EXTIF testing
68
	LAN_DOWN="0"
1472 richard 69
	if [ `/sbin/ip link | grep $EXTIF|grep "NO-CARRIER" | wc -l` -eq "1" ]
783 richard 70
		then
71
		LAN_DOWN="1"
308 richard 72
	fi
783 richard 73
	# Default GW testing
74
	if [ $LAN_DOWN -eq "0" ]
308 richard 75
		then
2830 rexy 76
		GW_EXIST=`/sbin/ip route list|grep ^default|wc -l`
77
		if [ $GW_EXIST -eq "0" ] # no GW defined !
2376 tom.houday 78
			then
2830 rexy 79
			systemctl restart network
80
		else	
81
			IP_GW=`/sbin/ip route list|grep ^default|cut -d" " -f3`
82
			arp_reply=`/usr/sbin/arping -I$EXTIF -c1 $IP_GW|grep response|cut -d" " -f2`
83
			if [ $arp_reply -eq "0" ]
84
				then
85
				LAN_DOWN="2"
86
			fi
783 richard 87
		fi
308 richard 88
	fi
2864 rexy 89
	# DNS request testing
90
	if [ $LAN_DOWN -eq "0" ]
91
		then
92
			dns_reply=`/usr/bin/host -W1 free.fr|grep SERVFAIL|wc -l`
93
			if [ $dns_reply -eq "1" ]
94
				then
95
				LAN_DOWN="3"
96
			fi
97
	fi
783 richard 98
	# if LAN pb detected, users are warned
99
	if [ $LAN_DOWN != "0" ]
100
		then
101
			lan_down_alert
102
	# else switch in normal mode
103
	else
987 richard 104
		echo "Internet access is OK for now"
2250 tom.houday 105
		net_pb=`grep "network_pb = true;" $Index_Page|wc -l`
783 richard 106
		if [ $net_pb != "0" ]
107
			then
2250 tom.houday 108
			/bin/sed -i "s?^\$network_pb.*?\$network_pb = false;?g" $Index_Page
1472 richard 109
			$IPTABLES -D PREROUTING -t nat -i $TUNIF -p udp --dport domain -j REDIRECT --to-port 56
783 richard 110
		fi
111
	fi
112
}
113
 
114
usage="Usage: alcasar-watchdog.sh {-lt --lan_test}"
115
case $1 in
116
	-\? | -h* | --h*)
117
		echo "$usage"
118
		exit 0
119
		;;
120
	-lt | --lan_test)
121
		lan_test
122
		exit 0
123
		;;
124
	*)
125
		lan_test
2108 richard 126
		# We disconnect inactive users (its means that their 'status.php' tab has been closed --> their ip address isn't in $current_users_file)
127
		# process each equipment known by chilli to check if IP address is usurped (with arping)
2394 tom.houday 128
		for system in `/usr/sbin/chilli_query list | grep -v "0\.0\.0\.0"`
783 richard 129
		do
130
			active_ip=`echo $system |cut -d" " -f2`
131
			active_session=`echo $system |cut -d" " -f5`
132
			active_mac=`echo $system | cut -d" " -f1`
840 richard 133
			active_user=`echo $system |cut -d" " -f6`
2394 tom.houday 134
			# We disconnect inactive user here :
2516 rexy 135
			# We check if the user isn't an auth @MAC and if he is still connected
2108 richard 136
			if [ "$active_user" != "$active_mac" ] && [ $(expr $active_session) -eq 1 ]; then
2107 raphael.pi 137
				if [ -e $current_users_file ]; then
138
					# We check if user @IP is in 'current_users.txt'
2394 tom.houday 139
					cmp_user_ok=$(cat $current_users_file | awk -F':' "\$1 == \"$active_ip\" {print \$2}")
2108 richard 140
					# If not we disconnect this user.
2394 tom.houday 141
					if [ -z "$cmp_user_ok" ]; then
2537 tom.houday 142
						logger -t alcasar-watchdog "$active_ip ($active_mac) can't be contact. We disconnects the user ($active_user)."
2107 raphael.pi 143
						/usr/sbin/chilli_query logout $active_mac
2394 tom.houday 144
					elif [ "$cmp_user_ok" == "TEMP" ]; then
2376 tom.houday 145
						# Remove the user's IP from 'current_users.txt'. Every user need to insert their @IP everytime to prove their connectivity.
2394 tom.houday 146
						sed -i "/^$active_ip:$cmp_user_ok\$/d" $current_users_file
2107 raphael.pi 147
					fi
2278 richard 148
				else # "current_user.txt" does not exists. We disconnect every users.
2841 rexy 149
					logger -t alcasar-watchdog "The file /tmp/current_users.txt doesn't' exist. We disconnects the user $active_user"
2107 raphael.pi 150
					/usr/sbin/chilli_query logout $active_mac
151
				fi
152
			fi
2108 richard 153
			# IP usurpation test : process only equipment with an authenticated user
783 richard 154
			if [[ $(expr $active_session) -eq 1 ]]
1157 stephane 155
			then
2278 richard 156
				arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c1 -w4 $active_ip|grep -c "Unicast reply"`
157
				# disconnect users whose equipement is usurped. For example, if there are 2 same @MAC it will make 2 lines in output.
158
				if [[ $(expr $arp_reply) -gt 1 ]]
2376 tom.houday 159
					then 
2455 tom.houday 160
					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 161
					logger -t alcasar-watchdog "$active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)."
783 richard 162
					/usr/sbin/chilli_query logout $active_mac
1500 richard 163
					chmod 644 /var/Save/security/watchdog.log
783 richard 164
				fi
165
			fi
166
		done
167
		;;
2454 tom.houday 168
esac
597 richard 169
IFS=$OLDIFS