1 |
#!/bin/bash
|
1 |
#!/bin/bash
|
2 |
# $Id: alcasar-watchdog.sh 2113 2017-01-08 22:43:26Z richard $
|
2 |
# $Id: alcasar-watchdog.sh 2250 2017-05-22 22:00:03Z tom.houdayer $
|
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=`echo "$private_ip_mask" |cut -d"/" -f1` # @ip du portail (côté LAN)
|
21 |
PRIVATE_IP=${PRIVATE_IP:=192.168.182.1}
|
21 |
PRIVATE_IP=${PRIVATE_IP:=192.168.182.1}
|
22 |
current_users_file="/var/tmp/havp/current_users.txt" # file containing active users with their "status.php" tab open
|
22 |
current_users_file="/var/tmp/havp/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 "$EXTIF (WAN card) link down"
|
35 |
logger "$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 "can't contact the default router"
|
40 |
logger "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 |
esac
|
44 |
esac
|
45 |
net_pb=`grep "network_pb = True;" $Index_Page|wc -l`
|
45 |
net_pb=`grep "network_pb = true;" $Index_Page|wc -l`
|
46 |
if [ $net_pb = "0" ] # user alert (only the first time)
|
46 |
if [ $net_pb = "0" ] # user alert (only the first time)
|
47 |
then
|
47 |
then
|
48 |
/bin/sed -i "s?^\$network_pb.*?\$network_pb = True;?g" $Index_Page
|
48 |
/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
|
49 |
$IPTABLES -I PREROUTING -t nat -i $TUNIF -p udp --dport domain -j REDIRECT --to-port 56
|
50 |
fi
|
50 |
fi
|
51 |
}
|
51 |
}
|
52 |
|
52 |
|
53 |
function lan_test ()
|
53 |
function lan_test ()
|
54 |
# LAN connectiivity testing
|
54 |
# LAN connectiivity testing
|
55 |
{
|
55 |
{
|
56 |
watchdog_process=`ps -C alcasar-watchdog.sh|wc -l`
|
56 |
watchdog_process=`ps -C alcasar-watchdog.sh|wc -l`
|
57 |
if [[ $(expr $watchdog_process) -gt 3 ]]
|
57 |
if [[ $(expr $watchdog_process) -gt 3 ]]
|
58 |
then
|
58 |
then
|
59 |
echo "ALCASAR watchdog is already running"
|
59 |
echo "ALCASAR watchdog is already running"
|
60 |
exit 0
|
60 |
exit 0
|
61 |
fi
|
61 |
fi
|
62 |
# EXTIF testing
|
62 |
# EXTIF testing
|
63 |
LAN_DOWN="0"
|
63 |
LAN_DOWN="0"
|
64 |
if [ `/sbin/ip link | grep $EXTIF|grep "NO-CARRIER" | wc -l` -eq "1" ]
|
64 |
if [ `/sbin/ip link | grep $EXTIF|grep "NO-CARRIER" | wc -l` -eq "1" ]
|
65 |
then
|
65 |
then
|
66 |
LAN_DOWN="1"
|
66 |
LAN_DOWN="1"
|
67 |
fi
|
67 |
fi
|
68 |
# Default GW testing
|
68 |
# Default GW testing
|
69 |
if [ $LAN_DOWN -eq "0" ]
|
69 |
if [ $LAN_DOWN -eq "0" ]
|
70 |
then
|
70 |
then
|
71 |
IP_GW=`/sbin/ip route list|grep ^default|cut -d" " -f3`
|
71 |
IP_GW=`/sbin/ip route list|grep ^default|cut -d" " -f3`
|
72 |
arp_reply=`/usr/sbin/arping -I$EXTIF -c1 $IP_GW|grep response|cut -d" " -f2`
|
72 |
arp_reply=`/usr/sbin/arping -I$EXTIF -c1 $IP_GW|grep response|cut -d" " -f2`
|
73 |
if [ $arp_reply -eq "0" ]
|
73 |
if [ $arp_reply -eq "0" ]
|
74 |
then
|
74 |
then
|
75 |
LAN_DOWN="2"
|
75 |
LAN_DOWN="2"
|
76 |
fi
|
76 |
fi
|
77 |
fi
|
77 |
fi
|
78 |
# if LAN pb detected, users are warned
|
78 |
# if LAN pb detected, users are warned
|
79 |
if [ $LAN_DOWN != "0" ]
|
79 |
if [ $LAN_DOWN != "0" ]
|
80 |
then
|
80 |
then
|
81 |
lan_down_alert
|
81 |
lan_down_alert
|
82 |
# else switch in normal mode
|
82 |
# else switch in normal mode
|
83 |
else
|
83 |
else
|
84 |
echo "Internet access is OK for now"
|
84 |
echo "Internet access is OK for now"
|
85 |
net_pb=`grep "network_pb = True;" $Index_Page|wc -l`
|
85 |
net_pb=`grep "network_pb = true;" $Index_Page|wc -l`
|
86 |
if [ $net_pb != "0" ]
|
86 |
if [ $net_pb != "0" ]
|
87 |
then
|
87 |
then
|
88 |
/bin/sed -i "s?^\$network_pb.*?\$network_pb = False;?g" $Index_Page
|
88 |
/bin/sed -i "s?^\$network_pb.*?\$network_pb = false;?g" $Index_Page
|
89 |
$IPTABLES -D PREROUTING -t nat -i $TUNIF -p udp --dport domain -j REDIRECT --to-port 56
|
89 |
$IPTABLES -D PREROUTING -t nat -i $TUNIF -p udp --dport domain -j REDIRECT --to-port 56
|
90 |
fi
|
90 |
fi
|
91 |
fi
|
91 |
fi
|
92 |
}
|
92 |
}
|
93 |
|
93 |
|
94 |
usage="Usage: alcasar-watchdog.sh {-lt --lan_test}"
|
94 |
usage="Usage: alcasar-watchdog.sh {-lt --lan_test}"
|
95 |
case $1 in
|
95 |
case $1 in
|
96 |
-\? | -h* | --h*)
|
96 |
-\? | -h* | --h*)
|
97 |
echo "$usage"
|
97 |
echo "$usage"
|
98 |
exit 0
|
98 |
exit 0
|
99 |
;;
|
99 |
;;
|
100 |
-lt | --lan_test)
|
100 |
-lt | --lan_test)
|
101 |
lan_test
|
101 |
lan_test
|
102 |
exit 0
|
102 |
exit 0
|
103 |
;;
|
103 |
;;
|
104 |
*)
|
104 |
*)
|
105 |
lan_test
|
105 |
lan_test
|
106 |
# We disconnect inactive users (its means that their 'status.php' tab has been closed --> their ip address isn't in $current_users_file)
|
106 |
# We disconnect inactive users (its means that their 'status.php' tab has been closed --> their ip address isn't in $current_users_file)
|
107 |
# process each equipment known by chilli to check if IP address is usurped (with arping)
|
107 |
# process each equipment known by chilli to check if IP address is usurped (with arping)
|
108 |
for system in `/usr/sbin/chilli_query list |grep -v "\.0\.0\.0"`
|
108 |
for system in `/usr/sbin/chilli_query list |grep -v "\.0\.0\.0"`
|
109 |
do
|
109 |
do
|
110 |
active_ip=`echo $system |cut -d" " -f2`
|
110 |
active_ip=`echo $system |cut -d" " -f2`
|
111 |
active_session=`echo $system |cut -d" " -f5`
|
111 |
active_session=`echo $system |cut -d" " -f5`
|
112 |
active_mac=`echo $system | cut -d" " -f1`
|
112 |
active_mac=`echo $system | cut -d" " -f1`
|
113 |
active_user=`echo $system |cut -d" " -f6`
|
113 |
active_user=`echo $system |cut -d" " -f6`
|
114 |
#We disconnect inactive user here :
|
114 |
#We disconnect inactive user here :
|
115 |
#We check if this is not an auth @MAC and if he is still connected
|
115 |
#We check if this is not an auth @MAC and if he is still connected
|
116 |
if [ "$active_user" != "$active_mac" ] && [ $(expr $active_session) -eq 1 ]; then
|
116 |
if [ "$active_user" != "$active_mac" ] && [ $(expr $active_session) -eq 1 ]; then
|
117 |
if [ -e $current_users_file ]; then
|
117 |
if [ -e $current_users_file ]; then
|
118 |
# We check if user @IP is in 'current_users.txt'
|
118 |
# We check if user @IP is in 'current_users.txt'
|
119 |
cmp_user_ok=$(cat $current_users_file | grep $active_ip | wc -w)
|
119 |
cmp_user_ok=$(cat $current_users_file | grep $active_ip | wc -w)
|
120 |
# If not we disconnect this user.
|
120 |
# If not we disconnect this user.
|
121 |
if [ $cmp_user_ok -eq 0 ]; then
|
121 |
if [ $cmp_user_ok -eq 0 ]; then
|
122 |
logger "alcasar-watchdog : $active_ip ($active_mac) can't be contact. We disconnects the user ($active_user)."
|
122 |
logger "alcasar-watchdog : $active_ip ($active_mac) can't be contact. We disconnects the user ($active_user)."
|
123 |
/usr/sbin/chilli_query logout $active_mac
|
123 |
/usr/sbin/chilli_query logout $active_mac
|
124 |
fi
|
124 |
fi
|
125 |
# Remove the user's IP from 'current_users.txt'. Every user need to insert their @IP everytime to prove their connectivity.
|
125 |
# Remove the user's IP from 'current_users.txt'. Every user need to insert their @IP everytime to prove their connectivity.
|
126 |
sed -i "/^$active_ip/d" $current_users_file
|
126 |
sed -i "/^$active_ip/d" $current_users_file
|
127 |
else # "/tmp/current_user.txt" does not exists. We disconnect every users.
|
127 |
else # "/tmp/current_user.txt" does not exists. We disconnect every users.
|
128 |
logger "alcasar-watchdog : The file /var/tmp/havp/current_users.txt doen't' exist. We disconnects the user $active_user"
|
128 |
logger "alcasar-watchdog : The file /var/tmp/havp/current_users.txt doen't' exist. We disconnects the user $active_user"
|
129 |
/usr/sbin/chilli_query logout $active_mac
|
129 |
/usr/sbin/chilli_query logout $active_mac
|
130 |
fi
|
130 |
fi
|
131 |
fi
|
131 |
fi
|
132 |
# IP usurpation test : process only equipment with an authenticated user
|
132 |
# IP usurpation test : process only equipment with an authenticated user
|
133 |
if [[ $(expr $active_session) -eq 1 ]]
|
133 |
if [[ $(expr $active_session) -eq 1 ]]
|
134 |
then
|
134 |
then
|
135 |
arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c2 -w4 $active_ip|grep "Unicast reply"|wc -l`
|
135 |
arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c2 -w4 $active_ip|grep "Unicast reply"|wc -l`
|
136 |
# disconnect users whose equipement is usurped. For example, if there are 2 same @MAC it will make 3 lines in output.
|
136 |
# disconnect users whose equipement is usurped. For example, if there are 2 same @MAC it will make 3 lines in output.
|
137 |
if [[ $(expr $arp_reply) -gt 2 ]]
|
137 |
if [[ $(expr $arp_reply) -gt 2 ]]
|
138 |
then
|
138 |
then
|
139 |
echo "$(date "+[%x-%X] : ")alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)." >> /var/Save/security/watchdog.log
|
139 |
echo "$(date "+[%x-%X] : ")alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)." >> /var/Save/security/watchdog.log
|
140 |
logger "alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)."
|
140 |
logger "alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)."
|
141 |
/usr/sbin/chilli_query logout $active_mac
|
141 |
/usr/sbin/chilli_query logout $active_mac
|
142 |
chmod 644 /var/Save/security/watchdog.log
|
142 |
chmod 644 /var/Save/security/watchdog.log
|
143 |
fi
|
143 |
fi
|
144 |
fi
|
144 |
fi
|
145 |
done
|
145 |
done
|
146 |
;;
|
146 |
;;
|
147 |
esac
|
147 |
esac
|
148 |
IFS=$OLDIFS
|
148 |
IFS=$OLDIFS
|
149 |
|
149 |
|