-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathmodify-spam.pl
executable file
·309 lines (283 loc) · 9.92 KB
/
modify-spam.pl
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/local/bin/perl
=head1 modify-spam.pl
Change spam filtering and delivery settings for a virtual server
This command can be used to change the location than email tagged as spam or
virus-laden is delivered to for one or more virtual servers. The servers to act
on can be either specified with the C<--domain> parameter, or all domains with
spam filtering enabled can be selected with C<--all-domains>.
The following parameters control what happens to messages identified as spam :
C<--spam-delete> Delete all spam mail.
C<--spam-deliver> Deliver to user's mailbox normally.
C<--spam-mailfile> Write to user's C<~/mail/spam> file in mbox format.
C<--spam-maildir> Write to user's C<~/Maildir/.spam> file in Maildir format.
C<--spam-file> Write to the file following this parameter, which must be relative to the user's home directory.
C<--spam-email> Forward to the email address following this parameter.
C<--spam-dest> Write to the absolute file following the parameter.
A similar set of options exist for virus filtering, but starting with C<--virus>
instead of C<--spam>.
Virtualmin has the ability to delete spam from the spam folders of all users
in a domain once it passes some threshold, such as age or size. You can
enable this with the C<--spamclear-days> parameter followed by the maximum
age in days, or C<--spamclear-size> followed by a size in bytes. Or to turn
off spam deletion, use the C<--spamclear-none> parameter.
Similarly you can enable automatic deletion from trash folders with the
C<--trashclear-days> parameter followed by the maximum age in days, or
C<--trashclear-size> followed by a size in bytes. Or to turn off trash
deletion, use the C<--trashclear-none> parameter.
SpamAssassin gives each message it scans a numeric score, and typically anything
above 5 is considered spam and placed in a separate user folder. However, you
can choose to simply delete all incoming spam with a score above some higher
threshold (such as 10) using the C<--spam-delete-level> parameter, which must
be followed by a number. To turn this behaviour off again, use the
C<--spam-no-delete-level> flag.
To enable the spamtrap and hamtrap aliases for the selected virtual servers,
you can use the C<--spamtrap> command-line flag. Similarly, to remove them
use the C<--no-spamtrap> flag. When enabled, users will be able to forward
spam to [email protected] for adding to the domain's blacklist.
=cut
package virtual_server;
if (!$module_name) {
$main::no_acl_check++;
$ENV{'WEBMIN_CONFIG'} ||= "/etc/webmin";
$ENV{'WEBMIN_VAR'} ||= "/var/webmin";
if ($0 =~ /^(.*)\/[^\/]+$/) {
chdir($pwd = $1);
}
else {
chop($pwd = `pwd`);
}
$0 = "$pwd/modify-spam.pl";
require './virtual-server-lib.pl';
$< == 0 || die "modify-spam.pl must be run as root";
}
&licence_status();
@OLDARGV = @ARGV;
$config{'spam'} || &usage("Spam filtering is not enabled for Virtualmin");
&set_all_text_print();
# Parse command-line args
$spamlevel = undef;
$auto = { };
while(@ARGV > 0) {
local $a = shift(@ARGV);
if ($a eq "--domain") {
push(@dnames, shift(@ARGV));
}
elsif ($a eq "--all-domains") {
$all_doms = 1;
}
elsif ($a =~ /^--(spam|virus)-delete$/) {
$mode{$1} = 0;
}
elsif ($a =~ /^--(spam)-deliver$/) {
$mode{$1} = 5;
}
elsif ($a =~ /^--(spam|virus)-(normal|mailfile)$/) {
$m = $1;
$mode{$m} = 4;
if (@ARGV && $ARGV[0] !~ /^-/) {
$dest{$m} = shift(@ARGV);
}
}
elsif ($a =~ /^--(spam|virus)-maildir$/) {
$m = $1;
$mode{$m} = 6;
if (@ARGV && $ARGV[0] !~ /^-/) {
$dest{$m} = shift(@ARGV);
}
}
elsif ($a =~ /^--(spam|virus)-file$/) {
$mode{$1} = 1;
$dest{$1} = shift(@ARGV);
$dest{$1} =~ /\S/ && $dest{$1} !~ /\.\./ &&
$dest{$1} !~ /^\// ||
&usage("The $a option must be followed by a relative filename");
}
elsif ($a =~ /^--(spam|virus)-email$/) {
$mode{$1} = 2;
$dest{$1} = shift(@ARGV);
$dest{$1} =~ /\@/ || &usage("The $a option must be followed by an email address");
}
elsif ($a =~ /^--(spam|virus)-dest$/) {
$mode{$1} = 3;
$dest{$1} = shift(@ARGV);
$dest{$1} =~ /\S/ || &usage("The $a option must be followed by a mail file path");
}
elsif ($a eq "--spam-whitelist") {
$spam_white = 1;
}
elsif ($a eq "--no-spam-whitelist") {
$spam_white = 0;
}
elsif ($a =~ /^--use-(spamc|spamassassin)$/) {
$spam_client = $1;
}
elsif ($a eq "--spamclear-none") {
$auto->{'days'} = $auto->{'size'} = undef;
}
elsif ($a eq "--spamclear-days") {
$auto->{'days'} = shift(@ARGV);
$auto->{'days'} =~ /^\d+$/ ||
&usage("The $a option must be followed by a number of days");
$auto->{'size'} = undef;
}
elsif ($a eq "--spamclear-size") {
$auto->{'size'} = shift(@ARGV);
$auto->{'size'} =~ /^\d+$/ ||
&usage("The $a option must be followed by a size in bytes");
$auto->{'days'} = undef;
}
elsif ($a eq "--trashclear-none") {
$auto->{'trashdays'} = $auto->{'trashsize'} = undef;
}
elsif ($a eq "--trashclear-days") {
$auto->{'trashdays'} = shift(@ARGV);
$auto->{'trashdays'} =~ /^\d+$/ ||
&usage("The $a option must be followed by a number of days");
$auto->{'trashsize'} = undef;
}
elsif ($a eq "--trashclear-size") {
$auto->{'trashsize'} = shift(@ARGV);
$auto->{'trashsize'} =~ /^\d+$/ ||
&usage("The $a option must be followed by a size in bytes");
$auto->{'trashdays'} = undef;
}
elsif ($a eq "--spam-delete-level") {
$spamlevel = shift(@ARGV);
$spamlevel =~ /^[1-9]\d*$/ ||
&usage("--spam-delete-level must be followed by a number");
}
elsif ($a eq "--spam-no-delete-level") {
$spamlevel = 0;
}
elsif ($a =~ /^--use-(clamscan|clamdscan|clamd-stream-client|clamdscan-remote)$/) {
$virus_scanner = $1;
}
elsif ($a eq "--spamtrap") {
$spamtrap = 1;
}
elsif ($a eq "--no-spamtrap") {
$spamtrap = 0;
}
elsif ($a eq "--multiline") {
$multiline = 1;
}
elsif ($a eq "--help") {
&usage();
}
else {
&usage("Unknown parameter $a");
}
}
@dnames || $all_doms || usage("No domains specified");
defined($mode{'spam'}) || defined($mode{'virus'}) || $spam_client ||
$virus_scanner || defined($auto) || defined($spamlevel) ||
defined($spamtrap) || &usage("Nothing to do");
# Get domains to update
if ($all_doms) {
@doms = grep { $_->{'spam'} } &list_domains();
}
else {
foreach $n (@dnames) {
$d = &get_domain_by("dom", $n);
$d || &usage("Domain $n does not exist");
$d->{'spam'} || &usage("Virtual server $n does not have spam filtering enabled");
push(@doms, $d);
}
}
# Do it for all domains
foreach $d (@doms) {
&$first_print("Updating server $d->{'dom'} ..");
&obtain_lock_spam($d);
&obtain_lock_cron($d);
&obtain_lock_mail($d);
&$indent_print();
if ($config{'spam'} && $d->{'spam'} &&
(defined($mode{'spam'}) || defined($spamlevel))) {
&save_domain_spam_delivery($d, $mode{'spam'}, $dest{'spam'},
$spamlevel, undef);
}
if ($config{'virus'} && $d->{'virus'} && defined($mode{'virus'})) {
&save_domain_virus_delivery($d, $mode{'virus'}, $dest{'virus'});
}
if ($config{'spam'} && $d->{'spam'} && $spam_client) {
&save_domain_spam_client($d, $spam_client);
&modify_webmin($d, $d);
}
if (defined($spam_white)) {
if (&get_domain_spam_client($d) eq "spamc") {
&$second_print(".. whitelist cannot be configured ".
"spamc is in use");
}
else {
$d->{'spam_white'} = $spam_white;
&update_spam_whitelist($d);
&save_domain($d);
}
}
if (keys %$auto) {
my $oldauto = &get_domain_spam_autoclear($d);
foreach my $k (keys %$auto) {
$oldauto->{$k} = $auto->{$k};
}
&save_domain_spam_autoclear($d, $oldauto);
}
if (defined($virus_scanner)) {
&save_domain_virus_scanner($d, $virus_scanner);
}
if (defined($spamtrap)) {
$st = &get_spamtrap_aliases($d);
if ($st < 0) {
&$first_print("Spam trap aliases already exist");
}
elsif ($st && !$spamtrap) {
&$first_print("Removing spam trap aliases ..");
$err = &delete_spamtrap_aliases($d);
&$second_print($err ? ".. failed : $err" : ".. done");
}
elsif (!$st && $spamtrap) {
&$first_print("Adding spam trap aliases ..");
$err = &setup_spamtrap_aliases($d);
&$second_print($err ? ".. failed : $err" : ".. done");
}
}
&$outdent_print();
&release_lock_mail($d);
&release_lock_spam($d);
&release_lock_cron($d);
&$second_print(".. done");
}
&run_post_actions();
&virtualmin_api_log(\@OLDARGV);
sub usage
{
print "$_[0]\n\n" if ($_[0]);
print "Changes the spam and virus delivery modes for one or more domains.\n";
print "\n";
print "virtualmin modify-spam --domain name | --all-domains\n";
print " [--spam-delete | --spam-deliver |\n";
print " --spam-mailfile [folder] |\n";
print" --spam-file file-under-home |\n";
print " --spam-email address | --spam-dest file |\n";
print " --spam-maildir [folder] ]\n";
print " [--spam-delete-level score | --spam-no-delete-level]\n";
print " [--virus-delete |\n";
print " --virus-mailfile [folder] |\n";
print " --virus-file file-under-home |\n";
print " --virus-email address | --virus-dest file\n";
print " --virus-maildir [folder] ]\n";
print " [--spam-whitelist | --no-spam-whitelist]\n";
print " [--use-spamassassin | --use-spamc]\n";
print " [--spamclear-none |\n";
print " --spamclear-days days\n";
print " --spamclear-size bytes]\n";
print " [--trashclear-none |\n";
print " --trashclear-days days\n";
print " --trashclear-size bytes]\n";
print " [--use-clamscan | --use-clamdscan |\n";
print " --use-clamd-stream-client | --use-clamdscan-remote]\n";
print " [--spamtrap | --no-spamtrap]\n";
print "\n";
print "Warning - modifying the SpamAssassin or virus scanning client for\n";
print "individual domains is deprecated.\n";
exit(1);
}