Subversion Repositories ALCASAR

Rev

Rev 1500 | Rev 1688 | 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 1535 2014-12-21 22:14:47Z richard $
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
376 franck 9
# - les équipements réseau ne répondent plus
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 
1 root 13
# - PCs are quiet
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"
316 richard 24
DIR_WEB="/var/www/html"
360 richard 25
Index_Page="$DIR_WEB/index.php"
1472 richard 26
IPTABLES="/sbin/iptables"
27
TUNIF="tun0"						# listen device for chilli daemon
597 richard 28
OLDIFS=$IFS
1 root 29
IFS=$'\n'
308 richard 30
 
783 richard 31
function lan_down_alert ()
1157 stephane 32
# users are redirected on ALCASAR IP address if a LAN problem is detected
308 richard 33
{
783 richard 34
	case $LAN_DOWN in
308 richard 35
	"1")
1469 richard 36
		logger "$EXTIF (WAN card) link down"
1474 richard 37
		echo "$EXTIF (WAN card) link down"
1469 richard 38
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"$EXTIF (WAN card) link down\";?g" $Index_Page
308 richard 39
		;;
40
	"2")
41
		logger "can't contact the default router"
987 richard 42
		echo "can't contact the default router"
363 richard 43
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"can't contact the default router\";?g" $Index_Page
308 richard 44
		;;
45
	esac
1472 richard 46
	net_pb=`grep "network_pb = True;" $Index_Page|wc -l`
47
	if [ $net_pb = "0" ] # user alert (only the first time)	
308 richard 48
		then
360 richard 49
		/bin/sed -i "s?^\$network_pb.*?\$network_pb = True;?g" $Index_Page
1472 richard 50
		$IPTABLES -I PREROUTING -t nat -i $TUNIF -p udp --dport domain -j REDIRECT --to-port 56
308 richard 51
	fi
52
}
53
 
783 richard 54
function lan_test ()
55
# LAN connectiivity testing
56
{
784 richard 57
	watchdog_process=`ps -C alcasar-watchdog.sh|wc -l`
58
	if [[ $(expr $watchdog_process) -gt 3 ]]
59
		then
60
		echo "ALCASAR watchdog is already running"
61
		exit 0
62
	fi
783 richard 63
	# EXTIF testing
64
	LAN_DOWN="0"
1472 richard 65
	if [ `/sbin/ip link | grep $EXTIF|grep "NO-CARRIER" | wc -l` -eq "1" ]
783 richard 66
		then
67
		LAN_DOWN="1"
308 richard 68
	fi
783 richard 69
	# Default GW testing
70
	if [ $LAN_DOWN -eq "0" ]
308 richard 71
		then
783 richard 72
		IP_GW=`/sbin/ip route list|grep ^default|cut -d" " -f3`
73
		arp_reply=`/usr/sbin/arping -I$EXTIF -c1 $IP_GW|grep response|cut -d" " -f2`
74
		if [ $arp_reply -eq "0" ]
75
	       		then
76
			LAN_DOWN="2"
77
		fi
308 richard 78
	fi
783 richard 79
	# if LAN pb detected, users are warned
80
	if [ $LAN_DOWN != "0" ]
81
		then
82
			lan_down_alert
83
	# else switch in normal mode
84
	else
987 richard 85
		echo "Internet access is OK for now"
1472 richard 86
		net_pb=`grep "network_pb = True;" $Index_Page|wc -l`
783 richard 87
		if [ $net_pb != "0" ]
88
			then
89
			/bin/sed -i "s?^\$network_pb.*?\$network_pb = False;?g" $Index_Page
1472 richard 90
			$IPTABLES -D PREROUTING -t nat -i $TUNIF -p udp --dport domain -j REDIRECT --to-port 56
783 richard 91
		fi
92
	fi
93
}
94
 
95
usage="Usage: alcasar-watchdog.sh {-lt --lan_test}"
96
case $1 in
97
	-\? | -h* | --h*)
98
		echo "$usage"
99
		exit 0
100
		;;
101
	-lt | --lan_test)
102
		lan_test
103
		exit 0
104
		;;
105
	*)
106
		lan_test
840 richard 107
		# read file that contains IP address of quiet equipments
783 richard 108
		if [ -e $tmp_file ]; then
109
			cat $tmp_file | while read noresponse
110
			do
111
				noresponse_ip=`echo $noresponse | cut -d" " -f1`
112
				noresponse_mac=`echo $noresponse | cut -d" " -f2`
840 richard 113
				noresponse_user=`echo $noresponse | cut -d" " -f3`
787 richard 114
				arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c1 -w4 $noresponse_ip|grep "Unicast reply"|wc -l`
783 richard 115
				if [[ $(expr $arp_reply) -eq 0 ]]
116
	       				then
845 richard 117
					logger "alcasar-watchdog $noresponse_ip ($noresponse_mac) can't be contact. Alcasar disconnects the user ($noresponse_user)."
840 richard 118
					/usr/sbin/chilli_query logout $noresponse_mac
119
					/usr/sbin/chilli_query dhcp-release $noresponse_mac  # release dhcp for mac_auth equipment 
783 richard 120
				fi
121
			done
122
			rm $tmp_file
1 root 123
		fi
1157 stephane 124
# process each equipment known by chilli
783 richard 125
		for system in `/usr/sbin/chilli_query list |grep -v "\.0\.0\.0"`
126
		do
127
			active_ip=`echo $system |cut -d" " -f2`
128
			active_session=`echo $system |cut -d" " -f5`
129
			active_mac=`echo $system | cut -d" " -f1`
840 richard 130
			active_user=`echo $system |cut -d" " -f6`
1157 stephane 131
# process only equipment with an authenticated user
783 richard 132
			if [[ $(expr $active_session) -eq 1 ]]
1157 stephane 133
			then
783 richard 134
				arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c2 -w4 $active_ip|grep "Unicast reply"|wc -l`
1157 stephane 135
# store @IP of quiet equipments
783 richard 136
				if [[ $(expr $arp_reply) -eq 0 ]]
137
	       				then
1157 stephane 138
					PTN='^[[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]]$'
139
					if [[ $(expr $active_user : $PTN) -eq 0 ]] # don't process @mac auth equipments
140
					then
141
						echo "$active_ip $active_mac $active_user" >> $tmp_file
142
					fi
783 richard 143
				fi
1157 stephane 144
# disconnect users whose equipement is usurped (@MAC)
783 richard 145
				if [[ $(expr $arp_reply) -gt 2 ]]
1202 crox53 146
	       				then 
1500 richard 147
					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 148
					logger "alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)."
783 richard 149
					/usr/sbin/chilli_query logout $active_mac
1500 richard 150
					chmod 644 /var/Save/security/watchdog.log
783 richard 151
				fi
152
			fi
153
		done
154
		;;
155
esac	
597 richard 156
IFS=$OLDIFS