Merge pull request #16 from msimerson/geoip

Geoip plugin improvements
This commit is contained in:
Ask Bjørn Hansen 2012-05-23 15:39:10 -07:00
commit 9e239fd83d
5 changed files with 72 additions and 11 deletions

View File

@ -153,6 +153,7 @@ t/plugin_tests/auth/authnull
t/plugin_tests/check_badrcptto t/plugin_tests/check_badrcptto
t/plugin_tests/greylisting t/plugin_tests/greylisting
t/plugin_tests/dnsbl t/plugin_tests/dnsbl
t/plugin_tests/ident/geoip
t/plugin_tests/rcpt_ok t/plugin_tests/rcpt_ok
t/qpsmtpd-address.t t/qpsmtpd-address.t
t/rset.t t/rset.t

View File

@ -15,6 +15,11 @@
# from one IP! # from one IP!
hosts_allow 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 <> # enable to accept MAIL FROM:/RCPT TO: addresses without surrounding <>
dont_require_anglebrackets dont_require_anglebrackets

View File

@ -7,27 +7,48 @@ do a lookup on incoming connections and record the country of origin.
Thats all it does. Thats all it does.
It logs the 2 char country code to note 'geoip_country'. It logs the 2 char country code to connection note I<geoip_country>.
It logs the country name to the connection note 'geoip_country_name'. It logs the country name to the connection note I<geoip_country_name>.
Other plugins can use that info to do things to the connection, like Other plugins can use that info to do things to the connection, like
reject or greylist. reject or greylist.
=cut =cut
use Geo::IP; use strict;
use warnings;
sub hook_connect { use Qpsmtpd::Constants;
my ($self) = @_; #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 $remote_ip = $self->qp->connection->remote_ip;
my $c_code = $geoip->country_code_by_addr( $remote_ip ) my $c_code = $geoip->country_code_by_addr( $remote_ip ) or do {
or return DECLINED; # if this fails, so too will name $self->log( LOGINFO, "fail: no results" );
my $c_name = $geoip->country_name_by_addr( $remote_ip ); 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); $self->qp->connection->notes('geoip_country', $c_code);
my $message = $c_code; my $message = $c_code;

View File

@ -1,5 +1,10 @@
#!perl -w #!perl -w
use strict;
use warnings;
use Qpsmtpd::Constants;
sub register_tests { sub register_tests {
my $self = shift; my $self = shift;
@ -17,7 +22,7 @@ sub test_auth_vpopmail {
my $self = shift; my $self = shift;
if ( ! $self->test_vpopmail_module ) { if ( ! $self->test_vpopmail_module ) {
$self->log(LOGERROR, "vpopmail plugin not configured" ); warn "vpopmail plugin not configured\n";
foreach ( 0..2) { ok( 1, "test_auth_vpopmail, skipped") }; foreach ( 0..2) { ok( 1, "test_auth_vpopmail, skipped") };
return; return;
}; };

View File

@ -0,0 +1,29 @@
#!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";
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");
};