#!/bin/sh
#
#   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='/var/packages/MailPlus-Server/target/etc/mimedefang';
BADURL='http://phishing.mailscanner.info/phishing.bad.sites.conf.gz';
SAFEURL='http://phishing.mailscanner.info/phishing.safe.sites.conf.gz';
MIMEDEFANG_SCRIPT='/var/packages/MailPlus-Server/target/scripts/daemon/mimedefang.sh'

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 Cannot find MIMEDefang configuration directory, update failed.
	echo Cannot find MIMEDefang configuration directory.
	echo Auto-updates of phishing.bad.sites.conf and phishing.safe.sites.conf will not happen.
	exit 1
fi

SYSLOG="mail"

gotAnError=0

# 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 MIMEDefang' >> $CONFIGDIR/phishing.bad.sites.custom
	echo '# directory. Note that phishing.bad.sites.conf is' >> $CONFIGDIR/phishing.bad.sites.custom
	echo '# overwritten when 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 MIMEDefang' >> $CONFIGDIR/phishing.safe.sites.custom
	echo '# directory. Note that phishing.safe.sites.conf is' >> $CONFIGDIR/phishing.safe.sites.custom
	echo '# overwritten when 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));

${MIMEDEFANG_SCRIPT} status
if [ 0 -eq $? ]; then
	${MIMEDEFANG_SCRIPT} reload
else
	echo "MIMEDefang is not running."
fi

exit ${gotAnError}
