Authentication/Protocol/SMTP.pm

filter: Statementstmt 70.1% 467 / 666 Branchbran 52.9% 145 / 274 Conditioncond 52.5% 21 / 40 MC/DCmcdc 25.0% 6 / 24 Subroutinesub 86.8% 33 / 38 Podpod 92.0% 23 / 25 totaltotal 65.1% 695 / 1067 CC 164 SCAR 43.3
CC
164
Cov
65%
smtp_command_data
219.3
smtp_command_mailfrom
72.6
smtp_forward_to_destination
59.2
CRAP 75.9
« Authentication/Metric.pm ↑ Summary Authentication/Protocol/Milter.pm »
1 package Mail::Milter::Authentication::Protocol::SMTP;
2 142 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 142 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 142 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 142 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: SMTP protocol handling
7 142 our $VERSION = '4.20261112'; # VERSION
8 142 ▶use Digest::MD5 qw{ md5_hex };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 142 ▶use Email::Date::Format qw{ email_date };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 142 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 142 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 142 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 142 ▶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 211 ▶sub smtp_init {
Subroutine smtp_init: called CC=2 SCAR=6.9
Pod documented
49 211 my ( $self ) = @_;
50  
51 211 ▶ return if $self->{'smtp'}->{'init_required'} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"smtp"}{"init_required"} == 0 168 43 211
52  
53 43 my $handler = $self->{'handler'}->{'_Handler'};
54 43 my $smtp = $self->{'smtp'};
55  
56 43 $handler->set_symbol( 'C', 'j', $smtp->{'server_name'} );
57 43 $handler->set_symbol( 'C', '{rcpt_host}', $smtp->{'server_name'} );
58  
59 43 $smtp->{'queue_id'} = substr( uc md5_hex( "Authentication Milter Client $PID " . time() . rand(100) ) , -11 );
60 43 $handler->set_symbol( 'C', 'i', $self->smtp_queue_id() );
61  
62 43 $smtp->{'count'}++ ;
63 43 $handler->dbgout( 'SMTP Transaction count', $self->{'count'} . '.' . $smtp->{'count'} , LOG_DEBUG );
64  
65 43 $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 247 ▶sub command_param {
Subroutine command_param: called CC=2 SCAR=7.0
Pod documented
216 247 my ( $command, $index ) = @_;
217 247 my $p = q{};
218 247 ▶ if ( length( $command ) >= $index ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $command >= $index) 247 0 247
219 247 $p = substr( $command, $index );
220 }
221 247 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 84 ▶sub split_command_params {
Subroutine split_command_params: called CC=2 SCAR=7.0
Pod undocumented
227 84 my ( $line ) = @_;
228 84 $line =~ s/^\s+//;
229 84 ▶ my $path_end = find_path_end( $line ) || length( $line );
Condition66%2/3
find_path_end($line) || length $line
l!l&&r!l&&!r
8301
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 84 my $path = substr( $line, 0, $path_end );
231 84 $line = substr( $line, $path_end );
232 84 $line =~ s/^\s+//;
233 84 return ( $path, split( /\s+/, $line ) );
234 }
235  
236 84 ▶sub find_path_end {
Subroutine find_path_end: called CC=13 SCAR=27.4
Pod undocumented
237 84 my ( $input ) = @_;
238 84 my $len = length( $input );
239 84 my @qstack;
240 84 ▶ for ( my $i = 0; $i < $len; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 1567 83 1650
241 1567 my $c = substr( $input, $i, 1 );
242 1567 ▶ if ( @qstack && $c eq $qstack[-1] ) {
Branch: 5 branches
BranchTrueFalseTotal
if (@qstack and $c eq $qstack[-1]) { } 16 1551 1567
elsif ($c eq '"') { } 0 1551 1551
elsif ($c eq '<' and !@qstack || $qstack[-1] eq '>') { } 16 1535 1551
elsif ($c eq '\\' and @qstack) { } 0 1535 1535
elsif ($c eq ' ' and not @qstack) { } 1 3067 3068
Condition100%3/3
@qstack and $c eq $qstack[-1]
!ll&&!rl&&r
125229916
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 16 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
1535016
not @qstack or $qstack[-1] eq ">"
l!l&&r!l&&!r
1600
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 16 push @qstack, '>';
251 ▶ } elsif ( $c eq '\\' && @qstack ) {
Condition33%1/3
$c eq "\\" and @qstack
!ll&&!rl&&r
153500
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
153311
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 83 ▶ return ( @qstack ? undef : $len );
Branch: 1 branch
BranchTrueFalseTotal
@qstack ? : 0 83 83
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 6 ▶sub smtp_command_rset {
Subroutine smtp_command_rset: called CC=4 SCAR=14.0
Pod documented
376 6 my ( $self, $command ) = @_;
377 6 my $smtp = $self->{'smtp'};
378 6 my $socket = $self->{'socket'};
379 6 $self->smtp_status('smtp.i.rset');
380 6 $smtp->{'mail_from'} = q{};
381 6 $smtp->{'headers'} = [];
382 6 $smtp->{'body'} = q{};
383 6 $smtp->{'has_data'} = 0;
384 6 $smtp->{'has_mail_from'} = 0;
385 6 $smtp->{'fwd_connect_host'} = undef;
386 6 $smtp->{'fwd_connect_ip'} = undef;
387 6 $smtp->{'fwd_helo_host'} = undef;
388 6 $smtp->{'fwd_ident'} = undef;
389 6 $smtp->{'lmtp_rcpt'} = [];
390 6 $smtp->{'string'} = q{};
391 6 $self->{'handler'}->{'_Handler'}->top_close_callback();
392  
393 6 $smtp->{'init_required'} = 1;
394 6 $self->smtp_init();
395  
396 6 my $smtp_conf = $self->get_smtp_config();
397  
398 6 my $handler = $self->{'handler'}->{'_Handler'};
399 6 ▶ if ( $handler->{'exit_on_close'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler->{"exit_on_close"}) 0 6 6
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 6 ▶ if ( $smtp_conf->{'pipeline_limit'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp_conf->{"pipeline_limit"}) 6 0 6
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 5 print $socket "250 2.0.0 Ok\r\n";
416 }
417  
418 42 ▶sub smtp_command_mailfrom {
Subroutine smtp_command_mailfrom: called CC=16 SCAR=42.8
Pod documented
419 42 my ( $self, $command ) = @_;
420 42 my $smtp = $self->{'smtp'};
421 42 my $socket = $self->{'socket'};
422 42 my $handler = $self->{'handler'}->{'_Handler'};
423 42 $self->smtp_status('smtp.i.mailfrom');
424  
425 42 my $returncode;
426 42 ▶ if ( $smtp->{'has_data'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"has_data"}) 0 42 42
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 42 ▶ if ( $smtp->{'has_mail_from'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"has_mail_from"}) 0 42 42
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 42 ▶ my $host = $smtp->{'fwd_connect_host'} || $smtp->{'connect_host'};
Condition33%1/3
$smtp->{'fwd_connect_host'} || $smtp->{'connect_host'}
l!l&&r!l&&!r
4200
MC/DC0%0/2
$smtp->{'fwd_connect_host'} || $smtp->{'connect_host'}
$smtp->{'fwd_connect_host'} $smtp->{'connect_host'}
Truth table33%1/3
A || B
A= $smtp->{'fwd_connect_host'}B= $smtp->{'connect_host'}
ABresult
1X1
011
000
439 42 ▶ my $ip = $smtp->{'fwd_connect_ip'} || $smtp->{'connect_ip'};
Condition33%1/3
$smtp->{'fwd_connect_ip'} || $smtp->{'connect_ip'}
l!l&&r!l&&!r
4200
MC/DC0%0/2
$smtp->{'fwd_connect_ip'} || $smtp->{'connect_ip'}
$smtp->{'fwd_connect_ip'} $smtp->{'connect_ip'}
Truth table33%1/3
A || B
A= $smtp->{'fwd_connect_ip'}B= $smtp->{'connect_ip'}
ABresult
1X1
011
000
440 42 ▶ my $helo = $smtp->{'fwd_helo_host'} || $smtp->{'helo_host'};
Condition33%1/3
$smtp->{'fwd_helo_host'} || $smtp->{'helo_host'}
l!l&&r!l&&!r
4200
MC/DC0%0/2
$smtp->{'fwd_helo_host'} || $smtp->{'helo_host'}
$smtp->{'fwd_helo_host'} $smtp->{'helo_host'}
Truth table33%1/3
A || B
A= $smtp->{'fwd_helo_host'}B= $smtp->{'helo_host'}
ABresult
1X1
011
000
441  
442 42 ▶ if ( substr( $ip, 0, 5 ) eq 'IPv6:' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($ip, 0, 5) eq "IPv6:") 0 42 42
443 0 $ip = substr( $ip, 5 );
444 }
445  
446 # Do connection remapping first
447 42 $handler->remap_connect_callback( $host, Net::IP->new( $ip ) );
448 42 $handler->remap_helo_callback( $helo );
449  
450 42 $self->logdebug( "Inbound IP Address " . $handler->{'ip_object'}->short() );
451 42 $returncode = $handler->top_connect_callback( $host, $handler->{'ip_object'} );
452 42 ▶ if ( $returncode == SMFIS_CONTINUE ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($returncode == 100) { } 42 0 42
elsif (my $reject_reason = $handler->get_reject_mail) { } 0 0 0
elsif (my $defer_reason = $handler->get_defer_mail) { } 0 0 0
453 42 $returncode = $handler->top_helo_callback( $handler->{'helo_name'} );
454 42 ▶ if ( $returncode == SMFIS_CONTINUE ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($returncode == 100) { } 42 0 42
elsif (my $reject_reason = $handler->get_reject_mail) { } 0 0 0
elsif (my $defer_reason = $handler->get_defer_mail) { } 0 0 0
455 42 my $envfrom = command_param( $command,10 );
456 42 $smtp->{'mail_from'} = $envfrom;
457 42 $returncode = $handler->top_envfrom_callback( split_command_params( $envfrom ) );
458 42 ▶ if ( $returncode == SMFIS_CONTINUE ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($returncode == 100) { } 42 0 42
elsif (my $reject_reason = $handler->get_reject_mail) { } 0 0 0
elsif (my $defer_reason = $handler->get_defer_mail) { } 0 0 0
459 42 $smtp->{'has_mail_from'} = 1;
460 42 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 42 ▶sub smtp_command_rcptto {
Subroutine smtp_command_rcptto: called CC=5 SCAR=21.7
Pod documented
506 42 my ( $self, $command ) = @_;
507 42 my $smtp = $self->{'smtp'};
508 42 my $socket = $self->{'socket'};
509 42 my $handler = $self->{'handler'}->{'_Handler'};
510 42 $self->smtp_status('smtp.i.rcptto');
511  
512 42 ▶ if ( $smtp->{'has_data'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"has_data"}) 0 42 42
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 42 my $envrcpt = command_param( $command,8 );
518 42 my $returncode = $handler->top_envrcpt_callback( split_command_params( $envrcpt ) );
519 42 ▶ if ( $returncode == SMFIS_CONTINUE ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($returncode == 100) { } 42 0 42
elsif (my $reject_reason = $handler->get_reject_mail) { } 0 0 0
elsif (my $defer_reason = $handler->get_defer_mail) { } 0 0 0
520 42 push @{ $smtp->{'lmtp_rcpt'} }, $envrcpt;
521 42 print $socket "250 2.0.0 Ok\r\n";
522 }
523 elsif ( my $reject_reason = $handler->get_reject_mail() ) {
524 0 $handler->clear_reject_mail();
525 0 $self->loginfo ( "SMTPReject: $reject_reason" );
526 0 print $socket $reject_reason . "\r\n";
527 }
528 elsif ( my $defer_reason = $handler->get_defer_mail() ) {
529 0 $handler->clear_defer_mail();
530 0 $self->loginfo ( "SMTPDefer: $defer_reason" );
531 0 print $socket $defer_reason . "\r\n";
532 }
533 else {
534 0 print $socket "451 4.0.3 That's not right\r\n";
535 }
536 }
537  
538 42 ▶sub smtp_command_data {
Subroutine smtp_command_data: called CC=51 SCAR=53.9
Pod documented
539 42 my ( $self, $command ) = @_;
540 42 my $smtp = $self->{'smtp'};
541 42 my $socket = $self->{'socket'};
542 42 my $handler = $self->{'handler'}->{'_Handler'};
543 42 $self->smtp_status('smtp.i.data');
544  
545 42 my $body = q{};
546 42 my $done = 0;
547 42 my $fail = 0;
548 42 my @header_split;
549 my $returncode;
550  
551 42 ▶ if ( ! scalar @{ $smtp->{'lmtp_rcpt'} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$smtp->{"lmtp_rcpt"};}) 0 42 42
552 0 $self->logerror( "No valid recipients" );
553 0 print $socket "554 No valid recipients\r\n";
554 0 return;
555 }
556  
557 42 ▶ if ( $smtp->{'has_data'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"has_data"}) 0 42 42
558 0 $self->logerror( "Repeated SMTP DATA command: $command" );
559 0 print $socket "503 5.5.2 One at a time please\r\n";
560 0 return;
561 }
562 42 print $socket "354 2.0.0 Send body\r\n";
563  
564 0 ▶ local $SIG{'ALRM'} = sub{ die "Timeout\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
565 42 eval{
566 42 alarm( $smtp->{'smtp_timeout_in'} );
567 HEADERS:
568 42 ▶ while ( my $dataline = <$socket> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $dataline = readline $socket)) 1518 0 1518
569 1518 $self->extra_debugging( "RAW DEBUG: ". $dataline );
570 1518 alarm( 0 );
571 1518 $dataline =~ s/\r?\n$//;
572 1518 ▶ if ( $dataline eq '.' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataline eq ".") 0 1518 1518
573 0 $done = 1;
574 0 last HEADERS;
575 }
576 # Handle transparency
577 1518 ▶ if ( $dataline =~ /^\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataline =~ /^\./u) 0 1518 1518
578 0 $dataline = substr( $dataline, 1 );
579 }
580 1518 ▶ if ( $dataline eq q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataline eq "") 42 1476 1518
581 42 last HEADERS;
582 }
583 1476 push @header_split, $dataline;
584 1476 alarm( $smtp->{'smtp_timeout_in'} );
585 }
586 };
587 42 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 42 42
588 0 $self->logerror( "Read Error: $error" );
589 0 $done = 1;
590 0 $fail = 1;
591 }
592 42 alarm( 0 );
593  
594 42 $self->smtp_status('smtp.i.data.process');
595 42 my $value = q{};
596 42 foreach my $header_line ( @header_split ) {
597 1476 ▶ if ( $header_line =~ /^\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header_line =~ /^\s/u) { } 985 491 1476
598 985 $value .= "\r\n" . $header_line;
599 }
600 else {
601 491 ▶ if ( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 449 42 491
602 449 push @{ $smtp->{'headers'} } , $value;
603 449 my ( $hkey, $hvalue ) = split ( ':', $value, 2 );
604 449 $hvalue =~ s/^\s+//;
605 449 $hkey =~ s/^\s+//;
606 449 $hkey =~ s/\s+$//;
607 449 ▶ if ( ! $fail ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fail) 449 0 449
608 449 my $returncode = $handler->top_header_callback( $hkey, $hvalue, $value );
609 449 ▶ if ( $returncode != SMFIS_CONTINUE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returncode != 100) 0 449 449
610 0 $fail = 1;
611 }
612 }
613 }
614 491 $value = $header_line;
615 }
616 }
617 42 ▶ if ( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 42 0 42
618 42 push @{ $smtp->{'headers'} } , $value;
619 42 my ( $hkey, $hvalue ) = split ( ':', $value, 2 );
620 42 $hvalue =~ s/^\s+//;
621 42 $hkey =~ s/^\s+//;
622 42 $hkey =~ s/\s+$//;
623 42 ▶ if ( ! $fail ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fail) 42 0 42
624 42 my $returncode = $handler->top_header_callback( $hkey, $hvalue, $value );
625 42 ▶ if ( $returncode != SMFIS_CONTINUE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returncode != 100) 0 42 42
626 0 $fail = 1;
627 }
628 }
629 }
630 42 ▶ if ( ! $fail ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fail) 42 0 42
631 42 $returncode = $handler->top_eoh_callback();
632 42 ▶ if ( $returncode != SMFIS_CONTINUE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returncode != 100) 0 42 42
633 0 $fail = 1;
634 }
635 }
636  
637 42 my $smtp_conf = $self->get_smtp_config();
638  
639 42 my $chunk_limit = 1048576; # Process in chunks no larger than...
640 42 ▶ if ( exists ( $smtp_conf->{ 'chunk_limit' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $smtp_conf->{"chunk_limit"}) 0 42 42
641 0 $chunk_limit = $smtp_conf->{ 'chunk_limit' };
642 }
643  
644 42 my $temp_file;
645 42 ▶ if ( exists ( $smtp_conf->{ 'temp_dir' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $smtp_conf->{"temp_dir"}) 0 42 42
646 # TODO use spool_dir to create this
647 # add config option to en/disable this
648 # allow dir to be overridden
649 0 $temp_file = File::Temp->new( DIR => $smtp_conf->{ 'temp_dir' } );
650 }
651  
652 42 $self->smtp_status('smtp.i.body');
653 42 my $body_chunk = q{};
654 42 ▶ if ( ! $done ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 42 0 42
655 42 eval {
656 42 alarm( $smtp->{'smtp_timeout_in'} );
657 DATA:
658 42 ▶ while ( my $dataline = <$socket> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $dataline = readline $socket)) 716 0 716
659 716 $self->extra_debugging( "RAW DEBUG: ". $dataline );
660 716 alarm( 0 );
661 716 ▶ last DATA if $dataline =~ /^\.\r\n/;
Branch: 1 branch
BranchTrueFalseTotal
if $dataline =~ /^\.\r\n/u 42 674 716
662 # Handle transparency
663 674 ▶ if ( $dataline =~ /^\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataline =~ /^\./u) 16 658 674
664 16 $dataline = substr( $dataline, 1 );
665 }
666  
667 674 ▶ if ( $temp_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($temp_file) { } 0 674 674
668 0 print $temp_file $dataline;
669 }
670 else {
671 674 $body .= $dataline;
672 }
673  
674 674 ▶ if ( length( $body_chunk ) + length( $dataline ) > $chunk_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($body_chunk) + length($dataline) > $chunk_limit) 0 674 674
675 0 $returncode = $handler->top_body_callback( $body_chunk );
676 0 ▶ if ( $returncode != SMFIS_CONTINUE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returncode != 100) 0 0 0
677 0 $fail = 1;
678 }
679 0 $body_chunk = q{};
680 }
681  
682 674 $body_chunk .= $dataline;
683  
684 674 alarm( $smtp->{'smtp_timeout_in'} );
685 }
686 42 ▶ if ( ! $fail ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fail) 42 0 42
687 42 $returncode = $handler->top_body_callback( $body_chunk );
688 42 ▶ if ( $returncode != SMFIS_CONTINUE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returncode != 100) 0 42 42
689 0 $fail = 1;
690 }
691 42 $body_chunk = q{};
692 }
693 };
694 42 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 42 42
695 0 $self->logerror( "Read Error: $error" );
696 0 $done = 1;
697 0 $fail = 1;
698 }
699 42 alarm( 0 );
700 }
701  
702 42 ▶ if ( ! $fail ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fail) 42 0 42
703 42 $returncode = $handler->top_eom_callback();
704 42 ▶ if ( $returncode != SMFIS_CONTINUE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returncode != 100) 1 41 42
705 1 $fail = 1;
706 }
707 }
708  
709 42 $self->smtp_status('smtp.i.data.received');
710  
711 42 ▶ if ( ! $fail ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $fail) { } 41 1 42
elsif (my $reject_reason = $handler->get_reject_mail) { } 1 0 1
elsif (my $defer_reason = $handler->get_defer_mail) { } 0 0 0
712  
713 41 ▶ if ( $temp_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($temp_file) { } 0 41 41
714 0 $smtp->{'spool'} = $temp_file;
715 }
716 else {
717 41 $smtp->{'body'} = $body;
718 }
719  
720 41 ▶ if ( $self->smtp_forward_to_destination() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->smtp_forward_to_destination) { } 41 0 41
721  
722 41 ▶ if ( my $reject_reason = $handler->get_quarantine_mail() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $reject_reason = $handler->get_quarantine_mail) { } 5 36 41
723 5 $handler->metric_count( 'mail_processed_total', { 'result' => 'quarantined' } );
724 }
725 else {
726 36 $handler->metric_count( 'mail_processed_total', { 'result' => 'accepted' } );
727 }
728  
729 41 $handler->dbgout( 'Accept string', $smtp->{'string'}, LOG_INFO );
730 41 $smtp->{'has_data'} = 1;
731  
732 41 ▶ if ( $smtp->{'using_lmtp'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{'using_lmtp'}) { } 0 41 41
733 0 foreach my $rcpt_to ( @{ $smtp->{'lmtp_rcpt'} } ) {
734 0 print $socket "250 2.0.0 Queued as " . $self->smtp_queue_id() . "\r\n";
735 }
736 }
737 else {
738 41 print $socket "250 2.0.0 Queued as " . $self->smtp_queue_id() . "\r\n";
739 }
740 }
741 else {
742 0 $self->logerror( "SMTP Mail Rejected" );
743 0 my $error = '451 4.0.4 That\'s not right';
744 0 my $upstream_error = $smtp->{'string'};
745 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
746 0 $handler->metric_count( 'mail_processed_total', { 'result' => 'deferred' } );
747 0 $error = $upstream_error;
748 }
749 elsif ( $upstream_error =~ /^5\d\d / ) {
750 # Also pass back rejects
751 0 $handler->metric_count( 'mail_processed_total', { 'result' => 'rejected' } );
752 0 $error = $upstream_error;
753 }
754 else {
755 0 $handler->metric_count( 'mail_processed_total', { 'result' => 'deferred_error' } );
756 0 $error .= ': ' . $upstream_error;
757 }
758 0 ▶ if ( $smtp->{'using_lmtp'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{'using_lmtp'}) { } 0 0 0
759 0 foreach my $rcpt_to ( @{ $smtp->{'lmtp_rcpt'} } ) {
760 0 print $socket "$error\r\n";
761 }
762 }
763 else {
764 0 print $socket "$error\r\n";
765 }
766 }
767 }
768 elsif ( my $reject_reason = $handler->get_reject_mail() ) {
769 1 $handler->metric_count( 'mail_processed_total', { 'result' => 'rejected' } );
770 1 $handler->clear_reject_mail();
771 1 ▶ if ( $smtp->{'using_lmtp'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{'using_lmtp'}) { } 0 1 1
772 0 foreach my $rcpt_to ( @{ $smtp->{'lmtp_rcpt'} } ) {
773 0 $self->loginfo ( "SMTPReject: $reject_reason" );
774 0 print $socket $reject_reason . "\r\n";
775 }
776 }
777 else {
778 1 $self->loginfo ( "SMTPReject: $reject_reason" );
779 1 print $socket $reject_reason . "\r\n";
780 }
781 }
782 elsif ( my $defer_reason = $handler->get_defer_mail() ) {
783 0 $handler->metric_count( 'mail_processed_total', { 'result' => 'defered' } );
784 0 $handler->clear_defer_mail();
785 0 ▶ if ( $smtp->{'using_lmtp'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{'using_lmtp'}) { } 0 0 0
786 0 foreach my $rcpt_to ( @{ $smtp->{'lmtp_rcpt'} } ) {
787 0 $self->loginfo ( "SMTPDefer: $defer_reason" );
788 0 print $socket $defer_reason . "\r\n";
789 }
790 }
791 else {
792 0 $self->loginfo ( "SMTPDefer: $defer_reason" );
793 0 print $socket $defer_reason . "\r\n";
794 }
795 }
796 else {
797 0 $handler->metric_count( 'mail_processed_total', { 'result' => 'deferred_error' } );
798 0 ▶ if ( $smtp->{'using_lmtp'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{'using_lmtp'}) { } 0 0 0
799 0 foreach my $rcpt_to ( @{ $smtp->{'lmtp_rcpt'} } ) {
800 0 print $socket "451 4.0.5 That's not right\r\n";
801 }
802 }
803 else {
804 0 print $socket "451 4.0.6 That's not right\r\n";
805 }
806 }
807 42 $self->smtp_status('smtp.i.data.done');
808  
809 # Reset
810 42 $smtp->{'mail_from'} = q{};
811 42 $smtp->{'rcpt_to'} = [];
812 42 $smtp->{'headers'} = [];
813 42 $smtp->{'body'} = q{};
814 42 $smtp->{'has_data'} = 0;
815 42 $smtp->{'fwd_connect_host'} = undef;
816 42 $smtp->{'fwd_connect_ip'} = undef;
817 42 $smtp->{'fwd_helo_host'} = undef;
818 42 $smtp->{'lmtp_rcpt'} = [];
819 42 $smtp->{'string'} = q{};
820 42 $self->{'handler'}->{'_Handler'}->top_close_callback();
821 42 $smtp->{'init_required'} = 1;
822  
823 42 ▶ if ( $handler->{'exit_on_close'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler->{"exit_on_close"}) 0 42 42
824 0 $smtp->{'last_command'} = 1;
825 0 $handler->dbgout( 'EXIT ON CLOSE REQUESTED', 'NO MORE SMTP', LOG_INFO );
826 }
827 }
828  
829 41 ▶sub smtp_insert_received_header {
Subroutine smtp_insert_received_header: called CC=1 SCAR=0.0
Pod documented
830 41 my ( $self ) = @_;
831 41 my $smtp = $self->{'smtp'};
832  
833 my $value = join ( q{},
834  
835 'from ',
836 $smtp->{'helo_host'},
837 ' (',
838 $smtp->{'connect_host'}
839 ,
840 ' [',
841 $smtp->{'connect_ip'},
842 '])',
843 "\r\n",
844  
845 ' by ',
846 41 $smtp->{'server_name'},
847 ' (Authentication Milter)',
848 ' with ESMTP',
849 "\r\n",
850  
851 ' id ',
852 $self->smtp_queue_id(),
853 ';',
854 "\r\n",
855  
856 ' ',
857 email_date(),
858  
859 );
860  
861 41 splice @{ $smtp->{'headers'} }, 0, 0, 'Received: '. $value;
862 }
863  
864 41 ▶sub smtp_forward_to_destination {
Subroutine smtp_forward_to_destination: called CC=28 SCAR=40.8
Pod documented
865 41 my ( $self ) = @_;
866 41 $self->smtp_status('smtp.o');
867  
868 41 my $smtp = $self->{'smtp'};
869  
870 41 $self->smtp_insert_received_header();
871  
872 41 my $smtp_conf = $self->get_smtp_config();
873  
874 41 my $sock = $smtp->{'destination_sock'};
875  
876 41 my $new_sock = 0;
877  
878 41 my $line;
879  
880 41 ▶ if ( $sock ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock) 5 36 41
881 5 ▶ if ( ! $sock->connected() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock->connected) 0 5 5
882 0 $self->logerror( "Outbound SMTP socket was disconnected by remote end" );
883 0 undef $sock;
884 }
885 }
886  
887 41 ▶ if ( ! $sock ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 36 5 41
888 36 $new_sock = 1;
889 36 $self->smtp_status('smtp.o.open');
890  
891 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
892 $sock = IO::Socket::INET->new(
893 'Proto' => 'tcp',
894 'PeerAddr' => $smtp_conf->{'sock_host'},
895 0 'PeerPort' => $smtp_conf->{'sock_port'},
896 );
897 }
898 elsif ( $smtp_conf->{'sock_type'} eq 'unix' ) {
899 $sock = IO::Socket::UNIX->new(
900 36 'Peer' => $smtp_conf->{'sock_path'},
901 );
902 }
903 else {
904 0 $self->logerror( 'Outbound SMTP Socket type unknown or undefined: ' . $smtp_conf->{'sock_type'} );
905 0 return 0;
906 }
907  
908 36 ▶ if ( ! $sock ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 0 36 36
909 0 $self->logerror( "Could not open outbound SMTP socket: $!" );
910 0 return 0;
911 }
912 36 eval {
913 36 $line = <$sock>;
914 };
915 36 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 36 36
916 0 $self->logerror( "Outbound SMTP Read Error: $error" );
917 0 return 0;
918 }
919 36 alarm( 0 );
920  
921 36 ▶ unless ( $line =~ /^220/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^220/u) 0 36 36
922 0 $self->logerror( "Unexpected SMTP response $line" );
923 0 return 0;
924 }
925  
926 36 $smtp->{'destination_sock'} = $sock;
927 }
928  
929 41 $self->logdebug( 'Sending envelope to destination' );
930  
931 41 ▶ if ( $new_sock ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_sock) { } 36 5 41
932 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
933 }
934 else {
935 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
936 }
937  
938 41 ▶ if ( $smtp->{'fwd_helo_host'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"fwd_helo_host"}) 41 0 41
939 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
940 }
941 41 ▶ if ( $smtp->{'fwd_connect_ip'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"fwd_connect_ip"}) 41 0 41
942 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
943 }
944 41 ▶ if ( $smtp->{'fwd_connect_host'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"fwd_connect_host"}) 41 0 41
945 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
946 }
947 41 ▶ if ( $smtp->{'fwd_ident'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->{"fwd_ident"}) 0 41 41
948 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
949 }
950  
951 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
952 41 foreach my $rcpt_to ( @{ $smtp->{'lmtp_rcpt'} } ) {
953 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
954 }
955  
956 41 $self->logdebug( 'Sending data to destination' );
957 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
958  
959 41 $self->smtp_status('smtp.o.body');
960 41 my $email = q{};
961 41 foreach my $header ( @{ $smtp->{'headers'} } ) {
962 601 $email .= "$header\r\n";
963 }
964 41 $email .= "\r\n";
965  
966 41 my $spool = $smtp->{'spool'};
967 41 ▶ if ( $spool ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spool) { } 0 41 41
968  
969 # Handle transparency - should not be any in headers, but for completeness
970 0 $email =~ s/\015\012\./\015\012\.\./g;
971  
972 0 print $sock $email;
973  
974 0 seek( $spool, 0, 0 );
975 0 ▶ while ( my $line = <$spool> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $spool)) 0 0 0
976 0 $line =~ s/\015?\012/\015\012/g;
977 0 $line =~ s/^\./\.\./g;
978 0 print $sock $line;
979 }
980  
981 }
982 else {
983 41 my $body = $smtp->{'body'};
984 41 $body =~ s/\015?\012/\015\012/g;
985 41 $email .= $body;
986  
987 # Handle transparency
988 41 $email =~ s/\015\012\./\015\012\.\./g;
989  
990 41 print $sock $email;
991 }
992  
993 41 $self->logdebug( 'Sending end to destination' );
994 41 ▶ $self->send_smtp_packet( $sock, '.', '250' ) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->send_smtp_packet($sock, ".", 250) or return 41 0 41
995 41 $self->logdebug( 'Sent to destination' );
996 41 $self->smtp_status('smtp.o.done');
997  
998 41 return 1;
999 }
1000  
1001 37 ▶sub close_destination_socket {
Subroutine close_destination_socket: called CC=3 SCAR=11.0
Pod documented
1002 37 my ( $self ) = @_;
1003 37 my $smtp = $self->{'smtp'};
1004 37 my $sock = $smtp->{'destination_sock'};
1005 37 ▶ return if ! $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 1 36 37
1006 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
1007 36 $sock->close();
1008 36 delete $smtp->{'destination_sock'};
1009 }
1010  
1011 364 ▶sub send_smtp_packet {
Subroutine send_smtp_packet: called CC=6 SCAR=19.6
Pod documented
1012 364 my ( $self, $socket, $send, $expect ) = @_;
1013 364 my $smtp = $self->{'smtp'};
1014  
1015 364 my $status = lc $send;
1016 364 $status =~ s/^([^ ]+) .*$/$1/;
1017 364 ▶ $status = 'dot' if $status eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "." 41 323 364
1018 364 $self->smtp_status('smtp.o.' . $status);
1019  
1020 364 print $socket "$send\r\n";
1021  
1022 364 $self->smtp_status('smtp.o.' . $status . '.wait');
1023  
1024 0 ▶ local $SIG{'ALRM'} = sub{ die "Timeout\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
1025 364 alarm( $smtp->{'smtp_timeout_out'} );
1026 364 my $recv;
1027 364 eval {
1028 364 $recv = <$socket>;
1029 364 $self->extra_debugging( "RAW DEBUG: ". $recv );
1030 364 ▶ while ( $recv =~ /^\d\d\d\-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recv =~ /^\d\d\d\-/u) 0 364 364
1031 0 $self->smtp_status('smtp.o.' . $status . '.waitext');
1032 0 $recv = <$socket>;
1033 0 $self->extra_debugging( "RAW DEBUG: ". $recv );
1034 }
1035 };
1036 364 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 364 364
1037 0 $self->logerror( "Outbound SMTP Read Error: $error" );
1038 0 $smtp->{'string'} = $error;
1039 0 return 0;
1040 }
1041 364 alarm( 0 );
1042 364 $self->smtp_status('smtp.o');
1043  
1044 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
1045 364 $smtp->{'string'} =~ s/\r//g;
1046 364 $smtp->{'string'} =~ s/\n//g;
1047  
1048 364 ▶ if ( $recv =~ /^$expect/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recv =~ /^$expect/u) { } 364 0 364
1049 364 return 1;
1050 }
1051 else {
1052 0 $self->logerror( "SMTP Send expected $expect received $recv when sending $send" );
1053 0 return 0;
1054 }
1055 }
1056  
1057 15 ▶sub add_header {
Subroutine add_header: called CC=1 SCAR=0.0
Pod documented
1058 15 my ( $self, $header, $value ) = @_;
1059 15 my $smtp = $self->{'smtp'};
1060 15 $value =~ s/\015?\012/\015\012/g;
1061 15 push @{ $smtp->{'headers'} } , "$header: $value";
1062 }
1063  
1064 23 ▶sub change_header {
Subroutine change_header: called CC=6 SCAR=18.0
Pod documented
1065 23 my ( $self, $header, $index, $value ) = @_;
1066 23 my $smtp = $self->{'smtp'};
1067  
1068 23 my $header_i = 0;
1069 23 my $search_i = 0;
1070 23 my $result_i;
1071  
1072 HEADER:
1073 23 foreach my $header_v ( @{ $smtp->{'headers'} } ) {
1074 # Derive the field name exactly as smtp_command_data does before the
1075 # header callbacks, so that handler occurrence indexes line up with
1076 # ours (e.g. "Authentication-Results : ..." is counted by handlers)
1077 144 my ( $hkey ) = split( ':', $header_v, 2 );
1078 144 $hkey =~ s/^\s+//;
1079 144 $hkey =~ s/\s+$//;
1080 144 ▶ if ( lc $hkey eq lc $header ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $hkey eq lc $header) 55 89 144
1081 55 $search_i ++;
1082 55 ▶ if ( $search_i == $index ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search_i == $index) 18 37 55
1083 18 $result_i = $header_i;
1084 18 last HEADER;
1085 }
1086 }
1087 126 $header_i ++;
1088 }
1089  
1090 23 ▶ if ( defined $result_i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result_i) 18 5 23
1091 18 ▶ if ( $value eq q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq '') { } 18 0 18
1092 18 splice @{ $smtp->{'headers'} }, $result_i, 1;
1093 }
1094 else {
1095 0 $value =~ s/\015?\012/\015\012/g;
1096 0 $smtp->{'headers'}->[ $result_i ] = "$header: $value";
1097 #untested.
1098 }
1099 }
1100 }
1101  
1102 497 ▶sub insert_header {
Subroutine insert_header: called CC=1 SCAR=0.0
Pod documented
1103 497 my ( $self, $index, $key, $value ) = @_;
1104 497 my $smtp = $self->{'smtp'};
1105 497 $value =~ s/\015?\012/\015\012/g;
1106 497 splice @{ $smtp->{'headers'} }, $index - 1, 0, "$key: $value";
1107 }
1108  
1109 142 1;
1110  
1111 __END__
« Authentication/Metric.pm ↑ Summary Authentication/Protocol/Milter.pm »