| 1 | package Mail::MtPolicyd::Plugin::DBL; | ||||||||||||||||||||||||||||
| 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 checking helo,sender domain,rdns against an DBL | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | extends 'Mail::MtPolicyd::Plugin'; | |||||||||||||||||||||||||||
| 10 | 2 | with 'Mail::MtPolicyd::Plugin::Role::Scoring'; | |||||||||||||||||||||||||||
| 11 | 2 | with 'Mail::MtPolicyd::Plugin::Role::UserConfig' => { | |||||||||||||||||||||||||||
| 12 | 'uc_attributes' => [ 'enabled', 'sender_mode', 'helo_name_mode', | ||||||||||||||||||||||||||||
| 13 | 'reverse_client_name_mode' ], | ||||||||||||||||||||||||||||
| 14 | }; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Mail::MtPolicyd::Plugin::Result; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use Mail::RBL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | has 'domain' => ( is => 'rw', isa => 'Str', required => 1 ); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | has 'enabled' => ( is => 'rw', isa => 'Str', default => 'on' ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | has 'sender_mode' => ( is => 'rw', isa => 'Str', default => 'reject' ); | |||||||||||||||||||||||||||
| 26 | 2 | has 'sender_score' => ( is => 'rw', isa => 'Maybe[Num]', default => 5 ); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 2 | has 'reverse_client_name_mode' => ( is => 'rw', isa => 'Str', default => 'reject' ); | |||||||||||||||||||||||||||
| 29 | 2 | has 'reverse_client_name_score' => ( is => 'rw', isa => 'Maybe[Num]', default => 2.5 ); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | has 'helo_name_mode' => ( is => 'rw', isa => 'Str', default => 'passive' ); | |||||||||||||||||||||||||||
| 32 | 2 | has 'helo_name_score' => ( is => 'rw', isa => 'Maybe[Num]', default => 1 ); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | has 'reject_message' => ( is => 'rw', isa => 'Str', | |||||||||||||||||||||||||||
| 35 | default => '%CHECK% rejected (%HOSTNAME%%INFO%)' ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | has '_rbl' => ( | ||||||||||||||||||||||||||||
| 38 | is => 'ro', isa => 'Mail::RBL', lazy => 1, | ||||||||||||||||||||||||||||
| 39 | default => sub { | ||||||||||||||||||||||||||||
| 40 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 1 | Mail::RBL->new($self->domain) | |||||||||||||||||||||||||||
| 42 | }, | ||||||||||||||||||||||||||||
| 43 | 2 | ); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub run { | ||||||||||||||||||||||||||||
| 46 | 5 | ▶ | my ( $self, $r ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=9 SCAR=22.3
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 5 | my $session = $r->session; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 5 | my $enabled = $self->get_uc( $session, 'enabled' ); | |||||||||||||||||||||||||||
| 50 | 5 | ▶ | if( $enabled eq 'off' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 1 | return; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 4 | foreach my $check ( 'sender', 'reverse_client_name', 'helo_name') { | |||||||||||||||||||||||||||
| 55 | 9 | my $hostname = $self->_get_hostname($r, $check); | |||||||||||||||||||||||||||
| 56 | 9 | ▶ | if( ! defined $hostname ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | next; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | my ( $ip_result, $info ) = $r->do_cached( $self->name.'-'.$check.'-result', | ||||||||||||||||||||||||||||
| 61 | 9 | ▶ | sub { $self->_rbl->check_rhsbl( $hostname ) } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 9 | ▶ | if( ! defined $ip_result ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 4 | $self->log($r, 'domain '.$hostname.' not on '.$self->domain.' blacklist'); | |||||||||||||||||||||||||||
| 64 | 4 | next; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 5 | ▶ | $self->log($r, 'domain '.$hostname.' is on '.$self->domain.' blacklist'. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | ( defined $info ? " ($info)" : '' ) ); | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 5 | my $score_attr = $check.'_score'; | |||||||||||||||||||||||||||
| 71 | 5 | ▶ | if( defined $self->$score_attr && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->$score_attr and not $r->is_already_done($self->name . '-' . $check . '-score')
MC/DC0%0/2
defined $self->$score_attr and not $r->is_already_done($self->name . '-' . $check . '-score')
defined $self->$score_attr
not $r->is_already_done($self->name . '-' . $check . '-score')
Truth table33%1/3
A and not(B)
A= defined $self->$score_attrB= not $r->is_already_done($self->name . '-' . $check . '-score')
| |||||||||||||||||||||||||||||
| 72 | ! $r->is_already_done($self->name.'-'.$check.'-score') ) { | ||||||||||||||||||||||||||||
| 73 | 5 | $self->add_score($r, $self->name.'-'.$check => $self->$score_attr ); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 5 | my $mode = $self->get_uc( $session, $check.'_mode' ); | |||||||||||||||||||||||||||
| 77 | 5 | ▶ | if( $mode eq 'reject' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 2 | return Mail::MtPolicyd::Plugin::Result->new( | |||||||||||||||||||||||||||
| 79 | action => $self->_get_reject_action($check, $hostname, $info), | ||||||||||||||||||||||||||||
| 80 | abort => 1, | ||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 2 | return; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub _get_hostname { | ||||||||||||||||||||||||||||
| 89 | 9 | ▶ | my ( $self, $r, $field ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_hostname: called CC=8 SCAR=24.9
| |||||||||||||||||||||||||||||
| 90 | 9 | my $value = $r->attr($field); | |||||||||||||||||||||||||||
| 91 | 9 | ▶ | if( ! defined $value ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | die($field.' not defined in request!'); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | # skip unknown and empty fields | ||||||||||||||||||||||||||||
| 95 | 9 | ▶ | if( $value eq 'unknown' || $value eq '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$value eq "unknown" or $value eq ""
MC/DC0%0/2
$value eq "unknown" or $value eq ""
$value eq "unknown"
$value eq ""
Truth table33%1/3
A or B
A= $value eq "unknown"B= $value eq ""
| |||||||||||||||||||||||||||||
| 96 | 0 | return; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | # skip ip addresses | ||||||||||||||||||||||||||||
| 99 | 9 | ▶ | if( $value =~ m/^\d+\.\d+\.\d+\.\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | return; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | # skip ip6 addresses | ||||||||||||||||||||||||||||
| 103 | 9 | ▶ | if( $value =~ m/:/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | return; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | # skip unqualified hostnames | ||||||||||||||||||||||||||||
| 107 | 9 | ▶ | if( $value !~ m/\./) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | return; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 9 | ▶ | if( $field eq 'sender') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 4 | $value =~ s/^[^@]*@//; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 9 | return($value); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | sub _get_reject_action { | ||||||||||||||||||||||||||||
| 118 | 2 | ▶ | my ( $self, $check, $hostname, $info ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_reject_action: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 119 | 2 | my $msg = $self->reject_message; | |||||||||||||||||||||||||||
| 120 | 2 | $msg =~ s/%CHECK%/$check/; | |||||||||||||||||||||||||||
| 121 | 2 | $msg =~ s/%HOSTNAME%/$hostname/; | |||||||||||||||||||||||||||
| 122 | 2 | ▶ | if( defined $info ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 2 | $msg =~ s/%INFO%/, $info/; | |||||||||||||||||||||||||||
| 124 | } else { | ||||||||||||||||||||||||||||
| 125 | 0 | $msg =~ s/%INFO%//; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 2 | return 'reject '.$msg; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 2 | 1; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =pod | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 NAME | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | Mail::MtPolicyd::Plugin::DBL - mtpolicyd plugin for checking helo,sender domain,rdns against an DBL | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 VERSION | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | version 2.01 | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Will check the sender, helo and reverse_client_name against an domain black list. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head1 PARAMETERS | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =over | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =item domain (required) | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | The domain of the blacklist to query. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =item enabled (default: on) | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | Set to 'off' to disable plugin. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Possible values: on,off | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =item uc_enabled (default: empty) | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | If specified the give variable within the session will overwrite the value of 'enabled' if set. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =item (uc_)sender_mode (default: reject), (uc_)helo_name_mode (default: passive), (uc_)reverse_client_name_mode (default: reject) | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Should the plugin return an reject if the check matches (reject) or | ||||||||||||||||||||||||||||
| 174 | just add an score (passive). | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | Possible values: reject, passive | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =item sender_score (default: 5) | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =item helo_name_score (default: 1) | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =item reverse_client_name_score (default: 2.5) | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Add the given score if check matched. | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =item score_field (default: score) | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Name of the session variable the score is stored in. | ||||||||||||||||||||||||||||
| 189 | Could be used if multiple scores are needed. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =back | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head1 EXAMPLE | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | Only the sender and the reverse_client_name check will cause an | ||||||||||||||||||||||||||||
| 196 | action to be executed (mode). | ||||||||||||||||||||||||||||
| 197 | The helo check will only add an score. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | <Plugin sh_dbl> | ||||||||||||||||||||||||||||
| 200 | module = "RBL" | ||||||||||||||||||||||||||||
| 201 | #enabled = "on" | ||||||||||||||||||||||||||||
| 202 | uc_enabled = "spamhaus" | ||||||||||||||||||||||||||||
| 203 | domain="dbl.spamhaus.org" | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | # do not reject based on helo | ||||||||||||||||||||||||||||
| 206 | #helo_name_mode=passive | ||||||||||||||||||||||||||||
| 207 | #helo_name_score=1 | ||||||||||||||||||||||||||||
| 208 | #sender_mode=reject | ||||||||||||||||||||||||||||
| 209 | #sender_score=5 | ||||||||||||||||||||||||||||
| 210 | #reverse_client_name_mode=reject | ||||||||||||||||||||||||||||
| 211 | #reverse_client_name_score=2.5 | ||||||||||||||||||||||||||||
| 212 | </Plugin> | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | Markus Benning <ich@markusbenning.de> | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | The GNU General Public License, Version 2, June 1991 | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =cut | ||||||||||||||||||||||||||||