line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mail::MtPolicyd::Plugin::Condition; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
2276
|
use Moose; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
16
|
|
4
|
2
|
|
|
2
|
|
12956
|
use namespace::autoclean; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
21
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '1.23'; # VERSION |
7
|
|
|
|
|
|
|
# ABSTRACT: mtpolicyd plugin for conditions based on session values |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
extends 'Mail::MtPolicyd::Plugin'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
with 'Mail::MtPolicyd::Plugin::Role::Scoring'; |
12
|
|
|
|
|
|
|
with 'Mail::MtPolicyd::Plugin::Role::UserConfig' => { |
13
|
|
|
|
|
|
|
'uc_attributes' => [ 'score', 'action' ], |
14
|
|
|
|
|
|
|
}; |
15
|
|
|
|
|
|
|
with 'Mail::MtPolicyd::Plugin::Role::PluginChain'; |
16
|
|
|
|
|
|
|
|
17
|
2
|
|
|
2
|
|
287
|
use Mail::MtPolicyd::Plugin::Result; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
910
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has 'key' => ( is => 'ro', isa => 'Str', required => 1 ); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has 'score' => ( is => 'rw', isa => 'Maybe[Num]' ); |
23
|
|
|
|
|
|
|
has 'action' => ( is => 'rw', isa => 'Maybe[Str]' ); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has 'match' => ( is => 'ro', isa => 'Maybe[Str]' ); |
26
|
|
|
|
|
|
|
has 're_match' => ( is => 'ro', isa => 'Maybe[Str]' ); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has 'gt_match' => ( is => 'ro', isa => 'Maybe[Num]' ); |
29
|
|
|
|
|
|
|
has 'lt_match' => ( is => 'ro', isa => 'Maybe[Num]' ); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub _match { |
32
|
3
|
|
|
3
|
|
7
|
my ( $self, $value ) = @_; |
33
|
|
|
|
|
|
|
|
34
|
3
|
100
|
66
|
|
|
117
|
if( defined $self->match && |
35
|
|
|
|
|
|
|
$value eq $self->match ) { |
36
|
2
|
|
|
|
|
8
|
return 1; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
1
|
|
|
|
|
42
|
my $regex = $self->re_match; |
40
|
1
|
50
|
33
|
|
|
5
|
if( defined $regex && $value =~ m/$regex/ ) { |
41
|
0
|
|
|
|
|
0
|
return 1; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
1
|
50
|
33
|
|
|
41
|
if( defined $self->lt_match && |
45
|
|
|
|
|
|
|
$value < $self->lt_match ) { |
46
|
0
|
|
|
|
|
0
|
return 1; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
1
|
50
|
33
|
|
|
40
|
if( defined $self->gt_match && |
50
|
|
|
|
|
|
|
$value > $self->gt_match ) { |
51
|
0
|
|
|
|
|
0
|
return 1; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
1
|
|
|
|
|
5
|
return 0; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub run { |
58
|
4
|
|
|
4
|
1
|
2274
|
my ( $self, $r ) = @_; |
59
|
4
|
|
|
|
|
173
|
my $key = $self->key; |
60
|
4
|
|
|
|
|
151
|
my $session = $r->session; |
61
|
|
|
|
|
|
|
|
62
|
4
|
|
|
|
|
9
|
my $value = $session->{$key}; |
63
|
4
|
100
|
|
|
|
12
|
if( ! defined $value ) { |
64
|
1
|
|
|
|
|
5
|
return; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
3
|
100
|
|
|
|
9
|
if( $self->_match($value) ) { |
68
|
2
|
|
|
|
|
18
|
$self->log($r, $key.' matched '.$value); |
69
|
2
|
|
|
|
|
13
|
my $score = $self->get_uc($session, 'score'); |
70
|
2
|
50
|
|
|
|
7
|
if( defined $score ) { |
71
|
0
|
|
|
|
|
0
|
$self->add_score($r, $self->name => $score); |
72
|
|
|
|
|
|
|
} |
73
|
2
|
|
|
|
|
9
|
my $action = $self->get_uc($session, 'action'); |
74
|
2
|
100
|
|
|
|
7
|
if( defined $action ) { |
75
|
1
|
|
|
|
|
60
|
return Mail::MtPolicyd::Plugin::Result->new( |
76
|
|
|
|
|
|
|
action => $action, |
77
|
|
|
|
|
|
|
abort => 1, |
78
|
|
|
|
|
|
|
); |
79
|
|
|
|
|
|
|
} |
80
|
1
|
50
|
|
|
|
46
|
if( defined $self->chain ) { |
81
|
1
|
|
|
|
|
42
|
my $chain_result = $self->chain->run( $r ); |
82
|
1
|
|
|
|
|
2
|
return( @{$chain_result->plugin_results} ); |
|
1
|
|
|
|
|
48
|
|
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
1
|
|
|
|
|
6
|
return; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
__END__ |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=pod |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=encoding UTF-8 |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head1 NAME |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Mail::MtPolicyd::Plugin::Condition - mtpolicyd plugin for conditions based on session values |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 VERSION |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
version 1.23 |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 DESCRIPTION |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Will return an action, score or execute futher plugins if the specified condition matched. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 PARAMETERS |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=over |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=item key (required) |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
The name of the variable within the session to check. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=back |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
At least one of the following parameters should be given or your condition will |
122
|
|
|
|
|
|
|
never match: |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=over |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=item match (default: empty) |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Simple string equal match. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=item re_match (default: empty) |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Match content of the session variable against an regex. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item lt_match (default: empty) |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Match if numerical less than. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=item gt_match (default: empty) |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Match if numerical greater than. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=back |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Finally an action must be specified. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
First the score will be applied the the action will be executed |
147
|
|
|
|
|
|
|
or if specified additional plugins will be executed. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=over |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=item action (default: empty) |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
The action to return when the condition matched. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item score (default: empty) |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
The score to add if the condition matched. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item Plugin (default: empty) |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Execute this plugins when the condition matched. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=back |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 EXAMPLE: use of postfix policy_context |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
The policy_context of postfix could be used to trigger checks |
168
|
|
|
|
|
|
|
in mtpolicyd. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
To activate additional checks in mtpolicyd from within postfix use may |
171
|
|
|
|
|
|
|
use a configuration in postfix main.cf like: |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# check, no additional checks |
174
|
|
|
|
|
|
|
check_policy_service inet:localhost:12345 |
175
|
|
|
|
|
|
|
... |
176
|
|
|
|
|
|
|
# check with additional checks! |
177
|
|
|
|
|
|
|
check_policy_service { inet:localhost:12345, policy_context=strict_checks } |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
In mtpolicyd.conf: |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
<Plugin if-strict-checks> |
182
|
|
|
|
|
|
|
module = "Condition" |
183
|
|
|
|
|
|
|
key = "policy_context" |
184
|
|
|
|
|
|
|
match = "strict_checks" |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
<Plugin strict-check> |
187
|
|
|
|
|
|
|
# ... |
188
|
|
|
|
|
|
|
</Plugin> |
189
|
|
|
|
|
|
|
# more checks ... |
190
|
|
|
|
|
|
|
</Plugin> |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
The policy_context feature will be available in postfix 3.1 and later. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
If you need completely different checks consider using the vhost_by_policy_context |
195
|
|
|
|
|
|
|
(L<mtpolicyd>) option with different virtual hosts. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head1 EXAMPLE: execute postgrey action in postfix |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
If the session variable "greylisting" is "on" return the postfix action "postgrey": |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
<Plugin trigger-greylisting> |
202
|
|
|
|
|
|
|
module = "Condition" |
203
|
|
|
|
|
|
|
key = "greylisting" |
204
|
|
|
|
|
|
|
match = "on" |
205
|
|
|
|
|
|
|
action = "postgrey" |
206
|
|
|
|
|
|
|
</Plugin> |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
The variable may be set by a UserConfig module like SqlUserConfig. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
The postgrey action in postfix may look like: |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
smtpd_restriction_classes = postgrey |
213
|
|
|
|
|
|
|
postgrey = check_policy_service inet:127.0.0.1:11023 |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head1 AUTHOR |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
Markus Benning <ich@markusbenning.de> |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>. |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
This is free software, licensed under: |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
The GNU General Public License, Version 2, June 1991 |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=cut |