lib/Mail/Decency/Core/POEForking/SMTP.pm

filter: Statementstmt 29.6% 35 / 118 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 57.8% 11 / 19 Podpod 100% 7 / 7 totaltotal 24.2% 53 / 219 CC 31 SCAR 55.7
CC
31
Cov
20%
smtp_input
552.0
smtp_flush
20.0
smtp_error
20.0
CRAP 263.5
« lib/Mail/Decency/Policy/Association.pm ↑ Summary lib/Mail/Decency/LogParser/Stats.pm »
1 package Mail::Decency::Core::POEForking::SMTP;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use version 0.74; our $VERSION = qv( "v0.1.5" );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use feature 'switch';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use base qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
11 Mail::Decency::Core::POEForking
12 9 /;
13  
14 9 ▶use POE qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
15 Wheel::ReadWrite
16 9 /;
17  
18 9 ▶use File::Temp qw/ tempfile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use Mail::Decency::ContentFilter::Core::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use Mail::Decency::Core::POEFilterSMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶use Socket qw/ inet_ntoa /;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 =head1 NAME
25  
26 Mail::Decency::Core::POEForking::SMTP
27  
28 =head1 DESCRIPTION
29  
30 SMTP Server for the content filter
31  
32 =head1 METHODS
33  
34 =head2 create_handler
35  
36 Called by the forking/treading parent server
37  
38 =cut
39  
40 0 ▶sub create_handler {
Subroutine create_handler: not called CC=1 SCAR=6.9
Pod documented
41 0 my $class = shift;
42 0 my ( $heap, $session, $socket, $peer_addr, $peer_port )
43 = @_[ HEAP, SESSION, ARG0, ARG1, ARG2 ];
44
45 POE::Session->create(
46 inline_states => {
47 _start => \&smtp_start,
48 _stop => \&smtp_stop,
49 smtp_input => \&smtp_input,
50 smtp_flush => \&smtp_flush,
51 smtp_error => \&smtp_error,
52 good_night => \&smtp_stop,
53 0 ▶ _parent => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 # _default => sub {
55 # my ($event, $args) = @_[ARG0, ARG1];
56 # warn "** SMTP: UNKNOWN EVENT $event, $args\n";
57 # }
58 },
59 heap => {
60 decency => $heap->{ decency },
61 conf => $heap->{ conf },
62 handler => $heap->{ decency }->get_handlers(),
63 logger => $heap->{ decency }->logger,
64 args => $heap->{ args },
65 0 parent => $session,
66 socket => $socket,
67 peer_addr => inet_ntoa( $peer_addr ),
68 peer_port => $peer_port,
69 }
70 );
71 }
72  
73  
74 =head2 smtp_start
75  
76 Start connection from postfix
77  
78 =cut
79  
80 0 ▶sub smtp_start {
Subroutine smtp_start: not called CC=1 SCAR=6.9
Pod documented
81 0 my ( $kernel, $session, $heap ) = @_[ KERNEL, SESSION, HEAP ];
82
83 # tell the parent session we are here
84 0 $kernel->post( $heap->{ parent }, "new_client_session", $session );
85
86 # start new r/w on the socket
87 $heap->{ client } = POE::Wheel::ReadWrite->new(
88 Handle => $heap->{ socket },
89 0 Filter => Mail::Decency::Core::POEFilterSMTP->new(),
90 InputEvent => "smtp_input",
91 ErrorEvent => "smtp_error",
92 FlushedEvent => "smtp_flush",
93 );
94
95 # say hello
96 0 $heap->{ client }->put( 220 => "Welcome" );
97 }
98  
99  
100 =head2 smtp_input
101  
102 Handling input
103  
104 =cut
105  
106 0 ▶sub smtp_input {
Subroutine smtp_input: not called CC=23 SCAR=63.1
Pod documented
107 0 my ( $kernel, $heap, $session, $input ) = @_[ KERNEL, HEAP, SESSION, ARG0 ];
108 0 ▶ return unless $input && ref( $input ) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $input and ref $input eq "ARRAY" 0 0 0
Condition0%0/3
$input and ref $input eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$input and ref $input eq "ARRAY"
$input ref $input eq "ARRAY"
Truth table0%0/3
A and B
A= $inputB= ref $input eq "ARRAY"
ABresult
0X0
100
111
109
110 # first pass parse
111 0 my ( $cmd, $arg, $line ) = @$input;
112 0 ▶ $arg ||= "";
Condition0%0/2
$arg ||= ""
l!l
00
MC/DC0%0/1
$arg ||= ""
$arg
Truth table0%0/2
A
A= $arg
Aresult
11
00
113
114 # being in the DATA part:
115 0 ▶ if ( $heap->{ in_data } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{'in_data'}) { } 0 0 0
116
117 # last final "." -> end of DATA
118 0 ▶ if ( $cmd eq '.' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq '.') { } 0 0 0
119
120 # close file handle
121 0 close delete $heap->{ mime_fh };
122
123 # unmark data
124 0 $heap->{ in_data } = 0;
125
126 # handle input data with decency
127 my ( $ok, $reject_message ) = $heap->{ handler }->( {
128 file => delete $heap->{ mime_file },
129 from => delete $heap->{ mail_from },
130 to => delete $heap->{ rcpt_to },
131 0 } );
132
133 # send bye to client
134 0 ▶ if ( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 0 0 0
135 0 $heap->{ decency }->logger->debug2( "Send 250 to postfix, mail accepted" );
136 0 $heap->{ client }->put( 250 => 'Bye ' );
137 }
138 else {
139 0 $heap->{ decency }->logger->debug2( "Send 554 to postfix, mail bounced" );
140 0 ▶ $heap->{ client }->put( 554 => $reject_message || "Rejected" );
Condition0%0/2
$reject_message || 'Rejected'
l!l
00
MC/DC0%0/1
$reject_message || 'Rejected'
$reject_message
Truth table0%0/2
A
A= $reject_message
Aresult
11
00
141 }
142
143 # close connection to postfix
144 delete $heap->{ $_ }
145 0 for qw/ mail_from rcpt_to mime_fh mime_file /; # client socket
146
147
148 # back to begin
149 0 $heap->{ in_data } = 0;
150 }
151
152 # collecting DATA
153 else {
154 0 my $fh = $heap->{ mime_fh };
155 0 print $fh $line;
156 }
157 }
158
159 # not in DATA -> catch SMTP commands
160 else {
161
162 0 ▶ warn "> $cmd | $arg\n" if $ENV{ DEBUG_SMTP };
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG_SMTP"} 0 0 0
163
164 # MAIL FROM commmand -> rewrite for handling
165 0 ▶ if ( $cmd eq 'MAIL' && $arg =~ /^FROM:\s*(?:<([^>]*?)>|(.*?))\s*$/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($cmd eq 'MAIL' and $arg =~ /^FROM:\s*(?:<([^>]*?)>|(.*?))\s*$/) { } 0 0 0
elsif ($cmd eq 'RCPT' and $arg =~ /^TO:\s*(?:<([^>]*?)>|(.*?))\s*$/) { } 0 0 0
elsif ($cmd eq 'DATA') { } 0 0 0
elsif ($cmd eq 'RSET') { } 0 0 0
Condition0%0/3
$cmd eq "MAIL" and $arg =~ /^FROM:\s*(?:<([^>]*?)>|(.*?))\s*$/
!ll&&!rl&&r
000
MC/DC0%0/2
$cmd eq "MAIL" and $arg =~ /^FROM:\s*(?:<([^>]*?)>|(.*?))\s*$/
$cmd eq "MAIL" $arg =~ /^FROM:\s*(?:<([^>]*?)>|(.*?))\s*$/
Truth table0%0/3
A and B
A= $cmd eq "MAIL"B= $arg =~ /^FROM:\s*(?:<([^>]*?)>|(.*?))\s*$/
ABresult
0X0
100
111
166 0 $cmd = 'MAIL_FROM';
167 0 ▶ $heap->{ mail_from } = $arg = $1 || $2 || "";
Condition0%0/5
$1 || $2 || ''
l!l
00
$1 or $2
l!l&&r!l&&!r
000
MC/DC0%0/2
$1 || $2 || ''
$1 $2
Truth table0%0/3
A or B
A= $1B= $2
ABresult
1X1
011
000
168 }
169
170 # RCPT TO commmand -> rewrite for handling
171 ▶ elsif ( $cmd eq 'RCPT' && $arg =~ /^TO:\s*(?:<([^>]*?)>|(.*?))\s*$/ ) {
Condition0%0/3
$cmd eq "RCPT" and $arg =~ /^TO:\s*(?:<([^>]*?)>|(.*?))\s*$/
!ll&&!rl&&r
000
MC/DC0%0/2
$cmd eq "RCPT" and $arg =~ /^TO:\s*(?:<([^>]*?)>|(.*?))\s*$/
$cmd eq "RCPT" $arg =~ /^TO:\s*(?:<([^>]*?)>|(.*?))\s*$/
Truth table0%0/3
A and B
A= $cmd eq "RCPT"B= $arg =~ /^TO:\s*(?:<([^>]*?)>|(.*?))\s*$/
ABresult
0X0
100
111
172 0 $cmd = 'RCPT_TO';
173 0 ▶ $heap->{ rcpt_to } = $arg = $1 || $2;
Condition0%0/3
$1 || $2
l!l&&r!l&&!r
000
MC/DC0%0/2
$1 || $2
$1 $2
Truth table0%0/3
A || B
A= $1B= $2
ABresult
1X1
011
000
174 }
175
176 # DATA commmand -> init data input
177 elsif ( $cmd eq 'DATA' ) {
178
179 # not heaving from and to ? not good -> bye to client
180 0 ▶ unless ( $heap->{ rcpt_to } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($heap->{"rcpt_to"}) 0 0 0
181
182 # clear heap
183 0 delete $heap->{ $_ } for qw/ mail_from /;
184
185 # send bye, close client
186 0 $heap->{ client }->put( 221 => 'Require RCPT TO' );
187
188 0 return;
189 }
190
191 # mark being in data
192 0 $heap->{ in_data } ++;
193
194 # new temp file in spool dir
195 0 my ( $th, $tn ) = tempfile( $heap->{ args }->{ temp_mask }, UNLINK => 0 );
196 0 $heap->{ mime_fh } = $th;
197 0 $heap->{ mime_file } = $tn;
198 }
199
200 # RSET .. not out fault ;)
201 elsif ( $cmd eq 'RSET' ) {
202 0 $heap->{ in_data } = 0;
203 close delete $heap->{ mime_fh }
204 0 ▶ if $heap->{ mime_fh };
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"mime_fh"} 0 0 0
205 0 delete $heap->{ $_ } for qw/ mime_file rcpt_to mail_from /;
206 }
207
208
209 0 smtp_response( $heap, $session, $cmd, $line );
210 }
211 }
212  
213  
214 =head2 smtp_stop
215  
216 Stop connection, good bye
217  
218 =cut
219  
220 0 ▶sub smtp_stop {
Subroutine smtp_stop: not called CC=3 SCAR=24.8
Pod documented
221 0 my ( $heap, $session ) = @_[ HEAP, SESSION ];
222
223 0 $heap->{ logger }->debug2( "Disconnecting from postfix" );
224 0 eval {
225 0 delete $heap->{ $_ } for qw/ client socket /;
226 };
227 0 ▶ $heap->{ logger }->error( "Could not remove client from list after postfix stop: $@" )
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
228 if $@;
229 #delete $heap->{ client } if $heap->{ client };
230 }
231  
232  
233 =head2 smtp_response
234  
235 Handle a SMTP input (eg RCPT TO)
236  
237 =cut
238  
239 0 ▶sub smtp_response {
Subroutine smtp_response: not called CC=1 SCAR=6.9
Pod documented
240 0 my ( $heap, $session, $cmd, $line ) = @_;
241
242 0 given ( $cmd ) {
243 0 when ( "EHLO" ) {
244 0 $heap->{ client }->put( 250 => 'XFORWARD NAME ADDR PROTO HELO', 'OK' );
245 }
246 0 when ( "HELO" ) {
247 0 $heap->{ client }->put( 250 => 'XFORWARD NAME ADDR PROTO HELO', 'OK' );
248 }
249 0 when ( "MAIL_FROM" ) {
250 0 $heap->{ client }->put( 250 => 'OK' );
251 }
252 0 when ( "RCPT_TO" ) {
253 0 $heap->{ client }->put( 250 => 'OK' );
254 }
255 0 when ( "DATA" ) {
256 0 $heap->{ client }->put( 354 => 'End with "." on a line by itself' );
257 }
258 0 when ( "QUIT" ) {
259 0 $heap->{ client }->put( 221 => 'Thanks for the fish' );
260 0 $heap->{ finished } ++;
261 }
262 0 when ( "RSET" ) {
263 0 $heap->{ client }->put( 250 => 'It\'s not my fault!' );
264 0 $heap->{ finished } ++;
265 }
266 0 default {
267 0 $heap->{ client }->put( 502 => 'This is not your postfix' );
268 }
269 }
270 }
271  
272  
273 =head2 smtp_flush
274  
275 =cut
276  
277 0 ▶sub smtp_flush {
Subroutine smtp_flush: not called CC=4 SCAR=30.0
Pod documented
278 0 my $heap = $_[ HEAP ];
279
280 0 ▶ if ( delete $heap->{ finished } && $heap->{ client } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $heap->{"finished"} and $heap->{"client"}) 0 0 0
Condition0%0/3
delete $heap->{"finished"} and $heap->{"client"}
!ll&&!rl&&r
000
MC/DC0%0/2
delete $heap->{"finished"} and $heap->{"client"}
delete $heap->{"finished"} $heap->{"client"}
Truth table0%0/3
A and B
A= delete $heap->{"finished"}B= $heap->{"client"}
ABresult
0X0
100
111
281 0 $heap->{ decency }->logger->debug2( "Got final flush from postfix. Delete client connection." );
282 0 eval { delete $heap->{ client }; };
283 0 ▶ $heap->{ decency }->logger->error( "Could not remove client from list after final flush: $@" ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
284
285 # eval { $heap->{ socket }->flush; };
286 # $heap->{ decency }->logger->error( "Could not flush socket after client removal: $@" ) if $@;
287 }
288 }
289  
290  
291  
292 =head2 smtp_error
293  
294 Called uppon client error .. eg sudden disconnect
295  
296 =cut
297  
298 0 ▶sub smtp_error {
Subroutine smtp_error: not called CC=4 SCAR=30.0
Pod documented
299 0 my ( $heap, $session, $operation, $errnum, $errstr, $id ) = @_[ HEAP, SESSION, ARG0..ARG3 ];
300
301 # disconnect ..
302 0 ▶ if ( $operation eq 'read' && $errnum == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operation eq 'read' and $errnum == 0) { } 0 0 0
Condition0%0/3
$operation eq "read" and $errnum == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$operation eq "read" and $errnum == 0
$operation eq "read" $errnum == 0
Truth table0%0/3
A and B
A= $operation eq "read"B= $errnum == 0
ABresult
0X0
100
111
303 0 $heap->{ decency }->logger->debug2( "Postfix closed connection" );
304 }
305
306 # real error ..
307 else {
308 0 $heap->{ decency }->logger->error( "Weird disconnection from postfix ". $session->ID. " (OP: $operation, ENUM: $errnum, ESTR: $errstr, WID: $id)" );
309 }
310
311 # close all sockets ..
312 0 eval {
313 #delete $heap->{ $_ } for qw/ client socket /; #
314 0 $heap->{ socket }->flush;
315 #delete $heap->{ client };
316 };
317 0 ▶ $heap->{ decency }->logger->error( "Could not remove client from list after weird disconnect: $@" )
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
318 if $@;
319
320
321 0 return ;
322 }
323  
324 =head1 AUTHOR
325  
326 Ulrich Kautz <uk@fortrabbit.de>
327  
328 =head1 COPYRIGHT
329  
330 Copyright (c) 2010 the L</AUTHOR> as listed above
331  
332 =head1 LICENCSE
333  
334 This library is free software and may be distributed under the same terms as perl itself.
335  
336 =cut
337  
338  
339  
340 9 1;
« lib/Mail/Decency/Policy/Association.pm ↑ Summary lib/Mail/Decency/LogParser/Stats.pm »