#!/usr/bin/perl -T
#########################################################################################
# removerblentry.pl
# Version 3.0.0
#
# Copyright 1997, 1998, 2004 Neil Harkins, William R. Thomas, Harlann Stenn,
# William W. Kimball Jr.
#
#      This file is part of popauth.
#
#      popauth is free software; you can redistribute it and/or modify
#      it under the terms of the GNU General Public License as published by
#      the Free Software Foundation; either version 2 of the License, or
#      (at your option) any later version.
#
#      popauth is distributed in the hope that it will be useful,
#      but WITHOUT ANY WARRANTY; without even the implied warranty of
#      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#      GNU General Public License for more details.
#
#      You should have received a copy of the GNU General Public License
#      along with popauth; if not, write to the Free Software
#      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# This program extends listrbl.pl and listrepeats.pl in order to delete records from the
# popauth RBL cache file.  The directory where the $rbl_cache file is located MUST BE
# WORLD WRITABLE in order for this program to function.  Be sure $path_postfix,
# $path_postfix_data, and $rbl_cache accurately reflect the location of the RBL cache
# file.
#
# Created by:  William W. Kimball Jr. <bkimball1@yahoo.com>
#########################################################################################
# vi: tabstop=3

# Pragmas
use warnings;
use strict;
use diagnostics;
use CGI;

my $path_postfix      = "/etc/postfix/";
my $path_postfix_data = $path_postfix . "data/";
my $rbl_cache         = $path_postfix_data . "rbl_cache";
my $rbl_cache_buffer  = $rbl_cache . ".buffer";
my $rbl_checks        = $path_postfix . "rbl_checks";
my $rbl_checks_db     = $rbl_checks . ".db";
my $cgi               = new CGI;
my $ip                = $cgi->param("id");
my $removecount       = 0;
my $listline          = "";
my @fields;

print "Content-type: text/plain\n\n";

# Don't do anything of the $rbl_cache doesn't yet exist.
if (-e $rbl_cache) {
	# Try to open the $rbl_cache and $rbl_cache_buffer files.
	if (open(RBLLIST, "< " . $rbl_cache)) {
		if (open(BUFFERLIST, "> " . $rbl_cache_buffer)) {
			# Look for a row representing the target IP address.
			while ($listline = <RBLLIST>) {
				chomp($listline);
				@fields = split(/\t/, $listline);

				if ($fields[0] == $ip) {
					# Remove this row by ignoring it.
					++$removecount;
				} else {
					# Preserve this record; These are not the rows we are looking for.
					print(BUFFERLIST $listline . "\n");
				}
			}

			close(BUFFERLIST);

			# Replace the cache file with the updated buffer file.
			rename($rbl_cache_buffer, $rbl_cache);
		}
		else {
			# Log failure.
			print("ERROR:  Unable to open $rbl_cache_buffer for buffer output!");
		}

		close(RBLLIST);
	}
	else {
		# Log failure.
		print("ERROR:  Unable to open $rbl_cache for input!");
	}
}
else {
	# Warn that there is no rbl_cache file.
	print("WARNING:  Cannot complete because $rbl_cache does not exist.");
}

# Report the result of this operation.
if ($removecount > 0) {
	print("\n\n$ip has been removed from $rbl_cache.");
} else {
	print("\n\nOperation failed.");
}

exit(0);
