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