Debian Codegraph @

plugins/badmailfromto

raw
1 #!perl -w
2
3 =head1 NAME
4
5 badmailfromto - checks the badmailfromto config
6
7 =head1 DESCRIPTION
8
9 Much like the similar badmailfrom, this plugin references both the
10 FROM: and TO: lines, and if they both are present in the badmailfromto
11 config file (a tab delimited list of FROM/TO pairs), then the message is
12 blocked as if the recipient (TO) didn't exist. This is specifically designed
13 to not give the impression that the sender is blocked (good for cases of
14 harassment).
15
16 Based heavily on badmailfrom.
17
18 =cut
19
20 use strict;
21 use Qpsmtpd::Constants;
22
23 sub hook_mail {
24 my ($self, $transaction, $sender, %param) = @_;
25
26 my @badmailfromto = $self->qp->config("badmailfromto");
27 return DECLINED if $self->is_sender_immune($sender, \@badmailfromto);
28
29 my $host = lc $sender->host;
30 my $from = lc($sender->user) . '@' . $host;
31
32 for my $bad (@badmailfromto) {
33 $bad =~ s/^\s*(\S+).*/$1/;
34 next unless $bad;
35 $bad = lc $bad;
36 if ($bad !~ m/\@/) {
37 $self->log(LOGWARN, 'bad config, no @ sign in ' . $bad);
38 next;
39 }
40 if ($bad eq $from || (substr($bad, 0, 1) eq '@' && $bad eq "\@$host")) {
41 $transaction->notes('badmailfromto', $bad);
42 }
43 }
44 return (DECLINED);
45 }
46
47 sub hook_rcpt {
48 my ($self, $transaction, $rcpt, %param) = @_;
49 my $recipient = lc($rcpt->user) . '@' . lc($rcpt->host);
50 my $sender = $transaction->notes('badmailfromto') or do {
51 $self->log(LOGDEBUG, "pass, sender not listed");
52 return (DECLINED);
53 };
54
55 foreach ($self->qp->config("badmailfromto")) {
56 my ($from, $to) = m/^\s*(\S+)\t(\S+).*/;
57 return (DENY, "mail to $recipient not accepted here")
58 if lc($from) eq $sender && lc($to) eq $recipient;
59 }
60 $self->log(LOGDEBUG, "pass, recipient not listed");
61 return (DECLINED);
62 }
63
64 sub is_sender_immune {
65 my ($self, $sender, $badmf) = @_;
66
67 if (!scalar @$badmf) {
68 $self->log(LOGDEBUG, 'skip, empty list');
69 return 1;
70 }
71
72 if (!$sender || $sender->format eq '<>') {
73 $self->log(LOGDEBUG, 'skip, null sender');
74 return 1;
75 }
76
77 if (!$sender->host || !$sender->user) {
78 $self->log(LOGDEBUG, 'skip, missing user or host');
79 return 1;
80 }
81
82 return;
83 }