blib/lib/LaBrea/Tarpit/DShield.pm

filter: Statementstmt 68.5% 133 / 194 Branchbran 57.5% 69 / 120 Conditioncond 70.0% 42 / 60 MC/DCmcdc 55.5% 20 / 36 Subroutinesub 81.8% 9 / 11 Podpod 66.6% 4 / 6 totaltotal 64.8% 277 / 427 CC 86 SCAR 36.6
CC
86
Cov
65%
mail2_Q
72.0
move2_Q
55.5
slurp
41.4
CRAP 39.0
« blib/lib/LaBrea/Tarpit/Get.pm ↑ Summary blib/lib/LaBrea/Tarpit.pm »
1 #!/usr/bin/perl
2 package LaBrea::Tarpit::DShield;
3 #
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #use diagnostics;
6  
7 4 ▶use vars qw($VERSION @ISA @EXPORT_OK *deliver2_DShield);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Fcntl qw(:DEFAULT :flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Net::Netmask;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 $VERSION = do { my @r = (q$Revision: 0.08 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
12  
13 4 require Exporter;
14 4 @ISA = qw(Exporter);
15 4 @EXPORT_OK = qw(
16 chk_config
17 deliver2_DShield
18 process_Q
19 move2_Q
20 mail2_Q
21 );
22  
23 4 *deliver2_DShield = \&process_Q;
24  
25 =head1 NAME
26  
27 LaBrea::Tarpit::DShield
28  
29 =head1 SYNOPSIS
30  
31 use LaBrea::Tarpit::DShield qw ( .... );
32  
33 =head1 DESCRIPTION - LaBrea::Tarpit::DShield
34  
35 Module provides mail support to parse and send reports to B<dshield.org>
36  
37 $rv = chk_config(\%config);
38 $rv = mail2_Q(\%config,\$message,[subject]);
39 $rv = move2_Q(\$config);
40 $rv = deliver2_DShield(\%config);
41 $rv = process_Q(\%config);
42  
43 =over 4
44  
45 =item $rv = chk_config(\%config);
46  
47 Check and adjust default configuration parameters.
48  
49 Check for valid e-mail address formats and
50 add leading ./ to DShield file path if needed.
51  
52 input: \%config
53 returns: false on success
54 error msg on failure
55  
56 Note: UserID is checked in move2_Q
57  
58 my $config = {
59 'DShield' => 'tmp/DShield.cache', # path/to/file
60 'To' => 'test@dshield.org', # or report@dshield.org
61 'From' => 'john.doe@foo.com',
62 'Reply-To' => 'john.doe@foo.com', # optional
63 # optional
64 'Obfuscate' => 'complete or partial',
65 # optional - ignore reports about this netblock
66 # when generating DShield reports
67 'SrcIgnore' => ['10.11.12.0/23','10.11.16.0/23'],
68 # either one or more working SMTP server's
69 'smtp' => 'iceman.dshield.org,mail.euclidian.com',
70 # or a sendmail compatible mail transport command
71 'sendmail' => '/usr/lib/sendmail -t -oi',
72 ############ used only by "move2_Q"
73 'UserID' => '0', # DShield UserID
74 };
75  
76 Called internally by all routines, it's error codes are returned by them.
77  
78 =cut
79  
80 36 ▶sub chk_config {
Subroutine chk_config: called CC=19 SCAR=29.5
Pod documented
81 36 my ($cfg) = @_;
82 return "missing DShield queue directory"
83 36 ▶ unless $cfg->{DShield};
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg->{"DShield"} 2 34 36
84 # add leading ./ if missing
85 $cfg->{DShield} = './' . $cfg->{DShield}
86 34 ▶ unless $cfg->{DShield} =~ m|/|;
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg->{"DShield"} =~ m[/] 1 33 34
87  
88 34 my $emailfmt = '^.+\@.+\..+'; # required e-mail format
89 return "missing or invalid To: email format"
90 34 ▶ unless $cfg->{To} && $cfg->{To} =~ /$emailfmt/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg->{"To"} and $cfg->{"To"} =~ /$emailfmt/ 4 30 34
Condition100%3/3
$cfg->{"To"} and $cfg->{"To"} =~ /$emailfmt/
!ll&&!rl&&r
3130
MC/DC100%2/2
$cfg->{"To"} and $cfg->{"To"} =~ /$emailfmt/
$cfg->{"To"} $cfg->{"To"} =~ /$emailfmt/
Truth table100%3/3
A and B
A= $cfg->{"To"}B= $cfg->{"To"} =~ /$emailfmt/
ABresult
0X0
100
111
91 return "missing or invalid From: email format"
92 30 ▶ unless $cfg->{From} && $cfg->{From} =~ /$emailfmt/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg->{"From"} and $cfg->{"From"} =~ /$emailfmt/ 2 28 30
Condition100%3/3
$cfg->{"From"} and $cfg->{"From"} =~ /$emailfmt/
!ll&&!rl&&r
1128
MC/DC100%2/2
$cfg->{"From"} and $cfg->{"From"} =~ /$emailfmt/
$cfg->{"From"} $cfg->{"From"} =~ /$emailfmt/
Truth table100%3/3
A and B
A= $cfg->{"From"}B= $cfg->{"From"} =~ /$emailfmt/
ABresult
0X0
100
111
93 28 ▶ $cfg->{'Reply-To'} = $cfg->{From} unless exists $cfg->{'Reply-To'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cfg->{"Reply-To"} 1 27 28
94 return "invalid Reply-To: email format"
95 unless defined $cfg->{'Reply-To'} &&
96 28 ▶ $cfg->{'Reply-To'} =~ /$emailfmt/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cfg->{"Reply-To"} and $cfg->{"Reply-To"} =~ /$emailfmt/ 2 26 28
Condition100%3/3
defined $cfg->{"Reply-To"} and $cfg->{"Reply-To"} =~ /$emailfmt/
!ll&&!rl&&r
1126
MC/DC100%2/2
defined $cfg->{"Reply-To"} and $cfg->{"Reply-To"} =~ /$emailfmt/
defined $cfg->{"Reply-To"} $cfg->{"Reply-To"} =~ /$emailfmt/
Truth table100%3/3
A and B
A= defined $cfg->{"Reply-To"}B= $cfg->{"Reply-To"} =~ /$emailfmt/
ABresult
0X0
100
111
97 return "unknown Obfuscate word: '$cfg->{Obfuscate}'"
98 if $cfg->{Obfuscate} &&
99 ( $cfg->{Obfuscate} !~ /^partial$/i &&
100 26 ▶ $cfg->{Obfuscate} !~ /^complete$/i );
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->{"Obfuscate"} and not $cfg->{"Obfuscate"} =~ /^partial$/i || $cfg->{"Obfuscate"} =~ /^complete$/i 4 22 26
Condition100%6/6
$cfg->{"Obfuscate"} and not $cfg->{"Obfuscate"} =~ /^partial$/i || $cfg->{"Obfuscate"} =~ /^complete$/i
!ll&&!rl&&r
1664
$cfg->{'Obfuscate'} =~ /^partial$/i || $cfg->{'Obfuscate'} =~ /^complete$/i
l!l&&r!l&&!r
244
MC/DC100%3/3
$cfg->{"Obfuscate"} and not $cfg->{"Obfuscate"} =~ /^partial$/i || $cfg->{"Obfuscate"} =~ /^complete$/i
$cfg->{"Obfuscate"} $cfg->{'Obfuscate'} =~ /^partial$/i $cfg->{'Obfuscate'} =~ /^complete$/i
Truth table100%4/4
A and not(B || C)
A= $cfg->{"Obfuscate"}B= $cfg->{'Obfuscate'} =~ /^partial$/iC= $cfg->{'Obfuscate'} =~ /^complete$/i
ABCresult
0XX0
11X0
1010
1001
101 return "missing mail agent"
102 22 ▶ unless $cfg->{smtp} || $cfg->{sendmail};
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg->{"smtp"} or $cfg->{"sendmail"} 2 20 22
Condition100%3/3
$cfg->{"smtp"} or $cfg->{"sendmail"}
l!l&&r!l&&!r
1732
MC/DC100%2/2
$cfg->{"smtp"} or $cfg->{"sendmail"}
$cfg->{"smtp"} $cfg->{"sendmail"}
Truth table100%3/3
A or B
A= $cfg->{"smtp"}B= $cfg->{"sendmail"}
ABresult
1X1
011
000
103 return "sendmail agent missing or not executable"
104 if $cfg->{sendmail} &&
105 20 ▶ $cfg->{sendmail} =~ /^([\S]+)/ &&
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->{"sendmail"} and $cfg->{"sendmail"} =~ /^([\S]+)/ and not -e $1 and not -x $1 1 19 20
Condition77%7/9
$cfg->{"sendmail"} and $cfg->{"sendmail"} =~ /^([\S]+)/ and not -e $1 and not -x $1
!ll&&!rl&&r
1901
$cfg->{"sendmail"} and $cfg->{"sendmail"} =~ /^([\S]+)/ and not -e $1
!ll&&!rl&&r
1721
$cfg->{"sendmail"} and $cfg->{"sendmail"} =~ /^([\S]+)/
!ll&&!rl&&r
1703
MC/DC50%2/4
$cfg->{"sendmail"} and $cfg->{"sendmail"} =~ /^([\S]+)/ and not -e $1 and not -x $1
$cfg->{"sendmail"} $cfg->{"sendmail"} =~ /^([\S]+)/ not -e $1 not -x $1
Truth table60%3/5
A and B and not(C) and not(D)
A= $cfg->{"sendmail"}B= $cfg->{"sendmail"} =~ /^([\S]+)/C= not -e $1D= not -x $1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
106 ! -e $1 &&
107 ! -x $1;
108 19 return undef;
109 }
110
111 =item $rv = mail2_Q(\%config,\$message,[subject]);
112  
113 Queue a mail message as specified by
114 To, From, Reply-To, etc...
115  
116 subject is optional
117  
118 Must run 'process_Q' or 'deliver2_DShield'
119 to actually mail the message
120  
121  
122 =cut
123  
124 0 ▶sub mail2_Q {
Subroutine mail2_Q: not called CC=8 SCAR=42.8
Pod documented
125 0 my ($cfg,$mp,$sub) = @_;
126 0 ▶ return "no message content" unless $mp && $$mp =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $mp and $$mp =~ /\S/ 0 0 0
Condition0%0/3
$mp and $$mp =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
$mp and $$mp =~ /\S/
$mp $$mp =~ /\S/
Truth table0%0/3
A and B
A= $mpB= $$mp =~ /\S/
ABresult
0X0
100
111
127 0 ▶ $sub = 'not specified' unless $sub =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub =~ /\S/ 0 0 0
128 0 ▶ return $_ if ($_ = chk_config($cfg));
Branch: 1 branch
BranchTrueFalseTotal
if $_ = chk_config($cfg) 0 0 0
129 0 my $f = $cfg->{DShield};
130 0 $f =~ m|(.*/)|;
131 0 my $dir = $1;
132 0 ▶ return "queue directory $dir not writable"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir and -w $dir 0 0 0
Condition0%0/3
-d $dir and -w $dir
!ll&&!rl&&r
000
MC/DC0%0/2
-d $dir and -w $dir
-d $dir -w $dir
Truth table0%0/3
A and B
A= -d $dirB= -w $dir
ABresult
0X0
100
111
133 unless -d $dir && -w $dir;
134 0 local(*LOCK,*IN,*OUT);
135 # open mail.q.tmp
136 0 ▶ return $_ if ($_ = open_Q(*LOCK,*OUT,$dir . 'mail'));
Branch: 1 branch
BranchTrueFalseTotal
if $_ = open_Q(*LOCK, *OUT, $dir . 'mail') 0 0 0
137 # now format the Q file output
138 0 print OUT qq|From: $cfg->{From}
139 To: $cfg->{To}
140 Reply-To: $cfg->{'Reply-To'}
141 X-mailer: LaBrea-DShield $VERSION
142 Subject: $sub
143  
144 $$mp
145 |;
146 0 close OUT;
147 0 rename $dir . 'mail.q.tmp', $dir . 'qF'. time .'.'. $$ .'.'. 2;
148 0 close LOCK;
149 0 return undef;
150 }
151  
152 # helper routine opens Q files
153 #
154 # input: LOCK, OUT handle pointers, file name
155 # returns: error message on failure
156 # returns: undef on success
157 #
158
159 7 ▶sub open_Q {
Subroutine open_Q: called CC=4 SCAR=16.0
Pod undocumented
160 7 my ($LOCK,$OUT,$f) = @_;
161 7 ▶ return "failed to open lockfile ${f}.flock" unless
Branch: 1 branch
BranchTrueFalseTotal
unless sysopen $LOCK, $f . '.flock', 578 0 7 7
162 sysopen $LOCK, $f . '.flock', O_RDWR|O_CREAT|O_TRUNC;
163 7 ▶ unless (flock($LOCK,LOCK_SH)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $LOCK, 1) 0 6 6
164 0 close $LOCK;
165 0 return "failed to lock $f";
166 }
167 6 ▶ unless (open($OUT,'>'.${f}.'.q.tmp')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $OUT, '>' . $f . '.q.tmp') 0 6 6
168 0 close $LOCK;
169 0 return "failed to open $f.q.tmp for write";
170 }
171 6 $_ = select $OUT;
172 6 $| = 1;
173 6 select $_;
174 6 return undef;
175 }
176  
177 =item $rv = move2_Q(\$config,$debug);
178  
179 Prepare the DShield file for mailing and rename
180 as a B<Que's> file in preparation for mailing.
181  
182 UserID, From, To, [Reply-To], and Subject are added to the file and it is
183 renamed qF_unique_string.
184  
185 No queue file is generated if the list of connections are empty.
186 This could happen when using the SrcIgnore option.
187  
188 input: \%config,$debug
189 output: false on success or no action
190 true = error message
191  
192 NOTE: do not use debug mode with the mail
193 address pointing to DShield,
194 point it to yourself
195  
196 $debug = missing normal operation
197 $debug = 0 normal operation
198 $debug = 1 do not delete cache file
199 $debug = 2 do not rename q-file
200  
201 =cut
202  
203 # $debug = true, do not delete 'dshield.cache'
204 # $debug > 1, as above and do not rename 'dshield.cache.q.tmp'
205
206 18 ▶sub move2_Q {
Subroutine move2_Q: called CC=29 SCAR=40.2
Pod documented
207 18 my ($cfg,$debug) = @_;
208 return "missing DShield UserID"
209 unless exists $cfg->{UserID} &&
210 defined $cfg->{UserID} &&
211 18 ▶ $cfg->{UserID} !~ /\D/;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cfg->{"UserID"} and defined $cfg->{"UserID"} and not $cfg->{"UserID"} =~ /\D/ 3 15 18
Condition100%6/6
exists $cfg->{"UserID"} and defined $cfg->{"UserID"} and not $cfg->{"UserID"} =~ /\D/
!ll&&!rl&&r
2115
exists $cfg->{"UserID"} and defined $cfg->{"UserID"}
!ll&&!rl&&r
1116
MC/DC100%3/3
exists $cfg->{"UserID"} and defined $cfg->{"UserID"} and not $cfg->{"UserID"} =~ /\D/
exists $cfg->{"UserID"} defined $cfg->{"UserID"} not $cfg->{"UserID"} =~ /\D/
Truth table100%4/4
A and B and not(C)
A= exists $cfg->{"UserID"}B= defined $cfg->{"UserID"}C= not $cfg->{"UserID"} =~ /\D/
ABCresult
0XX0
10X0
1100
1111
212 15 my $tmp = chk_config($cfg);
213 15 ▶ return $tmp if $tmp; # return existing config errors
Branch: 1 branch
BranchTrueFalseTotal
if $tmp 4 11 15
214 11 my $f = $cfg->{DShield};
215 11 ▶ return undef unless -e $f && -r $f; # nothing to do
Branch: 1 branch
BranchTrueFalseTotal
unless -e $f and -r $f 2 9 11
Condition66%2/3
-e $f and -r $f
!ll&&!rl&&r
209
MC/DC50%1/2
-e $f and -r $f
-e $f -r $f
Truth table66%2/3
A and B
A= -e $fB= -r $f
ABresult
0X0
100
111
216 9 ▶ return "$f not a plain file" unless -f $f;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $f 2 7 9
217 7 $f =~ m|(.*/)|;
218 7 my $dir = $1;
219 7 ▶ return "queue directory $dir not writable"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir and -w $dir 0 7 7
Condition33%1/3
-d $dir and -w $dir
!ll&&!rl&&r
007
MC/DC0%0/2
-d $dir and -w $dir
-d $dir -w $dir
Truth table33%1/3
A and B
A= -d $dirB= -w $dir
ABresult
0X0
100
111
220 unless -d $dir && -w $dir;
221 7 local (*LOCK,*IN,*OUT);
222 7 ▶ return $_ if ($_ = open_Q(*LOCK,*OUT,$f));
Branch: 1 branch
BranchTrueFalseTotal
if $_ = open_Q(*LOCK, *OUT, $f) 0 6 6
223 6 ▶ unless (open(IN,$f)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open IN, $f) 0 6 6
224 0 close OUT;
225 0 close LOCK;
226 0 return "failed to open $f for read"
227 }
228  
229 6 my @SrcIgnoreBlocks = ();
230 6 ▶ if ($cfg->{SrcIgnore}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg->{"SrcIgnore"}) 0 6 6
231 0 my($blockstr,$block);
232 0 for $blockstr (@{$cfg->{SrcIgnore}}) {
233 0 ▶ return "failed to allocate Net::Netmask"
Branch: 1 branch
BranchTrueFalseTotal
unless $block = "Net::Netmask"->new($blockstr) 0 0 0
234 unless $block = new Net::Netmask($blockstr);
235 0 push(@SrcIgnoreBlocks, $block);
236 }
237 }
238  
239 # now format the Q file output
240 6 print OUT qq|From: $cfg->{From}
241 To: $cfg->{To}
242 Reply-To: $cfg->{'Reply-To'}
243 X-mailer: LaBrea-DShield $VERSION
244 |;
245  
246 6 $tmp = 1; # line count, flag
247 6 my $ver = '';
248 6 my $entries = 0;
249  
250 DSLINE:
251 6 ▶ while(my $in = <IN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $in = readline IN)) 24 6 30
252 # $1 $2 $3 $4 $5 $6
253 # date time tza tzb version count src sp 1stQuad dest dp proto flags
254 24 ▶ unless ($in =~ /[^\s]+\s+[^\s]+\s+([\+\-0-9]+):([0-9]+)\s+UserID([^\s]+)\s+\d+\s+([^\s]+)\s+[^\s]+\s+(\d+)\.([^\s]+)\s+[^\s]+\s+\w+\s+\w+/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in =~ /[^\s]+\s+[^\s]+\s+([\+\-0-9]+):([0-9]+)\s+UserID([^\s]+)\s+\d+\s+([^\s]+)\s+[^\s]+\s+(\d+)\.([^\s]+)\s+[^\s]+\s+\w+\s+\w+/) 0 24 24
255 0 chop $in;
256 0 close OUT;
257 0 close IN;
258 0 unlink ${f}.'.q.tmp';
259 0 close LOCK;
260 0 return "line $tmp corrupt: $in";
261 }
262 24 ▶ unless ( $ver ) { # Subject printed yet??
Branch: 1 branch
BranchTrueFalseTotal
unless ($ver) 6 18 24
263 6 $ver = $3; # nope, mark and print
264 6 print OUT "Subject: FORMAT DSHIELD USERID $cfg->{UserID} TZ $1:$2 LaBrea_Tarpit_DShield ${ver}:$VERSION\n\n";
265 }
266 24 foreach my $block (@SrcIgnoreBlocks) {
267 0 ▶ next DSLINE if $block->match($4);
Branch: 1 branch
BranchTrueFalseTotal
if $block->match($4) 0 0 0
268 }
269 24 ▶ if (exists $cfg->{Obfuscate}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cfg->{"Obfuscate"}) 16 8 24
270 16 my $dest = $5 .'.'.$6;
271 16 ▶ my $rplc = ($cfg->{Obfuscate} =~ /complete/i)
Branch: 1 branch
BranchTrueFalseTotal
$cfg->{'Obfuscate'} =~ /complete/i ? : 12 4 16
272 ? '10.0.0.1' # complete
273 : '10.' . $6; # partial
274 16 $in =~ s/$dest/$rplc/;
275 }
276 # insert DShield ID
277 24 $in =~ s/UserID${ver}/$cfg->{UserID}/;
278 24 print OUT $in;
279 24 $entries++;
280 }
281 6 close OUT;
282 6 close IN;
283 6 ▶ if ($entries) { # Only send mail if matching entries were found.
Branch: 1 branch
BranchTrueFalseTotal
if ($entries) { } 6 0 6
284 6 ▶ rename ${f}.'.q.tmp', $dir . 'qF'. time .'.'. $$ .'.'. 0
Branch: 1 branch
BranchTrueFalseTotal
unless $debug and $debug > 1 3 3 6
Condition100%3/3
$debug and $debug > 1
!ll&&!rl&&r
123
MC/DC100%2/2
$debug and $debug > 1
$debug $debug > 1
Truth table100%3/3
A and B
A= $debugB= $debug > 1
ABresult
0X0
100
111
285 unless $debug && $debug > 1;
286 } else {
287 0 ▶ unlink ${f}.'.q.tmp' unless $debug && $debug > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $debug and $debug > 1 0 0 0
Condition0%0/3
$debug and $debug > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$debug and $debug > 1
$debug $debug > 1
Truth table0%0/3
A and B
A= $debugB= $debug > 1
ABresult
0X0
100
111
288 }
289  
290 6 ▶ $debug = ($debug || unlink $f) ? 0
Branch: 1 branch
BranchTrueFalseTotal
$debug || unlink($f) ? : 6 0 6
Condition66%2/3
$debug or unlink $f
l!l&&r!l&&!r
510
MC/DC0%0/2
$debug or unlink $f
$debug unlink $f
Truth table66%2/3
A or B
A= $debugB= unlink $f
ABresult
1X1
011
000
291 : "cannot unlink $f: Operation not permitted";
292 6 close LOCK;
293 6 return $debug;
294 }
295  
296 =item $rv = deliver2_DShield(\%config,$debug);
297  
298 Alias for B<process_Q>
299  
300 =item $rv = process_Q(\%config,$debug);
301  
302 Attempts to deliver messages in queue using the configured mail agent.
303 Failed attempts are left in the queue, successfull ones are deleted.
304  
305 input: \%config
306 returns: last error message
307 or false on success
308  
309 NOTE: do not use debug mode with the mail
310 address pointing to the real target,
311 point it to yourself
312  
313 $debug = missing normal operation
314 $debug = 0 normal operation
315 $debug = 1 generate mail file suffixed
316 with .stmp or .sendmail as
317 appropriate instead of sending
318 real mail
319 $debug = 2 do not delete input Q file
320  
321 =cut
322  
323 # input: \$config, $debug
324 # returns: error message
325 # or false on success
326 #
327 # $debug true will create a file called
328 # dF{time.pid} . '.smtp' containing the mail output
329 # or
330 # dF{time.pid} . '.sendmail'
331 #
332 # $debug > 1, will not delete queue files after processing
333 #
334 # SEE: RFC-821 for SMTP codes
335 #
336 5 ▶sub process_Q {
Subroutine process_Q: called CC=24 SCAR=36.0
Pod documented
337 5 my ($cfg,$debug) = @_;
338 5 my $tmp = chk_config($cfg);
339 5 ▶ return $tmp if $tmp;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp 1 4 5
340  
341 # FOR NOW
342 4 my $me = 'localhost';
343  
344 4 $cfg->{DShield} =~ m|(.*/)|;
345 4 my $dir = $1;
346 4 ▶ return "$dir not a directory or not writable"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir and -w $dir 0 4 4
Condition33%1/3
-d $dir and -w $dir
!ll&&!rl&&r
004
MC/DC0%0/2
-d $dir and -w $dir
-d $dir -w $dir
Truth table33%1/3
A and B
A= -d $dirB= -w $dir
ABresult
0X0
100
111
347 unless -d $dir && -w $dir;
348 4 local(*M,*QF);
349 4 my $M = *M;
350 4 ▶ return "could not open directory $dir"
Branch: 1 branch
BranchTrueFalseTotal
unless opendir $M, $dir 0 4 4
351 unless opendir($M,$dir);
352 4 my @qfiles = grep(/^qF/,readdir(M));
353 4 closedir $M;
354 4 ▶ return undef unless @qfiles; # punt if nothing to do
Branch: 1 branch
BranchTrueFalseTotal
unless @qfiles 1 3 4
355  
356 3 ▶ my $smtp = ($cfg->{smtp}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cfg->{'smtp'} ? : 1 2 3
357  
358 0 ▶ local $SIG{ALRM} = sub {die 'failed: 554 timeout ';};
Subroutine __ANON__: not called CC=1 SCAR=1.2
359  
360 ## define valid SMTP response codes for each action
361 #
362 3 my $resp = {
363 'CONN' => [220],
364 'HELO' => [250],
365 'MAIL' => [250],
366 'RCPT' => [250,251],
367 'DATA' => [354],
368 'ATAD' => [250], # the "period" '.'
369 'QUIT' => [221],
370 };
371  
372 3 my $err;
373 3 foreach my $qf (@qfiles) {
374 3 ▶ if ($debug) {
Branch: 2 branches
BranchTrueFalseTotal
if ($debug) { } 3 0 3
elsif ($smtp) { } 0 0 0
375 3 ▶ $tmp = ($smtp)
Branch: 1 branch
BranchTrueFalseTotal
$smtp ? : 1 2 3
376 ? $dir . $qf . '.smtp'
377 : $dir . $qf . '.sendmail';
378 3 $tmp =~ s/qF/dF/;
379 3 ▶ return "could not open debug file $tmp"
Branch: 1 branch
BranchTrueFalseTotal
unless open $M, '>' . $tmp 0 3 3
380 unless open($M,'>'. $tmp);
381 }
382 elsif ( $smtp ) { # is SMTP
383 0 require LaBrea::NetIO;
384 0 my @smtp_hosts = split(',',$cfg->{smtp});
385 0 foreach (@smtp_hosts) {
386 0 $tmp = LaBrea::NetIO::open_tcp($M,$_,25);
387 0 ▶ last unless $tmp;
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp 0 0 0
388 }
389 0 ▶ return $tmp if $tmp; # punt if error
Branch: 1 branch
BranchTrueFalseTotal
if $tmp 0 0 0
390 }
391 else { # must be sendmail
392 0 ▶ return "could not open sendmail"
Branch: 1 branch
BranchTrueFalseTotal
unless open $M, qq[|$cfg->{"sendmail"}] 0 0 0
393 unless open($M,"|$cfg->{sendmail}");
394 }
395  
396 3 $tmp = $dir . $qf;
397 3 ▶ unless (open(QF,$tmp)) { # sigh.... kill this loop
Branch: 1 branch
BranchTrueFalseTotal
unless (open QF, $tmp) 0 3 3
398 0 close $M;
399 0 next;
400 }
401  
402 # actually send the mail now
403 3 eval {
404  
405 3 alarm 240; # 4 minutes to complete task
406 3 ▶ if ($smtp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp) { } 1 2 3
407 1 slurp($M,'CONN',$resp,$debug); # connect
408  
409 1 syswrite($M,"HELO $me\n");
410 1 slurp($M,'HELO',$resp,$debug); # hello
411  
412 1 my $line = <QF>; # LINE one, From:
413 1 syswrite($M,'MAIL ' . $line);
414 1 slurp($M,'MAIL',$resp,$debug); # mail From:
415  
416 1 $line = <QF>;
417 1 syswrite($M,'RCPT ' . $line);
418 1 slurp($M,'RCPT',$resp,$debug); # receipient To:
419  
420 1 syswrite($M,"DATA\n"); # rest of headers and message follow
421 1 slurp($M,'DATA',$resp,$debug);
422  
423 1 ▶ while ($line = <QF>) { # send the rest
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline QF)) 8 1 9
424 8 syswrite($M,$line); #
425 }
426 1 syswrite($M,'.'."\n"); # terminate with period
427 1 slurp($M,'ATAD',$resp,$debug); # Got confirmation
428  
429 1 syswrite($M,"QUIT\n");
430 1 slurp($M,'QUIT',$resp,$debug);
431 }
432 else { # IT's sendmail or equivalent
433 2 ▶ while (<QF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline QF)) 20 2 22
434 20 syswrite($M,$_);
435 }
436 } # done
437 }; # end eval
438 3 alarm 0;
439 3 close QF;
440 3 close $M;
441 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 3 3
442 0 ▶ $err = ( $@ =~ /^(failed:\s+\d+\s+\w+)/ )
Branch: 1 branch
BranchTrueFalseTotal
$@ =~ /^(failed:\s+\d+\s+\w+)/ ? : 0 0 0
443 ? $1 : $@; #'failed: 554 unknown';
444 } else {
445 3 ▶ unlink $dir . $qf unless $debug && $debug > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $debug and $debug > 1 1 2 3
Condition66%2/3
$debug and $debug > 1
!ll&&!rl&&r
012
MC/DC50%1/2
$debug and $debug > 1
$debug $debug > 1
Truth table66%2/3
A and B
A= $debugB= $debug > 1
ABresult
0X0
100
111
446 }
447 }
448 3 return $err;
449 }
450  
451 # helper subroutine to get responses from SMTP servers
452 #
453 # input: handle, action, \%valid_response, $debug
454 # returns: true if OK to proceed
455 # else DIES
456 7 ▶sub slurp {
Subroutine slurp: called CC=7 SCAR=37.2
Pod undocumented
457 7 my ($S,$act,$rsp,$debug) = @_;
458 7 ▶ return 1 if $debug; # skip
Branch: 1 branch
BranchTrueFalseTotal
if $debug 7 0 7
459 0 my $buf;
460 0 sysread($S,$buf,1024); # better not be longer than this
461 0 my $code;
462 0 foreach(@{$rsp->{$act}}) {
463 0 ▶ next unless $buf =~ /^(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $buf =~ /^(\d+)/ 0 0 0
464 0 $code = $1;
465 0 ▶ return $code if $code == $_;
Branch: 1 branch
BranchTrueFalseTotal
if $code == $_ 0 0 0
466 }
467 # bummer, didn't get a response
468 0 ▶ if ( $act !~ /QUIT/i ) { # don't wait on quit, won't do any good
Branch: 1 branch
BranchTrueFalseTotal
unless ($act =~ /QUIT/i) 0 0 0
469 0 alarm 10; # give it 10 seconds
470 0 syswrite($S,"QUIT\n");
471 0 sysread($S,$buf,1024); # try to complete the transaction
472 }
473 0 ▶ $code = 554 unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 0 0
474 0 $code = sprintf("failed: %03d ",$code) . $act;
475 0 die $code;
476 }
477  
478 4 1;
479 __END__
« blib/lib/LaBrea/Tarpit/Get.pm ↑ Summary blib/lib/LaBrea/Tarpit.pm »