Skip to content
This repository
Browse code

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
  • Loading branch information...
commit 2dcd34467ee212ecabe8b479cae6cd0580de2f2a 1 parent d407f34
Matt Simerson authored May 22, 2012
5  config.sample/plugins
@@ -15,6 +15,11 @@
15 15
 # from one IP!
16 16
 hosts_allow
17 17
 
  18
+# information plugins
  19
+ident/geoip
  20
+#ident/p0f /tmp/.p0f_socket version 3
  21
+#connection_time
  22
+
18 23
 # enable to accept MAIL FROM:/RCPT TO: addresses without surrounding <>
19 24
 dont_require_anglebrackets
20 25
 
39  plugins/ident/geoip
@@ -7,27 +7,48 @@ do a lookup on incoming connections and record the country of origin.
7 7
 
8 8
 Thats all it does.
9 9
 
10  
-It logs the 2 char country code to note 'geoip_country'.
11  
-It logs the country name to the connection note 'geoip_country_name'.
  10
+It logs the 2 char country code to connection note I<geoip_country>.
  11
+It logs the country name to the connection note I<geoip_country_name>.
12 12
 
13 13
 Other plugins can use that info to do things to the connection, like
14 14
 reject or greylist.
15 15
 
16 16
 =cut
17 17
 
18  
-use Geo::IP;
  18
+use strict;
  19
+use warnings;
19 20
 
20  
-sub hook_connect {
21  
-    my ($self) = @_;
  21
+use Qpsmtpd::Constants;
  22
+#use Geo::IP;  # eval'ed in register()
22 23
 
23  
-    my $geoip = Geo::IP->new(GEOIP_STANDARD);
  24
+sub register {
  25
+    my $self = shift;
  26
+    eval 'use Geo::IP';
  27
+    if ( $@ ) {
  28
+        warn "could not load Geo::IP";
  29
+        $self->log( LOGERROR, "could not load Geo::IP" );
  30
+        return;
  31
+    };
  32
+
  33
+    $self->register_hook( 'connect', 'connect_handler' );
  34
+};
  35
+
  36
+sub connect_handler {
  37
+    my $self = shift;
  38
+
  39
+    my $geoip = Geo::IP->new();
24 40
     my $remote_ip = $self->qp->connection->remote_ip;
25 41
 
26  
-    my $c_code = $geoip->country_code_by_addr( $remote_ip )
27  
-        or return DECLINED;  # if this fails, so too will name
  42
+    my $c_code = $geoip->country_code_by_addr( $remote_ip ) or do {
  43
+        $self->log( LOGINFO, "fail: no results" );
  44
+        return DECLINED;
  45
+    };
  46
+
28 47
     my $c_name = $geoip->country_name_by_addr( $remote_ip );
  48
+    if ( $c_name ) {
  49
+        $self->qp->connection->notes('geoip_country_name', $c_name);
  50
+    };
29 51
 
30  
-    $self->qp->connection->notes('geoip_country_name', $c_name);
31 52
     $self->qp->connection->notes('geoip_country', $c_code);
32 53
 
33 54
     my $message  = $c_code;
30  t/plugin_tests/ident/geoip
... ...
@@ -0,0 +1,30 @@
  1
+#!perl -w
  2
+
  3
+use strict;
  4
+use warnings;
  5
+
  6
+use Qpsmtpd::Constants;
  7
+
  8
+sub register_tests {
  9
+    my $self = shift;
  10
+
  11
+    eval 'use Geo::IP';
  12
+    if ( $@ ) {
  13
+        warn "could not load Geo::IP\n";
  14
+        $self->log(LOGERROR, "could not load Geo::IP");
  15
+        return;
  16
+    };
  17
+
  18
+    $self->register_test('test_geoip_lookup', 2);
  19
+};
  20
+
  21
+sub test_geoip_lookup {
  22
+    my $self = shift;
  23
+
  24
+    $self->qp->connection->remote_ip('24.24.24.24');
  25
+    cmp_ok( $self->connect_handler(), '==', DECLINED, "exit code");
  26
+
  27
+    cmp_ok( $self->qp->connection->notes('geoip_country'), 'eq', 'US', "note");
  28
+};
  29
+
  30
+

0 notes on commit 2dcd344

Please sign in to comment.
Something went wrong with that request. Please try again.