Subversion Repositories ALCASAR

Rev

Rev 2841 | Rev 2871 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

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