Authentication/Protocol/SMTP.pm

filter: Statementstmt 72.1% 491 / 681 Branchbran 57.8% 162 / 280 Conditioncond 60.0% 24 / 40 MC/DCmcdc 25.0% 6 / 24 Subroutinesub 86.8% 33 / 38 Podpod 92.0% 23 / 25 totaltotal 67.9% 739 / 1088 CC 167 SCAR 40.2
CC
167
Cov
68%
smtp_command_data
140.7
smtp_command_mailfrom
63.4
smtp_forward_to_destination
59.2
CRAP 55.8
« Authentication/Net/ServerPatches.pm ↑ Summary Authentication/Handler/SPF.pm »
1 package Mail::Milter::Authentication::Protocol::SMTP;
2 146 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 146 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 146 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 146 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: SMTP protocol handling
7 146 our $VERSION = '4.20261116'; # VERSION
8 146 ▶use Digest::MD5 qw{ md5_hex };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 146 ▶use Email::Date::Format qw{ email_date };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 146 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 146 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 146 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 146 ▶use Net::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 16 ▶sub register_metrics {
Subroutine register_metrics: called CC=1 SCAR=0.0
Pod documented
16 return {
17 16 'mail_processed_total' => 'Number of emails processed',
18 };
19 }
20  
21 163 ▶sub get_smtp_config {
Subroutine get_smtp_config: called CC=2 SCAR=7.1
Pod documented
22 163 my ( $self ) = @_;
23 163 my $client_details = $self->get_client_details();
24 163 my $smtp_config;
25  
26 163 ▶ if ( exists( $self->{'config'}->{'smtp'}->{ $client_details } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'config'}{'smtp'}{$client_details}) { } 0 163 163
27 0 $smtp_config = $self->{'config'}->{'smtp'}->{ $client_details };
28 }
29 else {
30 163 $smtp_config = $self->{'config'}->{'smtp'};
31 }
32  
33 163 return $smtp_config;
34 }
35  
36 37 ▶sub queue_type {
Subroutine queue_type: called CC=2 SCAR=7.1
Pod documented
37 37 my ( $self ) = @_;
38 37 my $smtp_config = $self->get_smtp_config();
39 37 ▶ return $smtp_config->{'queue_type'} eq 'before' ? 'before' : 'after';
Branch: 1 branch
BranchTrueFalseTotal
$smtp_config->{'queue_type'} eq 'before' ? : 0 37 37
40 }
41  
42 1787 ▶sub smtp_status {
Subroutine smtp_status: called CC=1 SCAR=0.0
Pod documented
43 1787 my ( $self, $status ) = @_;
44 1787 my $smtp = $self->{'smtp'};
45 1787 $PROGRAM_NAME = $Mail::Milter::Authentication::Config::IDENT . ':' . $status . '(' . $self->{'count'} . '.' . $smtp->{'count'} . ')';
46 }
47  
48 212 ▶sub smtp_init {
Subroutine smtp_init: called CC=2 SCAR=6.9
Pod documented
49 212 my ( $self ) = @_;
50  
51 212 ▶ return if $self->{'smtp'}->{'init_required'} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"smtp"}{"init_required"} == 0 168 44 212
52  
53 44 my $handler = $self->{'handler'}->{'_Handler'};
54 44 my $smtp = $self->{'smtp'};
55  
56 44 $handler->set_symbol( 'C', 'j', $smtp->{'server_name'} );
57 44 $handler->set_symbol( 'C', '{rcpt_host}', $smtp->{'server_name'} );
58  
59 44 $smtp->{'queue_id'} = substr( uc md5_hex( "Authentication Milter Client $PID " . time() . rand(100) ) , -11 );
60 44 $handler->set_symbol( 'C', 'i', $self->smtp_queue_id() );
61  
62 44 $smtp->{'count'}++ ;
63 44 $handler->dbgout( 'SMTP Transaction count', $self->{'count'} . '.' . $smtp->{'count'} , LOG_DEBUG );
64  
65 44 $smtp->{'init_required'} = 0;
66 }
67  
68 37 ▶sub protocol_process_request {
Subroutine protocol_process_request: called CC=22 SCAR=34.6
Pod documented
69 37 my ( $self ) = @_;
70  
71 37 my $handler = $self->{'handler'}->{'_Handler'};
72 37 $handler->top_setup_callback();
73  
74 37 my $config = $self->{ 'config' };
75 37 ▶ my $seconds = $config->{'content_timeout'} // 300;
Condition50%1/2
$config->{"content_timeout"} // 300
l!l
370
MC/DC0%0/1
$config->{"content_timeout"} // 300
$config->{"content_timeout"}
Truth table50%1/2
A
A= $config->{"content_timeout"}
Aresult
11
00
76 37 $handler->set_overall_timeout( $seconds * 1000000 );
77  
78 $self->{'smtp'} = {
79 'fwd_helo_host' => undef,
80 'fwd_connect_ip' => undef,
81 'fwd_connect_host' => undef,
82 'fwd_ident' => undef,
83 'helo_host' => q{},
84 'mail_from' => q{},
85 'has_mail_from' => 0,
86 'has_data' => 0,
87 'connect_ip' => $self->{'server'}->{'peeraddr'},
88 37 'connect_host' => $self->{'server'}->{'peeraddr'},
89 'last_command' => 0,
90 'headers' => [],
91 'body' => q{},
92 'using_lmtp' => 0,
93 'lmtp_rcpt' => [],
94 'init_required' => 1,
95 'string' => q{},
96 'count' => 0,
97 };
98  
99 # If we have a UNIX connection then these will be undef,
100 # Set them to localhost to avoid warnings later.
101 37 ▶ if ( ! $self->{'smtp'}->{'connect_ip'} ) { $self->{'smtp'}->{'connect_ip'} = '127.0.0.1'; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"smtp"}{"connect_ip"}) 37 0 37
102  
103 37 ▶ if ( $self->{'smtp'}->{'connect_ip'} eq '127.0.0.1' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'smtp'}{'connect_ip'} eq '127.0.0.1') { } 37 0 37
104 37 $self->{'smtp'}->{'connect_host'} = 'localhost';
105 }
106 else {
107 # TODO do a reverse lookup here!
108 }
109  
110 37 my $smtp = $self->{'smtp'};
111 37 my $socket = $self->{'socket'};
112  
113 37 my $smtp_config = $self->get_smtp_config();
114 37 ▶ $smtp->{'server_name'} = $smtp_config->{'server_name'} || 'server.example.com';
Condition50%1/2
$smtp_config->{'server_name'} || 'server.example.com'
l!l
037
MC/DC0%0/1
$smtp_config->{'server_name'} || 'server.example.com'
$smtp_config->{'server_name'}
Truth table50%1/2
A
A= $smtp_config->{'server_name'}
Aresult
11
00
115 37 ▶ $smtp->{'smtp_timeout_in'} = $smtp_config->{'timeout_in'} || 60;
Condition50%1/2
$smtp_config->{'timeout_in'} || 60
l!l
037
MC/DC0%0/1
$smtp_config->{'timeout_in'} || 60
$smtp_config->{'timeout_in'}
Truth table50%1/2
A
A= $smtp_config->{'timeout_in'}
Aresult
11
00
116 37 ▶ $smtp->{'smtp_timeout_out'} = $smtp_config->{'timeout_out'} || 60;
Condition50%1/2
$smtp_config->{'timeout_out'} || 60
l!l
037
MC/DC0%0/1
$smtp_config->{'timeout_out'} || 60
$smtp_config->{'timeout_out'}
Truth table50%1/2
A
A= $smtp_config->{'timeout_out'}
Aresult
11
00
117  
118 37 print $socket "220 " . $smtp->{'server_name'} . " ESMTP AuthenticationMilter\r\n";
119  
120 37 $self->smtp_init();
121  
122 COMMAND:
123 37 ▶ while ( ! $smtp->{'last_command'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($smtp->{"last_command"}) 331 1 332
124  
125 331 my $command;
126 0 ▶ local $SIG{'ALRM'} = sub{ die "Timeout\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
127 331 alarm( $smtp->{'smtp_timeout_in'} );
128 331 eval {
129 331 $command = <$socket>;
130 };
131 331 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 331 331
132 0 $self->logerror( "Read Error: $error" );
133 0 last COMMAND;
134 }
135 331 alarm( 0 );
136  
137 331 ▶ if ( ! $command ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($command) 0 331 331
138 0 $self->logdebug( "receive NULL command" );
139 0 last COMMAND;
140 }
141  
142 331 $command =~ s/\r?\n$//;
143 331 my $uccommand = uc $command;
144  
145 331 $self->logdebug( "receive command $command" );
146  
147 331 ▶ if ( exists ( $smtp_config->{ 'debug_triggers' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $smtp_config->{"debug_triggers"}) 0 331 331
148 0 my $triggers = $smtp_config->{ 'debug_triggers' };
149 0 foreach my $trigger ( @$triggers ) {
150 0 ▶ if ( $command =~ /$trigger/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ /$trigger/u) 0 0 0
151 0 $self->enable_extra_debugging();
152 }
153 }
154 }
155  
156 331 my $returncode = SMFIS_CONTINUE;
157  
158 331 ▶ if ( $uccommand =~ /^EHLO/ ) {
Branch: 9 branches
BranchTrueFalseTotal
if ($uccommand =~ /^EHLO/u) { } 37 294 331
elsif ($uccommand =~ /^LHLO/u) { } 0 294 294
elsif ($uccommand =~ /^HELO/u) { } 0 294 294
elsif ($uccommand =~ /^XFORWARD/u) { } 126 168 294
elsif ($uccommand =~ /^MAIL FROM:/u) { } 42 126 168
elsif ($uccommand =~ /^RCPT TO:/u) { } 42 84 126
elsif ($uccommand =~ /^RSET/u) { } 6 78 84
elsif ($uccommand =~ /^DATA/u) { } 42 36 78
elsif ($uccommand =~ /^QUIT/u) { } 36 0 36
159 37 $self->smtp_command_ehlo( $command );
160 }
161 elsif ( $uccommand =~ /^LHLO/ ) {
162 0 $self->smtp_command_lhlo( $command );
163 }
164 elsif ( $uccommand =~ /^HELO/ ) {
165 0 $self->smtp_command_helo( $command );
166 }
167 elsif ( $uccommand =~ /^XFORWARD/ ) {
168 126 $self->smtp_command_xforward( $command );
169 }
170 elsif ( $uccommand =~ /^MAIL FROM:/ ) {
171 42 $self->smtp_init();
172 42 $self->smtp_command_mailfrom( $command );
173 }
174 elsif ( $uccommand =~ /^RCPT TO:/ ) {
175 42 $self->smtp_command_rcptto( $command );
176 }
177 elsif ( $uccommand =~ /^RSET/ ) {
178 6 $self->smtp_command_rset( $command );
179 }
180 elsif ( $uccommand =~ /^DATA/ ) {
181 42 $handler->set_overall_timeout( $seconds * 1000000 );
182 42 $self->smtp_command_data( $command );
183 }
184 elsif ( $uccommand =~ /^QUIT/ ){
185 36 $self->smtp_status('smtp.i.quit');
186 36 print $socket "221 2.0.0 Bye\n";
187 36 last COMMAND;
188 }
189 else {
190 0 $self->smtp_status('smtp.i.unknown');
191 0 $self->logerror( "Unknown SMTP command: $command" );
192 0 print $socket "502 5.5.2 I don't understand\r\n";
193 }
194  
195 }
196 37 $handler->clear_overall_timeout();
197  
198 37 $self->smtp_status('smtp.close');
199  
200 37 $self->close_destination_socket();
201  
202 37 delete $self->{'smtp'};
203 }
204  
205 125 ▶sub smtp_queue_id {
Subroutine smtp_queue_id: called CC=3 SCAR=12.3
Pod documented
206 125 my ( $self ) = @_;
207 125 my $smtp = $self->{'smtp'};
208 125 my $queue_id = $smtp->{'queue_id'};
209 125 ▶ if ( $smtp->{'fwd_ident'} && $smtp->{'fwd_ident'} ne '[UNAVAILABLE]' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"fwd_ident"} and $smtp->{"fwd_ident"} ne "[UNAVAILABLE]") 0 125 125
Condition33%1/3
$smtp->{"fwd_ident"} and $smtp->{"fwd_ident"} ne "[UNAVAILABLE]"
!ll&&!rl&&r
12500
MC/DC0%0/2
$smtp->{"fwd_ident"} and $smtp->{"fwd_ident"} ne "[UNAVAILABLE]"
$smtp->{"fwd_ident"} $smtp->{"fwd_ident"} ne "[UNAVAILABLE]"
Truth table33%1/3
A and B
A= $smtp->{"fwd_ident"}B= $smtp->{"fwd_ident"} ne "[UNAVAILABLE]"
ABresult
0X0
100
111
210 0 $queue_id .= '.' . $smtp->{'fwd_ident'};
211 }
212 125 return $queue_id;
213 }
214  
215 256 ▶sub command_param {
Subroutine command_param: called CC=2 SCAR=7.0
Pod documented
216 256 my ( $command, $index ) = @_;
217 256 my $p = q{};
218 256 ▶ if ( length( $command ) >= $index ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $command >= $index) 256 0 256
219 256 $p = substr( $command, $index );
220 }
221 256 return $p;
222 }
223  
224 # Splits the given command parameter line into a list of separate
225 # path (<foo@example.com>) and esmtp-param (KEYWORD=VALUE) strings.
226 93 ▶sub split_command_params {
Subroutine split_command_params: called CC=2 SCAR=7.0
Pod undocumented
227 93 my ( $line ) = @_;
228 93 $line =~ s/^\s+//;
229 93 ▶ my $path_end = find_path_end( $line ) || length( $line );
Condition66%2/3
find_path_end($line) || length $line
l!l&&r!l&&!r
9201
MC/DC50%1/2
find_path_end($line) || length $line
find_path_end($line) length $line
Truth table66%2/3
A || B
A= find_path_end($line)B= length $line
ABresult
1X1
011
000
230 93 my $path = substr( $line, 0, $path_end );
231 93 $line = substr( $line, $path_end );
232 93 $line =~ s/^\s+//;
233 93 return ( $path, split( /\s+/, $line ) );
234 }
235  
236 93 ▶sub find_path_end {
Subroutine find_path_end: called CC=13 SCAR=27.4
Pod undocumented
237 93 my ( $input ) = @_;
238 93 my $len = length( $input );
239 93 my @qstack;
240 93 ▶ for ( my $i = 0; $i < $len; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 1739 92 1831
241 1739 my $c = substr( $input, $i, 1 );
242 1739 ▶ if ( @qstack && $c eq $qstack[-1] ) {
Branch: 5 branches
BranchTrueFalseTotal
if (@qstack and $c eq $qstack[-1]) { } 25 1714 1739
elsif ($c eq '"') { } 0 1714 1714
elsif ($c eq '<' and !@qstack || $qstack[-1] eq '>') { } 25 1689 1714
elsif ($c eq '\\' and @qstack) { } 0 1689 1689
elsif ($c eq ' ' and not @qstack) { } 1 3375 3376
Condition100%3/3
@qstack and $c eq $qstack[-1]
!ll&&!rl&&r
126145325
MC/DC100%2/2
@qstack and $c eq $qstack[-1]
@qstack $c eq $qstack[-1]
Truth table100%3/3
A and B
A= @qstackB= $c eq $qstack[-1]
ABresult
0X0
100
111
243 # end of a quoted string
244 25 pop @qstack;
245 } elsif ( $c eq '"' ) {
246 # start of a "" quoted string
247 0 push @qstack, '"';
248 ▶ } elsif ( $c eq '<' && ( !@qstack || $qstack[-1] eq '>' ) ) {
Condition50%3/6
$c eq "<" and !@qstack || $qstack[-1] eq ">"
!ll&&!rl&&r
1689025
not @qstack or $qstack[-1] eq ">"
l!l&&r!l&&!r
2500
MC/DC33%1/3
$c eq "<" and !@qstack || $qstack[-1] eq ">"
$c eq "<" not @qstack $qstack[-1] eq ">"
Truth table50%2/4
A and not(B) or C
A= $c eq "<"B= not @qstackC= $qstack[-1] eq ">"
ABCresult
0XX0
1000
11X1
1011
249 # start of a <> quoted string (possibly inside of another <>)
250 25 push @qstack, '>';
251 ▶ } elsif ( $c eq '\\' && @qstack ) {
Condition33%1/3
$c eq "\\" and @qstack
!ll&&!rl&&r
168900
MC/DC0%0/2
$c eq "\\" and @qstack
$c eq "\\" @qstack
Truth table33%1/3
A and B
A= $c eq "\\"B= @qstack
ABresult
0X0
100
111
252 # escaped character within a quoted string
253 0 $i++;
254 ▶ } elsif ( $c eq ' ' && !@qstack ) {
Condition100%3/3
$c eq " " and not @qstack
!ll&&!rl&&r
168711
MC/DC100%2/2
$c eq " " and not @qstack
$c eq " " not @qstack
Truth table100%3/3
A and not(B)
A= $c eq " "B= not @qstack
ABresult
0X0
100
111
255 # found the end
256 1 return $i;
257 }
258 }
259 92 ▶ return ( @qstack ? undef : $len );
Branch: 1 branch
BranchTrueFalseTotal
@qstack ? : 0 92 92
260 }
261  
262 0 ▶sub smtp_command_lhlo {
Subroutine smtp_command_lhlo: not called CC=3 SCAR=24.8
Pod documented
263 0 my ( $self, $command ) = @_;
264 0 my $smtp = $self->{'smtp'};
265 0 my $socket = $self->{'socket'};
266 0 my $handler = $self->{'handler'}->{'_Handler'};
267 0 $self->smtp_status('smtp.i.lhlo');
268  
269 0 $smtp->{'using_lmtp'} = 1;
270  
271 0 ▶ if ( $smtp->{'has_data'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"has_data"}) 0 0 0
272 0 $self->logerror( "Out of Order SMTP command: $command" );
273 0 print $socket "501 5.5.2 Out of Order\r\n";
274 0 return;
275 }
276 0 $smtp->{'helo_host'} = command_param( $command,5 );
277 0 print $socket "250-" . $smtp->{'server_name'} . "\r\n";
278 0 ▶ if ( $self->queue_type() eq 'before' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->queue_type eq 'before') { } 0 0 0
279 0 print $socket "250-XFORWARD NAME ADDR HELO \r\n";
280 }
281 else {
282 0 print $socket "250-XFORWARD NAME ADDR HELO IDENT \r\n";
283 }
284 0 print $socket "250-PIPELINING\r\n";
285 0 print $socket "250-ENHANCEDSTATUSCODES\r\n";
286 0 print $socket "250 8BITMIME\r\n";
287 }
288  
289 37 ▶sub smtp_command_ehlo {
Subroutine smtp_command_ehlo: called CC=3 SCAR=11.7
Pod documented
290 37 my ( $self, $command ) = @_;
291 37 my $smtp = $self->{'smtp'};
292 37 my $socket = $self->{'socket'};
293 37 my $handler = $self->{'handler'}->{'_Handler'};
294 37 $self->smtp_status('smtp.i.ehlo');
295  
296 37 ▶ if ( $smtp->{'has_data'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"has_data"}) 0 37 37
297 0 $self->logerror( "Out of Order SMTP command: $command" );
298 0 print $socket "501 5.5.2 Out of Order\r\n";
299 0 return;
300 }
301 37 $smtp->{'helo_host'} = command_param( $command,5 );
302 37 print $socket "250-" . $smtp->{'server_name'} . "\r\n";
303 37 ▶ if ( $self->queue_type() eq 'before' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->queue_type eq 'before') { } 0 37 37
304 0 print $socket "250-XFORWARD NAME ADDR HELO \r\n";
305 }
306 else {
307 37 print $socket "250-XFORWARD NAME ADDR HELO IDENT \r\n";
308 }
309 37 print $socket "250-PIPELINING\r\n";
310 37 print $socket "250-ENHANCEDSTATUSCODES\r\n";
311 37 print $socket "250 8BITMIME\r\n";
312 }
313  
314 0 ▶sub smtp_command_helo {
Subroutine smtp_command_helo: not called CC=2 SCAR=17.9
Pod documented
315 0 my ( $self, $command ) = @_;
316 0 my $smtp = $self->{'smtp'};
317 0 my $socket = $self->{'socket'};
318 0 my $handler = $self->{'handler'}->{'_Handler'};
319 0 $self->smtp_status('smtp.i.helo');
320  
321 0 ▶ if ( $smtp->{'has_data'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"has_data"}) 0 0 0
322 0 $self->logerror( "Out of Order SMTP command: $command" );
323 0 print $socket "501 5.5.2 Out of Order\r\n";
324 0 return;
325 }
326 0 $smtp->{'helo_host'} = command_param( $command,5 );
327 0 print $socket "250 " . $smtp->{'server_name'} . " Hi " . $smtp->{'helo_host'} . "\r\n";
328 }
329  
330 126 ▶sub smtp_command_xforward {
Subroutine smtp_command_xforward: called CC=8 SCAR=25.4
Pod documented
331 126 my ( $self, $command ) = @_;
332 126 my $smtp = $self->{'smtp'};
333 126 my $socket = $self->{'socket'};
334 126 my $handler = $self->{'handler'}->{'_Handler'};
335 126 $self->smtp_status('smtp.i.xforward');
336  
337 126 $self->smtp_init();
338  
339 126 ▶ if ( $smtp->{'has_data'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"has_data"}) 0 126 126
340 0 $self->logerror( "Out of Order SMTP command: $command" );
341 0 print $socket "503 5.5.2 Out of Order\r\n";
342 0 return;
343 }
344 126 my $xdata = command_param( $command,9 );
345 126 foreach my $entry ( split( q{ }, $xdata ) ) {
346 126 my ( $key, $value ) = split( '=', $entry, 2 );
347 126 ▶ if ( $key eq 'NAME' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($key eq 'NAME') { } 42 84 126
elsif ($key eq 'ADDR') { } 42 42 84
elsif ($key eq 'HELO') { } 42 0 42
elsif ($key eq 'IDENT') { } 0 0 0
348 42 $smtp->{'fwd_connect_host'} = $value;
349 }
350 elsif ( $key eq 'ADDR' ) {
351 42 $smtp->{'fwd_connect_ip'} = $value;
352 }
353 elsif ( $key eq 'HELO' ) {
354 42 $smtp->{'fwd_helo_host'} = $value;
355 }
356 elsif ( $key eq 'IDENT' ) {
357 0 ▶ if ( $self->queue_type() eq 'before' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->queue_type eq 'before') { } 0 0 0
358 0 $self->logerror( "XForward IDENT received in before queue mode: $key=$value" );
359 }
360 else {
361 0 $smtp->{'fwd_ident'} = $value;
362 0 $handler->set_symbol( 'C', 'i', $self->smtp_queue_id() );
363 0 $handler->dbgout( 'Upstream ID', $value, LOG_INFO );
364 }
365 }
366 else {
367 # NOP
368 0 $self->logerror( "Unknown XForward Entry: $key=$value" );
369 ### log it here though
370 }
371 }
372 126 print $socket "250 2.0.0 Ok\r\n";
373 }
374  
375 7 ▶sub smtp_command_rset {
Subroutine smtp_command_rset: called CC=4 SCAR=13.9
Pod documented
376 7 my ( $self, $command ) = @_;
377 7 my $smtp = $self->{'smtp'};
378 7 my $socket = $self->{'socket'};
379 7 $self->smtp_status('smtp.i.rset');
380 7 $smtp->{'mail_from'} = q{};
381 7 $smtp->{'headers'} = [];
382 7 $smtp->{'body'} = q{};
383 7 $smtp->{'has_data'} = 0;
384 7 $smtp->{'has_mail_from'} = 0;
385 7 $smtp->{'fwd_connect_host'} = undef;
386 7 $smtp->{'fwd_connect_ip'} = undef;
387 7 $smtp->{'fwd_helo_host'} = undef;
388 7 $smtp->{'fwd_ident'} = undef;
389 7 $smtp->{'lmtp_rcpt'} = [];
390 7 $smtp->{'string'} = q{};
391 7 $self->{'handler'}->{'_Handler'}->top_close_callback();
392  
393 7 $smtp->{'init_required'} = 1;
394 7 $self->smtp_init();
395  
396 7 my $smtp_conf = $self->get_smtp_config();
397  
398 7 my $handler = $self->{'handler'}->{'_Handler'};
399 7 ▶ if ( $handler->{'exit_on_close'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler->{"exit_on_close"}) 0 7 7
400 0 $smtp->{'last_command'} = 1;
401 0 $handler->dbgout( 'EXIT ON CLOSE REQUESTED', 'closing on RSET', LOG_INFO );
402 0 print $socket "421 4.3.2 Pipeline limit reached\r\n";
403 0 return;
404 }
405 7 ▶ if ( $smtp_conf->{'pipeline_limit'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp_conf->{"pipeline_limit"}) 6 1 7
406 6 my $count = $smtp->{'count'};
407 6 my $limit = $smtp_conf->{'pipeline_limit'};
408 6 ▶ if ( $count > $limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > $limit) 1 5 6
409 1 $smtp->{'last_command'} = 1;
410 1 $handler->dbgout( 'SMTP Pipeline limit reached', 'closing on RSET', LOG_INFO );
411 1 print $socket "421 4.3.2 Pipeline limit reached\r\n";
412 1 return;
413 }
414 }
415 6 print $socket "250 2.0.0 Ok\r\n";
416 }
417  
418 47 ▶sub smtp_command_mailfrom {
Subroutine smtp_command_mailfrom: called CC=16 SCAR=41.5
Pod documented
419 47 my ( $self, $command ) = @_;
420 47 my $smtp = $self->{'smtp'};
421 47 my $socket = $self->{'socket'};
422 47 my $handler = $self->{'handler'}->{'_Handler'};
423 47 $self->smtp_status('smtp.i.mailfrom');
424  
425 47 my $returncode;
426 47 ▶ if ( $smtp->{'has_data'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"has_data"}) 0 47 47
427 0 $self->logerror( "Out of Order SMTP command: $command" );
428 0 print $socket "503 5.5.2 Out of Order\r\n";
429 0 return;
430 }
431 47 ▶ if ( $smtp->{'has_mail_from'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"has_mail_from"}) 0 47 47
432 0 $self->logerror( "Out of Order SMTP command: $command" );
433 0 print $socket "503 5.5.1 Nested MAIL Command\r\n";
434 0 return;
435 }
436  
437 # Do connect callback here, because of XFORWARD
438 47 ▶ my $host = $smtp->{'fwd_connect_host'} || $smtp->{'connect_host'};
Condition66%2/3
$smtp->{'fwd_connect_host'} || $smtp->{'connect_host'}
l!l&&r!l&&!r
4250
MC/DC0%0/2
$smtp->{'fwd_connect_host'} || $smtp->{'connect_host'}
$smtp->{'fwd_connect_host'} $smtp->{'connect_host'}
Truth table66%2/3
A || B
A= $smtp->{'fwd_connect_host'}B= $smtp->{'connect_host'}
ABresult
1X1
011
000
439 47 ▶ my $ip = $smtp->{'fwd_connect_ip'} || $smtp->{'connect_ip'};
Condition66%2/3
$smtp->{'fwd_connect_ip'} || $smtp->{'connect_ip'}
l!l&&r!l&&!r
4250
MC/DC0%0/2
$smtp->{'fwd_connect_ip'} || $smtp->{'connect_ip'}
$smtp->{'fwd_connect_ip'} $smtp->{'connect_ip'}
Truth table66%2/3
A || B
A= $smtp->{'fwd_connect_ip'}B= $smtp->{'connect_ip'}
ABresult
1X1
011
000
440 47 ▶ my $helo = $smtp->{'fwd_helo_host'} || $smtp->{'helo_host'};
Condition66%2/3
$smtp->{'fwd_helo_host'} || $smtp->{'helo_host'}
l!l&&r!l&&!r
4250
MC/DC0%0/2
$smtp->{'fwd_helo_host'} || $smtp->{'helo_host'}
$smtp->{'fwd_helo_host'} $smtp->{'helo_host'}
Truth table66%2/3
A || B
A= $smtp->{'fwd_helo_host'}B= $smtp->{'helo_host'}
ABresult
1X1
011
000
441  
442 47 ▶ if ( substr( $ip, 0, 5 ) eq 'IPv6:' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($ip, 0, 5) eq "IPv6:") 0 47 47
443 0 $ip = substr( $ip, 5 );
444 }
445  
446 # Do connection remapping first
447 47 $handler->remap_connect_callback( $host, Net::IP->new( $ip ) );
448 47 $handler->remap_helo_callback( $helo );
449  
450 47 $self->logdebug( "Inbound IP Address " . $handler->{'ip_object'}->short() );
451 47 $returncode = $handler->top_connect_callback( $host, $handler->{'ip_object'} );
452 47 ▶ if ( $returncode == SMFIS_CONTINUE ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($returncode == 100) { } 47 0 47
elsif (my $reject_reason = $handler->get_reject_mail) { } 0 0 0
elsif (my $defer_reason = $handler->get_defer_mail) { } 0 0 0
453 47 $returncode = $handler->top_helo_callback( $handler->{'helo_name'} );
454 47 ▶ if ( $returncode == SMFIS_CONTINUE ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($returncode == 100) { } 47 0 47
elsif (my $reject_reason = $handler->get_reject_mail) { } 0 0 0
elsif (my $defer_reason = $handler->get_defer_mail) { } 0 0 0
455 47 my $envfrom = command_param( $command,10 );
456 47 $smtp->{'mail_from'} = $envfrom;
457 47 $returncode = $handler->top_envfrom_callback( split_command_params( $envfrom ) );
458 47 ▶ if ( $returncode == SMFIS_CONTINUE ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($returncode == 100) { } 47 0 47
elsif (my $reject_reason = $handler->get_reject_mail) { } 0 0 0
elsif (my $defer_reason = $handler->get_defer_mail) { } 0 0 0
459 47 $smtp->{'has_mail_from'} = 1;
460 47 print $socket "250 2.0.0 Ok\r\n";
461 }
462 elsif ( my $reject_reason = $handler->get_reject_mail() ) {
463 0 $handler->clear_reject_mail();
464 0 $self->loginfo ( "SMTPReject: $reject_reason" );
465 0 print $socket $reject_reason . "\r\n";
466 }
467 elsif ( my $defer_reason = $handler->get_defer_mail() ) {
468 0 $handler->clear_defer_mail();
469 0 $self->loginfo ( "SMTPDefer: $defer_reason" );
470 0 print $socket $defer_reason . "\r\n";
471 }
472 else {
473 0 print $socket "451 4.0.0 MAIL - That's not right\r\n";
474 }
475 }
476 elsif ( my $reject_reason = $handler->get_reject_mail() ) {
477 0 $handler->clear_reject_mail();
478 0 $self->loginfo ( "SMTPReject: $reject_reason" );
479 0 print $socket $reject_reason . "\r\n";
480 }
481 elsif ( my $defer_reason = $handler->get_defer_mail() ) {
482 0 $handler->clear_defer_mail();
483 0 $self->loginfo ( "SMTPDefer: $defer_reason" );
484 0 print $socket $defer_reason . "\r\n";
485 }
486 else {
487 0 print $socket "451 4.0.1 HELO - That's not right\r\n";
488 }
489 }
490 elsif ( my $reject_reason = $handler->get_reject_mail() ) {
491 0 $handler->clear_reject_mail();
492 0 $self->loginfo ( "SMTPReject: $reject_reason" );
493 0 print $socket $reject_reason . "\r\n";
494 }
495 elsif ( my $defer_reason = $handler->get_defer_mail() ) {
496 0 $handler->clear_defer_mail();
497 0 $self->loginfo ( "SMTPDefer: $defer_reason" );
498 0 print $socket $defer_reason . "\r\n";
499 }
500 else {
501 0 print $socket "451 4.0.2 Connection - That's not right\r\n";
502 }
503 }
504  
505 49 ▶sub smtp_command_rcptto {
Subroutine smtp_command_rcptto: called CC=6 SCAR=22.1
Pod documented
506 49 my ( $self, $command ) = @_;
507 49 my $smtp = $self->{'smtp'};
508 49 my $socket = $self->{'socket'};
509 49 my $handler = $self->{'handler'}->{'_Handler'};
510 49 $self->smtp_status('smtp.i.rcptto');
511  
512 49 ▶ if ( $smtp->{'has_data'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"has_data"}) 0 49 49
513 0 $self->logerror( "Out of Order SMTP command: $command" );
514 0 print $socket "503 5.5.2 Out of Order\r\n";
515 0 return;
516 }
517 # The connect, helo, and envfrom callbacks are only run from MAIL FROM
518 49 ▶ if ( ! $smtp->{'has_mail_from'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($smtp->{"has_mail_from"}) 3 46 49
519 3 $self->logerror( "Out of Order SMTP command: $command" );
520 3 print $socket "503 5.5.1 Need MAIL command\r\n";
521 3 return;
522 }
523 46 my $envrcpt = command_param( $command,8 );
524 46 my $returncode = $handler->top_envrcpt_callback( split_command_params( $envrcpt ) );
525 46 ▶ if ( $returncode == SMFIS_CONTINUE ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($returncode == 100) { } 46 0 46
elsif (my $reject_reason = $handler->get_reject_mail) { } 0 0 0
elsif (my $defer_reason = $handler->get_defer_mail) { } 0 0 0
526 46 push @{ $smtp->{'lmtp_rcpt'} }, $envrcpt;
527 46 print $socket "250 2.0.0 Ok\r\n";
528 }
529 elsif ( my $reject_reason = $handler->get_reject_mail() ) {
530 0 $handler->clear_reject_mail();
531 0 $self->loginfo ( "SMTPReject: $reject_reason" );
532 0 print $socket $reject_reason . "\r\n";
533 }
534 elsif ( my $defer_reason = $handler->get_defer_mail() ) {
535 0 $handler->clear_defer_mail();
536 0 $self->loginfo ( "SMTPDefer: $defer_reason" );
537 0 print $socket $defer_reason . "\r\n";
538 }
539 else {
540 0 print $socket "451 4.0.3 That's not right\r\n";
541 }
542 }
543  
544 56 ▶sub smtp_command_data {
Subroutine smtp_command_data: called CC=53 SCAR=49.5
Pod documented
545 56 my ( $self, $command ) = @_;
546 56 my $smtp = $self->{'smtp'};
547 56 my $socket = $self->{'socket'};
548 56 my $handler = $self->{'handler'}->{'_Handler'};
549 56 $self->smtp_status('smtp.i.data');
550  
551 56 my $body = q{};
552 56 my $done = 0;
553 56 my $fail = 0;
554 56 my $saw_terminator = 0;
555 56 my @header_split;
556 my $returncode;
557  
558 56 ▶ if ( ! $smtp->{'has_mail_from'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($smtp->{"has_mail_from"}) 2 54 56
559 2 $self->logerror( "Out of Order SMTP command: $command" );
560 2 print $socket "503 5.5.1 Need MAIL command\r\n";
561 2 return;
562 }
563  
564 54 ▶ if ( ! scalar @{ $smtp->{'lmtp_rcpt'} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$smtp->{"lmtp_rcpt"};}) 0 54 54
565 0 $self->logerror( "No valid recipients" );
566 0 print $socket "554 No valid recipients\r\n";
567 0 return;
568 }
569  
570 54 ▶ if ( $smtp->{'has_data'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"has_data"}) 0 54 54
571 0 $self->logerror( "Repeated SMTP DATA command: $command" );
572 0 print $socket "503 5.5.2 One at a time please\r\n";
573 0 return;
574 }
575 54 print $socket "354 2.0.0 Send body\r\n";
576  
577 0 ▶ local $SIG{'ALRM'} = sub{ die "Timeout\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
578 54 eval{
579 54 alarm( $smtp->{'smtp_timeout_in'} );
580 HEADERS:
581 54 ▶ while ( my $dataline = <$socket> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $dataline = readline $socket)) 1552 1 1553
582 1552 $self->extra_debugging( "RAW DEBUG: ". $dataline );
583 1552 alarm( 0 );
584 # Only CRLF.CRLF ends DATA, a bare LF form could be read differently upstream
585 1552 ▶ if ( $dataline eq ".\r\n" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataline eq ".\r\n") 1 1551 1552
586 1 $done = 1;
587 1 $saw_terminator = 1;
588 1 last HEADERS;
589 }
590 1551 $dataline =~ s/\r?\n$//;
591 # Handle transparency
592 1551 ▶ if ( $dataline =~ /^\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataline =~ /^\./u) 1 1550 1551
593 1 $dataline = substr( $dataline, 1 );
594 }
595 1551 ▶ if ( $dataline eq q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataline eq "") 52 1499 1551
596 52 last HEADERS;
597 }
598 1499 push @header_split, $dataline;
599 1499 alarm( $smtp->{'smtp_timeout_in'} );
600 }
601 };
602 54 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 54 54
603 0 $self->logerror( "Read Error: $error" );
604 0 $done = 1;
605 0 $fail = 1;
606 }
607 54 alarm( 0 );
608  
609 54 $self->smtp_status('smtp.i.data.process');
610 54 my $value = q{};
611 54 foreach my $header_line ( @header_split ) {
612 1499 ▶ if ( $header_line =~ /^\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header_line =~ /^\s/u) { } 985 514 1499
613 985 $value .= "\r\n" . $header_line;
614 }
615 else {
616 514 ▶ if ( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 460 54 514
617 460 push @{ $smtp->{'headers'} } , $value;
618 460 my ( $hkey, $hvalue ) = split ( ':', $value, 2 );
619 460 $hvalue =~ s/^\s+//;
620 460 $hkey =~ s/^\s+//;
621 460 $hkey =~ s/\s+$//;
622 460 ▶ if ( ! $fail ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fail) 460 0 460
623 460 my $returncode = $handler->top_header_callback( $hkey, $hvalue, $value );
624 460 ▶ if ( $returncode != SMFIS_CONTINUE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returncode != 100) 0 460 460
625 0 $fail = 1;
626 }
627 }
628 }
629 514 $value = $header_line;
630 }
631 }
632 54 ▶ if ( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 54 0 54
633 54 push @{ $smtp->{'headers'} } , $value;
634 54 my ( $hkey, $hvalue ) = split ( ':', $value, 2 );
635 54 $hvalue =~ s/^\s+//;
636 54 $hkey =~ s/^\s+//;
637 54 $hkey =~ s/\s+$//;
638 54 ▶ if ( ! $fail ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fail) 54 0 54
639 54 my $returncode = $handler->top_header_callback( $hkey, $hvalue, $value );
640 54 ▶ if ( $returncode != SMFIS_CONTINUE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returncode != 100) 0 54 54
641 0 $fail = 1;
642 }
643 }
644 }
645 54 ▶ if ( ! $fail ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fail) 54 0 54
646 54 $returncode = $handler->top_eoh_callback();
647 54 ▶ if ( $returncode != SMFIS_CONTINUE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returncode != 100) 0 54 54
648 0 $fail = 1;
649 }
650 }
651  
652 54 my $smtp_conf = $self->get_smtp_config();
653  
654 54 my $chunk_limit = 1048576; # Process in chunks no larger than...
655 54 ▶ if ( exists ( $smtp_conf->{ 'chunk_limit' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $smtp_conf->{"chunk_limit"}) 0 54 54
656 0 $chunk_limit = $smtp_conf->{ 'chunk_limit' };
657 }
658  
659 54 my $temp_file;
660 54 ▶ if ( exists ( $smtp_conf->{ 'temp_dir' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $smtp_conf->{"temp_dir"}) 0 54 54
661 # TODO use spool_dir to create this
662 # add config option to en/disable this
663 # allow dir to be overridden
664 0 $temp_file = File::Temp->new( DIR => $smtp_conf->{ 'temp_dir' } );
665 }
666  
667 54 $self->smtp_status('smtp.i.body');
668 54 my $body_chunk = q{};
669 54 ▶ if ( ! $done ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 53 1 54
670 53 eval {
671 53 alarm( $smtp->{'smtp_timeout_in'} );
672 DATA:
673 53 ▶ while ( my $dataline = <$socket> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $dataline = readline $socket)) 736 4 740
674 736 $self->extra_debugging( "RAW DEBUG: ". $dataline );
675 736 alarm( 0 );
676 736 ▶ if ( $dataline eq ".\r\n" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataline eq ".\r\n") 49 687 736
677 49 $saw_terminator = 1;
678 49 last DATA;
679 }
680 # Handle transparency
681 687 ▶ if ( $dataline =~ /^\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataline =~ /^\./u) 18 669 687
682 18 $dataline = substr( $dataline, 1 );
683 }
684  
685 687 ▶ if ( $temp_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($temp_file) { } 0 687 687
686 0 print $temp_file $dataline;
687 }
688 else {
689 687 $body .= $dataline;
690 }
691  
692 687 ▶ if ( length( $body_chunk ) + length( $dataline ) > $chunk_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($body_chunk) + length($dataline) > $chunk_limit) 0 687 687
693 0 $returncode = $handler->top_body_callback( $body_chunk );
694 0 ▶ if ( $returncode != SMFIS_CONTINUE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returncode != 100) 0 0 0
695 0 $fail = 1;
696 }
697 0 $body_chunk = q{};
698 }
699  
700 687 $body_chunk .= $dataline;
701  
702 687 alarm( $smtp->{'smtp_timeout_in'} );
703 }
704 # The connection closed before the end of data marker, the message is incomplete
705 53 ▶ if ( ! $saw_terminator ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($saw_terminator) 4 49 53
706 4 die "Connection closed during DATA\n";
707 }
708 49 ▶ if ( ! $fail ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fail) 49 0 49
709 49 $returncode = $handler->top_body_callback( $body_chunk );
710 49 ▶ if ( $returncode != SMFIS_CONTINUE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returncode != 100) 0 49 49
711 0 $fail = 1;
712 }
713 49 $body_chunk = q{};
714 }
715 };
716 53 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 4 49 53
717 4 $self->logerror( "Read Error: $error" );
718 4 $done = 1;
719 4 $fail = 1;
720 }
721 53 alarm( 0 );
722 }
723  
724 54 ▶ if ( ! $fail ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fail) 50 4 54
725 50 $returncode = $handler->top_eom_callback();
726 50 ▶ if ( $returncode != SMFIS_CONTINUE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returncode != 100) 1 49 50
727 1 $fail = 1;
728 }
729 }
730  
731 54 $self->smtp_status('smtp.i.data.received');
732  
733 54 ▶ if ( ! $fail ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $fail) { } 49 5 54
elsif (my $reject_reason = $handler->get_reject_mail) { } 1 4 5
elsif (my $defer_reason = $handler->get_defer_mail) { } 0 4 4
734  
735 49 ▶ if ( $temp_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($temp_file) { } 0 49 49
736 0 $smtp->{'spool'} = $temp_file;
737 }
738 else {
739 49 $smtp->{'body'} = $body;
740 }
741  
742 49 ▶ if ( $self->smtp_forward_to_destination() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->smtp_forward_to_destination) { } 49 0 49
743  
744 49 ▶ if ( my $reject_reason = $handler->get_quarantine_mail() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $reject_reason = $handler->get_quarantine_mail) { } 5 44 49
745 5 $handler->metric_count( 'mail_processed_total', { 'result' => 'quarantined' } );
746 }
747 else {
748 44 $handler->metric_count( 'mail_processed_total', { 'result' => 'accepted' } );
749 }
750  
751 49 $handler->dbgout( 'Accept string', $smtp->{'string'}, LOG_INFO );
752 49 $smtp->{'has_data'} = 1;
753  
754 49 ▶ if ( $smtp->{'using_lmtp'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{'using_lmtp'}) { } 0 49 49
755 0 foreach my $rcpt_to ( @{ $smtp->{'lmtp_rcpt'} } ) {
756 0 print $socket "250 2.0.0 Queued as " . $self->smtp_queue_id() . "\r\n";
757 }
758 }
759 else {
760 49 print $socket "250 2.0.0 Queued as " . $self->smtp_queue_id() . "\r\n";
761 }
762 }
763 else {
764 0 $self->logerror( "SMTP Mail Rejected" );
765 0 my $error = '451 4.0.4 That\'s not right';
766 0 my $upstream_error = $smtp->{'string'};
767 0 ▶ if ( $upstream_error =~ /^4\d\d / ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($upstream_error =~ /^4\d\d /u) { } 0 0 0
elsif ($upstream_error =~ /^5\d\d /u) { } 0 0 0
768 0 $handler->metric_count( 'mail_processed_total', { 'result' => 'deferred' } );
769 0 $error = $upstream_error;
770 }
771 elsif ( $upstream_error =~ /^5\d\d / ) {
772 # Also pass back rejects
773 0 $handler->metric_count( 'mail_processed_total', { 'result' => 'rejected' } );
774 0 $error = $upstream_error;
775 }
776 else {
777 0 $handler->metric_count( 'mail_processed_total', { 'result' => 'deferred_error' } );
778 0 $error .= ': ' . $upstream_error;
779 }
780 0 ▶ if ( $smtp->{'using_lmtp'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{'using_lmtp'}) { } 0 0 0
781 0 foreach my $rcpt_to ( @{ $smtp->{'lmtp_rcpt'} } ) {
782 0 print $socket "$error\r\n";
783 }
784 }
785 else {
786 0 print $socket "$error\r\n";
787 }
788 }
789 }
790 elsif ( my $reject_reason = $handler->get_reject_mail() ) {
791 1 $handler->metric_count( 'mail_processed_total', { 'result' => 'rejected' } );
792 1 $handler->clear_reject_mail();
793 1 ▶ if ( $smtp->{'using_lmtp'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{'using_lmtp'}) { } 0 1 1
794 0 foreach my $rcpt_to ( @{ $smtp->{'lmtp_rcpt'} } ) {
795 0 $self->loginfo ( "SMTPReject: $reject_reason" );
796 0 print $socket $reject_reason . "\r\n";
797 }
798 }
799 else {
800 1 $self->loginfo ( "SMTPReject: $reject_reason" );
801 1 print $socket $reject_reason . "\r\n";
802 }
803 }
804 elsif ( my $defer_reason = $handler->get_defer_mail() ) {
805 0 $handler->metric_count( 'mail_processed_total', { 'result' => 'defered' } );
806 0 $handler->clear_defer_mail();
807 0 ▶ if ( $smtp->{'using_lmtp'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{'using_lmtp'}) { } 0 0 0
808 0 foreach my $rcpt_to ( @{ $smtp->{'lmtp_rcpt'} } ) {
809 0 $self->loginfo ( "SMTPDefer: $defer_reason" );
810 0 print $socket $defer_reason . "\r\n";
811 }
812 }
813 else {
814 0 $self->loginfo ( "SMTPDefer: $defer_reason" );
815 0 print $socket $defer_reason . "\r\n";
816 }
817 }
818 else {
819 4 $handler->metric_count( 'mail_processed_total', { 'result' => 'deferred_error' } );
820 4 ▶ if ( $smtp->{'using_lmtp'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{'using_lmtp'}) { } 0 4 4
821 0 foreach my $rcpt_to ( @{ $smtp->{'lmtp_rcpt'} } ) {
822 0 print $socket "451 4.0.5 That's not right\r\n";
823 }
824 }
825 else {
826 4 print $socket "451 4.0.6 That's not right\r\n";
827 }
828 }
829 54 $self->smtp_status('smtp.i.data.done');
830  
831 # Reset
832 54 $smtp->{'mail_from'} = q{};
833 54 $smtp->{'has_mail_from'} = 0;
834 54 $smtp->{'rcpt_to'} = [];
835 54 $smtp->{'headers'} = [];
836 54 $smtp->{'body'} = q{};
837 54 $smtp->{'has_data'} = 0;
838 54 $smtp->{'fwd_connect_host'} = undef;
839 54 $smtp->{'fwd_connect_ip'} = undef;
840 54 $smtp->{'fwd_helo_host'} = undef;
841 54 $smtp->{'lmtp_rcpt'} = [];
842 54 $smtp->{'string'} = q{};
843 54 $self->{'handler'}->{'_Handler'}->top_close_callback();
844 54 $smtp->{'init_required'} = 1;
845  
846 54 ▶ if ( $handler->{'exit_on_close'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler->{"exit_on_close"}) 0 54 54
847 0 $smtp->{'last_command'} = 1;
848 0 $handler->dbgout( 'EXIT ON CLOSE REQUESTED', 'NO MORE SMTP', LOG_INFO );
849 }
850 }
851  
852 41 ▶sub smtp_insert_received_header {
Subroutine smtp_insert_received_header: called CC=1 SCAR=0.0
Pod documented
853 41 my ( $self ) = @_;
854 41 my $smtp = $self->{'smtp'};
855  
856 my $value = join ( q{},
857  
858 'from ',
859 $smtp->{'helo_host'},
860 ' (',
861 $smtp->{'connect_host'}
862 ,
863 ' [',
864 $smtp->{'connect_ip'},
865 '])',
866 "\r\n",
867  
868 ' by ',
869 41 $smtp->{'server_name'},
870 ' (Authentication Milter)',
871 ' with ESMTP',
872 "\r\n",
873  
874 ' id ',
875 $self->smtp_queue_id(),
876 ';',
877 "\r\n",
878  
879 ' ',
880 email_date(),
881  
882 );
883  
884 41 splice @{ $smtp->{'headers'} }, 0, 0, 'Received: '. $value;
885 }
886  
887 41 ▶sub smtp_forward_to_destination {
Subroutine smtp_forward_to_destination: called CC=28 SCAR=40.8
Pod documented
888 41 my ( $self ) = @_;
889 41 $self->smtp_status('smtp.o');
890  
891 41 my $smtp = $self->{'smtp'};
892  
893 41 $self->smtp_insert_received_header();
894  
895 41 my $smtp_conf = $self->get_smtp_config();
896  
897 41 my $sock = $smtp->{'destination_sock'};
898  
899 41 my $new_sock = 0;
900  
901 41 my $line;
902  
903 41 ▶ if ( $sock ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock) 5 36 41
904 5 ▶ if ( ! $sock->connected() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock->connected) 0 5 5
905 0 $self->logerror( "Outbound SMTP socket was disconnected by remote end" );
906 0 undef $sock;
907 }
908 }
909  
910 41 ▶ if ( ! $sock ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 36 5 41
911 36 $new_sock = 1;
912 36 $self->smtp_status('smtp.o.open');
913  
914 36 ▶ if ( $smtp_conf->{'sock_type'} eq 'inet' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($smtp_conf->{'sock_type'} eq 'inet') { } 0 36 36
elsif ($smtp_conf->{'sock_type'} eq 'unix') { } 36 0 36
915 $sock = IO::Socket::INET->new(
916 'Proto' => 'tcp',
917 'PeerAddr' => $smtp_conf->{'sock_host'},
918 0 'PeerPort' => $smtp_conf->{'sock_port'},
919 );
920 }
921 elsif ( $smtp_conf->{'sock_type'} eq 'unix' ) {
922 $sock = IO::Socket::UNIX->new(
923 36 'Peer' => $smtp_conf->{'sock_path'},
924 );
925 }
926 else {
927 0 $self->logerror( 'Outbound SMTP Socket type unknown or undefined: ' . $smtp_conf->{'sock_type'} );
928 0 return 0;
929 }
930  
931 36 ▶ if ( ! $sock ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 0 36 36
932 0 $self->logerror( "Could not open outbound SMTP socket: $!" );
933 0 return 0;
934 }
935 36 eval {
936 36 $line = <$sock>;
937 };
938 36 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 36 36
939 0 $self->logerror( "Outbound SMTP Read Error: $error" );
940 0 return 0;
941 }
942 36 alarm( 0 );
943  
944 36 ▶ unless ( $line =~ /^220/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^220/u) 0 36 36
945 0 $self->logerror( "Unexpected SMTP response $line" );
946 0 return 0;
947 }
948  
949 36 $smtp->{'destination_sock'} = $sock;
950 }
951  
952 41 $self->logdebug( 'Sending envelope to destination' );
953  
954 41 ▶ if ( $new_sock ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_sock) { } 36 5 41
955 36 ▶ $self->send_smtp_packet( $sock, 'EHLO ' . $smtp->{'server_name'}, '250' ) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->send_smtp_packet($sock, 'EHLO ' . $smtp->{"server_name"}, 250) or return 36 0 36
956 }
957 else {
958 5 ▶ $self->send_smtp_packet( $sock, 'RSET', '250' ) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->send_smtp_packet($sock, "RSET", 250) or return 5 0 5
959 }
960  
961 41 ▶ if ( $smtp->{'fwd_helo_host'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"fwd_helo_host"}) 41 0 41
962 41 ▶ $self->send_smtp_packet( $sock, 'XFORWARD HELO=' . $smtp->{'fwd_helo_host'}, '250' ) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->send_smtp_packet($sock, 'XFORWARD HELO=' . $smtp->{"fwd_helo_host"}, 250) or return 41 0 41
963 }
964 41 ▶ if ( $smtp->{'fwd_connect_ip'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"fwd_connect_ip"}) 41 0 41
965 41 ▶ $self->send_smtp_packet( $sock, 'XFORWARD ADDR=' . $smtp->{'fwd_connect_ip'}, '250' ) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->send_smtp_packet($sock, 'XFORWARD ADDR=' . $smtp->{"fwd_connect_ip"}, 250) or return 41 0 41
966 }
967 41 ▶ if ( $smtp->{'fwd_connect_host'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"fwd_connect_host"}) 41 0 41
968 41 ▶ $self->send_smtp_packet( $sock, 'XFORWARD NAME=' . $smtp->{'fwd_connect_host'}, '250' ) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->send_smtp_packet($sock, 'XFORWARD NAME=' . $smtp->{"fwd_connect_host"}, 250) or return 41 0 41
969 }
970 41 ▶ if ( $smtp->{'fwd_ident'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"fwd_ident"}) 0 41 41
971 0 ▶ $self->send_smtp_packet( $sock, 'XFORWARD IDENT=' . $smtp->{'fwd_ident'}, '250' ) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->send_smtp_packet($sock, 'XFORWARD IDENT=' . $smtp->{"fwd_ident"}, 250) or return 0 0 0
972 }
973  
974 41 ▶ $self->send_smtp_packet( $sock, 'MAIL FROM:' . $smtp->{'mail_from'}, '250' ) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->send_smtp_packet($sock, 'MAIL FROM:' . $smtp->{"mail_from"}, 250) or return 41 0 41
975 41 foreach my $rcpt_to ( @{ $smtp->{'lmtp_rcpt'} } ) {
976 41 ▶ $self->send_smtp_packet( $sock, 'RCPT TO:' . $rcpt_to, '250' ) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->send_smtp_packet($sock, 'RCPT TO:' . $rcpt_to, 250) or return 41 0 41
977 }
978  
979 41 $self->logdebug( 'Sending data to destination' );
980 41 ▶ $self->send_smtp_packet( $sock, 'DATA', '354' ) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->send_smtp_packet($sock, "DATA", 354) or return 41 0 41
981  
982 41 $self->smtp_status('smtp.o.body');
983 41 my $email = q{};
984 41 foreach my $header ( @{ $smtp->{'headers'} } ) {
985 601 $email .= "$header\r\n";
986 }
987 41 $email .= "\r\n";
988  
989 41 my $spool = $smtp->{'spool'};
990 41 ▶ if ( $spool ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spool) { } 0 41 41
991  
992 # Handle transparency - should not be any in headers, but for completeness
993 0 $email =~ s/\015\012\./\015\012\.\./g;
994  
995 0 print $sock $email;
996  
997 0 seek( $spool, 0, 0 );
998 0 ▶ while ( my $line = <$spool> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $spool)) 0 0 0
999 0 $line =~ s/\015?\012/\015\012/g;
1000 0 $line =~ s/^\./\.\./g;
1001 0 print $sock $line;
1002 }
1003  
1004 }
1005 else {
1006 41 my $body = $smtp->{'body'};
1007 41 $body =~ s/\015?\012/\015\012/g;
1008 41 $email .= $body;
1009  
1010 # Handle transparency
1011 41 $email =~ s/\015\012\./\015\012\.\./g;
1012  
1013 41 print $sock $email;
1014 }
1015  
1016 41 $self->logdebug( 'Sending end to destination' );
1017 41 ▶ $self->send_smtp_packet( $sock, '.', '250' ) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->send_smtp_packet($sock, ".", 250) or return 41 0 41
1018 41 $self->logdebug( 'Sent to destination' );
1019 41 $self->smtp_status('smtp.o.done');
1020  
1021 41 return 1;
1022 }
1023  
1024 37 ▶sub close_destination_socket {
Subroutine close_destination_socket: called CC=3 SCAR=11.0
Pod documented
1025 37 my ( $self ) = @_;
1026 37 my $smtp = $self->{'smtp'};
1027 37 my $sock = $smtp->{'destination_sock'};
1028 37 ▶ return if ! $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 1 36 37
1029 36 ▶ $self->send_smtp_packet( $sock, 'QUIT', '221' ) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->send_smtp_packet($sock, "QUIT", 221) or return 36 0 36
1030 36 $sock->close();
1031 36 delete $smtp->{'destination_sock'};
1032 }
1033  
1034 364 ▶sub send_smtp_packet {
Subroutine send_smtp_packet: called CC=6 SCAR=19.6
Pod documented
1035 364 my ( $self, $socket, $send, $expect ) = @_;
1036 364 my $smtp = $self->{'smtp'};
1037  
1038 364 my $status = lc $send;
1039 364 $status =~ s/^([^ ]+) .*$/$1/;
1040 364 ▶ $status = 'dot' if $status eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "." 41 323 364
1041 364 $self->smtp_status('smtp.o.' . $status);
1042  
1043 364 print $socket "$send\r\n";
1044  
1045 364 $self->smtp_status('smtp.o.' . $status . '.wait');
1046  
1047 0 ▶ local $SIG{'ALRM'} = sub{ die "Timeout\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
1048 364 alarm( $smtp->{'smtp_timeout_out'} );
1049 364 my $recv;
1050 364 eval {
1051 364 $recv = <$socket>;
1052 364 $self->extra_debugging( "RAW DEBUG: ". $recv );
1053 364 ▶ while ( $recv =~ /^\d\d\d\-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recv =~ /^\d\d\d\-/u) 0 364 364
1054 0 $self->smtp_status('smtp.o.' . $status . '.waitext');
1055 0 $recv = <$socket>;
1056 0 $self->extra_debugging( "RAW DEBUG: ". $recv );
1057 }
1058 };
1059 364 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 364 364
1060 0 $self->logerror( "Outbound SMTP Read Error: $error" );
1061 0 $smtp->{'string'} = $error;
1062 0 return 0;
1063 }
1064 364 alarm( 0 );
1065 364 $self->smtp_status('smtp.o');
1066  
1067 364 ▶ $smtp->{'string'} = $recv || q{};
Condition50%1/2
$recv || ''
l!l
3640
MC/DC0%0/1
$recv || ''
$recv
Truth table50%1/2
A
A= $recv
Aresult
11
00
1068 364 $smtp->{'string'} =~ s/\r//g;
1069 364 $smtp->{'string'} =~ s/\n//g;
1070  
1071 364 ▶ if ( $recv =~ /^$expect/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recv =~ /^$expect/u) { } 364 0 364
1072 364 return 1;
1073 }
1074 else {
1075 0 $self->logerror( "SMTP Send expected $expect received $recv when sending $send" );
1076 0 return 0;
1077 }
1078 }
1079  
1080 15 ▶sub add_header {
Subroutine add_header: called CC=1 SCAR=0.0
Pod documented
1081 15 my ( $self, $header, $value ) = @_;
1082 15 my $smtp = $self->{'smtp'};
1083 15 $value =~ s/\015?\012/\015\012/g;
1084 15 push @{ $smtp->{'headers'} } , "$header: $value";
1085 }
1086  
1087 23 ▶sub change_header {
Subroutine change_header: called CC=6 SCAR=18.0
Pod documented
1088 23 my ( $self, $header, $index, $value ) = @_;
1089 23 my $smtp = $self->{'smtp'};
1090  
1091 23 my $header_i = 0;
1092 23 my $search_i = 0;
1093 23 my $result_i;
1094  
1095 HEADER:
1096 23 foreach my $header_v ( @{ $smtp->{'headers'} } ) {
1097 # Derive the field name exactly as smtp_command_data does before the
1098 # header callbacks, so that handler occurrence indexes line up with
1099 # ours (e.g. "Authentication-Results : ..." is counted by handlers)
1100 144 my ( $hkey ) = split( ':', $header_v, 2 );
1101 144 $hkey =~ s/^\s+//;
1102 144 $hkey =~ s/\s+$//;
1103 144 ▶ if ( lc $hkey eq lc $header ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $hkey eq lc $header) 55 89 144
1104 55 $search_i ++;
1105 55 ▶ if ( $search_i == $index ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search_i == $index) 18 37 55
1106 18 $result_i = $header_i;
1107 18 last HEADER;
1108 }
1109 }
1110 126 $header_i ++;
1111 }
1112  
1113 23 ▶ if ( defined $result_i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result_i) 18 5 23
1114 18 ▶ if ( $value eq q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq '') { } 18 0 18
1115 18 splice @{ $smtp->{'headers'} }, $result_i, 1;
1116 }
1117 else {
1118 0 $value =~ s/\015?\012/\015\012/g;
1119 0 $smtp->{'headers'}->[ $result_i ] = "$header: $value";
1120 #untested.
1121 }
1122 }
1123 }
1124  
1125 497 ▶sub insert_header {
Subroutine insert_header: called CC=1 SCAR=0.0
Pod documented
1126 497 my ( $self, $index, $key, $value ) = @_;
1127 497 my $smtp = $self->{'smtp'};
1128 497 $value =~ s/\015?\012/\015\012/g;
1129 497 splice @{ $smtp->{'headers'} }, $index - 1, 0, "$key: $value";
1130 }
1131  
1132 146 1;
1133  
1134 __END__
« Authentication/Net/ServerPatches.pm ↑ Summary Authentication/Handler/SPF.pm »