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

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

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

# do sanity check on given ip
if ( $new_ip !~ /^$ip_regexp$/) {
	die "$new_ip is not in aaa.bbb.ccc.ddd format!\n";
}
print "Debug: trying to add $new_ip\n" if defined($DEBUG);

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

# reopen it for writing
$mode='>>';
open( P, "$mode$etcdir/$static_file") || die "Can't write to $etcdir/$static_file: $!\n";

# check if ip is already in the ignore file, if not append it
if( !grep( /^\s*$new_ip[\s#]/, @ignored_ips)) {
	print "Debug: adding $new_ip!\n" if defined($DEBUG);
	print P "$new_ip\n";
}
close( P);

