| 1 |
#!perl -w |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
greylisting - delay mail from unknown senders |
| 6 |
|
| 7 |
=head1 DESCRIPTION |
| 8 |
|
| 9 |
Plugin implementing the 'greylisting' algorithm proposed by Evan |
| 10 |
Harris in http://projects.puremagic.com/greylisting/. Greylisting is |
| 11 |
a form of denysoft filter, where unrecognised new connections are |
| 12 |
temporarily denied for some initial period, to foil spammers using |
| 13 |
fire-and-forget spamware, http_proxies, etc. |
| 14 |
|
| 15 |
Greylisting tracks incoming connections using a triplet (see TRIPLET). It |
| 16 |
has configurable timeout periods (black/grey/white) to control whether |
| 17 |
connections are allowed, instead of using connection counts or rates. |
| 18 |
|
| 19 |
Automatic whitelisting is enabled for relayclients, whitelisted hosts, |
| 20 |
whitelisted senders, TLS connections, p0f matches, and geoip matches. |
| 21 |
|
| 22 |
=head1 TRIPLETS |
| 23 |
|
| 24 |
In greylisting, I<remote_ip>, I<sender>, and I<recipient> are referred to |
| 25 |
as the triplet that connections are deferred based on. This plugin allows |
| 26 |
tracking on any or all of the three, using only the IP address by default. |
| 27 |
A simple dbm database is used for tracking connections. |
| 28 |
|
| 29 |
How that works is best explained by example: |
| 30 |
|
| 31 |
A new connection arrives from the host shvj1.jpmchase.com. The sender is |
| 32 |
[email protected] and the recipient is [email protected]. This is |
| 33 |
the first connection for that triplet so the connection is deferred for |
| 34 |
I<black_timeout> minutes. After the timeout, but before the I<grey_timeout> |
| 35 |
elapses, shvj1.jpmchase.com retries and successfully delivers the mail. For |
| 36 |
the next I<white_timeout> days, emails for that triplet are not delayed. |
| 37 |
|
| 38 |
The next day, shvj1.jpmchase.com tries to deliver a new email from |
| 39 |
[email protected] to [email protected]. Since this triplet is new, it |
| 40 |
will be delayed as our initial connection in the last scenario was. This |
| 41 |
delay could end up costing over US $4B. |
| 42 |
|
| 43 |
By default, this plugin does not enable the sender or recipient in the triplet. |
| 44 |
Once an email from a remote server has been delivered to anyone on our server, |
| 45 |
that remote server is whitelisted for any sender and any recipient. This is a |
| 46 |
policy that delays less mail and is less likely to impoverish your bank. |
| 47 |
|
| 48 |
=head1 CONFIG |
| 49 |
|
| 50 |
The following parameters can be passed to greylisting: |
| 51 |
|
| 52 |
=head2 remote_ip <bool> |
| 53 |
|
| 54 |
Include the remote ip in the connection triplet? Default: 1 |
| 55 |
|
| 56 |
=head2 sender <bool> |
| 57 |
|
| 58 |
Include the sender in the connection triplet? Default: 0. |
| 59 |
|
| 60 |
=head2 recipient <bool> |
| 61 |
|
| 62 |
Include the recipient in the connection triplet? Default: 0. |
| 63 |
|
| 64 |
=head2 deny_late <bool> |
| 65 |
|
| 66 |
Whether to defer denials during the 'mail' hook or later during 'data_post' |
| 67 |
e.g. to allow per-recipient logging. Default: 0. |
| 68 |
|
| 69 |
=head2 black_timeout <timeout_seconds> |
| 70 |
|
| 71 |
The initial period during which we issue DENYSOFTs for connections from an |
| 72 |
unknown (or timed out) 'connection triplet'. Default: 50 minutes. |
| 73 |
|
| 74 |
=head2 grey_timeout <timeout_seconds> |
| 75 |
|
| 76 |
The subsequent 'grey' period, after the initial black blocking period, |
| 77 |
when we will accept a delivery from a formerly-unknown connection |
| 78 |
triplet. If a new connection is received during this time, we will |
| 79 |
record a successful delivery against this IP address, which whitelists |
| 80 |
it for future deliveries (see following). Default: 3 hours 20 minutes. |
| 81 |
|
| 82 |
=head2 white_timeout <timeout_seconds> |
| 83 |
|
| 84 |
The period after which a known connection triplet will be considered |
| 85 |
stale, and we will issue DENYSOFTs again. New deliveries reset the |
| 86 |
timestamp on the address and renew this timeout. Default: 36 days. |
| 87 |
|
| 88 |
=head2 reject <bool> |
| 89 |
|
| 90 |
Whether to issue deferrals (DENYSOFT) for black connections. Having reject |
| 91 |
disabled is useful for seeding the database and testing without impacting |
| 92 |
deliveries. It is recommended to begin with I<reject 0> for a week or two |
| 93 |
before enabling I<reject>. |
| 94 |
|
| 95 |
Default: 1 |
| 96 |
|
| 97 |
=head2 db_dir <path> |
| 98 |
|
| 99 |
Path to a directory in which the greylisting DB will be stored. This |
| 100 |
directory must be writable by the qpsmtpd user. By default, the first |
| 101 |
usable directory from the following list will be used: |
| 102 |
|
| 103 |
=over 4 |
| 104 |
|
| 105 |
=item /var/lib/qpsmtpd/greylisting |
| 106 |
|
| 107 |
=item I<BINDIR>/var/db (where BINDIR is the location of the qpsmtpd binary) |
| 108 |
|
| 109 |
=item I<BINDIR>/config |
| 110 |
|
| 111 |
=back |
| 112 |
|
| 113 |
=head2 per_recipient <bool> |
| 114 |
|
| 115 |
Flag to indicate whether to use per-recipient configs. |
| 116 |
|
| 117 |
=head2 per_recipient_db <bool> |
| 118 |
|
| 119 |
Flag to indicate whether to use per-recipient greylisting |
| 120 |
databases (default is to use a shared database). Per-recipient configuration |
| 121 |
directories, if determined, supercede I<db_dir>. |
| 122 |
|
| 123 |
=head2 nfslock <bool> |
| 124 |
|
| 125 |
Flag to indicate the database is stored on NFS. Uses File::NFSLock |
| 126 |
instead of flock. |
| 127 |
|
| 128 |
=head2 p0f |
| 129 |
|
| 130 |
Enable greylisting only when certain p0f criteria is met. The required |
| 131 |
argument is a comma delimited list of key/value pairs. The keys are the |
| 132 |
following p0f TCP fingerprint elements: genre, detail, uptime, link, and |
| 133 |
distance. |
| 134 |
|
| 135 |
To greylist emails from computers whose remote OS is windows: |
| 136 |
|
| 137 |
greylisting p0f genre,windows |
| 138 |
|
| 139 |
To greylist only windows computers on DSL links more than 3 network hops away: |
| 140 |
|
| 141 |
greylisting p0f genre,windows,link,dsl,distance,3 |
| 142 |
|
| 143 |
=head2 geoip |
| 144 |
|
| 145 |
Do not greylist connections that are in the comma separated list of countries. |
| 146 |
|
| 147 |
greylisting geoip US,UK |
| 148 |
|
| 149 |
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. |
| 150 |
|
| 151 |
=head2 loglevel |
| 152 |
|
| 153 |
Adjust the quantity of logging for this plugin. See docs/logging.pod |
| 154 |
|
| 155 |
|
| 156 |
=head1 AUTHOR |
| 157 |
|
| 158 |
Written by Gavin Carr <[email protected]>. |
| 159 |
|
| 160 |
nfslock feature by JT Moree <[email protected]> - 2007-01-22 |
| 161 |
|
| 162 |
p0f feature by Matt Simerson <[email protected]> - 2010-05-03 |
| 163 |
|
| 164 |
geoip, loglevel, reject added. Refactored into subs - Matt Simerson - 2012-05 |
| 165 |
|
| 166 |
=cut |
| 167 |
|
| 168 |
use strict; |
| 169 |
use warnings; |
| 170 |
use Qpsmtpd::Constants; |
| 171 |
|
| 172 |
my $VERSION = '0.11'; |
| 173 |
|
| 174 |
BEGIN { @AnyDBM_File::ISA = qw(DB_File GDBM_File NDBM_File) } |
| 175 |
use AnyDBM_File; |
| 176 |
use Fcntl qw(:DEFAULT :flock LOCK_EX LOCK_NB); |
| 177 |
use Net::IP; |
| 178 |
|
| 179 |
my $DENYMSG = "This mail is temporarily denied"; |
| 180 |
my ($QPHOME) = ($0 =~ m!(.*?)/([^/]+)$!); |
| 181 |
my $DB = "greylist.dbm"; |
| 182 |
my %PERMITTED_ARGS = map { $_ => 1 } qw(per_recipient remote_ip sender |
| 183 |
recipient black_timeout grey_timeout white_timeout deny_late db_dir |
| 184 |
nfslock p0f reject loglevel geoip upgrade ); |
| 185 |
|
| 186 |
my %DEFAULTS = ( |
| 187 |
remote_ip => 1, |
| 188 |
sender => 0, |
| 189 |
recipient => 0, |
| 190 |
reject => 1, |
| 191 |
black_timeout => 50 * 60, # 50m |
| 192 |
grey_timeout => 3 * 3600 + 20 * 60, # 3h:20m |
| 193 |
white_timeout => 36 * 3600 * 24, # 36 days |
| 194 |
nfslock => 0, |
| 195 |
p0f => undef, |
| 196 |
); |
| 197 |
|
| 198 |
sub register { |
| 199 |
my ($self, $qp, %arg) = @_; |
| 200 |
my $config = { |
| 201 |
%DEFAULTS, |
| 202 |
map { split /\s+/, $_, 2 } $self->qp->config('denysoft_greylist'), |
| 203 |
%arg |
| 204 |
}; |
| 205 |
if (my @bad = grep { !exists $PERMITTED_ARGS{$_} } sort keys %$config) { |
| 206 |
$self->log(LOGALERT, "invalid parameter(s): " . join(',', @bad)); |
| 207 |
} |
| 208 |
|
| 209 |
# backwards compatibility with deprecated 'mode' setting |
| 210 |
if (defined $config->{mode} && !defined $config->{reject}) { |
| 211 |
$config->{reject} = $config->{mode} =~ /testonly|off/i ? 0 : 1; |
| 212 |
} |
| 213 |
$self->{_args} = $config; |
| 214 |
unless ($config->{recipient} || $config->{per_recipient}) { |
| 215 |
$self->register_hook('mail', 'mail_handler'); |
| 216 |
} |
| 217 |
else { |
| 218 |
$self->register_hook('rcpt', 'rcpt_handler'); |
| 219 |
} |
| 220 |
$self->prune_db(); |
| 221 |
if ($self->{_args}{upgrade}) { |
| 222 |
$self->convert_db(); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
sub mail_handler { |
| 227 |
my ($self, $transaction, $sender) = @_; |
| 228 |
|
| 229 |
my ($status, $msg) = $self->greylist($transaction, $sender); |
| 230 |
|
| 231 |
return DECLINED if $status != DENYSOFT; |
| 232 |
|
| 233 |
if (!$self->{_args}{deny_late}) { |
| 234 |
return (DENYSOFT, $msg); |
| 235 |
} |
| 236 |
|
| 237 |
$transaction->notes('greylist', $msg); |
| 238 |
return DECLINED; |
| 239 |
} |
| 240 |
|
| 241 |
sub rcpt_handler { |
| 242 |
my ($self, $transaction, $rcpt) = @_; |
| 243 |
|
| 244 |
# Load per_recipient configs |
| 245 |
my $config = { |
| 246 |
%{$self->{_args}}, |
| 247 |
map { split /\s+/, $_, 2 } |
| 248 |
$self->qp->config('denysoft_greylist', {rcpt => $rcpt}) |
| 249 |
}; |
| 250 |
|
| 251 |
# Check greylisting |
| 252 |
my $sender = $transaction->sender; |
| 253 |
my ($status, $msg) = $self->greylist($transaction, $sender, $rcpt, $config); |
| 254 |
if ($status == DENYSOFT) { |
| 255 |
|
| 256 |
# Deny here (per-rcpt) unless this is a <> sender, for smtp probes |
| 257 |
return DENYSOFT, $msg if $sender->address; |
| 258 |
$transaction->notes('greylist', $msg); |
| 259 |
} |
| 260 |
return DECLINED; |
| 261 |
} |
| 262 |
|
| 263 |
sub hook_data { |
| 264 |
my ($self, $transaction) = @_; |
| 265 |
return DECLINED unless $transaction->notes('greylist'); |
| 266 |
|
| 267 |
# Decline if ALL recipients are whitelisted |
| 268 |
if (($transaction->notes('whitelistrcpt') || 0) == |
| 269 |
scalar($transaction->recipients)) |
| 270 |
{ |
| 271 |
$self->log(LOGWARN, "skip: all recipients whitelisted"); |
| 272 |
return DECLINED; |
| 273 |
} |
| 274 |
return DENYSOFT, $transaction->notes('greylist'); |
| 275 |
} |
| 276 |
|
| 277 |
sub greylist { |
| 278 |
my ($self, $transaction, $sender, $rcpt, $config) = @_; |
| 279 |
$config ||= $self->{_args}; |
| 280 |
$self->log(LOGDEBUG, |
| 281 |
"config: " |
| 282 |
. join(',', |
| 283 |
map { $_ . '=' . $config->{$_} } sort keys %$config) |
| 284 |
); |
| 285 |
|
| 286 |
return DECLINED if $self->is_immune(); |
| 287 |
return DECLINED if !$self->is_p0f_match(); |
| 288 |
return DECLINED if $self->geoip_match(); |
| 289 |
|
| 290 |
my $db = $self->get_db_location(); |
| 291 |
my $lock = $self->get_db_lock($db) or return DECLINED; |
| 292 |
my $tied = $self->get_db_tie($db, $lock) or return DECLINED; |
| 293 |
my $key = $self->get_db_key($sender, $rcpt) or return DECLINED; |
| 294 |
|
| 295 |
my $fmt = "%s:%d:%d:%d"; |
| 296 |
|
| 297 |
# new IP or entry timed out - record new |
| 298 |
if (!$tied->{$key}) { |
| 299 |
$tied->{$key} = sprintf $fmt, time, 1, 0, 0; |
| 300 |
$self->log(LOGWARN, "fail: initial DENYSOFT, unknown"); |
| 301 |
return $self->cleanup_and_return($tied, $lock); |
| 302 |
} |
| 303 |
|
| 304 |
my ($ts, $new, $black, $white) = split /:/, $tied->{$key}; |
| 305 |
$self->log(LOGDEBUG, "ts: " . localtime($ts) . ", now: " . localtime); |
| 306 |
|
| 307 |
if ($white) { |
| 308 |
|
| 309 |
# white IP - accept unless timed out |
| 310 |
if (time - $ts < $config->{white_timeout}) { |
| 311 |
$tied->{$key} = sprintf $fmt, time, $new, $black, ++$white; |
| 312 |
$self->log(LOGINFO, "pass: white, $white deliveries"); |
| 313 |
return $self->cleanup_and_return($tied, $lock, DECLINED); |
| 314 |
} |
| 315 |
else { |
| 316 |
$self->log(LOGINFO, "key $key has timed out (white)"); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
# Black IP - deny, but don't update timestamp |
| 321 |
if (time - $ts < $config->{black_timeout}) { |
| 322 |
$tied->{$key} = sprintf $fmt, $ts, $new, ++$black, 0; |
| 323 |
$self->log(LOGWARN, |
| 324 |
"fail: black DENYSOFT - $black deferred connections"); |
| 325 |
return $self->cleanup_and_return($tied, $lock); |
| 326 |
} |
| 327 |
|
| 328 |
# Grey IP - accept unless timed out |
| 329 |
elsif (time - $ts < $config->{grey_timeout}) { |
| 330 |
$tied->{$key} = sprintf $fmt, time, $new, $black, 1; |
| 331 |
$self->log(LOGWARN, "pass: updated grey->white"); |
| 332 |
return $self->cleanup_and_return($tied, $lock, DECLINED); |
| 333 |
} |
| 334 |
|
| 335 |
$self->log(LOGWARN, "pass: timed out (grey)"); |
| 336 |
return $self->cleanup_and_return($tied, $lock, DECLINED); |
| 337 |
} |
| 338 |
|
| 339 |
sub cleanup_and_return { |
| 340 |
my ($self, $tied, $lock, $return_val) = @_; |
| 341 |
|
| 342 |
untie $tied; |
| 343 |
close $lock; |
| 344 |
return $return_val if defined $return_val; # explicit override |
| 345 |
return DECLINED |
| 346 |
if defined $self->{_args}{reject} && !$self->{_args}{reject}; |
| 347 |
return (DENYSOFT, $DENYMSG); |
| 348 |
} |
| 349 |
|
| 350 |
sub get_db_key { |
| 351 |
my $self = shift; |
| 352 |
my $sender = shift || $self->qp->transaction->sender; |
| 353 |
my $rcpt = shift || ($self->qp->transaction->recipients)[0]; |
| 354 |
|
| 355 |
my @key; |
| 356 |
if ($self->{_args}{remote_ip}) { |
| 357 |
my $nip = Net::IP->new($self->qp->connection->remote_ip); |
| 358 |
push @key, $nip->intip; # convert IP to integer |
| 359 |
} |
| 360 |
|
| 361 |
push @key, $sender->address || '' if $self->{_args}{sender}; |
| 362 |
push @key, $rcpt->address if $rcpt && $self->{_args}{recipient}; |
| 363 |
if (!scalar @key) { |
| 364 |
$self->log(LOGERROR, "enable one of remote_ip, sender, or recipient!"); |
| 365 |
return; |
| 366 |
} |
| 367 |
return join ':', @key; |
| 368 |
} |
| 369 |
|
| 370 |
sub get_db_tie { |
| 371 |
my ($self, $db, $lock) = @_; |
| 372 |
|
| 373 |
tie(my %db, 'AnyDBM_File', $db, O_CREAT | O_RDWR, 0600) or do { |
| 374 |
$self->log(LOGCRIT, "tie to database $db failed: $!"); |
| 375 |
close $lock; |
| 376 |
return; |
| 377 |
}; |
| 378 |
return \%db; |
| 379 |
} |
| 380 |
|
| 381 |
sub get_db_location { |
| 382 |
my $self = shift; |
| 383 |
|
| 384 |
my $transaction = $self->qp->transaction; |
| 385 |
my $config = $self->{_args}; |
| 386 |
|
| 387 |
if ($config->{db_dir} && $config->{db_dir} =~ m{^([-a-zA-Z0-9./_]+)$}) { |
| 388 |
$config->{db_dir} = $1; |
| 389 |
} |
| 390 |
|
| 391 |
# Setup database location |
| 392 |
my $dbdir; |
| 393 |
if ($config->{per_recipient_db}) { |
| 394 |
$dbdir = $transaction->notes('per_rcpt_configdir'); |
| 395 |
} |
| 396 |
|
| 397 |
my @candidate_dirs = ( |
| 398 |
$dbdir, $config->{db_dir}, |
| 399 |
"/var/lib/qpsmtpd/greylisting", |
| 400 |
"$QPHOME/var/db", "$QPHOME/config", '.' |
| 401 |
); |
| 402 |
|
| 403 |
for my $d (@candidate_dirs) { |
| 404 |
next if !$d || !-d $d; # impossible |
| 405 |
$dbdir = $d; |
| 406 |
last; # first match wins |
| 407 |
} |
| 408 |
my $db = "$dbdir/$DB"; |
| 409 |
if (!-f $db && -f "$dbdir/denysoft_greylist.dbm") { |
| 410 |
$db = "$dbdir/denysoft_greylist.dbm"; # old DB name |
| 411 |
} |
| 412 |
$self->log(LOGDEBUG, "using $db as greylisting database"); |
| 413 |
return $db; |
| 414 |
} |
| 415 |
|
| 416 |
sub get_db_lock { |
| 417 |
my ($self, $db) = @_; |
| 418 |
|
| 419 |
return $self->get_db_lock_nfs($db) if $self->{_args}{nfslock}; |
| 420 |
|
| 421 |
# Check denysoft db |
| 422 |
open(my $lock, ">$db.lock") or do { |
| 423 |
$self->log(LOGCRIT, "opening lockfile failed: $!"); |
| 424 |
return; |
| 425 |
}; |
| 426 |
|
| 427 |
flock($lock, LOCK_EX) or do { |
| 428 |
$self->log(LOGCRIT, "flock of lockfile failed: $!"); |
| 429 |
close $lock; |
| 430 |
return; |
| 431 |
}; |
| 432 |
|
| 433 |
return $lock; |
| 434 |
} |
| 435 |
|
| 436 |
sub get_db_lock_nfs { |
| 437 |
my ($self, $db) = @_; |
| 438 |
|
| 439 |
require File::NFSLock; |
| 440 |
|
| 441 |
### set up a lock - lasts until object looses scope |
| 442 |
my $nfslock = new File::NFSLock { |
| 443 |
file => "$db.lock", |
| 444 |
lock_type => LOCK_EX | LOCK_NB, |
| 445 |
blocking_timeout => 10, # 10 sec |
| 446 |
stale_lock_timeout => 30 * 60, # 30 min |
| 447 |
} |
| 448 |
or do { |
| 449 |
$self->log(LOGCRIT, "nfs lockfile failed: $!"); |
| 450 |
return; |
| 451 |
}; |
| 452 |
|
| 453 |
open(my $lock, "+<$db.lock") or do { |
| 454 |
$self->log(LOGCRIT, "opening nfs lockfile failed: $!"); |
| 455 |
return; |
| 456 |
}; |
| 457 |
|
| 458 |
return $lock; |
| 459 |
} |
| 460 |
|
| 461 |
sub convert_db { |
| 462 |
my $self = shift; |
| 463 |
|
| 464 |
my $db = $self->get_db_location(); |
| 465 |
my $lock = $self->get_db_lock($db) or return DECLINED; |
| 466 |
my $tied = $self->get_db_tie($db, $lock) or return DECLINED; |
| 467 |
my $count = keys %$tied; |
| 468 |
|
| 469 |
my $converted = 0; |
| 470 |
foreach my $key (keys %$tied) { |
| 471 |
my (@parts) = split /:/, $key; |
| 472 |
next if $parts[0] =~ /^[\d]+$/; # already converted |
| 473 |
$converted++; |
| 474 |
my $nip = Net::IP->new($parts[0]); |
| 475 |
$parts[0] = $nip->intip; # convert IP to integer |
| 476 |
my $new_key = join ':', @parts; |
| 477 |
$tied->{$new_key} = $tied->{$key}; |
| 478 |
delete $tied->{$key}; |
| 479 |
} |
| 480 |
untie $tied; |
| 481 |
close $lock; |
| 482 |
$self->log(LOGINFO, "converted $converted of $count DB entries"); |
| 483 |
return $self->cleanup_and_return($tied, $lock, DECLINED); |
| 484 |
} |
| 485 |
|
| 486 |
sub prune_db { |
| 487 |
my $self = shift; |
| 488 |
|
| 489 |
my $db = $self->get_db_location(); |
| 490 |
my $lock = $self->get_db_lock($db) or return DECLINED; |
| 491 |
my $tied = $self->get_db_tie($db, $lock) or return DECLINED; |
| 492 |
my $count = keys %$tied; |
| 493 |
|
| 494 |
my $pruned = 0; |
| 495 |
foreach my $key (keys %$tied) { |
| 496 |
my ($ts, $new, $black, $white) = split /:/, $tied->{$key}; |
| 497 |
my $age = time - $ts; |
| 498 |
next if $age < $self->{_args}{white_timeout}; |
| 499 |
$pruned++; |
| 500 |
delete $tied->{$key}; |
| 501 |
} |
| 502 |
untie $tied; |
| 503 |
close $lock; |
| 504 |
$self->log(LOGINFO, "pruned $pruned of $count DB entries"); |
| 505 |
return $self->cleanup_and_return($tied, $lock, DECLINED); |
| 506 |
} |
| 507 |
|
| 508 |
sub p0f_match { |
| 509 |
my $self = shift; |
| 510 |
|
| 511 |
return if !$self->{_args}{p0f}; |
| 512 |
|
| 513 |
my $p0f = $self->connection->notes('p0f'); |
| 514 |
if (!$p0f || !ref $p0f) { # p0f fingerprint info not found |
| 515 |
$self->LOGINFO(LOGERROR, "p0f info missing"); |
| 516 |
return; |
| 517 |
} |
| 518 |
|
| 519 |
my %valid_matches = map { $_ => 1 } qw( genre detail uptime link distance ); |
| 520 |
my %requested_matches = split(/\,/, $self->{_args}{p0f}); |
| 521 |
|
| 522 |
foreach my $key (keys %requested_matches) { |
| 523 |
next if !$key; |
| 524 |
if (!defined $valid_matches{$key}) { |
| 525 |
$self->log(LOGERROR, "discarding invalid match key ($key)"); |
| 526 |
next; |
| 527 |
} |
| 528 |
my $value = $requested_matches{$key}; |
| 529 |
next if !defined $value; # bad config setting? |
| 530 |
next if !defined $p0f->{$key}; # p0f didn't detect the value |
| 531 |
|
| 532 |
if ($key eq 'distance' && $p0f->{$key} > $value) { |
| 533 |
$self->log(LOGDEBUG, "p0f distance match ($value)"); |
| 534 |
return 1; |
| 535 |
} |
| 536 |
if ($key eq 'genre' && $p0f->{$key} =~ /$value/i) { |
| 537 |
$self->log(LOGDEBUG, "p0f genre match ($value)"); |
| 538 |
return 1; |
| 539 |
} |
| 540 |
if ($key eq 'uptime' && $p0f->{$key} < $value) { |
| 541 |
$self->log(LOGDEBUG, "p0f uptime match ($value)"); |
| 542 |
return 1; |
| 543 |
} |
| 544 |
if ($key eq 'link' && $p0f->{$key} =~ /$value/i) { |
| 545 |
$self->log(LOGDEBUG, "p0f link match ($value)"); |
| 546 |
return 1; |
| 547 |
} |
| 548 |
} |
| 549 |
$self->log(LOGINFO, "skip: no p0f match"); |
| 550 |
return; |
| 551 |
} |
| 552 |
|
| 553 |
sub geoip_match { |
| 554 |
my $self = shift; |
| 555 |
|
| 556 |
return if !$self->{_args}{geoip}; |
| 557 |
|
| 558 |
my $country = $self->connection->notes('geoip_country'); |
| 559 |
my $c_name = $self->connection->notes('geoip_country_name') || ''; |
| 560 |
|
| 561 |
if (!$country) { |
| 562 |
$self->LOGINFO(LOGNOTICE, "skip: no geoip country"); |
| 563 |
return; |
| 564 |
} |
| 565 |
|
| 566 |
my @countries = split /,/, $self->{_args}{geoip}; |
| 567 |
foreach (@countries) { |
| 568 |
$self->LOGINFO(LOGINFO, "pass: geoip country match ($_, $c_name)"); |
| 569 |
return 1 if lc $_ eq lc $country; |
| 570 |
} |
| 571 |
|
| 572 |
$self->LOGINFO(LOGINFO, "skip: no geoip match ($c_name)"); |
| 573 |
return; |
| 574 |
} |
| 575 |
|