Utoljára aktív 6 months ago

spamsink.sh Eredeti
1# TODO
2# * deliver over lmtp to cyrus [DONE]
3# * verify reciep check (include "revieced for") [DONE]
4# * pipe mail to sa-learn [DONE]
5# * pipe mail to dcc
6# * pipe mail to razor [DONE]
7# * blacklist anywhere
8# * dump mail into file
9
10# initial some values
11exit=1
12i=1
13tempfilename="/tmp/mailsink"
14reciep_detection=0
15removetemps=0
16targets="spamfalle.info"
17deliver_to_mailbox=0
18sa_report=0
19razor_report=0
20razor_options="-conf=/etc/razor/razor-agent.conf"
21mailbox="user.spamfalle"
22auth="spamfalle"
23
24# unique filename
25tempfile=${tempfilename}`date +%s`
26
27while read j ; do
28 echo "${j}" >> ${tempfile}
29done
30# deliver mail into mailbox
31if [ ${deliver_to_mailbox} -eq "1" ] ; then
32 cat ${tempfile} | formail -I"From " | cyrdeliver -d -m ${mailbox} -a ${auth}
33fi
34# report mail as spam to spamassassin
35if [ ${sa_report} -eq "1" ] ; then
36 sa-learn --spam ${tempfile}
37fi
38# report mail as spam to razor
39if [ ${razor_report} -eq "1" ] ; then
40 razor-report ${razor_options} ${tempfile}
41fi
42# reciep_detection
43if [ ${reciep_detection} -eq "1" ] ; then
44 # get reciep
45 to=`egrep -i "^To:.*\ <?.*\@.*\..*>?$" ${tempfile} | tail -1 | sed "s/^to:.* <\?//i" | sed "s/>$//"`
46 # check if mail is send to target
47 for jj in ${targets}; do
48 if [ `echo "${to}" | egrep -i "${jj}"` ] ; then
49 exit=0
50 fi
51 done
52 if [ ${exit} -eq "1" ] ; then
53 forcount=`egrep -c "^for\ <?.*\@.*\..*>?;\ ((Mon)|(Tue)|(Wed)|(Thu)|(Fri)|(Sat)|(Sun)),\ (([\ 0][1-9])|([1-2][0-9])|(3[0-1]))\ ((Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec))\ [1-2][0-9]{3}"`
54 jjj=1
55 while [ ${jjj} -le ${forcount} ] ; do
56 for[$jjj]=`egrep -c "^for\ <?.*\@.*\..*>?;\ ((Mon)|(Tue)|(Wed)|(Thu)|(Fri)|(Sat)|(Sun)),\ (([\ 0][1-9])|([1-2][0-9])|(3[0-1]))\ ((Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec))\ [1-2][0-9]{3}"`
57 jjj=$[$jjj+1]
58 done
59 for jj in ${targets}; do
60 jjj=1
61 while [ "${jjj}" -le "${forcount}" && "${exit}" -eq "1" ] ; do
62 if [ `echo "${for[$jjj]}" | egrep -i "${jj}"` ] ; then
63 exit=0
64 fi
65 jjj=$[$jjj+1]
66 done
67 done
68 fi
69 if [ ${exit} -eq "1" ] ; then
70 exit 1
71 fi
72fi
73# count relays
74relaycount=`grep -c ^Received ${tempfile}`
75# write lines matching "^Received:" into array
76while [ ${i} -le ${relaycount} ] ; do
77 relay[$i]=`grep -i "^Received:" ${tempfile} | tail -${i} | head -1 | sed "s/^Received: .*(//i" | sed "s/).*//" | sed "s/.*\[//" | sed "s/\].*//"`
78 i=$[$i+1]
79done
80i=1
81# get IP of first nonrfc1938 IP
82while [ ${i} -le ${relaycount} ] ; do
83 relayhost=${relay[$i]}
84 i=$[$i+1]
85 if ! [ `echo "${relayhost}" | egrep "^127\.0\.0|^192\.168|^10|^172\.1[6-9]|^172\.2|^172\.3[0-1]|^169\.254"` ] ; then
86 i=$[${relaycount}+1]
87 fi
88done
89# ensure $relayhost is realy an IP
90relayhost=`echo "${relayhost}" | egrep "^([12]?[0-9]?[0-9].){3}([12]?[0-9]?[0-9])$"`
91# get returnpath
92returnpath=`egrep -i "^Return-Path:.*\ <?.*\@.*\..*>?$" ${tempfile} | tail -1 | sed "s/^Return-Path:.* <\?//i" | sed "s/>$//"`
93# get from
94from=`egrep -i "^From:.*\ <?.*\@.*\..*>?$" ${tempfile} | tail -1 | sed "s/^From:.* <\?//i" | sed "s/>$//"`
95# save subject for mail, give a hint if return path differs
96if [ ${from} = ${returnpath} ] ; then
97 reportsubject="Spamsink Mail - Sender: ${from} Relay: ${relayhost}"
98else
99 reportsubject="Spamsink Mail - Sender: ${from} Return-Path: ${returnpath} Relay: ${relayhost}"
100fi
101# cat ${tempfile} | mail -s ${reportsubject} waja@cyconet.org
102echo ${reportsubject}
103if [ ${removetemps} -eq "1" ] ; then
104 rm ${tempfile}
105fi
106exit 0