Debian Codegraph @

plugins/naughty

raw
1 #!perl -w
2
3 =head1 NAME
4
5 naughty - dispose of naughty connections
6
7 =head1 SYNOPSIS
8
9 Rather than immediately terminating naughty connections, plugins can flag the connection and dispose of it later. Examples are B<dnsbl>, B<karma>, B<greylisting>, B<resolvable_fromhost>, B<SPF>, and B<DKIM>.
10
11 =head1 BACKGROUND
12
13 Historically, deferred rejection was based on the belief that malware will retry less if we disconnect after RCPT. Observations in 2012 suggest it makes no measurable difference when we disconnect.
14
15 Disconnecting early will block connections from your users who are roaming, or whose IP space is voluntarily listed by their ISP. Deferring rejection until after the remote has had the ability to authenticate allows RBLs to be safely used on port 25 and 587.
16
17 Some (much older) RFCs suggest deferring later.
18
19 For these and other reasons, a few plugins implemented deferred rejection on their own. By having naughty, other plugins can be much simpler.
20
21 =head1 DESCRIPTION
22
23 Naughty provides the following:
24
25 =head2 consistency
26
27 With one change to the config of naughty, all plugins can reject their messages at the preferred time. I use this feature for spam filter training. When setting up a new server, I use 'naughty reject data_post' until after dspam is trained. Once the bayesian filters are trained, I change to 'naughty reject data', and avoid processing the message bodies.
28
29 =head2 efficiency
30
31 After a connection is marked as naughty, subsequent plugins can detect that and skip processing. Plugins like SpamAssassin and DSPAM can benefit from using naughty connections to train their filters.
32
33 Since many connections are from blacklisted IPs, naughty significantly reduces the resources required to dispose of them. Over 80% of my connections are disposed of after after a few DNS queries (B<dnsbl> or one DB query (B<karma>) and 0.01s of compute time.
34
35 =head2 simplicity
36
37 Rather than having plugins split processing across hooks, plugins can run to completion when they have the information they need, issue a I<reject naughty> if warranted, and be done.
38
39 This may help reduce the code divergence between the sync and async deployment models.
40
41 =head2 authentication
42
43 When a user authenticates, the naughty flag on their connection is cleared. This allows users to send email from IPs that fail connection tests such as B<dnsbl>. Note that if I<reject connect> is set, connections will not get the chance to authenticate. To allow clients a chance to authenticate, I<reject mail> works well.
44
45 =head1 HOW TO USE
46
47 Set the connection note I<naughty> to the message you wish to send the naughty sender during rejection.
48
49 $self->connection->notes('naughty', $message);
50
51 This happens for plugins automatically if they use the $self->get_reject()
52 method and have set I<reject naughty> in the plugin configuration.
53
54 =head1 CONFIGURATION
55
56 =head2 reject
57
58 naughty reject [ connect | mail | rcpt | data | data_post ]
59
60 The phase of the connection in which the naughty connection will be terminated.
61 Keep in mind that if you choose rcpt and a plugin (like B<rcpt_ok>) runs first,
62 and B<rcpt_ok> returns OK, then this plugin will not get called and the
63 message will not get rejected.
64
65 Solutions are to make sure B<naughty> is listed before rcpt_ok in config/plugins
66 or set naughty to run in a phase after the one you wish to complete.
67 In this case, use data instead of rcpt to disconnect after rcpt_ok. The latter
68 is particularly useful if your rcpt plugins skip naughty testing. In that case,
69 any recipient is accepted for naughty connections, which inhibits spammers
70 from detecting address validity.
71
72 =head2 reject_type [ temp | perm | disconnect ]
73
74 If the plugin that set naughty didn't specify, what type of rejection should
75 be sent? See docs/config.pod
76
77 =head2 loglevel
78
79 Adjust the quantity of logging for this plugin. See docs/logging.pod
80
81 =head1 EXAMPLES
82
83 Here's how to use naughty and get_reject in your plugin:
84
85 sub register {
86 my ($self, $qp) = (shift, shift);
87 $self->{_args} = { @_ };
88 $self->{_args}{reject} ||= 'naughty';
89 };
90
91 sub connect_handler {
92 my ($self, $transaction) = @_;
93 ... do a bunch of stuff ...
94 return DECLINED if is_okay();
95 return $self->get_reject( $message, $optional_log_message );
96 };
97
98 =head1 AUTHOR
99
100 2012 - Matt Simerson - [email protected]
101
102 =cut
103
104 use strict;
105 use warnings;
106
107 use Qpsmtpd::Constants;
108
109 sub register {
110 my ($self, $qp) = (shift, shift);
111 $self->log(LOGERROR, "Bad arguments") if @_ % 2;
112 $self->{_args} = {@_};
113 $self->{_args}{reject} ||= 'rcpt';
114 $self->{_args}{reject_type} ||= 'disconnect';
115
116 my $reject = lc $self->{_args}{reject};
117 my %hooks =
118 map { $_ => 1 } qw/ connect mail rcpt data data_post hook_queue_post /;
119
120 if (!$hooks{$reject}) {
121 $self->log(LOGERROR, "fail, invalid hook $reject");
122 $self->register_hook('data_post', 'naughty');
123 return;
124 }
125
126 # just in case naughty doesn't disconnect, which can happen if a plugin
127 # with the same hook returned OK before naughty ran, or ....
128 if ($reject ne 'data_post' && $reject ne 'hook_queue_post') {
129 $self->register_hook('data_post', 'naughty');
130 }
131
132 $self->log(LOGDEBUG, "registering hook $reject");
133 $self->register_hook($reject, 'naughty');
134 }
135
136 sub naughty {
137 my $self = shift;
138 my $naughty = $self->connection->notes('naughty') or do {
139 $self->log(LOGINFO, 'pass');
140 return DECLINED;
141 };
142 $self->log(LOGINFO, "disconnecting");
143 my $rtype = $self->connection->notes( 'naughty_reject_type' );
144 my $type = $self->get_reject_type( 'disconnect', $rtype );
145 return ($type, $naughty);
146 }
147