Subversion Repositories ALCASAR

Rev

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

Rev 2864 Rev 2871
1
#!/bin/bash
1
#!/bin/bash
2
# $Id: alcasar-watchdog.sh 2864 2020-10-18 09:06:17Z rexy $
2
# $Id: alcasar-watchdog.sh 2871 2020-10-28 18:17:44Z 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="192.168.182.1"
20
PRIVATE_IP="192.168.182.1"
21
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")
44
	"3")
45
		logger -t alcasar-watchdog "can't resolv DNS queries"
45
		logger -t alcasar-watchdog "can't resolv DNS queries"
46
		echo "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
47
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"can't resolv DNS queries\";?g" $Index_Page
48
		;;
48
		;;
49
	esac
49
	esac
50
	net_pb=`grep "network_pb = true;" $Index_Page|wc -l`
50
	net_pb=`grep "network_pb = true;" $Index_Page|wc -l`
51
	if [ $net_pb = "0" ] # user alert (only the first time)
51
	if [ $net_pb = "0" ] # user alert (only the first time)
52
		then
52
		then
53
		/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
54
		$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
55
	fi
55
	fi
56
}
56
}
57
 
57
 
58
function lan_test ()
58
function lan_test ()
59
# LAN connectiivity testing
59
# LAN connectiivity testing
60
{
60
{
61
	watchdog_process=`ps -C alcasar-watchdog.sh|wc -l`
61
	watchdog_process=`ps -C alcasar-watchdog.sh|wc -l`
62
	if [[ $(expr $watchdog_process) -gt 3 ]]
62
	if [[ $(expr $watchdog_process) -gt 3 ]]
63
		then
63
		then
64
		echo "ALCASAR watchdog is already running"
64
		echo "ALCASAR watchdog is already running"
65
		exit 0
65
		exit 0
66
	fi
66
	fi
67
	# EXTIF testing
67
	# EXTIF testing
68
	LAN_DOWN="0"
68
	LAN_DOWN="0"
69
	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" ]
70
		then
70
		then
71
		LAN_DOWN="1"
71
		LAN_DOWN="1"
72
	fi
72
	fi
73
	# Default GW testing
73
	# Default GW testing
74
	if [ $LAN_DOWN -eq "0" ]
74
	if [ $LAN_DOWN -eq "0" ]
75
		then
75
		then
76
		GW_EXIST=`/sbin/ip route list|grep ^default|wc -l`
76
		GW_EXIST=`/sbin/ip route list|grep ^default|wc -l`
77
		if [ $GW_EXIST -eq "0" ] # no GW defined !
77
		if [ $GW_EXIST -eq "0" ] # no GW defined !
78
			then
78
			then
79
			systemctl restart network
79
			systemctl restart network
80
		else	
80
		else	
81
			IP_GW=`/sbin/ip route list|grep ^default|cut -d" " -f3`
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`
82
			arp_reply=`/usr/sbin/arping -I$EXTIF -c1 $IP_GW|grep response|cut -d" " -f2`
83
			if [ $arp_reply -eq "0" ]
83
			if [ $arp_reply -eq "0" ]
84
				then
84
				then
85
				LAN_DOWN="2"
85
				LAN_DOWN="2"
86
			fi
86
			fi
87
		fi
87
		fi
88
	fi
88
	fi
89
	# DNS request testing
89
	# DNS request testing (twice)
90
	if [ $LAN_DOWN -eq "0" ]
90
	if [ $LAN_DOWN -eq "0" ]
91
		then
91
		then
92
			dns_reply=`/usr/bin/host -W1 free.fr|grep SERVFAIL|wc -l`
92
			dns_reply=`/usr/bin/host -W1 www.free.fr|grep SERVFAIL|wc -l`
93
			if [ $dns_reply -eq "1" ]
93
			if [ $dns_reply -eq "1" ]
94
				then
94
				then
-
 
95
				dns_reply=`/usr/bin/host -W1 www.startpage.com|grep SERVFAIL|wc -l`
-
 
96
				if [ $dns_reply -eq "1" ]
95
				LAN_DOWN="3"
97
					then LAN_DOWN="3"
-
 
98
				fi
96
			fi
99
			fi
97
	fi
100
	fi
98
	# if LAN pb detected, users are warned
101
	# if LAN pb detected, users are warned
99
	if [ $LAN_DOWN != "0" ]
102
	if [ $LAN_DOWN != "0" ]
100
		then
103
		then
101
			lan_down_alert
104
			lan_down_alert
102
	# else switch in normal mode
105
	# else switch in normal mode
103
	else
106
	else
104
		echo "Internet access is OK for now"
107
		echo "Internet access is OK for now"
105
		net_pb=`grep "network_pb = true;" $Index_Page|wc -l`
108
		net_pb=`grep "network_pb = true;" $Index_Page|wc -l`
106
		if [ $net_pb != "0" ]
109
		if [ $net_pb != "0" ]
107
			then
110
			then
108
			/bin/sed -i "s?^\$network_pb.*?\$network_pb = false;?g" $Index_Page
111
			/bin/sed -i "s?^\$network_pb.*?\$network_pb = false;?g" $Index_Page
109
			$IPTABLES -D PREROUTING -t nat -i $TUNIF -p udp --dport domain -j REDIRECT --to-port 56
112
			$IPTABLES -D PREROUTING -t nat -i $TUNIF -p udp --dport domain -j REDIRECT --to-port 56
110
		fi
113
		fi
111
	fi
114
	fi
112
}
115
}
113
 
116
 
114
usage="Usage: alcasar-watchdog.sh {-lt --lan_test}"
117
usage="Usage: alcasar-watchdog.sh {-lt --lan_test}"
115
case $1 in
118
case $1 in
116
	-\? | -h* | --h*)
119
	-\? | -h* | --h*)
117
		echo "$usage"
120
		echo "$usage"
118
		exit 0
121
		exit 0
119
		;;
122
		;;
120
	-lt | --lan_test)
123
	-lt | --lan_test)
121
		lan_test
124
		lan_test
122
		exit 0
125
		exit 0
123
		;;
126
		;;
124
	*)
127
	*)
125
		lan_test
128
		lan_test
126
		# We disconnect inactive users (its means that their 'status.php' tab has been closed --> their ip address isn't in $current_users_file)
129
		# 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)
130
		# process each equipment known by chilli to check if IP address is usurped (with arping)
128
		for system in `/usr/sbin/chilli_query list | grep -v "0\.0\.0\.0"`
131
		for system in `/usr/sbin/chilli_query list | grep -v "0\.0\.0\.0"`
129
		do
132
		do
130
			active_ip=`echo $system |cut -d" " -f2`
133
			active_ip=`echo $system |cut -d" " -f2`
131
			active_session=`echo $system |cut -d" " -f5`
134
			active_session=`echo $system |cut -d" " -f5`
132
			active_mac=`echo $system | cut -d" " -f1`
135
			active_mac=`echo $system | cut -d" " -f1`
133
			active_user=`echo $system |cut -d" " -f6`
136
			active_user=`echo $system |cut -d" " -f6`
134
			# We disconnect inactive user here :
137
			# We disconnect inactive user here :
135
			# We check if the user isn't an auth @MAC and if he is still connected
138
			# We check if the user isn't an auth @MAC and if he is still connected
136
			if [ "$active_user" != "$active_mac" ] && [ $(expr $active_session) -eq 1 ]; then
139
			if [ "$active_user" != "$active_mac" ] && [ $(expr $active_session) -eq 1 ]; then
137
				if [ -e $current_users_file ]; then
140
				if [ -e $current_users_file ]; then
138
					# We check if user @IP is in 'current_users.txt'
141
					# We check if user @IP is in 'current_users.txt'
139
					cmp_user_ok=$(cat $current_users_file | awk -F':' "\$1 == \"$active_ip\" {print \$2}")
142
					cmp_user_ok=$(cat $current_users_file | awk -F':' "\$1 == \"$active_ip\" {print \$2}")
140
					# If not we disconnect this user.
143
					# If not we disconnect this user.
141
					if [ -z "$cmp_user_ok" ]; then
144
					if [ -z "$cmp_user_ok" ]; then
142
						logger -t alcasar-watchdog "$active_ip ($active_mac) can't be contact. We disconnects the user ($active_user)."
145
						logger -t alcasar-watchdog "$active_ip ($active_mac) can't be contact. We disconnects the user ($active_user)."
143
						/usr/sbin/chilli_query logout $active_mac
146
						/usr/sbin/chilli_query logout $active_mac
144
					elif [ "$cmp_user_ok" == "TEMP" ]; then
147
					elif [ "$cmp_user_ok" == "TEMP" ]; then
145
						# Remove the user's IP from 'current_users.txt'. Every user need to insert their @IP everytime to prove their connectivity.
148
						# Remove the user's IP from 'current_users.txt'. Every user need to insert their @IP everytime to prove their connectivity.
146
						sed -i "/^$active_ip:$cmp_user_ok\$/d" $current_users_file
149
						sed -i "/^$active_ip:$cmp_user_ok\$/d" $current_users_file
147
					fi
150
					fi
148
				else # "current_user.txt" does not exists. We disconnect every users.
151
				else # "current_user.txt" does not exists. We disconnect every users.
149
					logger -t alcasar-watchdog "The file /tmp/current_users.txt doesn't' exist. We disconnects the user $active_user"
152
					logger -t alcasar-watchdog "The file /tmp/current_users.txt doesn't' exist. We disconnects the user $active_user"
150
					/usr/sbin/chilli_query logout $active_mac
153
					/usr/sbin/chilli_query logout $active_mac
151
				fi
154
				fi
152
			fi
155
			fi
153
			# IP usurpation test : process only equipment with an authenticated user
156
			# IP usurpation test : process only equipment with an authenticated user
154
			if [[ $(expr $active_session) -eq 1 ]]
157
			if [[ $(expr $active_session) -eq 1 ]]
155
			then
158
			then
156
				arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c1 -w4 $active_ip|grep -c "Unicast reply"`
159
				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.
160
				# 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 ]]
161
				if [[ $(expr $arp_reply) -gt 1 ]]
159
					then 
162
					then 
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
163
					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
161
					logger -t alcasar-watchdog "$active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)."
164
					logger -t alcasar-watchdog "$active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)."
162
					/usr/sbin/chilli_query logout $active_mac
165
					/usr/sbin/chilli_query logout $active_mac
163
					chmod 644 /var/Save/security/watchdog.log
166
					chmod 644 /var/Save/security/watchdog.log
164
				fi
167
				fi
165
			fi
168
			fi
166
		done
169
		done
167
		;;
170
		;;
168
esac
171
esac
169
IFS=$OLDIFS
172
IFS=$OLDIFS
170
 
173