2012-04-29 10:35:59 +02:00
|
|
|
#!perl -w
|
2004-08-29 09:47:25 +02:00
|
|
|
|
2012-06-22 11:38:01 +02:00
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
geoip - provide geographic information about mail senders.
|
|
|
|
|
2012-04-08 02:11:16 +02:00
|
|
|
=head1 SYNOPSIS
|
2004-08-29 09:47:25 +02:00
|
|
|
|
2014-11-04 23:43:54 +01:00
|
|
|
Use MaxMind's GeoIP databases and the GeoIP2 or Geo::IP perl modules to report
|
|
|
|
geographic information about incoming connections.
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
2014-11-10 22:32:28 +01:00
|
|
|
Save geographic information about the sender in the following connection notes:
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
geoip_country - 2 char country code
|
2012-06-23 02:29:42 +02:00
|
|
|
geoip_country_name - english name of country
|
2012-06-22 11:38:01 +02:00
|
|
|
geoip_continent - 2 char continent code
|
2012-06-23 02:29:42 +02:00
|
|
|
geoip_city - english name of city
|
2012-06-22 11:38:01 +02:00
|
|
|
geoip_distance - distance in kilometers
|
2014-11-10 22:32:28 +01:00
|
|
|
geoip_asn - network number
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
And adds entries like this to your logs:
|
|
|
|
|
2012-06-23 02:29:42 +02:00
|
|
|
(connect) ident::geoip: NA, US, United States, 1319 km
|
|
|
|
(connect) ident::geoip: AS, IN, India, 13862 km
|
2012-06-22 11:38:01 +02:00
|
|
|
(connect) ident::geoip: fail: no results
|
2012-06-23 02:29:42 +02:00
|
|
|
(connect) ident::geoip: NA, CA, Canada, 2464 km
|
|
|
|
(connect) ident::geoip: NA, US, United States, 2318 km
|
|
|
|
(connect) ident::geoip: AS, PK, Pakistan, 12578 km
|
|
|
|
(connect) ident::geoip: AS, TJ, Tajikistan, 11965 km
|
|
|
|
(connect) ident::geoip: EU, AT, Austria, 8745 km
|
|
|
|
(connect) ident::geoip: AS, IR, Iran, Islamic Republic of, 12180 km
|
|
|
|
(connect) ident::geoip: EU, BY, Belarus, 9030 km
|
|
|
|
(connect) ident::geoip: AS, CN, China, 11254 km
|
|
|
|
(connect) ident::geoip: NA, PA, Panama, 3163 km
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
Calculating the distance has three prerequsites:
|
|
|
|
|
|
|
|
1. The MaxMind city database (free or subscription)
|
|
|
|
2. The Math::Complex perl module
|
|
|
|
3. The IP address of this mail server (see CONFIG)
|
|
|
|
|
|
|
|
Other plugins can utilize the geographic notes to alter the
|
|
|
|
connection, reject, greylist, etc.
|
|
|
|
|
|
|
|
=head1 CONFIG
|
|
|
|
|
|
|
|
The following options can be appended in this plugins config/plugins entry.
|
|
|
|
|
|
|
|
=head2 distance <IP Address>
|
|
|
|
|
|
|
|
Enables geodesic distance calculation. Will calculate the distance "as the
|
|
|
|
crow flies" from the remote mail server. Accepts a single argument, the IP
|
|
|
|
address to calculate the distance from. This will typically be the public
|
|
|
|
IP of your mail server.
|
|
|
|
|
|
|
|
ident/geoip [ distance 192.0.1.5 ]
|
|
|
|
|
|
|
|
Default: none. (no distance calculations)
|
|
|
|
|
2013-03-23 06:03:28 +01:00
|
|
|
=head2 too_far <distance in km>
|
|
|
|
|
|
|
|
Assign negative karma to connections further than this many km.
|
|
|
|
|
|
|
|
Default: none
|
|
|
|
|
2012-06-22 11:38:01 +02:00
|
|
|
=head2 db_dir </path/to/GeoIP>
|
|
|
|
|
|
|
|
The path to the GeoIP database directory.
|
|
|
|
|
|
|
|
ident/geoip [ db_dir /etc/GeoIP ]
|
|
|
|
|
|
|
|
Default: /usr/local/share/GeoIP
|
|
|
|
|
|
|
|
=head1 LIMITATIONS
|
|
|
|
|
|
|
|
The distance calculations are more concerned with being fast than accurate.
|
|
|
|
The MaxMind location data is collected from whois and is of limited accuracy.
|
|
|
|
MaxMind offers more accurate data for a fee.
|
|
|
|
|
|
|
|
For distance calculations, the earth is considered a perfect sphere. In
|
|
|
|
reality, it is not. Accuracy should be within 1%.
|
|
|
|
|
|
|
|
This plugin does not update the GeoIP databases. You may want to.
|
|
|
|
|
|
|
|
=head1 CHANGES
|
|
|
|
|
2014-11-05 09:23:31 +01:00
|
|
|
2014-06 - Matt Simerson - added GeoIP2 support
|
|
|
|
|
2012-06-22 11:38:01 +02:00
|
|
|
2012-06 - Matt Simerson - added GeoIP City support, continent, distance
|
2004-08-29 09:47:25 +02:00
|
|
|
|
2012-06-22 11:38:01 +02:00
|
|
|
2012-05 - Matt Simerson - added geoip_country_name note, added tests
|
2004-08-29 09:47:25 +02:00
|
|
|
|
2012-06-22 11:38:01 +02:00
|
|
|
=head1 SEE ALSO
|
2012-05-07 09:36:00 +02:00
|
|
|
|
2012-06-22 11:38:01 +02:00
|
|
|
MaxMind: http://www.maxmind.com/
|
|
|
|
|
|
|
|
Databases: http://geolite.maxmind.com/download/geoip/database
|
|
|
|
|
|
|
|
It may become worth adding support for Geo::IPfree, which uses another
|
|
|
|
data source: http://software77.net/geo-ip/
|
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS
|
|
|
|
|
2014-11-05 22:50:19 +01:00
|
|
|
MaxMind - the packager and distributor of the free GeoIP data
|
|
|
|
|
2012-06-22 11:38:01 +02:00
|
|
|
Stevan Bajic, the DSPAM author, who suggested SNARE, which describes using
|
|
|
|
geodesic distance to determine spam probability. The research paper on SNARE
|
|
|
|
can be found here:
|
|
|
|
http://smartech.gatech.edu/bitstream/handle/1853/25135/GT-CSE-08-02.pdf
|
2004-08-29 09:47:25 +02:00
|
|
|
|
|
|
|
=cut
|
|
|
|
|
2012-05-22 22:40:39 +02:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
2004-08-29 09:47:25 +02:00
|
|
|
|
2014-11-04 23:43:54 +01:00
|
|
|
use lib 'lib';
|
2012-05-22 22:40:39 +02:00
|
|
|
use Qpsmtpd::Constants;
|
2013-04-21 06:50:39 +02:00
|
|
|
|
2014-11-04 23:43:54 +01:00
|
|
|
#use GeoIP2; # eval'ed in register()
|
|
|
|
#use Geo::IP; # eval loaded if GeoIP2 doesn't
|
2012-06-22 11:38:01 +02:00
|
|
|
#use Math::Trig; # eval'ed in set_distance_gc
|
2004-08-29 09:47:25 +02:00
|
|
|
|
2012-05-22 22:40:39 +02:00
|
|
|
sub register {
|
2013-12-18 06:02:07 +01:00
|
|
|
my ($self, $qp, @args) = @_;
|
2012-06-22 11:38:01 +02:00
|
|
|
|
2013-12-18 06:02:07 +01:00
|
|
|
$self->log(LOGERROR, "Bad arguments") if @args % 2;
|
|
|
|
$self->{_args} = {@args};
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->{_args}{db_dir} ||= '/usr/local/share/GeoIP';
|
2012-06-22 11:38:01 +02:00
|
|
|
|
2014-11-07 22:58:12 +01:00
|
|
|
$self->load_geoip() or return;
|
2014-11-07 23:32:44 +01:00
|
|
|
return if ! $self->qp->config('add_geoip_headers');
|
2014-11-07 23:30:54 +01:00
|
|
|
$self->register_hook( data_post => 'add_headers' );
|
2014-11-07 22:58:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub load_geoip {
|
2014-11-07 23:22:49 +01:00
|
|
|
my ( $self ) = @_;
|
2014-11-07 22:58:12 +01:00
|
|
|
$self->load_geoip2() and return 1;
|
|
|
|
$self->load_geoip1() and return 1;
|
2014-11-05 22:50:19 +01:00
|
|
|
}
|
2014-11-05 09:23:31 +01:00
|
|
|
|
2014-11-05 22:50:19 +01:00
|
|
|
sub load_geoip1 {
|
|
|
|
my $self = shift;
|
2014-11-05 09:23:31 +01:00
|
|
|
|
|
|
|
eval 'use Geo::IP';
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($@) {
|
2014-11-05 09:23:31 +01:00
|
|
|
warn "could not load Geo::IP";
|
|
|
|
$self->log(LOGERROR, "could not load Geo::IP");
|
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-22 22:40:39 +02:00
|
|
|
|
2014-11-05 09:23:31 +01:00
|
|
|
$self->open_geoip_db();
|
|
|
|
|
2012-06-22 11:38:01 +02:00
|
|
|
# Note that opening the GeoIP DB only in register has caused problems before:
|
|
|
|
# https://github.com/smtpd/qpsmtpd/commit/29ea9516806e9a8ca6519fcf987dbd684793ebdd#plugins/ident/geoip
|
|
|
|
# Opening the DB anew for every connection is horribly inefficient.
|
|
|
|
# Instead, attempt to reopen upon connect if the DB connection fails.
|
|
|
|
$self->init_my_country_code();
|
|
|
|
|
2014-11-11 01:29:59 +01:00
|
|
|
$self->register_hook('connect', 'geoip_lookup');
|
2014-11-07 22:58:12 +01:00
|
|
|
return 1;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-22 22:40:39 +02:00
|
|
|
|
2014-11-05 22:50:19 +01:00
|
|
|
sub load_geoip2 {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
eval 'use GeoIP2::Database::Reader';
|
|
|
|
if ($@) {
|
|
|
|
$self->log(LOGERROR, "could not load GeoIP2");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->log(LOGINFO, "GeoIP2 loaded");
|
|
|
|
|
|
|
|
eval {
|
|
|
|
$self->{_geoip2_city} = GeoIP2::Database::Reader->new(
|
|
|
|
file => $self->{_args}{db_dir} . '/GeoLite2-City.mmdb',
|
|
|
|
);
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
$self->log(LOGERROR, "unable to load GeoLite2-City.mmdb");
|
|
|
|
}
|
|
|
|
|
|
|
|
eval {
|
|
|
|
$self->{_geoip2_country} = GeoIP2::Database::Reader->new(
|
|
|
|
file => $self->{_args}{db_dir} . '/GeoLite2-Country.mmdb',
|
|
|
|
);
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
$self->log(LOGERROR, "unable to load GeoLite2-Country.mmdb");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($self->{_geoip2_city} || $self->{_geoip2_country}) {
|
|
|
|
$self->register_hook('connect', 'geoip2_lookup');
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-07 23:30:54 +01:00
|
|
|
sub add_headers {
|
|
|
|
my ( $self, $txn ) = @_;
|
|
|
|
for my $h (qw( Country Country-Name Continent City ASN )) {
|
|
|
|
my $note = lc "geoip_$h";
|
|
|
|
$h =~ s/-/_/;
|
|
|
|
next if ! $self->connection->notes($note);
|
|
|
|
$txn->header->delete("X-GeoIP-$h");
|
|
|
|
$txn->header->add( "X-GeoIP-$h", $self->connection->notes($note), 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-05 09:23:31 +01:00
|
|
|
sub geoip2_lookup {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $ip = $self->qp->connection->remote_ip;
|
2014-11-11 07:49:46 +01:00
|
|
|
return DECLINED if $self->is_localhost($ip);
|
2014-11-05 09:23:31 +01:00
|
|
|
|
|
|
|
if ($self->{_geoip2_city}) {
|
|
|
|
my $city_rec = $self->{_geoip2_city}->city(ip => $ip);
|
|
|
|
if ($city_rec) {
|
|
|
|
$self->qp->connection->notes('geoip_country', $city_rec->country->iso_code());
|
|
|
|
$self->qp->connection->notes('geoip_country_name', $city_rec->country->name());
|
|
|
|
$self->qp->connection->notes('geoip_continent', $city_rec->continent->code());
|
|
|
|
$self->qp->connection->notes('geoip_city', $city_rec->city->name());
|
|
|
|
$self->qp->connection->notes('geoip_asn', $city_rec->traits->autonomous_system_number());
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($self->{_geoip2_country}) {
|
|
|
|
my $country_rec = $self->{_geoip2_country}->country(ip => $ip);
|
|
|
|
if ($country_rec) {
|
|
|
|
$self->qp->connection->notes('geoip_country', $country_rec->country->iso_code());
|
|
|
|
$self->qp->connection->notes('geoip_country_name', $country_rec->country->name());
|
|
|
|
$self->qp->connection->notes('geoip_continent', $country_rec->continent->code());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
2014-11-04 23:43:54 +01:00
|
|
|
sub geoip_lookup {
|
2012-05-22 22:40:39 +02:00
|
|
|
my $self = shift;
|
|
|
|
|
2014-11-11 07:49:46 +01:00
|
|
|
return DECLINED if $self->is_localhost($self->qp->connection->remote_ip);
|
|
|
|
|
2012-06-22 11:38:01 +02:00
|
|
|
# reopen the DB if Geo::IP failed due to DB update
|
|
|
|
$self->open_geoip_db();
|
2012-05-07 09:36:00 +02:00
|
|
|
|
2012-06-22 11:38:01 +02:00
|
|
|
my $c_code = $self->set_country_code() or do {
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->log(LOGINFO, "skip, no results");
|
2012-05-22 22:40:39 +02:00
|
|
|
return DECLINED;
|
|
|
|
};
|
|
|
|
|
2014-11-05 23:23:06 +01:00
|
|
|
$self->set_asn();
|
|
|
|
|
2012-06-22 11:38:01 +02:00
|
|
|
my $c_name = $self->set_country_name();
|
2012-06-23 02:29:42 +02:00
|
|
|
my ($city, $continent_code, $distance) = '';
|
2012-05-07 09:36:00 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($self->{_my_country_code}) {
|
|
|
|
$continent_code = $self->set_continent($c_code);
|
|
|
|
$city = $self->set_city_gc();
|
|
|
|
$distance = $self->set_distance_gc();
|
|
|
|
}
|
2004-08-29 09:47:25 +02:00
|
|
|
|
2012-06-23 02:29:42 +02:00
|
|
|
my @msg_parts;
|
2014-11-04 23:43:54 +01:00
|
|
|
if ($continent_code && $continent_code ne '--') {
|
|
|
|
push @msg_parts, $continent_code;
|
|
|
|
};
|
2013-04-21 06:50:39 +02:00
|
|
|
push @msg_parts, $c_code if $c_code;
|
|
|
|
|
2012-06-23 02:29:42 +02:00
|
|
|
#push @msg_parts, $c_name if $c_name;
|
2013-04-21 06:50:39 +02:00
|
|
|
push @msg_parts, $city if $city;
|
|
|
|
if ($distance) {
|
2013-03-23 06:03:28 +01:00
|
|
|
push @msg_parts, "\t$distance km";
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($self->{_args}{too_far} && $distance > $self->{_args}{too_far}) {
|
|
|
|
$self->adjust_karma(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$self->log(LOGINFO, join(", ", @msg_parts));
|
2004-08-29 09:47:25 +02:00
|
|
|
|
2012-04-22 23:00:53 +02:00
|
|
|
return DECLINED;
|
2004-08-29 09:47:25 +02:00
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
sub open_geoip_db {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# this might detect if the DB connection failed. If not, this is where
|
|
|
|
# to add more code to do it.
|
2013-04-21 06:50:39 +02:00
|
|
|
return if (defined $self->{_geoip_city} || defined $self->{_geoip});
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
# The methods for using GeoIP work differently for the City vs Country DB
|
|
|
|
# save the handles in different locations
|
|
|
|
my $db_dir = $self->{_args}{db_dir};
|
2013-04-21 06:50:39 +02:00
|
|
|
foreach my $db (qw/ GeoIPCity GeoLiteCity /) {
|
2014-11-05 09:23:31 +01:00
|
|
|
next if !-f "$db_dir/$db.dat";
|
2014-11-05 22:50:19 +01:00
|
|
|
$self->log(LOGINFO, "using db $db");
|
2014-11-05 09:23:31 +01:00
|
|
|
$self->{_geoip_city} = Geo::IP->open("$db_dir/$db.dat");
|
|
|
|
}
|
|
|
|
|
2014-11-05 22:50:19 +01:00
|
|
|
if (-f "$db_dir/GeoIPASNum.dat") {
|
|
|
|
$self->log(LOGINFO, "using GeoIPASNum");
|
|
|
|
$self->{GeoIPASNum} = Geo::IP->open("$db_dir/GeoIPASNum.dat");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-f "$db_dir/GeoIPASNumv6.dat") {
|
|
|
|
$self->log(LOGINFO, "using GeoIPASNumv6");
|
|
|
|
$self->{GeoIPASNumv6} = Geo::IP->open("$db_dir/GeoIPASNumv6.dat");
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
# can't think of a good reason to load country if city data is present
|
2013-04-21 06:50:39 +02:00
|
|
|
if (!$self->{_geoip_city}) {
|
2012-06-22 11:38:01 +02:00
|
|
|
$self->log(LOGDEBUG, "using default db");
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->{_geoip} = Geo::IP->new(); # loads default Country DB
|
|
|
|
}
|
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
sub init_my_country_code {
|
|
|
|
my $self = shift;
|
|
|
|
my $ip = $self->{_args}{distance} or return;
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->{_my_country_code} = $self->get_country_code($ip);
|
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
sub set_country_code {
|
|
|
|
my $self = shift;
|
|
|
|
my $remote_ip = $self->qp->connection->remote_ip;
|
2014-11-04 23:43:54 +01:00
|
|
|
|
|
|
|
my $code = $self->{_geoip_city}
|
|
|
|
? $self->get_country_code_gc($remote_ip)
|
|
|
|
: $self->get_country_code($remote_ip);
|
|
|
|
|
|
|
|
return if ! $code;
|
2012-06-22 11:38:01 +02:00
|
|
|
$self->qp->connection->notes('geoip_country', $code);
|
|
|
|
return $code;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
sub get_country_code {
|
|
|
|
my $self = shift;
|
|
|
|
my $ip = shift || $self->qp->connection->remote_ip;
|
2014-11-04 23:43:54 +01:00
|
|
|
if ($self->{_geoip_city}) {
|
|
|
|
return $self->get_country_code_gc($ip);
|
|
|
|
};
|
2013-04-21 06:50:39 +02:00
|
|
|
return $self->{_geoip}->country_code_by_addr($ip);
|
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
sub get_country_code_gc {
|
|
|
|
my $self = shift;
|
2013-04-21 06:50:39 +02:00
|
|
|
my $ip = shift || $self->qp->connection->remote_ip;
|
|
|
|
$self->{_geoip_record} = $self->{_geoip_city}->record_by_addr($ip)
|
|
|
|
or return;
|
2012-06-22 11:38:01 +02:00
|
|
|
return $self->{_geoip_record}->country_code;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
sub set_country_name {
|
|
|
|
my $self = shift;
|
|
|
|
my $remote_ip = $self->qp->connection->remote_ip;
|
2014-11-04 23:43:54 +01:00
|
|
|
|
|
|
|
my $name = $self->{_geoip_city}
|
|
|
|
? $self->get_country_name_gc($remote_ip)
|
|
|
|
: $self->{_geoip}->country_name_by_addr($remote_ip);
|
|
|
|
|
|
|
|
return if ! $name;
|
2012-06-22 11:38:01 +02:00
|
|
|
$self->qp->connection->notes('geoip_country_name', $name);
|
|
|
|
return $name;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
2014-11-04 23:43:54 +01:00
|
|
|
sub get_country_name_gc {
|
2012-06-22 11:38:01 +02:00
|
|
|
my $self = shift;
|
2013-04-21 06:50:39 +02:00
|
|
|
return if !$self->{_geoip_record};
|
2014-11-04 23:43:54 +01:00
|
|
|
return $self->{_geoip_record}->country_name();
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
sub set_continent {
|
2014-11-04 23:43:54 +01:00
|
|
|
my ($self, $country_code) = @_;
|
|
|
|
$country_code or return;
|
|
|
|
|
|
|
|
my $continent = $self->{_geoip_city}
|
|
|
|
? $self->get_continent_gc()
|
|
|
|
: $self->{_geoip}->continent_code_by_country_code($country_code);
|
|
|
|
|
|
|
|
$continent or return;
|
2012-06-22 11:38:01 +02:00
|
|
|
$self->qp->connection->notes('geoip_continent', $continent);
|
|
|
|
return $continent;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
2014-11-04 23:43:54 +01:00
|
|
|
sub get_continent_gc {
|
2012-06-22 11:38:01 +02:00
|
|
|
my $self = shift;
|
2013-04-21 06:50:39 +02:00
|
|
|
return if !$self->{_geoip_record};
|
2014-11-04 23:43:54 +01:00
|
|
|
return $self->{_geoip_record}->continent_code();
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2014-11-05 09:23:31 +01:00
|
|
|
|
|
|
|
sub set_asn {
|
2014-11-05 22:50:19 +01:00
|
|
|
my ($self, $ip) = @_;
|
|
|
|
$ip ||= $self->qp->connection->remote_ip;
|
2014-11-05 09:23:31 +01:00
|
|
|
|
2014-11-05 22:50:19 +01:00
|
|
|
if (Qpsmtpd::Base->is_ipv6($ip)) {
|
|
|
|
return $self->set_asn_ipv6($ip);
|
|
|
|
}
|
|
|
|
return if ! $self->{GeoIPASNum};
|
2014-11-05 09:23:31 +01:00
|
|
|
|
2014-11-05 22:50:19 +01:00
|
|
|
my $asn = $self->{GeoIPASNum}->name_by_addr($ip) or return;
|
2014-11-05 22:52:21 +01:00
|
|
|
if ('AS' eq substr($asn, 0, 2)) {
|
|
|
|
$asn = substr($asn, 2);
|
|
|
|
}
|
2014-11-05 09:23:31 +01:00
|
|
|
$self->qp->connection->notes('geoip_asn', $asn);
|
|
|
|
return $asn;
|
|
|
|
}
|
|
|
|
|
2014-11-05 22:50:19 +01:00
|
|
|
sub set_asn_ipv6 {
|
|
|
|
my ($self, $ip) = @_;
|
|
|
|
$ip ||= $self->qp->connection->remote_ip;
|
|
|
|
|
|
|
|
return if ! $self->{GeoIPASNumv6};
|
|
|
|
|
|
|
|
my $asn = $self->{GeoIPASNumv6}->name_by_addr_v6($ip) or return;
|
|
|
|
$self->qp->connection->notes('geoip_asn', $asn);
|
|
|
|
return $asn;
|
2014-11-05 09:23:31 +01:00
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
2012-06-23 02:29:42 +02:00
|
|
|
sub set_city_gc {
|
|
|
|
my $self = shift;
|
2013-04-21 06:50:39 +02:00
|
|
|
return if !$self->{_geoip_record};
|
2012-06-23 02:29:42 +02:00
|
|
|
my $city = $self->{_geoip_record}->city() or return;
|
|
|
|
$self->qp->connection->notes('geoip_city', $city);
|
|
|
|
return $city;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-23 02:29:42 +02:00
|
|
|
|
2012-06-22 11:38:01 +02:00
|
|
|
sub set_distance_gc {
|
|
|
|
my $self = shift;
|
2013-04-21 06:50:39 +02:00
|
|
|
return if !$self->{_geoip_record};
|
2012-06-22 11:38:01 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self_lat, $self_lon) = $self->get_my_lat_lon() or return;
|
2012-06-22 11:38:01 +02:00
|
|
|
my ($sender_lat, $sender_lon) = $self->get_sender_lat_lon() or return;
|
|
|
|
|
|
|
|
eval 'use Math::Trig qw(great_circle_distance deg2rad)';
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($@) {
|
|
|
|
$self->log(LOGERROR,
|
|
|
|
"can't calculate distance, Math::Trig not installed");
|
2012-06-22 11:38:01 +02:00
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
# Notice the 90 - latitude: phi zero is at the North Pole.
|
2013-04-21 06:50:39 +02:00
|
|
|
sub NESW { deg2rad($_[0]), deg2rad(90 - $_[1]) }
|
|
|
|
my @me = NESW($self_lon, $self_lat);
|
2012-06-22 11:38:01 +02:00
|
|
|
my @sender = NESW($sender_lon, $sender_lat);
|
2013-04-21 06:50:39 +02:00
|
|
|
my $km = great_circle_distance(@me, @sender, 6378);
|
2012-06-22 11:38:01 +02:00
|
|
|
$km = sprintf("%.0f", $km);
|
|
|
|
|
|
|
|
$self->qp->connection->notes('geoip_distance', $km);
|
2013-04-21 06:50:39 +02:00
|
|
|
|
2012-06-22 11:38:01 +02:00
|
|
|
#$self->log( LOGINFO, "distance $km km");
|
|
|
|
return $km;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
sub get_my_lat_lon {
|
|
|
|
my $self = shift;
|
2013-04-21 06:50:39 +02:00
|
|
|
return if !$self->{_geoip_city};
|
2012-06-22 11:38:01 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($self->{_latitude} && $self->{_longitude}) {
|
2014-09-18 03:28:51 +02:00
|
|
|
return $self->{_latitude}, $self->{_longitude}; # cached
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
my $ip = $self->{_args}{distance} or return;
|
2012-06-22 11:38:01 +02:00
|
|
|
my $record = $self->{_geoip_city}->record_by_addr($ip) or do {
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->log(LOGERROR, "no record for my Geo::IP location");
|
2012-06-22 11:38:01 +02:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
$self->{_latitude} = $record->latitude();
|
|
|
|
$self->{_longitude} = $record->longitude();
|
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if (!$self->{_latitude} || !$self->{_longitude}) {
|
|
|
|
$self->log(LOGNOTICE, "could not get my lat/lon");
|
|
|
|
}
|
2014-09-18 03:28:51 +02:00
|
|
|
return $self->{_latitude}, $self->{_longitude};
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
sub get_sender_lat_lon {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $lat = $self->{_geoip_record}->latitude();
|
|
|
|
my $lon = $self->{_geoip_record}->longitude();
|
2013-04-21 06:50:39 +02:00
|
|
|
if (!$lat || !$lon) {
|
|
|
|
$self->log(LOGNOTICE, "could not get sender lat/lon");
|
2012-06-22 11:38:01 +02:00
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2014-09-18 03:28:51 +02:00
|
|
|
return $lat, $lon;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|