| 1 | #!/usr/bin/perl
 | 
|---|
| 2 | # Delist an IP
 | 
|---|
| 3 | # 2010/09/07 kdeugau@deepnet.cx
 | 
|---|
| 4 | 
 | 
|---|
| 5 | use strict;
 | 
|---|
| 6 | use warnings;
 | 
|---|
| 7 | use DBI;
 | 
|---|
| 8 | 
 | 
|---|
| 9 | use DNSBL;
 | 
|---|
| 10 | 
 | 
|---|
| 11 | my $dnsbl = new DNSBL;
 | 
|---|
| 12 | 
 | 
|---|
| 13 | # default DB info - all other settings should be loaded from the DB.
 | 
|---|
| 14 | my $dbhost = "localhost";
 | 
|---|
| 15 | my $dbname = "dnsbl";
 | 
|---|
| 16 | my $dbuser = "dnsbl";
 | 
|---|
| 17 | my $dbpass = "spambgone";
 | 
|---|
| 18 | 
 | 
|---|
| 19 | die "Usage: delist-ip <list> <IP>\n".
 | 
|---|
| 20 |         "  <list> should be the DNSBL you want to remove the IP from\n"
 | 
|---|
| 21 |         if !$ARGV[1];
 | 
|---|
| 22 | my $cfgname = shift @ARGV;
 | 
|---|
| 23 | 
 | 
|---|
| 24 | # Load a config ref containing DB host, name, user, and pass info based on
 | 
|---|
| 25 | # from the server name + full script web path.  This allows us to host
 | 
|---|
| 26 | # multiple instances without having to duplicate the code.
 | 
|---|
| 27 | # This file is a Perl fragment to be processed inline.
 | 
|---|
| 28 | if (-e "/etc/dnsbl/$cfgname.conf") {
 | 
|---|
| 29 |   my $cfg = `cat /etc/dnsbl/$cfgname.conf`;
 | 
|---|
| 30 |   ($cfg) = ($cfg =~ /^(.+)$/s);         # avoid warnings, failures, and general nastiness with taint mode
 | 
|---|
| 31 |   eval $cfg;
 | 
|---|
| 32 | }
 | 
|---|
| 33 | 
 | 
|---|
| 34 | my $dbh = $dnsbl->connect($dbhost, $dbname, $dbuser, $dbpass);
 | 
|---|
| 35 | 
 | 
|---|
| 36 | my %config;
 | 
|---|
| 37 | my $sth = $dbh->prepare("SELECT key,value FROM misc");
 | 
|---|
| 38 | $sth->execute;
 | 
|---|
| 39 | while (my ($key,$value) = $sth->fetchrow_array) {
 | 
|---|
| 40 |   $config{$key} = $value;
 | 
|---|
| 41 | }
 | 
|---|
| 42 | 
 | 
|---|
| 43 | my $removeme = $ARGV[0];
 | 
|---|
| 44 | 
 | 
|---|
| 45 | $sth = $dbh->prepare("SELECT ip,count,s4list,added FROM iplist WHERE ip=?");
 | 
|---|
| 46 | $sth->execute($removeme);
 | 
|---|
| 47 | my ($ip,$count,$s4list,$added) = $sth->fetchrow_array;
 | 
|---|
| 48 | 
 | 
|---|
| 49 | die "IP $removeme not found.  Exiting.\n" if !$ip;
 | 
|---|
| 50 | 
 | 
|---|
| 51 | # need to do the next in a single transaction
 | 
|---|
| 52 | local $dbh->{AutoCommit} = 0;
 | 
|---|
| 53 | local $dbh->{RaiseError} = 1;
 | 
|---|
| 54 | eval {
 | 
|---|
| 55 |   $sth = $dbh->prepare("INSERT INTO waslisted (ip,count,s4list,origadded) VALUES (?,?,?,?)");
 | 
|---|
| 56 |   $sth->execute($ip,$count,$s4list,$added);
 | 
|---|
| 57 |   $sth = $dbh->prepare("DELETE FROM iplist WHERE ip=?");
 | 
|---|
| 58 |   $sth->execute($ip);
 | 
|---|
| 59 |   $dbh->commit;
 | 
|---|
| 60 | };
 | 
|---|
| 61 | if ($@) {
 | 
|---|
| 62 |   my $msg = $@;
 | 
|---|
| 63 |   eval { $dbh->rollback; };
 | 
|---|
| 64 |   print "Failed to move record from iplist to waslisted: $msg\n";
 | 
|---|
| 65 | }
 | 
|---|