Debian Codegraph @

plugins/badrcptto

raw
1 #!perl -w
2
3 =head1 SYNOPSIS
4
5 deny connections to recipients in the I<badrcptto> file
6
7 like badmailfrom, but for recipient address rather than sender
8
9 =head1 CONFIG
10
11 Recipients are matched against entries in I<config/badrcptto>. Entries can be
12 a complete email address, a host entry that starts with an @ symbol, or a
13 regular expression. For regexp pattern matches, see PATTERNS.
14
15 =head1 PATTERNS
16
17 This allows special patterns to be denied (e.g. percent hack, bangs,
18 double ats).
19
20 Patterns are stored in the format pattern\sresponse, where pattern
21 is a Perl pattern expression. Don't forget to anchor the pattern if
22 you want to restrict it from matching anywhere in the string.
23
24 qpsmtpd already ensures that the address contains an @, with something
25 to the left and right of the @.
26
27 =head1 AUTHOR
28
29 2002 - original badrcptto plugin - apparently Jim Winstead
30 https://github.com/smtpd/qpsmtpd/commits/master/plugins/check_badrcptto
31
32 2005 - pattern feature, (c) Gordon Rowell <[email protected]>
33
34 2012 - merged the two, refactored, added tests - Matt Simerson
35
36 =head1 LICENSE
37
38 This software is free software and may be distributed under the same
39 terms as qpsmtpd itself.
40
41 =cut
42
43 use strict;
44 use warnings;
45
46 use Qpsmtpd::Constants;
47 use Qpsmtpd::DSN;
48
49 sub hook_rcpt {
50 my ($self, $transaction, $recipient, %param) = @_;
51
52 return (DECLINED) if $self->is_immune();
53
54 my ($host, $to) = $self->get_host_and_to($recipient)
55 or return (DECLINED);
56
57 my @badrcptto = $self->qp->config("badrcptto") or do {
58 $self->log(LOGINFO, "skip, empty config");
59 return (DECLINED);
60 };
61
62 for my $line (@badrcptto) {
63 $line =~ s/^\s+//g; # trim leading whitespace
64 my ($bad, $reason) = split /\s+/, $line, 2;
65 next if !$bad;
66 if ($self->is_match($to, lc($bad), $host)) {
67 $self->adjust_karma(-2);
68 if ($reason) {
69 return (DENY, "mail to $bad not accepted here");
70 }
71 else {
72 return Qpsmtpd::DSN->no_such_user(
73 "mail to $bad not accepted here");
74 }
75 }
76 }
77 $self->log(LOGINFO, 'pass');
78 return (DECLINED);
79 }
80
81 sub is_match {
82 my ($self, $to, $bad, $host) = @_;
83
84 if ($bad =~ /[\/\^\$\*\+\!\%\?\\]/) { # it's a regexp
85 $self->log(LOGDEBUG, "badmailfrom pattern ($bad) match for $to");
86 if ($to =~ /$bad/i) {
87 $self->log(LOGINFO, 'fail: pattern match');
88 return 1;
89 }
90 return;
91 }
92
93 if ($bad !~ m/\@/) {
94 $self->log(LOGERROR, "badrcptto: bad config: no \@ sign in $bad");
95 return;
96 }
97
98 $bad = lc $bad;
99 $to = lc $to;
100
101 if (substr($bad, 0, 1) eq '@') {
102 if ($bad eq "\@$host") {
103 $self->log(LOGINFO, 'fail: host match');
104 return 1;
105 }
106 return;
107 }
108
109 if ($bad eq $to) {
110 $self->log(LOGINFO, 'fail: rcpt match');
111 return 1;
112 }
113 return;
114 }
115
116 sub get_host_and_to {
117 my ($self, $recipient) = @_;
118
119 if (!$recipient) {
120 $self->log(LOGERROR, 'skip: no recipient!');
121 return;
122 }
123
124 if (!$recipient->host || !$recipient->user) {
125 $self->log(LOGINFO, 'skip: missing host or user');
126 return;
127 }
128
129 my $host = lc $recipient->host;
130 return ($host, lc($recipient->user) . '@' . $host);
131 }