| 1 | package Mail::MtPolicyd::Plugin::Condition; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '2.01'; # VERSION | |||||||||||||||||||||||||||
| 7 | # ABSTRACT: mtpolicyd plugin for conditions based on session values | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | extends 'Mail::MtPolicyd::Plugin'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | with 'Mail::MtPolicyd::Plugin::Role::Scoring'; | |||||||||||||||||||||||||||
| 12 | 2 | with 'Mail::MtPolicyd::Plugin::Role::UserConfig' => { | |||||||||||||||||||||||||||
| 13 | 'uc_attributes' => [ 'score', 'action' ], | ||||||||||||||||||||||||||||
| 14 | }; | ||||||||||||||||||||||||||||
| 15 | 2 | with 'Mail::MtPolicyd::Plugin::Role::PluginChain'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use Mail::MtPolicyd::Plugin::Result; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | has 'key' => ( is => 'rw', isa => 'Str', required => 1 ); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | has 'score' => ( is => 'rw', isa => 'Maybe[Num]' ); | |||||||||||||||||||||||||||
| 23 | 2 | has 'action' => ( is => 'rw', isa => 'Maybe[Str]' ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | has 'match' => ( is => 'rw', isa => 'Maybe[Str]' ); | |||||||||||||||||||||||||||
| 26 | 2 | has 're_match' => ( is => 'rw', isa => 'Maybe[Str]' ); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 2 | has 'gt_match' => ( is => 'rw', isa => 'Maybe[Num]' ); | |||||||||||||||||||||||||||
| 29 | 2 | has 'lt_match' => ( is => 'rw', isa => 'Maybe[Num]' ); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | has 'invert' => ( is => 'rw', isa => 'Bool', default => 0 ); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | sub _match { | ||||||||||||||||||||||||||||
| 34 | 5 | ▶ | my ( $self, $value ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_match: called CC=9 SCAR=27.1
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 5 | ▶ | if( defined $self->match && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->match and $value eq $self->match
MC/DC50%1/2
defined $self->match and $value eq $self->match
defined $self->match
$value eq $self->match
Truth table66%2/3
A and B
A= defined $self->matchB= $value eq $self->match
| |||||||||||||||||||||||||||||
| 37 | $value eq $self->match ) { | ||||||||||||||||||||||||||||
| 38 | 4 | return 1; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | my $regex = $self->re_match; | |||||||||||||||||||||||||||
| 42 | 1 | ▶ | if( defined $regex && $value =~ m/$regex/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $regex and $value =~ /$regex/
MC/DC0%0/2
defined $regex and $value =~ /$regex/
defined $regex
$value =~ /$regex/
Truth table33%1/3
A and B
A= defined $regexB= $value =~ /$regex/
| |||||||||||||||||||||||||||||
| 43 | 0 | return 1; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | ▶ | if( defined $self->lt_match && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->lt_match and $value < $self->lt_match
MC/DC0%0/2
defined $self->lt_match and $value < $self->lt_match
defined $self->lt_match
$value < $self->lt_match
Truth table33%1/3
A and B
A= defined $self->lt_matchB= $value < $self->lt_match
| |||||||||||||||||||||||||||||
| 47 | $value < $self->lt_match ) { | ||||||||||||||||||||||||||||
| 48 | 0 | return 1; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | if( defined $self->gt_match && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->gt_match and $value > $self->gt_match
MC/DC0%0/2
defined $self->gt_match and $value > $self->gt_match
defined $self->gt_match
$value > $self->gt_match
Truth table33%1/3
A and B
A= defined $self->gt_matchB= $value > $self->gt_match
| |||||||||||||||||||||||||||||
| 52 | $value > $self->gt_match ) { | ||||||||||||||||||||||||||||
| 53 | 0 | return 1; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 1 | return 0; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | sub run { | ||||||||||||||||||||||||||||
| 60 | 6 | ▶ | my ( $self, $r ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 6 | my $key = $self->key; | |||||||||||||||||||||||||||
| 62 | 6 | my $session = $r->session; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 6 | my $value = $r->get( $key ); | |||||||||||||||||||||||||||
| 65 | 6 | ▶ | if( ! defined $value ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 1 | return; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 5 | my $matched = $self->_match($value); | |||||||||||||||||||||||||||
| 70 | 5 | ▶ | if( $self->invert ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 1 | $matched = ! $matched; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 5 | ▶ | if( $matched ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 3 | $self->log($r, $key.' matched '.$value); | |||||||||||||||||||||||||||
| 76 | 3 | my $score = $self->get_uc($session, 'score'); | |||||||||||||||||||||||||||
| 77 | 3 | ▶ | if( defined $score ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | $self->add_score($r, $self->name => $score); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 3 | my $action = $self->get_uc($session, 'action'); | |||||||||||||||||||||||||||
| 81 | 3 | ▶ | if( defined $action ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 2 | return Mail::MtPolicyd::Plugin::Result->new( | |||||||||||||||||||||||||||
| 83 | action => $action, | ||||||||||||||||||||||||||||
| 84 | abort => 1, | ||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 1 | ▶ | if( defined $self->chain ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 1 | my $chain_result = $self->chain->run( $r ); | |||||||||||||||||||||||||||
| 89 | 1 | return( @{$chain_result->plugin_results} ); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 2 | return; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | 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.01 | ||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||