- Timestamp:
- 03/25/11 17:16:38 (14 years ago)
- Location:
- trunk/dnsbl
- Files:
-
- 4 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/dnsbl/DNSBL.pm
r29 r32 138 138 sub initexport { 139 139 $parsth = $dbh->prepare("SELECT count(i.ip),b.block,b.level,b.listme AS oobblock,o.listme AS ooborg ". 140 "FROM iplist i INNER JOIN blocks b ON i. ip <<b.block INNER JOIN orgs o ON b.orgid = o.orgid ".140 "FROM iplist i INNER JOIN blocks b ON i.parent = b.block INNER JOIN orgs o ON b.orgid = o.orgid ". 141 141 "WHERE b.block >>= ? ". 142 142 "GROUP BY b.block,b.level,b.listme,o.listme ORDER BY b.block"); 143 $sthmoron = $dbh->prepare("SELECT ip,s4list FROM iplist WHERE ip << ? ORDER BY ip");143 $sthmoron = $dbh->prepare("SELECT ip,s4list FROM iplist WHERE parent = ?"); 144 144 } 145 145 … … 166 166 if ($rep =~ /^[\d.]+$/) { 167 167 # weesa gonna ASS-U-ME IP addresses are sanely formatted. 168 $sth = $dbh->prepare("SELECT count FROM iplist WHERE ip=?"); 169 $sth->execute($rep) or die "eep? ".$dbh->errstr."\n"; 170 $rows = $sth->rows; 171 if ($rows == 0) { 172 $sth = $dbh->prepare("INSERT INTO iplist (ip) VALUES (?)"); 173 } elsif ($rows == 1) { 174 $sth = $dbh->prepare("UPDATE iplist SET count=count+1 WHERE ip=?"); 175 } else { 176 die "db corrupt: found $rows matches on $rep\n"; 168 eval { 169 $sth = $dbh->prepare("SELECT count FROM iplist WHERE ip=?"); 170 $sth->execute($rep) or die "eep? ".$dbh->errstr."\n"; 171 $rows = $sth->rows; 172 if ($rows == 0) { 173 $sth = $dbh->prepare("INSERT INTO iplist (ip,parent) VALUES ". 174 "(?,(SELECT block FROM blocks WHERE block >> ? ORDER BY level DESC LIMIT 1))"); 175 $sth->execute($rep,$rep) or die "couldn't add entry for $rep: ".$dbh->errstr."\n"; 176 } elsif ($rows == 1) { 177 $sth = $dbh->prepare("UPDATE iplist SET count=count+1 WHERE ip=?"); 178 $sth->execute($rep) or die "couldn't update listing for $rep: ".$dbh->errstr."\n"; 179 } else { 180 die "db corrupt: found $rows matches on $rep\n"; 181 } 182 $sth = $dbh->prepare("SELECT block FROM blocks WHERE block >> ?"); 183 $sth->execute($rep); 184 my $updsth = $dbh->prepare("UPDATE blocks SET ipcount=(SELECT count(*) FROM iplist WHERE ip << ?) WHERE block=?"); 185 while (my ($block) = $sth->fetchrow_array) { 186 $updsth->execute($block,$block); 187 } 188 $dbh->commit; 189 }; 190 if ($@) { 191 my $msg = $@; 192 return "failed adding $rep: $msg"; 177 193 } 178 $sth->execute($rep) or die "couldn't update listing for $rep: ".$dbh->errstr."\n";179 194 } else { 180 195 return; 181 196 } 182 $dbh->commit;183 197 return $rows; 184 198 } # end report() … … 228 242 return "$blockin not a single CIDR range" if !$block; 229 243 230 local $dbh->{AutoCommit} = 1; # force autocommit 231 232 my $sth = $dbh->prepare("INSERT INTO blocks (block,orgid,level) VALUES (?,?,?)"); 233 $sth->execute("$block",$orgid,$level); 234 return $sth->errstr if $sth->err; 244 # local $dbh->{AutoCommit} = 1; # force autocommit 245 246 my $sth; 247 eval { 248 my $parent = '0/0'; 249 if ($level > 0) { 250 $sth = $dbh->prepare("SELECT block FROM blocks WHERE block >> ? ORDER BY level DESC LIMIT 1"); 251 $sth->execute("$block"); 252 ($parent) = $sth->fetchrow_array; 253 } 254 $sth = $dbh->prepare("INSERT INTO blocks (block,orgid,level,parent,ipcount) VALUES (?,?,?,?,". 255 "(SELECT count(*) FROM iplist WHERE ip << ?))"); 256 $sth->execute("$block",$orgid,$level,$parent,"$block"); 257 $sth = $dbh->prepare("UPDATE iplist SET parent=? WHERE parent=? AND ip << ?"); 258 $sth->execute("$block",$parent,"$block"); 259 $dbh->commit; 260 }; 261 if ($@) { 262 my $msg = $@; 263 eval { dbh->rollback; }; 264 return "failed to add $block: $msg"; 265 } 235 266 # nb: no need to return anything, since the CIDR block is the key 236 267 } … … 287 318 return if !$block; 288 319 289 $sth = $dbh->prepare("SELECT count(*) FROM iplist WHERE ip <<?");320 $sth = $dbh->prepare("SELECT ipcount FROM blocks WHERE block = ?"); 290 321 $sth->execute($entity); 291 322 my ($bcount) = $sth->fetchrow_array; … … 343 374 } 344 375 345 my $sth = $dbh->prepare("SELECT count(*) FROM blocks WHERE block <<?");376 my $sth = $dbh->prepare("SELECT count(*) FROM blocks WHERE parent = ?"); 346 377 $sth->execute($container); 347 378 my ($nblocks) = $sth->fetchrow_array(); … … 356 387 my $bcount; 357 388 if ($container ne '0.0.0.0/0') { 358 $sth = $dbh->prepare("SELECT count(*) FROM iplist WHERE ip << ?"); 359 $sth->execute($container); 360 ($bcount) = $sth->fetchrow_array(); 361 362 $sth = $dbh->prepare("SELECT b.listme,o.listme ". 389 $sth = $dbh->prepare("SELECT b.ipcount,b.listme,o.listme ". 363 390 "FROM blocks b INNER JOIN orgs o ON b.orgid=o.orgid ". 364 391 "WHERE b.block = ?"); 365 392 $sth->execute($container); 366 ($ listme,$listorg) = $sth->fetchrow_array();393 ($bcount,$listme,$listorg) = $sth->fetchrow_array(); 367 394 368 395 $bitmask |= $bitfields{$level-1} if $bcount >= $autolist{$masklen}; … … 374 401 if ($nblocks > 0) { 375 402 my $sthsubblocks = $dbh->prepare("SELECT block FROM blocks ". 376 "WHERE level =? and block << ? ORDER BY block, masklen(block) DESC");403 "WHERE level = ? AND parent = ?"); 377 404 $sthsubblocks->execute($level, $container); 378 405 while (my ($cidr) = $sthsubblocks->fetchrow_array()) { … … 490 517 491 518 my $cidr = new NetAddr::IP "$block"; 492 my $sth = $dbh->prepare(" select count(*) from iplist where ip <<?");519 my $sth = $dbh->prepare("SELECT ipcount FROM blocks WHERE block = ?"); 493 520 $sth->execute("$cidr"); 494 521 my ($count) = $sth->fetchrow_array; -
trunk/dnsbl/browse.cgi
r25 r32 54 54 my $basesql = "SELECT b.block,o.orgname,b.listme,o.listme,b.comments,o.comments ". 55 55 "FROM blocks b INNER JOIN orgs o ON b.orgid=o.orgid ". 56 "WHERE b. block <<= ";56 "WHERE b.parent = "; 57 57 my $sth0 = $dbh->prepare($basesql."'0/0' AND b.level=0 ORDER BY block"); 58 58 #my $sth0 = $dbh->prepare($basesql."'64/8' AND b.level=0 ORDER BY block"); 59 59 my $sth1 = $dbh->prepare($basesql."? AND b.level=1 ORDER BY block"); 60 60 my $sth2 = $dbh->prepare($basesql."? AND b.level=2 ORDER BY block"); 61 my $sthiplist = $dbh->prepare("select * from iplist where ip <<= ? order by ip");61 my $sthiplist = $dbh->prepare("select * from iplist where parent = ? order by ip"); 62 62 63 63 my %ipseen; -
trunk/dnsbl/dnsbl.cgi
r29 r32 96 96 97 97 my $err = ''; 98 my $count = $dnsbl->report($webvar{ip});99 98 my $org0id = $dnsbl->orgexists($webvar{org0}); 100 99 if (!$org0id) { … … 132 131 } 133 132 } 133 my $count = $dnsbl->report($webvar{ip}); 134 134 135 135 $page->param(ip => $webvar{ip}); -
trunk/dnsbl/dnsbl.sql
r29 r32 5 5 SET client_encoding = 'UTF8'; 6 6 SET check_function_bodies = false; 7 SET client_min_messages = warning; 7 8 8 9 -- 9 -- TOC entry 7 (OID 196077)10 10 -- Name: blocks; Type: TABLE; Schema: public; Owner: dnsbl 11 11 -- … … 16 16 orgid integer, 17 17 "level" integer DEFAULT 0, 18 comments text DEFAULT ''::text 18 comments text DEFAULT ''::text, 19 parent cidr NOT NULL, 20 ipcount integer NOT NULL 19 21 ); 20 22 21 23 22 24 -- 23 -- TOC entry 8 (OID 196085)24 25 -- Name: iplist; Type: TABLE; Schema: public; Owner: dnsbl 25 26 -- … … 29 30 count integer DEFAULT 1, 30 31 s4list boolean DEFAULT false, 31 added timestamp with time zone DEFAULT now() 32 added timestamp with time zone DEFAULT now(), 33 parent cidr NOT NULL 32 34 ); 33 35 34 36 35 37 -- 36 -- TOC entry 9 (OID 196090)37 38 -- Name: orgs; Type: TABLE; Schema: public; Owner: dnsbl 38 39 -- … … 47 48 48 49 -- 49 -- TOC entry 10 (OID 196099)50 50 -- Name: autolist; Type: TABLE; Schema: public; Owner: dnsbl 51 51 -- … … 58 58 59 59 -- 60 -- TOC entry 11 (OID 196147)61 60 -- Name: misc; Type: TABLE; Schema: public; Owner: dnsbl 62 61 -- … … 70 69 -- for tracking IPs that get removed from listing 71 70 -- note we don't want a PK on IP as with iplist, since we may end up removing an IP more than once. :/ 71 -- 72 -- Name: waslisted; Type: TABLE; Schema: public; Owner: dnsbl 73 -- 72 74 73 75 CREATE TABLE waslisted ( … … 79 81 ); 80 82 83 81 84 -- 82 -- Data for TOC entry 23 (OID 196099) 85 -- Name: autolist_pkey; Type: CONSTRAINT; Schema: public; Owner: dnsbl 86 -- 87 88 ALTER TABLE ONLY autolist 89 ADD CONSTRAINT autolist_pkey PRIMARY KEY (masklen); 90 91 92 -- 93 -- Name: blocks_pkey; Type: CONSTRAINT; Schema: public; Owner: dnsbl 94 -- 95 96 ALTER TABLE ONLY blocks 97 ADD CONSTRAINT blocks_pkey PRIMARY KEY (block); 98 99 100 -- 101 -- Name: iplist_pkey; Type: CONSTRAINT; Schema: public; Owner: dnsbl 102 -- 103 104 ALTER TABLE ONLY iplist 105 ADD CONSTRAINT iplist_pkey PRIMARY KEY (ip); 106 107 108 -- 109 -- Name: orgs_pkey; Type: CONSTRAINT; Schema: public; Owner: dnsbl 110 -- 111 112 ALTER TABLE ONLY orgs 113 ADD CONSTRAINT orgs_pkey PRIMARY KEY (orgid); 114 115 116 -- 117 -- Name: blocks_parent_index; Type: INDEX; Schema: public; Owner: dnsbl 118 -- 119 120 CREATE INDEX blocks_parent_index ON blocks USING btree (parent); 121 122 123 -- 124 -- Name: ip_parent_index; Type: INDEX; Schema: public; Owner: dnsbl 125 -- 126 127 CREATE INDEX ip_parent_index ON iplist USING btree (parent); 128 129 130 -- 131 -- Name: blocks_orgid_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dnsbl 132 -- 133 134 ALTER TABLE ONLY blocks 135 ADD CONSTRAINT blocks_orgid_fkey FOREIGN KEY (orgid) REFERENCES orgs(orgid); 136 137 138 -- 83 139 -- Name: autolist; Type: TABLE DATA; Schema: public; Owner: dnsbl 84 140 -- … … 118 174 0 2147483648 119 175 \. 120 121 122 --123 -- TOC entry 12 (OID 196101)124 -- Name: blocks_pkey; Type: CONSTRAINT; Schema: public; Owner: dnsbl125 --126 127 ALTER TABLE ONLY blocks128 ADD CONSTRAINT blocks_pkey PRIMARY KEY (block);129 130 131 --132 -- TOC entry 13 (OID 196103)133 -- Name: iplist_pkey; Type: CONSTRAINT; Schema: public; Owner: dnsbl134 --135 136 ALTER TABLE ONLY iplist137 ADD CONSTRAINT iplist_pkey PRIMARY KEY (ip);138 139 140 --141 -- TOC entry 14 (OID 196105)142 -- Name: orgs_pkey; Type: CONSTRAINT; Schema: public; Owner: dnsbl143 --144 145 ALTER TABLE ONLY orgs146 ADD CONSTRAINT orgs_pkey PRIMARY KEY (orgid);147 148 149 --150 -- TOC entry 15 (OID 196215)151 -- Name: autolist_pkey; Type: CONSTRAINT; Schema: public; Owner: dnsbl152 --153 154 ALTER TABLE ONLY autolist155 ADD CONSTRAINT autolist_pkey PRIMARY KEY (masklen);156 157 158 --159 -- TOC entry 25 (OID 196107)160 -- Name: blocks_orgid_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dnsbl161 --162 163 ALTER TABLE ONLY blocks164 ADD CONSTRAINT blocks_orgid_fkey FOREIGN KEY (orgid) REFERENCES orgs(orgid);165 -
trunk/dnsbl/export-dnsbl
r29 r32 47 47 my $mode = $ARGV[0] || 'tiny'; 48 48 49 #$dnsbl->export($ipref,$mode,1,'65.60/18');50 #$dnsbl->export($ipref,$mode,1,'67.136.0.0/14');51 #$dnsbl->export($ipref,$mode,1,'83.76/15');52 #$dnsbl->export($ipref,$mode,1,'76.73.0.0/17');53 #$dnsbl->export($ipref,$mode,1,'174.36.0.0/15');54 49 $dnsbl->initexport; 50 #$dnsbl->export($ipref,$mode,1,'50.22.0.0/15'); 55 51 $dnsbl->export($ipref,$mode); 56 52
Note:
See TracChangeset
for help on using the changeset viewer.