head	1.1;
branch	1.1.1;
access;
symbols
	RELENG_8_4:1.1.1.5.0.30
	RELENG_9_1_0_RELEASE:1.1.1.5
	RELENG_9_1:1.1.1.5.0.28
	RELENG_9_1_BP:1.1.1.5
	RELENG_8_3_0_RELEASE:1.1.1.5
	RELENG_8_3:1.1.1.5.0.26
	RELENG_8_3_BP:1.1.1.5
	RELENG_9_0_0_RELEASE:1.1.1.5
	RELENG_9_0:1.1.1.5.0.24
	RELENG_9_0_BP:1.1.1.5
	RELENG_9:1.1.1.5.0.22
	RELENG_9_BP:1.1.1.5
	RELENG_7_4_0_RELEASE:1.1.1.5
	RELENG_8_2_0_RELEASE:1.1.1.5
	RELENG_7_4:1.1.1.5.0.20
	RELENG_7_4_BP:1.1.1.5
	RELENG_8_2:1.1.1.5.0.18
	RELENG_8_2_BP:1.1.1.5
	RELENG_8_1_0_RELEASE:1.1.1.5
	RELENG_8_1:1.1.1.5.0.16
	RELENG_8_1_BP:1.1.1.5
	RELENG_7_3_0_RELEASE:1.1.1.5
	RELENG_7_3:1.1.1.5.0.14
	RELENG_7_3_BP:1.1.1.5
	RELENG_8_0_0_RELEASE:1.1.1.5
	RELENG_8_0:1.1.1.5.0.12
	RELENG_8_0_BP:1.1.1.5
	RELENG_8:1.1.1.5.0.10
	RELENG_8_BP:1.1.1.5
	RELENG_7_2_0_RELEASE:1.1.1.5
	RELENG_7_2:1.1.1.5.0.8
	RELENG_7_2_BP:1.1.1.5
	RELENG_7_1_0_RELEASE:1.1.1.5
	RELENG_6_4_0_RELEASE:1.1.1.3.8.2
	RELENG_7_1:1.1.1.5.0.6
	RELENG_7_1_BP:1.1.1.5
	RELENG_6_4:1.1.1.3.8.2.0.4
	RELENG_6_4_BP:1.1.1.3.8.2
	RELENG_7_0_0_RELEASE:1.1.1.5
	RELENG_6_3_0_RELEASE:1.1.1.3.8.2
	RELENG_7_0:1.1.1.5.0.4
	RELENG_7_0_BP:1.1.1.5
	RELENG_6_3:1.1.1.3.8.2.0.2
	RELENG_6_3_BP:1.1.1.3.8.2
	v8_14_2:1.1.1.5
	RELENG_7:1.1.1.5.0.2
	RELENG_7_BP:1.1.1.5
	v8_14_1:1.1.1.5
	RELENG_6_2_0_RELEASE:1.1.1.3.8.1
	RELENG_6_2:1.1.1.3.8.1.0.2
	RELENG_6_2_BP:1.1.1.3.8.1
	v8_13_8:1.1.1.4
	v8_13_7:1.1.1.3
	RELENG_5_5_0_RELEASE:1.1.1.3
	RELENG_5_5:1.1.1.3.0.14
	RELENG_5_5_BP:1.1.1.3
	RELENG_6_1_0_RELEASE:1.1.1.3
	RELENG_6_1:1.1.1.3.0.12
	RELENG_6_1_BP:1.1.1.3
	v8_13_6:1.1.1.3
	RELENG_6_0_0_RELEASE:1.1.1.3
	RELENG_6_0:1.1.1.3.0.10
	RELENG_6_0_BP:1.1.1.3
	RELENG_6:1.1.1.3.0.8
	RELENG_6_BP:1.1.1.3
	v8_13_4:1.1.1.3
	RELENG_5_4_0_RELEASE:1.1.1.3
	RELENG_5_4:1.1.1.3.0.6
	RELENG_5_4_BP:1.1.1.3
	v8_13_3:1.1.1.3
	RELENG_4_11_0_RELEASE:1.1.1.1.2.3
	RELENG_4_11:1.1.1.1.2.3.0.2
	RELENG_4_11_BP:1.1.1.1.2.3
	RELENG_5_3_0_RELEASE:1.1.1.3
	RELENG_5_3:1.1.1.3.0.4
	RELENG_5_3_BP:1.1.1.3
	RELENG_5:1.1.1.3.0.2
	RELENG_5_BP:1.1.1.3
	v8_13_1:1.1.1.3
	RELENG_4_10_0_RELEASE:1.1.1.1.2.2
	RELENG_4_10:1.1.1.1.2.2.0.6
	RELENG_4_10_BP:1.1.1.1.2.2
	RELENG_5_2_1_RELEASE:1.1.1.2
	v8_12_11:1.1.1.2
	RELENG_5_2_0_RELEASE:1.1.1.2
	RELENG_5_2:1.1.1.2.0.4
	RELENG_5_2_BP:1.1.1.2
	RELENG_4_9_0_RELEASE:1.1.1.1.2.2
	RELENG_4_9:1.1.1.1.2.2.0.4
	RELENG_4_9_BP:1.1.1.1.2.2
	v8_12_10:1.1.1.2
	RELENG_5_1_0_RELEASE:1.1.1.2
	RELENG_5_1:1.1.1.2.0.2
	RELENG_5_1_BP:1.1.1.2
	RELENG_4_8_0_RELEASE:1.1.1.1.2.2
	v8_12_9:1.1.1.2
	RELENG_4_8:1.1.1.1.2.2.0.2
	RELENG_4_8_BP:1.1.1.1.2.2
	v8_12_8:1.1.1.2
	v8_12_7:1.1.1.2
	RELENG_5_0_0_RELEASE:1.1.1.1
	RELENG_5_0:1.1.1.1.0.4
	RELENG_5_0_BP:1.1.1.1
	RELENG_4_7_0_RELEASE:1.1.1.1.2.1
	RELENG_4_7:1.1.1.1.2.1.0.10
	RELENG_4_7_BP:1.1.1.1.2.1
	v8_12_6:1.1.1.1
	RELENG_4_6_2_RELEASE:1.1.1.1.2.1
	RELENG_4_6_1_RELEASE:1.1.1.1.2.1
	v8_12_5:1.1.1.1
	v8_12_4:1.1.1.1
	RELENG_4_6_0_RELEASE:1.1.1.1.2.1
	RELENG_4_6:1.1.1.1.2.1.0.8
	RELENG_4_6_BP:1.1.1.1.2.1
	v8_12_3_20020420:1.1.1.1
	v8_12_3:1.1.1.1
	v8_12_2:1.1.1.1
	RELENG_4_5_0_RELEASE:1.1.1.1.2.1
	RELENG_4_5:1.1.1.1.2.1.0.6
	RELENG_4_5_BP:1.1.1.1.2.1
	RELENG_4_4_0_RELEASE:1.1.1.1.2.1
	RELENG_4_4:1.1.1.1.2.1.0.4
	RELENG_4_4_BP:1.1.1.1.2.1
	v8_11_6:1.1.1.1
	v8_11_5:1.1.1.1
	v8_11_4:1.1.1.1
	RELENG_4_3_0_RELEASE:1.1.1.1.2.1
	RELENG_4_3:1.1.1.1.2.1.0.2
	RELENG_4_3_BP:1.1.1.1.2.1
	v8_11_3:1.1.1.1
	v8_11_2:1.1.1.1
	RELENG_4_2_0_RELEASE:1.1.1.1.2.1
	v8_11_1:1.1.1.1
	RELENG_4_1_1_RELEASE:1.1.1.1.2.1
	PRE_SMPNG:1.1.1.1
	RELENG_4:1.1.1.1.0.2
	v8_11_0:1.1.1.1
	SENDMAIL:1.1.1;
locks; strict;
comment	@# @;


1.1
date	2000.08.12.21.55.41;	author gshapiro;	state Exp;
branches
	1.1.1.1;
next	;

1.1.1.1
date	2000.08.12.21.55.41;	author gshapiro;	state Exp;
branches
	1.1.1.1.2.1;
next	1.1.1.2;

1.1.1.2
date	2003.02.08.20.31.24;	author gshapiro;	state Exp;
branches;
next	1.1.1.3;

1.1.1.3
date	2004.08.01.01.04.52;	author gshapiro;	state Exp;
branches
	1.1.1.3.2.1
	1.1.1.3.8.1;
next	1.1.1.4;

1.1.1.4
date	2006.08.17.05.09.13;	author gshapiro;	state Exp;
branches;
next	1.1.1.5;

1.1.1.5
date	2007.04.09.01.38.49;	author gshapiro;	state Exp;
branches
	1.1.1.5.30.1;
next	;

1.1.1.1.2.1
date	2000.08.27.17.31.13;	author gshapiro;	state Exp;
branches;
next	1.1.1.1.2.2;

1.1.1.1.2.2
date	2003.02.13.18.03.14;	author gshapiro;	state Exp;
branches;
next	1.1.1.1.2.3;

1.1.1.1.2.3
date	2004.08.09.00.15.36;	author gshapiro;	state Exp;
branches;
next	1.1.1.1.2.4;

1.1.1.1.2.4
date	2006.08.23.03.39.11;	author gshapiro;	state Exp;
branches;
next	1.1.1.1.2.5;

1.1.1.1.2.5
date	2007.04.12.03.12.57;	author gshapiro;	state Exp;
branches;
next	;

1.1.1.3.2.1
date	2006.08.23.03.34.52;	author gshapiro;	state Exp;
branches;
next	1.1.1.3.2.2;

1.1.1.3.2.2
date	2007.04.12.03.12.21;	author gshapiro;	state Exp;
branches;
next	;

1.1.1.3.8.1
date	2006.08.23.03.29.51;	author gshapiro;	state Exp;
branches;
next	1.1.1.3.8.2;

1.1.1.3.8.2
date	2007.04.12.03.11.35;	author gshapiro;	state Exp;
branches;
next	;

1.1.1.5.30.1
date	2007.04.09.01.38.49;	author svnexp;	state dead;
branches;
next	1.1.1.5.30.2;

1.1.1.5.30.2
date	2013.03.28.13.01.58;	author svnexp;	state Exp;
branches;
next	;


desc
@@


1.1
log
@Initial revision
@
text
@#!/usr/local/bin/perl -w

# v 0.2-very-very-beta
#
# 17 July 2000 Derek J. Balling (dredd@@megacity.org)
#
# The $SENDMAIL flag tells the code to lump networks in sendmail format
# if applicable. If this flag is disabled, cidrexpand will literally create
# a single line for each entry, which may or may not be what you want. :)
# makes for a rather large hash table...
#
# Acts as a preparser on /etc/mail/access_db to allow you to use address/bit
# notation. Caveat: the address portion MUST be the start address or your
# results will NOT be what what you want.
#
#
# usage:
#  cidrexpand < /etc/mail/access | makemap hash /etc/mail/access
#
#
# Report bugs to: dredd@@megacity.org
#

my $spaceregex = '\s+';

while (my $arg = shift @@ARGV)
{
     if ($arg eq '-t')
     {
	$spaceregex = shift;
     }
}

use strict;

my $SENDMAIL = 1;

while (<>)
{
     my ($left,$right,$space);

     if (! /^(\d+\.){3}\d+\/\d\d?$spaceregex.*/ )
     {
	print;
     }
     else
     {
	($left,$space,$right) = /^((?:\d+\.){3}\d+\/\d\d?)($spaceregex)(.*)$/;

	my @@new_lefts = expand_network($left);
	foreach my $nl (@@new_lefts)
	{
	    print "$nl$space$right\n";
	}

     }
}
    
sub expand_network
{
     my ($network,$mask) = split /\//, shift;
     my @@diffs = calc_changes($network,$mask);
     my ($first,$second,$third,$fourth) = split /\./, $network;

     my @@rc = ();

     for my $f ($first..($first+$diffs[0]))
     {
	if ( ( $SENDMAIL ) and ($diffs[1] == 255))
	{
	    push @@rc, "$f";
	}
	else
	{
	    for my $s ($second..($second+$diffs[1]))
	    {
		if ( ($SENDMAIL) and ($diffs[2] == 255) )
		{
		    push @@rc,"$f\.$s";
		}
		else
		{
		    for my $t ($third..($third+$diffs[2]))
		    {
			if ( ($SENDMAIL) and ($diffs[3] == 255))
			{
			    push @@rc, "$f\.$s\.$t";
			}
			else
			{
			    for my $fr ($fourth..($fourth+$diffs[3]))
			    {
				push @@rc, "$f\.$s\.$t\.$fr";
			    }
			}
		    }
		}
	    }
	}
     }
     return @@rc;
}

sub calc_changes
{
     my ($network,$mask) = @@_;
    
     my @@octs = split /\./, $network;
    
     my ($first,$second,$third,$fourth) = (0,0,0,0);
    
     my $power = 32 - $mask;
    
     if ($mask > 24)
     {
	$fourth = 2**$power - 1;
     }
     elsif ($mask > 16)
     {
	$fourth = 255;
	$third = 2**($power-8) - 1;
     }
     elsif ($mask > 8)
     {
	$fourth = 255;
	$third  = 255;
	$second = 2**($power-16) - 1;
     }
     elsif ($mask > 0)
     {
	$fourth = 255;
	$third = 255;
	$second = 255;
	$first = 2**($power-24) - 1;
     }
     return ($first,$second,$third,$fourth);
}
@


1.1.1.1
log
@Import of sendmail version 8.11.0 into vendor branch SENDMAIL with
release tag v8_11_0.

Obtained from: ftp://ftp.sendmail.org/pub/sendmail/
@
text
@@


1.1.1.2
log
@Import sendmail 8.12.7
@
text
@d3 1
a3 1
# v 0.2-very-beta
a15 12
# 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.
#
# the -r flag to makemap will make it "do the right thing"
#
# Modifications
# -------------
#  5 Nov  2002 Richard Rognlie (richard@@sendmail.com)
#     Added code to deal with the prefix tags that may now be included in
#     the access_db
d17 2
a18 2
#     Added clarification in the notes for what to do if you have 
#     exceptions to a larger CIDR block.
a19 2
# usage:
#  cidrexpand < /etc/mail/access | makemap -r hash /etc/mail/access
d40 1
a40 1
     my ($prefix,$left,$right,$space);
d42 1
a42 1
     if (! /^(|\S\S*:)(\d+\.){3}\d+\/\d\d?$spaceregex.*/ )
d48 1
a48 1
	($prefix,$left,$space,$right) = /^(|\S\S*:)((?:\d+\.){3}\d+\/\d\d?)($spaceregex)(.*)$/;
d53 1
a53 1
	    print "$prefix$nl$space$right\n";
a135 8
     elsif ($mask == 0)
     {
	$fourth = 255;
	$third = 255;
	$second = 255;
	$first = 255;
     }

@


1.1.1.3
log
@Import sendmail 8.13.1
@
text
@d1 1
a1 1
#!/usr/bin/perl -w
d3 1
a3 1
# $Id: cidrexpand,v 8.4 2002/11/22 21:13:14 ca Exp $
d5 6
a10 1
# v 0.4
a11 2
# 17 July 2000 Derek J. Balling (dredd@@megacity.org)
# 
d13 2
a14 1
# notation. 
d25 1
a25 4
# 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)
d32 1
a32 1
# usage: 
d35 1
a35 2
#
# Report bugs to: <dredd@@megacity.org>
a37 4

use strict;
use Net::CIDR;

d42 2
a43 2
    if ($arg eq '-t')
    {
d45 1
a45 1
    }
d54 1
a54 1
    my ($prefix,$left,$right,$space);
d56 2
a57 2
    if (! /^(|\S\S*:)(\d+\.){3}\d+\/\d\d?$spaceregex.*/ )
    {
d59 3
a61 3
    }
    else
    {
d63 1
a63 1
	
d69 2
a70 1
    }
d75 9
a83 7
    my $left_input = shift;
    my @@rc = ($left_input);
    my ($network,$mask) = split /\//, $left_input;
    if (defined $mask)
    {
	my @@parts = split /\./, $network;
	while ($#parts < 3)
d85 1
a85 1
	    push @@parts, "0";
d87 72
a158 6
	my $clean_input = join '.', @@parts;
	$clean_input .= "/$mask";
	my @@octets = Net::CIDR::cidr2octets($clean_input);
	@@rc = @@octets;
    }
    return @@rc;
@


1.1.1.3.2.1
log
@MFC: sendmail 8.13.8
@
text
@d3 1
a3 1
# $Id: cidrexpand,v 8.4.2.1 2006/08/07 17:22:10 ca Exp $
d8 1
a8 1
#
d10 1
a10 1
# notation.
d28 1
a28 1
#     Added clarification in the notes for what to do if you have
d31 1
a31 6
#  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:
d75 1
a75 1

a82 2
	return (0..255)	if $mask == 0;

@


1.1.1.3.2.2
log
@MFC: sendmail 8.14.1
@
text
@d3 1
a3 1
# $Id: cidrexpand,v 8.8 2006/08/07 17:18:37 ca Exp $
d8 1
a8 1
# 
d10 1
a10 1
# notation. 
d28 1
a28 1
#     Added clarification in the notes for what to do if you have 
a30 10
#  26 Jul 2006 Richard Rognlie (richard@@sendmail.com>
#     Added code to strip "comments" (anything after a non-escaped #)
#     # characters after a \ or within quotes (single and double) are
#     left intact. 
#
#     e.g.
#	From:1.2.3.4	550 Die spammer # spammed us 2006.07.26
#     becomes
#	From:1.2.3.4	550 Die spammer 
#
d36 1
a36 1
# usage: 
a45 1
use Getopt::Std;
d47 1
a47 2
our ($opt_c,$opt_t);
getopts('ct:');
d49 1
a49 2
my $spaceregex = '\s+';
if ($opt_t)
d51 4
a54 1
    $spaceregex = $opt_t;
d57 4
a62 1
    chomp;
a64 29
    if ( (/\#/) && $opt_c )
    {
	# print "checking...\n";
	my $i;
	my $qtype='';
	for ($i=0 ; $i<length($_) ; $i++) 
	{
	    my $ch = substr($_,$i,1);
	    if ($ch eq '\\') 
	    {
		$i++;
		next;
	    }
	    elsif ($qtype eq '' && $ch eq '#') 
	    {
		substr($_,$i) = '';
		last;
	    }
	    elsif ($qtype ne '' && $ch eq $qtype) 
	    {
		$qtype = '';
	    }
	    elsif ($qtype eq '' && $ch =~ /[\'\"]/) 
	    {
		$qtype = $ch;
	    }
	}
    } 
    
d67 1
a67 1
	print "$_\n";
d71 1
a71 2
	($prefix,$left,$space,$right) = 
	    /^(|\S\S*:)((?:\d+\.){3}\d+\/\d\d?)($spaceregex)(.*)$/;
d80 1
a80 1
    
@


1.1.1.3.8.1
log
@MFC: sendmail 8.13.8
@
text
@d3 1
a3 1
# $Id: cidrexpand,v 8.4.2.1 2006/08/07 17:22:10 ca Exp $
d8 1
a8 1
#
d10 1
a10 1
# notation.
d28 1
a28 1
#     Added clarification in the notes for what to do if you have
d31 1
a31 6
#  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:
d75 1
a75 1

a82 2
	return (0..255)	if $mask == 0;

@


1.1.1.3.8.2
log
@MFC: sendmail 8.14.1
@
text
@d3 1
a3 1
# $Id: cidrexpand,v 8.8 2006/08/07 17:18:37 ca Exp $
d8 1
a8 1
# 
d10 1
a10 1
# notation. 
d28 1
a28 1
#     Added clarification in the notes for what to do if you have 
a30 10
#  26 Jul 2006 Richard Rognlie (richard@@sendmail.com>
#     Added code to strip "comments" (anything after a non-escaped #)
#     # characters after a \ or within quotes (single and double) are
#     left intact. 
#
#     e.g.
#	From:1.2.3.4	550 Die spammer # spammed us 2006.07.26
#     becomes
#	From:1.2.3.4	550 Die spammer 
#
d36 1
a36 1
# usage: 
a45 1
use Getopt::Std;
d47 1
a47 2
our ($opt_c,$opt_t);
getopts('ct:');
d49 1
a49 2
my $spaceregex = '\s+';
if ($opt_t)
d51 4
a54 1
    $spaceregex = $opt_t;
d57 4
a62 1
    chomp;
a64 29
    if ( (/\#/) && $opt_c )
    {
	# print "checking...\n";
	my $i;
	my $qtype='';
	for ($i=0 ; $i<length($_) ; $i++) 
	{
	    my $ch = substr($_,$i,1);
	    if ($ch eq '\\') 
	    {
		$i++;
		next;
	    }
	    elsif ($qtype eq '' && $ch eq '#') 
	    {
		substr($_,$i) = '';
		last;
	    }
	    elsif ($qtype ne '' && $ch eq $qtype) 
	    {
		$qtype = '';
	    }
	    elsif ($qtype eq '' && $ch =~ /[\'\"]/) 
	    {
		$qtype = $ch;
	    }
	}
    } 
    
d67 1
a67 1
	print "$_\n";
d71 1
a71 2
	($prefix,$left,$space,$right) = 
	    /^(|\S\S*:)((?:\d+\.){3}\d+\/\d\d?)($spaceregex)(.*)$/;
d80 1
a80 1
    
@


1.1.1.4
log
@Import sendmail 8.13.8
@
text
@d3 1
a3 1
# $Id: cidrexpand,v 8.4.2.1 2006/08/07 17:22:10 ca Exp $
d8 1
a8 1
#
d10 1
a10 1
# notation.
d28 1
a28 1
#     Added clarification in the notes for what to do if you have
d31 1
a31 6
#  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:
d75 1
a75 1

a82 2
	return (0..255)	if $mask == 0;

@


1.1.1.5
log
@Import sendmail 8.14.1
@
text
@d3 1
a3 1
# $Id: cidrexpand,v 8.8 2006/08/07 17:18:37 ca Exp $
d8 1
a8 1
# 
d10 1
a10 1
# notation. 
d28 1
a28 1
#     Added clarification in the notes for what to do if you have 
a30 10
#  26 Jul 2006 Richard Rognlie (richard@@sendmail.com>
#     Added code to strip "comments" (anything after a non-escaped #)
#     # characters after a \ or within quotes (single and double) are
#     left intact. 
#
#     e.g.
#	From:1.2.3.4	550 Die spammer # spammed us 2006.07.26
#     becomes
#	From:1.2.3.4	550 Die spammer 
#
d36 1
a36 1
# usage: 
a45 1
use Getopt::Std;
d47 1
a47 2
our ($opt_c,$opt_t);
getopts('ct:');
d49 1
a49 2
my $spaceregex = '\s+';
if ($opt_t)
d51 4
a54 1
    $spaceregex = $opt_t;
d57 4
a62 1
    chomp;
a64 29
    if ( (/\#/) && $opt_c )
    {
	# print "checking...\n";
	my $i;
	my $qtype='';
	for ($i=0 ; $i<length($_) ; $i++) 
	{
	    my $ch = substr($_,$i,1);
	    if ($ch eq '\\') 
	    {
		$i++;
		next;
	    }
	    elsif ($qtype eq '' && $ch eq '#') 
	    {
		substr($_,$i) = '';
		last;
	    }
	    elsif ($qtype ne '' && $ch eq $qtype) 
	    {
		$qtype = '';
	    }
	    elsif ($qtype eq '' && $ch =~ /[\'\"]/) 
	    {
		$qtype = $ch;
	    }
	}
    } 
    
d67 1
a67 1
	print "$_\n";
d71 1
a71 2
	($prefix,$left,$space,$right) = 
	    /^(|\S\S*:)((?:\d+\.){3}\d+\/\d\d?)($spaceregex)(.*)$/;
d80 1
a80 1
    
@


1.1.1.5.30.1
log
@file cidrexpand was added on branch RELENG_8_4 on 2013-03-28 13:01:58 +0000
@
text
@d1 138
@


1.1.1.5.30.2
log
@## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/248810
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
@
text
@a0 138
#!/usr/bin/perl -w

# $Id: cidrexpand,v 8.8 2006/08/07 17:18:37 ca Exp $
#
# v 0.4
#
# 17 July 2000 Derek J. Balling (dredd@@megacity.org)
# 
# Acts as a preparser on /etc/mail/access_db to allow you to use address/bit
# notation. 
#
# 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.
#
# the -r flag to makemap will make it "do the right thing"
#
# Modifications
# -------------
# 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)
#     Added code to deal with the prefix tags that may now be included in
#     the access_db
#
#     Added clarification in the notes for what to do if you have 
#     exceptions to a larger CIDR block.
#
#  26 Jul 2006 Richard Rognlie (richard@@sendmail.com>
#     Added code to strip "comments" (anything after a non-escaped #)
#     # characters after a \ or within quotes (single and double) are
#     left intact. 
#
#     e.g.
#	From:1.2.3.4	550 Die spammer # spammed us 2006.07.26
#     becomes
#	From:1.2.3.4	550 Die spammer 
#
#  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: 
#  cidrexpand < /etc/mail/access | makemap -r hash /etc/mail/access
#
#
# Report bugs to: <dredd@@megacity.org>
#


use strict;
use Net::CIDR;
use Getopt::Std;

our ($opt_c,$opt_t);
getopts('ct:');

my $spaceregex = '\s+';
if ($opt_t)
{
    $spaceregex = $opt_t;
}

while (<>)
{
    chomp;
    my ($prefix,$left,$right,$space);

    if ( (/\#/) && $opt_c )
    {
	# print "checking...\n";
	my $i;
	my $qtype='';
	for ($i=0 ; $i<length($_) ; $i++) 
	{
	    my $ch = substr($_,$i,1);
	    if ($ch eq '\\') 
	    {
		$i++;
		next;
	    }
	    elsif ($qtype eq '' && $ch eq '#') 
	    {
		substr($_,$i) = '';
		last;
	    }
	    elsif ($qtype ne '' && $ch eq $qtype) 
	    {
		$qtype = '';
	    }
	    elsif ($qtype eq '' && $ch =~ /[\'\"]/) 
	    {
		$qtype = $ch;
	    }
	}
    } 
    
    if (! /^(|\S\S*:)(\d+\.){3}\d+\/\d\d?$spaceregex.*/ )
    {
	print "$_\n";
    }
    else
    {
	($prefix,$left,$space,$right) = 
	    /^(|\S\S*:)((?:\d+\.){3}\d+\/\d\d?)($spaceregex)(.*)$/;
	
	my @@new_lefts = expand_network($left);
	foreach my $nl (@@new_lefts)
	{
	    print "$prefix$nl$space$right\n";
	}
    }
}
    
sub expand_network
{
    my $left_input = shift;
    my @@rc = ($left_input);
    my ($network,$mask) = split /\//, $left_input;
    if (defined $mask)
    {
	return (0..255)	if $mask == 0;

	my @@parts = split /\./, $network;
	while ($#parts < 3)
	{
	    push @@parts, "0";
	}
	my $clean_input = join '.', @@parts;
	$clean_input .= "/$mask";
	my @@octets = Net::CIDR::cidr2octets($clean_input);
	@@rc = @@octets;
    }
    return @@rc;
}
@


1.1.1.1.2.1
log
@MFC: Import of sendmail 8.11.0.
     Includes: moving /etc/aliases to /etc/mail/aliases, removing outdated
               documentation, and updating freebsd.mc for new syntax.
@
text
@@


1.1.1.1.2.2
log
@MFC: sendmail 8.12.7
@
text
@d3 1
a3 1
# v 0.2-very-beta
a15 12
# 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.
#
# the -r flag to makemap will make it "do the right thing"
#
# Modifications
# -------------
#  5 Nov  2002 Richard Rognlie (richard@@sendmail.com)
#     Added code to deal with the prefix tags that may now be included in
#     the access_db
d17 2
a18 2
#     Added clarification in the notes for what to do if you have 
#     exceptions to a larger CIDR block.
a19 2
# usage:
#  cidrexpand < /etc/mail/access | makemap -r hash /etc/mail/access
d40 1
a40 1
     my ($prefix,$left,$right,$space);
d42 1
a42 1
     if (! /^(|\S\S*:)(\d+\.){3}\d+\/\d\d?$spaceregex.*/ )
d48 1
a48 1
	($prefix,$left,$space,$right) = /^(|\S\S*:)((?:\d+\.){3}\d+\/\d\d?)($spaceregex)(.*)$/;
d53 1
a53 1
	    print "$prefix$nl$space$right\n";
a135 8
     elsif ($mask == 0)
     {
	$fourth = 255;
	$third = 255;
	$second = 255;
	$first = 255;
     }

@


1.1.1.1.2.3
log
@MFC: sendmail 8.13.1 import and related changes
@
text
@d1 1
a1 1
#!/usr/bin/perl -w
d3 1
a3 1
# $Id: cidrexpand,v 8.4 2002/11/22 21:13:14 ca Exp $
d5 6
a10 1
# v 0.4
a11 2
# 17 July 2000 Derek J. Balling (dredd@@megacity.org)
# 
d13 2
a14 1
# notation. 
d25 1
a25 4
# 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)
d32 1
a32 1
# usage: 
d35 1
a35 2
#
# Report bugs to: <dredd@@megacity.org>
a37 4

use strict;
use Net::CIDR;

d42 2
a43 2
    if ($arg eq '-t')
    {
d45 1
a45 1
    }
d54 1
a54 1
    my ($prefix,$left,$right,$space);
d56 2
a57 2
    if (! /^(|\S\S*:)(\d+\.){3}\d+\/\d\d?$spaceregex.*/ )
    {
d59 3
a61 3
    }
    else
    {
d63 1
a63 1
	
d69 2
a70 1
    }
d75 9
a83 7
    my $left_input = shift;
    my @@rc = ($left_input);
    my ($network,$mask) = split /\//, $left_input;
    if (defined $mask)
    {
	my @@parts = split /\./, $network;
	while ($#parts < 3)
d85 1
a85 1
	    push @@parts, "0";
d87 72
a158 6
	my $clean_input = join '.', @@parts;
	$clean_input .= "/$mask";
	my @@octets = Net::CIDR::cidr2octets($clean_input);
	@@rc = @@octets;
    }
    return @@rc;
@


1.1.1.1.2.4
log
@MFC: sendmail 8.13.8
@
text
@d3 1
a3 1
# $Id: cidrexpand,v 8.4.2.1 2006/08/07 17:22:10 ca Exp $
d8 1
a8 1
#
d10 1
a10 1
# notation.
d28 1
a28 1
#     Added clarification in the notes for what to do if you have
d31 1
a31 6
#  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:
d75 1
a75 1

a82 2
	return (0..255)	if $mask == 0;

@


1.1.1.1.2.5
log
@MFC: sendmail 8.14.1
@
text
@d3 1
a3 1
# $Id: cidrexpand,v 8.8 2006/08/07 17:18:37 ca Exp $
d8 1
a8 1
# 
d10 1
a10 1
# notation. 
d28 1
a28 1
#     Added clarification in the notes for what to do if you have 
a30 10
#  26 Jul 2006 Richard Rognlie (richard@@sendmail.com>
#     Added code to strip "comments" (anything after a non-escaped #)
#     # characters after a \ or within quotes (single and double) are
#     left intact. 
#
#     e.g.
#	From:1.2.3.4	550 Die spammer # spammed us 2006.07.26
#     becomes
#	From:1.2.3.4	550 Die spammer 
#
d36 1
a36 1
# usage: 
a45 1
use Getopt::Std;
d47 1
a47 2
our ($opt_c,$opt_t);
getopts('ct:');
d49 1
a49 2
my $spaceregex = '\s+';
if ($opt_t)
d51 4
a54 1
    $spaceregex = $opt_t;
d57 4
a62 1
    chomp;
a64 29
    if ( (/\#/) && $opt_c )
    {
	# print "checking...\n";
	my $i;
	my $qtype='';
	for ($i=0 ; $i<length($_) ; $i++) 
	{
	    my $ch = substr($_,$i,1);
	    if ($ch eq '\\') 
	    {
		$i++;
		next;
	    }
	    elsif ($qtype eq '' && $ch eq '#') 
	    {
		substr($_,$i) = '';
		last;
	    }
	    elsif ($qtype ne '' && $ch eq $qtype) 
	    {
		$qtype = '';
	    }
	    elsif ($qtype eq '' && $ch =~ /[\'\"]/) 
	    {
		$qtype = $ch;
	    }
	}
    } 
    
d67 1
a67 1
	print "$_\n";
d71 1
a71 2
	($prefix,$left,$space,$right) = 
	    /^(|\S\S*:)((?:\d+\.){3}\d+\/\d\d?)($spaceregex)(.*)$/;
d80 1
a80 1
    
@


