blib/lib/Mail/Toaster/Setup/Maildrop.pm untested

filter: Statementstmt 0.0% 0 / 121 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 50 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 232 SCAR 63.1
CC
23
Cov
0%
filter
56.0
filter_file
56.0
install
30.0
CRAP 552.0
1 package Mail::Toaster::Setup::Maildrop;
2 use strict;
3 use warnings;
4  
5 our $VERSION = '5.50';
6  
7 use File::Basename;
8 use English '-no_match_vars';
9 use Params::Validate ':all';
10  
11 use lib 'lib';
12 use parent 'Mail::Toaster::Base';
13  
14 sub install {
15 my $self = shift;
16 my %p = validate( @_, { $self->get_std_opts },);
17  
18 my $ver = $self->conf->{'install_maildrop'} or do {
19 $self->audit( "skipping maildrop install, not enabled.");
20 return 0;
21 };
22  
23 my $prefix = $self->conf->{'toaster_prefix'} || "/usr/local";
24  
25 if ( $ver eq "port" || $ver eq "1" ) {
26 if ( $OSNAME eq "freebsd" ) {
27 # pre-installing pcre supresses a dialog
28 $self->freebsd->install_package( "pcre" );
29 $self->freebsd->install_port( "pcre",
30 options => '# written by Mail::Toaster
31 # Options for pcre-8.33
32 _OPTIONS_READ=pcre-8.33
33 _FILE_COMPLETE_OPTIONS_LIST=STACK_RECURSION
34 OPTIONS_FILE_SET+=STACK_RECURSION
35 ',
36 );
37 $self->freebsd->install_package( "maildrop" );
38 $self->freebsd->install_port( "maildrop", flags => "WITH_MAILDIRQUOTA=1",);
39 }
40 elsif ( $OSNAME eq "darwin" ) {
41 $self->darwin->install_port( "maildrop" );
42 }
43 $ver = "2.7.1";
44 }
45  
46 $self->util->find_bin( "maildrop", fatal => 0 )
47 or $self->util->install_from_source(
48 package => 'maildrop-' . $ver,
49 site => 'http://' . $self->conf->{'toaster_sf_mirror'},
50 url => '/courier',
51 targets => [
52 './configure --prefix=' . $prefix . ' --exec-prefix=' . $prefix,
53 'make',
54 'make install-strip',
55 'make install-man'
56 ],
57 source_sub_dir => 'mail',
58 );
59  
60 my $etcmail = "$prefix/etc/mail";
61 unless ( -d $etcmail ) {
62 mkdir( $etcmail, oct('0755') )
63 or $self->util->mkdir_system( dir => $etcmail, mode=>'0755' );
64 }
65  
66 $self->filter();
67 $self->imap_subscribe();
68 $self->filter_logs();
69 };
70  
71 sub filter {
72 my $self = shift;
73 my %p = validate( @_, { $self->get_std_opts },);
74  
75 my $prefix = $self->conf->{'toaster_prefix'} || "/usr/local";
76 my $logbase = $self->conf->{'qmail_log_base'};
77  
78 if ( !$logbase ) {
79 $logbase = -d "/var/log/qmail" ? "/var/log/qmail" : "/var/log/mail";
80 }
81  
82 my $filterfile = $self->conf->{'filtering_maildrop_filter_file'}
83 || "$prefix/etc/mail/mailfilter";
84  
85 my ( $file, $path ) = fileparse($filterfile); chop $path;
86  
87 $self->util->mkdir_system( dir => $path ) if ! -d $path;
88  
89 return $self->error( "$path doesn't exist and I couldn't create it.")
90 if ! -d $path;
91  
92 my @lines = $self->filter_file( logbase => $logbase );
93  
94 my $user = $self->conf->{'vpopmail_user'} || "vpopmail";
95 my $group = $self->conf->{'vpopmail_group'} || "vchkpw";
96  
97 # if the mailfilter file doesn't exist, create it
98 if ( -e $filterfile ) {
99 $self->util->file_write( "$filterfile.new", lines => \@lines, mode =>'0600' );
100 $self->util->install_if_changed(
101 newfile => "$filterfile.new",
102 existing => $filterfile,
103 mode => '0600',
104 clean => 0,
105 notify => 1,
106 archive => 1,
107 );
108 }
109 else {
110 $self->util->file_write( $filterfile, lines => \@lines, mode => '0600' );
111 $self->audit("installed new $filterfile, ok");
112 };
113  
114 $self->util->chown( $filterfile, uid => $user, gid => $group );
115  
116 $file = "/etc/newsyslog.conf";
117 if ( -e $file && ! `grep maildrop $file`) {
118 $self->util->file_write( $file,
119 lines =>
120 ["/var/log/mail/maildrop.log $user:$group 644 3 1000 * Z"],
121 append => 1,
122 );
123 };
124 return 1;
125 }
126  
127 sub filter_file {
128 my $self = shift;
129 my %p = validate( @_, { 'logbase' => SCALAR, $self->get_std_opts, },);
130  
131 my $logbase = $p{'logbase'};
132  
133 my $prefix = $self->conf->{'toaster_prefix'} || "/usr/local";
134 my $filterfile = $self->conf->{'filtering_maildrop_filter_file'}
135 || "$prefix/etc/mail/mailfilter";
136  
137 my @lines = 'SHELL="/bin/sh"';
138 push @lines, <<"EOMAILDROP";
139 import EXT
140 import HOST
141 VHOME=`pwd`
142 TIMESTAMP=`date "+\%b \%d \%H:\%M:\%S"`
143  
144 ##
145 # title: mailfilter-site
146 # author: Matt Simerson
147 # version 2.17
148 #
149 # This file is automatically generated by toaster_setup.pl,
150 # DO NOT HAND EDIT, your changes may get overwritten!
151 #
152 # Make changes to toaster-watcher.conf, and run
153 # toaster_setup.pl -s maildrop to rebuild this file. Old versions
154 # are preserved as $filterfile.timestamp
155 #
156 # Usage: Install this file in your local etc/mail/mailfilter. On
157 # your system, this is $prefix/etc/mail/mailfilter
158 #
159 # Create a .qmail file in each users Maildir as follows:
160 # echo "| $prefix/bin/maildrop $prefix/etc/mail/mailfilter" \
161 # > ~vpopmail/domains/example.com/user/.qmail
162 #
163 # You can also use qmailadmin v1.0.26 or higher to do that for you
164 # via it is --enable-modify-spam and --enable-spam-command options.
165 # This is the default behavior for your Mail::Toaster.
166 #
167 # Environment Variables you can import from qmail-local:
168 # SENDER is the envelope sender address
169 # NEWSENDER is the forwarding envelope sender address
170 # RECIPIENT is the envelope recipient address, local\@domain
171 # USER is user
172 # HOME is your home directory
173 # HOST is the domain part of the recipient address
174 # LOCAL is the local part
175 # EXT is the address extension, ext.
176 # HOST2 is the portion of HOST preceding the last dot
177 # HOST3 is the portion of HOST preceding the second-to-last dot
178 # HOST4 is the portion of HOST preceding the third-to-last dot
179 # EXT2 is the portion of EXT following the first dash
180 # EXT3 is the portion following the second dash;
181 # EXT4 is the portion following the third dash.
182 # DEFAULT is the portion corresponding to the default part of the .qmail-... file name
183 # DEFAULT is not set if the file name does not end with default
184 # DTLINE and RPLINE are the usual Delivered-To and Return-Path lines, including newlines
185 #
186 # qmail-local will be calling maildrop. The exit codes that qmail-local
187 # understands are:
188 # 0 - delivery is complete
189 # 111 - temporary error
190 # xxx - unknown failure
191 ##
192 EOMAILDROP
193  
194 $self->conf->{'filtering_verbose'} ? push @lines, qq{logfile "$logbase/maildrop.log"}
195 : push @lines, qq{#logfile "$logbase/maildrop.log"};
196  
197 push @lines, <<'EOMAILDROP2';
198 log "$TIMESTAMP - BEGIN maildrop processing for $EXT@$HOST ==="
199  
200 # I have seen cases where EXT or HOST is unset. This can be caused by
201 # various blunders committed by the sysadmin so we should test and make
202 # sure things are not too messed up.
203 #
204 # By exiting with error 111, the error will be logged, giving an admin
205 # the chance to notice and fix the problem before the message bounces.
206  
207 if ( $EXT eq "" )
208 {
209 log " FAILURE: EXT is not a valid value"
210 log "=== END === $EXT@$HOST failure (EXT variable not imported)"
211 EXITCODE=111
212 exit
213 }
214  
215 if ( $HOST eq "" )
216 {
217 log " FAILURE: HOST is not a valid value"
218 log "=== END === $EXT@$HOST failure (HOST variable not imported)"
219 EXITCODE=111
220 exit
221 }
222  
223 EOMAILDROP2
224  
225 my $spamass_method = $self->conf->{'filtering_spamassassin_method'};
226  
227 if ( $spamass_method eq "user" || $spamass_method eq "domain" ) {
228  
229 push @lines, <<"EOMAILDROP3";
230 ##
231 # Note that if you want to pass a message larger than 250k to spamd
232 # and have it processed, you will need to also set spamc -s. See the
233 # spamc man page for more details.
234 ##
235  
236 exception {
237 if ( /^X-Spam-Status: /:h )
238 {
239 # do not pass through spamassassin if the message already
240 # has an X-Spam-Status header.
241  
242 log "Message already has X-Spam-Status header, skipping spamc"
243 }
244 else
245 {
246 if ( \$SIZE < 256000 ) # Filter if message is less than 250k
247 {
248 `test -x $prefix/bin/spamc`
249 if ( \$RETURNCODE == 0 )
250 {
251 log `date "+\%b \%d \%H:\%M:\%S"`" \$PID - running message through spamc"
252 exception {
253 xfilter '$prefix/bin/spamc -u "\$EXT\@\$HOST"'
254 }
255 }
256 else
257 {
258 log " WARNING: no $prefix/bin/spamc binary!"
259 }
260 }
261 }
262 }
263 EOMAILDROP3
264  
265 }
266  
267 push @lines, <<"EOMAILDROP4";
268 ##
269 # Include any rules set up for the user - this gives the
270 # administrator a way to override the sitewide mailfilter file
271 #
272 # this is also the "suggested" way to set individual values
273 # for maildrop such as quota.
274 ##
275  
276 `test -r \$VHOME/.mailfilter`
277 if( \$RETURNCODE == 0 )
278 {
279 log " including \$VHOME/.mailfilter"
280 exception {
281 include \$VHOME/.mailfilter
282 }
283 }
284  
285 ##
286 # create the maildirsize file if it does not already exist
287 # (could also be done via "deliverquota user\@dom.com 10MS,1000C)
288 ##
289  
290 `test -e \$VHOME/Maildir/maildirsize`
291 if( \$RETURNCODE == 1)
292 {
293 VUSERINFO="$prefix/vpopmail/bin/vuserinfo"
294 `test -x \$VUSERINFO`
295 if ( \$RETURNCODE == 0)
296 {
297 log " creating \$VHOME/Maildir/maildirsize for quotas"
298 `\$VUSERINFO -Q \$EXT\@\$HOST`
299  
300 `test -s "\$VHOME/Maildir/maildirsize"`
301 if ( \$RETURNCODE == 0 )
302 {
303 `/usr/sbin/chown vpopmail:vchkpw \$VHOME/Maildir/maildirsize`
304 `/bin/chmod 640 \$VHOME/Maildir/maildirsize`
305 }
306 }
307 else
308 {
309 log " WARNING: cannot find vuserinfo! Please edit mailfilter"
310 }
311 }
312  
313 EOMAILDROP4
314  
315 my $head = $self->util->find_bin( 'head' );
316  
317 push @lines, <<"EOMAILDROP5";
318 ##
319 # Set MAILDIRQUOTA. If this is not set, maildrop and deliverquota
320 # will not enforce quotas for message delivery.
321 ##
322  
323 `test -e \$VHOME/Maildir/maildirsize`
324 if( \$RETURNCODE == 0)
325 {
326 MAILDIRQUOTA=`$head -n1 \$VHOME/Maildir/maildirsize`
327 }
328  
329 # if the user does not have a Spam folder, create it.
330  
331 `test -d \$VHOME/Maildir/.Spam`
332 if( \$RETURNCODE == 1 )
333 {
334  
335 MAILDIRMAKE="$prefix/bin/maildirmake"
336 `test -x \$MAILDIRMAKE`
337 if ( \$RETURNCODE == 1 )
338 {
339 MAILDIRMAKE="$prefix/bin/maildrop-maildirmake"
340 `test -x \$MAILDIRMAKE`
341 }
342  
343 if ( \$RETURNCODE == 1 )
344 {
345 log " WARNING: no maildirmake!"
346 }
347 else
348 {
349 log " creating \$VHOME/Maildir/.Spam "
350 `\$MAILDIRMAKE -f Spam \$VHOME/Maildir`
351 `$prefix/sbin/subscribeIMAP.sh Spam \$VHOME`
352 }
353 }
354  
355 ##
356 # The message should be tagged, so lets bag it.
357 ##
358 # HAM: X-Spam-Status: No, score=-2.6 required=5.0
359 # SPAM: X-Spam-Status: Yes, score=8.9 required=5.0
360 #
361 # Note: SA < 3.0 uses "hits" instead of "score"
362 #
363 # if ( /^X-Spam-Status: *Yes/) # test if spam status is yes
364 # The following regexp matches any spam message and sets the
365 # variable \$MATCH2 to the spam score.
366  
367 if ( /X-Spam-Status: Yes/:h)
368 {
369 if ( /X-Spam-Status: Yes, (hits|score)=([\\d\\.\\-]+)\\s/:h)
370 {
371 EOMAILDROP5
372  
373 my $discard = $self->conf->{'filtering_spama_discard_score'};
374 my $pyzor = $self->conf->{'filtering_report_spam_pyzor'};
375 my $sa_report = $self->conf->{'filtering_report_spam_spamassassin'};
376  
377 if ($discard) {
378  
379 push @lines, <<"EOMAILDROP6";
380 # if the message scored a $discard or higher, then there is no point in
381 # keeping it around. SpamAssassin already knows it as spam, and
382 # has already "autolearned" from it if you have that enabled. The
383 # end user likely does not want it. If you wanted to cc it, or
384 # deliver it elsewhere for inclusion in a spam corpus, you could
385 # easily do so with a cc or xfilter command
386  
387 if ( \$MATCH2 >= $discard ) # from Adam Senuik post to mail-toasters
388 {
389 EOMAILDROP6
390  
391 if ( $pyzor && !$sa_report ) {
392  
393 push @lines, <<"EOMAILDROP7";
394 `test -x $prefix/bin/pyzor`
395 if( \$RETURNCODE == 0 )
396 {
397 # if the pyzor binary is installed, report all messages with
398 # high spam scores to the pyzor servers
399  
400 log " SPAM: score \$MATCH2: reporting to Pyzor"
401 exception {
402 xfilter "$prefix/bin/pyzor report"
403 }
404 }
405 EOMAILDROP7
406 }
407  
408 if ($sa_report) {
409  
410 push @lines, <<"EOMAILDROP8";
411  
412 # new in version 2.5 of Mail::Toaster mailfiter
413 `test -x $prefix/bin/spamassassin`
414 if( \$RETURNCODE == 0 )
415 {
416 # if the spamassassin binary is installed, report messages with
417 # high spam scores to spamassassin (and consequently pyzor, dcc,
418 # razor, and SpamCop)
419  
420 log " SPAM: score \$MATCH2: reporting spam via spamassassin -r"
421 exception {
422 xfilter "$prefix/bin/spamassassin -r"
423 }
424 }
425 EOMAILDROP8
426 }
427  
428 push @lines, <<"EOMAILDROP9";
429 log " SPAM: score \$MATCH2 exceeds $discard: nuking message!"
430 log "=== END === \$EXT\@\$HOST success (discarded)"
431 EXITCODE=0
432 exit
433 }
434 EOMAILDROP9
435 }
436  
437 push @lines, <<"EOMAILDROP10";
438 log " SPAM: score \$MATCH2: delivering to \$VHOME/Maildir/.Spam"
439 log "=== END === \$EXT\@\$HOST success"
440 exception {
441 to "\$VHOME/Maildir/.Spam"
442 }
443 }
444 else
445 {
446 log " SpamAssassin regexp match error!"
447 }
448 }
449  
450 if ( /^X-Spam-Status: No, (score|hits)=([\\d\\.\\-]+)\\s/:h)
451 {
452 log " message is SA clean (\$MATCH2)"
453 }
454  
455 EOMAILDROP10
456  
457 if ( $self->conf->{install_dspam} ) {
458  
459 push @lines, <<"EOMAILDROP_DSPAM";
460 if ( /^X-DSPAM-Result: /:h )
461 {
462 log " has X-DSPAM-Result header"
463 }
464 else
465 {
466 if ( \$SIZE < 4194304 ) # Filter if message is less than 4MB
467 {
468 `test -x $prefix/bin/dspam`
469 if ( \$RETURNCODE == 0 )
470 {
471 log `date "+\%b \%d \%H:\%M:\%S"`" \$PID - running message through dspam"
472 exception {
473 xfilter '$prefix/bin/dspam --user \$EXT\@\$HOST --process --deliver=innocent,spam --stdout'
474 }
475 }
476 else
477 {
478 log " WARNING: no $prefix/bin/dspam binary!"
479 }
480 }
481 }
482  
483 ##
484 # Check for DSPAM tag
485 ##
486 # HAM: X-DSPAM-Result: Innocent, probability=0.0000, confidence=0.94
487 # SPAM: X-DSPAM-Result: Spam, probability=1.0000, confidence=0.99
488 #
489  
490 if ( /X-DSPAM-Result: /:h)
491 {
492 if ( /X-DSPAM-Result: Spam/:h)
493 {
494 if ( /X-DSPAM-Result: Spam, probability=([\\d\\.]+), confidence=([\\d\\.]+)/:h)
495 {
496 if ( \$MATCH1 == 1 && \$MATCH2 >= .50 )
497 {
498 if ( /^X-Spam-Status: /:h )
499 {
500 if ( /X-Spam-Status: Yes/:h)
501 {
502 log " DSPAM: delivering spam (\$MATCH2) to \$VHOME/Maildir/.Spam"
503 log "=== END === \$EXT\@\$HOST success"
504 exception {
505 to "\$VHOME/Maildir/.Spam"
506 }
507 }
508 else
509 {
510 log " DSPAM says spam (\$MATCH2) SA says no"
511 }
512 }
513 }
514 else
515 {
516 log " DSPAM suspects spam (\$MATCH2)"
517 }
518 }
519 else
520 {
521 log " DSPAM regexp match error!"
522 }
523 }
524 else
525 {
526 log " dspam says innocent"
527 }
528 }
529  
530 EOMAILDROP_DSPAM
531 ;
532 };
533  
534 push @lines, <<"EOMAILDROP11";
535 ##
536 # Include any other rules that the user might have from
537 # sqwebmail or other compatible program
538 ##
539  
540 `test -r \$VHOME/Maildir/.mailfilter`
541 if( \$RETURNCODE == 0 )
542 {
543 log " including \$VHOME/Maildir/.mailfilter"
544 exception {
545 include \$VHOME/Maildir/.mailfilter
546 }
547 }
548  
549 `test -r \$VHOME/Maildir/mailfilter`
550 if( \$RETURNCODE == 0 )
551 {
552 log " including \$VHOME/Maildir/mailfilter"
553 exception {
554 include \$VHOME/Maildir/mailfilter
555 }
556 }
557  
558 log " delivering to \$VHOME/Maildir"
559  
560 # make sure the deliverquota binary exists and is executable
561 # if not, then we cannot enforce quotas. If we do not check
562 # and the binary is missing, maildrop silently discards mail.
563  
564 DELIVERQUOTA="$prefix/bin/deliverquota"
565 `test -x \$DELIVERQUOTA`
566 if ( \$RETURNCODE == 1 )
567 {
568 DELIVERQUOTA="$prefix/bin/maildrop-deliverquota"
569 `test -x \$DELIVERQUOTA`
570 }
571  
572 if ( \$RETURNCODE == 1 )
573 {
574 log " WARNING: no deliverquota!"
575 log "=== END === \$EXT\@\$HOST success"
576 exception {
577 to "\$VHOME/Maildir"
578 }
579 }
580 else
581 {
582 exception {
583 xfilter "\$DELIVERQUOTA -w 90 \$VHOME/Maildir"
584 }
585  
586 ##
587 # check to make sure the message was delivered
588 # returncode 77 means that out maildir was overquota - bounce mail
589 ##
590 if( \$RETURNCODE == 77)
591 {
592 #log " BOUNCED: bouncesaying '\$EXT\@\$HOST is over quota'"
593 log "=== END === \$EXT\@\$HOST bounced"
594 to "|/var/qmail/bin/bouncesaying '\$EXT\@\$HOST is over quota'"
595 }
596 else
597 {
598 log "=== END === \$EXT\@\$HOST success (quota)"
599 EXITCODE=0
600 exit
601 }
602 }
603  
604 log "WARNING: This message should never be printed!"
605 EOMAILDROP11
606  
607 return @lines;
608 }
609  
610 sub filter_logs {
611 my $self = shift;
612  
613 my $log = $self->conf->{'qmail_log_base'} || "/var/log/mail";
614  
615 $self->util->mkdir_system( dir => $log, verbose => 0 ) if ! -d $log;
616  
617 $self->util->chown( $log,
618 uid => $self->conf->{'qmail_log_user'} || 'qmaill',
619 gid => $self->conf->{'qmail_log_group'} || 'qnofiles',
620 sudo => $UID == 0 ? 0 : 1,
621 );
622  
623 my $logf = "$log/maildrop.log";
624  
625 $self->util->file_write( $logf, lines => ["begin"] ) if ! -e $logf;
626  
627 $self->util->chown( $logf,
628 uid => $self->conf->{'vpopmail_user'} || "vpopmail",
629 gid => $self->conf->{'vpopmail_group'} || "vchkpw",
630 sudo => $UID == 0 ? 0 : 1,
631 );
632 }
633  
634 sub imap_subscribe {
635 my $self = shift;
636 my $prefix = $self->conf->{'toaster_prefix'} || "/usr/local";
637 my $sub_file = "$prefix/sbin/subscribeIMAP.sh";
638  
639 my $sub_bin = $self->util->find_bin( "$prefix/sbin/subscribeIMAP.sh", verbose => 0, fatal => 0 );
640 return 1 if ( $sub_bin && -e $sub_bin );
641  
642 my $chown = $self->util->find_bin( 'chown' );
643 my $chmod = $self->util->find_bin( 'chmod' );
644  
645 my @lines = '#!/bin/sh
646 #
647 # This subscribes the folder passed as $1 to courier imap
648 # so that Maildir reading apps (Sqwebmail, Courier-IMAP) and
649 # IMAP clients (squirrelmail, Mailman, etc) will recognize the
650 # extra mail folder.
651  
652 # Matt Simerson - 12 June 2003
653  
654 LIST="$2/Maildir/courierimapsubscribed"
655  
656 if [ -f "$LIST" ]; then
657 # if the file exists, check it for the new folder
658 TEST=`cat "$LIST" | grep "INBOX.$1"`
659  
660 # if it is not there, add it
661 if [ "$TEST" = "" ]; then
662 echo "INBOX.$1" >> $LIST
663 fi
664 else
665 # the file does not exist so we define the full list
666 # and then create the file.
667 FULL="INBOX\nINBOX.Sent\nINBOX.Trash\nINBOX.Drafts\nINBOX.$1"
668  
669 echo -e $FULL > $LIST
670 ' . $chown . ' vpopmail:vchkpw $LIST
671 ' . $chmod . ' 644 $LIST
672 fi
673 ';
674  
675 $self->util->file_write( $sub_file, lines => \@lines );
676  
677 $self->util->chmod(
678 file_or_dir => $sub_file,
679 mode => '0555',
680 sudo => $UID == 0 ? 0 : 1,
681 );
682 };
683  
684 1;
685 __END__;
686