-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathfeature-spam.pl
executable file
·1920 lines (1779 loc) · 50.7 KB
/
feature-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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Functions for turning SpamAssassin filtering on or off on a per-domain basis
sub init_spam
{
$domain_lookup_cmd = "$module_config_directory/lookup-domain.pl";
$procmail_spam_dir = "$module_config_directory/procmail";
$spam_config_dir = "$module_config_directory/spam";
$quota_spam_margin = 5*1024*1024;
$spamassassin_lock_file = "/tmp/virtualmin.spamassassin";
}
sub require_spam
{
return if ($require_spam++);
&foreign_require("procmail");
&foreign_require("spam");
}
sub check_depends_spam
{
if (!$_[0]->{'mail'}) {
# Mail must be enabled for spam filtering to work!
return $text{'setup_edepspam'};
}
return undef;
}
# setup_spam(&domain)
# Adds the master procmail entry for domain-specific spam filtering, plus an
# include file for this domain.
sub setup_spam
{
my ($d) = @_;
&$first_print($text{'setup_spam'});
&require_spam();
&foreign_require("cron");
# Create the needed directories now, so we can lock files in them
if (!-d $procmail_spam_dir) {
&make_dir($procmail_spam_dir, 0755);
&set_ownership_permissions(undef, undef, 0755, $procmail_spam_dir);
}
if (!-d $spam_config_dir) {
&make_dir($spam_config_dir, 0755);
&set_ownership_permissions(undef, undef, 0755, $spam_config_dir);
}
local $spamdir = "$spam_config_dir/$d->{'id'}";
&make_dir($spamdir, 0750);
&set_ownership_permissions($d->{'uid'}, $d->{'gid'}, 0750, $spamdir);
&obtain_lock_spam($d);
&obtain_lock_cron($d);
# Add the procmail entry to get the VIRTUALMIN variable
local @recipes = &procmail::get_procmailrc();
local ($r, $gotvirt, $gotdef);
foreach $r (@recipes) {
if ($r->{'type'} eq '=' &&
$r->{'action'} =~ /^VIRTUALMIN=/) {
$gotvirt++;
}
elsif ($r->{'name'} eq "DEFAULT") {
$gotdef++;
}
}
if (!$gotvirt) {
# Need to add entries to lookup the domain, and run it's include file
local $var1 = { 'flags' => [ 'w', 'i' ],
'conds' => [ ],
'type' => '=',
'action' => "VIRTUALMIN=|$domain_lookup_cmd \$LOGNAME" };
local $testcmd = &has_command("test") || "test";
local $var2 = { 'flags' => [ ],
'conds' => [ [ "?", "$testcmd \"\$VIRTUALMIN\" != \"\"" ] ],
'block' => "INCLUDERC=$procmail_spam_dir/\$VIRTUALMIN",
};
if ($gconfig{'os_type'} eq 'solaris') {
# Need to call sh as shell explicitly
$var2->{'conds'} =
[ [ "?", "sh -c \"$testcmd '\$VIRTUALMIN' != ''\"" ] ];
}
# If the procmailrc file is empty, add at the end.
# If there is a TRAP variable, add after it (so we do logging properly)
# Otherwise, add at the top
if (@recipes) {
# Has some recipes .. check if there is a TRAP
local ($trap, $aftertrap);
for(my $i=0; $i<@recipes; $i++) {
if ($recipes[$i]->{'name'} eq 'TRAP') {
$trap = $recipes[$i];
$trapafter = $recipes[$i+1];
}
}
if ($trapafter) {
# Add before the recipe that is after TRAP
&procmail::create_recipe_before($var1, $trapafter);
&procmail::create_recipe_before($var2, $trapafter);
}
elsif ($trap) {
# Nothing after TRAP, so just add at end
&procmail::create_recipe($var1);
&procmail::create_recipe($var2);
}
else {
# Just add at start
&procmail::create_recipe_before($var1, $recipes[0]);
&procmail::create_recipe_before($var2, $recipes[0]);
}
}
else {
&procmail::create_recipe($var1);
&procmail::create_recipe($var2);
}
}
# Add procmail rule to bounce mail if quota is full
&setup_quota_full_bounce();
# Create the lookup-domain.pl wrapper script, and hack it to turn off setuid
&cron::create_wrapper($domain_lookup_cmd, $module_name,
"lookup-domain.pl");
local $lref = &read_file_lines($domain_lookup_cmd);
splice(@$lref, 1, 0, "delete(\$ENV{'IFS'});",
"delete(\$ENV{'CDPATH'});",
"delete(\$ENV{'ENV'});",
"delete(\$ENV{'BASH_ENV'});",
"\$ENV{'PATH'} = '/bin:/usr/bin';",
"\$< = \$>;",
"\$( = \$);");
&flush_file_lines($domain_lookup_cmd);
# Build spamassassin command to call
local $cmd = &spamassassin_client_command($d);
# Create recipes to call spamassassin
local $spamrc = "$procmail_spam_dir/$d->{'id'}";
local $recipe0 = { 'name' => 'DROPPRIVS', # Run all commands as user
'value' => 'yes' };
local @conds;
if ($cmd =~ /spamassassin/ && $config{'spam_size'}) {
# Add condition for max message size
push(@conds, [ '<', $config{'spam_size'} ]);
}
local $recipe1 = { 'flags' => [ 'f', 'w' ], # Call spamassassin
'conds' => \@conds,
'type' => '|',
'action' => $cmd,
};
if ($config{'spam_lock'}) {
# Add locking to prevent concurrent runs
$recipe1->{'lockfile'} = $spamassassin_lock_file;
}
local ($recipe2, $recipe3);
local $varon = { 'name' => 'SPAMMODE', 'value' => 1 };
if ($config{'spam_level'}) {
# Recipe to delete high-score spam
local $stars = join("", map { "\\*" } (1..$config{'spam_level'}));
$recipe3 = { 'flags' => [ ],
'conds' => [ [ '', '^X-Spam-Level: '.$stars ] ],
'action' => '/dev/null' };
}
if ($config{'spam_delivery'}) {
# Receipe to deliver spam to some folder
$recipe2 = { 'flags' => [ ],
'conds' => [ [ '', '^X-Spam-Status: Yes' ] ],
'action' => $config{'spam_delivery'} };
}
local $varoff = { 'name' => 'SPAMMODE', 'value' => 0 };
&procmail::create_recipe($recipe0, $spamrc);
&procmail::create_recipe($recipe1, $spamrc);
if ($recipe2 || $recipe3) {
&procmail::create_recipe($varon, $spamrc);
&procmail::create_recipe($recipe3, $spamrc) if ($recipe3);
&procmail::create_recipe($recipe2, $spamrc) if ($recipe2);
&procmail::create_recipe($varoff, $spamrc);
}
&set_ownership_permissions(undef, undef, 0755, $spamrc);
# Link all files in the default directory (/etc/mail/spamassassin) to
# the domain's directory
&create_spam_config_links($d);
# Create the config file for this server
&open_tempfile(TOUCH, ">$spamdir/virtualmin.cf", 0, 1);
&print_tempfile(TOUCH, "whitelist_from $d->{'emailto_addr'}\n");
&close_tempfile(TOUCH);
&set_ownership_permissions($d->{'uid'}, $d->{'gid'}, 0755,
"$spamdir/virtualmin.cf");
# Whitelist all domain mailboxes
if ($config{'spam_white'}) {
$d->{'spam_white'} = 1;
&update_spam_whitelist($d);
}
# Setup automatic spam clearing
my $opts = { };
my ($cmode, $cnum) = split(/\s+/, $tmpl->{'spamclear'});
if ($cmode eq 'days' || $cmode eq 'size') {
$opts->{$cmode} = $cnum;
}
my ($tmode, $tnum) = split(/\s+/, $tmpl->{'trashclear'});
if ($tmode eq 'days' || $tmode eq 'size') {
$opts->{'trash'.$tmode} = $tnum;
}
if (keys %$opts) {
&save_domain_spam_autoclear($d, $opts);
}
# Setup spamtrap aliases, if requested
if ($tmpl->{'spamtrap'} eq 'yes') {
&obtain_lock_mail($d);
&setup_spamtrap_aliases($d);
&release_lock_mail($d);
}
&release_lock_cron($d);
&release_lock_spam($d);
&$second_print($text{'setup_done'});
return 1;
}
# spamassassin_client_command(&domain, [client])
# Returns the command for calling spamassassin in some domain, plus args
sub spamassassin_client_command
{
my ($d, $client) = @_;
my $spamid = $d->{'parent'} || $d->{'id'};
$client ||= $config{'spam_client'};
my $cmd = &has_command($client);
if ($client eq 'spamc') {
local ($host, $port) = split(/:/, $config{'spam_host'});
if ($host) {
$cmd .= " -d $host";
if ($port) {
$cmd .= " -p $port";
}
}
if ($config{'spam_size'}) {
$cmd .= " -s $config{'spam_size'}";
}
}
else {
$cmd .= " --siteconfigpath $spam_config_dir/$spamid";
}
return $cmd;
}
# validate_spam(&domain)
# Make sure the domain's procmail config file exists
sub validate_spam
{
my ($d) = @_;
my $spamrc = "$procmail_spam_dir/$d->{'id'}";
return &text('validate_espamprocmail', "<tt>$spamrc</tt>") if (!-r $spamrc);
my $spamdir = "$spam_config_dir/$d->{'id'}";
return &text('validate_espamconfig', "<tt>$spamdir</tt>") if (!-d $spamdir);
&require_spam();
my @recs = &procmail::parse_procmail_file($spamrc);
my $cmd = $spam::config{'spamassassin'};
my $found;
foreach my $r (@recs) {
$found++ if ($r->{'action'} =~ /\Q$cmd\E|spamc|spamassassin/);
}
return &text('validate_espamcall', "<tt>$spamrc</tt>") if (!$found);
return undef;
}
# setup_default_delivery()
# Adds or removes a rule at the end of /etc/procmailrc delivering to $DEFAULT,
# depending on the config setting
sub setup_default_delivery
{
&require_spam();
&obtain_lock_spam();
local @recipes = &procmail::get_procmailrc();
my ($gotdef, $gotorgmail, $gotdel, $gotdrop);
foreach my $r (@recipes) {
if ($r->{'action'} eq '$DEFAULT' && !@{$r->{'conds'}}) {
$gotdel = $r;
}
}
# The rule to deliver to $DEFAULT is needed to prevent users from creating
# their own .procmailrc files
if ($config{'default_procmail'} && !$gotdel) {
# Append default delivery rule
my $rec = { 'flags' => [ ],
'conds' => [ ],
'action' => '$DEFAULT' };
&procmail::create_recipe($rec);
}
elsif (!$config{'default_procmail'} && $gotdel) {
# Remove default delivery rule
&procmail::delete_recipe($gotdel);
}
# Find the DEFAULT variable setting
@recipes = &procmail::get_procmailrc();
foreach my $r (@recipes) {
if ($r->{'name'} eq 'DEFAULT') {
$gotdef = $r;
}
}
# The DEFAULT destination needs to be set to match the mail server, as procmail
# will deliver to /var/mail/USER by default
local ($dir, $style, $mailbox, $maildir) = &get_mail_style();
local $maildef = $dir && $dir =~ /^(.*)\/$/ ? "$1/\$LOGNAME/" :
$dir ? "$dir/\$LOGNAME" :
$maildir ? "\$HOME/$maildir/" :
$mailbox ? "\$HOME/$mailbox" :
"/var/mail/\$LOGNAME";
if ($gotdef) {
# Update default delivery definition
$gotdef->{'value'} = $maildef;
&procmail::modify_recipe($gotdef);
}
else {
# Create default delivery definition
my $rec = { 'name' => 'DEFAULT',
'value' => $maildef };
if (@recipes) {
&procmail::create_recipe_before($rec, $recipes[0]);
}
else {
&procmail::create_recipe($rec);
}
}
# Find the ORGMAIL variable
@recipes = &procmail::get_procmailrc();
foreach my $r (@recipes) {
if ($r->{'name'} eq 'ORGMAIL') {
$gotorgmail = $r;
}
}
# Same for the ORGMAIL destination, to prevent delivery falling back to
# /var/mail/XXX in an over-quota situation
if ($gotorgmail) {
# Update default delivery rule
$gotorgmail->{'value'} = $maildef;
&procmail::modify_recipe($gotorgmail);
}
else {
# Create default delivery rule
my $rec = { 'name' => 'ORGMAIL',
'value' => $maildef };
if (@recipes) {
&procmail::create_recipe_before($rec, $recipes[0]);
}
else {
&procmail::create_recipe($rec);
}
}
# Re-get the default delivery receipe, and DROPPRIVS
$gotdel = undef;
@recipes = &procmail::get_procmailrc();
foreach my $r (@recipes) {
if ($r->{'action'} eq '$DEFAULT' &&
!@{$r->{'conds'}}) {
$gotdel = $r;
}
elsif ($r->{'name'} eq 'DROPPRIVS') {
$gotdrop = $r;
}
}
# DROPPRIVS needs to be set to yes to force delivery as the correct user. This
# must be done before the rule that delivers to $DEFAULT, or at the end of the
# file.
if (!$gotdrop) {
my $rec = { 'name' => 'DROPPRIVS',
'value' => 'yes' };
if ($gotdel) {
# Add before default rule
&procmail::create_recipe_before($rec, $gotdel);
}
else {
# Add at end
&procmail::create_recipe($rec);
}
}
&release_lock_spam();
}
# enable_procmail_logging()
# Configure Procmail to log to /var/log/procmail.log, and setup logrotate
# for that directory.
sub enable_procmail_logging
{
&require_spam();
&obtain_lock_spam();
local @recipes = &procmail::get_procmailrc();
local ($gotlog, $gottrap);
foreach my $r (@recipes) {
if ($r->{'name'} eq 'LOGFILE') {
$gotlog = 1;
}
if ($r->{'name'} eq 'TRAP') {
$gottrap = 1;
}
}
if (!$gotlog) {
# Add LOGFILE variables
my $rec0 = { 'name' => 'LOGFILE',
'value' => $procmail_log_file };
&procmail::create_recipe_before($rec0, $recipes[0]);
}
if (!$gottrap) {
# Add TRAP, which specifies a command to output logging info about
# the email after delivery
my $rec1 = { 'name' => 'TRAP', 'value' => $procmail_log_cmd };
&procmail::create_recipe_before($rec1, $recipes[0]);
}
# For any domains with spam or virus filtering enabled, add SPAMMODE and
# VIRUSMODE procmail variables so that the logger knows what kind of destination
# email ended up at
foreach my $d (&list_domains()) {
next if (!$d->{'spam'});
&obtain_lock_spam($d);
local $spamrc = "$procmail_spam_dir/$d->{'id'}";
local @recipes = &procmail::parse_procmail_file($spamrc);
local ($spamrec, $spamrecafter, $gotspammode);
local $i = 0;
foreach my $r (@recipes) {
if ($r->{'name'} eq 'SPAMMODE') {
$gotspammode = 1;
}
elsif ($r->{'conds'}->[0]->[1] eq '^X-Spam-Status: Yes') {
# Found place to insert
$spamrec = $r;
$spamrecafter = $recipes[$i+1];
last;
}
$i++;
}
if ($spamrec && !$gotspammode) {
local $varon = { 'name' => 'SPAMMODE', 'value' => 1 };
local $varoff = { 'name' => 'SPAMMODE', 'value' => 0 };
if ($spamrecafter) {
&procmail::create_recipe_before($varoff, $spamrecafter,
$spamrc);
}
else {
&procmail::create_recipe($varoff, $spamrc);
}
&procmail::create_recipe_before($varon, $spamrec, $spamrc);
}
# Do the same for viruses
if ($d->{'virus'}) {
local @recipes = &procmail::parse_procmail_file($spamrc);
local ($clamrec, $clamafter, $gotclammode);
local $i = 0;
foreach my $r (@recipes) {
if ($r->{'name'} eq 'VIRUSMODE') {
$gotclammode = 1;
}
elsif ($r->{'action'} =~ /^\Q$clam_wrapper_cmd\E/) {
# Insert after this one
$clamrec = $recipes[$i+1];
$clamrecafter = $recipes[$i+2];
}
$i++;
}
if ($clamrec && !$gotclammode) {
local $varon = { 'name' => 'VIRUSMODE', 'value' => 1 };
local $varoff = { 'name' => 'VIRUSMODE', 'value' => 0 };
if ($clamrecafter) {
&procmail::create_recipe_before(
$varoff, $clamrecafter, $spamrc);
}
else {
&procmail::create_recipe($varoff, $spamrc);
}
&procmail::create_recipe_before(
$varon, $clamrec, $spamrc);
}
}
&release_lock_spam($d);
}
# Copy the log writer command to /etc/webmin
©_source_dest("$module_root_directory/procmail-logger.pl",
$procmail_log_cmd);
&set_ownership_permissions(undef, undef, 0755, $procmail_log_cmd);
if ($config{'logrotate'} && &foreign_installed("logrotate")) {
# Add logrotate section, if needed
&require_logrotate();
local $log = &get_logrotate_section($procmail_log_file);
if (!$log) {
local $parent = &logrotate::get_config_parent();
local $lconf = { 'file' => &logrotate::get_add_file(),
'name' => [ $procmail_log_file ] };
$lconf->{'members'} = [
{ 'name' => 'rotate',
'value' => $config{'logrotate_num'} || 5 },
{ 'name' => 'daily' },
{ 'name' => 'compress' },
];
&lock_file($lconf->{'file'});
&logrotate::save_directive($parent, undef, $lconf);
&flush_file_lines($lconf->{'file'});
&unlock_file($lconf->{'file'});
}
# Make sure file exists, so logrotate doesn't complain
if (!-r $procmail_log_file) {
open(LOG, ">$procmail_log_file");
close(LOG);
}
}
&release_lock_spam();
}
# procmail_logging_enabled()
# Returns 1 if logging entries exist in /etc/procmailrc
sub procmail_logging_enabled
{
&require_spam();
local @recipes = &procmail::get_procmailrc();
foreach my $r (@recipes) {
if ($r->{'name'} eq 'LOGFILE') {
return 1;
}
}
return 0;
}
# modify_spam(&domain, &olddomain)
# Doesn't have to do anything
sub modify_spam
{
my ($d, $oldd) = @_;
return 1;
}
# delete_spam(&domain)
# Just remove the domain's procmail config file
sub delete_spam
{
my ($d) = @_;
&$first_print($d->{'virus'} ? $text{'delete_spamvirus'}
: $text{'delete_spam'});
&obtain_lock_spam($d);
local $spamrc = "$procmail_spam_dir/$d->{'id'}";
&unlink_logged($spamrc);
local $spamdir = "$spam_config_dir/$d->{'id'}";
&system_logged("rm -rf ".quotemeta($spamdir));
&clear_lookup_domain_cache($d);
&save_domain_spam_autoclear($d, undef);
&release_lock_spam($d);
&$second_print($text{'setup_done'});
return 1;
}
# clone_spam(&domain, &old-domain)
# Copy per-domain procmail rules and spamassassin config files to new domain,
# correcting the domain ID
sub clone_spam
{
local ($d, $oldd) = @_;
&$first_print($text{'clone_spam'});
&obtain_lock_spam($d);
local $pm = "$procmail_spam_dir/$d->{'id'}";
local $opm = "$procmail_spam_dir/$oldd->{'id'}";
©_source_dest($opm, $pm);
local $lref = &read_file_lines($pm);
foreach my $l (@$lref) {
$l =~ s/\Q$oldd->{'id'}\E/$d->{'id'}/;
}
&flush_file_lines($pm);
local $spamdir = "$spam_config_dir/$d->{'id'}";
local $ospamdir = "$spam_config_dir/$oldd->{'id'}";
&system_logged("rm -rf ".quotemeta($spamdir)."/*");
&system_logged("cd ".quotemeta($ospamdir)." && tar cf - . | ".
"(cd $spamdir && tar xpf -)");
# Fix email addresses in per-domain spamassassin config file
local $spamfile = "$spamdir/virtualmin.cf";
&set_ownership_permissions($d->{'uid'}, $d->{'gid'}, undef, $spamfile);
local $lref = &read_file_lines($spamfile);
foreach my $l (@$lref) {
if ($l =~ /^whitelist_from\s+\Q$oldd->{'emailto_addr'}\E/) {
$l = "whitelist_from $d->{'emailto_addr'}";
}
}
&flush_file_lines($spamfile);
# Re-update whitelist
if ($d->{'spam_white'}) {
&update_spam_whitelist($d);
}
# Copy automatic spam clearing
&save_domain_spam_autoclear($d, &get_domain_spam_autoclear($oldd));
&release_lock_spam($d);
&$second_print($text{'setup_done'});
return 1;
}
# check_spam_clash()
# No need to check for clashes ..
sub check_spam_clash
{
return 0;
}
# backup_spam(&domain, file)
# Saves the server's procmail and spamassassin configuration to a file.
# Also saves the auto-spam clearing settings.
sub backup_spam
{
local ($d, $file, $opts, $homefmt, $increment, $asd, $allopts, $key) = @_;
local $compression = $allopts->{'dir'}->{'compression'};
&$first_print($text{'backup_spamcp'});
local $spamrc = "$procmail_spam_dir/$d->{'id'}";
local $spamdir = "$spam_config_dir/$d->{'id'}";
if (-r $spamrc) {
©_write_as_domain_user($d, $spamrc, $file);
local $temp = &transname();
&execute_command(&make_archive_command(
$compression, $spamdir, $temp, ".")." 2>&1");
©_write_as_domain_user($d, $temp, $file."_cf");
&unlink_file($temp);
# Save spam clearing
local $auto = &get_domain_spam_autoclear($_[0]);
&write_as_domain_user($d,
sub { &write_file($file."_auto", $auto || { }) });
&$second_print($text{'setup_done'});
return 1;
}
else {
&$second_print($text{'backup_nospam'});
return 0;
}
}
# restore_spam(&domain, file)
# Restores the domains procmail and spamassassin configuration files.
# Also restores auto-clearing setting, if in backup.
sub restore_spam
{
local ($d, $file) = @_;
&$first_print($text{'restore_spamcp'});
&obtain_lock_spam($d);
local $spamrc = "$procmail_spam_dir/$d->{'id'}";
local $spamdir = "$spam_config_dir/$d->{'id'}";
©_source_dest($file, $spamrc);
&execute_command(&make_unarchive_command($spamdir, $file."_cf")." 2>&1");
# Fix any incorrect /etc/webmin references
my $lref = &read_file_lines($spamrc);
foreach my $l (@$lref) {
if ($l =~ /^(.*\s+)(\S+\/clam-wrapper.pl)(\s+.*)$/) {
$l = $1.$clam_wrapper_cmd.$3;
}
if ($l =~ /^(.*\s+--siteconfigpath\s+)(\S+\/virtual-server\/spam\/\S+)$/) {
$l = $1.$spamdir;
}
}
&flush_file_lines($spamrc);
if (-r $file."_auto") {
# Replace auto-clearing setting
&save_domain_spam_autoclear($d, undef);
local %auto;
&read_file($file."_auto", \%auto);
if (%auto) {
&save_domain_spam_autoclear($d, \%auto);
}
}
# If spamtrap aliases exist, make sure the files and cron job do
local $st = &get_spamtrap_aliases($d);
if ($st > 0) {
&setup_spamtrap_directories($d);
&setup_spamtrap_cron();
}
# Re-create all spam links
&create_spam_config_links($d);
&release_lock_spam($d);
&$second_print($text{'setup_done'});
return 1;
}
# save_global_spam_lockfile(enable)
# Adds or removes a lockfile to all domains' spamassassin calls
sub save_global_spam_lockfile
{
local ($enabled) = @_;
foreach my $d (&get_domain_by("spam", 1)) {
local $spamrc = "$procmail_spam_dir/$d->{'id'}";
local @recipes = &procmail::parse_procmail_file($spamrc);
local @spamrec = &find_spam_recipe(\@recipes);
if ($spamrec[0]) {
# Found it .. modify
if ($enabled) {
$spamrec[0]->{'lockfile'} = $spamassassin_lock_file;
}
else {
delete($spamrec[0]->{'lockfile'});
}
&procmail::modify_recipe($spamrec[0]);
}
}
}
# sysinfo_spam()
# Returns the SpamAssassin version
sub sysinfo_spam
{
&require_spam();
local $vers = &spam::get_spamassassin_version();
return ( [ $text{'sysinfo_spam'}, $vers ] );
}
sub links_spam
{
local ($d) = @_;
local $client = &get_domain_spam_client($d);
if ($client ne "spamc") {
return ( { 'mod' => 'spam',
'desc' => $text{'links_spam'},
'page' => 'index.cgi?file='.&urlize(
"$spam_config_dir/$d->{'id'}/virtualmin.cf").
'&title='.&urlize(&show_domain_name($d)),
'cat' => 'mail',
});
}
return ( );
}
# find_spam_recipe(&recipes)
# Returns the five recipes used for spam filtering, some of which may be
# undef if not set. They are :
# 0 - Call to spamassassin or spamc
# 1 - Setting of SPAMMODE=1
# 2 - Delivery for high-score spam
# 3 - Delivery for other spam
# 4 - Setting of SPAMMODE=0
sub find_spam_recipe
{
local ($recs) = @_;
local @rv;
for(my $i=0; $i<@$recs; $i++) {
if ($recs->[$i]->{'action'} =~ /(spamassassin|spamc)($|\s)/) {
# Found spamassassin
$rv[0] = $recs->[$i];
}
elsif ($recs->[$i]->{'name'} eq 'SPAMMODE') {
# Start or end of spam delivery block
if ($recs->[$i]->{'value'} eq '1') {
$rv[1] = $recs->[$i];
}
else {
# End of recipes we care about
$rv[4] = $recs->[$i];
last;
}
}
else {
# Look at conditions
my $r = $recs->[$i];
foreach my $c (@{$r->{'conds'}}) {
if ($c->[1] =~ /X-Spam-Status/i) {
$rv[3] ||= $r;
}
elsif ($c->[1] =~ /X-Spam-Level/i) {
$rv[2] ||= $r;
}
}
}
}
return @rv;
}
# get_domain_spam_delivery(&domain)
# Returns the delivery mode and dest for some domain. The modes can be :
# 0 - Throw away , 1 - File under home , 2 - Forward to email , 3 - Other file,
# 4 - Normal ~/mail/spam file , 5 - Deliver normally , 6 - ~/Maildir/.spam/ ,
# -1 - Broken!
# Also returns the score at which spam is deleted, and the destination (usually
# /dev/null)
sub get_domain_spam_delivery
{
local ($d) = @_;
&require_spam();
local $spamrc = "$procmail_spam_dir/$d->{'id'}";
local @recipes = &procmail::parse_procmail_file($spamrc);
local @spamrec = &find_spam_recipe(\@recipes);
local @rv;
if (!@spamrec) {
@rv = (-1, undef);
}
elsif (!$spamrec[3]) {
@rv = (5, undef);
}
elsif ($spamrec[3]->{'action'} eq '/dev/null') {
@rv = (0, undef);
}
elsif ($spamrec[3]->{'action'} =~ /^\$HOME\/mail\/(spam|Spam|junk|Junk)$/) {
@rv = (4, $1);
}
elsif ($spamrec[3]->{'action'} =~ /^\$HOME\/Maildir\/\.(spam|Spam|junk|Junk)\/$/) {
@rv = (6, $1);
}
elsif ($spamrec[3]->{'action'} =~ /^\$HOME\/(.*)$/) {
@rv = (1, $1);
}
elsif ($spamrec[3]->{'action'} =~ /\@/) {
@rv = (2, $spamrec[3]->{'action'});
}
else {
@rv = (3, $spamrec[3]->{'action'});
}
if ($spamrec[2]) {
# Add deletion recipe info
foreach my $c (@{$spamrec[2]->{'conds'}}) {
if ($c->[1] =~ /X-Spam-Level:\s+((\\\*)+)/i) {
# Found it
push(@rv, length($1)/2, $r->{'action'});
}
}
}
return @rv;
}
# save_domain_spam_delivery(&domain, [mode, dest], [delete-level, delete-dest])
# Updates the delivery method for spam for some domain
sub save_domain_spam_delivery
{
local ($d, $mode, $dest, $level, $ddest) = @_;
&require_spam();
local $spamrc = "$procmail_spam_dir/$d->{'id'}";
local @recipes = &procmail::parse_procmail_file($spamrc);
local @spamrec = &find_spam_recipe(\@recipes);
return 0 if (!@spamrec);
# Preserve existing settings if not set
local ($oldmode, $olddest, $oldlevel, $oldddest) =
&get_domain_spam_delivery($d);
if (!defined($mode)) {
($mode, $dest) = ($oldmode, $olddest);
}
elsif (!defined($dest)) {
$dest = $olddest;
}
if (!defined($level)) {
$level = $oldlevel;
}
if (!defined($ddest)) {
$ddest = $oldddest;
}
$ddest ||= "/dev/null";
# Remove the existing recipes (except the spamassassin call)
local @todel = sort { $b->{'line'} <=> $a->{'line'} }
grep { $_ } @spamrec[1..$#spamrec];
foreach my $r (@todel) {
&procmail::delete_recipe($r);
}
# Make those we now want
local @want;
if ($mode != 5 || $level) {
# Start of delivery section
push(@want, { 'name' => 'SPAMMODE', 'value' => 1 });
}
if ($level) {
# High-level deletion
local $stars = join("", map { "\\*" } (1..$level));
push(@want, { 'conds' => [ [ '', '^X-Spam-Level: '.$stars ] ],
'action' => $ddest });
}
if ($mode != 5) {
# Regular delivery
local $folder;
if ($mode == 4 || $mode == 6) {
if ($dest =~ /^[a-z0-9\.\_\-]+$/i) {
$folder = $dest;
}
else {
$folder = &default_spam_folder_suffix($d);
}
}
local $action = $mode == 0 ? "/dev/null" :
$mode == 4 ? "\$HOME/mail/$folder" :
$mode == 6 ? "\$HOME/Maildir/.$folder/" :
$mode == 1 ? "\$HOME/$dest" :
$dest;
local $type = $mode == 2 ? "!" : "";
push(@want, { 'conds' => [ [ '', '^X-Spam-Status: Yes' ] ],
'action' => $action,
'type' => $type });
}
if ($mode != 5 || $level) {
# End of delivery section
push(@want, { 'name' => 'SPAMMODE', 'value' => 0 });
}
# Add them
if (@want) {
@recipes = &procmail::parse_procmail_file($spamrc);
@spamrec = &find_spam_recipe(\@recipes);
local $idx = &indexof($spamrec[0], @recipes);
if ($idx == $#recipes) {
# Add at end
foreach my $r (@want) {
&procmail::create_recipe($r, $spamrc);
}
}
else {
# After spamassassin call
foreach my $r (@want) {
procmail::create_recipe_before($r, $recipes[$idx+1],
$spamrc);
}
}
}
&clear_lookup_domain_cache($_[0]);
return 1;
}
# default_spam_folder_suffix(&domain)
# Returns either "Spam" or "Junk" depending on the default folder name
sub default_spam_folder_suffix
{
my ($d) = @_;
if ($config{'spam_delivery'} =~ /\/Maildir\/\.([^\/]+)\/$/) {
return $1;
}
elsif ($config{'spam_delivery'} =~ /\/mail\/([^\/]+)$/) {
return $1;
}
return "Junk";
}
# get_domain_spam_client(&domain)
# Returns the client program (spamassassin or spamc) used by some domain
sub get_domain_spam_client
{
local ($d) = @_;
&require_spam();
local $spamrc = "$procmail_spam_dir/$d->{'id'}";
local @recipes = &procmail::parse_procmail_file($spamrc);
foreach my $r (@recipes) {
if ($r->{'action'} =~ /\/\S+\/(spamassassin|spamc)/) {
return $1;
}
}
return undef; # Cannot happen!
}
# save_domain_spam_client(&domain, spamassassin|spamc)
# Updates the procmail rule which calls spamassassin or spamc
sub save_domain_spam_client
{
local ($d, $client) = @_;
&require_spam();
local $spamrc = "$procmail_spam_dir/$d->{'id'}";
local @recipes = &procmail::parse_procmail_file($spamrc);
foreach my $r (@recipes) {
if ($r->{'action'} =~ /\/\S+\/(spamassassin|spamc)/) {
$r->{'action'} = &spamassassin_client_command($d, $client);
local ($c) = grep { $_->[0] eq '<' } @{$r->{'conds'}};
if ($c && !$config{'spam_size'}) {
# Remove size condition
@{$r->{'conds'}} = grep { $_ ne $c } @{$r->{'conds'}};
}
elsif (!$c && $config{'spam_size'}) {
# Add size condition
push(@{$r->{'conds'}}, [ '<', $config{'spam_size'} ]);
}
elsif ($c && $config{'spam_size'}) {
# Fix size in condition
$c->[1] = $config{'spam_size'};
}
&procmail::modify_recipe($r);
}
}
}
# get_global_spam_client()
# Returns the spam client that is supposed to be used by all domains. If this
# is spamc, also returns the spamd hostname and max message size
sub get_global_spam_client
{