Advertisement
fakessh

add header geo and antiabuse for amavisd

May 3rd, 2011
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.21 KB | None | 0 0
  1. package Amavis::Custom;
  2. use strict;
  3. use re 'taint';
  4. use warnings;
  5. use warnings FATAL => qw(utf8 void);
  6. no warnings qw(uninitialized redefine);
  7.  
  8. BEGIN {
  9.   import Amavis::Conf qw(:platform :confvars c cr ca);
  10.   import Amavis::Util qw(do_log untaint min max);
  11.   import Amavis::rfc2821_2822_Tools qw(split_address parse_received);
  12. }
  13.  
  14. sub new {
  15.   my($class,$conn,$msginfo) = @_;
  16.   my($self) = bless {}, $class;
  17.   checks_geo($self,$conn,$msginfo);
  18.   $self;
  19. }
  20.  
  21. use Geo::IP;
  22. sub checks_geo {
  23.   my($self,$conn,$msginfo) = @_;
  24.   if (!exists $self->{geoip}) {  # first time only in a child process
  25.     my $geo_file = "/usr/share/GeoIP/GeoLiteCity.dat";
  26.     $self->{geoip} = Geo::IP->open($geo_file, GEOIP_STANDARD);
  27.     if (!$self->{geoip}) {
  28.       do_log(0, "GeoIP: failed to open %s", $geo_file);
  29.     } else {
  30.       $self->{geoip}->set_charset(GEOIP_CHARSET_UTF8);
  31.     }
  32.   }
  33.   my($sender) = $msginfo->sender;
  34.   my($localpart,$domain) = split_address($sender);
  35.  
  36.   if ($self->{geoip}) {
  37.     my $last_received_ip =
  38.       Amavis::UnmangleSender::parse_ip_address_from_received($msginfo);
  39.     my($cl_ip) = $msginfo->client_addr;
  40.     if (defined $last_received_ip && $last_received_ip ne '') {
  41.       my($country_name,$region_name,$city);
  42.       my $record = $self->{geoip}->record_by_addr($last_received_ip);
  43.       if (!$record) {
  44.         do_log(2, "GeoIP: no record for %s", $last_received_ip);
  45.       } else {
  46.         $country_name = $record->country_name;
  47.         $region_name = $record->region_name;
  48.         $city = $record->city;
  49.       }
  50.           if (defined $cl_ip && $cl_ip ne '') {
  51.       my($country_name_cl_ip,$region_name_cl_ip,$city_cl_ip);
  52.       my $record_cl_ip = $self->{geoip}->record_by_addr($cl_ip);
  53.       if (!$record_cl_ip) {
  54.         do_log(2, "GeoIP: no record for %s", $cl_ip);
  55.       } else {
  56.         $country_name_cl_ip = $record_cl_ip->country_name;
  57.         $region_name_cl_ip = $record_cl_ip->region_name;
  58.         $city_cl_ip = $record_cl_ip->city;
  59.       }
  60.  
  61.       no strict;
  62.       do_log(2, "GeoIP: %-15s %s %s, %s, %s", $last_received_ip,
  63.                 $msginfo->is_in_contents_category(CC_SPAM) ? 'SPAM' : '    ',
  64.                 map(defined $_ && $_ ne '' ? $_ : "-",
  65.                     $country_name, $region_name, $city));
  66.       do_log(2, "GeoIP: %-15s %s %s, %s, %s", $last_received_ip,
  67.                 $msginfo->is_in_contents_category(CC_SPAM) ? 'SPAM' : '    ',
  68.                 map(defined $_ && $_ ne '' ? $_ : "-",
  69.                     $country_name_cl_ip, $region_name_cl_ip, $city_ip));
  70.       my $hdr_edits = $msginfo->header_edits;
  71.       $hdr_edits->add_header('X-Amavis-GeoIP', "$country_name $region_name $city");
  72.       $hdr_edits->add_header('X-Amavis-GeoIP', "$country_name_cl_ip $region_name_cl_ip $city_cl_ip");
  73.       $hdr_edits->add_header('X-Header-AntiAbuse', "report abuse to postmaster\@fakessh.eu");
  74.       $hdr_edits->add_header('X-Header-AntiAbuse', "sender $sender $localpart $domain");
  75.       $hdr_edits->add_header('X-Header-AntiAbuse', "client addr $cl_ip");
  76.       $hdr_edits->add_header('X-Header-AntiAbuse', "client addr $last_received_ip");
  77.       $hdr_edits->add_header('X-Header-AntiAbuse', "primary hostname r13151.ovh.net");
  78.       use strict;
  79.     }
  80.   }
  81. }
  82. }
  83.  
  84. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement