File: //sbin/ms-update-phishing
#!/usr/bin/env bash
#
# MailScanner - SMTP E-Mail Virus Scanner
# Copyright (C) 2002-2017 Julian Field, Thom van der Boon
#
# $Id: update_bad_safe_phishing_sites 3982 2017-08-22 09:00:39Z sysjkf $
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# set your options here
#
#CONFIGDIR='/opt/MailScanner/etc';
CONFIGDIR='/etc/MailScanner';
BADURL='http://phishing.mailscanner.info/phishing.bad.sites.conf.gz';
SAFEURL='http://phishing.mailscanner.info/phishing.safe.sites.conf.gz';
MSSTARTSCRIPT='/etc/init.d/mailscanner';
MSSERVICENAME='mailscanner.service';
PIDFile=`ms-peek 'PID file' $CONFIGDIR/MailScanner.conf`
DEBUG=0
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/etc:/usr/local/bin:/usr/sfw/bin
export PATH
if [ -d $CONFIGDIR ]; then
cd $CONFIGDIR
else
logger -p mail.warn -t update.phishing.sites Cannot find MailScanner configuration directory, update failed.
echo Cannot find MailScanner configuration directory.
echo Auto-updates of phishing.bad.sites.conf and phishing.safe.sites.conf will not happen.
exit 1
fi
SYSLOG=`/usr/sbin/ms-peek SyslogFacility MailScanner.conf`
if [ "x$SYSLOG" == "x" ]; then
SYSLOG="mail"
fi
# Load MailScanner Defaults
if [ -f $CONFIGDIR/defaults ] ; then
. $CONFIGDIR/defaults
fi
gotAnError=0
if [ -z "$ms_cron_ps_restart" ]; then
echo 'ms_cron_ps_restart not found in defaults. Adding....'
echo '' >> $CONFIGDIR/defaults
echo '' >> $CONFIGDIR/defaults
echo '# Restart MailScanner after Update Safe/Bad Phishing sites' >> $CONFIGDIR/defaults
echo '#' >> $CONFIGDIR/defaults
echo '# Restart MailScanner after update of the Safe/Bad Phishing sites files.' >> $CONFIGDIR/defaults
echo '# This is disabled by default.' >> $CONFIGDIR/defaults
echo '#' >> $CONFIGDIR/defaults
echo '# This is executed during the DAILY cron option. 0 = off, 1 = on' >> $CONFIGDIR/defaults
echo '#' >> $CONFIGDIR/defaults
echo '' >> $CONFIGDIR/defaults
echo 'ms_cron_ps_restart=0' >> $CONFIGDIR/defaults
ms_cron_ps_restart=0;
fi
# check for the custom config file and create if missing
if [ ! -f $CONFIGDIR/phishing.bad.sites.custom ]; then
echo '# Add your custom Phishing bad sites to the' > $CONFIGDIR/phishing.bad.sites.custom
echo '# phishing.bad.sites.custom file in your MailScanner' >> $CONFIGDIR/phishing.bad.sites.custom
echo '# directory. Note that phishing.bad.sites.conf is' >> $CONFIGDIR/phishing.bad.sites.custom
echo '# overwritten when ms-update-phishing is executed.' >> $CONFIGDIR/phishing.bad.sites.custom
echo '#' >> $CONFIGDIR/phishing.bad.sites.custom
fi
# check for the custom config file and create if missing
if [ ! -f $CONFIGDIR/phishing.safe.sites.custom ]; then
echo '# Add your custom Phishing safe sites to the' > $CONFIGDIR/phishing.safe.sites.custom
echo '# phishing.safe.sites.custom file in your MailScanner' >> $CONFIGDIR/phishing.safe.sites.custom
echo '# directory. Note that phishing.safe.sites.conf is' >> $CONFIGDIR/phishing.safe.sites.custom
echo '# overwritten when ms-update-phishing is executed.' >> $CONFIGDIR/phishing.safe.sites.custom
echo '#' >> $CONFIGDIR/phishing.safe.sites.custom
fi
# Sanity check for regular config files, create if missing
[ ! -f $CONFIGDIR/phishing.safe.sites.conf ] && touch $CONFIGDIR/phishing.safe.sites.conf
[ ! -f $CONFIGDIR/phishing.bad.sites.conf ] && touch $CONFIGDIR/phishing.bad.sites.conf
if hash curl 2>/dev/null; then
[ $DEBUG -eq 1 ] && echo 'Found curl'
CURLORWGET='curl';
else
if hash wget 2>/dev/null; then
[ $DEBUG -eq 1 ] && echo 'Found wget'
CURLORWGET='wget';
else
logger -p $SYSLOG.warn -t update.bad.phishing.sites Cannot find wget or curl, update failed. ; echo Cannot find wget or curl to do phishing sites update. ; exit 1
fi
fi
[ $DEBUG -eq 1 ] && echo Trying to update phishing.bad.sites.conf.master....
bad_updated=1;
if [ $CURLORWGET = 'curl' ]; then
curl -S -A "msv5 Update Script v0.2.0" -z $CONFIGDIR/phishing.bad.sites.conf.master.gz -o $CONFIGDIR/phishing.bad.sites.conf.master.gz $BADURL &> /dev/null
if [ "$?" = "0" ]; then
gunzip -f $CONFIGDIR/phishing.bad.sites.conf.master.gz
[ $DEBUG -eq 1 ] && echo Check or download phishing.bad.sites.conf.master completed OK.
if [ -r $CONFIGDIR/phishing.bad.sites.conf.master.backup ]; then
if [ $CONFIGDIR/phishing.bad.sites.conf.master -nt $CONFIGDIR/phishing.bad.sites.conf.master.backup ]; then
filesize=$(wc -l < $CONFIGDIR/phishing.bad.sites.conf.master)
if [ $filesize -gt 1000 ]; then
[ $DEBUG -eq 1 ] && echo Download fresh phishing.bad.sites.conf.master succeeded, so make a backup
cp -f $CONFIGDIR/phishing.bad.sites.conf.master $CONFIGDIR/phishing.bad.sites.conf.master.backup
else
[ $DEBUG -eq 1 ] && echo Check phishing.bad.sites.conf.master failed: It looks corrupt
rm -f $CONFIGDIR/phishing.bad.sites.conf.master
logger -p $SYSLOG.warn -t update.bad.phishing.sites Downloaded bad phishing site master file is corrupt, update failed. ; echo "Downloaded bad phishing site master file is corrupt, removing it." ;
gotAnError=1
fi
else
[ $DEBUG -eq 1 ] && echo Remote phishing.bad.sites.conf.master not newer than local copy.... everything OK
bad_updated=0
fi
else
filesize=$(wc -l < $CONFIGDIR/phishing.bad.sites.conf.master)
if [ $filesize -gt 1000 ]; then
[ $DEBUG -eq 1 ] && echo Download fresh phishing.bad.sites.conf.master succeeded, so make a backup
cp -f $CONFIGDIR/phishing.bad.sites.conf.master $CONFIGDIR/phishing.bad.sites.conf.master.backup
else
[ $DEBUG -eq 1 ] && echo Check phishing.bad.sites.conf.master failed: It looks corrupt
rm -f $CONFIGDIR/phishing.bad.sites.conf.master
logger -p $SYSLOG.warn -t update.bad.phishing.sites Downloaded bad phishing site master file is corrupt, update failed. ; echo "Downloaded bad phishing site master file is corrupt, removing it." ;
gotAnError=1
fi
fi
else
logger -p $SYSLOG.warn -t update.bad.phishing.sites Updating using curl failed, trying wget. ; echo Updating using curl failed, trying wget. ; CURLORWGET='wget';
fi
fi
if [ $CURLORWGET = 'wget' ]; then
echo Trying wget....
wget -q --user-agent="msv5 Update Script v0.2.0" --no-check-certificate -N -O phishing.bad.sites.conf.master.gz $BADURL
if [ "$?" = "0" ]; then
gunzip -f $CONFIGDIR/phishing.bad.sites.conf.master.gz
[ $DEBUG -eq 1 ] && echo Check or download phishing.bad.sites.conf.master completed OK.
if [ -r $CONFIGDIR/phishing.bad.sites.conf.master.backup ]; then
if [ $CONFIGDIR/phishing.bad.sites.conf.master -nt $CONFIGDIR/phishing.bad.sites.conf.master.backup ]; then
filesize=$(wc -l < $CONFIGDIR/phishing.bad.sites.conf.master)
if [ $filesize -gt 1000 ]; then
[ $DEBUG -eq 1 ] && echo Download fresh phishing.bad.sites.conf.master succeeded, so make a backup
cp -f $CONFIGDIR/phishing.bad.sites.conf.master $CONFIGDIR/phishing.bad.sites.conf.master.backup
else
[ $DEBUG -eq 1 ] && echo Check phishing.bad.sites.conf.master failed: It looks corrupt
rm -f $CONFIGDIR/phishing.bad.sites.conf.master
logger -p $SYSLOG.warn -t update.bad.phishing.sites Downloaded bad phishing site master file is corrupt, removing it. ; echo "Downloaded bad phishing site master file is corrupt, removing it." ;
gotAnError=1
fi
else
[ $DEBUG -eq 1 ] && echo Remote phishing.bad.sites.conf.master not newer than local copy.... everything OK
bad_updated=0
fi
else
filesize=$(wc -l < $CONFIGDIR/phishing.bad.sites.conf.master)
if [ $filesize -gt 1000 ]; then
[ $DEBUG -eq 1 ] && echo Download fresh phishing.bad.sites.conf.master succeeded, so make a backup
cp -f $CONFIGDIR/phishing.bad.sites.conf.master $CONFIGDIR/phishing.bad.sites.conf.master.backup
else
[ $DEBUG -eq 1 ] && echo Check phishing.bad.sites.conf.master failed: It looks corrupt
rm -f $CONFIGDIR/phishing.bad.sites.conf.master
logger -p $SYSLOG.warn -t update.bad.phishing.sites Downloaded bad phishing site master file is corrupt, removing it. ; echo "Downloaded bad phishing site master file is corrupt, removing it." ;
gotAnError=1
fi
fi
else
rm -f $CONFIGDIR/phishing.bad.sites.conf.master
logger -p $SYSLOG.warn -t update.bad.phishing.sites The curl/wget download of the bad phishing site master file failed. Removing whatever was downloaded.; echo The curl/wget download of the bad phishing site master file failed. Removing whatever was downloaded.;
gotAnError=1
fi
fi
bad_custom_time=$(stat -c %Y phishing.bad.sites.custom)
bad_conf_time=$(stat -c %Y phishing.bad.sites.conf)
if [ $bad_custom_time -ge $bad_conf_time ]; then
bad_updated=1
fi
if [ $bad_updated -ge 1 ]; then
if [ -s phishing.bad.sites.conf.master ]; then
[ $DEBUG -eq 1 ] && echo Creating fresh phishing.bad.sites.conf....
cat phishing.bad.sites.custom phishing.bad.sites.conf.master | \
uniq > phishing.bad.sites.conf.new
rm -f phishing.bad.sites.conf
mv -f phishing.bad.sites.conf.new phishing.bad.sites.conf
chmod a+r phishing.bad.sites.conf
[ $DEBUG -eq 1 ] && echo Creation of fresh phishing.bad.sites.conf is OK
logger -p $SYSLOG.info -t update.bad.phishing.sites Phishing bad sites list updated
else
logger -p $SYSLOG.info -t update.bad.phishing.sites The phishing bad sites master file does not exist or is not readable. Update failed! ; echo "The phishing bad sites master file does not exist or is not readable. Update failed!" ;
gotAnError=1
bad_updated=0
fi
fi
[ $DEBUG -eq 1 ] && echo Trying to update phishing.safe.sites.conf.master....
safe_updated=1;
if [ $CURLORWGET = 'curl' ]; then
curl -S -A "msv5 Update Script v0.2.0" -z $CONFIGDIR/phishing.safe.sites.conf.master.gz -o $CONFIGDIR/phishing.safe.sites.conf.master.gz $SAFEURL &> /dev/null
if [ "$?" = "0" ]; then
gunzip -f $CONFIGDIR/phishing.safe.sites.conf.master.gz
[ $DEBUG -eq 1 ] && echo Check or download phishing.safe.sites.conf.master completed OK.
if [ -r $CONFIGDIR/phishing.safe.sites.conf.master.backup ]; then
if [ $CONFIGDIR/phishing.safe.sites.conf.master -nt $CONFIGDIR/phishing.safe.sites.conf.master.backup ]; then
filesize=$(wc -l < $CONFIGDIR/phishing.safe.sites.conf.master)
if [ $filesize -gt 500 ]; then
[ $DEBUG -eq 1 ] && echo Download fresh phishing.safe.sites.conf.master succeeded, so make a backup
cp -f $CONFIGDIR/phishing.safe.sites.conf.master $CONFIGDIR/phishing.safe.sites.conf.master.backup
else
[ $DEBUG -eq 1 ] && echo Check phishing.safe.sites.conf.master failed: It looks corrupt
rm -f $CONFIGDIR/phishing.safe.sites.conf.master
logger -p $SYSLOG.warn -t update.safe.phishing.sites Downloaded safe phishing site master file is corrupt, update failed. ; echo "Downloaded safe phishing site master file is corrupt, removing it." ;
gotAnError=1
fi
else
[ $DEBUG -eq 1 ] && echo Remote phishing.safe.sites.conf.master not newer than local copy.... everything OK
safe_updated=0
fi
else
filesize=$(wc -l < $CONFIGDIR/phishing.safe.sites.conf.master)
if [ $filesize -gt 500 ]; then
[ $DEBUG -eq 1 ] && echo Download fresh phishing.safe.sites.conf.master succeeded, so make a backup
cp -f $CONFIGDIR/phishing.safe.sites.conf.master $CONFIGDIR/phishing.safe.sites.conf.master.backup
else
[ $DEBUG -eq 1 ] && echo Check phishing.safe.sites.conf.master failed: It looks corrupt
rm -f $CONFIGDIR/phishing.safe.sites.conf.master
logger -p $SYSLOG.warn -t update.safe.phishing.sites Downloaded safe phishing site master file is corrupt, update failed. ; echo "Downloaded safe phishing site master file is corrupt, removing it." ;
gotAnError=1
fi
fi
else
logger -p $SYSLOG.warn -t update.safe.phishing.sites Updating using curl failed, trying wget. ; echo Updating using curl failed, trying wget. ; CURLORWGET='wget';
fi
fi
if [ $CURLORWGET = 'wget' ]; then
echo Trying wget....
wget -q --user-agent="msv5 Update Script v0.2.0" --no-check-certificate -N -O phishing.safe.sites.conf.master.gz $SAFEURL
if [ "$?" = "0" ]; then
gunzip -f $CONFIGDIR/phishing.safe.sites.conf.master.gz
[ $DEBUG -eq 1 ] && echo Check or download phishing.safe.sites.conf.master completed OK.
if [ -r $CONFIGDIR/phishing.safe.sites.conf.master.backup ]; then
if [ $CONFIGDIR/phishing.safe.sites.conf.master -nt $CONFIGDIR/phishing.safe.sites.conf.master.backup ]; then
filesize=$(wc -l < $CONFIGDIR/phishing.safe.sites.conf.master)
if [ $filesize -gt 500 ]; then
[ $DEBUG -eq 1 ] && echo Download fresh phishing.safe.sites.conf.master succeeded, so make a backup
cp -f $CONFIGDIR/phishing.safe.sites.conf.master $CONFIGDIR/phishing.safe.sites.conf.master.backup
else
[ $DEBUG -eq 1 ] && echo Check phishing.safe.sites.conf.master failed: It looks corrupt
rm -f $CONFIGDIR/phishing.safe.sites.conf.master
logger -p $SYSLOG.warn -t update.safe.phishing.sites Downloaded safe phishing site master file is corrupt, removing it. ; echo "Downloaded safe phishing site master file is corrupt, removing it." ;
fi
else
[ $DEBUG -eq 1 ] && echo Remote phishing.safe.sites.conf.master not newer than local copy.... everything OK
safe_updated=0
fi
else
filesize=$(wc -l < $CONFIGDIR/phishing.safe.sites.conf.master)
if [ $filesize -gt 500 ]; then
[ $DEBUG -eq 1 ] && echo Download fresh phishing.safe.sites.conf.master succeeded, so make a backup
cp -f $CONFIGDIR/phishing.safe.sites.conf.master $CONFIGDIR/phishing.safe.sites.conf.master.backup
else
[ $DEBUG -eq 1 ] && echo Check phishing.safe.sites.conf.master failed: It looks corrupt
rm -f $CONFIGDIR/phishing.safe.sites.conf.master
logger -p $SYSLOG.warn -t update.safe.phishing.sites Downloaded safe phishing site master file is corrupt, removing it. ; echo "Downloaded safe phishing site master file is corrupt, removing it." ;
gotAnError=1
fi
fi
else
rm -f $CONFIGDIR/phishing.safe.sites.conf.master
logger -p $SYSLOG.warn -t update.safe.phishing.sites The curl/wget download of the safe phishing site master file failed. Removing whatever was downloaded.; echo The curl/wget download of the safe phishing site master file failed. Removing whatever was downloaded.;
gotAnError=1
fi
fi
safe_custom_time=$(stat -c %Y phishing.safe.sites.custom)
safe_conf_time=$(stat -c %Y phishing.safe.sites.conf)
if [ $safe_custom_time -ge $safe_conf_time ]; then
safe_updated=1
fi
if [ $safe_updated -ge 1 ]; then
if [ -s phishing.safe.sites.conf.master ]; then
[ $DEBUG -eq 1 ] && echo Creating fresh phishing.safe.sites.conf....
cat phishing.safe.sites.custom phishing.safe.sites.conf.master | \
uniq > phishing.safe.sites.conf.new
rm -f phishing.safe.sites.conf
mv -f phishing.safe.sites.conf.new phishing.safe.sites.conf
chmod a+r phishing.safe.sites.conf
[ $DEBUG -eq 1 ] && echo Creation of fresh phishing.safe.sites.conf is OK
logger -p $SYSLOG.info -t update.safe.phishing.sites Phishing safe sites list updated
else
logger -p $SYSLOG.info -t update.safe.phishing.sites The phishing safe sites master file does not exist or is not readable. Update failed! ; echo "The phishing safe sites master file does not exist or is not readable. Update failed!" ;
gotAnError=1
safe_updated=0
fi
fi
updated=$(($bad_updated + $safe_updated));
if [ $updated -ge 1 ]; then
if [ "$ms_cron_ps_restart" = "1" ]; then
if [ ! -f /var/lock/subsys/MailScanner.off -a -f $PIDFile ]; then
logger -p $SYSLOG.info -t ms-update-phishing restart MailScanner....
if [ -d /etc/systemd ]; then
systemctl restart $MSSERVICENAME >/dev/null 2>&1
if [ $? != 0 ] ; then
[ $DEBUG -eq 1 ] && echo "MailScanner reload failed - Retrying..."
systemctl restart $MSSERVICENAME >/dev/null 2>&1
if [ $? != 0 ] ; then
[ $DEBUG -eq 1 ] && echo "Stopping MailScanner..."
systemctl stop $MSSERVICENAME >/dev/null 2>&1
[ $DEBUG -eq 1 ] && echo "Waiting for a minute..."
perl -e "sleep 60;"
[ $DEBUG -eq 1 ] && echo "Attemping to start MailScanner..."
systemctl start $MSSERVICENAME >/dev/null 2>&1
fi
fi
else
if [ -s $MSSTARTSCRIPT ]; then
$MSSTARTSCRIPT restart >/dev/null 2>&1
if [ $? != 0 ] ; then
[ $DEBUG -eq 1 ] && echo "MailScanner reload failed - Retrying..."
$MSSTARTSCRIPT restart >/dev/null 2>&1
if [ $? != 0 ] ; then
[ $DEBUG -eq 1 ] && echo "Stopping MailScanner..."
$MSSTARTSCRIPT stop >/dev/null 2>&1
[ $DEBUG -eq 1 ] && echo "Waiting for a minute..."
perl -e "sleep 60;"
[ $DEBUG -eq 1 ] && echo "Attemping to start MailScanner..."
$MSSTARTSCRIPT start >/dev/null 2>&1
fi
fi
fi
fi
fi
fi
fi
exit ${gotAnError}