1 | #!/usr/bin/perl
|
---|
2 | # hack to set new parent column on blocks and iplist tables
|
---|
3 |
|
---|
4 | use strict;
|
---|
5 | use warnings;
|
---|
6 | use DBI;
|
---|
7 |
|
---|
8 | use DNSBL;
|
---|
9 |
|
---|
10 | my $dnsbl = new DNSBL;
|
---|
11 |
|
---|
12 | # default DB info - all other settings should be loaded from the DB.
|
---|
13 | my $dbhost = "dbhost";
|
---|
14 | my $dbname = "dnsbl";
|
---|
15 | my $dbuser = "dnsbl";
|
---|
16 | my $dbpass = "spambgone";
|
---|
17 |
|
---|
18 | die "Need config argument\n" if !$ARGV[0];
|
---|
19 | my $cfgname = shift @ARGV;
|
---|
20 |
|
---|
21 | # Load a config ref containing DB host, name, user, and pass info based on
|
---|
22 | # from the server name + full script web path. This allows us to host
|
---|
23 | # multiple instances without having to duplicate the code.
|
---|
24 | # This file is a Perl fragment to be processed inline.
|
---|
25 | if (-e "/etc/dnsbl/$cfgname.conf") {
|
---|
26 | my $cfg = `cat /etc/dnsbl/$cfgname.conf`;
|
---|
27 | ($cfg) = ($cfg =~ /^(.+)$/s); # avoid warnings, failures, and general nastiness with taint mode
|
---|
28 | eval $cfg;
|
---|
29 | }
|
---|
30 |
|
---|
31 | my $dbh = $dnsbl->connect($dbhost, $dbname, $dbuser, $dbpass);
|
---|
32 |
|
---|
33 | print "block parents\n";
|
---|
34 | my $sthplist = $dbh->prepare("SELECT block FROM blocks WHERE level=?");
|
---|
35 | my $sthclist = $dbh->prepare("UPDATE blocks SET parent=? WHERE level=? AND block << ?");
|
---|
36 | foreach my $level ((0,1)) {
|
---|
37 | $sthplist->execute($level) or die "failed to get blocklist: ".$sthplist->errstr."\n";
|
---|
38 | while (my ($block) = $sthplist->fetchrow_array) {
|
---|
39 | $sthclist->execute($block,$level+1,$block) or die "failed setparent on $block subs: ".$sthclist->errstr."\n";
|
---|
40 | }
|
---|
41 | }
|
---|
42 | print " done\n";
|
---|
43 |
|
---|
44 | print "block counts\n";
|
---|
45 | my $sthblist = $dbh->prepare("SELECT block FROM blocks");
|
---|
46 | my $sthcount = $dbh->prepare("UPDATE blocks SET ipcount=(SELECT count(*) FROM iplist WHERE ip << ?) WHERE block = ?");
|
---|
47 | $sthblist->execute;
|
---|
48 | while (my ($block) = $sthblist->fetchrow_array) {
|
---|
49 | $sthcount->execute($block,$block) or die "failed setcount on $block: ".$sthcount->errstr."\n";
|
---|
50 | }
|
---|
51 | print " done\n";
|
---|
52 |
|
---|
53 | print "ip parents\n";
|
---|
54 | $sthplist = $dbh->prepare("SELECT block FROM blocks ORDER BY level");
|
---|
55 | $sthclist = $dbh->prepare("UPDATE iplist SET parent = ? WHERE ip << ?");
|
---|
56 | $sthplist->execute;
|
---|
57 | while (my ($block) = $sthplist->fetchrow_array) {
|
---|
58 | $sthclist->execute($block,$block);
|
---|
59 | }
|
---|
60 |
|
---|
61 | # and yes, we do this in one huge block.
|
---|
62 | $dbh->commit;
|
---|
63 | print " done\n";
|
---|