line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mail::MtPolicyd::Plugin::GeoIPAction; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1107
|
use Moose; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
4
|
1
|
|
|
1
|
|
4144
|
use namespace::autoclean; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '2.01'; # VERSION |
7
|
|
|
|
|
|
|
# ABSTRACT: mtpolicyd plugin for checking geo information of an ip |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
extends 'Mail::MtPolicyd::Plugin'; |
11
|
|
|
|
|
|
|
with 'Mail::MtPolicyd::Plugin::Role::Scoring'; |
12
|
|
|
|
|
|
|
with 'Mail::MtPolicyd::Plugin::Role::UserConfig' => { |
13
|
|
|
|
|
|
|
'uc_attributes' => [ 'enabled', 'mode' ], |
14
|
|
|
|
|
|
|
}; |
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
1
|
|
99
|
use Mail::MtPolicyd::Plugin::Result; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
464
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has 'result_from' => ( is => 'rw', isa => 'Str', required => 1 ); |
19
|
|
|
|
|
|
|
has 'enabled' => ( is => 'rw', isa => 'Str', default => 'on' ); |
20
|
|
|
|
|
|
|
has 'mode' => ( is => 'rw', isa => 'Str', default => 'reject' ); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has 'country_codes' => ( is => 'rw', isa => 'Str', required => 1 ); |
23
|
|
|
|
|
|
|
has '_country_codes' => ( |
24
|
|
|
|
|
|
|
is => 'ro', isa => 'ArrayRef', lazy => 1, |
25
|
|
|
|
|
|
|
default => sub { |
26
|
|
|
|
|
|
|
my $self = shift; |
27
|
|
|
|
|
|
|
return [ split(/\s*,\s*/, $self->country_codes) ]; |
28
|
|
|
|
|
|
|
}, |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub is_in_country_codes { |
32
|
0
|
|
|
0
|
0
|
|
my ( $self, $cc ) = @_; |
33
|
0
|
0
|
|
|
|
|
if ( grep { $_ eq $cc } @{$self->_country_codes} ) { |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
return(1); |
35
|
|
|
|
|
|
|
} |
36
|
0
|
|
|
|
|
|
return(0); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
has 'reject_message' => ( |
40
|
|
|
|
|
|
|
is => 'ro', isa => 'Str', default => 'delivery from %CC% (%IP%) rejected', |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
has 'score' => ( is => 'rw', isa => 'Maybe[Num]' ); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub run { |
46
|
0
|
|
|
0
|
1
|
|
my ( $self, $r ) = @_; |
47
|
0
|
|
|
|
|
|
my $ip = $r->attr('client_address'); |
48
|
0
|
|
|
|
|
|
my $session = $r->session; |
49
|
0
|
|
|
|
|
|
my $mode = $self->get_uc( $session, 'mode' ); |
50
|
0
|
|
|
|
|
|
my $enabled = $self->get_uc( $session, 'enabled' ); |
51
|
|
|
|
|
|
|
|
52
|
0
|
0
|
|
|
|
|
if( $enabled eq 'off' ) { |
53
|
0
|
|
|
|
|
|
return; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
my $result_key = 'geoip-'.$self->result_from.'-result'; |
57
|
0
|
0
|
|
|
|
|
if( ! defined $session->{$result_key} ) { |
58
|
0
|
|
|
|
|
|
$self->log( $r, 'no GeoIP check result for '.$self->name.' found!'); |
59
|
0
|
|
|
|
|
|
return; |
60
|
|
|
|
|
|
|
} |
61
|
0
|
|
|
|
|
|
my ( $country_code ) = @{$session->{$result_key}}; |
|
0
|
|
|
|
|
|
|
62
|
0
|
0
|
|
|
|
|
if( ! defined $country_code ) { |
63
|
0
|
|
|
|
|
|
return; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
0
|
0
|
|
|
|
|
if( ! $self->is_in_country_codes( $country_code ) ) { |
67
|
0
|
|
|
|
|
|
$self->log( $r, 'country_code '.$country_code.' of IP not in country_code list'.$self->name); |
68
|
0
|
|
|
|
|
|
return; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
$self->log( $r, 'country code '.$country_code.' on list'.$self->name ); |
72
|
0
|
0
|
0
|
|
|
|
if( defined $self->score && ! $r->is_already_done('geoip-'.$self->name.'-score') ) { |
73
|
0
|
|
|
|
|
|
$self->add_score($r, $self->name => $self->score); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
0
|
0
|
|
|
|
|
if( $mode eq 'reject' ) { |
77
|
0
|
|
|
|
|
|
return Mail::MtPolicyd::Plugin::Result->new( |
78
|
|
|
|
|
|
|
action => $self->_get_reject_action($ip, $country_code ), |
79
|
|
|
|
|
|
|
abort => 1, |
80
|
|
|
|
|
|
|
); |
81
|
|
|
|
|
|
|
} |
82
|
0
|
0
|
0
|
|
|
|
if( $mode eq 'accept' || $mode eq 'dunno' ) { |
83
|
0
|
|
|
|
|
|
return Mail::MtPolicyd::Plugin::Result->new_dunno; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
return; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub _get_reject_action { |
90
|
0
|
|
|
0
|
|
|
my ( $self, $ip, $cc ) = @_; |
91
|
0
|
|
|
|
|
|
my $message = $self->reject_message; |
92
|
0
|
|
|
|
|
|
$message =~ s/%IP%/$ip/; |
93
|
0
|
|
|
|
|
|
$message =~ s/%CC%/$cc/; |
94
|
0
|
|
|
|
|
|
return('reject '.$message); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
1; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
__END__ |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=pod |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=encoding UTF-8 |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 NAME |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Mail::MtPolicyd::Plugin::GeoIPAction - mtpolicyd plugin for checking geo information of an ip |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 VERSION |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
version 2.01 |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 DESCRIPTION |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
This plugin will execute an action or score based on a previous lookup |
118
|
|
|
|
|
|
|
done with GeoIPLookup plugin. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 PARAMETERS |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=over |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item result_from (required) |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Take the GeoIP information from the result of this plugin. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
The plugin in must be executed before this plugin. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=item (uc_)enabled (default: on) |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Enable/disable this plugin. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item country_codes (required) |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
A comma separated list of 2 letter country codes to match. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=item (uc_)mode (default: reject) |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
If set to 'passive' no action will be returned. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item reject_message (default: 'delivery from %CC% (%IP%) rejected) |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Could be used to specify an custom reject message. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item score (default: empty) |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
A score to apply to the message. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=back |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 AUTHOR |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Markus Benning <ich@markusbenning.de> |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
This is free software, licensed under: |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
The GNU General Public License, Version 2, June 1991 |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=cut |