Inhaltsverzeichnis

Spamreport


spamcheck.sh
/usr/local/bin/spamcheck.sh
#!/bin/bash
 
#########################################################
# UKBW-Spam-Report, 2007-05-01, chhaas, IuK / Netzwerke #
#########################################################
 
#set -o verbose
 
########
# ToDo #
########
# - printvariablen bei "grep X-Spam-Status:" korrigieren
# - Mail an antispam@uk-bw.de zusammenfassen: 1 Mail die alle Spammails inkl. Empfaenger beinhaltet
# - Variablen fuer FROM: und RETURN-PATH einrichten
# - evtl. SpamAssassin Spam-Tags (Grund fuer Spam-Klassifizierung) mit in Reports einbauen
# - Mail-Adressen evtl. per LDAP-Abfrage herausfinden
# - in Spamreport XX durch Datum des letzten Spamreports ersetzen
 
 
#############
# Variables #
#############
DATESTRING=`date +"%Y-%m-%d_%H%M"`
TMPDIR=`mktemp -d /tmp/spam.${DATESTRING}.XXXXXX` || exit 1
DATENOW=`date +"%d.%m.%Y, %H:%M"`
USERFOLDERS=`ls -1d /var/spool/imap/user/*`
#USERFOLDERS="/var/spool/imap/user/haasc"
LASTRUNFILE="/tmp/spam_lastrun"
MAILSUBJECT_ONE="neue Mail in Ihrem 'Junk'-Ordner"
MAILSUBJECT_MORE="neue Mails in Ihrem 'Junk'-Ordner"
MAILHEAD="/usr/local/chhaas-skripts/spamreport_header.txt"
MAILFOOT="/usr/local/chhaas-skripts/spamreport_footer.txt"
TOTAL_SYS_NEWSPAMCOUNT="0"
TOTAL_SYS_OLDSPAMCOUNT="0"
TOTAL_SYS_SPAMCOUNT="0"
 
 
####################
# Set Lastrun Date #
####################
# touch ${LASTRUNFILE}".new"
echo ${DATENOW} > ${LASTRUNFILE}".new"
 
#########################
# Get previous run date #
#########################
OLDDATE=`cat ${LASTRUNFILE}`
 
if [ -d ${TMPDIR} ]; then
        for DIR in ${USERFOLDERS}; do
                echo "DIR :" ${DIR}
		MAILBOX=`echo ${DIR} | awk -F'/' '{print $6}'`
 
                echo "MAILBOX: " ${MAILBOX}
		cd ${DIR}
                if [ -d "Junk" ]; then
			##################
			# Reset Counters #
			##################
			NEWSPAMCOUNT=0
			OLDSPAMCOUNT=0
			TOTALSPAMCOUNT=0
 
			########################
			# Generate Spam-Report #
			########################
			PROTOFILE="${TMPDIR}/${MAILBOX}.report"
			FILECOUNT=`find ${DIR}/Junk -type f | wc -l`
			TOTALSPAMCOUNT=`expr ${FILECOUNT} - 3`
###			echo "TOTALSPAMCOUNT: $TOTALSPAMCOUNT" >> ${PROTOFILE}
### 2. Kopfblock (PROTOFILE)
			echo " " >> ${PROTOFILE}
			echo " Neue Nachrichten mit Spam-Status in Ihrem 'Junk'-Ordner:"  >> ${PROTOFILE}
			echo "----------------------------------------------------------------------------------------" >> ${PROTOFILE}
 
###			for NEWSPAM in `find Junk -cnewer Junk/ | grep "Junk/" | grep -v "cyrus." 2> /dev/null`; do
			for NEWSPAM in `find Junk -cnewer ${LASTRUNFILE} | grep "Junk/" | grep -v "cyrus." 2> /dev/null`; do
				NEWSPAMCOUNT=`expr ${NEWSPAMCOUNT} + 1`
 
###				echo "----------------------------------------------------------------------------------------" >> ${PROTOFILE}
###				echo "Nachricht ${NEWSPAMCOUNT}" >> ${PROTOFILE}
				echo "" >> ${PROTOFILE}
### Test:
				echo "    ${NEWSPAM}" >> ${PROTOFILE}
				echo "    "`grep "Subject: " ${NEWSPAM} | head -1` >> ${PROTOFILE}
				echo "    "`grep "To: " ${NEWSPAM} | head -1` >> ${PROTOFILE}
				echo "    "`grep "Delivered-To: " ${NEWSPAM} | head -1` >> ${PROTOFILE}
				echo "    "`grep "From: " ${NEWSPAM} | head -1` >> ${PROTOFILE}
				echo "    "`grep "Date: " ${NEWSPAM} | head -1` >> ${PROTOFILE}
				echo "    "`grep "X-Spam-Status: " ${NEWSPAM} | awk  '{print "Spamwertung unseres Systems: "$2" / "$3}'` >> ${PROTOFILE}
				echo "    "`grep "Content analysis details:" ${NEWSPAM} | awk '{print "Spamwertung: "$4" Punkte, "$6" benoetigt)" }'` >> ${PROTOFILE}
                                echo "    Nachrichtengroesse: "`ls -lisah ${NEWSPAM} | awk '{print $2}'` >> ${PROTOFILE}
                                echo "    Dateianhaenge:" `grep "Content-Disposition: attachment; filename=" ${NEWSPAM} | awk -F '"' '{print $2}' | xargs` >> ${PROTOFILE}
                                echo "" >> ${PROTOFILE}
				echo "----------------------------------------------------------------------------------------" >> ${PROTOFILE}
                        done;
 
                        echo "" >> ${PROTOFILE}
			echo "----------------------------------------------------------------------------------------" >> ${PROTOFILE}
                        echo "Folgende Dateien sind zur Loeschung vorgesehen:"  >> ${PROTOFILE}
                        echo "(Mails aelter als 14 Tage)"  >> ${PROTOFILE}
                        for OLDSPAM in `find "Junk" -mtime +14 | grep "Junk/" | grep -v "cyrus." 2> /dev/null`; do
                                OLDSPAMCOUNT=`expr ${OLDSPAMCOUNT} + 1`
###				echo "----------------------------------------------------------------------------------------" >> ${PROTOFILE}
###				echo "Nachricht ${OLDSPAMCOUNT}" >> ${PROTOFILE}
				echo "" >> ${PROTOFILE}
				echo "    "`grep "Subject: " ${OLDSPAM} | head -1` >> ${PROTOFILE}
				echo "    "`grep "To: " ${OLDSPAM} | head -1` >> ${PROTOFILE}
				echo "    "`grep "From: " ${OLDSPAM} | head -1` >> ${PROTOFILE}
				echo "    "`grep "Date: " ${OLDSPAM} | head -1` >> ${PROTOFILE}
				echo "    "`grep "X-Spam-Status: " ${OLDSPAM} | awk  '{print "Spamwertung: "$2" / "$3}'` >> ${PROTOFILE}
			done;
 
 
                        ########################################################
                        # Only do this if we have spam (${TOTALSPAMCOUNT} > 0) #
                        ########################################################
                        if [ "${TOTALSPAMCOUNT}" -gt "0" ]; then
###				echo "===--->>> SPAM vorhanden!!! <<<---===" >> ${PROTOFILE}
                                USER_MAILFILE="${TMPDIR}/${MAILBOX}.mail"
				USER_HEADER="${TMPDIR}/${MAILBOX}_header.mail"
 
                                # hier stattdessen besser eine Abfrage aus dem LDAP nach der email-Adresse einfuegen!
				MAILADDR=`ldapsearch -x -h localhost -b 'dc=uk-bw,dc=de' "uid=${MAILBOX}" mail | grep "mail: " | awk -F' ' '{print $2}'`
				# Alternativ, testweise:
				# MAILADDR=${MAILBOX}@uk-bw.de -> funktioniert!
				# MAILADDR=`grep "${MAILBOX}" /etc/postfix/maps/virtual | sed -n '/^@/!p' | head -1 | awk '{print $1}'`
				echo "MAILADDR: ${MAILADDR}"
				if [ ${NEWSPAMCOUNT} -gt "1" ]; then
                                        MAILNEWSUBJ="Subject: ${NEWSPAMCOUNT} ${MAILSUBJECT_MORE}, ${DATENOW}"
                                else
                                        if [ ${NEWSPAMCOUNT} -eq "0" ]; then
                                                MAILNEWSUBJ="Subject: SPAM zur Loeschung, ${DATENOW}"
                                        else
                                                MAILNEWSUBJ="Subject: ${NEWSPAMCOUNT} ${MAILSUBJECT_ONE}, ${DATENOW}"
                                        fi;
                                fi;
 
###				if [ ! -n "${MAILADDR}" ]; then
###					MAILADDR="info"`grep "${MAILBOX}" /etc/postfix/maps/virtual | head -1 | awk '{print $1}'`
###				fi;
 
 
                                ######################
                                # Send Mail to Users #
				######################
                                echo "To: ${MAILADDR}" >> ${USER_MAILFILE}
                                echo ${MAILNEWSUBJ} >> ${USER_MAILFILE}
                                echo "Return-Path: UKBW Hostmaster <hostmaster@uk-bw.de>" >> ${USER_MAILFILE}
                                echo "From: UKBW Hostmaster <hostmaster@uk-bw.de>" >> ${USER_MAILFILE}
                                echo "Errors-To: hostmaster@uk-bw.de" >> ${USER_MAILFILE}
                                echo "" >> ${USER_MAILFILE}
                                echo "" >> ${USER_MAILFILE}
### 1. Kopfblock (USER_HEADER)
###				echo " 1. Kopfblock (USER_HEADER)" >> ${USER_HEADER}
			        echo "----------------------------------------------------------------------------------------" >> ${USER_HEADER}
			        echo "" >> ${USER_HEADER}
			        echo " Junk-/Spam-Status-Report fuer den Zeitraum vom ${OLDDATE} bis ${DATENOW}" >> ${USER_HEADER}
			        echo "" >> ${USER_HEADER}
			        echo "----------------------------------------------------------------------------------------" >> ${USER_HEADER}
			        echo "" >> ${USER_HEADER}
				echo " Anzahl neuer Nachrichten mit Spam-Status in Ihrem 'Junk'-Ordner: ${NEWSPAMCOUNT}" >> ${USER_HEADER}
				echo " Gesamtanzahl Nachrichten mit Spam-Status in Ihrem 'Junk'-Ordner: ${TOTALSPAMCOUNT}" >> ${USER_HEADER}
				echo "" >> ${USER_HEADER}
			        echo "----------------------------------------------------------------------------------------" >> ${USER_HEADER}
 
### hier wird die Reihenfolge festgelegt:
				cat ${MAILHEAD} ${USER_HEADER} ${PROTOFILE} ${MAILFOOT} >> ${USER_MAILFILE}
 
###				Mail waehrend Test-Phase nicht senden, chhaas:
###				cat ${USER_MAILFILE} | sendmail -t -F "UKBW Hostmaster <hostmaster@uk-bw.de>" -oi
 
 
 
				####################################
				# Build Summary for Antspam-System #
				####################################
#				SYSPROTOFILE=`touch ${TMPDIR}/antispam-system.report`
				SYSPROTOFILE="${TMPDIR}/antispam-system.report"				
				# hier stattdessen besser eine Abfrage aus dem LDAP nach der email-Adresse einfuegen!
				echo " Spamreport fuer: ${MAILADDR}" >> ${SYSPROTOFILE}
				echo " Anzahl neuer Spam-Mails: ${NEWSPAMCOUNT}" >> ${SYSPROTOFILE}
				echo " Gesamt-Anzahl der Mails im Junk-Ordner: ${TOTALSPAMCOUNT}" >> ${SYSPROTOFILE}
				echo "----------------------------------------------------------------------------------------" >> ${SYSPROTOFILE}
###				cat ${PROTOFILE} >> ${SYSPROTOFILE}
                                echo "" >> ${SYSPROTOFILE}
                        fi;
		### unnoetig, wenn zum Schluss sowieso der ganze Ordner geloescht wird:
		echo "${PROTOFILE} wird geloescht!"
		rm -f ${PROTOFILE}
		ls ${PROTOFILE}
		rm -f ${USER_HEADER}
###		rm -f ${USER_MAILFILE}
		#
		echo "NEWSPAMCOUNT: '${NEWSPAMCOUNT}'"
	    	echo "OLDSPAMCOUNT: '${OLDSPAMCOUNT}'"
	    	echo "TOTALSPAMCOUNT: '${TOTALSPAMCOUNT}'"
		echo "TOTAL_SYS_SPAMCOUNT (pre): '${TOTAL_SYS_SPAMCOUNT}'"
		TOTAL_SYS_SPAMCOUNT=`expr ${TOTAL_SYS_SPAMCOUNT} + ${TOTALSPAMCOUNT}`
		echo "TOTAL_SYS_SPAMCOUNT (post): '${TOTAL_SYS_SPAMCOUNT}'"
		TOTAL_SYS_NEWSPAMCOUNT=`expr ${TOTAL_SYS_NEWSPAMCOUNT} + ${NEWSPAMCOUNT}`
		echo "TOTAL_SYS_NEWSPAMCOUNT: '${TOTAL_SYS_NEWSPAMCOUNT}'"
		TOTAL_SYS_OLDSPAMCOUNT=`expr ${TOTAL_SYS_OLDSPAMCOUNT} + ${OLDSPAMCOUNT}`
		echo "TOTAL_SYS_OLDSPAMCOUNT: '${TOTAL_SYS_OLDSPAMCOUNT}'"
		fi;
                cd -
        done;
 
	#TOTAL_SYS_SPAMCOUNT=`expr ${TOTAL_SYS_NEWSPAMCOUNT} + ${TOTAL_SYS_OLDSPAMCOUNT}`
	echo "TOTAL_SYS_SPAMCOUNT: '${TOTAL_SYS_SPAMCOUNT}'"
	#echo "OLDDATE: '${OLDDATE}'"
	echo "hier ;-)"
 
        ##########################################################
        # Send Mail to UKBW Anti-Spam-System <antispam@uk-bw.de> #
        ##########################################################
#	SYS_MAILFILE=`touch ${TMPDIR}/antispam-system.mail`
	SYS_MAILFILE="${TMPDIR}/antispam-system.mail"
        echo "To: Team Netzwerke Stuttgart <netzwerke-s@uk-bw.de>" >> ${SYS_MAILFILE}
	echo "CC: Susann Meissner <susann.meissner@uk-bw.de>" >> ${SYS_MAILFILE}
        echo "Subject: SPAM-Reports (${DATENOW})" >> ${SYS_MAILFILE}
        echo "Return-Path: UKBW Hostmaster <hostmaster@uk-bw.de>" >> ${SYS_MAILFILE}
        echo "From: UKBW Hostmaster <hostmaster@uk-bw.de>" >> ${SYS_MAILFILE}
        echo "Errors-To: hostmaster@uk-bw.de" >> ${SYS_MAILFILE}
        echo "" >> ${SYS_MAILFILE}
        echo "" >> ${SYS_MAILFILE}
	echo "----------------------------------------------------------------------------------------" >> ${SYS_MAILFILE}
        echo "" >> ${SYS_MAILFILE}
        echo " Gesamt-Status-Report fuer den Zeitraum vom ${OLDDATE} bis ${DATENOW}" >> ${SYS_MAILFILE}
        echo "" >> ${SYS_MAILFILE}
	echo "----------------------------------------------------------------------------------------" >> ${SYS_MAILFILE}
        echo "" >> ${SYS_MAILFILE}
	echo " Gesamtanzahl neuer Nachrichten mit Spam-Status: ${TOTAL_SYS_NEWSPAMCOUNT=}" >> ${SYS_MAILFILE}
	echo " Gesamtanzahl alter Nachrichten mit Spam-Status: ${TOTAL_SYS_OLDSPAMCOUNT=}" >> ${SYS_MAILFILE}
	echo " Gesamtanzahl aller Nachrichten mit Spam-Status: ${TOTAL_SYS_SPAMCOUNT}" >> ${SYS_MAILFILE}
	echo "----------------------------------------------------------------------------------------" >> ${SYS_MAILFILE}
        echo "" >> ${SYS_MAILFILE}
	cat ${SYSPROTOFILE} >> ${SYS_MAILFILE}
 
###	Mail waehrend Test-Phase nicht senden, chhaas:
	cat ${SYS_MAILFILE} | sendmail -t -F "UKBW Hostmaster <hostmaster@uk-bw.de>" -oi
 
        #
        # Delete TMPDIR and move Lastrunfile to place
        #
        # rm -rf $TMPDIR
### debug:
###        mv ${LASTRUNFILE}".new" ${LASTRUNFILE}
fi;
 
echo "Exitcode: $?"
exit


spamreport_footer.txt
==============================================================================
Spamreport-Footer chhaas, UKBW 2007
spamreport_header.txt
Ihr freundlicher Mail-Admin



zurück