source: trunk/dnsbl/DNSBL.pm@ 77

Last change on this file since 77 was 77, checked in by Kris Deugau, 10 days ago

/trunk/dnsbl

Finally unpack the subtle SQL tweaks from production that kept excluded
blocks from setting bitmask values, and properly handle setting and
unsetting the per-IP exclusion flag

  • Property svn:keywords set to Date Rev Author Id
File size: 20.0 KB
RevLine 
[2]1# DNSBL
2# Functions for interacting with the DNSBL database
[40]3##
4# $Id: DNSBL.pm 77 2025-09-10 16:22:02Z kdeugau $
[73]5# Copyright 2009-2012,2014,2018,2025 Kris Deugau <kdeugau@deepnet.cx>
[40]6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19##
[2]20
21package DNSBL;
22
23use strict;
24use warnings;
25use Exporter;
[73]26
[2]27use DBI;
28use NetAddr::IP;
29
30use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
31
[73]32$VERSION = 3.0;
[2]33@ISA = qw(Exporter);
[73]34@EXPORT_OK = qw( $dbh );
[2]35
[73]36@EXPORT = qw( $dbh );
[2]37%EXPORT_TAGS = ( ALL => [qw(
38 )]
39 );
40
41## "constants"
42
[36]43# w00t! somewhere along the line, by accident or intent, SA's
44# check_dnsbl_sub can now check up to 24 bits of an DNSBL return value!
[2]45# 1 not available so we don't $self->shoot(foot)
46our %bitfields = (
[50]47 # ip
48 ip => 2,
49 # "I'm a total spamming moron!" - per-IP only!
50 slist => 128,
51
52 # Block listings. Ordering for levels 0, 1, 2 not ideal due to evolution of code.
53 # Levels 3 and higher are more coherently ordered
54
55 # Automatically listed blocks based on IP counts
[2]56 0 => 16,
57 1 => 8,
58 2 => 4,
[50]59 3 => 4096,
60 4 => 32768,
61 5 => 262144,
62 6 => 2097152,
63
64 # Out-of-band
65 org0 => 32,
66 block0 => 64,
67 org1 => 256,
68 org2 => 512,
69 block1 => 1024,
70 block2 => 2048,
71 org3 => 8192,
72 block3 => 16384,
73 org4 => 65536,
74 block4 => 131072,
75 org5 => 524288,
76 block5 => 1048576,
77 org6 => 4194304,
78 block6 => 8388608,
79
[2]80);
81
82# probably needs some tuning; even 7 hits in a /24 is a pretty small percentage
83# number of IPs in a block of the given masklength needed to have that block automatically listed
[24]84# defaults: (overridden by entries in db:autolist)
[2]85our %autolist = (
[10]86 31 => 1,
[2]87 30 => 1,
88 29 => 2,
89 28 => 3,
90 27 => 4,
91 26 => 5,
92 25 => 6,
93 24 => 7,
94 23 => 8,
95 22 => 10,
[15]96 21 => 13,
97 20 => 16,
98 19 => 19,
99 18 => 22,
100 17 => 26,
101 16 => 30,
102 15 => 34,
103 14 => 38,
104 13 => 42,
105 12 => 46,
106 11 => 50,
107 10 => 54,
108 9 => 58,
109 8 => 62,
[2]110 7 => 2**31,
111 6 => 2**31,
112 5 => 2**31,
113 4 => 2**31,
114 3 => 2**31,
115 2 => 2**31,
116 1 => 2**31,
117 0 => 2**31
118);
119
120# le sigh. constants for masklength iterationing
121our @howmany = (1,128,64,32,16,8,4,2,1,128,64,32,16,8,4,2,1,128,64,32,16,8,4,2,1,128,64,32,16,8,4,2);
122
[48]123# hard max depth. There are not enough bits in a 32-bit IP in 127/8 for more than 7 sets of 3 block-level
124# flags, plus one for the IP, plus one for an "alternate" IP flag, plus reserving the least significant bit
125# as a "don't use this because Reasons"
126our $maxlvl = 6;
127
[2]128# variables
129our $dbh;
130
131our $err;
[66]132our $errstr = '';
[2]133
134# basic object subs
135sub new {
[73]136 my $this = shift;
137 my $class = ref($this) || $this;
138 my %args = @_;
[2]139
[73]140 # Prepopulate a basic config. Note some of these *will* cause errors if left unset.
141 my %defconfig = (
142 dbhost => "localhost",
143 dbname => "dnsbl",
144 dbuser => "dnsbl",
145 dbpass => "spambgone",
146 );
147
148 my %siteconfig;
149 my $dbhost;
150 my $dbname;
151 my $dbuser;
152 my $dbpass;
153 if (defined($args{configfile})) {
154 if (-e $args{configfile} && -f $args{configfile}) {
155 my $ret = eval `cat $args{configfile}`;
156 unless ($ret) {
157 if ($@) { $errstr = "couldn't parse $args{configfile}: $@\n"; return; }
158 if (!defined($ret)) { $errstr = "couldn't load $args{configfile}: $!\n"; return; }
159 if (!$ret) { $errstr = "couldn't load $args{configfile}\n"; return; }
160 }
161 # crossload legacy variables, but prefer new %siteconfig values
162 $siteconfig{dbhost} = $dbhost if !$siteconfig{dbhost} && $dbhost;
163 $siteconfig{dbname} = $dbname if !$siteconfig{dbname} && $dbname;
164 $siteconfig{dbuser} = $dbuser if !$siteconfig{dbuser} && $dbuser;
165 $siteconfig{dbpass} = $dbpass if !$siteconfig{dbpass} && $dbpass;
166 }
167 }
168
169 # Assemble the object. Apply configuration hashes in order of precedence.
170 my $self = {
171 # Hardcoded defaults
172 %defconfig,
173 # Default config file OR caller-specified one, loaded above
174 %siteconfig,
175 # Caller-specified arguments
176 %args
177 };
178 bless $self, $class;
179
180 return $self;
181} # new()
182
[2]183sub DESTROY {
184 my $self = shift;
[25]185 $self->dbclose() if $dbh;
[2]186}
187
188# JIC someone wants to close the db but not finish the script
189sub dbclose {
190 $dbh->rollback;
191 $dbh->disconnect;
192}
193
194## specific object subs:
195
196sub connect {
[25]197 my $self = shift;
[2]198 ## want to NOT autocommit everything, it's unlikely we'll step on our own toes but...
[73]199 $dbh = DBI->connect("DBI:Pg:host=$self->{dbhost};dbname=$self->{dbname}", $self->{dbuser}, $self->{dbpass}, {
[2]200 AutoCommit => 0,
201 PrintError => 1
202 })
203 or die "database inaccessible: ".$DBI::errstr;
[24]204 my $sth = $dbh->prepare("SELECT masklen,ipcount FROM autolist");
205 $sth->execute;
206 while (my ($masklen,$ipcount) = $sth->fetchrow_array) {
207 $autolist{$masklen} = $ipcount;
208 }
[2]209 return $dbh;
210}
211
[5]212
[23]213## DNSBLDB::initexport()
214# Prepare a couple of statement handles for later processing in export(). Assists in ~3x speed increase.
215my $parsth;
216my $sthmoron;
217sub initexport {
218 $parsth = $dbh->prepare("SELECT count(i.ip),b.block,b.level,b.listme AS oobblock,o.listme AS ooborg ".
[32]219 "FROM iplist i INNER JOIN blocks b ON i.parent = b.block INNER JOIN orgs o ON b.orgid = o.orgid ".
[23]220 "WHERE b.block >>= ? ".
221 "GROUP BY b.block,b.level,b.listme,o.listme ORDER BY b.block");
[69]222 $sthmoron = $dbh->prepare("SELECT ip,s4list,exclude FROM iplist WHERE parent = ?");
[23]223}
224
225
[5]226## DNSBL::ipexists()
227# return report count if the IP has been reported, otherwise return undef
[2]228sub ipexists {
229 my $self = shift;
230 my $ip = shift;
[66]231 my $sth = $dbh->prepare("SELECT count, exclude FROM iplist WHERE ip=?");
[2]232 $sth->execute($ip);
[66]233 my $ret = $sth->fetchrow_arrayref();
[2]234 return $ret;
[5]235} # end ipexists()
[2]236
237
238# report an IP or URI to the db
239# increments a hit counter iff the reported IP or URI exists, otherwise it adds it
240sub report {
241 my $self = shift;
242 my $rep = shift;
[70]243 my $exclude = shift || 'n';
[77]244 $exclude = 'y' if $exclude eq 'on';
[2]245 my $sth;
246 my $rows = 0;
247 if ($rep =~ /^[\d.]+$/) {
248 # weesa gonna ASS-U-ME IP addresses are sanely formatted.
[32]249 eval {
250 $sth = $dbh->prepare("SELECT count FROM iplist WHERE ip=?");
251 $sth->execute($rep) or die "eep? ".$dbh->errstr."\n";
252 $rows = $sth->rows;
253 if ($rows == 0) {
[66]254 $sth = $dbh->prepare("INSERT INTO iplist (ip,parent,exclude) VALUES ".
255 "(?,(SELECT block FROM blocks WHERE block >> ? ORDER BY level DESC LIMIT 1),?)");
[77]256 $sth->execute($rep, $rep, $exclude) or die "couldn't add entry for $rep: ".$dbh->errstr."\n";
[32]257 } elsif ($rows == 1) {
[66]258 $sth = $dbh->prepare("UPDATE iplist SET count=count+1,".
[77]259 " exclude = ? WHERE ip = ?");
[71]260 $sth->execute($exclude, $rep) or die "couldn't update listing for $rep: ".$dbh->errstr."\n";
[32]261 } else {
262 die "db corrupt: found $rows matches on $rep\n";
263 }
264 $sth = $dbh->prepare("SELECT block FROM blocks WHERE block >> ?");
265 $sth->execute($rep);
[70]266 my $updsth = $dbh->prepare("UPDATE blocks SET ipcount=(".
267 "SELECT count(*) FROM iplist i JOIN blocks b ON b.block=i.parent WHERE i.ip << ? AND i.exclude='n' AND b.exclude='n'".
[77]268 ") WHERE block = ?");
[32]269 while (my ($block) = $sth->fetchrow_array) {
270 $updsth->execute($block,$block);
271 }
272 $dbh->commit;
273 };
274 if ($@) {
275 my $msg = $@;
276 return "failed adding $rep: $msg";
[2]277 }
278 } else {
279 return;
280 }
281 return $rows;
282} # end report()
283
284
285# add a new org
286# return the orgid
287# if the org exists, return the orgid anyway
288sub addorg {
289 my $self = shift;
290 my $orgname = shift;
291 my $listme = shift || 'n';
292 my $ret = $self->orgexists($orgname);
293 return $ret if $ret;
294 my $sth = $dbh->prepare("INSERT INTO orgs (orgname,listme) VALUES (?,?)");
295 $sth->execute($orgname,$listme) or die "couldn't add org $orgname: ".$dbh->errstr."\n";
296 $dbh->commit;
297 $sth = $dbh->prepare("SELECT orgid FROM orgs WHERE orgname=?");
298 $sth->execute($orgname);
299 my ($orgid) = $sth->fetchrow_array();
300 return $orgid;
301} # end addorg
302
303
304# checks for existence - nb, exact match! No way to really handle anything else. :/
305sub orgexists {
306 my $self = shift;
307 my $org = shift;
308 my $sth = $dbh->prepare("SELECT orgid FROM orgs WHERE orgname=?");
309 $sth->execute($org);
310 my ($ret) = $sth->fetchrow_array();
311 return $ret;
312} # end orgexists();
313
314
[54]315# take an arbitrary IP range and an IP, and return the CIDR block (if any) the IP is in.
316sub range2cidr {
317 my $self = shift;
318 my $rstart = shift;
319 my $rend = shift;
320 my $ip = shift;
321
322 $rstart = new NetAddr::IP $rstart;
323 $rend = new NetAddr::IP $rend;
324 # Basic algoithm: Set the mask on the IP, and see if both $rstart and $rend
325 # are within the range defined by that IP/mask. Continue making the mask
326 # larger until success.
327
328 my $mask;
329 for ($mask = 32; $mask > 0; $mask--) {
330 my $ip = NetAddr::IP->new("$ip/$mask");
331 if (NetAddr::IP->new($ip->network->addr) >= $rstart &&
332 NetAddr::IP->new($ip->broadcast->addr) <= $rend) {
333 next;
334 } else {
335 $mask++;
336 last;
337 }
338 }
339 my $realnet = NetAddr::IP->new("$ip/$mask")->network;
340
341 return "$realnet";
342} # end range2cidr()
343
344
[2]345# add a block. requires the orgid
346##fixme needs error handling
347sub addblock {
348 my $self = shift;
349 my $blockin = shift;
350 my $orgid = shift;
351 my $level = shift;
[72]352 my $exclude = shift || 'n';
[66]353 my $comment = shift;
[2]354 $blockin =~ s/^\s+//;
355 $blockin =~ s/\s+$//;
356 my $block = new NetAddr::IP "$blockin"; # need this to clean up messes like ranges. sigh.
357
[11]358 return "$blockin not a single CIDR range" if !$block;
359
[32]360# local $dbh->{AutoCommit} = 1; # force autocommit
[2]361
[32]362 my $sth;
363 eval {
364 my $parent = '0/0';
365 if ($level > 0) {
366 $sth = $dbh->prepare("SELECT block FROM blocks WHERE block >> ? ORDER BY level DESC LIMIT 1");
367 $sth->execute("$block");
368 ($parent) = $sth->fetchrow_array;
369 }
[66]370 $sth = $dbh->prepare("INSERT INTO blocks (block,orgid,level,parent,exclude,comments,ipcount) VALUES (?,?,?,?,?,?,".
[72]371 "(SELECT count(*) FROM iplist i JOIN blocks b ON b.block=i.parent WHERE i.ip << ? AND i.exclude='n' AND b.exclude='n'))");
[66]372 $sth->execute("$block",$orgid,$level,$parent,$exclude,$comment,"$block");
[32]373 $sth = $dbh->prepare("UPDATE iplist SET parent=? WHERE parent=? AND ip << ?");
374 $sth->execute("$block",$parent,"$block");
375 $dbh->commit;
376 };
377 if ($@) {
378 my $msg = $@;
379 eval { dbh->rollback; };
380 return "failed to add $block: $msg";
381 }
[2]382 # nb: no need to return anything, since the CIDR block is the key
383}
384
385
[66]386# Update a netblock entry. Supports (un)setting the exclude flag and the comment.
387# Does NOT do any magic around leftover IPs within the block
388sub updateblock {
389 my $self = shift;
390 my $blockin = shift;
391 my $orgid = shift;
392 my $level = shift;
[72]393 my $exclude = shift || 'n';
[66]394 my $comment = shift;
395 $blockin =~ s/^\s+//;
396 $blockin =~ s/\s+$//;
397 my $block = new NetAddr::IP "$blockin"; # need this to clean up messes like ranges. sigh.
398
399 return "$blockin not a single CIDR range" if !$block;
400
401 local $dbh->{AutoCommit} = 0;
402 local $dbh->{RaiseError} = 1;
403
404 my $sth;
405 eval {
406 my $parent = '0/0';
407 if ($level > 0) {
408 $sth = $dbh->prepare("SELECT block FROM blocks WHERE block >> ? ORDER BY level DESC LIMIT 1");
409 $sth->execute("$block");
410 ($parent) = $sth->fetchrow_array;
411 }
412 $sth = $dbh->prepare("UPDATE blocks SET exclude = ?, comments = ?, ipcount = ".
[77]413 "(SELECT count(*) FROM iplist i JOIN blocks b ON b.block=i.parent WHERE i.ip << ? AND i.exclude='n' AND b.exclude='n')".
[66]414 " WHERE block = ?");
415 $sth->execute($exclude, $comment, "$block", "$block");
416 $sth = $dbh->prepare("UPDATE iplist SET parent=? WHERE parent=? AND ip << ?");
417 $sth->execute("$block", $parent, "$block");
418 $dbh->commit;
419 };
420 if ($@) {
421 my $msg = $@;
422 eval { dbh->rollback; };
423 return "failed to update $block: $msg";
424 }
425 # nb: no need to return anything, since the CIDR block is the key
[72]426} # updateblock()
[66]427
428
[2]429sub blockexists {
430 my $self = shift;
431 my $block = shift;
432 my $sth = $dbh->prepare("SELECT count(*) FROM blocks WHERE block=?");
433 $sth->execute($block);
434 my ($ret) = $sth->fetchrow_array();
435 return $ret;
436}
437
438
[66]439# returns list (block,blockcomment,orgname) for the block that contains the passed IP.
[2]440# accepts a level argument if you don't want the top-level registrar allocation block
441sub getcontainer {
442 my $self = shift;
443 my $ip = shift;
444 my $level = shift || 0;
[66]445 my $sth = $dbh->prepare("SELECT b.block,b.comments,o.orgname FROM blocks b INNER JOIN orgs o ".
[2]446 "ON b.orgid=o.orgid WHERE b.block >> ? AND b.level = ?");
447 $sth->execute($ip,$level);
448 return $sth->fetchrow_array();
449} # end getcontainer()
450
451
[29]452# Get info about whether a block, IP or org is listed
453# Returns ?
454sub islisted {
455 my $self = shift;
456 my $entity = shift;
457
458 my $sth;
459
460 if ($entity =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
461 # looking for IP
462
[66]463 $sth = $dbh->prepare("SELECT ip,s4list,exclude FROM iplist WHERE ip=?");
[29]464 $sth->execute($entity);
465 my @ret = $sth->fetchrow_array;
466 return @ret if @ret;
467
468 } elsif ($entity =~ m|^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/(\d+)$|) {
469 # block
470
471 my $masklen = $1;
472
[66]473 $sth = $dbh->prepare("SELECT block,listme,exclude,ipcount FROM blocks WHERE block = ?");
[29]474 $sth->execute($entity);
[66]475 my ($block, $listme, $exclude, $bcount) = $sth->fetchrow_array;
[29]476
477 return if !$block;
478
[66]479 my @ret = ( ($bcount >= $autolist{$masklen}), $listme, $exclude);
[29]480 return @ret;
481
482 } else {
483 # org
484
485 $sth = $dbh->prepare("SELECT orgid,listme FROM orgs WHERE orgname=?");
486 $sth->execute($entity);
487 my ($orgid,$listme) = $sth->fetchrow_array;
488 return $listme if $orgid;
489
490 }
491
492 return undef;
493
494} # end islisted()
495
496
[2]497# whee! Recursion is Fun!
498# Call ourself to dig down through the layers of blocks from registar-allocation
[48]499# (level 0) to final block (level n, not to exceed $maxlvl)
[2]500# Take a reference to a hash, and stuff it full of blacklisting goodness.
501# Optionally accept a level, block-container, and OOB block and org arguments for
502# the container to check and return
503# Returns no value directly
504# Calls itself to walk down the tree of containers
505sub export {
506 my $self = shift;
507 my $listhosts = shift;
508
509# Export data as CIDR netblocks or classful (A/B/C) blocks
510# Assume classful as it's more compatible with different DNS servers
511 my $mode = shift || 'class';
512
513# Assume we're checking the whole enchilada if we don't get told where to look.
514 my $level = shift || 0;
515 my $container = shift || '0.0.0.0/0';
[23]516 my $bitmask = shift || 0;
[2]517
[66]518 if ($level == 0) {
519 $errstr = '';
520 }
521
522 return if ($errstr =~ /no connection to the server/);
[48]523 if ($level > $maxlvl) {
[2]524 warn "getting too deep, breaking off! ($container, $level)\n";
525 return;
526 }
527
528# fiddle $container into a sane state.
529 if ($container =~ m|^\d+\.\d+\.\d+/\d+$|) {
530 $container =~ s|/(\d+)$|.0/$1|;
531 } elsif ($container =~ m|^\d+\.\d+/\d+$|) {
532 $container =~ s|/(\d+)$|.0.0/$1|;
533 } elsif ($container =~ m|^\d+/(\d+)$|) {
534 $container =~ s|/(\d+)$|.0.0.0/$1|;
535 }
536
[66]537
538 # catch database-went-away errors
539 local $dbh->{RaiseError} = 1;
540 eval {
541
542
[32]543 my $sth = $dbh->prepare("SELECT count(*) FROM blocks WHERE parent = ?");
[2]544 $sth->execute($container);
545 my ($nblocks) = $sth->fetchrow_array();
546
[23]547 # need this for a bunch of things, may as well do it here
548 my ($masklen) = ($container =~ m|/(\d+)$|);
549
550# Update the bitmask variable with the current block info as needed.
551# Much faster than retrieving this data later (~3x faster!).
552 my $listme;
553 my $listorg;
554 my $bcount;
[66]555 my $bexclude;
[23]556 if ($container ne '0.0.0.0/0') {
[66]557 $sth = $dbh->prepare("SELECT b.ipcount,b.listme,b.exclude,o.listme ".
[23]558 "FROM blocks b INNER JOIN orgs o ON b.orgid=o.orgid ".
559 "WHERE b.block = ?");
560 $sth->execute($container);
[66]561 ($bcount,$listme,$bexclude,$listorg) = $sth->fetchrow_array();
[77]562 $bcount = 0 if !$bcount;
[23]563 $bitmask |= $bitfields{$level-1} if $bcount >= $autolist{$masklen};
[36]564 $bitmask |= $bitfields{"block".($level-1)} if $listme;
565 $bitmask |= $bitfields{"org".($level-1)} if $listorg;
[23]566 }
567
568# hm. can't seem to move this prepare elsewhere. :(
[2]569 if ($nblocks > 0) {
[66]570 my $sthsubblocks = $dbh->prepare("SELECT block,exclude FROM blocks ".
[32]571 "WHERE level = ? AND parent = ?");
[23]572 $sthsubblocks->execute($level, $container);
[66]573 while (my ($cidr, $exclude) = $sthsubblocks->fetchrow_array()) {
574 if ($exclude) {
575 $listhosts->{$cidr} = -1;
576 } else { # don't check subtrees of an excluded block; rbldnsd doesn't support deep flip-flopping like that
577 $self->export($listhosts,$mode,$level+1,$cidr,$bitmask)
578 or die $errstr;
579 }
[2]580 }
581 } # avoid checking content of subs if we don't have any
582
583 # don't check all 4.2 billion IPs individually if we're looking at all teh Intarwebs
584 return if $container eq '0.0.0.0/0';
585
586##fixme: need a way to dig out orphan IPs at all levels - IPs not found in a
587# subblock of the current container when the current container *has* subblocks
588# NB: this may be better handled as an out-of-band data-integrity-checker
589
590 # decrement level here so the right bitfield setting gets picked. this segment
591 # is inherently off-by-one from the block-recursion loop, and I can't see a
592 # better way to work around that. >:(
593 $level--;
594
[4]595 if ($mode eq 'cidr') {
[23]596 $listhosts->{$container} |= $bitmask if $bitmask && ($listme || $listorg || ($bcount >= $autolist{$masklen}));
[2]597 } else {
598 # if $cidr->masklen is <= 24, iterate on /24 boundaries for bulk sublisting
599 # if $cidr->masklen is <= 16, iterate on /16 boundaries for bulk sublisting
600 # if $cidr->masklen is <= 8, iterate on /8 boundaries for bulk sublisting
601
[23]602 if ($bitmask) {
[2]603 my @blocksubs;
604 if ($masklen <= 30 && $masklen > 24) {
605 my ($net,$octet) = ($container =~ m|^(\d+\.\d+\.\d+\.)(\d+)/|);
606 for (my $entry = $octet; $entry < ($octet + $howmany[$masklen]); $entry++) {
607 my $host = "$net$entry";
608 $listhosts->{$host} = 0 if !defined($listhosts->{$host});
[23]609 $listhosts->{$host} |= $bitmask;
[2]610 }
611 } elsif ($masklen <= 24 && $masklen > 16) {
612 my ($net,$octet) = ($container =~ m|^(\d+\.\d+\.)(\d+)\.\d+/|);
613 for (my $entry = $octet; $entry < ($octet + $howmany[$masklen]); $entry++) {
614 my $twofour = "$net$entry.*";
[23]615 $listhosts->{$twofour} |= $bitmask;
[2]616 }
617 } elsif ($masklen <= 16 && $masklen > 8) {
618 my ($net,$octet) = ($container =~ m|^(\d+\.)(\d+)\.\d+\.\d+/|);
619 for (my $entry = $octet; $entry < ($octet + $howmany[$masklen]); $entry++) {
620 my $sixteen = "$net$entry.*";
[23]621 $listhosts->{$sixteen} |= $bitmask;
[2]622 }
623 } elsif ($masklen <= 8) {
624 my ($octet) = ($container =~ m|^(\d+)\.\d+\.\d+\.\d+/|);
625 for (my $entry = $octet; $entry < ($octet + $howmany[$masklen]); $entry++) {
626 my $eight = "$entry.*";
[23]627 $listhosts->{$eight} |= $bitmask;
[2]628 }
629 }
630
631 } # generate autolist entries for ips/octets not (yet) seen in reports
632
633 } # cidr vs classful mode
634
[23]635 $sthmoron->execute($container);
[66]636 while (my ($ip,$moron,$exclude) = $sthmoron->fetchrow_array()) {
[2]637 if ($moron) {
638 $listhosts->{$ip} = $bitfields{slist};
[66]639 } elsif ($exclude) {
640 $listhosts->{$ip} = -1;
[2]641 } else {
[66]642 $listhosts->{$ip} |= $bitmask;
[2]643 $listhosts->{$ip} |= $bitfields{ip};
644 }
645 }
646
[66]647
648 }; # db-went-away-catching eval
649 if ($@) {
650 $errstr = $@;
651 warn "export truncated: $errstr\n";
652 return;
653 }
654
655
[2]656# get IPs which for reasons unknown are apparently allocated directly from the
657# parent registry (and so do not have containing netblocks in this system) O_o
658# select * from iplist where not (select count(*) from blocks where ip << block) > 0;
659
[66]660 return 1;
[2]661} # end export()
662
663
664## DNSBL::autolist_block()
665# check if a block should be autolisted
666sub autolist_block {
667 my $self = shift;
668 my $block = shift;
669
670 my $cidr = new NetAddr::IP "$block";
[32]671 my $sth = $dbh->prepare("SELECT ipcount FROM blocks WHERE block = ?");
[2]672 $sth->execute("$cidr");
673 my ($count) = $sth->fetchrow_array;
674
675 return 1 if $count >= $autolist{$cidr->masklen};
676 return 0;
677} # end autolist_block()
678
679
680# make Perl happy
6811;
Note: See TracBrowser for help on using the repository browser.