| 1 |
#!perl -w |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
karma - reward nice and penalize naughty mail senders |
| 6 |
|
| 7 |
=head1 SYNOPSIS |
| 8 |
|
| 9 |
Karma tracks sender history, allowing us to provide differing levels |
| 10 |
of service to naughty, nice, and unknown senders. |
| 11 |
|
| 12 |
=head1 DESCRIPTION |
| 13 |
|
| 14 |
Karma records the number of nice, naughty, and total connections from mail |
| 15 |
senders. After sending a naughty message, if a sender has more naughty than |
| 16 |
nice connections, they are penalized for I<penalty_days>. Connections |
| 17 |
from senders in the penalty box are rejected per the settings in I<reject>. |
| 18 |
|
| 19 |
Karma provides other plugins with a karma value they can use to be more |
| 20 |
lenient, strict, or skip processing entirely. |
| 21 |
|
| 22 |
Karma is small, fast, and ruthlessly efficient. Karma can be used to craft |
| 23 |
custom connection policies such as these two examples: |
| 24 |
|
| 25 |
=over 4 |
| 26 |
|
| 27 |
Hi there, well known and well behaved sender. Please help yourself to greater concurrency (hosts_allow), multiple recipients (karma), and no delays (early_sender). |
| 28 |
|
| 29 |
Hi there, naughty sender. You get a max concurrency of 1, max recipients of 2, and SMTP delays. |
| 30 |
|
| 31 |
=back |
| 32 |
|
| 33 |
=head1 CONFIG |
| 34 |
|
| 35 |
=head2 negative <integer> |
| 36 |
|
| 37 |
How negative a senders karma can get before we penalize them for sending a |
| 38 |
naughty message. Karma is the number of nice - naughty connections. |
| 39 |
|
| 40 |
Default: 1 |
| 41 |
|
| 42 |
Examples: |
| 43 |
|
| 44 |
negative 1: 0 nice - 1 naughty = karma -1, penalize |
| 45 |
negative 1: 1 nice - 1 naughty = karma 0, okay |
| 46 |
negative 2: 1 nice - 2 naughty = karma -1, okay |
| 47 |
negative 2: 1 nice - 3 naughty = karma -2, penalize |
| 48 |
|
| 49 |
With the default negative limit of one, there's a very small chance you could |
| 50 |
penalize a "mostly good" sender. Raising it to 2 reduces that possibility to |
| 51 |
improbable. |
| 52 |
|
| 53 |
=head2 penalty_days <days> |
| 54 |
|
| 55 |
The number of days a naughty sender is refused connections. Use a decimal |
| 56 |
value to penalize for portions of days. |
| 57 |
|
| 58 |
karma penalty_days 1 |
| 59 |
|
| 60 |
Default: 1 |
| 61 |
|
| 62 |
=head2 reject |
| 63 |
|
| 64 |
karma reject [ 0 | 1 | connect | naughty ] |
| 65 |
|
| 66 |
I<0> will not reject any connections. |
| 67 |
|
| 68 |
I<1> will reject naughty senders. |
| 69 |
|
| 70 |
I<connect> is the most efficient setting. |
| 71 |
|
| 72 |
To reject at any other connection hook, use the I<naughty> setting and the |
| 73 |
B<naughty> plugin. |
| 74 |
|
| 75 |
=head2 db_dir <path> |
| 76 |
|
| 77 |
Path to a directory in which the DB will be stored. This directory must be |
| 78 |
writable by the qpsmtpd user. If unset, the first usable directory from the |
| 79 |
following list will be used: |
| 80 |
|
| 81 |
=over 4 |
| 82 |
|
| 83 |
=item /var/lib/qpsmtpd/karma |
| 84 |
|
| 85 |
=item I<BINDIR>/var/db (where BINDIR is the location of the qpsmtpd binary) |
| 86 |
|
| 87 |
=item I<BINDIR>/config |
| 88 |
|
| 89 |
=back |
| 90 |
|
| 91 |
=head2 loglevel |
| 92 |
|
| 93 |
Adjust the quantity of logging for this plugin. See docs/logging.pod |
| 94 |
|
| 95 |
=head1 BENEFITS |
| 96 |
|
| 97 |
Karma reduces the resources wasted by naughty mailers. When used with |
| 98 |
I<reject connect>, naughty senders are disconnected in about 0.1 seconds. |
| 99 |
|
| 100 |
The biggest gains to be had are by having heavy plugins (spamassassin, dspam, |
| 101 |
virus filters) set the B<karma> connection note (see KARMA) when they encounter |
| 102 |
naughty senders. Reasons to send servers to the penalty box could include |
| 103 |
sending a virus, early talking, or sending messages with a very high spam |
| 104 |
score. |
| 105 |
|
| 106 |
This plugin does not penalize connections with transaction notes I<relayclient> |
| 107 |
or I<whitelisthost> set. These notes would have been set by the B<relay>, |
| 108 |
B<whitelist>, and B<dns_whitelist_soft> plugins. Obviously, those plugins must |
| 109 |
run before B<karma> for that to work. |
| 110 |
|
| 111 |
=head1 KARMA |
| 112 |
|
| 113 |
No attempt is made by this plugin to determine karma. It is up to other |
| 114 |
plugins to reward well behaved senders with positive karma and smite poorly |
| 115 |
behaved senders with negative karma. See B<USING KARMA IN OTHER PLUGINS> |
| 116 |
|
| 117 |
After the connection ends, B<karma> will record the result. Mail servers whose |
| 118 |
naughty connections exceed nice ones are sent to the penalty box. Servers in |
| 119 |
the penalty box will be tersely disconnected for I<penalty_days>. Here is |
| 120 |
an example connection from an IP in the penalty box: |
| 121 |
|
| 122 |
73122 Connection from smtp.midsetmediacorp.com [64.185.226.65] |
| 123 |
73122 (connect) ident::geoip: US, United States |
| 124 |
73122 (connect) ident::p0f: Windows 7 or 8 |
| 125 |
73122 (connect) earlytalker: pass: 64.185.226.65 said nothing spontaneous |
| 126 |
73122 (connect) relay: skip: no match |
| 127 |
73122 (connect) karma: fail |
| 128 |
73122 550 You were naughty. You are cannot connect for 0.99 more days. |
| 129 |
73122 click, disconnecting |
| 130 |
73122 (post-connection) connection_time: 1.048 s. |
| 131 |
|
| 132 |
If we only set negative karma, we will almost certainly penalize servers we |
| 133 |
want to receive mail from. For example, a Yahoo user sends an egregious spam |
| 134 |
to a user on our server. Now nobody on our server can receive email from that |
| 135 |
Yahoo server for I<penalty_days>. This should happen approximately 0% of |
| 136 |
the time if we are careful to also set positive karma. |
| 137 |
|
| 138 |
=head1 KARMA HISTORY |
| 139 |
|
| 140 |
Karma maintains a history for each IP. When a senders history has decreased |
| 141 |
below -5 and they have never sent a good message, they get a karma bonus. |
| 142 |
The bonus tacks on an extra day of blocking for every naughty message they |
| 143 |
send. |
| 144 |
|
| 145 |
Example: an unknown sender delivers a spam. They get a one day penalty_box. |
| 146 |
After 5 days, 5 spams, 5 penalties, and 0 nice messages, they get a six day |
| 147 |
penalty. The next offense gets a 7 day penalty, and so on. |
| 148 |
|
| 149 |
=head1 USING KARMA |
| 150 |
|
| 151 |
To get rid of naughty connections as fast as possible, run karma before other |
| 152 |
connection plugins. Plugins that trigger DNS lookups or impose time delays |
| 153 |
should run after B<karma>. In this example, karma runs before all but the |
| 154 |
ident plugins. |
| 155 |
|
| 156 |
89011 Connection from Unknown [69.61.27.204] |
| 157 |
89011 (connect) ident::geoip: US, United States |
| 158 |
89011 (connect) ident::p0f: Linux 3.x |
| 159 |
89011 (connect) karma: fail, 1 naughty, 0 nice, 1 connects |
| 160 |
89011 550 You were naughty. You are penalized for 0.99 more days. |
| 161 |
89011 click, disconnecting |
| 162 |
89011 (post-connection) connection_time: 0.118 s. |
| 163 |
88798 cleaning up after 89011 |
| 164 |
|
| 165 |
Unlike RBLs, B<karma> only penalizes IPs that have sent us spam, and only when |
| 166 |
those senders have sent us more spam than ham. |
| 167 |
|
| 168 |
=head1 USING KARMA IN OTHER PLUGINS |
| 169 |
|
| 170 |
This plugin sets the connection note I<karma_history>. Your plugin can |
| 171 |
use the senders karma to be more gracious or rude to senders. The value of |
| 172 |
I<karma_history> is the number of nice connections minus naughty |
| 173 |
ones. The higher the number, the better you should treat the sender. |
| 174 |
|
| 175 |
To alter a connections karma based on its behavior, do this: |
| 176 |
|
| 177 |
$self->adjust_karma( -1 ); # lower karma (naughty) |
| 178 |
$self->adjust_karma( 1 ); # raise karma (good) |
| 179 |
|
| 180 |
|
| 181 |
=head1 EFFECTIVENESS |
| 182 |
|
| 183 |
In the first 24 hours, B<karma> rejected 8% of all connections. After one |
| 184 |
week of running with I<penalty_days 1>, karma has rejected 15% of all |
| 185 |
connections. |
| 186 |
|
| 187 |
This plugins effectiveness results from the propensity of naughty senders |
| 188 |
to be repeat offenders. Limiting them to a single offense per day(s) greatly |
| 189 |
reduces the resources they can waste. |
| 190 |
|
| 191 |
Of the connections that had previously passed all other checks and were caught |
| 192 |
only by spamassassin and/or dspam, B<karma> rejected 31 percent. Since |
| 193 |
spamassassin and dspam consume more resources than others plugins, this plugin |
| 194 |
seems to be a very big win. |
| 195 |
|
| 196 |
=head1 DATABASE |
| 197 |
|
| 198 |
Connection summaries are stored in a database. The database key is the integer |
| 199 |
value of the remote IP. The DB value is a : delimited list containing a penalty |
| 200 |
box start time (if the server is/was on timeout) and the count of naughty, |
| 201 |
nice, and total connections. The database can be listed and searched with the |
| 202 |
karma_tool script. |
| 203 |
|
| 204 |
=head1 BUGS & LIMITATIONS |
| 205 |
|
| 206 |
This plugin is reactionary. Like the FBI, it doesn't do anything until |
| 207 |
after a crime has been committed. |
| 208 |
|
| 209 |
There is little to be gained by listing servers that are already on DNS |
| 210 |
blacklists, send to invalid users, earlytalkers, etc. Those already have |
| 211 |
very lightweight tests. |
| 212 |
|
| 213 |
=head1 TODO |
| 214 |
|
| 215 |
* Avoid storing results for DNSBL listed IPs |
| 216 |
* some type of ASN integration, for tracking karma of 'neighborhoods' |
| 217 |
|
| 218 |
=head1 AUTHOR |
| 219 |
|
| 220 |
2013 - MS - Addeded penalty for spammy TLDs |
| 221 |
2012 - Matt Simerson - [email protected] |
| 222 |
|
| 223 |
=head1 ACKNOWLEDGEMENTS |
| 224 |
|
| 225 |
Gavin Carr's DB implementation in the greylisting plugin. |
| 226 |
|
| 227 |
=cut |
| 228 |
|
| 229 |
use strict; |
| 230 |
use warnings; |
| 231 |
|
| 232 |
use Qpsmtpd::Constants; |
| 233 |
|
| 234 |
BEGIN { @AnyDBM_File::ISA = qw(DB_File GDBM_File NDBM_File) } |
| 235 |
use AnyDBM_File; |
| 236 |
use Fcntl qw(:DEFAULT :flock LOCK_EX LOCK_NB); |
| 237 |
use Net::IP; |
| 238 |
|
| 239 |
sub register { |
| 240 |
my ($self, $qp) = (shift, shift); |
| 241 |
$self->log(LOGERROR, "Bad arguments") if @_ % 2; |
| 242 |
$self->{_args} = {@_}; |
| 243 |
$self->{_args}{negative} ||= 1; |
| 244 |
$self->{_args}{penalty_days} ||= 1; |
| 245 |
$self->{_args}{reject_type} ||= 'disconnect'; |
| 246 |
|
| 247 |
if (!defined $self->{_args}{reject}) { |
| 248 |
$self->{_args}{reject} = 'naughty'; |
| 249 |
} |
| 250 |
|
| 251 |
#$self->prune_db(); # keep the DB compact |
| 252 |
$self->register_hook('connect', 'connect_handler'); |
| 253 |
$self->register_hook('mail', 'from_handler'); |
| 254 |
$self->register_hook('rcpt', 'rcpt_handler'); |
| 255 |
$self->register_hook('data', 'data_handler'); |
| 256 |
$self->register_hook('data_post', 'data_handler'); |
| 257 |
$self->register_hook('disconnect', 'disconnect_handler'); |
| 258 |
} |
| 259 |
|
| 260 |
sub hook_pre_connection { |
| 261 |
my ($self, $transaction, %args) = @_; |
| 262 |
|
| 263 |
$self->connection->notes('karma_history', 0); |
| 264 |
|
| 265 |
my $remote_ip = $args{remote_ip}; |
| 266 |
|
| 267 |
my $db = $self->get_db_location(); |
| 268 |
my $lock = $self->get_db_lock($db) or return DECLINED; |
| 269 |
my $tied = $self->get_db_tie($db, $lock) or return DECLINED; |
| 270 |
my $key = $self->get_db_key($remote_ip) or do { |
| 271 |
$self->log(LOGINFO, "skip, unable to get DB key"); |
| 272 |
return DECLINED; |
| 273 |
}; |
| 274 |
|
| 275 |
if (!$tied->{$key}) { |
| 276 |
$self->log(LOGDEBUG, "pass, no record"); |
| 277 |
return $self->cleanup_and_return($tied, $lock); |
| 278 |
} |
| 279 |
|
| 280 |
my ($penalty_start_ts, $naughty, $nice, $connects) = |
| 281 |
$self->parse_db_record($tied->{$key}); |
| 282 |
$self->calc_karma($naughty, $nice); |
| 283 |
return $self->cleanup_and_return($tied, $lock); |
| 284 |
} |
| 285 |
|
| 286 |
sub connect_handler { |
| 287 |
my $self = shift; |
| 288 |
|
| 289 |
$self->connection->notes('karma', 0); # default |
| 290 |
|
| 291 |
return DECLINED if $self->is_immune(); |
| 292 |
|
| 293 |
my $db = $self->get_db_location(); |
| 294 |
my $lock = $self->get_db_lock($db) or return DECLINED; |
| 295 |
my $tied = $self->get_db_tie($db, $lock) or return DECLINED; |
| 296 |
my $key = $self->get_db_key() or do { |
| 297 |
$self->log(LOGINFO, "skip, unable to get DB key"); |
| 298 |
return DECLINED; |
| 299 |
}; |
| 300 |
|
| 301 |
if (!$tied->{$key}) { |
| 302 |
$self->log(LOGINFO, "pass, no record"); |
| 303 |
return $self->cleanup_and_return($tied, $lock); |
| 304 |
} |
| 305 |
|
| 306 |
my ($penalty_start_ts, $naughty, $nice, $connects) = |
| 307 |
$self->parse_db_record($tied->{$key}); |
| 308 |
my $summary = "$naughty naughty, $nice nice, $connects connects"; |
| 309 |
my $karma = $self->calc_karma($naughty, $nice); |
| 310 |
|
| 311 |
if (!$penalty_start_ts) { |
| 312 |
$self->log(LOGINFO, "pass, no penalty ($summary)"); |
| 313 |
return $self->cleanup_and_return($tied, $lock); |
| 314 |
} |
| 315 |
|
| 316 |
my $days_old = (time - $penalty_start_ts) / 86400; |
| 317 |
if ($days_old >= $self->{_args}{penalty_days}) { |
| 318 |
$self->log(LOGINFO, "pass, penalty expired ($summary)"); |
| 319 |
return $self->cleanup_and_return($tied, $lock); |
| 320 |
} |
| 321 |
|
| 322 |
$tied->{$key} = join(':', $penalty_start_ts, $naughty, $nice, ++$connects); |
| 323 |
$self->cleanup_and_return($tied, $lock); |
| 324 |
|
| 325 |
my $left = sprintf "%.2f", $self->{_args}{penalty_days} - $days_old; |
| 326 |
my $mess = "You were naughty. You cannot connect for $left more days."; |
| 327 |
|
| 328 |
return $self->get_reject($mess, $karma); |
| 329 |
} |
| 330 |
|
| 331 |
sub from_handler { |
| 332 |
my ($self,$transaction, $sender, %args) = @_; |
| 333 |
|
| 334 |
# test if sender has placed an illegal (RFC (2)821) space in envelope from |
| 335 |
my $full_from = $self->connection->notes('envelope_from'); |
| 336 |
$self->illegal_envelope_format( $full_from ); |
| 337 |
|
| 338 |
my %spammy_tlds = ( |
| 339 |
map { $_ => 4 } qw/ info pw /, |
| 340 |
map { $_ => 3 } qw/ tw biz /, |
| 341 |
map { $_ => 2 } qw/ cl br fr be jp no se sg /, |
| 342 |
); |
| 343 |
foreach my $tld ( keys %spammy_tlds ) { |
| 344 |
my $len = length $tld; |
| 345 |
my $score = $spammy_tlds{$tld} or next; |
| 346 |
$len ++; |
| 347 |
if ( $sender->host && ".$tld" eq substr($sender->host,-$len,$len) ) { |
| 348 |
$self->log(LOGINFO, "penalizing .$tld envelope sender"); |
| 349 |
$self->adjust_karma(-$score); |
| 350 |
}; |
| 351 |
}; |
| 352 |
|
| 353 |
return DECLINED; |
| 354 |
}; |
| 355 |
|
| 356 |
sub rcpt_handler { |
| 357 |
my ($self,$transaction, $recipient, %args) = @_; |
| 358 |
|
| 359 |
$self->illegal_envelope_format( |
| 360 |
$self->connection->notes('envelope_rcpt'), |
| 361 |
); |
| 362 |
|
| 363 |
my $count = $self->connection->notes('recipient_count') || 0; |
| 364 |
$count++; |
| 365 |
if ( $count > 1 ) { |
| 366 |
$self->log(LOGINFO, "recipients c: $count ($recipient)"); |
| 367 |
$self->connection->notes('recipient_count', $count); |
| 368 |
}; |
| 369 |
|
| 370 |
return DECLINED if $self->is_immune(); |
| 371 |
|
| 372 |
my $recipients = scalar $self->transaction->recipients or do { |
| 373 |
$self->log(LOGDEBUG, "info, no recipient count"); |
| 374 |
return DECLINED; |
| 375 |
}; |
| 376 |
$self->log(LOGINFO, "recipients t: $recipients ($recipient)"); |
| 377 |
|
| 378 |
my $history = $self->connection->notes('karma_history'); |
| 379 |
if ( $history > 0 ) { |
| 380 |
$self->log(LOGINFO, "info, good history"); |
| 381 |
return DECLINED; |
| 382 |
}; |
| 383 |
|
| 384 |
my $karma = $self->connection->notes('karma'); |
| 385 |
if ( $karma > 0 ) { |
| 386 |
$self->log(LOGINFO, "info, good connection"); |
| 387 |
return DECLINED; |
| 388 |
}; |
| 389 |
|
| 390 |
# limit # of recipients if host has negative or unknown karma |
| 391 |
return (DENY, "too many recipients for karma $karma (h: $history)"); |
| 392 |
} |
| 393 |
|
| 394 |
sub data_handler { |
| 395 |
my ($self, $transaction) = @_; |
| 396 |
|
| 397 |
return DECLINED if $self->is_immune(); |
| 398 |
return DECLINED if $self->is_naughty(); # let naughty do it |
| 399 |
|
| 400 |
# cutting off a naughty sender at DATA prevents having to receive the message |
| 401 |
my $karma = $self->connection->notes('karma'); |
| 402 |
if ( $karma < -4 ) { # bad karma |
| 403 |
return $self->get_reject("very bad karma: $karma"); |
| 404 |
}; |
| 405 |
|
| 406 |
return DECLINED; |
| 407 |
} |
| 408 |
|
| 409 |
sub disconnect_handler { |
| 410 |
my $self = shift; |
| 411 |
|
| 412 |
my $karma = $self->connection->notes('karma') or do { |
| 413 |
$self->log(LOGDEBUG, "no karma"); |
| 414 |
return DECLINED; |
| 415 |
}; |
| 416 |
|
| 417 |
my $db = $self->get_db_location(); |
| 418 |
my $lock = $self->get_db_lock($db) or return DECLINED; |
| 419 |
my $tied = $self->get_db_tie($db, $lock) or return DECLINED; |
| 420 |
my $key = $self->get_db_key(); |
| 421 |
|
| 422 |
my ($penalty_start_ts, $naughty, $nice, $connects) = |
| 423 |
$self->parse_db_record($tied->{$key}); |
| 424 |
my $history = ($nice || 0) - $naughty; |
| 425 |
my $log_mess = ''; |
| 426 |
|
| 427 |
if ($karma < -2) { # they achieved at least 2 strikes |
| 428 |
$history--; |
| 429 |
my $negative_limit = 0 - $self->{_args}{negative}; |
| 430 |
if ($history <= $negative_limit) { |
| 431 |
if ($nice == 0 && $history < -5) { |
| 432 |
$log_mess = ", penalty box bonus!"; |
| 433 |
$penalty_start_ts = sprintf "%s", time + abs($history) * 86400; |
| 434 |
} |
| 435 |
else { |
| 436 |
$penalty_start_ts = sprintf "%s", time; |
| 437 |
} |
| 438 |
$log_mess = "negative, sent to penalty box" . $log_mess; |
| 439 |
} |
| 440 |
else { |
| 441 |
$log_mess = "negative"; |
| 442 |
} |
| 443 |
} |
| 444 |
elsif ($karma > 2) { |
| 445 |
$nice++; |
| 446 |
$log_mess = "positive"; |
| 447 |
} |
| 448 |
else { |
| 449 |
$log_mess = "neutral"; |
| 450 |
} |
| 451 |
$self->log(LOGINFO, $log_mess . ", (msg: $karma, his: $history)"); |
| 452 |
|
| 453 |
$tied->{$key} = join(':', $penalty_start_ts, $naughty, $nice, ++$connects); |
| 454 |
return $self->cleanup_and_return($tied, $lock); |
| 455 |
} |
| 456 |
|
| 457 |
sub illegal_envelope_format { |
| 458 |
my ($self, $addr) = @_; |
| 459 |
|
| 460 |
# test if envelope address has an illegal (RFC (2)821) space |
| 461 |
if ( uc substr($addr,0,6) ne 'FROM:<' && uc substr($addr,0,4) ne 'TO:<' ) { |
| 462 |
$self->log(LOGINFO, "illegal envelope address format: $addr" ); |
| 463 |
$self->adjust_karma(-2); |
| 464 |
}; |
| 465 |
}; |
| 466 |
|
| 467 |
sub parse_db_record { |
| 468 |
my ($self, $value) = @_; |
| 469 |
|
| 470 |
my $penalty_start_ts = my $naughty = my $nice = my $connects = 0; |
| 471 |
if ($value) { |
| 472 |
($penalty_start_ts, $naughty, $nice, $connects) = split /:/, $value; |
| 473 |
$penalty_start_ts ||= 0; |
| 474 |
$nice ||= 0; |
| 475 |
$naughty ||= 0; |
| 476 |
$connects ||= 0; |
| 477 |
} |
| 478 |
return ($penalty_start_ts, $naughty, $nice, $connects); |
| 479 |
} |
| 480 |
|
| 481 |
sub calc_karma { |
| 482 |
my ($self, $naughty, $nice) = @_; |
| 483 |
return 0 if (!$naughty && !$nice); |
| 484 |
|
| 485 |
my $karma = ($nice || 0) - ($naughty || 0); |
| 486 |
$self->connection->notes('karma_history', $karma); |
| 487 |
$self->adjust_karma(1) if $karma > 10; |
| 488 |
return $karma; |
| 489 |
} |
| 490 |
|
| 491 |
sub cleanup_and_return { |
| 492 |
my ($self, $tied, $lock, $return_val) = @_; |
| 493 |
|
| 494 |
untie $tied; |
| 495 |
close $lock; |
| 496 |
return ($return_val) if defined $return_val; # explicit override |
| 497 |
return (DECLINED); |
| 498 |
} |
| 499 |
|
| 500 |
sub get_db_key { |
| 501 |
my $self = shift; |
| 502 |
my $ip = shift || $self->qp->connection->remote_ip; |
| 503 |
my $nip = Net::IP->new($ip) or do { |
| 504 |
$self->log(LOGERROR, "skip, unable to determine remote IP"); |
| 505 |
return; |
| 506 |
}; |
| 507 |
return $nip->intip; # convert IP to an int |
| 508 |
} |
| 509 |
|
| 510 |
sub get_db_tie { |
| 511 |
my ($self, $db, $lock) = @_; |
| 512 |
|
| 513 |
tie(my %db, 'AnyDBM_File', $db, O_CREAT | O_RDWR, 0600) or do { |
| 514 |
$self->log(LOGCRIT, "error, tie to database $db failed: $!"); |
| 515 |
close $lock; |
| 516 |
return; |
| 517 |
}; |
| 518 |
return \%db; |
| 519 |
} |
| 520 |
|
| 521 |
sub get_db_location { |
| 522 |
my $self = shift; |
| 523 |
|
| 524 |
# Setup database location |
| 525 |
my ($QPHOME) = ($0 =~ m!(.*?)/([^/]+)$!); |
| 526 |
my @candidate_dirs = ( |
| 527 |
$self->{args}{db_dir}, |
| 528 |
"/var/lib/qpsmtpd/karma", "$QPHOME/var/db", |
| 529 |
"$QPHOME/config", '.' |
| 530 |
); |
| 531 |
|
| 532 |
my $dbdir; |
| 533 |
for my $d (@candidate_dirs) { |
| 534 |
next if !$d || !-d $d; # impossible |
| 535 |
$dbdir = $d; |
| 536 |
last; # first match wins |
| 537 |
} |
| 538 |
my $db = "$dbdir/karma.dbm"; |
| 539 |
$self->log(LOGDEBUG, "using $db as karma database"); |
| 540 |
return $db; |
| 541 |
} |
| 542 |
|
| 543 |
sub get_db_lock { |
| 544 |
my ($self, $db) = @_; |
| 545 |
|
| 546 |
return $self->get_db_lock_nfs($db) if $self->{_args}{nfslock}; |
| 547 |
|
| 548 |
# Check denysoft db |
| 549 |
open(my $lock, ">$db.lock") or do { |
| 550 |
$self->log(LOGCRIT, "error, opening lockfile failed: $!"); |
| 551 |
return; |
| 552 |
}; |
| 553 |
|
| 554 |
flock($lock, LOCK_EX) or do { |
| 555 |
$self->log(LOGCRIT, "error, flock of lockfile failed: $!"); |
| 556 |
close $lock; |
| 557 |
return; |
| 558 |
}; |
| 559 |
|
| 560 |
return $lock; |
| 561 |
} |
| 562 |
|
| 563 |
sub get_db_lock_nfs { |
| 564 |
my ($self, $db) = @_; |
| 565 |
|
| 566 |
require File::NFSLock; |
| 567 |
|
| 568 |
### set up a lock - lasts until object looses scope |
| 569 |
my $nfslock = new File::NFSLock { |
| 570 |
file => "$db.lock", |
| 571 |
lock_type => LOCK_EX | LOCK_NB, |
| 572 |
blocking_timeout => 10, # 10 sec |
| 573 |
stale_lock_timeout => 30 * 60, # 30 min |
| 574 |
} |
| 575 |
or do { |
| 576 |
$self->log(LOGCRIT, "error, nfs lockfile failed: $!"); |
| 577 |
return; |
| 578 |
}; |
| 579 |
|
| 580 |
open(my $lock, "+<$db.lock") or do { |
| 581 |
$self->log(LOGCRIT, "error, opening nfs lockfile failed: $!"); |
| 582 |
return; |
| 583 |
}; |
| 584 |
|
| 585 |
return $lock; |
| 586 |
} |
| 587 |
|
| 588 |
sub prune_db { |
| 589 |
my $self = shift; |
| 590 |
|
| 591 |
my $db = $self->get_db_location(); |
| 592 |
my $lock = $self->get_db_lock($db) or return DECLINED; |
| 593 |
my $tied = $self->get_db_tie($db, $lock) or return DECLINED; |
| 594 |
my $count = keys %$tied; |
| 595 |
|
| 596 |
my $pruned = 0; |
| 597 |
foreach my $key (keys %$tied) { |
| 598 |
my $ts = $tied->{$key}; |
| 599 |
my $days_old = (time - $ts) / 86400; |
| 600 |
next if $days_old < $self->{_args}{penalty_days} * 2; |
| 601 |
delete $tied->{$key}; |
| 602 |
$pruned++; |
| 603 |
} |
| 604 |
untie $tied; |
| 605 |
close $lock; |
| 606 |
$self->log(LOGINFO, "pruned $pruned of $count DB entries"); |
| 607 |
return $self->cleanup_and_return($tied, $lock, DECLINED); |
| 608 |
} |
| 609 |
|