Debian Codegraph @

plugins/badmailfrom

raw
1 #!perl -w
2
3 =head1 NAME
4
5 check_badmailfrom - checks the badmailfrom config, with per-line reasons
6
7 =head1 DESCRIPTION
8
9 Reads the "badmailfrom" configuration like qmail-smtpd does. From the
10 qmail-smtpd docs:
11
12 "Unacceptable envelope sender addresses. qmail-smtpd will reject every
13 recipient address for a message if the envelope sender address is
14 listed in badmailfrom. A line in badmailfrom may be of the form
15 @host, meaning every address at host."
16
17 You may include an optional message after the sender address (leave a space),
18 to be used when rejecting the sender.
19
20 =head1 CONFIGURATION
21
22 =head2 reject
23
24 badmailfrom reject [ 0 | 1 | naughty ]
25
26 I<0> will not reject any connections.
27
28 I<1> will reject naughty senders.
29
30 I<connect> is the most efficient setting. It's also the default.
31
32 To reject at any other connection hook, use the I<naughty> setting and the
33 B<naughty> plugin.
34
35 =head1 PATTERNS
36
37 This plugin also supports regular expression matches. This allows
38 special patterns to be denied (e.g. FQDN-VERP, percent hack, bangs,
39 double ats).
40
41 Patterns are stored in the format pattern(\s+)response, where pattern
42 is a Perl pattern expression. Don't forget to anchor the pattern
43 (front ^ and back $) if you want to restrict it from matching
44 anywhere in the string.
45
46 ^streamsendbouncer@.*\.mailengine1\.com$ Your right-hand side VERP doesn't fool me
47 ^return.*@.*\.pidplate\.biz$ I don't want it regardless of subdomain
48 ^admin.*\.ppoonn400\.com$
49
50
51 =head1 AUTHORS
52
53 2002 - Jim Winstead - initial author of badmailfrom
54
55 2010 - Johan Almqvist <[email protected]> - pattern matching plugin
56
57 2012 - Matt Simerson - merging of the two and plugin tests
58
59 =cut
60
61 sub register {
62 my ($self, $qp) = (shift, shift);
63 $self->{_args} = {@_};
64
65 $self->{_args}{reject} = 1 if !defined $self->{_args}{reject};
66 }
67
68 sub hook_mail {
69 my ($self, $transaction, $sender, %param) = @_;
70
71 return DECLINED if $self->is_immune();
72
73 my @badmailfrom = $self->qp->config('badmailfrom');
74 if (defined $self->{_badmailfrom_config}) { # testing
75 @badmailfrom = @{$self->{_badmailfrom_config}};
76 }
77 return DECLINED if $self->is_immune_sender($sender, \@badmailfrom);
78
79 my $host = lc $sender->host;
80 my $from = lc($sender->user) . '@' . $host;
81
82 for my $config (@badmailfrom) {
83 $config =~ s/^\s+//g; # trim leading whitespace
84 my ($bad, $reason) = split /\s+/, $config, 2;
85 next unless $bad;
86 next unless $self->is_match($from, $bad, $host);
87 $reason ||= "Your envelope sender is in my badmailfrom list";
88 $self->adjust_karma(-1);
89 return $self->get_reject($reason);
90 }
91
92 $self->log(LOGINFO, "pass");
93 return DECLINED;
94 }
95
96 sub is_match {
97 my ($self, $from, $bad, $host) = @_;
98
99 if ($bad =~ /[\/\^\$\*\+\!\%\?\\]/) { # it's a regexp
100 if ($from =~ /$bad/) {
101 $self->log(LOGDEBUG, "badmailfrom pattern ($bad) match for $from");
102 return 1;
103 }
104 return;
105 }
106
107 $bad = lc $bad;
108 if ($bad !~ m/\@/) {
109 $self->log(LOGWARN, "badmailfrom: bad config: no \@ sign in $bad");
110 return;
111 }
112 if (substr($bad, 0, 1) eq '@') {
113 return 1 if $bad eq "\@$host";
114 return;
115 }
116 return if $bad ne $from;
117 return 1;
118 }
119
120 sub is_immune_sender {
121 my ($self, $sender, $badmf) = @_;
122
123 if (!scalar @$badmf) {
124 $self->log(LOGDEBUG, 'skip, empty list');
125 return 1;
126 }
127
128 if (!$sender || $sender->format eq '<>') {
129 $self->log(LOGDEBUG, 'skip, null sender');
130 return 1;
131 }
132
133 if (!$sender->host || !$sender->user) {
134 $self->log(LOGDEBUG, 'skip, missing user or host');
135 return 1;
136 }
137
138 return;
139 }