File: greylisting

package info (click to toggle)
qpsmtpd 0.40-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,024 kB
  • ctags: 393
  • sloc: perl: 6,462; sh: 383; makefile: 54
file content (296 lines) | stat: -rw-r--r-- 9,686 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
=head1 NAME

denysoft_greylist

=head1 DESCRIPTION

Plugin to implement the 'greylisting' algorithm proposed by Evan 
Harris in http://projects.puremagic.com/greylisting/. Greylisting is 
a form of denysoft filter, where unrecognised new connections are 
temporarily denied for some initial period, to foil spammers using 
fire-and-forget spamware, http_proxies, etc.

Greylisting adds two main features: it tracks incoming connections 
using a triplet of remote IP address, sender, and recipient, rather 
than just using the remote IP; and it uses a set of timeout periods 
(black/grey/white) to control whether connections are allowed, instead 
of using connection counts or rates.

This plugin allows connection tracking on any or all of IP address, 
sender, and recipient (but uses IP address only, by default), with 
configurable greylist timeout periods. A simple dbm database is used 
for tracking connections, and relayclients are always allowed 
through. The plugin supports whitelisting using the whitelist_soft
plugin (optional).


=head1 CONFIG

The following parameters can be passed to denysoft_greylist:

=over 4

=item remote_ip <bool>

Whether to include the remote ip address in tracking connections.
Default: 1.

=item sender <bool>

Whether to include the sender in tracking connections. Default: 0.

=item recipient <bool>

Whether to include the recipient in tracking connections. Default: 0.

=item deny_late <bool>

Whether to defer denials during the 'mail' hook until 'data_post'
e.g. to allow per-recipient logging. Default: 0.

=item black_timeout <timeout_seconds>

The initial period, in seconds, for which we issue DENYSOFTs for 
connections from an unknown (or timed out) IP address and/or sender
and/or recipient (a 'connection triplet'). Default: 50 minutes.

=item grey_timeout <timeout_seconds>

The subsequent 'grey' period, after the initial black blocking period,
when we will accept a delivery from a formerly-unknown connection
triplet. If a new connection is received during this time, we will 
record a successful delivery against this IP address, which whitelists 
it for future deliveries (see following). Default: 3 hours 20 minutes.

=item white_timeout <timeout_seconds>

The period after which a known connection triplet will be considered 
stale, and we will issue DENYSOFTs again. New deliveries reset the 
timestamp on the address and renew this timeout. Default: 36 days.

=item mode ( denysoft | testonly | off )

Operating mode. In 'denysoft' mode we log and track connections and 
issue DENYSOFTs for black connections; in 'testonly' mode we log and 
track connections as normal, but never actually issue DENYSOFTs 
(useful for seeding the database and testing without impacting 
deliveries); in 'off' mode we do nothing (useful for turning 
greylisting off globally if using per_recipient configs). 
Default: denysoft.

=item db_dir <path>

Path to a directory in which the greylisting DB will be stored.  This
directory must be writable by the qpsmtpd user.  By default, the first
usable directory from the following list will be used:

=over 4

=item /var/lib/qpsmtpd/greylisting

=item I<BINDIR>/var/db (where BINDIR is the location of the qpsmtpd binary)

=item I<BINDIR>/config

=back

=item per_recipient <bool>

Flag to indicate whether to use per-recipient configs. 

=item per_recipient_db <bool>

Flag to indicate whether to use per-recipient greylisting 
databases (default is to use a shared database).  Per-recipient configuration
directories, if determined, supercede I<db_dir>.

=back

=head1 BUGS

Database locking is implemented using flock, which may not work on 
network filesystems e.g. NFS. If this is a problem, you may want to
use something like File::NFSLock instead.

=head1 AUTHOR

Written by Gavin Carr <gavin@openfusion.com.au>.

=cut

BEGIN { @AnyDBM_File::ISA = qw(DB_File GDBM_File NDBM_File) }
use AnyDBM_File;
use Fcntl qw(:DEFAULT :flock);
use strict;

my $VERSION = '0.07';

my $DENYMSG = "This mail is temporarily denied";
my ($QPHOME) = ($0 =~ m!(.*?)/([^/]+)$!);
my $DB = "denysoft_greylist.dbm";
my %PERMITTED_ARGS = map { $_ => 1 } qw(per_recipient remote_ip sender recipient 
  black_timeout grey_timeout white_timeout deny_late mode db_dir);

my %DEFAULTS = (
  remote_ip => 1,
  sender => 0,
  recipient => 0,
  black_timeout => 50 * 60,
  grey_timeout =>  3 * 3600 + 20 * 60,
  white_timeout => 36 * 24 * 3600,
  mode => 'denysoft',
);

sub register {
  my ($self, $qp, %arg) = @_;
  my $config = { %DEFAULTS, 
    map { split /\s+/, $_, 2 } $self->qp->config('denysoft_greylist'), 
    %arg };
  if (my @bad = grep { ! exists $PERMITTED_ARGS{$_} } sort keys %$config) {
    $self->log(LOGALERT, "invalid parameter(s): " . join(',',@bad));
  }
  $self->{_greylist_config} = $config;
  unless ($config->{recipient} || $config->{per_recipient}) {
    $self->register_hook("mail", "mail_handler");
  } else {
    $self->register_hook("rcpt", "rcpt_handler");
  }
}

sub mail_handler {
  my ($self, $transaction, $sender) = @_;
  my ($status, $msg) = $self->denysoft_greylist($transaction, $sender, undef);
  if ($status == DENYSOFT) {
    my $config = $self->{_greylist_config};
    return DENYSOFT, $msg unless $config->{deny_late};
    $transaction->notes('denysoft_greylist', $msg) 
  }
  return DECLINED;
}

sub rcpt_handler {
  my ($self, $transaction, $rcpt) = @_;
  # Load per_recipient configs
  my $config = { %{$self->{_greylist_config}},
    map { split /\s+/, $_, 2 } $self->qp->config('denysoft_greylist', { rcpt => $rcpt }) };
  # Check greylisting
  my $sender = $transaction->sender;
  my ($status, $msg) = $self->denysoft_greylist($transaction, $sender, $rcpt, $config);
  if ($status == DENYSOFT) {
    # Deny here (per-rcpt) unless this is a <> sender, for smtp probes
    return DENYSOFT, $msg if $sender->address;
    $transaction->notes('denysoft_greylist', $msg);
  }
  return DECLINED;
}

sub hook_data {
  my ($self, $transaction) = @_;
  my $note = $transaction->notes('denysoft_greylist');
  return DECLINED unless $note;
  # Decline if ALL recipients are whitelisted
  if (($transaction->notes('whitelistrcpt')||0) == scalar($transaction->recipients)) {
    $self->log(LOGWARN,"all recipients whitelisted - skipping");
    return DECLINED;
  }
  return DENYSOFT, $note;
}

sub denysoft_greylist {
  my ($self, $transaction, $sender, $rcpt, $config) = @_;
  $config ||= $self->{_greylist_config};
  $self->log(LOGDEBUG, "config: " . join(',',map { $_ . '=' . $config->{$_} } sort keys %$config));

  # Always allow relayclients and whitelisted hosts/senders
  return DECLINED if $self->qp->connection->relay_client();
  return DECLINED if $self->qp->connection->notes('whitelisthost');
  return DECLINED if $transaction->notes('whitelistsender');

  if ($config->{db_dir} =~ m{^([-a-zA-Z0-9./_]+)$}) {
    $config->{db_dir} = $1; 
  }

  # Setup database location
  my $dbdir = $transaction->notes('per_rcpt_configdir') 
    if $config->{per_recipient_db};
  for my $d ($dbdir, $config->{db_dir}, "/var/lib/qpsmtpd/greylisting",
             "$QPHOME/var/db", "$QPHOME/config") {
    last if $dbdir ||= $d && -d $d && $d;
  }
  my $db = "$dbdir/$DB";
  $self->log(LOGINFO,"using $db as greylisting database");

  my $remote_ip = $self->qp->connection->remote_ip;
  my $fmt = "%s:%d:%d:%d";

  # Check denysoft db
  unless (open LOCK, ">$db.lock") {
    $self->log(LOGCRIT, "opening lockfile failed: $!");
    return DECLINED;
  }
  unless (flock LOCK, LOCK_EX) {
    $self->log(LOGCRIT, "flock of lockfile failed: $!");
    close LOCK;
    return DECLINED;
  }
  my %db = ();
  unless (tie %db, 'AnyDBM_File', $db, O_CREAT|O_RDWR, 0600) {
    $self->log(LOGCRIT, "tie to database $db failed: $!");
    close LOCK;
    return DECLINED;
  }
  my @key;
  push @key, $remote_ip             if $config->{remote_ip};
  push @key, $sender->address || '' if $config->{sender};
  push @key, $rcpt->address         if $rcpt && $config->{recipient};
  my $key = join ':', @key;
  my ($ts, $new, $black, $white) = (0,0,0,0);
  if ($db{$key}) {
    ($ts, $new, $black, $white) = split /:/, $db{$key};
    $self->log(LOGERROR, "ts: " . localtime($ts) . ", now: " . localtime);
    if (! $white) {
      # Black IP - deny, but don't update timestamp
      if (time - $ts < $config->{black_timeout}) {
        $db{$key} = sprintf $fmt, $ts, $new, ++$black, 0;
        $self->log(LOGCRIT, "key $key black DENYSOFT - $black failed connections");
        untie %db;
        close LOCK;
        return $config->{mode} eq 'testonly' ? DECLINED : DENYSOFT, $DENYMSG;
      }
      # Grey IP - accept unless timed out
      elsif (time - $ts < $config->{grey_timeout}) {
        $db{$key} = sprintf $fmt, time, $new, $black, 1;
        $self->log(LOGCRIT, "key $key updated grey->white");
        untie %db;
        close LOCK;
        return DECLINED;
      }
      else {
        $self->log(LOGERROR, "key $key has timed out (grey)");
      }
    }
    # White IP - accept unless timed out
    else {
      if (time - $ts < $config->{white_timeout}) {
        $db{$key} = sprintf $fmt, time, $new, $black, ++$white;
        $self->log(LOGCRIT, "key $key is white, $white deliveries");
        untie %db;
        close LOCK;
        return DECLINED;
      }
      else {
        $self->log(LOGERROR, "key $key has timed out (white)");
      }
    }
  }

  # New ip or entry timed out - record new and return DENYSOFT
  $db{$key} = sprintf $fmt, time, ++$new, $black, 0;
  $self->log(LOGCRIT, "key $key initial DENYSOFT, unknown");
  untie %db;
  close LOCK;
  return $config->{mode} eq 'testonly' ? DECLINED : DENYSOFT, $DENYMSG;
}

# arch-tag: 6ef5919e-404b-4c87-bcfe-7e9f383f3901