Authentication/Protocol/Milter.pm

filter: Statementstmt 80.9% 140 / 173 Branchbran 67.5% 73 / 108 Conditioncond 28.0% 7 / 25 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 100% 15 / 15 Podpod 100% 10 / 10 totaltotal 71.0% 245 / 345 CC 65 SCAR 42.1
CC
65
Cov
72%
milter_process_command
112.5
milter_process_connect
9.2
protocol_process_request
8.0
CRAP 67.5
« Authentication/Handler/DKIM.pm ↑ Summary Authentication/Net/ServerPatches.pm »
1 package Mail::Milter::Authentication::Protocol::Milter;
2 143 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 143 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 143 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 143 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Milter protocol handling
7 143 our $VERSION = '4.20261116'; # VERSION
8 143 ▶use Net::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 19 ▶sub register_metrics {
Subroutine register_metrics: called CC=1 SCAR=0.0
Pod documented
11 return {
12 19 'mail_processed_total' => 'Number of emails processed',
13 };
14 }
15  
16 33 ▶sub protocol_process_request {
Subroutine protocol_process_request: called CC=7 SCAR=20.7
Pod documented
17 33 my ( $self ) = @_;
18  
19 33 my $handler = $self->{'handler'}->{'_Handler'};
20 33 $handler->top_setup_callback();
21  
22 COMMAND:
23 33 while ( 1 ) {
24  
25 # Get packet length
26 923 ▶ my $length = unpack('N', $self->milter_read_block(4) ) || last;
Condition50%1/2
unpack('N', $self->milter_read_block(4)) || (last)
l!l
9230
MC/DC0%0/1
unpack('N', $self->milter_read_block(4)) || (last)
unpack('N', $self->milter_read_block(4))
Truth table50%1/2
A
A= unpack('N', $self->milter_read_block(4))
Aresult
11
00
27 923 ▶ $self->fatal("bad packet length $length") if ($length <= 0 || $length > 131072);
Branch: 1 branch
BranchTrueFalseTotal
if $length <= 0 or $length > 131072 0 923 923
Condition33%1/3
$length <= 0 or $length > 131072
l!l&&r!l&&!r
00923
MC/DC0%0/2
$length <= 0 or $length > 131072
$length <= 0 $length > 131072
Truth table33%1/3
A or B
A= $length <= 0B= $length > 131072
ABresult
1X1
011
000
28  
29 # Get command
30 923 ▶ my $command = $self->milter_read_block(1) || last;
Condition50%1/2
$self->milter_read_block(1) || (last)
l!l
9230
MC/DC0%0/1
$self->milter_read_block(1) || (last)
$self->milter_read_block(1)
Truth table50%1/2
A
A= $self->milter_read_block(1)
Aresult
11
00
31 923 $self->logdebug( "receive command $command" );
32  
33 # Get data
34 923 my $data = $self->milter_read_block($length - 1);
35 923 ▶ if ( ! defined ( $data ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 923 923
36 0 $self->fatal('EOF in stream');
37 }
38  
39 923 ▶ last COMMAND if $command eq SMFIC_QUIT;
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "Q" 33 890 923
40 890 $self->milter_process_command( $command, $data );
41  
42 }
43 }
44  
45 890 ▶sub milter_process_command {
Subroutine milter_process_command: called CC=46 SCAR=47.2
Pod documented
46 890 my ( $self, $command, $buffer ) = @_;
47 890 $self->logdebug ( "process command $command" );
48  
49 890 my $handler = $self->{'handler'}->{'_Handler'};
50  
51 890 my $returncode = SMFIS_CONTINUE;
52  
53 890 ▶ if ( $command eq SMFIC_CONNECT ) {
Branch: 13 branches
BranchTrueFalseTotal
if ($command eq 'C') { } 33 857 890
elsif ($command eq 'A') { } 33 824 857
elsif ($command eq 'B') { } 31 793 824
elsif ($command eq 'D') { } 196 597 793
elsif ($command eq 'E') { } 33 564 597
elsif ($command eq 'H') { } 33 531 564
elsif ($command eq 'L') { } 399 132 531
elsif ($command eq 'M') { } 33 99 132
elsif ($command eq 'N') { } 33 66 99
elsif ($command eq 'O') { } 33 33 66
elsif ($command eq 'R') { } 33 0 33
elsif ($command eq 'T') { } 0 0 0
elsif ($command eq 'U') { } 0 0 0
54 33 my ( $host, $ip ) = $self->milter_process_connect( $buffer );
55 33 $handler->remap_connect_callback( $host, $ip );
56 33 $returncode = $handler->top_connect_callback( $host, $handler->{'ip_object'} );
57 }
58 elsif ( $command eq SMFIC_ABORT ) {
59 33 $returncode = $handler->top_abort_callback();
60 }
61 elsif ( $command eq SMFIC_BODY ) {
62 31 $returncode = $handler->top_body_callback( $buffer );
63 }
64 elsif ( $command eq SMFIC_MACRO ) {
65 196 ▶ $self->fatal('SMFIC_MACRO: empty packet') unless ( $buffer =~ s/^(.)// );
Branch: 1 branch
BranchTrueFalseTotal
unless $buffer =~ s/^(.)//u 0 196 196
66 196 my $code = $1;
67 196 my $data = $self->milter_split_buffer( $buffer );
68 196 ▶ push ( @$data, q{} ) if (( @$data & 1 ) != 0 ); # pad last entry with empty string if odd number
Branch: 1 branch
BranchTrueFalseTotal
if (@$data & 1) != 0 1 195 196
69 196 my %datahash = @$data;
70 196 foreach my $key ( keys %datahash ) {
71 328 $handler->set_symbol( $code, $key, $datahash{$key} );
72 }
73 196 undef $returncode;
74 }
75 elsif ( $command eq SMFIC_BODYEOB ) {
76 33 $returncode = $handler->top_eom_callback();
77 33 ▶ if ( $returncode == SMFIS_CONTINUE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returncode == 100) 31 2 33
78 31 $handler->metric_count( 'mail_processed_total', { 'result' => 'accepted' } );
79 }
80 }
81 elsif ( $command eq SMFIC_HELO ) {
82 33 my $helo = $self->milter_split_buffer( $buffer );
83 33 $handler->remap_helo_callback( @$helo );
84 33 $returncode = $handler->top_helo_callback( $handler->{'helo_name'} );
85 }
86 elsif ( $command eq SMFIC_HEADER ) {
87 399 my $header = $self->milter_split_buffer( $buffer );
88 0 ▶ if ( @$header == 1 ) { push @$header , q{}; };
Branch: 1 branch
BranchTrueFalseTotal
if (@$header == 1) 0 399 399
89 399 ▶ my $original = join( $self->{'headers_include_space'} ? ':': ': ', @$header );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'headers_include_space'} ? : 0 399 399
90 399 push @$header, $original;
91 399 $header->[1] =~ s/^\s+//;
92 399 $header->[0] =~ s/^\s+//;
93 399 $header->[0] =~ s/\s+$//;
94 399 $returncode = $handler->top_header_callback( @$header );
95 }
96 elsif ( $command eq SMFIC_MAIL ) {
97 33 my $envfrom = $self->milter_split_buffer( $buffer );
98 33 $returncode = $handler->top_envfrom_callback( @$envfrom );
99 }
100 elsif ( $command eq SMFIC_EOH ) {
101 33 $returncode = $handler->top_eoh_callback();
102 }
103 elsif ( $command eq SMFIC_OPTNEG ) {
104 33 ▶ $self->fatal('SMFIC_OPTNEG: packet has wrong size') unless (length($buffer) == 12);
Branch: 1 branch
BranchTrueFalseTotal
unless length $buffer == 12 0 33 33
105 33 my ($ver, $actions, $protocol) = unpack('NNN', $buffer);
106 33 ▶ $self->fatal("SMFIC_OPTNEG: unknown milter protocol version $ver") unless ($ver >= 2 && $ver <= 6);
Branch: 1 branch
BranchTrueFalseTotal
unless $ver >= 2 and $ver <= 6 0 33 33
Condition33%1/3
$ver >= 2 and $ver <= 6
!ll&&!rl&&r
0033
MC/DC0%0/2
$ver >= 2 and $ver <= 6
$ver >= 2 $ver <= 6
Truth table33%1/3
A and B
A= $ver >= 2B= $ver <= 6
ABresult
0X0
100
111
107 33 my $actions_reply = $self->{'callback_flags'} & $actions;
108 33 my $protocol_reply = $self->{'protocol'} & $protocol;
109 33 $self->write_packet(SMFIC_OPTNEG,
110 pack('NNN', 2, $actions_reply, $protocol_reply)
111 );
112 33 undef $returncode;
113 33 $self->{'headers_include_space'} = ($protocol_reply & SMFIP_HDR_LEADSPC) != 0;
114 }
115 elsif ( $command eq SMFIC_RCPT ) {
116 33 my $envrcpt = $self->milter_split_buffer( $buffer );
117 33 $returncode = $handler->top_envrcpt_callback( @$envrcpt );
118 }
119 elsif ( $command eq SMFIC_DATA ) {
120 }
121 elsif ( $command eq SMFIC_UNKNOWN ) {
122 0 undef $returncode;
123 # Unknown SMTP command received
124 }
125 else {
126 0 $self->fatal("Unknown milter command $command");
127 }
128  
129 890 my $config = $self->{'config'};
130  
131 890 my $reject_reason;
132 my $defer_reason;
133 890 my $quarantine_reason;
134 890 ▶ if ( $reject_reason = $handler->get_reject_mail() ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($reject_reason = $handler->get_reject_mail) { } 1 889 890
elsif ($defer_reason = $handler->get_defer_mail) { } 0 889 889
elsif ($quarantine_reason = $handler->get_quarantine_mail) { } 12 877 889
135 1 $handler->clear_reject_mail();
136 1 $returncode = SMFIS_REJECT;
137 }
138 elsif ( $defer_reason = $handler->get_defer_mail() ) {
139 0 $handler->clear_defer_mail();
140 0 $returncode = SMFIS_TEMPFAIL;
141 }
142 elsif ( $quarantine_reason = $handler->get_quarantine_mail() ) {
143 12 ▶ if ( $config->{'milter_quarantine'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'milter_quarantine'}) { } 0 12 12
144 0 $handler->clear_quarantine_mail();
145 0 $returncode = SMFIR_QUARANTINE;
146 }
147 else {
148 12 undef $quarantine_reason;
149 }
150 }
151  
152 890 ▶ if (defined $returncode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $returncode) 661 229 890
153 661 ▶ if ( $returncode eq SMFIR_QUARANTINE ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($returncode eq 'q') { } 0 661 661
elsif ($returncode == 100) { } 637 24 661
elsif ($returncode == 104) { } 23 1 24
elsif ($returncode == 101) { } 1 0 1
elsif ($returncode == 102) { } 0 0 0
elsif ($returncode == 103) { } 0 0 0
154 # NOP
155 }
156 elsif ( $returncode == SMFIS_CONTINUE ) {
157 637 $returncode = SMFIR_CONTINUE;
158 }
159 elsif ( $returncode == SMFIS_TEMPFAIL ) {
160 23 $returncode = SMFIR_TEMPFAIL;
161 }
162 elsif ( $returncode == SMFIS_REJECT ) {
163 1 $returncode = SMFIR_REJECT;
164 }
165 elsif ( $returncode == SMFIS_DISCARD ) {
166 0 $returncode = SMFIR_DISCARD;
167 }
168 elsif ( $returncode == SMFIS_ACCEPT ) {
169 0 $returncode = SMFIR_ACCEPT;
170 0 $handler->metric_count( 'mail_processed_total', { 'result' => 'accepted' } );
171 }
172  
173 661 ▶ if ( $config->{'dryrun'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"dryrun"}) 46 615 661
174 46 ▶ if ( $returncode ne SMFIR_CONTINUE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returncode ne "c") 23 23 46
175 23 $self->loginfo ( "dryrun returncode changed from $returncode to continue" );
176 23 $returncode = SMFIR_CONTINUE;
177 }
178 }
179  
180 661 ▶ if ( $command ne SMFIC_ABORT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command ne "A") 628 33 661
181 628 ▶ if ( $reject_reason ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($reject_reason) { } 1 627 628
elsif ($defer_reason) { } 0 627 627
elsif ($quarantine_reason) { } 0 627 627
182 1 my ( $rcode, $xcode, $message ) = split( ' ', $reject_reason, 3 );
183 1 ▶ if ($rcode !~ /^[5]\d\d$/ || $xcode !~ /^[5]\.\d\.\d$/ || substr($rcode, 0, 1) ne substr($xcode, 0, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rcode =~ /^[5]\d\d$/u && $xcode =~ /^[5]\.\d\.\d$/u or substr($rcode, 0, 1) ne substr($xcode, 0, 1)) { } 0 1 1
Condition33%2/6
not $rcode =~ /^[5]\d\d$/u && $xcode =~ /^[5]\.\d\.\d$/u or substr($rcode, 0, 1) ne substr($xcode, 0, 1)
l!l&&r!l&&!r
001
$rcode =~ /^[5]\d\d$/u && $xcode =~ /^[5]\.\d\.\d$/u
!ll&&!rl&&r
001
MC/DC0%0/3
not $rcode =~ /^[5]\d\d$/u && $xcode =~ /^[5]\.\d\.\d$/u or substr($rcode, 0, 1) ne substr($xcode, 0, 1)
$rcode =~ /^[5]\d\d$/u $xcode =~ /^[5]\.\d\.\d$/u substr($rcode, 0, 1) ne substr($xcode, 0, 1)
Truth table25%1/4
not(A && B) or C
A= $rcode =~ /^[5]\d\d$/uB= $xcode =~ /^[5]\.\d\.\d$/uC= substr($rcode, 0, 1) ne substr($xcode, 0, 1)
ABCresult
0XX1
10X1
1111
1100
184 0 $handler->metric_count( 'mail_processed_total', { 'result' => 'deferred_error' } );
185 0 $self->loginfo ( "Invalid reject message $reject_reason - setting to TempFail" );
186 0 $self->write_packet(SMFIR_TEMPFAIL );
187 }
188 else {
189 1 $handler->metric_count( 'mail_processed_total', { 'result' => 'rejected' } );
190 1 $self->loginfo ( "SMTPReject: $reject_reason" );
191 1 $self->write_packet( SMFIR_REPLYCODE,
192 $reject_reason
193 . "\0"
194 );
195 }
196 }
197 elsif ( $defer_reason ) {
198 0 my ( $rcode, $xcode, $message ) = split( ' ', $defer_reason, 3 );
199 0 ▶ if ($rcode !~ /^[4]\d\d$/ || $xcode !~ /^[4]\.\d\.\d$/ || substr($rcode, 0, 1) ne substr($xcode, 0, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rcode =~ /^[4]\d\d$/u && $xcode =~ /^[4]\.\d\.\d$/u or substr($rcode, 0, 1) ne substr($xcode, 0, 1)) { } 0 0 0
Condition0%0/6
not $rcode =~ /^[4]\d\d$/u && $xcode =~ /^[4]\.\d\.\d$/u or substr($rcode, 0, 1) ne substr($xcode, 0, 1)
l!l&&r!l&&!r
000
$rcode =~ /^[4]\d\d$/u && $xcode =~ /^[4]\.\d\.\d$/u
!ll&&!rl&&r
000
MC/DC0%0/3
not $rcode =~ /^[4]\d\d$/u && $xcode =~ /^[4]\.\d\.\d$/u or substr($rcode, 0, 1) ne substr($xcode, 0, 1)
$rcode =~ /^[4]\d\d$/u $xcode =~ /^[4]\.\d\.\d$/u substr($rcode, 0, 1) ne substr($xcode, 0, 1)
Truth table0%0/4
not(A && B) or C
A= $rcode =~ /^[4]\d\d$/uB= $xcode =~ /^[4]\.\d\.\d$/uC= substr($rcode, 0, 1) ne substr($xcode, 0, 1)
ABCresult
0XX1
10X1
1111
1100
200 0 $handler->metric_count( 'mail_processed_total', { 'result' => 'deferred_error' } );
201 0 $self->loginfo ( "Invalid defer message $defer_reason - setting to TempFail" );
202 0 $self->write_packet(SMFIR_TEMPFAIL );
203 }
204 else {
205 0 $handler->metric_count( 'mail_processed_total', { 'result' => 'deferred' } );
206 0 $self->loginfo ( "SMTPDefer: $defer_reason" );
207 0 $self->write_packet( SMFIR_REPLYCODE,
208 $defer_reason
209 . "\0"
210 );
211 }
212 }
213 elsif ( $quarantine_reason ) {
214 0 $handler->metric_count( 'mail_processed_total', { 'result' => 'quarantined' } );
215 0 $self->loginfo ( "SMTPQuarantine: $quarantine_reason" );
216 0 $self->write_packet( SMFIR_QUARANTINE,
217 $quarantine_reason
218 . "\0"
219 );
220 }
221 else {
222 627 $self->write_packet($returncode);
223 }
224 }
225 }
226 }
227  
228 33 ▶sub milter_process_connect {
Subroutine milter_process_connect: called CC=6 SCAR=22.2
Pod documented
229 33 my ( $self, $buffer ) = @_;
230  
231 33 ▶ unless ($buffer =~ s/^([^\0]*)\0(.)//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buffer =~ s/^([^\0]*)\0(.)//u) 0 33 33
232 0 $self->fatal('SMFIC_CONNECT: invalid connect info');
233 }
234 33 my $ip;
235 33 my $host = $1;
236  
237 33 my ($port, $addr) = unpack('nZ*', $buffer);
238  
239 33 ▶ if ( substr( $addr, 0, 5 ) eq 'IPv6:' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($addr, 0, 5) eq "IPv6:") 0 33 33
240 0 $addr = substr( $addr, 5 );
241 }
242  
243 33 ▶ if ( ! defined ( $addr ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $addr) { } 0 33 33
elsif (length $addr == 0) { } 0 33 33
244 0 $self->logerror('Unknown IP address format UNDEF');
245 0 $ip = undef;
246 # Could potentially fail here, connection is likely bad anyway.
247 }
248 elsif ( length ( $addr ) == 0 ) {
249 0 $self->logerror('Unknown IP address format NULL');
250 0 $ip = undef;
251 # Could potentially fail here, connection is likely bad anyway.
252 }
253 else {
254 33 eval {
255 33 $ip = Net::IP->new( $addr );
256 };
257 33 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 33 33
258 0 $self->logerror('Unknown IP address format - ' . $addr . ' - ' . $error );
259 0 $ip = undef;
260 # Could potentially fail here, connection is likely bad anyway.
261 }
262 }
263  
264 33 return ( $host, $ip );
265 }
266  
267 2769 ▶sub milter_read_block {
Subroutine milter_read_block: called CC=4 SCAR=14.1
Pod documented
268 2769 my ( $self, $len ) = @_;
269 2769 my $socket = $self->{'socket'};
270 2769 my $sofar = 0;
271 2769 my $buffer = q{};
272 2769 ▶ while ($len > $sofar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $sofar) 2638 2769 5407
273 2638 my $read = $socket->sysread($buffer, $len - $sofar, $sofar);
274 2638 ▶ last if (!defined($read) || $read <= 0); # EOF
Branch: 1 branch
BranchTrueFalseTotal
if not defined $read or $read <= 0 0 2638 2638
Condition33%1/3
not defined $read or $read <= 0
l!l&&r!l&&!r
002638
MC/DC0%0/2
not defined $read or $read <= 0
not defined $read $read <= 0
Truth table33%1/3
not(A) or B
A= not defined $readB= $read <= 0
ABresult
1X1
011
000
275 2638 $sofar += $read;
276 }
277 2769 return $buffer;
278 }
279  
280 694 ▶sub milter_split_buffer {
Subroutine milter_split_buffer: called CC=1 SCAR=0.0
Pod documented
281 694 my ( $self, $buffer ) = @_;
282 694 $buffer =~ s/\0$//; # remove trailing NUL
283 694 return [ split(/\0/, $buffer) ];
284 };
285  
286 ##
287  
288 9 ▶sub add_header {
Subroutine add_header: called CC=2 SCAR=7.1
Pod documented
289 9 my ( $self, $header, $value ) = @_;
290 9 $value =~ s/\015\012/\012/g;
291 $self->write_packet( SMFIR_ADDHEADER,
292 $header
293 . "\0"
294 9 ▶ . ($self->{'headers_include_space'} ? ' ' : '')
Branch: 1 branch
BranchTrueFalseTotal
$self->{'headers_include_space'} ? : 0 9 9
295 . $value
296 . "\0"
297 );
298 }
299  
300 13 ▶sub change_header {
Subroutine change_header: called CC=3 SCAR=11.4
Pod documented
301 13 my ( $self, $header, $index, $value ) = @_;
302 13 ▶ $value = '' unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 13 13
303 13 $value =~ s/\015\012/\012/g;
304 $self->write_packet( SMFIR_CHGHEADER,
305 pack('N', $index)
306 . $header
307 . "\0"
308 13 ▶ . ($self->{'headers_include_space'} ? ' ' : '')
Branch: 1 branch
BranchTrueFalseTotal
$self->{'headers_include_space'} ? : 0 13 13
309 . $value
310 . "\0"
311 );
312 }
313  
314 63 ▶sub insert_header {
Subroutine insert_header: called CC=2 SCAR=7.1
Pod documented
315 63 my ( $self, $index, $key, $value ) = @_;
316 63 $value =~ s/\015\012/\012/g;
317 $self->write_packet( SMFIR_INSHEADER,
318 pack( 'N', $index )
319 . $key
320 . "\0"
321 63 ▶ . ($self->{'headers_include_space'} ? ' ' : '')
Branch: 1 branch
BranchTrueFalseTotal
$self->{'headers_include_space'} ? : 0 63 63
322 . $value
323 . "\0"
324 );
325 }
326  
327 746 ▶sub write_packet {
Subroutine write_packet: called CC=2 SCAR=6.9
Pod documented
328 746 my ( $self, $code, $data ) = @_;
329 746 $self->logdebug ( "send command $code" );
330 746 my $socket = $self->{'socket'};
331 746 ▶ $data = q{} unless defined($data);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 627 119 746
332 746 my $len = pack('N', length($data) + 1);
333 746 $socket->syswrite($len);
334 746 $socket->syswrite($code);
335 746 $socket->syswrite($data);
336 }
337  
338 143 1;
339  
340 __END__
« Authentication/Handler/DKIM.pm ↑ Summary Authentication/Net/ServerPatches.pm »