forked from msimerson/Mail-Toaster-6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmt6-mailfilter.txt
265 lines (237 loc) · 7.29 KB
/
mt6-mailfilter.txt
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
SHELL="/bin/sh"
import EXT
import HOST
VHOME=`pwd`
TIMESTAMP=`date "+%b %d %H:%M:%S"`
##
# title: mailfilter-site
# author: Matt Simerson
# version 3.1
#
# Usage: Install this file in /etc/mailfilter.
#
# Create a .qmail file in each users Maildir as follows:
# echo "| /usr/local/bin/maildrop /etc/mailfilter" > ~vpopmail/domains/example.com/user/.qmail
#
# Qmailadmin v1.0.26 or higher does that automatically with
# options --enable-modify-spam and --enable-spam-command.
#
# Environment Variables from qmail-local:
# SENDER is the envelope sender address
# NEWSENDER is the forwarding envelope sender address
# RECIPIENT is the envelope recipient address, local@domain
# USER is user
# HOME is your home directory
# HOST is the domain part of the recipient address
# LOCAL is the local part
# EXT is the address extension, ext.
# HOST2 is the portion of HOST preceding the last dot
# HOST3 is the portion of HOST preceding the second-to-last dot
# HOST4 is the portion of HOST preceding the third-to-last dot
# EXT2 is the portion of EXT following the first dash
# EXT3 is the portion following the second dash;
# EXT4 is the portion following the third dash.
# DEFAULT is the portion corresponding to the default part of the .qmail-... file name
# DEFAULT is not set if the file name does not end with default
# DTLINE and RPLINE are the usual Delivered-To and Return-Path lines, including newlines
#
# qmail-local will call maildrop. The exit codes that qmail-local understands are:
# 0 - delivery is complete
# 111 - temporary error
# xxx - unknown failure
##
#logfile "/var/log/mail/maildrop.log"
log "$TIMESTAMP - BEGIN maildrop processing for $EXT@$HOST ==="
# sysadmin blunders can cause EXT or HOST to be unset.
# test and make assure things are not too messed up.
#
# By exiting with error 111, the error will be logged, giving an admin
# the chance to notice and fix the problem before the message bounces.
if ( $EXT eq "" )
{
log " FAILURE: EXT is not a valid value"
log "=== END === $EXT@$HOST failure (EXT variable not imported)"
EXITCODE=111
exit
}
if ( $HOST eq "" )
{
log " FAILURE: HOST is not a valid value"
log "=== END === $EXT@$HOST failure (HOST variable not imported)"
EXITCODE=111
exit
}
##
# Include user rules, for overrides of the the sitewide mailfilter
#
# this is also the "suggested" way to set individual values
# for maildrop such as quota.
##
`test -r $VHOME/.mailfilter`
if( $RETURNCODE == 0 )
{
log " including $VHOME/.mailfilter"
exception {
include $VHOME/.mailfilter
}
}
##
# if it does not exist, create the maildirsize file
# (can also be done via "deliverquota [email protected] 10MS,1000C)
##
`test -e $VHOME/Maildir/maildirsize`
if( $RETURNCODE == 1)
{
VUSERINFO="/usr/local/vpopmail/bin/vuserinfo"
`test -x $VUSERINFO`
if ( $RETURNCODE == 0)
{
log " creating $VHOME/Maildir/maildirsize for quotas"
`$VUSERINFO -Q $EXT@$HOST`
`test -s "$VHOME/Maildir/maildirsize"`
if ( $RETURNCODE == 0 )
{
`/usr/sbin/chown vpopmail:vchkpw $VHOME/Maildir/maildirsize`
`/bin/chmod 640 $VHOME/Maildir/maildirsize`
}
}
else
{
log " WARNING: cannot find vuserinfo! Please edit mailfilter"
}
}
##
# Set MAILDIRQUOTA. If not set, maildrop and deliverquota
# will not enforce quotas for message delivery.
##
`test -e $VHOME/Maildir/maildirsize`
if( $RETURNCODE == 0)
{
MAILDIRQUOTA=`/usr/bin/head -n1 $VHOME/Maildir/maildirsize`
}
# if the user does not have a Spam folder, create it.
`test -d $VHOME/Maildir/.Spam`
if( $RETURNCODE == 1 )
{
MAILDIRMAKE="/usr/local/bin/maildirmake"
`test -x $MAILDIRMAKE`
if ( $RETURNCODE == 1 )
{
MAILDIRMAKE="/usr/local/bin/maildrop-maildirmake"
`test -x $MAILDIRMAKE`
}
if ( $RETURNCODE == 1 )
{
log " WARNING: no maildirmake!"
}
else
{
log " creating $VHOME/Maildir/.Spam "
`$MAILDIRMAKE -f Spam $VHOME/Maildir`
}
}
##
# The message should be tagged, lets bag it.
##
# HAM: X-Spam-Status: No, score=-2.6 required=5.0
# SPAM: X-Spam-Status: Yes, score=8.9 required=5.0
#
# Note: SA < 3.0 uses "hits" instead of "score"
#
# if ( /^X-Spam-Status: *Yes/) # test if spam status is yes
# The following regexp matches any spam message and sets the
# variable $MATCH2 to the spam score.
if ( /X-Spam-Status: Yes/:h)
{
if ( /X-Spam-Status: Yes, (hits|score)=([\d\.\-]+)\s/:h)
{
# if the message scored a 12 or higher, then there is no point in
# keeping it around. SpamAssassin already knows it as spam, and
# has already "autolearned" from it if you have that enabled. The
# end user likely does not want it. If you wanted to cc it, or
# deliver it elsewhere for inclusion in a spam corpus, you could
# easily do so with a cc or xfilter command
if ( $MATCH2 >= 12 ) # from Adam Senuik post to mail-toasters
{
log " SPAM: score $MATCH2 exceeds 12: nuking message!"
log "=== END === $EXT@$HOST success (discarded)"
EXITCODE=0
exit
}
log " SPAM: score $MATCH2: delivering to $VHOME/Maildir/.Spam"
log "=== END === $EXT@$HOST success"
exception {
to "$VHOME/Maildir/.Spam"
}
}
else
{
log " SpamAssassin regexp match error!"
}
}
if ( /^X-Spam-Status: No, (score|hits)=([\d\.\-]+)\s/:h)
{
log " message is SA clean ($MATCH2)"
}
##
# Include any other rules that the user might have from
# sqwebmail or other compatible program
##
`test -r $VHOME/Maildir/.mailfilter`
if( $RETURNCODE == 0 )
{
log " including $VHOME/Maildir/.mailfilter"
exception {
include $VHOME/Maildir/.mailfilter
}
}
`test -r $VHOME/Maildir/mailfilter`
if( $RETURNCODE == 0 )
{
log " including $VHOME/Maildir/mailfilter"
exception {
include $VHOME/Maildir/mailfilter
}
}
log " delivering to $VHOME/Maildir"
# make sure the deliverquota binary exists and is executable
# if not, then we cannot enforce quotas. If we do not check
# and the binary is missing, maildrop silently discards mail.
DELIVERQUOTA="/usr/local/bin/deliverquota"
`test -x $DELIVERQUOTA`
if ( $RETURNCODE == 1 )
{
DELIVERQUOTA="/usr/local/bin/maildrop-deliverquota"
`test -x $DELIVERQUOTA`
}
if ( $RETURNCODE == 1 )
{
log " WARNING: no deliverquota!"
log "=== END === $EXT@$HOST success"
exception {
to "$VHOME/Maildir"
}
}
else
{
exception {
xfilter "$DELIVERQUOTA -w 90 $VHOME/Maildir"
}
##
# check to make sure the message was delivered
# returncode 77 means that out maildir was overquota - bounce mail
##
if( $RETURNCODE == 77)
{
#log " BOUNCED: bouncesaying '$EXT@$HOST is over quota'"
log "=== END === $EXT@$HOST bounced"
to "|/var/qmail/bin/bouncesaying '$EXT@$HOST is over quota'"
}
else
{
log "=== END === $EXT@$HOST success (quota)"
EXITCODE=0
exit
}
}
log "WARNING: This message should never be printed!"