blob: 3ab29fb081e3b4bbb66cec2230eeae50acc9032d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
#
# Notes:
# chmod 1755 qmail-queue.scan; chown qmailq:qmail qmail-queue.scan
# SQMAIL/tmp has to exist prior with owner qmaill:qmail
#
# Usage:
# Generate: control/spamdomains
# Input: recipient-domain:spam-threshold
# Sample: example.com:8
# allspams:0
# #testdomain:11 # entries with leading '#' are disregarded
#
# Caution:
# This script is a sample. Depending on your anti-virus and/or anti-spam
# software, some heavy tweaking is required.
#
#
# Dependencies:
# Korn shell
# DJB's MESS822 package (or not)
#
# Environment variables used:
#
# $MAILFROM -- set by qmail-smtpd
# $RCPTTO -- set by qmail-smtpd
#
# Virus scanner:
# The AV scanner returns '0' if ok -- or '1'/'2' if virus -- anything else = error
#
# Spam scanner:
# SpamAssassin > 3.0 (spamc/spamd)
#
# Output:
# RC=0; if ok
# RC=32; if virus
# RC=33; if spam
# RC=81; if error
#
# Performance:
# Put SQMAIL/tmp on a RAMDISK
#
# License:
# Public Domain
#
# Author:
# Dr. Erwin Hoffmann - FEHCom
#
# Version:
# 0.9.7
#
#------------------------------------------------------------------------------------------------
SQMAIL=HOME
#
alias -x SCANNER='/usr/local/bin/clamdscan'
#alias -x SCANNER='/etc/iscan/vscan'
alias -x SPAMMER='/usr/bin/spamc'
alias -x 822FIELD='/usr/local/bin/822field'
# alias -x 822FIELD='/usr/bin/grep'
#
VERBOSE=0
#
SCANNERARGS="--no-summary"
SPAMMERARGS=""
#
## No code change necessary from here
#
typeset SPAM
integer SPAMC=0
integer SPAMTHRESHOLD=-1
integer SPAMTH
typeset SPAMDOMAINS
ID="${RANDOM}$$"
MESSAGE="${SQMAIL}/tmp/msg.${ID}"
export DTLINE="spam-queue"
#
[[ ! -d ${SQMAIL}/tmp ]] && exit 53
cat > ${MESSAGE} || exit 53
#
## Virus scanning
#
if [[ "x${QHPSI}" = "x" ]]; then
VIRUS=$(SCANNER ${SCANNERARGS} ${MESSAGE})
VRC=$?
[[ ${VERBOSE} -gt 0 ]] && print -u2 "AV Scanner info [`SCANNER` -V]: ${VIRUS}"
# VIRUS=$(echo "${VIRUS}" | grep -e "\*\*") ## for TrendMicro only
case ${VRC} in
(0) RC=0;;
(1|2) exec 1>&2; echo "Infected email not delivered (${VIRUS})"; RC=32;;
(*) exec 1>&2; echo "`SCANNER -V` internal error (${VRC})"; RC=81;;
esac
fi
#
## Check Spamlevel for each domain
#
if [[ -f ${SQMAIL}/control/spamdomains && ${RC} -eq 0 ]]; then
SPAMDOMAINS=$(grep -v "^#" ${SQMAIL}/control/spamdomains)
for LINE in ${SPAMDOMAINS}
do
DOMAIN=${LINE%:*}
SPAMTH=${LINE#*:*}
[[ $(echo "${RCPTTO}" | grep -ci "${DOMAIN}") -gt 0 ]] && SPAMTHRESHOLD=${SPAMTH}
done
[[ ${VERBOSE} -gt 0 ]] && print -u2 "Rcptdomain: ${RCPTTO#*@} -- Threshold: ${SPAMTHRESHOLD}"
if [[ ${SPAMTHRESHOLD} -ge 0 ]]; then
#
## Spam recognition -- the following codes is only useful for SpamAssassins spamc version 3.x
#
SPAM=$(SPAMMER ${SPAMMERARGS} < ${MESSAGE} > ${MESSAGE}_$$ && mv ${MESSAGE}_$$ ${MESSAGE} || exit 53)
SPAM=$(822FIELD "X-Spam-Level" < ${MESSAGE} | head -n 1)
SPAM=${SPAM# }
[[ ${VERBOSE} -gt 0 ]] && print -u2 echo "[$(echo `SPAMMER -V` | tr -d '\n')]: ${SPAMTHRESHOLD}"
if [[ "x${SPAM}" != "x" ]]; then
if [[ $(echo "${SPAM}" | grep -c "X-Spam-Level") -gt 0 ]]; then
SPAMC=$(echo "${SPAM##X-Spam-Level:}" tr -d ' ' | tr -d '\n' | wc -c)
fi
else
SPAMC=$(echo "${SPAM}" | awk -F"/" '{print $1}' | awk -F"." '{print $1}')
fi
[[ ${VERBOSE} -gt 0 ]] && print -u2 "Spam: ${SPAM} - Spamc: ${SPAMC}"
#
## Spam rejection
#
if [[ ${SPAMC} -gt 0 && ${SPAMC} -gt ${SPAMTHRESHOLD} ]]; then
#
# If you enable one of the next lines, the sender receives a rejection, while the email is let thru
#
# ${SQMAIL}/bin/forward ${MAILFROM} "${RCPTTO}" < ${MESSAGE}
# ${SQMAIL}/bin/forward ${MAILFROM} "${DELIVERTO}" < ${MESSAGE}
export SPAMSCORE="${SPAMC}"
RC=33
fi
fi
fi
[[ ${RC} -eq 0 ]] && ${SQMAIL}/bin/qmail-queue < ${MESSAGE}
rm ${MESSAGE}
exit ${RC}
|