From cff651507fe6fb2e9f4e6bda21fe5b5718c9e65c Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Mon, 10 Nov 2014 12:49:29 -0800 Subject: [PATCH 1/5] add maxmax DB fetch script to bin/ --- Makefile.PL | 3 ++- bin/geolite-mirror-simple.pl | 49 ++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 bin/geolite-mirror-simple.pl diff --git a/Makefile.PL b/Makefile.PL index 8ae2b99..945db7c 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -32,7 +32,8 @@ WriteMakefile( # 'Mail::SpamAssassin' => 0, # 'GeoIP2' => 2, # 'Geo::IP' => 1, -# 'Math::Complex' => 0, # geodesic distance in geoip +# 'Math::Complex' => 0, # geodesic distance in Geo::IP +# 'PerlIO::gzip' => 0, # gunzip GeoIP databases # 'Mail::SPF' => 0, }, ABSTRACT => 'Flexible smtpd daemon written in Perl', diff --git a/bin/geolite-mirror-simple.pl b/bin/geolite-mirror-simple.pl new file mode 100644 index 0000000..a3338eb --- /dev/null +++ b/bin/geolite-mirror-simple.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl +use strict; +our $VERSION = '0.02'; +use LWP::Simple qw/ mirror RC_NOT_MODIFIED RC_OK $ua /; +use File::Copy qw/ mv /; +use File::Spec; +use PerlIO::gzip; + +# --- maxmind.com - please send comments to support@maxmind.com +# +# mirror various maxmind databases from geolite.maxmind.com. +# The script download only changed files, unzip the files and +# move it into the desired directory. +# +# Here is a sample cron entry that check daily for new files. +# 34 15 * * * /usr/local/bin/geolite-mirror-simple.pl + +# adjust the path to your needs. Make sure the directories exists +-d ( my $download_dir = '/usr/local/share/GeoIP/download' ) or die $!; +-d ( my $dest_dir = '/usr/local/share/GeoIP' ) or die $!; + +# --- remove lines you do not need +# geoip customers should rename or remove GeoIP.dat.gz and GeoIPCity.dat.gz +# This example overwrite your GeoIPCity.dat database! + +my %mirror = ( # local-filename geolite-name + 'GeoIP.dat.gz' => 'GeoLiteCountry/GeoIP.dat.gz', + 'GeoIPCity.dat.gz' => 'GeoLiteCity.dat.gz', + 'GeoIPv6.dat.gz' => 'GeoIPv6.dat.gz', + 'GeoIPASNum.dat.gz' => 'asnum/GeoIPASNum.dat.gz' +); + +$ua->agent("MaxMind-geolite-mirror-simple/$VERSION"); +my $dl_path = 'http://geolite.maxmind.com/download/geoip/database/'; + +chdir $download_dir or die $!; +for my $f ( keys %mirror ) { + my $rc = mirror( $dl_path . $mirror{$f}, $f ); + next if $rc == RC_NOT_MODIFIED; + if ( $rc == RC_OK ) { + ( my $outfile = $f ) =~ s/\.gz$//; + open my $in, '<:gzip', $f or die $!; + open my $out, '>', $outfile or die $!; + print $out $_ or die $! while <$in>; + mv( $outfile, File::Spec->catfile( $dest_dir, $outfile ) ) or die $!; + } +} +exit 0; + From 4acf8602bdac5b5a6d1dd44b8a072372a46d642e Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Mon, 10 Nov 2014 13:32:28 -0800 Subject: [PATCH 2/5] update docs with geoip_asn note addition --- plugins/ident/geoip | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/ident/geoip b/plugins/ident/geoip index 0a44eaa..9f60fd5 100644 --- a/plugins/ident/geoip +++ b/plugins/ident/geoip @@ -11,13 +11,14 @@ geographic information about incoming connections. =head1 DESCRIPTION -This plugin saves geographic information in the following connection notes: +Save geographic information about the sender in the following connection notes: geoip_country - 2 char country code geoip_country_name - english name of country geoip_continent - 2 char continent code geoip_city - english name of city geoip_distance - distance in kilometers + geoip_asn - network number And adds entries like this to your logs: @@ -150,7 +151,7 @@ sub load_geoip1 { # Instead, attempt to reopen upon connect if the DB connection fails. $self->init_my_country_code(); - $self->register_hook('connect', 'geoip_lookup'); + $self->register_hook('connect', 'geoip_lookup'); } sub load_geoip2 { @@ -464,4 +465,3 @@ sub get_sender_lat_lon { } return $lat, $lon; } - From 3acc6dd3d99b7bfeb7bad87fa33af68dced22781 Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Mon, 10 Nov 2014 16:29:59 -0800 Subject: [PATCH 3/5] updated MANIFEST with new bin/ file --- MANIFEST | 2 ++ plugins/ident/geoip | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/MANIFEST b/MANIFEST index 4ebdec3..89a1018 100644 --- a/MANIFEST +++ b/MANIFEST @@ -1,5 +1,6 @@ .gitignore .travis.yml +bin/geolite-mirror-simple.pl bin/install_deps.pl Changes config.sample/badhelo @@ -161,6 +162,7 @@ t/config/flat_auth_pw t/config/invalid_resolvable_fromhost t/config/norelayclients t/config/plugins +t/config/postgrey_whitelist_clients t/config/public_suffix_list t/config/rcpthosts t/config/relayclients diff --git a/plugins/ident/geoip b/plugins/ident/geoip index 9f60fd5..bb8b193 100644 --- a/plugins/ident/geoip +++ b/plugins/ident/geoip @@ -151,7 +151,7 @@ sub load_geoip1 { # Instead, attempt to reopen upon connect if the DB connection fails. $self->init_my_country_code(); - $self->register_hook('connect', 'geoip_lookup'); + $self->register_hook('connect', 'geoip_lookup'); } sub load_geoip2 { From 03e7ef722c2f7adb36b52faca2f4652314b6241b Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Tue, 11 Nov 2014 01:49:46 -0500 Subject: [PATCH 4/5] geoip: skip lookups for localhost --- plugins/ident/geoip | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/ident/geoip b/plugins/ident/geoip index bb8b193..df66dd1 100644 --- a/plugins/ident/geoip +++ b/plugins/ident/geoip @@ -195,6 +195,7 @@ sub geoip2_lookup { my $self = shift; my $ip = $self->qp->connection->remote_ip; + return DECLINED if $self->is_localhost($ip); if ($self->{_geoip2_city}) { my $city_rec = $self->{_geoip2_city}->city(ip => $ip); @@ -223,6 +224,8 @@ sub geoip2_lookup { sub geoip_lookup { my $self = shift; + return DECLINED if $self->is_localhost($self->qp->connection->remote_ip); + # reopen the DB if Geo::IP failed due to DB update $self->open_geoip_db(); From 84db310736c278f97dc439431c6a317c6c35618c Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Tue, 11 Nov 2014 02:02:21 -0500 Subject: [PATCH 5/5] geoip: don't run ASN tests if ASN db didn't load --- t/plugin_tests/ident/geoip | 2 ++ 1 file changed, 2 insertions(+) diff --git a/t/plugin_tests/ident/geoip b/t/plugin_tests/ident/geoip index 43392fd..56e0b38 100644 --- a/t/plugin_tests/ident/geoip +++ b/t/plugin_tests/ident/geoip @@ -169,6 +169,8 @@ sub test_set_distance { sub test_set_asn { my $self = shift; + return if !$self->{GeoIPASNum}; + $self->qp->connection->remote_ip(''); $self->set_asn(); my $asn = $self->set_asn();