2012-04-29 10:35:59 +02:00
#!perl -w
2012-05-21 05:04:40 +02:00
2005-02-15 22:42:52 +01:00
=head1 NAME
2012-05-21 05:04:40 +02:00
greylisting - delay mail from unknown senders
2005-02-15 22:42:52 +01:00
=head1 DESCRIPTION
2012-05-21 05:04:40 +02:00
Plugin implementing the 'greylisting' algorithm proposed by Evan
2014-11-03 23:14:51 +01:00
Harris in http://projects.puremagic.com/greylisting/ and defined in
RFC 6647: http://tools.ietf.org/html/rfc6647
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.
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
Greylisting tracks incoming connections using a triplet (see TRIPLET). It
has configurable timeout periods (black/grey/white) to control whether
connections are allowed, instead of using connection counts or rates.
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
Automatic whitelisting is enabled for relayclients, whitelisted hosts,
2012-06-23 06:43:53 +02:00
whitelisted senders, TLS connections, p0f matches, and geoip matches.
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
=head1 TRIPLETS
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
In greylisting, I<remote_ip>, I<sender>, and I<recipient> are referred to
as the triplet that connections are deferred based on. This plugin allows
tracking on any or all of the three, using only the IP address by default.
A simple dbm database is used for tracking connections.
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
How that works is best explained by example:
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
A new connection arrives from the host shvj1.jpmchase.com. The sender is
chase@alerts.chase.com and the recipient is londonwhale@example.com. This is
the first connection for that triplet so the connection is deferred for
2014-12-29 04:16:34 +01:00
I<black_timeout> minutes. After the timeout elapses, shvj1.jpmchase.com retries
and successfully delivers the mail. For the next I<white_timeout> days, emails
for that triplet are not delayed.
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
The next day, shvj1.jpmchase.com tries to deliver a new email from
alerts@alerts.chase.com to jdimon@example.com. Since this triplet is new, it
will be delayed as our initial connection in the last scenario was. This
delay could end up costing over US $4B.
By default, this plugin does not enable the sender or recipient in the triplet.
Once an email from a remote server has been delivered to anyone on our server,
that remote server is whitelisted for any sender and any recipient. This is a
policy that delays less mail and is less likely to impoverish your bank.
=head1 CONFIG
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
The following parameters can be passed to greylisting:
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
=head2 remote_ip <bool>
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
Include the remote ip in the connection triplet? Default: 1
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
=head2 sender <bool>
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
Include the sender in the connection triplet? Default: 0.
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
=head2 recipient <bool>
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
Include the recipient in the connection triplet? Default: 0.
=head2 deny_late <bool>
Whether to defer denials during the 'mail' hook or later during 'data_post'
2005-02-15 22:42:52 +01:00
e.g. to allow per-recipient logging. Default: 0.
2012-05-21 05:04:40 +02:00
=head2 black_timeout <timeout_seconds>
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
The initial period during which we issue DENYSOFTs for connections from an
unknown (or timed out) 'connection triplet'. Default: 50 minutes.
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
=head2 white_timeout <timeout_seconds>
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
The period after which a known connection triplet will be considered
stale, and we will issue DENYSOFTs again. New deliveries reset the
2005-02-15 22:42:52 +01:00
timestamp on the address and renew this timeout. Default: 36 days.
2012-05-21 05:04:40 +02:00
=head2 reject <bool>
Whether to issue deferrals (DENYSOFT) for black connections. Having reject
disabled is useful for seeding the database and testing without impacting
deliveries. It is recommended to begin with I<reject 0> for a week or two
before enabling I<reject>.
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
Default: 1
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
=head2 db_dir <path>
2006-11-05 11:38:16 +01:00
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
2015-01-23 22:13:56 +01:00
=head2 redis <host[:port]>
2015-01-23 10:08:07 +01:00
Location of redis server where the greylisting DB will be stored.
Redis can be used as a scalable and clusterable alternative
to a simple DBM file. For more information, see http://redis.io
2012-05-21 05:04:40 +02:00
=head2 per_recipient <bool>
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
Flag to indicate whether to use per-recipient configs.
2005-02-15 22:42:52 +01:00
2012-05-21 05:04:40 +02:00
=head2 nfslock <bool>
2012-04-08 02:36:02 +02:00
Flag to indicate the database is stored on NFS. Uses File::NFSLock
instead of flock.
2012-05-21 05:04:40 +02:00
=head2 p0f
2010-05-11 06:55:53 +02:00
2012-05-21 05:04:40 +02:00
Enable greylisting only when certain p0f criteria is met. The required
argument is a comma delimited list of key/value pairs. The keys are the
following p0f TCP fingerprint elements: genre, detail, uptime, link, and
distance.
2010-05-11 06:55:53 +02:00
2012-05-21 05:04:40 +02:00
To greylist emails from computers whose remote OS is windows:
2010-05-11 06:55:53 +02:00
2012-05-21 05:04:40 +02:00
greylisting p0f genre,windows
2010-05-11 06:55:53 +02:00
2012-05-21 05:04:40 +02:00
To greylist only windows computers on DSL links more than 3 network hops away:
2010-05-11 06:55:53 +02:00
2012-05-21 05:04:40 +02:00
greylisting p0f genre,windows,link,dsl,distance,3
2010-05-11 06:55:53 +02:00
2012-05-21 05:04:40 +02:00
=head2 geoip
Do not greylist connections that are in the comma separated list of countries.
greylisting geoip US,UK
Prior to adding GeoIP support, I greylisted all connections from windows computers. That deters the vast majority of spam connections, but it also delays legit mail from @msn, @live.com, and a small handful of other servers. Since adding geoip support, I haven't seen a single valid mail delivery delayed.
=head2 loglevel
Adjust the quantity of logging for this plugin. See docs/logging.pod
2012-04-08 02:11:16 +02:00
2014-11-25 17:42:47 +01:00
=head1 CHANGES
The per_recipient_db configuration option has been removed.
It relied on a note that was not set anywhere in upstream QP.
The latest version of this plugin that supported this configuration
option can be found here:
https://github.com/smtpd/qpsmtpd/blob/ea2f1e89dd6b72f1c06191425e2bd8d98bea2ac6/plugins/greylisting
2005-02-15 22:42:52 +01:00
=head1 AUTHOR
Written by Gavin Carr <gavin@openfusion.com.au>.
2014-09-16 08:41:31 +02:00
2007-01-22 - nfslock feature by JT Moree <jtmoree@kahalacorp.com>
2012-05-21 05:04:40 +02:00
2014-09-16 08:41:31 +02:00
2010-05-03 - p0f feature by Matt Simerson <msimerson@cpan.org>
2010-05-11 06:55:53 +02:00
2014-09-16 08:41:31 +02:00
2012-05 - geoip, loglevel, reject added. Refactored into subs by Matt Simerson
2012-04-08 02:36:02 +02:00
2005-02-15 22:42:52 +01:00
=cut
2012-05-21 05:04:40 +02:00
use strict;
use warnings;
2014-09-16 08:41:31 +02:00
use Net::IP;
2012-05-21 05:04:40 +02:00
use Qpsmtpd::Constants;
2014-09-16 08:41:31 +02:00
my $VERSION = '0.12';
2012-05-21 05:04:40 +02:00
2013-04-21 06:50:39 +02:00
my $DENYMSG = "This mail is temporarily denied";
2012-05-21 05:04:40 +02:00
my %PERMITTED_ARGS = map { $_ => 1 } qw(per_recipient remote_ip sender
2015-01-23 22:13:56 +01:00
recipient black_timeout white_timeout deny_late db_dir redis
2012-05-21 05:04:40 +02:00
nfslock p0f reject loglevel geoip upgrade );
2014-12-29 04:16:34 +01:00
$PERMITTED_ARGS{grey_timeout} = 1; # Legacy argument now ignored
2007-03-15 07:49:23 +01:00
2005-02-15 22:42:52 +01:00
my %DEFAULTS = (
2013-04-21 06:50:39 +02:00
remote_ip => 1,
sender => 0,
recipient => 0,
reject => 1,
black_timeout => 50 * 60, # 50m
white_timeout => 36 * 3600 * 24, # 36 days
nfslock => 0,
p0f => undef,
);
2005-02-15 22:42:52 +01:00
sub register {
2012-05-21 05:04:40 +02:00
my ($self, $qp, %arg) = @_;
2014-09-16 08:41:31 +02:00
my $c = $self->qp->config('denysoft_greylist');
2013-04-21 06:50:39 +02:00
my $config = {
%DEFAULTS,
2014-09-16 08:41:31 +02:00
($c ? map { split /\s+/, $_, 2 } $c : ()),
2013-04-21 06:50:39 +02:00
%arg
};
if (my @bad = grep { !exists $PERMITTED_ARGS{$_} } sort keys %$config) {
$self->log(LOGALERT, "invalid parameter(s): " . join(',', @bad));
2012-05-21 05:04:40 +02:00
}
2013-04-21 06:50:39 +02:00
2012-05-21 05:04:40 +02:00
# backwards compatibility with deprecated 'mode' setting
2013-04-21 06:50:39 +02:00
if (defined $config->{mode} && !defined $config->{reject}) {
2012-05-21 05:04:40 +02:00
$config->{reject} = $config->{mode} =~ /testonly|off/i ? 0 : 1;
2013-04-21 06:50:39 +02:00
}
2012-05-21 05:04:40 +02:00
$self->{_args} = $config;
2015-01-23 10:08:07 +01:00
$self->init_db() or return;
$self->register_hooks();
2012-05-21 05:04:40 +02:00
$self->prune_db();
2013-04-21 06:50:39 +02:00
if ($self->{_args}{upgrade}) {
2012-05-21 05:04:40 +02:00
$self->convert_db();
2013-04-21 06:50:39 +02:00
}
2014-11-06 23:21:10 +01:00
$self->load_exclude_files();
}
2015-01-23 10:08:07 +01:00
sub register_hooks {
my ($self) = @_;
$self->register_hook('data', 'data_handler');
if ($self->{_args}{recipient} || $self->{_args}{per_recipient}) {
$self->register_hook('rcpt', 'rcpt_handler');
}
else {
$self->register_hook('mail', 'mail_handler');
}
}
2014-11-26 00:52:18 +01:00
sub init_db {
2015-01-23 10:08:07 +01:00
my ($self) = @_;
2015-01-23 22:13:56 +01:00
return $self->init_redis if $self->{_args}{redis};
2015-01-23 10:08:07 +01:00
return $self->init_dbm;
}
sub init_redis {
my ($self) = @_;
eval {
$self->db(
name => 'greylist',
class => 'Qpsmtpd::DB::Redis',
server => $self->parse_redis_server,
) or die 'Unknown error';
};
return 1 if ! $@;
2015-01-23 22:23:18 +01:00
$self->log(LOGCRIT, "Unable to connect to redis, GREYLISTING DISABLED: $@");
2015-01-23 10:08:07 +01:00
return 0;
}
sub parse_redis_server {
my ($self) = @_;
2015-01-23 22:13:56 +01:00
my $server = $self->{_args}{redis};
2015-01-23 10:08:07 +01:00
return $server if $server =~ /:/;
return "$server:6379";
}
sub init_dbm {
my ($self) = @_;
$self->db(
name => 'greylist',
class => 'Qpsmtpd::DB::File::DBM'
) or return 0;
2014-11-26 00:52:18 +01:00
my $cdir = $self->{_args}{db_dir};
$cdir = $1 if $cdir and $cdir =~ m{^([-a-zA-Z0-9./_]+)$};
# greylisting-specific hints for where to store the greylist DB
my $db_dir = $self->db->dir( $cdir, '/var/lib/qpsmtpd/greylisting' );
2015-01-23 10:08:07 +01:00
return 1 if $self->db->file_extension ne '.dbm';
2014-11-26 00:52:18 +01:00
$self->db->nfs_locking( $self->{_args}{nfslock} );
# Work around old DBM filename
2015-01-23 10:08:07 +01:00
return 1 if -f "$db_dir/greylist.dbm";
2014-11-26 00:52:18 +01:00
my $oldname = 'denysoft_greylist';
2015-01-23 10:08:07 +01:00
return 1 if ! -f "$db_dir/$oldname.dbm";
2014-11-26 00:52:18 +01:00
$self->db->name($oldname);
2015-01-23 10:08:07 +01:00
return 1;
2014-11-26 00:52:18 +01:00
}
2014-11-06 23:21:10 +01:00
sub load_exclude_files {
my ( $self ) = @_;
$self->load_exclude_file($_) for $self->qp->config('greylist_exclude_files');
}
sub load_exclude_file {
my ( $self, $filename ) = @_;
my $fh;
2014-11-06 23:23:29 +01:00
if ( ! open $fh, $filename ) {
warn "Couldn't open greylist exclude file $filename:$!\n";
2014-11-06 23:21:10 +01:00
next;
}
while ( my $line = <$fh> ) {
chomp $line;
$line =~ s/#.*//;
$line =~ s/\s//g;
next if ! $line;
$self->exclude_host($line);
}
}
sub exclude_host {
my ( $self, $pattern ) = @_;
if ( $pattern =~ /^\/(.*)\/$/ ) {
2014-11-07 00:02:27 +01:00
push @{ $self->{_exclude_re} }, qr/$1/;
2014-11-06 23:21:10 +01:00
}
2014-11-13 23:29:52 +01:00
elsif ( $self->is_valid_ip($pattern) ) {
2014-11-07 00:02:27 +01:00
$self->{_exclude_ip}{$pattern} = 1;
2014-11-06 23:21:10 +01:00
}
else {
2014-11-07 00:02:27 +01:00
$self->{_exclude_hostname}{$pattern} = 1;
2014-11-06 23:21:10 +01:00
}
}
sub exclude_file_match {
my ( $self ) = @_;
return 1 if $self->{_exclude_ip}{ $self->connection->remote_ip };
2014-11-07 00:02:27 +01:00
return 0 if ! $self->connection->remote_host;
return 1 if $self->{_exclude_hostname}{ $self->connection->remote_host };
2014-11-06 23:21:10 +01:00
for my $re ( @{ $self->{_exclude_re} || [] } ) {
return 1 if $self->connection->remote_host =~ $re;
}
return 0;
2005-02-15 22:42:52 +01:00
}
sub mail_handler {
2012-05-21 05:04:40 +02:00
my ($self, $transaction, $sender) = @_;
my ($status, $msg) = $self->greylist($transaction, $sender);
return DECLINED if $status != DENYSOFT;
2013-04-21 06:50:39 +02:00
if (!$self->{_args}{deny_late}) {
2014-09-18 03:28:51 +02:00
return DENYSOFT, $msg;
2013-04-21 06:50:39 +02:00
}
2012-05-21 05:04:40 +02:00
$transaction->notes('greylist', $msg);
return DECLINED;
2005-02-15 22:42:52 +01:00
}
sub rcpt_handler {
2013-04-21 06:50:39 +02:00
my ($self, $transaction, $rcpt) = @_;
# Load per_recipient configs
my $config = {
%{$self->{_args}},
map { split /\s+/, $_, 2 }
$self->qp->config('denysoft_greylist', {rcpt => $rcpt})
};
# Check greylisting
my $sender = $transaction->sender;
my ($status, $msg) = $self->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('greylist', $msg);
}
return DECLINED;
2005-02-15 22:42:52 +01:00
}
2015-01-23 10:08:07 +01:00
sub data_handler {
2013-04-21 06:50:39 +02:00
my ($self, $transaction) = @_;
return DECLINED unless $transaction->notes('greylist');
# Decline if ALL recipients are whitelisted
2014-09-16 08:41:31 +02:00
my $recips = scalar $transaction->recipients || 0;
if (($transaction->notes('whitelistrcpt') || 0) == $recips) {
2013-04-21 06:50:39 +02:00
$self->log(LOGWARN, "skip: all recipients whitelisted");
return DECLINED;
}
return DENYSOFT, $transaction->notes('greylist');
2005-02-15 22:42:52 +01:00
}
2012-05-21 05:04:40 +02:00
sub greylist {
my ($self, $transaction, $sender, $rcpt, $config) = @_;
$config ||= $self->{_args};
2013-04-21 06:50:39 +02:00
$self->log(LOGDEBUG,
"config: "
. join(',',
2014-12-05 18:31:59 +01:00
map { $_ . '=' . (defined $config->{$_} ? $config->{$_} : '') } sort keys %$config)
2013-04-21 06:50:39 +02:00
);
2005-02-15 22:42:52 +01:00
2014-10-27 23:24:06 +01:00
return DECLINED if $self->exclude();
2005-02-15 22:42:52 +01:00
2014-11-26 00:52:18 +01:00
2014-11-25 00:09:42 +01:00
my $key = $self->get_greylist_key($sender, $rcpt) or return DECLINED;
2010-05-11 06:55:53 +02:00
2013-04-21 06:50:39 +02:00
my $fmt = "%s:%d:%d:%d";
2007-03-15 07:51:37 +01:00
2014-11-26 23:28:52 +01:00
$self->db->lock or return DECLINED;
2014-11-26 23:06:24 +01:00
my $value = $self->db->get($key);
if ( ! $value ) {
# new IP or entry timed out - record new
2014-12-22 22:22:11 +01:00
$self->db->set( $key, sprintf $fmt, $self->now, 1, 0, 0 );
2012-05-21 05:04:40 +02:00
$self->log(LOGWARN, "fail: initial DENYSOFT, unknown");
2014-11-26 23:06:24 +01:00
return $self->cleanup_and_return();
2013-04-21 06:50:39 +02:00
}
2005-02-15 22:42:52 +01:00
2014-11-26 23:06:24 +01:00
my ( $ts, $new, $black, $white ) = split /:/, $value;
2012-05-21 05:04:40 +02:00
$self->log(LOGDEBUG, "ts: " . localtime($ts) . ", now: " . localtime);
2005-02-15 22:42:52 +01:00
2013-04-21 06:50:39 +02:00
if ($white) {
# white IP - accept unless timed out
2014-12-22 22:22:11 +01:00
if ( $self->now - $ts < $config->{white_timeout} ) {
$self->db->set( $key, sprintf $fmt, $self->now, $new, $black, ++$white );
2012-05-21 05:04:40 +02:00
$self->log(LOGINFO, "pass: white, $white deliveries");
2014-11-26 23:06:24 +01:00
return $self->cleanup_and_return(DECLINED);
2012-05-21 05:04:40 +02:00
}
else {
$self->log(LOGINFO, "key $key has timed out (white)");
}
2013-04-21 06:50:39 +02:00
}
2012-05-21 05:04:40 +02:00
2013-04-21 06:50:39 +02:00
# Black IP - deny, but don't update timestamp
2014-12-22 22:22:11 +01:00
if ( $self->now - $ts < $config->{black_timeout} ) {
2014-11-26 23:06:24 +01:00
$self->db->set( $key, sprintf $fmt, $ts, $new, ++$black, 0 );
2013-04-21 06:50:39 +02:00
$self->log(LOGWARN,
"fail: black DENYSOFT - $black deferred connections");
2014-11-26 23:06:24 +01:00
return $self->cleanup_and_return();
2012-04-08 02:36:02 +02:00
}
2012-05-21 05:04:40 +02:00
$self->log(LOGWARN, "pass: timed out (grey)");
2014-11-26 23:06:24 +01:00
return $self->cleanup_and_return(DECLINED);
2012-05-21 05:04:40 +02:00
}
2014-12-22 22:22:11 +01:00
# This exists purely to be overridden for testing
sub now { time() }
2012-05-21 05:04:40 +02:00
sub cleanup_and_return {
2014-11-26 23:06:24 +01:00
my ($self, $return_val) = @_;
2012-05-21 05:04:40 +02:00
2014-11-26 23:28:52 +01:00
$self->db->unlock;
2013-04-21 06:50:39 +02:00
return $return_val if defined $return_val; # explicit override
return DECLINED
if defined $self->{_args}{reject} && !$self->{_args}{reject};
2014-09-18 03:28:51 +02:00
return DENYSOFT, $DENYMSG;
2013-04-21 06:50:39 +02:00
}
2012-05-21 05:04:40 +02:00
2014-11-25 00:09:42 +01:00
sub get_greylist_key {
2013-04-21 06:50:39 +02:00
my $self = shift;
2012-05-21 05:04:40 +02:00
my $sender = shift || $self->qp->transaction->sender;
2013-04-21 06:50:39 +02:00
my $rcpt = shift || ($self->qp->transaction->recipients)[0];
2012-05-21 05:04:40 +02:00
my @key;
2013-04-21 06:50:39 +02:00
if ($self->{_args}{remote_ip}) {
my $nip = Net::IP->new($self->qp->connection->remote_ip);
push @key, $nip->intip; # convert IP to integer
}
2012-05-21 05:04:40 +02:00
push @key, $sender->address || '' if $self->{_args}{sender};
2013-04-21 06:50:39 +02:00
push @key, $rcpt->address if $rcpt && $self->{_args}{recipient};
if (!scalar @key) {
2012-05-21 05:04:40 +02:00
$self->log(LOGERROR, "enable one of remote_ip, sender, or recipient!");
return;
2013-04-21 06:50:39 +02:00
}
2012-05-21 05:04:40 +02:00
return join ':', @key;
2013-04-21 06:50:39 +02:00
}
2012-05-21 05:04:40 +02:00
sub convert_db {
my $self = shift;
2014-11-26 23:28:52 +01:00
$self->db->lock or return DECLINED;
2014-11-26 23:06:24 +01:00
my $count = $self->db->size;
2012-05-21 05:04:40 +02:00
my $converted = 0;
2014-11-26 23:06:24 +01:00
foreach my $key ( $self->db->get_keys ) {
2013-04-21 06:50:39 +02:00
my (@parts) = split /:/, $key;
next if $parts[0] =~ /^[\d]+$/; # already converted
2012-05-21 05:04:40 +02:00
$converted++;
2013-04-21 06:50:39 +02:00
my $nip = Net::IP->new($parts[0]);
$parts[0] = $nip->intip; # convert IP to integer
2012-05-21 05:04:40 +02:00
my $new_key = join ':', @parts;
2014-11-26 23:06:24 +01:00
$self->db->set( $new_key, $self->db->get($key) );
$self->db->delete( $key );
2013-04-21 06:50:39 +02:00
}
2014-11-26 23:06:24 +01:00
$self->db->unlock;
2013-04-21 06:50:39 +02:00
$self->log(LOGINFO, "converted $converted of $count DB entries");
2015-01-22 00:31:47 +01:00
return DECLINED;
2013-04-21 06:50:39 +02:00
}
2012-05-21 05:04:40 +02:00
sub prune_db {
my $self = shift;
2014-11-26 23:28:52 +01:00
$self->db->lock or return DECLINED;
2014-11-26 23:06:24 +01:00
my $count = $self->db->size;
2012-05-21 05:04:40 +02:00
my $pruned = 0;
2014-11-26 23:06:24 +01:00
foreach my $key ( $self->db->get_keys ) {
my ($ts, $new, $black, $white) = split /:/, $self->db->get($key);
2014-12-22 22:22:11 +01:00
my $age = $self->now - $ts;
2012-05-21 05:04:40 +02:00
next if $age < $self->{_args}{white_timeout};
$pruned++;
2014-11-26 23:06:24 +01:00
$self->db->delete($key);
2013-04-21 06:50:39 +02:00
}
2014-11-26 23:06:24 +01:00
$self->db->unlock;
2013-04-21 06:50:39 +02:00
$self->log(LOGINFO, "pruned $pruned of $count DB entries");
2015-01-22 00:31:47 +01:00
return DECLINED;
2013-04-21 06:50:39 +02:00
}
2012-05-21 05:04:40 +02:00
2014-10-27 23:24:06 +01:00
sub exclude {
2014-10-28 20:37:56 +01:00
my ( $self ) = @_;
2014-10-27 23:24:06 +01:00
return 1 if $self->is_immune();
2014-12-23 00:00:29 +01:00
return 1 if $self->{_args}{p0f} && ! $self->p0f_match();
2014-10-27 23:24:06 +01:00
return 1 if $self->geoip_match();
2014-11-06 23:21:10 +01:00
return 1 if $self->exclude_file_match();
2014-10-28 20:37:56 +01:00
return;
2014-10-27 23:24:06 +01:00
}
2010-05-11 06:55:53 +02:00
sub p0f_match {
my $self = shift;
2013-04-21 06:50:39 +02:00
return if !$self->{_args}{p0f};
2012-06-03 03:44:46 +02:00
2010-05-11 06:55:53 +02:00
my $p0f = $self->connection->notes('p0f');
2013-04-21 06:50:39 +02:00
if (!$p0f || !ref $p0f) { # p0f fingerprint info not found
2012-05-21 05:04:40 +02:00
$self->LOGINFO(LOGERROR, "p0f info missing");
return;
2013-04-21 06:50:39 +02:00
}
2010-05-11 06:55:53 +02:00
my %valid_matches = map { $_ => 1 } qw( genre detail uptime link distance );
2013-04-21 06:50:39 +02:00
my %requested_matches = split(/\,/, $self->{_args}{p0f});
2010-05-11 06:55:53 +02:00
foreach my $key (keys %requested_matches) {
2013-04-21 06:50:39 +02:00
next if !$key;
if (!defined $valid_matches{$key}) {
$self->log(LOGERROR, "discarding invalid match key ($key)");
2012-05-21 05:04:40 +02:00
next;
2013-04-21 06:50:39 +02:00
}
2010-05-11 06:55:53 +02:00
my $value = $requested_matches{$key};
2013-04-21 06:50:39 +02:00
next if !defined $value; # bad config setting?
next if !defined $p0f->{$key}; # p0f didn't detect the value
2012-05-21 05:04:40 +02:00
2013-04-21 06:50:39 +02:00
if ($key eq 'distance' && $p0f->{$key} > $value) {
2012-05-21 05:04:40 +02:00
$self->log(LOGDEBUG, "p0f distance match ($value)");
return 1;
2013-04-21 06:50:39 +02:00
}
if ($key eq 'genre' && $p0f->{$key} =~ /$value/i) {
2012-05-21 05:04:40 +02:00
$self->log(LOGDEBUG, "p0f genre match ($value)");
return 1;
2013-04-21 06:50:39 +02:00
}
if ($key eq 'uptime' && $p0f->{$key} < $value) {
2012-05-21 05:04:40 +02:00
$self->log(LOGDEBUG, "p0f uptime match ($value)");
return 1;
2013-04-21 06:50:39 +02:00
}
if ($key eq 'link' && $p0f->{$key} =~ /$value/i) {
2012-05-21 05:04:40 +02:00
$self->log(LOGDEBUG, "p0f link match ($value)");
return 1;
2013-04-21 06:50:39 +02:00
}
2010-05-11 06:55:53 +02:00
}
2012-05-21 05:04:40 +02:00
$self->log(LOGINFO, "skip: no p0f match");
2010-05-11 06:55:53 +02:00
return;
}
2012-05-21 05:04:40 +02:00
sub geoip_match {
my $self = shift;
2013-04-21 06:50:39 +02:00
return if !$self->{_args}{geoip};
2012-06-03 03:44:46 +02:00
2012-05-21 05:04:40 +02:00
my $country = $self->connection->notes('geoip_country');
2013-04-21 06:50:39 +02:00
my $c_name = $self->connection->notes('geoip_country_name') || '';
2012-05-21 05:04:40 +02:00
2013-04-21 06:50:39 +02:00
if (!$country) {
2012-05-21 05:04:40 +02:00
$self->LOGINFO(LOGNOTICE, "skip: no geoip country");
return;
2013-04-21 06:50:39 +02:00
}
2012-05-21 05:04:40 +02:00
my @countries = split /,/, $self->{_args}{geoip};
2013-04-21 06:50:39 +02:00
foreach (@countries) {
2012-05-21 05:04:40 +02:00
$self->LOGINFO(LOGINFO, "pass: geoip country match ($_, $c_name)");
return 1 if lc $_ eq lc $country;
2013-04-21 06:50:39 +02:00
}
2012-05-21 05:04:40 +02:00
$self->LOGINFO(LOGINFO, "skip: no geoip match ($c_name)");
return;
}
2012-04-08 02:36:02 +02:00