#!/usr/bin/perl -w
# $Id: portsentry-rm-ip,v 1.3 2001/07/01 22:51:00 agx Exp $
#
# remove an IP from portsentry.ignore.static file
#
# by Guido Guenther <agx@debian.org>

use File::Temp qw( tempfile );

$etcdir='/etc/portsentry';			
$static_file="$etcdir/portsentry.ignore.static";
$tmpdir="/var/lib/portsentry";
$ip_regexp='[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}';

if( @ARGV == 0) {
	die 
"Remove an ip from portsentry.ignore.static.
usage: portsentry-add-ip ip-address\n";
} else {
        $rm_ip = $ARGV[0];
}

if ( $rm_ip !~ /^$ip_regexp$/) {
	die "$rm_ip is not in aaa.bbb.ccc.ddd format!\n";
}

open( P, "$static_file") || die "Can't read $static_file: $!\n";
@ignored_ips = <P>;
close( P);

($th, $tfname) = tempfile("portsentry.ignore.static.XXXXXX", DIR => $tmpdir);

# write out everything except the mathing ip
@tmp_ips=grep( !/^\s*$rm_ip[\s#]/, @ignored_ips);
print $th @tmp_ips;
close( $th);

# move temp-file to portsentry.ignore.static
rename($tfname, $static_file);
chmod( 0644, $static_file );
