| 672 | 
           richard | 
           1 | 
           #!/bin/bash
  | 
        
        
           | 64 | 
           franck | 
           2 | 
           # $Id: alcasar-watchdog.sh 1377 2014-06-10 22:16:50Z 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 | 
              | 
        
        
           | 1377 | 
           richard | 
           16 | 
           EXTIF="enp1s0"
  | 
        
        
            | 
            | 
           17 | 
           INTIF="enp2s0"
  | 
        
        
           | 786 | 
           richard | 
           18 | 
           conf_file="/usr/local/etc/alcasar.conf"
  | 
        
        
            | 
            | 
           19 | 
           private_ip_mask=`grep PRIVATE_IP= $conf_file|cut -d"=" -f2`
  | 
        
        
            | 
            | 
           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"
  | 
        
        
           | 597 | 
           richard | 
           26 | 
           OLDIFS=$IFS
  | 
        
        
           | 1 | 
           root | 
           27 | 
           IFS=$'\n'
  | 
        
        
           | 308 | 
           richard | 
           28 | 
              | 
        
        
           | 783 | 
           richard | 
           29 | 
           function lan_down_alert ()
  | 
        
        
           | 1157 | 
           stephane | 
           30 | 
           # users are redirected on ALCASAR IP address if a LAN problem is detected
  | 
        
        
           | 308 | 
           richard | 
           31 | 
           {
  | 
        
        
           | 783 | 
           richard | 
           32 | 
           	case $LAN_DOWN in
  | 
        
        
           | 308 | 
           richard | 
           33 | 
           	"1")
  | 
        
        
            | 
            | 
           34 | 
           		logger "eth0 link down"
  | 
        
        
           | 987 | 
           richard | 
           35 | 
           		echo "eth0 is down"
  | 
        
        
           | 363 | 
           richard | 
           36 | 
           		/bin/sed -i "s?diagnostic =.*?diagnostic = \"eth0 link down\";?g" $Index_Page
  | 
        
        
           | 308 | 
           richard | 
           37 | 
           		;;
  | 
        
        
            | 
            | 
           38 | 
           	"2")
  | 
        
        
            | 
            | 
           39 | 
           		logger "can't contact the default router"
  | 
        
        
           | 987 | 
           richard | 
           40 | 
           		echo "can't contact the default router"
  | 
        
        
           | 363 | 
           richard | 
           41 | 
           		/bin/sed -i "s?diagnostic =.*?diagnostic = \"can't contact the default router\";?g" $Index_Page
  | 
        
        
           | 308 | 
           richard | 
           42 | 
           		;;
  | 
        
        
            | 
            | 
           43 | 
           	esac
  | 
        
        
           | 520 | 
           richard | 
           44 | 
           	net_pb=`cat /etc/dnsmasq.conf|grep "address=/#/"|wc -l`
  | 
        
        
           | 1157 | 
           stephane | 
           45 | 
           	if [ $net_pb = "0" ] # user alert
  | 
        
        
           | 308 | 
           richard | 
           46 | 
           		then
  | 
        
        
           | 360 | 
           richard | 
           47 | 
           		/bin/sed -i "s?^\$network_pb.*?\$network_pb = True;?g" $Index_Page
  | 
        
        
           | 1377 | 
           richard | 
           48 | 
           		/bin/sed -i "s?^conf-dir=.*?address=\/#\/$PRIVATE_IP?g" /etc/dnsmasq-blacklist.conf
  | 
        
        
           | 520 | 
           richard | 
           49 | 
           		/bin/sed -i "1i\address=\/#\/$PRIVATE_IP" /etc/dnsmasq.conf
  | 
        
        
           | 308 | 
           richard | 
           50 | 
           		/etc/init.d/dnsmasq restart
  | 
        
        
            | 
            | 
           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"
  | 
        
        
           | 988 | 
           franck | 
           65 | 
           	if [ "`/usr/sbin/ethtool $EXTIF|grep Link|cut -d' ' -f3`" != "yes" ] && [ "`/sbin/mii-tool $EXTIF | grep -i link | awk '{print $NF}'`" != "ok" ]
  | 
        
        
           | 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"
  | 
        
        
           | 783 | 
           richard | 
           86 | 
           		net_pb=`cat /etc/dnsmasq.conf|grep "address=/#/"|wc -l`
  | 
        
        
            | 
            | 
           87 | 
           		if [ $net_pb != "0" ]
  | 
        
        
            | 
            | 
           88 | 
           			then
  | 
        
        
            | 
            | 
           89 | 
           			/bin/sed -i "s?^\$network_pb.*?\$network_pb = False;?g" $Index_Page
  | 
        
        
           | 1377 | 
           richard | 
           90 | 
           			/bin/sed -i "s?^address=\/#\/.*?conf-dir=/usr/local/share/dnsmasq-bl-enabled?g" /etc/dnsmasq-blacklist.conf
  | 
        
        
           | 783 | 
           richard | 
           91 | 
           			/bin/sed -i "/^address=/d" /etc/dnsmasq.conf
  | 
        
        
            | 
            | 
           92 | 
           			/etc/init.d/dnsmasq restart
  | 
        
        
            | 
            | 
           93 | 
           		fi
  | 
        
        
            | 
            | 
           94 | 
           	fi
  | 
        
        
            | 
            | 
           95 | 
           }
  | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
           usage="Usage: alcasar-watchdog.sh {-lt --lan_test}"
  | 
        
        
            | 
            | 
           98 | 
           case $1 in
  | 
        
        
            | 
            | 
           99 | 
           	-\? | -h* | --h*)
  | 
        
        
            | 
            | 
           100 | 
           		echo "$usage"
  | 
        
        
            | 
            | 
           101 | 
           		exit 0
  | 
        
        
            | 
            | 
           102 | 
           		;;
  | 
        
        
            | 
            | 
           103 | 
           	-lt | --lan_test)
  | 
        
        
            | 
            | 
           104 | 
           		lan_test
  | 
        
        
            | 
            | 
           105 | 
           		exit 0
  | 
        
        
            | 
            | 
           106 | 
           		;;
  | 
        
        
            | 
            | 
           107 | 
           	*)
  | 
        
        
            | 
            | 
           108 | 
           		lan_test
  | 
        
        
           | 840 | 
           richard | 
           109 | 
           		# read file that contains IP address of quiet equipments
  | 
        
        
           | 783 | 
           richard | 
           110 | 
           		if [ -e $tmp_file ]; then
  | 
        
        
            | 
            | 
           111 | 
           			cat $tmp_file | while read noresponse
  | 
        
        
            | 
            | 
           112 | 
           			do
  | 
        
        
            | 
            | 
           113 | 
           				noresponse_ip=`echo $noresponse | cut -d" " -f1`
  | 
        
        
            | 
            | 
           114 | 
           				noresponse_mac=`echo $noresponse | cut -d" " -f2`
  | 
        
        
           | 840 | 
           richard | 
           115 | 
           				noresponse_user=`echo $noresponse | cut -d" " -f3`
  | 
        
        
           | 787 | 
           richard | 
           116 | 
           				arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c1 -w4 $noresponse_ip|grep "Unicast reply"|wc -l`
  | 
        
        
           | 783 | 
           richard | 
           117 | 
           				if [[ $(expr $arp_reply) -eq 0 ]]
  | 
        
        
            | 
            | 
           118 | 
           	       				then
  | 
        
        
           | 845 | 
           richard | 
           119 | 
           					logger "alcasar-watchdog $noresponse_ip ($noresponse_mac) can't be contact. Alcasar disconnects the user ($noresponse_user)."
  | 
        
        
           | 840 | 
           richard | 
           120 | 
           					/usr/sbin/chilli_query logout $noresponse_mac
  | 
        
        
            | 
            | 
           121 | 
           					/usr/sbin/chilli_query dhcp-release $noresponse_mac  # release dhcp for mac_auth equipment 
  | 
        
        
           | 783 | 
           richard | 
           122 | 
           				fi
  | 
        
        
            | 
            | 
           123 | 
           			done
  | 
        
        
            | 
            | 
           124 | 
           			rm $tmp_file
  | 
        
        
           | 1 | 
           root | 
           125 | 
           		fi
  | 
        
        
           | 1157 | 
           stephane | 
           126 | 
           # process each equipment known by chilli
  | 
        
        
           | 783 | 
           richard | 
           127 | 
           		for system in `/usr/sbin/chilli_query list |grep -v "\.0\.0\.0"`
  | 
        
        
            | 
            | 
           128 | 
           		do
  | 
        
        
            | 
            | 
           129 | 
           			active_ip=`echo $system |cut -d" " -f2`
  | 
        
        
            | 
            | 
           130 | 
           			active_session=`echo $system |cut -d" " -f5`
  | 
        
        
            | 
            | 
           131 | 
           			active_mac=`echo $system | cut -d" " -f1`
  | 
        
        
           | 840 | 
           richard | 
           132 | 
           			active_user=`echo $system |cut -d" " -f6`
  | 
        
        
           | 1157 | 
           stephane | 
           133 | 
           # process only equipment with an authenticated user
  | 
        
        
           | 783 | 
           richard | 
           134 | 
           			if [[ $(expr $active_session) -eq 1 ]]
  | 
        
        
           | 1157 | 
           stephane | 
           135 | 
           			then
  | 
        
        
           | 783 | 
           richard | 
           136 | 
           				arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c2 -w4 $active_ip|grep "Unicast reply"|wc -l`
  | 
        
        
           | 1157 | 
           stephane | 
           137 | 
           # store @IP of quiet equipments
  | 
        
        
           | 783 | 
           richard | 
           138 | 
           				if [[ $(expr $arp_reply) -eq 0 ]]
  | 
        
        
            | 
            | 
           139 | 
           	       				then
  | 
        
        
           | 1157 | 
           stephane | 
           140 | 
           					PTN='^[[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]]$'
  | 
        
        
            | 
            | 
           141 | 
           					if [[ $(expr $active_user : $PTN) -eq 0 ]] # don't process @mac auth equipments
  | 
        
        
            | 
            | 
           142 | 
           					then
  | 
        
        
            | 
            | 
           143 | 
           						echo "$active_ip $active_mac $active_user" >> $tmp_file
  | 
        
        
            | 
            | 
           144 | 
           					fi
  | 
        
        
           | 783 | 
           richard | 
           145 | 
           				fi
  | 
        
        
           | 1157 | 
           stephane | 
           146 | 
           # disconnect users whose equipement is usurped (@MAC)
  | 
        
        
           | 783 | 
           richard | 
           147 | 
           				if [[ $(expr $arp_reply) -gt 2 ]]
  | 
        
        
           | 1202 | 
           crox53 | 
           148 | 
           	       				then 
  | 
        
        
            | 
            | 
           149 | 
           					echo "$(date "+[%x-%X] : ")alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)." >> /var/Save/logs/security/watchdog.log
  | 
        
        
           | 840 | 
           richard | 
           150 | 
           					logger "alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user ($active_user)."
  | 
        
        
           | 783 | 
           richard | 
           151 | 
           					/usr/sbin/chilli_query logout $active_mac
  | 
        
        
           | 1165 | 
           crox53 | 
           152 | 
           					chmod 644 /var/Save/logs/security/watchdog.log
  | 
        
        
           | 783 | 
           richard | 
           153 | 
           				fi
  | 
        
        
            | 
            | 
           154 | 
           			fi
  | 
        
        
            | 
            | 
           155 | 
           		done
  | 
        
        
            | 
            | 
           156 | 
           		;;
  | 
        
        
            | 
            | 
           157 | 
           esac	
  | 
        
        
           | 597 | 
           richard | 
           158 | 
           IFS=$OLDIFS
  |