From 2dcd34467ee212ecabe8b479cae6cd0580de2f2a Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Tue, 22 May 2012 16:40:39 -0400 Subject: [PATCH] geoip: eval loading of Geo::IP, tests, enabled in config eval Geo::IP and log an error if missing added 2 tests enabled in config/plugins --- config.sample/plugins | 5 +++++ plugins/ident/geoip | 41 ++++++++++++++++++++++++++++---------- t/plugin_tests/ident/geoip | 30 ++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 t/plugin_tests/ident/geoip diff --git a/config.sample/plugins b/config.sample/plugins index b3d35e2..785c7f7 100644 --- a/config.sample/plugins +++ b/config.sample/plugins @@ -15,6 +15,11 @@ # from one IP! hosts_allow +# information plugins +ident/geoip +#ident/p0f /tmp/.p0f_socket version 3 +#connection_time + # enable to accept MAIL FROM:/RCPT TO: addresses without surrounding <> dont_require_anglebrackets diff --git a/plugins/ident/geoip b/plugins/ident/geoip index bfe8e30..f6b337f 100644 --- a/plugins/ident/geoip +++ b/plugins/ident/geoip @@ -7,27 +7,48 @@ do a lookup on incoming connections and record the country of origin. Thats all it does. -It logs the 2 char country code to note 'geoip_country'. -It logs the country name to the connection note 'geoip_country_name'. +It logs the 2 char country code to connection note I. +It logs the country name to the connection note I. Other plugins can use that info to do things to the connection, like reject or greylist. =cut -use Geo::IP; +use strict; +use warnings; -sub hook_connect { - my ($self) = @_; +use Qpsmtpd::Constants; +#use Geo::IP; # eval'ed in register() - my $geoip = Geo::IP->new(GEOIP_STANDARD); +sub register { + my $self = shift; + eval 'use Geo::IP'; + if ( $@ ) { + warn "could not load Geo::IP"; + $self->log( LOGERROR, "could not load Geo::IP" ); + return; + }; + + $self->register_hook( 'connect', 'connect_handler' ); +}; + +sub connect_handler { + my $self = shift; + + my $geoip = Geo::IP->new(); my $remote_ip = $self->qp->connection->remote_ip; - my $c_code = $geoip->country_code_by_addr( $remote_ip ) - or return DECLINED; # if this fails, so too will name - my $c_name = $geoip->country_name_by_addr( $remote_ip ); + my $c_code = $geoip->country_code_by_addr( $remote_ip ) or do { + $self->log( LOGINFO, "fail: no results" ); + return DECLINED; + }; + + my $c_name = $geoip->country_name_by_addr( $remote_ip ); + if ( $c_name ) { + $self->qp->connection->notes('geoip_country_name', $c_name); + }; - $self->qp->connection->notes('geoip_country_name', $c_name); $self->qp->connection->notes('geoip_country', $c_code); my $message = $c_code; diff --git a/t/plugin_tests/ident/geoip b/t/plugin_tests/ident/geoip new file mode 100644 index 0000000..ff8d31f --- /dev/null +++ b/t/plugin_tests/ident/geoip @@ -0,0 +1,30 @@ +#!perl -w + +use strict; +use warnings; + +use Qpsmtpd::Constants; + +sub register_tests { + my $self = shift; + + eval 'use Geo::IP'; + if ( $@ ) { + warn "could not load Geo::IP\n"; + $self->log(LOGERROR, "could not load Geo::IP"); + return; + }; + + $self->register_test('test_geoip_lookup', 2); +}; + +sub test_geoip_lookup { + my $self = shift; + + $self->qp->connection->remote_ip('24.24.24.24'); + cmp_ok( $self->connect_handler(), '==', DECLINED, "exit code"); + + cmp_ok( $self->qp->connection->notes('geoip_country'), 'eq', 'US', "note"); +}; + +