2012-06-02 08:45:25 +02:00
#!perl -w
=head1 NAME
karma - reward nice and penalize naughty mail senders
=head1 SYNOPSIS
2013-03-13 08:19:48 +01:00
Karma tracks sender history, allowing us to provide differing levels
2012-06-02 08:45:25 +02:00
of service to naughty, nice, and unknown senders.
=head1 DESCRIPTION
Karma records the number of nice, naughty, and total connections from mail
senders. After sending a naughty message, if a sender has more naughty than
nice connections, they are penalized for I<penalty_days>. Connections
2013-03-13 08:19:48 +01:00
from senders in the penalty box are rejected per the settings in I<reject>.
2012-06-02 08:45:25 +02:00
Karma provides other plugins with a karma value they can use to be more
lenient, strict, or skip processing entirely.
Karma is small, fast, and ruthlessly efficient. Karma can be used to craft
2013-04-24 22:30:28 +02:00
custom connection policies such as these two examples:
2012-06-02 08:45:25 +02:00
2013-04-24 22:30:28 +02:00
=over 4
2012-06-02 08:45:25 +02:00
2013-04-21 05:53:27 +02:00
Hi there, well known and well behaved sender. Please help yourself to greater concurrency (hosts_allow), multiple recipients (karma), and no delays (early_sender).
2012-06-02 08:45:25 +02:00
2013-04-21 05:53:27 +02:00
Hi there, naughty sender. You get a max concurrency of 1, max recipients of 2, and SMTP delays.
2012-06-02 08:45:25 +02:00
2013-04-24 22:30:28 +02:00
=back
2012-06-02 08:45:25 +02:00
=head1 CONFIG
=head2 negative <integer>
How negative a senders karma can get before we penalize them for sending a
2013-04-24 22:30:28 +02:00
naughty message. Karma is the number of nice - naughty connections.
2012-06-02 08:45:25 +02:00
Default: 1
Examples:
2012-06-22 11:38:01 +02:00
negative 1: 0 nice - 1 naughty = karma -1, penalize
negative 1: 1 nice - 1 naughty = karma 0, okay
negative 2: 1 nice - 2 naughty = karma -1, okay
negative 2: 1 nice - 3 naughty = karma -2, penalize
2012-06-02 08:45:25 +02:00
With the default negative limit of one, there's a very small chance you could
penalize a "mostly good" sender. Raising it to 2 reduces that possibility to
improbable.
=head2 penalty_days <days>
The number of days a naughty sender is refused connections. Use a decimal
value to penalize for portions of days.
karma penalty_days 1
Default: 1
=head2 reject
2012-06-22 11:38:01 +02:00
karma reject [ 0 | 1 | connect | naughty ]
2012-06-02 08:45:25 +02:00
I<0> will not reject any connections.
I<1> will reject naughty senders.
2013-04-24 22:30:28 +02:00
I<connect> is the most efficient setting.
2012-06-02 08:45:25 +02:00
2012-06-22 11:38:01 +02:00
To reject at any other connection hook, use the I<naughty> setting and the
B<naughty> plugin.
2012-06-02 08:45:25 +02:00
=head2 db_dir <path>
Path to a directory in which the DB will be stored. This directory must be
writable by the qpsmtpd user. If unset, the first usable directory from the
following list will be used:
=over 4
=item /var/lib/qpsmtpd/karma
=item I<BINDIR>/var/db (where BINDIR is the location of the qpsmtpd binary)
=item I<BINDIR>/config
=back
=head2 loglevel
Adjust the quantity of logging for this plugin. See docs/logging.pod
=head1 BENEFITS
2012-06-22 11:38:01 +02:00
Karma reduces the resources wasted by naughty mailers. When used with
I<reject connect>, naughty senders are disconnected in about 0.1 seconds.
2012-06-02 08:45:25 +02:00
The biggest gains to be had are by having heavy plugins (spamassassin, dspam,
2013-03-23 07:15:24 +01:00
virus filters) set the B<karma> connection note (see KARMA) when they encounter
2012-06-02 08:45:25 +02:00
naughty senders. Reasons to send servers to the penalty box could include
sending a virus, early talking, or sending messages with a very high spam
score.
This plugin does not penalize connections with transaction notes I<relayclient>
2013-04-24 22:30:28 +02:00
or I<whitelisthost> set. These notes would have been set by the B<relay>,
2012-06-02 08:45:25 +02:00
B<whitelist>, and B<dns_whitelist_soft> plugins. Obviously, those plugins must
run before B<karma> for that to work.
=head1 KARMA
2013-03-23 07:15:24 +01:00
No attempt is made by this plugin to determine karma. It is up to other
plugins to reward well behaved senders with positive karma and smite poorly
behaved senders with negative karma. See B<USING KARMA IN OTHER PLUGINS>
2012-06-02 08:45:25 +02:00
After the connection ends, B<karma> will record the result. Mail servers whose
naughty connections exceed nice ones are sent to the penalty box. Servers in
the penalty box will be tersely disconnected for I<penalty_days>. Here is
an example connection from an IP in the penalty box:
73122 Connection from smtp.midsetmediacorp.com [64.185.226.65]
73122 (connect) ident::geoip: US, United States
73122 (connect) ident::p0f: Windows 7 or 8
73122 (connect) earlytalker: pass: 64.185.226.65 said nothing spontaneous
73122 (connect) relay: skip: no match
73122 (connect) karma: fail
2013-03-13 08:19:48 +01:00
73122 550 You were naughty. You are cannot connect for 0.99 more days.
2012-06-02 08:45:25 +02:00
73122 click, disconnecting
73122 (post-connection) connection_time: 1.048 s.
2012-06-22 11:38:01 +02:00
If we only set negative karma, we will almost certainly penalize servers we
2012-06-02 08:45:25 +02:00
want to receive mail from. For example, a Yahoo user sends an egregious spam
to a user on our server. Now nobody on our server can receive email from that
Yahoo server for I<penalty_days>. This should happen approximately 0% of
the time if we are careful to also set positive karma.
2012-06-22 11:38:01 +02:00
=head1 KARMA HISTORY
Karma maintains a history for each IP. When a senders history has decreased
below -5 and they have never sent a good message, they get a karma bonus.
The bonus tacks on an extra day of blocking for every naughty message they
2013-03-23 07:15:24 +01:00
send.
2012-06-22 11:38:01 +02:00
Example: an unknown sender delivers a spam. They get a one day penalty_box.
After 5 days, 5 spams, 5 penalties, and 0 nice messages, they get a six day
2013-03-23 07:15:24 +01:00
penalty. The next offense gets a 7 day penalty, and so on.
2012-06-22 11:38:01 +02:00
2012-06-02 08:45:25 +02:00
=head1 USING KARMA
To get rid of naughty connections as fast as possible, run karma before other
connection plugins. Plugins that trigger DNS lookups or impose time delays
should run after B<karma>. In this example, karma runs before all but the
ident plugins.
89011 Connection from Unknown [69.61.27.204]
89011 (connect) ident::geoip: US, United States
89011 (connect) ident::p0f: Linux 3.x
89011 (connect) karma: fail, 1 naughty, 0 nice, 1 connects
89011 550 You were naughty. You are penalized for 0.99 more days.
89011 click, disconnecting
89011 (post-connection) connection_time: 0.118 s.
88798 cleaning up after 89011
Unlike RBLs, B<karma> only penalizes IPs that have sent us spam, and only when
2013-03-23 07:15:24 +01:00
those senders have sent us more spam than ham.
2012-06-02 08:45:25 +02:00
=head1 USING KARMA IN OTHER PLUGINS
This plugin sets the connection note I<karma_history>. Your plugin can
use the senders karma to be more gracious or rude to senders. The value of
2012-06-30 21:37:25 +02:00
I<karma_history> is the number of nice connections minus naughty
2012-06-02 08:45:25 +02:00
ones. The higher the number, the better you should treat the sender.
2012-06-30 21:37:25 +02:00
To alter a connections karma based on its behavior, do this:
$self->adjust_karma( -1 ); # lower karma (naughty)
$self->adjust_karma( 1 ); # raise karma (good)
2012-06-02 08:45:25 +02:00
=head1 EFFECTIVENESS
In the first 24 hours, B<karma> rejected 8% of all connections. After one
week of running with I<penalty_days 1>, karma has rejected 15% of all
connections.
This plugins effectiveness results from the propensity of naughty senders
to be repeat offenders. Limiting them to a single offense per day(s) greatly
2012-06-30 21:37:25 +02:00
reduces the resources they can waste.
2012-06-02 08:45:25 +02:00
Of the connections that had previously passed all other checks and were caught
only by spamassassin and/or dspam, B<karma> rejected 31 percent. Since
spamassassin and dspam consume more resources than others plugins, this plugin
seems to be a very big win.
=head1 DATABASE
2013-03-23 07:15:24 +01:00
Connection summaries are stored in a database. The database key is the integer
value of the remote IP. The DB value is a : delimited list containing a penalty
2012-06-02 08:45:25 +02:00
box start time (if the server is/was on timeout) and the count of naughty,
nice, and total connections. The database can be listed and searched with the
2012-06-30 21:37:25 +02:00
karma_tool script.
2012-06-02 08:45:25 +02:00
=head1 BUGS & LIMITATIONS
2013-03-13 08:19:48 +01:00
This plugin is reactionary. Like the FBI, it doesn't do anything until
after a crime has been committed.
2012-06-02 08:45:25 +02:00
There is little to be gained by listing servers that are already on DNS
2013-03-13 08:19:48 +01:00
blacklists, send to invalid users, earlytalkers, etc. Those already have
2012-06-02 08:45:25 +02:00
very lightweight tests.
2013-12-18 06:15:20 +01:00
=head1 TODO
* Avoid storing results for DNSBL listed IPs
* some type of ASN integration, for tracking karma of 'neighborhoods'
2012-06-02 08:45:25 +02:00
=head1 AUTHOR
2013-12-18 06:15:20 +01:00
2013 - MS - Addeded penalty for spammy TLDs
2012-06-02 08:45:25 +02:00
2012 - Matt Simerson - msimerson@cpan.org
=head1 ACKNOWLEDGEMENTS
Gavin Carr's DB implementation in the greylisting plugin.
=cut
use strict;
use warnings;
use Qpsmtpd::Constants;
BEGIN { @AnyDBM_File::ISA = qw(DB_File GDBM_File NDBM_File) }
use AnyDBM_File;
use Fcntl qw(:DEFAULT :flock LOCK_EX LOCK_NB);
use Net::IP;
sub register {
2013-04-21 06:50:39 +02:00
my ($self, $qp) = (shift, shift);
2012-06-02 08:45:25 +02:00
$self->log(LOGERROR, "Bad arguments") if @_ % 2;
2013-04-21 06:50:39 +02:00
$self->{_args} = {@_};
2012-06-02 08:45:25 +02:00
$self->{_args}{negative} ||= 1;
$self->{_args}{penalty_days} ||= 1;
$self->{_args}{reject_type} ||= 'disconnect';
2013-04-21 06:50:39 +02:00
if (!defined $self->{_args}{reject}) {
2012-06-22 11:38:01 +02:00
$self->{_args}{reject} = 'naughty';
2013-04-21 06:50:39 +02:00
}
2012-06-02 08:45:25 +02:00
#$self->prune_db(); # keep the DB compact
2013-04-21 06:50:39 +02:00
$self->register_hook('connect', 'connect_handler');
2013-12-18 06:15:20 +01:00
$self->register_hook('mail', 'from_handler');
$self->register_hook('rcpt', 'rcpt_handler');
2013-04-21 06:50:39 +02:00
$self->register_hook('data', 'data_handler');
2013-04-24 22:30:28 +02:00
$self->register_hook('data_post', 'data_handler');
2013-04-21 06:50:39 +02:00
$self->register_hook('disconnect', 'disconnect_handler');
2012-06-02 08:45:25 +02:00
}
2013-03-13 08:19:48 +01:00
sub hook_pre_connection {
2013-04-21 06:50:39 +02:00
my ($self, $transaction, %args) = @_;
2013-03-13 08:19:48 +01:00
$self->connection->notes('karma_history', 0);
my $remote_ip = $args{remote_ip};
2013-04-21 06:50:39 +02:00
2014-11-25 00:09:42 +01:00
my $db = $self->get_dbm_location();
my $lock = $self->get_dbm_lock($db) or return DECLINED;
my $tied = $self->get_dbm_tie($db, $lock) or return DECLINED;
my $key = $self->get_karma_key($remote_ip) or do {
2013-04-21 06:50:39 +02:00
$self->log(LOGINFO, "skip, unable to get DB key");
2013-03-13 08:19:48 +01:00
return DECLINED;
};
2013-04-21 06:50:39 +02:00
if (!$tied->{$key}) {
2013-03-23 07:15:24 +01:00
$self->log(LOGDEBUG, "pass, no record");
2013-04-21 06:50:39 +02:00
return $self->cleanup_and_return($tied, $lock);
}
2013-03-13 08:19:48 +01:00
2013-04-21 06:50:39 +02:00
my ($penalty_start_ts, $naughty, $nice, $connects) =
2013-04-30 04:06:47 +02:00
$self->parse_db_record($tied->{$key});
2013-03-13 08:19:48 +01:00
$self->calc_karma($naughty, $nice);
2013-04-21 06:50:39 +02:00
return $self->cleanup_and_return($tied, $lock);
}
2013-03-13 08:19:48 +01:00
2012-06-02 08:45:25 +02:00
sub connect_handler {
my $self = shift;
2013-04-21 06:50:39 +02:00
$self->connection->notes('karma', 0); # default
2012-06-02 08:45:25 +02:00
return DECLINED if $self->is_immune();
2014-11-25 00:09:42 +01:00
my $db = $self->get_dbm_location();
my $lock = $self->get_dbm_lock($db) or return DECLINED;
my $tied = $self->get_dbm_tie($db, $lock) or return DECLINED;
my $key = $self->get_karma_key() or do {
2013-04-21 06:50:39 +02:00
$self->log(LOGINFO, "skip, unable to get DB key");
2012-06-23 05:57:43 +02:00
return DECLINED;
};
2012-06-02 08:45:25 +02:00
2013-04-21 06:50:39 +02:00
if (!$tied->{$key}) {
2012-06-02 08:45:25 +02:00
$self->log(LOGINFO, "pass, no record");
2013-04-21 06:50:39 +02:00
return $self->cleanup_and_return($tied, $lock);
}
2012-06-02 08:45:25 +02:00
2013-04-21 06:50:39 +02:00
my ($penalty_start_ts, $naughty, $nice, $connects) =
2013-04-30 04:06:47 +02:00
$self->parse_db_record($tied->{$key});
2012-06-02 08:45:25 +02:00
my $summary = "$naughty naughty, $nice nice, $connects connects";
2012-06-22 11:38:01 +02:00
my $karma = $self->calc_karma($naughty, $nice);
2012-06-02 08:45:25 +02:00
2013-04-21 06:50:39 +02:00
if (!$penalty_start_ts) {
2012-06-02 08:45:25 +02:00
$self->log(LOGINFO, "pass, no penalty ($summary)");
2013-04-21 06:50:39 +02:00
return $self->cleanup_and_return($tied, $lock);
}
2012-06-02 08:45:25 +02:00
my $days_old = (time - $penalty_start_ts) / 86400;
2013-04-21 06:50:39 +02:00
if ($days_old >= $self->{_args}{penalty_days}) {
2012-06-02 08:45:25 +02:00
$self->log(LOGINFO, "pass, penalty expired ($summary)");
2013-04-21 06:50:39 +02:00
return $self->cleanup_and_return($tied, $lock);
}
2012-06-02 08:45:25 +02:00
$tied->{$key} = join(':', $penalty_start_ts, $naughty, $nice, ++$connects);
2013-04-21 06:50:39 +02:00
$self->cleanup_and_return($tied, $lock);
2012-06-02 08:45:25 +02:00
my $left = sprintf "%.2f", $self->{_args}{penalty_days} - $days_old;
2013-03-13 08:19:48 +01:00
my $mess = "You were naughty. You cannot connect for $left more days.";
2012-06-02 08:45:25 +02:00
2013-04-21 06:50:39 +02:00
return $self->get_reject($mess, $karma);
2012-06-02 08:45:25 +02:00
}
2013-04-30 04:06:47 +02:00
sub from_handler {
2013-12-18 06:15:20 +01:00
my ($self,$transaction, $sender, %args) = @_;
2013-04-30 04:06:47 +02:00
# test if sender has placed an illegal (RFC (2)821) space in envelope from
my $full_from = $self->connection->notes('envelope_from');
$self->illegal_envelope_format( $full_from );
2013-12-18 06:15:20 +01:00
my %spammy_tlds = (
map { $_ => 4 } qw/ info pw /,
map { $_ => 3 } qw/ tw biz /,
map { $_ => 2 } qw/ cl br fr be jp no se sg /,
);
foreach my $tld ( keys %spammy_tlds ) {
my $len = length $tld;
my $score = $spammy_tlds{$tld} or next;
$len ++;
if ( $sender->host && ".$tld" eq substr($sender->host,-$len,$len) ) {
$self->log(LOGINFO, "penalizing .$tld envelope sender");
$self->adjust_karma(-$score);
2014-11-11 23:59:40 +01:00
}
}
2013-12-18 06:15:20 +01:00
2013-04-30 04:06:47 +02:00
return DECLINED;
2014-11-11 23:59:40 +01:00
}
2013-04-30 04:06:47 +02:00
2013-04-21 05:53:27 +02:00
sub rcpt_handler {
2013-12-18 06:15:20 +01:00
my ($self,$transaction, $recipient, %args) = @_;
2013-04-21 05:53:27 +02:00
2013-04-30 04:06:47 +02:00
$self->illegal_envelope_format(
$self->connection->notes('envelope_rcpt'),
);
my $count = $self->connection->notes('recipient_count') || 0;
$count++;
if ( $count > 1 ) {
2013-12-18 06:15:20 +01:00
$self->log(LOGINFO, "recipients c: $count ($recipient)");
2013-04-30 04:06:47 +02:00
$self->connection->notes('recipient_count', $count);
2014-11-11 23:59:40 +01:00
}
2013-04-30 04:06:47 +02:00
2013-04-24 22:30:28 +02:00
return DECLINED if $self->is_immune();
my $recipients = scalar $self->transaction->recipients or do {
$self->log(LOGDEBUG, "info, no recipient count");
return DECLINED;
};
2013-12-18 06:15:20 +01:00
$self->log(LOGINFO, "recipients t: $recipients ($recipient)");
2013-04-21 05:53:27 +02:00
2013-04-22 08:29:29 +02:00
my $history = $self->connection->notes('karma_history');
2013-04-24 22:30:28 +02:00
if ( $history > 0 ) {
2013-04-30 04:06:47 +02:00
$self->log(LOGINFO, "info, good history");
2013-04-24 22:30:28 +02:00
return DECLINED;
2014-11-11 23:59:40 +01:00
}
2013-04-22 08:29:29 +02:00
my $karma = $self->connection->notes('karma');
2013-04-24 22:30:28 +02:00
if ( $karma > 0 ) {
2013-04-30 04:06:47 +02:00
$self->log(LOGINFO, "info, good connection");
2013-04-24 22:30:28 +02:00
return DECLINED;
2014-11-11 23:59:40 +01:00
}
2013-04-21 05:53:27 +02:00
2013-04-21 06:50:39 +02:00
# limit # of recipients if host has negative or unknown karma
2014-09-18 03:28:51 +02:00
return DENY, "too many recipients for karma $karma (h: $history)";
2013-04-21 06:50:39 +02:00
}
2013-04-21 05:53:27 +02:00
2013-03-28 22:30:25 +01:00
sub data_handler {
my ($self, $transaction) = @_;
2013-04-24 22:30:28 +02:00
return DECLINED if $self->is_immune();
return DECLINED if $self->is_naughty(); # let naughty do it
2013-04-22 08:29:29 +02:00
2013-04-24 22:30:28 +02:00
# cutting off a naughty sender at DATA prevents having to receive the message
2013-04-22 08:29:29 +02:00
my $karma = $self->connection->notes('karma');
2013-12-18 06:15:20 +01:00
if ( $karma < -4 ) { # bad karma
2013-04-22 08:29:29 +02:00
return $self->get_reject("very bad karma: $karma");
2014-11-11 23:59:40 +01:00
}
2013-04-22 08:29:29 +02:00
2013-03-28 22:30:25 +01:00
return DECLINED;
2013-04-21 06:50:39 +02:00
}
2013-03-28 22:30:25 +01:00
2012-06-02 08:45:25 +02:00
sub disconnect_handler {
my $self = shift;
my $karma = $self->connection->notes('karma') or do {
$self->log(LOGDEBUG, "no karma");
return DECLINED;
};
2014-11-25 00:09:42 +01:00
my $db = $self->get_dbm_location();
my $lock = $self->get_dbm_lock($db) or return DECLINED;
my $tied = $self->get_dbm_tie($db, $lock) or return DECLINED;
my $key = $self->get_karma_key();
2012-06-02 08:45:25 +02:00
2013-04-21 06:50:39 +02:00
my ($penalty_start_ts, $naughty, $nice, $connects) =
2013-04-30 04:06:47 +02:00
$self->parse_db_record($tied->{$key});
2013-04-21 06:50:39 +02:00
my $history = ($nice || 0) - $naughty;
2013-03-23 07:15:24 +01:00
my $log_mess = '';
2012-06-02 08:45:25 +02:00
2013-12-18 06:15:20 +01:00
if ($karma < -2) { # they achieved at least 2 strikes
2013-03-13 08:19:48 +01:00
$history--;
2012-06-02 08:45:25 +02:00
my $negative_limit = 0 - $self->{_args}{negative};
2013-04-21 06:50:39 +02:00
if ($history <= $negative_limit) {
if ($nice == 0 && $history < -5) {
2013-03-23 07:15:24 +01:00
$log_mess = ", penalty box bonus!";
2012-06-22 11:38:01 +02:00
$penalty_start_ts = sprintf "%s", time + abs($history) * 86400;
}
else {
$penalty_start_ts = sprintf "%s", time;
2013-04-21 06:50:39 +02:00
}
2013-03-23 07:15:24 +01:00
$log_mess = "negative, sent to penalty box" . $log_mess;
2012-06-02 08:45:25 +02:00
}
else {
2013-03-23 07:15:24 +01:00
$log_mess = "negative";
2013-04-21 06:50:39 +02:00
}
2012-06-02 08:45:25 +02:00
}
2013-12-18 06:15:20 +01:00
elsif ($karma > 2) {
2012-06-02 08:45:25 +02:00
$nice++;
2013-03-23 07:15:24 +01:00
$log_mess = "positive";
}
else {
$log_mess = "neutral";
2012-06-02 08:45:25 +02:00
}
2013-04-21 06:50:39 +02:00
$self->log(LOGINFO, $log_mess . ", (msg: $karma, his: $history)");
2012-06-02 08:45:25 +02:00
$tied->{$key} = join(':', $penalty_start_ts, $naughty, $nice, ++$connects);
2013-04-21 06:50:39 +02:00
return $self->cleanup_and_return($tied, $lock);
2012-06-02 08:45:25 +02:00
}
2013-04-30 04:06:47 +02:00
sub illegal_envelope_format {
my ($self, $addr) = @_;
# test if envelope address has an illegal (RFC (2)821) space
if ( uc substr($addr,0,6) ne 'FROM:<' && uc substr($addr,0,4) ne 'TO:<' ) {
$self->log(LOGINFO, "illegal envelope address format: $addr" );
2013-12-18 06:15:20 +01:00
$self->adjust_karma(-2);
2014-11-11 23:59:40 +01:00
}
}
2013-04-30 04:06:47 +02:00
sub parse_db_record {
2012-06-02 08:45:25 +02:00
my ($self, $value) = @_;
my $penalty_start_ts = my $naughty = my $nice = my $connects = 0;
2013-04-21 06:50:39 +02:00
if ($value) {
2012-06-02 08:45:25 +02:00
($penalty_start_ts, $naughty, $nice, $connects) = split /:/, $value;
$penalty_start_ts ||= 0;
2013-04-21 06:50:39 +02:00
$nice ||= 0;
$naughty ||= 0;
$connects ||= 0;
}
2014-09-18 03:28:51 +02:00
return $penalty_start_ts, $naughty, $nice, $connects;
2013-04-21 06:50:39 +02:00
}
2012-06-02 08:45:25 +02:00
2012-06-22 11:38:01 +02:00
sub calc_karma {
my ($self, $naughty, $nice) = @_;
2013-04-21 06:50:39 +02:00
return 0 if (!$naughty && !$nice);
2012-06-22 11:38:01 +02:00
2013-04-21 06:50:39 +02:00
my $karma = ($nice || 0) - ($naughty || 0);
$self->connection->notes('karma_history', $karma);
$self->adjust_karma(1) if $karma > 10;
2012-06-22 11:38:01 +02:00
return $karma;
2013-04-21 06:50:39 +02:00
}
2012-06-22 11:38:01 +02:00
2012-06-02 08:45:25 +02:00
sub cleanup_and_return {
2013-04-21 06:50:39 +02:00
my ($self, $tied, $lock, $return_val) = @_;
2012-06-02 08:45:25 +02:00
untie $tied;
close $lock;
2014-09-18 03:28:51 +02:00
return $return_val if defined $return_val; # explicit override
return DECLINED;
2013-04-21 06:50:39 +02:00
}
2012-06-02 08:45:25 +02:00
2014-11-25 00:09:42 +01:00
sub get_karma_key {
2012-06-02 08:45:25 +02:00
my $self = shift;
2013-04-21 06:50:39 +02:00
my $ip = shift || $self->qp->connection->remote_ip;
my $nip = Net::IP->new($ip) or do {
2013-03-13 08:19:48 +01:00
$self->log(LOGERROR, "skip, unable to determine remote IP");
return;
};
2013-04-21 06:50:39 +02:00
return $nip->intip; # convert IP to an int
}
2012-06-02 08:45:25 +02:00
2014-11-25 00:09:42 +01:00
sub get_dbm_tie {
2013-04-21 06:50:39 +02:00
my ($self, $db, $lock) = @_;
2012-06-02 08:45:25 +02:00
2013-04-21 06:50:39 +02:00
tie(my %db, 'AnyDBM_File', $db, O_CREAT | O_RDWR, 0600) or do {
2012-06-30 22:11:54 +02:00
$self->log(LOGCRIT, "error, tie to database $db failed: $!");
2012-06-02 08:45:25 +02:00
close $lock;
return;
};
return \%db;
2013-04-21 06:50:39 +02:00
}
2012-06-02 08:45:25 +02:00
2014-11-25 00:09:42 +01:00
sub get_dbm_location {
2012-06-02 08:45:25 +02:00
my $self = shift;
# Setup database location
my ($QPHOME) = ($0 =~ m!(.*?)/([^/]+)$!);
2013-04-21 06:50:39 +02:00
my @candidate_dirs = (
$self->{args}{db_dir},
"/var/lib/qpsmtpd/karma", "$QPHOME/var/db",
"$QPHOME/config", '.'
);
2012-06-02 08:45:25 +02:00
my $dbdir;
2013-04-21 06:50:39 +02:00
for my $d (@candidate_dirs) {
next if !$d || !-d $d; # impossible
2012-06-02 08:45:25 +02:00
$dbdir = $d;
2013-04-21 06:50:39 +02:00
last; # first match wins
2012-06-02 08:45:25 +02:00
}
my $db = "$dbdir/karma.dbm";
2013-04-21 06:50:39 +02:00
$self->log(LOGDEBUG, "using $db as karma database");
2012-06-02 08:45:25 +02:00
return $db;
2013-04-21 06:50:39 +02:00
}
2012-06-02 08:45:25 +02:00
2014-11-25 00:09:42 +01:00
sub get_dbm_lock {
2012-06-02 08:45:25 +02:00
my ($self, $db) = @_;
2014-11-25 00:09:42 +01:00
return $self->get_dbm_lock_nfs($db) if $self->{_args}{nfslock};
2012-06-02 08:45:25 +02:00
# Check denysoft db
2013-04-21 06:50:39 +02:00
open(my $lock, ">$db.lock") or do {
2012-06-30 22:11:54 +02:00
$self->log(LOGCRIT, "error, opening lockfile failed: $!");
2012-06-02 08:45:25 +02:00
return;
};
2013-04-21 06:50:39 +02:00
flock($lock, LOCK_EX) or do {
2012-06-30 22:11:54 +02:00
$self->log(LOGCRIT, "error, flock of lockfile failed: $!");
2012-06-02 08:45:25 +02:00
close $lock;
return;
};
return $lock;
}
2014-11-25 00:09:42 +01:00
sub get_dbm_lock_nfs {
2012-06-02 08:45:25 +02:00
my ($self, $db) = @_;
require File::NFSLock;
### set up a lock - lasts until object looses scope
my $nfslock = new File::NFSLock {
2013-04-21 06:50:39 +02:00
file => "$db.lock",
lock_type => LOCK_EX | LOCK_NB,
blocking_timeout => 10, # 10 sec
stale_lock_timeout => 30 * 60, # 30 min
}
or do {
2012-06-30 22:11:54 +02:00
$self->log(LOGCRIT, "error, nfs lockfile failed: $!");
2012-06-02 08:45:25 +02:00
return;
2013-04-21 06:50:39 +02:00
};
2012-06-02 08:45:25 +02:00
2013-04-21 06:50:39 +02:00
open(my $lock, "+<$db.lock") or do {
2012-06-30 22:11:54 +02:00
$self->log(LOGCRIT, "error, opening nfs lockfile failed: $!");
2012-06-02 08:45:25 +02:00
return;
};
return $lock;
2013-04-21 06:50:39 +02:00
}
2012-06-02 08:45:25 +02:00
sub prune_db {
my $self = shift;
2014-11-25 00:09:42 +01:00
my $db = $self->get_dbm_location();
my $lock = $self->get_dbm_lock($db) or return DECLINED;
my $tied = $self->get_dbm_tie($db, $lock) or return DECLINED;
2012-06-02 08:45:25 +02:00
my $count = keys %$tied;
my $pruned = 0;
2013-04-21 06:50:39 +02:00
foreach my $key (keys %$tied) {
my $ts = $tied->{$key};
my $days_old = (time - $ts) / 86400;
2012-06-02 08:45:25 +02:00
next if $days_old < $self->{_args}{penalty_days} * 2;
delete $tied->{$key};
$pruned++;
2013-04-21 06:50:39 +02:00
}
2012-06-02 08:45:25 +02:00
untie $tied;
close $lock;
2013-04-21 06:50:39 +02:00
$self->log(LOGINFO, "pruned $pruned of $count DB entries");
return $self->cleanup_and_return($tied, $lock, DECLINED);
}
2012-06-02 08:45:25 +02:00