blib/lib/Milter/Client.pm

filter: Statementstmt 61.9% 212 / 342 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 63 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 88.1% 67 / 76 Podpod 100% 6 / 6 totaltotal 46.4% 285 / 613 CC 77 SCAR 52.4
CC
77
Cov
43%
sendmail
600.0
conversation
380.0
decode
342.0
CRAP 189.2
↑ Summary
1 package Milter::Client 0.002;
2  
3 # ABSTRACT: Send commands to milters and get responses
4  
5  
6 1 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use re '/aa';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use IO::Socket::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Socket qw{SOCK_STREAM};
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 our %EXPORT_TAGS = (
18 constants => [
19 qw{
20 SMFIC_ABORT
21 SMFIC_BODY
22 SMFIC_CONNECT
23 SMFIC_MACRO
24 SMFIC_BODYEOB
25 SMFIC_HELO
26 SMFIC_HEADER
27 SMFIC_MAIL
28 SMFIC_EOH
29 SMFIC_OPTNEG
30 SMFIC_RCPT
31 SMFIC_QUIT
32 SMFIC_DATA
33 SMFIC_UNKNOWN
34 SMFIR_ADDRCPT
35 SMFIR_DELRCPT
36 SMFIR_ADDRCPT_PAR
37 SMFIR_ACCEPT
38 SMFIR_REPLBODY
39 SMFIR_CONTINUE
40 SMFIR_DISCARD
41 SMFIR_ADDHEADER
42 SMFIR_INSHEADER
43 SMFIR_SETSYMLIST
44 SMFIR_CHGHEADER
45 SMFIR_PROGRESS
46 SMFIR_QUARANTINE
47 SMFIR_REJECT
48 SMFIR_CHGFROM
49 SMFIR_TEMPFAIL
50 SMFIR_REPLYCODE
51 SMFIR_SKIP
52 SMFIA_UNKNOWN
53 SMFIA_UNIX
54 SMFIA_INET
55 SMFIA_INET6
56 CLIENT_TIMEOUT
57 CLIENT_EOF
58 MILTER_CHUNK_SIZE
59 }
60 ],
61 protocol => [
62 qw{
63 SMFIP_NOCONNECT
64 SMFIP_NOHELO
65 SMFIP_NOMAIL
66 SMFIP_NORCPT
67 SMFIP_NOBODY
68 SMFIP_NOHDRS
69 SMFIP_NOEOH
70 SMFIP_NR_HDR
71 SMFIP_NOUNKNOWN
72 SMFIP_NODATA
73 SMFIP_SKIP
74 SMFIP_NR_CONN
75 SMFIP_NR_HELO
76 SMFIP_NR_MAIL
77 SMFIP_NR_RCPT
78 SMFIP_NR_DATA
79 SMFIP_NR_UNKN
80 SMFIP_NR_EOH
81 SMFIP_NR_BODY
82 }
83 ],
84 );
85 1 our @EXPORT_OK = ( map( { @$_ } values(%EXPORT_TAGS) ), qw{split_message} );
86  
87 # The protocol's names are the interface: callers write SMFIC_HELO as sendmail's headers do, which Readonly cannot export.
88 ## no critic (PreferredModules)
89  
90 # Commands:
91 1 ▶use constant SMFIC_ABORT => 'A';
Subroutine BEGIN: called CC=1 SCAR=0.0
92 1 ▶use constant SMFIC_BODY => 'B';
Subroutine BEGIN: called CC=1 SCAR=0.0
93 1 ▶use constant SMFIC_CONNECT => 'C';
Subroutine BEGIN: called CC=1 SCAR=0.0
94 1 ▶use constant SMFIC_MACRO => 'D';
Subroutine BEGIN: called CC=1 SCAR=0.0
95 1 ▶use constant SMFIC_BODYEOB => 'E';
Subroutine BEGIN: called CC=1 SCAR=0.0
96 1 ▶use constant SMFIC_HELO => 'H';
Subroutine BEGIN: called CC=1 SCAR=0.0
97 1 ▶use constant SMFIC_HEADER => 'L';
Subroutine BEGIN: called CC=1 SCAR=0.0
98 1 ▶use constant SMFIC_MAIL => 'M';
Subroutine BEGIN: called CC=1 SCAR=0.0
99 1 ▶use constant SMFIC_EOH => 'N';
Subroutine BEGIN: called CC=1 SCAR=0.0
100 1 ▶use constant SMFIC_OPTNEG => 'O';
Subroutine BEGIN: called CC=1 SCAR=0.0
101 1 ▶use constant SMFIC_RCPT => 'R';
Subroutine BEGIN: called CC=1 SCAR=0.0
102 1 ▶use constant SMFIC_QUIT => 'Q';
Subroutine BEGIN: called CC=1 SCAR=0.0
103 1 ▶use constant SMFIC_DATA => 'T'; # v4
Subroutine BEGIN: called CC=1 SCAR=0.0
104 1 ▶use constant SMFIC_UNKNOWN => 'U'; # v3
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 # Responses:
107 1 ▶use constant SMFIR_ADDRCPT => '+';
Subroutine BEGIN: called CC=1 SCAR=0.0
108 1 ▶use constant SMFIR_DELRCPT => '-';
Subroutine BEGIN: called CC=1 SCAR=0.0
109 1 ▶use constant SMFIR_ADDRCPT_PAR => '2';
Subroutine BEGIN: called CC=1 SCAR=0.0
110 1 ▶use constant SMFIR_ACCEPT => 'a';
Subroutine BEGIN: called CC=1 SCAR=0.0
111 1 ▶use constant SMFIR_REPLBODY => 'b';
Subroutine BEGIN: called CC=1 SCAR=0.0
112 1 ▶use constant SMFIR_CONTINUE => 'c';
Subroutine BEGIN: called CC=1 SCAR=0.0
113 1 ▶use constant SMFIR_DISCARD => 'd';
Subroutine BEGIN: called CC=1 SCAR=0.0
114 1 ▶use constant SMFIR_ADDHEADER => 'h';
Subroutine BEGIN: called CC=1 SCAR=0.0
115 1 ▶use constant SMFIR_INSHEADER => 'i'; # v3, or v2 and Sendmail 8.13+
Subroutine BEGIN: called CC=1 SCAR=0.0
116 1 ▶use constant SMFIR_SETSYMLIST => 'l';
Subroutine BEGIN: called CC=1 SCAR=0.0
117 1 ▶use constant SMFIR_CHGHEADER => 'm';
Subroutine BEGIN: called CC=1 SCAR=0.0
118 1 ▶use constant SMFIR_PROGRESS => 'p';
Subroutine BEGIN: called CC=1 SCAR=0.0
119 1 ▶use constant SMFIR_QUARANTINE => 'q';
Subroutine BEGIN: called CC=1 SCAR=0.0
120 1 ▶use constant SMFIR_REJECT => 'r';
Subroutine BEGIN: called CC=1 SCAR=0.0
121 1 ▶use constant SMFIR_CHGFROM => 'e'; # Sendmail 8.14+
Subroutine BEGIN: called CC=1 SCAR=0.0
122 1 ▶use constant SMFIR_TEMPFAIL => 't';
Subroutine BEGIN: called CC=1 SCAR=0.0
123 1 ▶use constant SMFIR_REPLYCODE => 'y';
Subroutine BEGIN: called CC=1 SCAR=0.0
124 1 ▶use constant SMFIR_SKIP => 's'; # v6, to a body chunk: send no more of the body
Subroutine BEGIN: called CC=1 SCAR=0.0
125  
126 # Socktypes
127 1 ▶use constant SMFIA_UNKNOWN => 'U';
Subroutine BEGIN: called CC=1 SCAR=0.0
128 1 ▶use constant SMFIA_UNIX => 'L';
Subroutine BEGIN: called CC=1 SCAR=0.0
129 1 ▶use constant SMFIA_INET => '4';
Subroutine BEGIN: called CC=1 SCAR=0.0
130 1 ▶use constant SMFIA_INET6 => '6';
Subroutine BEGIN: called CC=1 SCAR=0.0
131  
132 # Protocol steps a milter can ask the MTA to leave out, or not to wait for a reply to
133 1 ▶use constant SMFIP_NOCONNECT => 0x00000001;
Subroutine BEGIN: called CC=1 SCAR=0.0
134 1 ▶use constant SMFIP_NOHELO => 0x00000002;
Subroutine BEGIN: called CC=1 SCAR=0.0
135 1 ▶use constant SMFIP_NOMAIL => 0x00000004;
Subroutine BEGIN: called CC=1 SCAR=0.0
136 1 ▶use constant SMFIP_NORCPT => 0x00000008;
Subroutine BEGIN: called CC=1 SCAR=0.0
137 1 ▶use constant SMFIP_NOBODY => 0x00000010;
Subroutine BEGIN: called CC=1 SCAR=0.0
138 1 ▶use constant SMFIP_NOHDRS => 0x00000020;
Subroutine BEGIN: called CC=1 SCAR=0.0
139 1 ▶use constant SMFIP_NOEOH => 0x00000040;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 1 ▶use constant SMFIP_NR_HDR => 0x00000080;
Subroutine BEGIN: called CC=1 SCAR=0.0
141 1 ▶use constant SMFIP_NOUNKNOWN => 0x00000100;
Subroutine BEGIN: called CC=1 SCAR=0.0
142 1 ▶use constant SMFIP_NODATA => 0x00000200;
Subroutine BEGIN: called CC=1 SCAR=0.0
143 1 ▶use constant SMFIP_SKIP => 0x00000400; # offered by an MTA which takes SMFIR_SKIP
Subroutine BEGIN: called CC=1 SCAR=0.0
144 1 ▶use constant SMFIP_NR_CONN => 0x00001000;
Subroutine BEGIN: called CC=1 SCAR=0.0
145 1 ▶use constant SMFIP_NR_HELO => 0x00002000;
Subroutine BEGIN: called CC=1 SCAR=0.0
146 1 ▶use constant SMFIP_NR_MAIL => 0x00004000;
Subroutine BEGIN: called CC=1 SCAR=0.0
147 1 ▶use constant SMFIP_NR_RCPT => 0x00008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
148 1 ▶use constant SMFIP_NR_DATA => 0x00010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
149 1 ▶use constant SMFIP_NR_UNKN => 0x00020000;
Subroutine BEGIN: called CC=1 SCAR=0.0
150 1 ▶use constant SMFIP_NR_EOH => 0x00040000;
Subroutine BEGIN: called CC=1 SCAR=0.0
151 1 ▶use constant SMFIP_NR_BODY => 0x00080000;
Subroutine BEGIN: called CC=1 SCAR=0.0
152  
153 # Not part of the milter protocol. Returned by sendmail() when asked to report on a hung or dead milter.
154 1 ▶use constant CLIENT_TIMEOUT => 'timeout';
Subroutine BEGIN: called CC=1 SCAR=0.0
155 1 ▶use constant CLIENT_EOF => 'eof';
Subroutine BEGIN: called CC=1 SCAR=0.0
156  
157 # Largest body chunk a milter will accept unless it negotiates otherwise (MILTER_CHUNK_SIZE in libmilter)
158 1 ▶use constant MILTER_CHUNK_SIZE => 65535;
Subroutine BEGIN: called CC=1 SCAR=0.0
159  
160 ## use critic
161  
162 # Pack templates for sending over messages
163 1 my %templates = (
164 SMFIC_OPTNEG() => "A N N N",
165 SMFIC_CONNECT() => "A Z* A n Z*",
166 SMFIC_HELO() => "A Z*",
167 SMFIC_MAIL() => "A Z*",
168 SMFIC_RCPT() => "A Z*",
169 SMFIC_DATA() => "A",
170 SMFIC_HEADER() => "A Z* Z*",
171 SMFIC_EOH() => "A",
172 SMFIC_BODY() => "A a*",
173 SMFIC_BODYEOB() => "A",
174 SMFIC_QUIT() => "A",
175 );
176  
177 # For each command, the protocol flag which leaves it out and the one which says the milter sends no reply to it
178 1 my %STEP = (
179 SMFIC_CONNECT() => [ SMFIP_NOCONNECT, SMFIP_NR_CONN ],
180 SMFIC_HELO() => [ SMFIP_NOHELO, SMFIP_NR_HELO ],
181 SMFIC_MAIL() => [ SMFIP_NOMAIL, SMFIP_NR_MAIL ],
182 SMFIC_RCPT() => [ SMFIP_NORCPT, SMFIP_NR_RCPT ],
183 SMFIC_DATA() => [ SMFIP_NODATA, SMFIP_NR_DATA ],
184 SMFIC_HEADER() => [ SMFIP_NOHDRS, SMFIP_NR_HDR ],
185 SMFIC_EOH() => [ SMFIP_NOEOH, SMFIP_NR_EOH ],
186 SMFIC_BODY() => [ SMFIP_NOBODY, SMFIP_NR_BODY ],
187 SMFIC_UNKNOWN() => [ SMFIP_NOUNKNOWN, SMFIP_NR_UNKN ],
188 );
189  
190 # Read exactly that many bytes, or die with CLIENT_EOF
191 0 ▶my $read_exactly = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
192 0 my ( $sock, $want ) = @_;
193 0 my $buf = '';
194 0 ▶ while ( length($buf) < $want ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf < $want) 0 0 0
195 0 my $got = sysread( $sock, $buf, $want - length($buf), length($buf) );
196 0 ▶ die CLIENT_EOF . "\n" unless $got;
Branch: 1 branch
BranchTrueFalseTotal
unless $got 0 0 0
197 }
198 0 return $buf;
199 1 };
200  
201 # Read one reply packet: 4 byte length, then that many bytes of code & payload.
202 0 ▶my $read_reply = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
203 0 my ( $sock, $timeout ) = @_;
204 0 ▶ $timeout ||= 1;
Condition0%0/2
$timeout ||= 1
l!l
00
MC/DC0%0/1
$timeout ||= 1
$timeout
Truth table0%0/2
A
A= $timeout
Aresult
11
00
205  
206 0 my $packet;
207 0 local $@;
208 0 my $ok = eval {
209 0 ▶ local $SIG{ALRM} = sub { die CLIENT_TIMEOUT . "\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
210 0 Time::HiRes::alarm($timeout);
211 0 my $len = unpack( 'N', $read_exactly->( $sock, 4 ) );
212 0 $packet = $read_exactly->( $sock, $len );
213 0 Time::HiRes::alarm(0);
214 0 1;
215 };
216 0 Time::HiRes::alarm(0);
217 0 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
218 0 my $err = $@;
219 0 chomp $err;
220 0 ▶ return CLIENT_TIMEOUT if $err eq CLIENT_TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
if $err eq "timeout" 0 0 0
221 0 return CLIENT_EOF;
222 }
223  
224 # The terminator of a string payload goes, but the negotiation and a new body are binary, and may end in a NUL that is data
225 0 my ( $code, $payload ) = unpack( 'a a*', $packet );
226 0 ▶ $payload =~ s/\0\z//xs if defined $payload && $code ne SMFIC_OPTNEG && $code ne SMFIR_REPLBODY;
Branch: 1 branch
BranchTrueFalseTotal
if defined $payload and $code ne "O" and $code ne "b" 0 0 0
Condition0%0/6
defined $payload and $code ne "O" and $code ne "b"
!ll&&!rl&&r
000
defined $payload and $code ne "O"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $payload and $code ne "O" and $code ne "b"
defined $payload $code ne "O" $code ne "b"
Truth table0%0/4
A and B and C
A= defined $payloadB= $code ne "O"C= $code ne "b"
ABCresult
0XX0
10X0
1100
1111
227 0 return ( $code, $payload );
228 1 };
229  
230  
231 1 my %MODIFICATIONS = map { $_ => 1 } ( SMFIR_ADDRCPT, SMFIR_DELRCPT, SMFIR_ADDRCPT_PAR, SMFIR_REPLBODY, SMFIR_ADDHEADER, SMFIR_INSHEADER, SMFIR_CHGHEADER, SMFIR_CHGFROM, SMFIR_QUARANTINE, SMFIR_PROGRESS );
232  
233 # Bogus sendmail.
234 0 ▶sub sendmail {
Subroutine sendmail: not called CC=24 SCAR=64.0
Pod documented
235 0 my ( $sock, @cmds ) = @_;
236 0 ▶ my %opts = ref $cmds[0] eq 'HASH' ? %{ shift @cmds } : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $cmds[0] eq 'HASH' ? : 0 0 0
237 0 my @mods;
238 0 my $protocol = 0;
239 0 my $skip_body;
240  
241 # A milter which hangs up is reported (or presumed to continue), not fatal
242 0 local $SIG{PIPE} = 'IGNORE';
243  
244 0 foreach my $args (@cmds) {
245 0 my $action = $args->[0];
246 0 ▶ my ( $left_out, $no_reply ) = @{ $STEP{ $action eq SMFIC_MACRO ? $args->[1] : $action } // [ 0, 0 ] };
Branch: 1 branch
BranchTrueFalseTotal
$action eq 'D' ? : 0 0 0
Condition0%0/2
$STEP{$action eq "D" ? $args->[1] : $action} // [0, 0]
l!l
00
MC/DC0%0/1
$STEP{$action eq "D" ? $args->[1] : $action} // [0, 0]
$STEP{$action eq "D" ? $args->[1] : $action}
Truth table0%0/2
A
A= $STEP{$action eq "D" ? $args->[1] : $action}
Aresult
11
00
247 0 ▶ next if $protocol & $left_out;
Branch: 1 branch
BranchTrueFalseTotal
if $protocol & $left_out 0 0 0
248 0 ▶ next if $skip_body && $action eq SMFIC_BODY;
Branch: 1 branch
BranchTrueFalseTotal
if $skip_body and $action eq "B" 0 0 0
Condition0%0/3
$skip_body and $action eq "B"
!ll&&!rl&&r
000
MC/DC0%0/2
$skip_body and $action eq "B"
$skip_body $action eq "B"
Truth table0%0/3
A and B
A= $skip_bodyB= $action eq "B"
ABresult
0X0
100
111
249  
250 # The single string form of SMFIC_HEADER needs the value terminator added
251 0 ▶ my @args = ( $action eq SMFIC_HEADER && @$args == 2 ) ? ( @$args, '' ) : @$args;
Branch: 1 branch
BranchTrueFalseTotal
$action eq 'L' && @$args == 2 ? : 0 0 0
Condition0%0/3
$action eq "L" and @$args == 2
!ll&&!rl&&r
000
MC/DC0%0/2
$action eq "L" and @$args == 2
$action eq "L" @$args == 2
Truth table0%0/3
A and B
A= $action eq "L"B= @$args == 2
ABresult
0X0
100
111
252  
253 # A macro command carries the command it is for, then any number of name/value pairs
254 0 ▶ my $template = $action eq SMFIC_MACRO ? 'A A' . ( ' Z*' x ( @args - 2 ) ) : $templates{$action};
Branch: 1 branch
BranchTrueFalseTotal
$action eq 'D' ? : 0 0 0
255 0 my $packed = pack( $template, @args );
256  
257 # What we will actually send over the wire
258 0 my $packed_with_length = pack( 'N a*', length($packed), $packed );
259 0 my $sent = syswrite $sock, $packed_with_length;
260 0 ▶ next if $action eq SMFIC_QUIT || $action eq SMFIC_MACRO || $protocol & $no_reply;
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "Q" or $action eq "D" or $protocol & $no_reply 0 0 0
Condition0%0/6
$action eq "Q" or $action eq "D" or $protocol & $no_reply
l!l&&r!l&&!r
000
$action eq "Q" or $action eq "D"
l!l&&r!l&&!r
000
MC/DC0%0/3
$action eq "Q" or $action eq "D" or $protocol & $no_reply
$action eq "Q" $action eq "D" $protocol & $no_reply
Truth table0%0/4
A or B or C
A= $action eq "Q"B= $action eq "D"C= $protocol & $no_reply
ABCresult
1XX1
01X1
0011
0000
261  
262 0 ▶ my ( $res, $payload ) = $sent ? $read_reply->( $sock, $opts{timeout} ) : (CLIENT_EOF);
Branch: 1 branch
BranchTrueFalseTotal
$sent ? : 0 0 0
263  
264 # A milter may send any number of modifications (and progress reports) before its final reply, each a packet of its own
265 0 ▶ while ( $MODIFICATIONS{$res} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MODIFICATIONS{$res}) 0 0 0
266 0 ▶ push @mods, [ $res, $payload ] unless $res eq SMFIR_PROGRESS;
Branch: 1 branch
BranchTrueFalseTotal
unless $res eq "p" 0 0 0
267 0 ( $res, $payload ) = $read_reply->( $sock, $opts{timeout} );
268 }
269  
270 0 ▶ if ( $res eq CLIENT_TIMEOUT || $res eq CLIENT_EOF ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res eq "timeout" or $res eq "eof") 0 0 0
Condition0%0/3
$res eq "timeout" or $res eq "eof"
l!l&&r!l&&!r
000
MC/DC0%0/2
$res eq "timeout" or $res eq "eof"
$res eq "timeout" $res eq "eof"
Truth table0%0/3
A or B
A= $res eq "timeout"B= $res eq "eof"
ABresult
1X1
011
000
271 0 ▶ return ( $res, undef, \@mods ) if defined $opts{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"timeout"} 0 0 0
272 0 next;
273 }
274  
275 # The milter's version, actions and protocol steps, then any macros it wants, which we send regardless
276 0 ▶ if ( $res eq SMFIC_OPTNEG ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res eq "O") 0 0 0
277 0 ( undef, undef, $protocol ) = unpack( 'N N N', $payload );
278 0 next;
279 }
280 0 ▶ if ( $res eq SMFIR_SKIP && $action eq SMFIC_BODY ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res eq "s" and $action eq "B") 0 0 0
Condition0%0/3
$res eq "s" and $action eq "B"
!ll&&!rl&&r
000
MC/DC0%0/2
$res eq "s" and $action eq "B"
$res eq "s" $action eq "B"
Truth table0%0/3
A and B
A= $res eq "s"B= $action eq "B"
ABresult
0X0
100
111
281 0 $skip_body = 1;
282 0 next;
283 }
284 0 ▶ return ( $res, $payload, \@mods ) if $res ne SMFIR_CONTINUE;
Branch: 1 branch
BranchTrueFalseTotal
if $res ne "c" 0 0 0
285 }
286 0 return ( SMFIR_CONTINUE, undef, \@mods );
287 }
288  
289  
290 0 ▶sub milter_socket {
Subroutine milter_socket: not called CC=7 SCAR=40.3
Pod documented
291 0 my ( $spec, $timeout ) = @_;
292 0 my $sock;
293 0 ▶ if ( my ($path) = $spec =~ m{\A (?:(?:unix|local):)? (/.*) \z}xs ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my($path) = $spec =~ m[\A (?:(?:unix|local):)? (/.*) \z]aasx) { } 0 0 0
elsif (() = $spec =~ /\A inet6?: (\d+) (?:\@(.+))? \z/aasx) { } 0 0 0
294 0 ▶ $sock = IO::Socket::UNIX->new( Type => SOCK_STREAM(), Peer => $path ) or die "Cannot connect to $spec: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$sock = "IO::Socket::UNIX"->new("Type", 1, "Peer", $path) or die "Cannot connect to ${spec}: $!\n" 0 0 0
295 }
296 elsif ( my ( $port, $host ) = $spec =~ m{\A inet6?: (\d+) (?:\@(.+))? \z}xs ) {
297 0 ▶ $sock = IO::Socket::IP->new( PeerHost => $host // 'localhost', PeerPort => $port, Type => SOCK_STREAM(), ( $timeout ? ( Timeout => $timeout ) : () ) ) or die "Cannot connect to $spec: $@\n";
Branch: 2 branches
BranchTrueFalseTotal
$sock = "IO::Socket::IP"->new("PeerHost", $host // "localhost", "PeerPort", $port, "Type", 1, $timeout ? ("Timeout", $timeout) : ()) or die "Cannot connect to ${spec}: $@\n" 0 0 0
$timeout ? : 0 0 0
Condition0%0/2
$host // "localhost"
l!l
00
MC/DC0%0/1
$host // "localhost"
$host
Truth table0%0/2
A
A= $host
Aresult
11
00
298 }
299 else {
300 0 die "Cannot connect to $spec: not unix:/path, inet:port\@host or inet6:port\@host\n";
301 }
302 0 return $sock;
303 }
304  
305  
306 1 my %STAGE = ( connect => SMFIC_CONNECT, helo => SMFIC_HELO, mail => SMFIC_MAIL, rcpt => SMFIC_RCPT, data => SMFIC_DATA, eoh => SMFIC_EOH, eom => SMFIC_BODYEOB );
307  
308 0 ▶sub conversation {
Subroutine conversation: not called CC=19 SCAR=59.4
Pod documented
309 0 my ( $raw, $env ) = @_;
310 0 ▶ $env //= {};
Condition0%0/2
$env //= {}
l!l
00
MC/DC0%0/1
$env //= {}
$env
Truth table0%0/2
A
A= $env
Aresult
11
00
311 0 my ( $fields, $body ) = split_message($raw);
312 0 $body =~ s/(?<!\r)\n/\r\n/xsg;
313  
314 0 ▶ my %macros = %{ $env->{macros} // {} };
Condition0%0/2
$env->{"macros"} // {}
l!l
00
MC/DC0%0/1
$env->{"macros"} // {}
$env->{"macros"}
Truth table0%0/2
A
A= $env->{"macros"}
Aresult
11
00
315 0 ▶ $macros{mail} = { i => $env->{queue_id}, %{ $macros{mail} // {} } } if defined $env->{queue_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $env->{"queue_id"} 0 0 0
Condition0%0/2
$macros{"mail"} // {}
l!l
00
MC/DC0%0/1
$macros{"mail"} // {}
$macros{"mail"}
Truth table0%0/2
A
A= $macros{"mail"}
Aresult
11
00
316 0 my %before;
317 0 foreach my $stage ( sort keys %macros ) {
318 0 ▶ my $command = $STAGE{$stage} // die "No such stage as $stage: one of " . join( ', ', sort keys %STAGE ) . "\n";
Condition0%0/2
$STAGE{$stage} // die("No such stage as ${stage}: one of " . join(", ", sort(keys %STAGE)) . "\n")
l!l
00
MC/DC0%0/1
$STAGE{$stage} // die("No such stage as ${stage}: one of " . join(", ", sort(keys %STAGE)) . "\n")
$STAGE{$stage}
Truth table0%0/2
A
A= $STAGE{$stage}
Aresult
11
00
319 0 my $set = $macros{$stage};
320 0 $before{$command} = [ [ SMFIC_MACRO, $command, map { $_ => $set->{$_} } sort keys %$set ] ];
321 }
322  
323 0 my $ip = $env->{client_ip};
324 0 ▶ my $family = !$ip ? SMFIA_UNKNOWN : index( $ip, ':' ) >= 0 ? SMFIA_INET6 : SMFIA_INET;
Branch: 2 branches
BranchTrueFalseTotal
!$ip ? : 0 0 0
index($ip, ':') != -1 ? : 0 0 0
325 0 ▶ my $host = $env->{client_host} // ( $ip ? "[$ip]" : 'localhost' );
Branch: 1 branch
BranchTrueFalseTotal
$ip ? : 0 0 0
Condition0%0/2
$env->{"client_host"} // ($ip ? "[$ip]" : "localhost")
l!l
00
MC/DC0%0/1
$env->{"client_host"} // ($ip ? "[$ip]" : "localhost")
$env->{"client_host"}
Truth table0%0/2
A
A= $env->{"client_host"}
Aresult
11
00
326 0 ▶ my @rcpts = ref $env->{rcpt_to} ? @{ $env->{rcpt_to} } : ( $env->{rcpt_to} // '' );
Branch: 1 branch
BranchTrueFalseTotal
ref $env->{'rcpt_to'} ? : 0 0 0
Condition0%0/2
$env->{"rcpt_to"} // ""
l!l
00
MC/DC0%0/1
$env->{"rcpt_to"} // ""
$env->{"rcpt_to"}
Truth table0%0/2
A
A= $env->{"rcpt_to"}
Aresult
11
00
327  
328 my @steps = (
329 [ SMFIC_CONNECT, $host, $family, 25, $ip // '' ],
330 [ SMFIC_HELO, $env->{helo} // 'unknown' ],
331 [ SMFIC_MAIL, '<' . ( $env->{mail_from} // '' ) . '>' ],
332 0 ( map { [ SMFIC_RCPT, "<$_>" ] } @rcpts ),
333 [SMFIC_DATA],
334 0 ▶ ( map { [ SMFIC_HEADER, @$_ ] } @$fields ),
Condition0%0/6
$ip // ""
l!l
00
$env->{"helo"} // "unknown"
l!l
00
$env->{"mail_from"} // ""
l!l
00
MC/DC0%0/1
$ip // ""
$ip
MC/DC0%0/1
$env->{"helo"} // "unknown"
$env->{"helo"}
MC/DC0%0/1
$env->{"mail_from"} // ""
$env->{"mail_from"}
Truth table0%0/2
A
A= $ip
Aresult
11
00
Truth table0%0/2
A
A= $env->{"helo"}
Aresult
11
00
Truth table0%0/2
A
A= $env->{"mail_from"}
Aresult
11
00
335 [SMFIC_EOH],
336 body_chunks($body),
337 [SMFIC_BODYEOB],
338 );
339  
340 # A macro goes before the first command of its stage only, as before the first of several RCPTs
341 0 my ( %sent, @cmds );
342 0 foreach my $step (@steps) {
343 0 ▶ push @cmds, @{ $before{ $step->[0] } // [] } unless $sent{ $step->[0] }++;
Branch: 1 branch
BranchTrueFalseTotal
unless $sent{$step->[0]}++ 0 0 0
Condition0%0/2
$before{$step->[0]} // []
l!l
00
MC/DC0%0/1
$before{$step->[0]} // []
$before{$step->[0]}
Truth table0%0/2
A
A= $before{$step->[0]}
Aresult
11
00
344 0 push @cmds, $step;
345 }
346 0 return ( [ SMFIC_OPTNEG, 6, 0x1FF, 0x1FFFFF ], @cmds, [SMFIC_QUIT] );
347 }
348  
349  
350 0 ▶sub split_message {
Subroutine split_message: not called CC=4 SCAR=30.0
Pod documented
351 0 my ($raw) = @_;
352 0 my ( $head, $body ) = split( /\r?\n\r?\n/xs, $raw, 2 );
353 0 ▶ $body //= '';
Condition0%0/2
$body //= ""
l!l
00
MC/DC0%0/1
$body //= ""
$body
Truth table0%0/2
A
A= $body
Aresult
11
00
354  
355 0 my @fields;
356 0 foreach my $line ( split( /\r?\n(?![ \t])/xs, $head ) ) {
357 0 ▶ my ( $name, $value ) = $line =~ m/\A ([^:\s]+) : [ \t]? (.*) \z/xs or next;
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /\A ([^:\s]+) : [ \t]? (.*) \z/aasx or next 0 0 0
358 0 $value =~ s/\r\n/\n/xsg;
359 0 push @fields, [ $name, $value ];
360 }
361 0 return ( \@fields, $body );
362 }
363  
364  
365 0 ▶sub body_chunks {
Subroutine body_chunks: not called CC=2 SCAR=17.9
Pod documented
366 0 my ($body) = @_;
367 0 my @chunks;
368 0 ▶ for ( my $pos = 0; $pos < length($body); $pos += MILTER_CHUNK_SIZE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $body) 0 0 0
369 0 push @chunks, [ SMFIC_BODY, substr( $body, $pos, MILTER_CHUNK_SIZE ) ];
370 }
371 0 return @chunks;
372 }
373  
374  
375 1 my %FINAL = ( SMFIR_ACCEPT() => 'accept', SMFIR_CONTINUE() => 'continue', SMFIR_DISCARD() => 'discard', SMFIR_REJECT() => 'reject', SMFIR_TEMPFAIL() => 'tempfail', CLIENT_TIMEOUT() => 'timeout', CLIENT_EOF() => 'eof' );
376  
377 0 ▶sub decode {
Subroutine decode: not called CC=18 SCAR=58.3
Pod documented
378 0 my ( $code, $payload ) = @_;
379 0 ▶ $payload //= '';
Condition0%0/2
$payload //= ""
l!l
00
MC/DC0%0/1
$payload //= ""
$payload
Truth table0%0/2
A
A= $payload
Aresult
11
00
380 0 ▶ return { action => $FINAL{$code} } if $FINAL{$code};
Branch: 1 branch
BranchTrueFalseTotal
if $FINAL{$code} 0 0 0
381 0 ▶ if ( $code eq SMFIR_ADDHEADER ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq "h") 0 0 0
382 0 my ( $name, $value ) = unpack( 'Z* a*', $payload );
383 0 return { action => 'add_header', name => $name, value => $value };
384 }
385 0 ▶ if ( $code eq SMFIR_INSHEADER || $code eq SMFIR_CHGHEADER ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq "i" or $code eq "m") 0 0 0
Condition0%0/3
$code eq "i" or $code eq "m"
l!l&&r!l&&!r
000
MC/DC0%0/2
$code eq "i" or $code eq "m"
$code eq "i" $code eq "m"
Truth table0%0/3
A or B
A= $code eq "i"B= $code eq "m"
ABresult
1X1
011
000
386 0 my ( $index, $name, $value ) = unpack( 'N Z* a*', $payload );
387 0 ▶ return { action => 'insert_header', index => $index, name => $name, value => $value } if $code eq SMFIR_INSHEADER;
Branch: 1 branch
BranchTrueFalseTotal
if $code eq "i" 0 0 0
388 0 ▶ return { action => 'delete_header', index => $index, name => $name } if $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "" 0 0 0
389 0 return { action => 'change_header', index => $index, name => $name, value => $value };
390 }
391 0 ▶ if ( $code eq SMFIR_ADDRCPT || $code eq SMFIR_ADDRCPT_PAR || $code eq SMFIR_CHGFROM ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq "+" or $code eq 2 or $code eq "e") 0 0 0
Condition0%0/6
$code eq "+" or $code eq 2 or $code eq "e"
l!l&&r!l&&!r
000
$code eq "+" or $code eq 2
l!l&&r!l&&!r
000
MC/DC0%0/3
$code eq "+" or $code eq 2 or $code eq "e"
$code eq "+" $code eq 2 $code eq "e"
Truth table0%0/4
A or B or C
A= $code eq "+"B= $code eq 2C= $code eq "e"
ABCresult
1XX1
01X1
0011
0000
392 0 my ( $addr, $args ) = unpack( 'Z* a*', $payload );
393 0 ▶ my $field = $code eq SMFIR_CHGFROM ? 'from' : 'rcpt';
Branch: 1 branch
BranchTrueFalseTotal
$code eq 'e' ? : 0 0 0
394 0 ▶ return { action => $code eq SMFIR_CHGFROM ? 'change_from' : 'add_rcpt', $field => $addr, ( $args ne '' ? ( args => $args ) : () ) };
Branch: 2 branches
BranchTrueFalseTotal
$code eq 'e' ? : 0 0 0
$args ne '' ? : 0 0 0
395 }
396 0 ▶ return { action => 'delete_rcpt', rcpt => $payload } if $code eq SMFIR_DELRCPT;
Branch: 1 branch
BranchTrueFalseTotal
if $code eq "-" 0 0 0
397 0 ▶ return { action => 'replace_body', body => $payload } if $code eq SMFIR_REPLBODY;
Branch: 1 branch
BranchTrueFalseTotal
if $code eq "b" 0 0 0
398 0 ▶ return { action => 'quarantine', reason => $payload } if $code eq SMFIR_QUARANTINE;
Branch: 1 branch
BranchTrueFalseTotal
if $code eq "q" 0 0 0
399 0 ▶ return { action => 'reply', reply => $payload } if $code eq SMFIR_REPLYCODE;
Branch: 1 branch
BranchTrueFalseTotal
if $code eq "y" 0 0 0
400 0 die "No such milter reply as '$code'\n";
401 }
402  
403 1 1;
404  
405 __END__
↑ Summary