freebsd_amp_hwpstate/contrib/sendmail/contrib/cidrexpand

102 lines
2.1 KiB
Plaintext
Raw Normal View History

2004-08-01 01:04:57 +00:00
#!/usr/bin/perl -w
2006-08-17 05:10:43 +00:00
# $Id: cidrexpand,v 8.4.2.1 2006/08/07 17:22:10 ca Exp $
#
2004-08-01 01:04:57 +00:00
# v 0.4
#
2004-08-01 01:04:57 +00:00
# 17 July 2000 Derek J. Balling (dredd@megacity.org)
2006-08-17 05:10:43 +00:00
#
# Acts as a preparser on /etc/mail/access_db to allow you to use address/bit
2006-08-17 05:10:43 +00:00
# notation.
#
2003-02-08 20:31:29 +00:00
# If you have two overlapping CIDR blocks with conflicting actions
# e.g. 10.2.3.128/25 REJECT and 10.2.3.143 ACCEPT
# make sure that the exceptions to the more general block are specified
# later in the access_db.
#
2003-02-08 20:31:29 +00:00
# the -r flag to makemap will make it "do the right thing"
#
# Modifications
# -------------
2004-08-01 01:04:57 +00:00
# 26 Jul 2001 Derek Balling (dredd@megacity.org)
# Now uses Net::CIDR because it makes life a lot easier.
#
# 5 Nov 2002 Richard Rognlie (richard@sendmail.com)
2003-02-08 20:31:29 +00:00
# Added code to deal with the prefix tags that may now be included in
# the access_db
#
2006-08-17 05:10:43 +00:00
# Added clarification in the notes for what to do if you have
2003-02-08 20:31:29 +00:00
# exceptions to a larger CIDR block.
#
2006-08-17 05:10:43 +00:00
# 3 August 2006
#
# Corrected a bug to have it handle the special case of "0.0.0.0/0"
# since Net::CIDR doesn't handle it properly.
#
# usage:
2003-02-08 20:31:29 +00:00
# cidrexpand < /etc/mail/access | makemap -r hash /etc/mail/access
#
#
2004-08-01 01:04:57 +00:00
# Report bugs to: <dredd@megacity.org>
#
use strict;
use Net::CIDR;
my $spaceregex = '\s+';
while (my $arg = shift @ARGV)
{
2004-08-01 01:04:57 +00:00
if ($arg eq '-t')
{
$spaceregex = shift;
2004-08-01 01:04:57 +00:00
}
}
use strict;
my $SENDMAIL = 1;
while (<>)
{
2004-08-01 01:04:57 +00:00
my ($prefix,$left,$right,$space);
2004-08-01 01:04:57 +00:00
if (! /^(|\S\S*:)(\d+\.){3}\d+\/\d\d?$spaceregex.*/ )
{
print;
2004-08-01 01:04:57 +00:00
}
else
{
2003-02-08 20:31:29 +00:00
($prefix,$left,$space,$right) = /^(|\S\S*:)((?:\d+\.){3}\d+\/\d\d?)($spaceregex)(.*)$/;
2004-08-01 01:04:57 +00:00
my @new_lefts = expand_network($left);
foreach my $nl (@new_lefts)
{
2003-02-08 20:31:29 +00:00
print "$prefix$nl$space$right\n";
}
2004-08-01 01:04:57 +00:00
}
}
2006-08-17 05:10:43 +00:00
sub expand_network
{
2004-08-01 01:04:57 +00:00
my $left_input = shift;
my @rc = ($left_input);
my ($network,$mask) = split /\//, $left_input;
if (defined $mask)
{
2006-08-17 05:10:43 +00:00
return (0..255) if $mask == 0;
2004-08-01 01:04:57 +00:00
my @parts = split /\./, $network;
while ($#parts < 3)
{
2004-08-01 01:04:57 +00:00
push @parts, "0";
}
2004-08-01 01:04:57 +00:00
my $clean_input = join '.', @parts;
$clean_input .= "/$mask";
my @octets = Net::CIDR::cidr2octets($clean_input);
@rc = @octets;
}
return @rc;
}