blib/lib/Net/DNSBL/Monitor.pm

filter: Statementstmt 97.8% 325 / 332 Branchbran 82.1% 143 / 174 Conditioncond 58.9% 69 / 117 MC/DCmcdc 33.8% 22 / 65 Subroutinesub 95.0% 19 / 20 Podpod 45.4% 5 / 11 totaltotal 81.0% 583 / 719 CC 146 SCAR 46.6
CC
146
Cov
86%
run
157.7
cmurl
11.0
plainresp
9.1
CRAP 105.4
↑ Summary recurse2txt »
1 #!/usr/bin/perl
2 package Net::DNSBL::Monitor;
3  
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #use diagnostics;
6 7 ▶use Net::DNS::Codes qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Net::DNS::ToolKit qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
8 newhead
9 gethead
10 inet_ntoa
11 inet_aton
12 7 );
13 7 ▶use Net::DNS::ToolKit::RR;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Net::DNS::ToolKit::Utilities qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
15 id
16 revIP
17 7 );
18 7 ▶use NetAddr::IP::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use Net::DNSBL::Utilities qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
20 DO
21 list2NetAddr
22 matchNetAddr
23 7 );
24 #use Net::DNS::ToolKit::Debug qw(
25 # print_head
26 # print_buf
27 #);
28  
29 7 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
30 $VERSION @ISA @EXPORT_OK
31 7 );
32 7 require Exporter;
33 7 @ISA = qw(Exporter);
34  
35 7 $VERSION = do { my @r = (q$Revision: 0.12 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
36  
37 7 @EXPORT_OK = qw(
38 run
39 plaintxt
40 htmltxt
41 plainresp
42 htmlresp
43 );
44  
45 # undocumented $DEBUG values
46 #
47 # 1 => return @ip array
48 # 2 => return %dnsbls initialization hash
49 # 3 => return ($iptr,$regexptr) ignore, regexp ptrs
50 # 4 => return %ips union aging hash
51 # 5 => return %qc hash
52 # 6 => return %response hash
53 # other => return un-converted %dnsbls hash
54  
55 =head1 NAME
56  
57 Net::DNSBL::Monitor - Monitor DNSBL response
58  
59 =head1 SYNOPSIS
60  
61 use Net::DNSBL::Monitor qw(
62 run
63 plaintxt
64 htmltxt
65 plainresp
66 htmlresp
67 );
68  
69 ($dnsblcnts,$responses)=run(\%config,$NonBlockSock,$sockaddr_in);
70 $text = plaintxt(\%config,\%dnsblcnts);
71 $html = htmltxt(\%config,\%dnsblnts);
72 $text = plainresp(\%responses);
73 $html = htmlresp(\$config,\%responses);
74  
75  
76 =head1 DESCRIPTION
77  
78 B<Net::DNSBL::Monitor> is the Perl module that can be used to check when
79 ranges of IP addresses have been blacklisted. B<Net::DNSBL::Monitor>
80 generates a statistical report for the DNSBL's interrogated and provides an
81 individual report for each IP address found in a DNSBL.
82 The module is used to implement the reporting script B<dnsblmon>.
83  
84 =head1 CONFIGURATION FILE
85  
86 With the addition of a few elements, the configuration file for B<dnsblmon>
87 shares a common format with the Mail::SpamCannibal sc_BLcheck.pl script, and
88 Net::DNSBL::Statistics config file,
89 facilitating common maintenance of DNSBL's for your MTA installation.
90  
91 The sample configuration file
92 B<monitor.conf.sample> is heavily commented with the details for each
93 configuration element.
94  
95 A useful list of DNSBL zones for the config file (put together by
96 "Andrey V. Stolyarov" <croco-gnu@openwall.com>)
97 maybe found in the B<contrib> directory C<zones4config.file>.
98  
99 =head1 SYSTEM SIGNALS
100  
101 B<dnsblmon> responds to the following system signals:
102  
103 =over 2
104  
105 =item * TERM
106  
107 Script is terminated.
108  
109 =back
110  
111 =head1 PERL MODULE DESCRIPTION - Script Implementation
112  
113 B<Net::DNSBL::Monitor> provides most of the functions that implement
114 B<dnsblmon> which is a script that collects statistics and responses for a list of IP
115 address interrogated against a list of DNSBL's
116  
117 =head1 dnsblmon usage
118  
119 How to use B<dnsblmon>
120  
121 Syntax: dnsblmon path/to/config.file
122 or
123 dnsblmon -t path/to/config.file
124 dnsblmon -w path/to/config.file
125  
126 dnsblmon path/to/config.file -r -s
127 dnsblmon path/to/config.file -s -r
128  
129 dnsblmon path/to/config.file \
130 -r path/outfile1 \
131 -s path/outfile2
132  
133 Normally dnsblmon prints an IP report sorted by "comment"
134 and IP of the DNSBL's interrogated with their reply
135 results for each IP address.
136  
137 The 'comment' field may contain html markup text.
138  
139 i.e. commenta
140 1.2.3.4 127.0.0.3 zen.spamhaus.org
141 127.0.0.5 dnsbl.sorbs.net
142 1.2.3.5 127.0.0.5 dnsbl.sorbs.net
143  
144 commentb
145 3.4.5.6 127.0.0.2 bl.spamcannibal.org
146  
147  
148 With the (-s) switch, dnsblmon prints a sorted list
149 (by count) of the DNSBL's interrogated with their
150 reply count, percentage of the total count, and any
151 comments from the DNSBL's 'comment' key field in
152 the config file.
153  
154 The 'comment' field may contain html markup text.
155  
156 i.e.
157 44 100.0% TOTAL IP's interrogated
158 41 93.2% UNION of all results
159 34 77.3% dnsbl.sorbs.net comment
160 ........
161  
162 The -t switch will print a start and stop time to STDOUT
163  
164 i.e.
165 # start: Fri Jan 4 17:46:44 2008
166 # stop : Fri Jan 4 17:58:21 2008
167  
168 The -w switch will put the output into an HTML table
169 without the <table> statement </table>., a commment as above
170 and with an <a href="...">dnsbl name</a> statement replacing
171 the dnsbl name if the 'url' key is present in the config file.
172  
173 The -r and -s switchs are position dependent output designators.
174  
175 -r REPORT output designator
176 -s STATISTICS output designator
177  
178 as follows:
179  
180 -r -s would produce the report on STDOUT and the statistics
181 on STDERR.
182 -s -r would produce the statistics on STDOUT and the
183 report on STDERR.
184  
185 -s outfile1 -r outfile2 would write the statistics and report
186 to outfile1 and outfile2 respectively.
187  
188 Other combinations are possible. Switches are order dependent
189 but not positionally dependent with respect to other switches
190  
191 =head1 dnsblmon input file format
192  
193 The input file format for B<dnsblmon> consists of an address element block
194 in the form used by B<NetAddr::IP> followed by a 'comment field'. The report
195 generated will be sorted on the 'comment field', then by IP address.
196  
197 i.e. input format
198  
199 1.2.3.4 single host IP belonging to XYZ
200 2.3.4.5/24 A class C belonging to ABC hosting
201 etc...
202  
203  
204 =head1 Net::DNSBL::Monitor FUNCTIONS
205  
206 =over 4
207  
208 =item * ($dnsblcnts,$responses)=run(\%config,$NonBlockSock,$sockaddr_in);
209  
210 Returns the total number of IP's interrogated (IP list less white listed items) and a hash of DNSBL
211 names and their respective SPAM reply counts or equivalent for 'in-addr.arpa' and GENERIC.
212  
213 input: config pointer,
214 non-blocking socket descriptor,
215 sockaddr_in for local DNS host
216  
217 returns: pointer to dnsbl count hash
218 pointer to response hash
219  
220 The dnsbl count hash will have two added keys:
221  
222 TOTAL the total number of interrogations less whitelist
223 UNION the total number of spam 'hits'
224  
225 The response hash will be of the form:
226  
227 $response = {
228 '1.2.3.4' => {
229 'COMMENT' => 'text from comment field for IP range',
230 'bl.xyz.com' => '127.0.0.2',
231 'bl.abc.net' => '127.0.0.5',
232 },
233 etc...
234 };
235  
236 HINTs: use Net::NBsocket qw( open_udbNB sockaddr_in );
237 use Net::DNS::ToolKit qw( get_ns );
238  
239 my $sock = open_udpNB();
240 my $sockaddr_in = sockaddr_in(53, scalar get_ns());
241  
242 =cut
243  
244 7 my $w = 0;
245 7 my @w = qw( \ | / - );
246 263 ▶sub whirl {
Subroutine whirl: called CC=2 SCAR=14.0
Pod undocumented
247 263 return;
248 0 print STDERR "\r",$w[$w],"\r";
249 0 ▶ $w = 0 if ++$w > $#w;
Branch: 1 branch
BranchTrueFalseTotal
if ++$w > $#w 0 0 0
250 }
251  
252 13 ▶sub run {
Subroutine run: called CC=108 SCAR=50.6
Pod documented
253 13 my($conf,$Usock,$U_Sin,$DEBUG) = @_;
254 13 my(%ips,%ipin);
255 13 ▶ return () unless $conf->{FILES};
Branch: 1 branch
BranchTrueFalseTotal
unless $conf->{"FILES"} 1 12 13
256 12 ▶ my @files = (ref $conf->{FILES}) ? @{$conf->{FILES}} : ($conf->{FILES});
Branch: 1 branch
BranchTrueFalseTotal
ref $conf->{'FILES'} ? : 3 9 12
257  
258 12 local *F;
259 12 foreach (@files) {
260 15 ▶ next unless -e $_ && open F, $_;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $_ and open F, $_ 5 10 15
Condition66%2/3
-e $_ and open F, $_
!ll&&!rl&&r
5010
MC/DC50%1/2
-e $_ and open F, $_
-e $_ open F, $_
Truth table66%2/3
A and B
A= -e $_B= open F, $_
ABresult
0X0
100
111
261 10 foreach (<F>) {
262 84 ▶ next unless $_ =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /\S/ 2 82 84
263 82 ▶ next if $_ =~ /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^\s*#/ 0 82 82
264 82 ▶ next unless $_ =~ /(\d{1,3}[\S]*)\s*/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /(\d{1,3}[\S]*)\s*/ 0 82 82
265 82 my $nad = $1;
266 82 ▶ my $cmt = $' || '';
Condition100%2/2
$' || ''
l!l
6814
MC/DC100%1/1
$' || ''
$'
Truth table100%2/2
A
A= $'
Aresult
11
00
267 82 ▶ while ($cmt =~ /\s$/) { chop $cmt };
Branch: 1 branch
BranchTrueFalseTotal
if ($cmt =~ /\s$/) 66 82 148
268 82 my $nip = new NetAddr::IP::Lite($nad);
269 82 my $this = $nip->network();
270 82 my $stop = $nip->network();
271 82 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$this != $stop and do { do { my $tip = $this->addr; $ipin{"$tip"} = $cmt; ++$this } } 47 82 129
272 129 my $tip = $this->addr();
273 129 $ipin{"$tip"} = $cmt;
274 129 ++$this;
275 } while $this != $stop
276 }
277 10 close F;
278 }
279  
280 12 my @ips = keys %ipin;
281  
282 12 ▶ return @ips if $DEBUG && $DEBUG == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG and $DEBUG == 1 3 9 12
Condition100%3/3
$DEBUG and $DEBUG == 1
!ll&&!rl&&r
183
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
283  
284 9 my @NAignor;
285 9 ▶ if ($conf->{IGNORE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"IGNORE"}) 9 0 9
286 9 list2NetAddr($conf->{IGNORE},\@NAignor);
287 }
288  
289 ############## configure %dnsbl has for accumulating stats ###############
290 9 ▶ my @DNSBLs = grep( $_ =~ /^[0-9a-z]+\.[0-9a-z]/i && $_ !~ /in-addr/i, keys %{$conf});
Condition66%2/3
$_ =~ /^[0-9a-z]+\.[0-9a-z]/i && !($_ =~ /in-addr/i)
!ll&&!rl&&r
36035
MC/DC50%1/2
$_ =~ /^[0-9a-z]+\.[0-9a-z]/i && !($_ =~ /in-addr/i)
$_ =~ /^[0-9a-z]+\.[0-9a-z]/i !($_ =~ /in-addr/i)
Truth table66%2/3
A && not(B)
A= $_ =~ /^[0-9a-z]+\.[0-9a-z]/iB= !($_ =~ /in-addr/i)
ABresult
0X0
100
111
291  
292 9 my %dnsbls;
293  
294 9 foreach(@DNSBLs) {
295 35 $dnsbls{"$_"} = {
296 C => 0, # count
297 TO => 0, # timeouts
298 };
299 }
300  
301 #### %dnsbls configuration complete, configure maximum union timeout
302 9 my $uto = 0;
303 9 foreach(keys %dnsbls) {
304 next unless exists $conf->{"$_"} &&
305 35 ▶ exists $conf->{"$_"}->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$conf{"$_"} and exists $$conf{"$_"}{"timeout"} 0 35 35
Condition33%1/3
exists $$conf{"$_"} and exists $$conf{"$_"}{"timeout"}
!ll&&!rl&&r
0035
MC/DC0%0/2
exists $$conf{"$_"} and exists $$conf{"$_"}{"timeout"}
exists $$conf{"$_"} exists $$conf{"$_"}{"timeout"}
Truth table33%1/3
A and B
A= exists $$conf{"$_"}B= exists $$conf{"$_"}{"timeout"}
ABresult
0X0
100
111
306 35 ▶ next if $conf->{"$_"}->{timeout} < $uto;
Branch: 1 branch
BranchTrueFalseTotal
if $$conf{"$_"}{"timeout"} < $uto 6 29 35
307 29 $uto = $conf->{"$_"}->{timeout};
308 }
309  
310 9 my($iptr,$regexptr);
311 9 my $needPTR = 0;
312 9 ▶ if ($conf->{'in-addr.arpa'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"in-addr.arpa"}) 5 4 9
313 5 $dnsbls{'in-addr.arpa'} = { C => 0 };
314 5 ▶ $needPTR = $conf->{'in-addr.arpa'}->{timeout} || 30;
Condition50%1/2
$conf->{'in-addr.arpa'}{'timeout'} || 30
l!l
50
MC/DC0%0/1
$conf->{'in-addr.arpa'}{'timeout'} || 30
$conf->{'in-addr.arpa'}{'timeout'}
Truth table50%1/2
A
A= $conf->{'in-addr.arpa'}{'timeout'}
Aresult
11
00
315 }
316 9 ▶ if ($conf->{GENERIC}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"GENERIC"}) 9 0 9
317 9 $dnsbls{GENERIC} = { C => 0 };
318 9 ▶ $needPTR = ($conf->{GENERIC}->{timeout} || 30)
Branch: 1 branch
BranchTrueFalseTotal
unless $needPTR 4 5 9
Condition50%1/2
$conf->{'GENERIC'}{'timeout'} || 30
l!l
40
MC/DC0%0/1
$conf->{'GENERIC'}{'timeout'} || 30
$conf->{'GENERIC'}{'timeout'}
Truth table50%1/2
A
A= $conf->{'GENERIC'}{'timeout'}
Aresult
11
00
319 unless $needPTR;
320 9 ▶ undef $regexptr unless ($regexptr = $conf->{GENERIC}->{regexp}) &&
Branch: 1 branch
BranchTrueFalseTotal
unless $regexptr = $conf->{"GENERIC"}{"regexp"} and ref $regexptr eq "ARRAY" and @$regexptr > 0 0 9 9
Condition33%2/6
$regexptr = $conf->{"GENERIC"}{"regexp"} and ref $regexptr eq "ARRAY" and @$regexptr > 0
!ll&&!rl&&r
009
$regexptr = $conf->{"GENERIC"}{"regexp"} and ref $regexptr eq "ARRAY"
!ll&&!rl&&r
009
MC/DC0%0/3
$regexptr = $conf->{"GENERIC"}{"regexp"} and ref $regexptr eq "ARRAY" and @$regexptr > 0
$regexptr = $conf->{"GENERIC"}{"regexp"} ref $regexptr eq "ARRAY" @$regexptr > 0
Truth table25%1/4
A and B and C
A= $regexptr = $conf->{"GENERIC"}{"regexp"}B= ref $regexptr eq "ARRAY"C= @$regexptr > 0
ABCresult
0XX0
10X0
1100
1111
321 ref $regexptr eq 'ARRAY' && @$regexptr > 0;
322 9 ▶ undef $iptr unless ($iptr = $conf->{GENERIC}->{ignore}) &&
Branch: 1 branch
BranchTrueFalseTotal
unless $iptr = $conf->{"GENERIC"}{"ignore"} and ref $iptr eq "ARRAY" and @$iptr > 0 0 9 9
Condition33%2/6
$iptr = $conf->{"GENERIC"}{"ignore"} and ref $iptr eq "ARRAY" and @$iptr > 0
!ll&&!rl&&r
009
$iptr = $conf->{"GENERIC"}{"ignore"} and ref $iptr eq "ARRAY"
!ll&&!rl&&r
009
MC/DC0%0/3
$iptr = $conf->{"GENERIC"}{"ignore"} and ref $iptr eq "ARRAY" and @$iptr > 0
$iptr = $conf->{"GENERIC"}{"ignore"} ref $iptr eq "ARRAY" @$iptr > 0
Truth table25%1/4
A and B and C
A= $iptr = $conf->{"GENERIC"}{"ignore"}B= ref $iptr eq "ARRAY"C= @$iptr > 0
ABCresult
0XX0
10X0
1100
1111
323 ref $iptr eq 'ARRAY' && @$iptr > 0;
324 }
325 ### adjust $uto to account for generic retries and in-addr.arpa timeouts
326 9 ▶ $uto = $needPTR
Branch: 1 branch
BranchTrueFalseTotal
if $uto < $needPTR 0 9 9
327 if $uto < $needPTR;
328 9 ▶ $uto = 30 unless $uto;
Branch: 1 branch
BranchTrueFalseTotal
unless $uto 0 9 9
329 #### maximum $uto = 2x max delay + a little
330 9 $uto *= 2;
331 9 $uto += 5;
332  
333 9 ▶ return %dnsbls if $DEBUG && $DEBUG == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG and $DEBUG == 2 1 8 9
Condition100%3/3
$DEBUG and $DEBUG == 2
!ll&&!rl&&r
171
MC/DC100%2/2
$DEBUG and $DEBUG == 2
$DEBUG $DEBUG == 2
Truth table100%3/3
A and B
A= $DEBUGB= $DEBUG == 2
ABresult
0X0
100
111
334  
335 8 ▶ return ($iptr,$regexptr) if $DEBUG && $DEBUG == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG and $DEBUG == 3 1 7 8
Condition100%3/3
$DEBUG and $DEBUG == 3
!ll&&!rl&&r
161
MC/DC100%2/2
$DEBUG and $DEBUG == 3
$DEBUG $DEBUG == 3
Truth table100%3/3
A and B
A= $DEBUGB= $DEBUG == 3
ABresult
0X0
100
111
336  
337 7 my %qc = (
338 'in-addr' => 0,
339 'regular' => 0,
340 # retries below
341 'generic' => 0,
342 'retry-r' => 0,
343 );
344  
345 7 my %queue;
346 7 my $fileno = fileno($Usock);
347 7 my $vin = '';
348 7 vec($vin,$fileno,1) = 1;
349 7 my $Run = 1;
350 0 ▶ local $SIG{TERM} = sub {$Run = 0};
Subroutine __ANON__: not called CC=1 SCAR=1.2
351  
352 7 my $qsize = keys %dnsbls;
353 7 my $then = time;
354 7 my $uage = $then; # union aging every 5 seconds
355 7 my ($get,$put,$parse) = new Net::DNS::ToolKit::RR;
356 7 my($comment,$rin,$rout,$win,$wout,$ip,$packet,$name,$id,$msg,$bl,$now,$revIP,$nfound,$answer,$ttl,$rdl,@rdata,@sndQ);
357  
358 7 my %respons = (
359 # ip => {
360 # COMMENT => 'comment text',
361 # 'dnsbl.name' => 'code',
362 # },
363 );
364  
365 LOOP:
366 7 ▶ while ($Run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Run) 349 0 349
367 349 my $Q = keys %queue;
368 349 ▶ if ($qsize > $Q && ($ip = shift @ips)) { # run results for next IP if queue is not double full
Branch: 1 branch
BranchTrueFalseTotal
if ($qsize > $Q and $ip = shift @ips) 70 279 349
Condition100%3/3
$qsize > $Q and $ip = shift @ips
!ll&&!rl&&r
2443570
MC/DC100%2/2
$qsize > $Q and $ip = shift @ips
$qsize > $Q $ip = shift @ips
Truth table100%3/3
A and B
A= $qsize > $QB= $ip = shift @ips
ABresult
0X0
100
111
369 70 ▶ next if matchNetAddr($ip,\@NAignor);
Branch: 1 branch
BranchTrueFalseTotal
if matchNetAddr($ip, \@NAignor) 14 56 70
370 56 ++$dnsbls{TOTAL}->{C};
371 56 $now = time;
372 56 $revIP = revIP($ip);
373 56 ▶ if ($needPTR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needPTR) 56 0 56
374 56 $id = makid(\%queue);
375 56 $name = $revIP .'.in-addr.arpa';
376 56 $packet = makequery($put,$id,$name,T_PTR());
377 56 $queue{$id} = {
378 B => 'in-addr.arpa',
379 Q => $packet,
380 T => $now + $needPTR, # timeout
381 R => 0, # retry
382 X => $revIP,
383 };
384 56 push @sndQ, $packet;
385 56 ++$qc{'in-addr'};
386 }
387
388 56 foreach $bl (@DNSBLs) {
389 168 ▶ next if $dnsbls{"$bl"}->{TO} > 5; # ignore this BL if it timed out to many times
Branch: 1 branch
BranchTrueFalseTotal
if $dnsbls{"$bl"}{"TO"} > 5 4 164 168
390 164 $id = makid(\%queue);
391 164 $name = $revIP .'.'. $bl;
392 164 $packet = makequery($put,$id,$name,T_A());
393 $queue{$id} = {
394 B => "$bl",
395 Q => $packet,
396 164 ▶ T => $now + ($conf->{"$bl"}->{timeout} || 30),
Condition50%1/2
$$conf{"$bl"}{'timeout'} || 30
l!l
1640
MC/DC0%0/1
$$conf{"$bl"}{'timeout'} || 30
$$conf{"$bl"}{'timeout'}
Truth table50%1/2
A
A= $$conf{"$bl"}{'timeout'}
Aresult
11
00
397 R => 0,
398 X => $revIP,
399 };
400 164 push @sndQ, $packet;
401 164 ++$qc{regular};
402 }
403 }
404  
405 # wait for some responses
406 335 $rin = $vin;
407 335 ▶ if (@sndQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sndQ) { } 74 261 335
408 74 $win = $vin;
409 } else {
410 261 $win = '';
411 }
412 335 $nfound = select($rout=$rin,$wout=$win,undef,0.5); # tick each second
413 335 ▶ if ($nfound > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound > 0) { } 181 154 335
414 181 ▶ while (vec($wout,$fileno,1) && @sndQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $wout, $fileno, 1 and @sndQ) 304 181 485
Condition100%3/3
vec $wout, $fileno, 1 and @sndQ
!ll&&!rl&&r
10774304
MC/DC100%2/2
vec $wout, $fileno, 1 and @sndQ
vec $wout, $fileno, 1 @sndQ
Truth table100%3/3
A and B
A= vec $wout, $fileno, 1B= @sndQ
ABresult
0X0
100
111
415 304 $packet = shift @sndQ;
416 #print STDERR "WRITE\n";
417 #print_buf(\$packet);
418 #print STDERR "\n";
419 304 send($Usock,$packet,0,$U_Sin);
420 304 ▶ whirl() if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 263 41 304
421 }
422 181 ▶ if (vec($rout,$fileno,1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $rout, $fileno, 1) 133 48 181
423 133 undef $msg;
424 133 ▶ next unless recv($Usock,$msg,,PACKETSZ,0); # ignore receive errors
Branch: 1 branch
BranchTrueFalseTotal
unless recv $Usock, $msg, PACKETSZ(), 0 0 133 133
425 133 ▶ next unless length($msg) > HFIXEDSZ; # ignore short packets
Branch: 1 branch
BranchTrueFalseTotal
unless length $msg > HFIXEDSZ() 0 133 133
426 #print STDERR "RECEIVE\n";
427 #print_buf(\$msg);
428 #print STDERR "\n";
429 133 my($off,$qr,$opcode,$aa,$tc,$rd,$ra,$mbz,$ad,$cd,$rcode,$qdcount,$ancount,$nscount,$arcount);
430 133 ($off,$id,$qr,$opcode,$aa,$tc,$rd,$ra,$mbz,$ad,$cd,$rcode,$qdcount,$ancount,$nscount,$arcount)= gethead(\$msg);
431 next unless
432 $tc == 0 &&
433 $qr == 1 &&
434 $opcode == QUERY &&
435 ($rcode == NOERROR || $rcode == NXDOMAIN || $rcode == SERVFAIL) &&
436 $qdcount == 1 &&
437 133 ▶ exists $queue{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $tc == 0 and $qr == 1 and $opcode == QUERY() and $rcode == NOERROR() || $rcode == NXDOMAIN() || $rcode == SERVFAIL() and $qdcount == 1 and exists $queue{$id} 0 133 133
Condition47%10/21
$tc == 0 and $qr == 1 and $opcode == QUERY() and $rcode == NOERROR() || $rcode == NXDOMAIN() || $rcode == SERVFAIL() and $qdcount == 1 and exists $queue{$id}
!ll&&!rl&&r
00133
$tc == 0 and $qr == 1 and $opcode == QUERY() and $rcode == NOERROR() || $rcode == NXDOMAIN() || $rcode == SERVFAIL() and $qdcount == 1
!ll&&!rl&&r
00133
$tc == 0 and $qr == 1 and $opcode == QUERY() and $rcode == NOERROR() || $rcode == NXDOMAIN() || $rcode == SERVFAIL()
!ll&&!rl&&r
00133
$tc == 0 and $qr == 1 and $opcode == QUERY()
!ll&&!rl&&r
00133
$tc == 0 and $qr == 1
!ll&&!rl&&r
00133
$rcode == NOERROR() or $rcode == NXDOMAIN() or $rcode == SERVFAIL()
l!l&&r!l&&!r
112210
$rcode == NOERROR() or $rcode == NXDOMAIN()
l!l&&r!l&&!r
842821
MC/DC0%0/8
$tc == 0 and $qr == 1 and $opcode == QUERY() and $rcode == NOERROR() || $rcode == NXDOMAIN() || $rcode == SERVFAIL() and $qdcount == 1 and exists $queue{$id}
$tc == 0 $qr == 1 $opcode == QUERY() $rcode == NOERROR() $rcode == NXDOMAIN() $rcode == SERVFAIL() $qdcount == 1 exists $queue{$id}
Truth table23%3/13
A and B and C and D or E or F and G and H
A= $tc == 0B= $qr == 1C= $opcode == QUERY()D= $rcode == NOERROR()E= $rcode == NXDOMAIN()F= $rcode == SERVFAIL()G= $qdcount == 1H= exists $queue{$id}
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
110XXXXX0
111000XX0
1111XX0X0
11101X0X0
1110010X0
1111XX100
11101X100
111001100
1111XX111
11101X111
111001111
438  
439 133 ($off,my($name,$t,$class)) = $get->Question(\$msg,$off);
440 133 ▶ next unless $class == C_IN; # not my question
Branch: 1 branch
BranchTrueFalseTotal
unless $class == C_IN() 0 133 133
441  
442 133 $bl = $queue{$id}->{B};
443 133 $revIP = $queue{$id}->{X};
444
445 133 delete $queue{$id};
446 133 ▶ $dnsbls{"$bl"}->{TO} = 0 # reset timeout count
Branch: 1 branch
BranchTrueFalseTotal
unless $bl eq "in-addr.arpa" 84 49 133
447 unless $bl eq 'in-addr.arpa';
448 133 ▶ if ($ancount && $rcode == &NOERROR) { # if good response
Branch: 3 branches
BranchTrueFalseTotal
if ($ancount and $rcode == &NOERROR) { } 84 49 133
elsif ($t == T_A()) { } 35 14 49
elsif ($needPTR and $t == T_PTR() and exists $conf->{'in-addr.arpa'}) { } 6 8 14
Condition66%2/3
$ancount and $rcode == &NOERROR
!ll&&!rl&&r
49084
MC/DC50%1/2
$ancount and $rcode == &NOERROR
$ancount $rcode == &NOERROR
Truth table66%2/3
A and B
A= $ancountB= $rcode == &NOERROR
ABresult
0X0
100
111
449 84 $name =~ /(?:\d+\.\d+\.\d+\.\d+\.)/i;
450 84 ▶ next unless lc $bl eq lc $' &&
Branch: 1 branch
BranchTrueFalseTotal
unless lc $bl eq lc $' and $t == T_A() || $t == T_PTR() 0 84 84
Condition50%3/6
lc $bl eq lc $' and $t == T_A() || $t == T_PTR()
!ll&&!rl&&r
0084
$t == T_A() or $t == T_PTR()
l!l&&r!l&&!r
49350
MC/DC0%0/3
lc $bl eq lc $' and $t == T_A() || $t == T_PTR()
lc $bl eq lc $' $t == T_A() $t == T_PTR()
Truth table50%2/4
A and B or C
A= lc $bl eq lc $'B= $t == T_A()C= $t == T_PTR()
ABCresult
0XX0
1000
11X1
1011
451 ($t == T_A || $t == T_PTR);
452  
453 84 undef $answer;
454 84 my @generic;
455 ANSWER:
456 84 foreach(0..$ancount-1) {
457 126 ($off,$name,$t,$class,$ttl,$rdl,@rdata) = $get->next(\$msg,$off);
458 126 ▶ if ($t == T_A) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t == T_A()) { } 77 49 126
elsif ($needPTR and $t == T_PTR() and exists $dnsbls{'GENERIC'}) { } 49 0 49
459 77 foreach $answer (@rdata) {
460 77 $ip = inet_ntoa($answer);
461 77 ▶ if (grep($ip eq $_,keys %{$conf->{"$bl"}->{accept}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep(($ip eq $_), keys %{$$conf{"$bl"}{"accept"};})) 35 42 77
462 35 onion(\%dnsbls,\%ips,$revIP,$now + $uto,\%ipin,$bl,\%respons,$ip);
463 #print STDERR "FAILED $name $ip\n";
464 35 next LOOP;
465 }
466 }
467 42 ▶ if (exists $conf->{"$bl"}->{acceptany}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$conf{"$bl"}{"acceptany"}) 0 42 42
468 0 $ip = inet_ntoa($rdata[0]);
469 0 onion(\%dnsbls,\%ips,$revIP,$now + $uto,\%ipin,$bl,\%respons,$ip);
470 0 next LOOP;
471 }
472 }
473 ▶ elsif ($needPTR && $t == T_PTR && exists $dnsbls{GENERIC}) {
Condition33%2/6
$needPTR and $t == T_PTR() and exists $dnsbls{"GENERIC"}
!ll&&!rl&&r
0049
$needPTR and $t == T_PTR()
!ll&&!rl&&r
0049
MC/DC0%0/3
$needPTR and $t == T_PTR() and exists $dnsbls{"GENERIC"}
$needPTR $t == T_PTR() exists $dnsbls{"GENERIC"}
Truth table25%1/4
A and B and C
A= $needPTRB= $t == T_PTR()C= exists $dnsbls{"GENERIC"}
ABCresult
0XX0
10X0
1100
1111
474 # positive in-addr.arpa responses are ignored
475 49 push @generic, $rdata[0];
476 49 next ANSWER;
477 }
478 }
479 49 ▶ if (@generic) {
Branch: 1 branch
BranchTrueFalseTotal
if (@generic) 35 14 49
480 35 foreach my $g (@generic) {
481 49 ▶ next LOOP if $iptr && grep($g =~ /$_/i, @$iptr);
Branch: 1 branch
BranchTrueFalseTotal
if $iptr and grep(($g =~ /$_/i), @$iptr) 14 35 49
Condition66%2/3
$iptr and grep(($g =~ /$_/i), @$iptr)
!ll&&!rl&&r
03514
MC/DC50%1/2
$iptr and grep(($g =~ /$_/i), @$iptr)
$iptr grep(($g =~ /$_/i), @$iptr)
Truth table66%2/3
A and B
A= $iptrB= grep(($g =~ /$_/i), @$iptr)
ABresult
0X0
100
111
482 35 ▶ next LOOP if $g && ! grep($g =~ /$_/i, @$regexptr);
Branch: 1 branch
BranchTrueFalseTotal
if $g and not grep(($g =~ /$_/i), @$regexptr) 7 28 35
Condition66%2/3
$g and not grep(($g =~ /$_/i), @$regexptr)
!ll&&!rl&&r
0287
MC/DC50%1/2
$g and not grep(($g =~ /$_/i), @$regexptr)
$g not grep(($g =~ /$_/i), @$regexptr)
Truth table66%2/3
A and not(B)
A= $gB= not grep(($g =~ /$_/i), @$regexptr)
ABresult
0X0
100
111
483 }
484 14 onion(\%dnsbls,\%ips,$revIP,$now + $uto,\%ipin,'GENERIC',\%respons,'127.0.0.2');
485 #print STDERR "FAILED $name GENERIC\n";
486 }
487 }
488 elsif ($t == T_A) {
489 35 next LOOP; # tis a lookup failure or no response... ignore
490 }
491 ▶ elsif ($needPTR && $t == T_PTR && exists $conf->{'in-addr.arpa'}) {
Condition50%3/6
$needPTR and $t == T_PTR() and exists $conf->{"in-addr.arpa"}
!ll&&!rl&&r
086
$needPTR and $t == T_PTR()
!ll&&!rl&&r
0014
MC/DC33%1/3
$needPTR and $t == T_PTR() and exists $conf->{"in-addr.arpa"}
$needPTR $t == T_PTR() exists $conf->{"in-addr.arpa"}
Truth table50%2/4
A and B and C
A= $needPTRB= $t == T_PTR()C= exists $conf->{"in-addr.arpa"}
ABCresult
0XX0
10X0
1100
1111
492 6 onion(\%dnsbls,\%ips,$revIP,$now + $uto,\%ipin,'in-addr.arpa',\%respons,'127.0.0.2');
493 #print STDERR "FAILED $name ERROR\n";
494 6 next LOOP;
495 }
496 }
497 }
498 ######################################################################
499 else { # timeout
500 154 $now = time;
501 154 ▶ next unless $now > $then;
Branch: 1 branch
BranchTrueFalseTotal
unless $now > $then 77 77 154
502 77 $then = $now;
503 77 my @queue = sort {$queue{$a}->{T} <=> $queue{$b}->{T}} keys %queue;
504 77 foreach $id (@queue) { # check for DNSBL timeouts
505 209 ▶ last if $now < $queue{$id}->{T};
Branch: 1 branch
BranchTrueFalseTotal
if $now < $queue{$id}{"T"} 38 171 209
506 171 $bl = $queue{$id}->{B};
507 171 ▶ if ($bl eq 'in-addr.arpa') {
Branch: 1 branch
BranchTrueFalseTotal
if ($bl eq 'in-addr.arpa') { } 11 160 171
508 11 ▶ if (exists $conf->{'in-addr.arpa'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $conf->{'in-addr.arpa'}) { } 3 8 11
elsif (exists $dnsbls{'GENERIC'}) { } 8 0 8
509 3 $revIP = $queue{$id}->{X};
510 3 delete $queue{$id};
511 3 onion(\%dnsbls,\%ips,$revIP,$now + $uto,\%ipin,'in-addr.arpa',\%respons,'127.0.0.2');
512 #print STDERR "FAILED $revIP.in-addr.arpa timeout\n";
513 }
514 elsif (exists $dnsbls{GENERIC}) {
515 8 ▶ unless ($queue{$id}->{R}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($queue{$id}{'R'}) { } 4 4 8
516 4 $queue{$id}->{R} = 1; # retry generic queries
517 4 $queue{$id}->{T} = $now + $needPTR;
518 4 push @sndQ, $queue{$id}->{Q};
519 4 ++$qc{generic};
520 }
521 else {
522 4 delete $queue{$id};
523 }
524 }
525 }
526 else { # regular DNSBL
527 160 ▶ unless ($queue{$id}->{R}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($queue{$id}{'R'}) { } 80 80 160
528 80 $queue{$id}->{R} = 1;
529 80 ▶ $queue{$id}->{T} = $now + ($conf->{"$bl"}->{timeout} || 30);
Condition50%1/2
$$conf{"$bl"}{'timeout'} || 30
l!l
800
MC/DC0%0/1
$$conf{"$bl"}{'timeout'} || 30
$$conf{"$bl"}{'timeout'}
Truth table50%1/2
A
A= $$conf{"$bl"}{'timeout'}
Aresult
11
00
530 80 push @sndQ, $queue{$id}->{Q};
531 80 ++$qc{'retry-r'};
532 }
533 else {
534 80 $revIP = $queue{$id}->{X};
535 80 delete $queue{$id};
536 80 ++$dnsbls{"$bl"}->{TO};
537 }
538 }
539 }
540 77 ▶ last LOOP unless @ips || keys %queue; # run through all IP's and remaining queue items
Branch: 1 branch
BranchTrueFalseTotal
unless @ips or keys %queue 7 70 77
Condition100%3/3
@ips or keys %queue
l!l&&r!l&&!r
52187
MC/DC100%2/2
@ips or keys %queue
@ips keys %queue
Truth table100%3/3
A or B
A= @ipsB= keys %queue
ABresult
1X1
011
000
541 70 ▶ next unless $uage < $now;
Branch: 1 branch
BranchTrueFalseTotal
unless $uage < $now 56 14 70
542 14 $uage = $now + 5;
543 14 @_ = sort {$ips{"$a"} <=> $ips{"$b"}} keys %ips;
544 14 foreach (@_) {
545 11 ▶ last if $ips{"$_"} > $now;
Branch: 1 branch
BranchTrueFalseTotal
if $ips{"$_"} > $now 11 0 11
546 0 delete $ips{"$_"};
547 }
548 } # else nfound
549 } # while ($Run)
550  
551 7 ▶ close $Usock unless $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless $DEBUG 1 6 7
552  
553 7 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) { } 6 1 7
554 6 ▶ return %ips if $DEBUG == 4;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG == 4 2 4 6
555 4 ▶ return %qc if $DEBUG == 5;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG == 5 2 2 4
556 2 ▶ return %respons if $DEBUG == 6;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG == 6 0 2 2
557 2 return %dnsbls; # for any other debug value
558 }
559 else {
560 1 foreach(keys %dnsbls) {
561 6 $dnsbls{$_} = $dnsbls{$_}->{C};
562 }
563 }
564 1 return (\%dnsbls,\%respons);
565 }
566  
567 220 ▶sub makequery {
Subroutine makequery: called CC=1 SCAR=0.0
Pod undocumented
568 220 my($put,$id,$name,$type) = @_;
569 220 my $buf;
570 220 my $off = newhead(\$buf,
571 $id,
572 BITS_QUERY | RD,
573 1,0,0,0,
574 );
575 220 $off = $put->Question(\$buf,$off,$name,$type,C_IN);
576 220 return $buf;
577 }
578  
579 220 ▶sub makid {
Subroutine makid: called CC=2 SCAR=7.0
Pod undocumented
580 220 my $qp = shift;
581 220 my $id;
582 do {
583 220 $id = id()
584 220 ▶ } while exists $qp->{$id};
Branch: 1 branch
BranchTrueFalseTotal
exists $qp->{$id} and do { do { $id = id() } } 0 220 220
585 220 return $id;
586 }
587  
588 58 ▶sub union {
Subroutine union: called CC=3 SCAR=11.0
Pod undocumented
589 58 my($dnsbls,$union,$rip,$expire) = @_;
590 58 $expire += 30; # union cache expiration is alway longer than timeouts
591 58 ▶ if (exists $union->{"$rip"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$union{"$rip"}) { } 24 34 58
592 $union->{"$rip"} = $expire
593 24 ▶ if $expire > $union->{"$rip"};
Branch: 1 branch
BranchTrueFalseTotal
if $expire > $$union{"$rip"} 0 24 24
594 } else {
595 34 $union->{"$rip"} = $expire;
596 34 ++$dnsbls->{UNION}->{C};
597 }
598 }
599  
600 58 ▶sub onion {
Subroutine onion: called CC=2 SCAR=6.9
Pod undocumented
601 58 my($dnsbls,$union,$rip,$expire,$ipin,$bl,$resp,$code) = @_;
602 58 ++$dnsbls->{"$bl"}->{C};
603 58 &union;
604 58 my $ip = revIP($rip);
605 58 ▶ if (exists $resp->{"$ip"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$resp{"$ip"}) { } 24 34 58
606 24 $resp->{"$ip"}->{"$bl"} = $code;
607 }
608 else {
609 $resp->{"$ip"} = {
610 34 COMMENT => $ipin->{"$ip"},
611 "$bl" => $code,
612 }
613 }
614 }
615  
616  
617 =item * $text = plaintxt($config,\%dnsbls);
618  
619 Generate a plain text report of the form:
620  
621 44 100.0% TOTAL IP's interrogated
622 41 93.2% UNION of all results
623 34 77.3% dnsbl.sorbs.net comment
624 22 50.0% GENERIC comment
625 13 29.5% in-addr.arpa comment
626 11 25.0% cbl.abuseat.org comment
627 9 20.5% list.dsbl.org comment
628 2 4.5% dnsbl.njabl.org comment
629 1 2.3% bl.spamcannibal.org comment
630 0 0.0% dynablock.njabl.org comment
631  
632 input: configuration pointer,
633 dnsbl count hash pointer
634 returns: text buffer
635  
636 The 'comment' comes from the config file 'comment' key field for each
637 specified DNSBL or is blank if there is no 'comment' key.
638  
639 =cut
640  
641 # return 'comment' and 'url' if present
642 # input: $conf, $bl, $nbsp
643 # output: if $nbsp ($comment,$url)
644 # if !$nbsp $comment
645 #
646 39 ▶sub cmurl {
Subroutine cmurl: called CC=11 SCAR=24.0
Pod undocumented
647 39 my($conf,$bl,$nbsp) = @_;
648 39 ▶ if ($bl eq 'TOTAL') {
Branch: 2 branches
BranchTrueFalseTotal
if ($bl eq 'TOTAL') { } 2 37 39
elsif ($bl eq 'UNION') { } 2 35 37
649 2 ▶ if ($nbsp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nbsp) { } 1 1 2
650 1 return (q|IP's interrogated|,'');
651 } else {
652 1 return q|IP's interrogated|;
653 }
654 } elsif ($bl eq 'UNION') {
655 2 ▶ if ($nbsp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nbsp) { } 1 1 2
656 1 return (q|of all results|,'');
657 } else {
658 1 return q|of all results|;
659 }
660 }
661 my $comment = (exists $conf->{"$bl"}->{comment} && $conf->{"$bl"}->{comment})
662 ? $conf->{"$bl"}->{comment}
663 35 ▶ : ($nbsp) ? '&nbsp;' : '';
Branch: 2 branches
BranchTrueFalseTotal
exists $$conf{"$bl"}{'comment'} && $$conf{"$bl"}{'comment'} ? : 10 25 35
$nbsp ? : 20 5 25
Condition66%2/3
exists $$conf{"$bl"}{"comment"} and $$conf{"$bl"}{"comment"}
!ll&&!rl&&r
25010
MC/DC50%1/2
exists $$conf{"$bl"}{"comment"} and $$conf{"$bl"}{"comment"}
exists $$conf{"$bl"}{"comment"} $$conf{"$bl"}{"comment"}
Truth table66%2/3
A and B
A= exists $$conf{"$bl"}{"comment"}B= $$conf{"$bl"}{"comment"}
ABresult
0X0
100
111
664 35 ▶ return $comment unless $nbsp;
Branch: 1 branch
BranchTrueFalseTotal
unless $nbsp 10 25 35
665 my $url = (exists $conf->{"$bl"}->{url} && $conf->{"$bl"}->{url})
666 25 ▶ ? q|<a href="|. $conf->{"$bl"}->{url} .q|">|
Branch: 1 branch
BranchTrueFalseTotal
exists $$conf{"$bl"}{'url'} && $$conf{"$bl"}{'url'} ? : 5 20 25
Condition66%2/3
exists $$conf{"$bl"}{"url"} and $$conf{"$bl"}{"url"}
!ll&&!rl&&r
2005
MC/DC50%1/2
exists $$conf{"$bl"}{"url"} and $$conf{"$bl"}{"url"}
exists $$conf{"$bl"}{"url"} $$conf{"$bl"}{"url"}
Truth table66%2/3
A and B
A= exists $$conf{"$bl"}{"url"}B= $$conf{"$bl"}{"url"}
ABresult
0X0
100
111
667 : '';
668 25 return ($comment,$url);
669 }
670
671 1 ▶sub plaintxt {
Subroutine plaintxt: called CC=4 SCAR=14.2
Pod documented
672 1 my($conf,$dnsbls) = @_;
673 return "# ERROR list is empty\n"
674 1 ▶ unless keys %$dnsbls && $dnsbls->{TOTAL};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$dnsbls and $dnsbls->{"TOTAL"} 0 1 1
Condition33%1/3
keys %$dnsbls and $dnsbls->{"TOTAL"}
!ll&&!rl&&r
001
MC/DC0%0/2
keys %$dnsbls and $dnsbls->{"TOTAL"}
keys %$dnsbls $dnsbls->{"TOTAL"}
Truth table33%1/3
A and B
A= keys %$dnsblsB= $dnsbls->{"TOTAL"}
ABresult
0X0
100
111
675 1 my $txt = '';
676 1 my $tot = $dnsbls->{TOTAL}/100;
677 1 my $len = length($dnsbls->{TOTAL});
678 1 foreach(sort{$dnsbls->{"$b"} <=> $dnsbls->{"$a"}} keys %$dnsbls) {
679 12 my $comment = cmurl($conf,$_);
680 12 $txt .= sprintf("% ${len}u% 6.1f%% %s %s\n",$dnsbls->{"$_"},$dnsbls->{"$_"}/$tot,$_,$comment);
681 }
682 1 return $txt;
683 }
684  
685 =item * $html = htmltxt($config,\%dnsbls);
686  
687 Generate a report as above but with E<lt>trE<gt>E<lt>tdE<gt>E<lt>/tdE<gt>E<lt>/trE<gt> table markup. The
688 E<lt>tableE<gt>E<lt>/tableE<gt> tags are not generated. If there is a 'url' key field in the
689 respective DNSBL config entry, the DNSBL name is provide with E<lt>a href="url"E<gt>DNSBLE<lt>/aE<gt>
690 tags with the specified 'url' as the 'href' value.
691  
692 input: configuration pointer,
693 dnsbl count hash pointer
694 returns: html text buffer
695  
696 A one line example corresponding to the text line above:
697  
698 34 77.3% dnsbl.sorbs.net
699  
700 with a 'comment' key of: 127.0.0.2,5,7,8,9,10,12
701 and a 'url' key of: http://www.au.sorbs.net/using.shtml
702  
703 <tr class=dnsbl><td align=right>34</td>
704 <td align=right>77.3%</td>
705 <td align=left><a
706 href="http://www.au.sorbs.net/using.shtml">dnsbl.sorbs.net</a></td>
707 <td align=left>127.0.0.2,5,7,8,9,10,12</td>
708 </tr>
709  
710 =cut
711  
712 1 ▶sub htmltxt {
Subroutine htmltxt: called CC=5 SCAR=16.3
Pod documented
713 1 my($conf,$dnsbls) = @_;
714 return "<!-- ERROR list is empty -->\n"
715 1 ▶ unless keys %$dnsbls && $dnsbls->{TOTAL};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$dnsbls and $dnsbls->{"TOTAL"} 0 1 1
Condition33%1/3
keys %$dnsbls and $dnsbls->{"TOTAL"}
!ll&&!rl&&r
001
MC/DC0%0/2
keys %$dnsbls and $dnsbls->{"TOTAL"}
keys %$dnsbls $dnsbls->{"TOTAL"}
Truth table33%1/3
A and B
A= keys %$dnsblsB= $dnsbls->{"TOTAL"}
ABresult
0X0
100
111
716 1 my $html = '';
717 1 my $tot = $dnsbls->{TOTAL}/100;
718 1 my $len = length($dnsbls->{TOTAL});
719 1 foreach(sort{$dnsbls->{"$b"} <=> $dnsbls->{"$a"}} keys %$dnsbls) {
720 12 my($comment,$url) = cmurl($conf,$_,1);
721 12 ▶ my $aa = $url ? '</a>' : '';
Branch: 1 branch
BranchTrueFalseTotal
$url ? : 3 9 12
722 12 my $count = $dnsbls->{"$_"};
723 12 $html .= '<tr class=dnsbl><td align=right>'.
724 $count .'</td><td align=right>'.
725 sprintf("%.1f",$count/$tot) .'%</td><td align=left>'.
726 $url . $_ . $aa .'</td><td align=left>'. $comment .'</td></tr>'."\n";
727 }
728 1 return $html;
729 }
730  
731 =item * $text = plainresp(\%response);
732  
733 Generate a plain text report of the form:
734  
735 comment string 1
736 ipaddr1 response code dnsbl
737 response code dnsbl
738 response code dnsbl
739 ipaddr2 response code dnsbl
740  
741 comment string 2
742 ipaddr3 response code dnsbl
743  
744 etc...
745
746 input: response hash pointer
747 returns: text buffer
748  
749 =cut
750  
751 1 ▶sub plainresp {
Subroutine plainresp: called CC=9 SCAR=22.1
Pod documented
752 1 my($r) = @_;
753 1 ▶ return "# RESPONSE list is empty\n"
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$r 0 1 1
754 unless keys %$r;
755 1 my $txt = '';
756 1 my $ck = 'tHiSrAnDoMsTrInG'; # comment key
757 1 my($cmt,$ip,$pad);
758 1 foreach $ip (sort {
759 $r->{"$a"}->{COMMENT} cmp $r->{"$b"}->{COMMENT}
760 ||
761 13 ▶ inet_aton($a) cmp inet_aton($b)
Condition66%2/3
$$r{"$a"}{'COMMENT'} cmp $$r{"$b"}{'COMMENT'} || inet_aton($a) cmp inet_aton($b)
l!l&&r!l&&!r
1030
MC/DC0%0/2
$$r{"$a"}{'COMMENT'} cmp $$r{"$b"}{'COMMENT'} || inet_aton($a) cmp inet_aton($b)
$$r{"$a"}{'COMMENT'} cmp $$r{"$b"}{'COMMENT'} inet_aton($a) cmp inet_aton($b)
Truth table66%2/3
A || B
A= $$r{"$a"}{'COMMENT'} cmp $$r{"$b"}{'COMMENT'}B= inet_aton($a) cmp inet_aton($b)
ABresult
1X1
011
000
762 } keys %$r) {
763
764 7 ▶ unless ($ck eq $r->{"$ip"}->{COMMENT}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ck eq $$r{"$ip"}{"COMMENT"}) 4 3 7
765 4 $cmt = $ck = $r->{"$ip"}->{COMMENT};
766 4 $txt .= "\n$cmt\n";
767 }
768 7 $txt .= " $ip\t";
769 7 ▶ $txt .= "\t" if length("$ip") < 12;
Branch: 1 branch
BranchTrueFalseTotal
if length "$ip" < 12 7 0 7
770 7 $pad = '';
771 7 foreach (sort keys %{$r->{"$ip"}}) {
772 22 ▶ next if $_ eq 'COMMENT';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "COMMENT" 7 15 22
773 15 my $rc = $r->{"$ip"}->{"$_"};
774 15 $txt .= $pad . $rc ."\t";
775 15 ▶ $txt .= "\t" if length($rc) < 8;
Branch: 1 branch
BranchTrueFalseTotal
if length $rc < 8 0 15 15
776 15 $txt .= $_ ."\n";
777 15 $pad = "\t\t\t";
778 }
779 }
780 1 return $txt;
781 }
782  
783 =item * $html = htmlresp(\%config,\%response);
784  
785 Generate a report as above but with E<lt>trE<gt>E<lt>tdE<gt>E<lt>/tdE<gt>E<lt>/trE<gt> table markup. The
786 E<lt>tableE<gt>E<lt>/tableE<gt> tags are not generated.
787  
788 input: config hash pointer
789 response hash pointer
790 returns: html text buffer
791  
792 A one line example corresponding to the text line above:
793  
794 34 77.3% dnsbl.sorbs.net
795  
796 with a 'comment' key of: 127.0.0.2,5,7,8,9,10,12
797 and a 'url' key of: http://www.au.sorbs.net/using.shtml
798  
799 <tr class=dnsbl><td align=right>34</td>
800 <td align=right>77.3%</td>
801 <td align=left><a
802 href="http://www.au.sorbs.net/using.shtml">dnsbl.sorbs.net</a></td>
803 <td align=left>127.0.0.2,5,7,8,9,10,12</td>
804 </tr>
805  
806 =back
807  
808 =cut
809  
810 1 ▶sub htmlresp {
Subroutine htmlresp: called CC=9 SCAR=22.0
Pod documented
811 1 my($conf,$r) = @_;
812 1 ▶ return "# RESPONSE list is empty\n"
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$r 0 1 1
813 unless keys %$r;
814 1 my $txt = '';
815 1 my $ck = 'ThIsRaNdOmStRiNg'; # comment key
816 1 my $tbl = '';
817 1 my $rs = ''; # non numeric 1st time
818 1 my($cmt,$ip,$tr);
819 1 foreach $ip (sort {
820 $r->{"$a"}->{COMMENT} cmp $r->{"$b"}->{COMMENT}
821 ||
822 13 ▶ inet_aton($a) cmp inet_aton($b)
Condition66%2/3
$$r{"$a"}{'COMMENT'} cmp $$r{"$b"}{'COMMENT'} || inet_aton($a) cmp inet_aton($b)
l!l&&r!l&&!r
1030
MC/DC0%0/2
$$r{"$a"}{'COMMENT'} cmp $$r{"$b"}{'COMMENT'} || inet_aton($a) cmp inet_aton($b)
$$r{"$a"}{'COMMENT'} cmp $$r{"$b"}{'COMMENT'} inet_aton($a) cmp inet_aton($b)
Truth table66%2/3
A || B
A= $$r{"$a"}{'COMMENT'} cmp $$r{"$b"}{'COMMENT'}B= inet_aton($a) cmp inet_aton($b)
ABresult
1X1
011
000
823 } keys %$r) {
824 7 ▶ if ($ck eq $r->{"$ip"}->{COMMENT}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ck eq $$r{"$ip"}{'COMMENT'}) { } 3 4 7
825 ;
826 } else {
827 4 $txt .= $rs . $tbl . "\n";
828 4 $ck = $r->{"$ip"}->{COMMENT};
829 4 ▶ $cmt = $ck || '&nbsp;';
Condition100%2/2
$ck || '&nbsp;'
l!l
31
MC/DC100%1/1
$ck || '&nbsp;'
$ck
Truth table100%2/2
A
A= $ck
Aresult
11
00
830 4 $txt .= '<tr valign=top align=left><td rowspan=';
831 4 $tr = ">$cmt</td>";
832 4 $tbl = '';
833 4 $rs = 0;
834 }
835
836 7 my $pad = (keys %{$r->{"$ip"}}) -1;
837 7 $pad = '<td rowspan='. $pad .'>'. $ip .'</td>';
838 7 foreach (sort keys %{$r->{"$ip"}}) {
839 22 ▶ next if $_ eq 'COMMENT';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "COMMENT" 7 15 22
840 15 ++$rs;
841 15 my $rtc = $r->{"$ip"}->{"$_"};
842 15 my($comment,$url) = cmurl($conf,$_,1);
843 15 ▶ my $aa = $url ? '</a>' : '';
Branch: 1 branch
BranchTrueFalseTotal
$url ? : 2 13 15
844 15 $tbl .= $tr . $pad .'<td>'. $rtc .'</td><td>'. $url . $_ . $aa ."</td></tr>\n";
845 15 $pad = '';
846 15 $tr = '<tr valign=top align=left>';
847 }
848 }
849
850 1 return $txt . $rs . $tbl;
851 }
852  
853 =head1 Monitor Web Page HOWTO
854  
855 Read the C<contrib/HOWTO> document that describes the scripts used with
856 'cron' to auto generate web pages for the statistics reports
857  
858 =head1 EXPORT_OK
859  
860 run
861 plaintxt
862 htmltxt
863 plainresp
864 htmlresp
865  
866 =head1 AUTHOR
867  
868 Michael Robinton, michael@bizsystems.com
869  
870 =head1 COPYRIGHT
871  
872 Copyright 2008-2014, Michael Robinton.
873 This program is free software; you can redistribute it and/or modify
874 it under the terms of the GNU General Public License as published by
875 the Free Software Foundation; either version 2 of the License, or
876 (at your option) any later version.
877  
878 This program is distributed in the hope that it will be useful,
879 but WITHOUT ANY WARRANTY; without even the implied warranty of
880 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
881 GNU General Public License for more details.
882  
883 You should have received a copy of the GNU General Public License
884 along with this program; if not, write to the Free Software
885 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
886  
887 =head1 SEE ALSO
888  
889 L<Mail::SpamCannibal>,
890 L<Net::DNS::Codes>,
891 L<Net::DNS::ToolKit>,
892 L<Net::DNSBL::MultiDaemon>
893 L<NetAddr::IP>
894  
895 =cut
896  
897 7 1;
↑ Summary recurse2txt »