Import sendmail 8.13.7
[dragonfly.git] / contrib / sendmail-8.13.7 / contrib / cidrexpand
blobb7ace25adfbe3aa787c306607a6fa7657425787b
1 #!/usr/bin/perl -w
3 # $Id: cidrexpand,v 8.4 2002/11/22 21:13:14 ca Exp $
5 # v 0.4
7 # 17 July 2000 Derek J. Balling (dredd@megacity.org)
8 #
9 # Acts as a preparser on /etc/mail/access_db to allow you to use address/bit
10 # notation.
12 # If you have two overlapping CIDR blocks with conflicting actions
13 # e.g. 10.2.3.128/25 REJECT and 10.2.3.143 ACCEPT
14 # make sure that the exceptions to the more general block are specified
15 # later in the access_db.
17 # the -r flag to makemap will make it "do the right thing"
19 # Modifications
20 # -------------
21 # 26 Jul 2001 Derek Balling (dredd@megacity.org)
22 # Now uses Net::CIDR because it makes life a lot easier.
24 # 5 Nov 2002 Richard Rognlie (richard@sendmail.com)
25 # Added code to deal with the prefix tags that may now be included in
26 # the access_db
28 # Added clarification in the notes for what to do if you have
29 # exceptions to a larger CIDR block.
31 # usage:
32 # cidrexpand < /etc/mail/access | makemap -r hash /etc/mail/access
35 # Report bugs to: <dredd@megacity.org>
39 use strict;
40 use Net::CIDR;
42 my $spaceregex = '\s+';
44 while (my $arg = shift @ARGV)
46 if ($arg eq '-t')
48 $spaceregex = shift;
52 use strict;
54 my $SENDMAIL = 1;
56 while (<>)
58 my ($prefix,$left,$right,$space);
60 if (! /^(|\S\S*:)(\d+\.){3}\d+\/\d\d?$spaceregex.*/ )
62 print;
64 else
66 ($prefix,$left,$space,$right) = /^(|\S\S*:)((?:\d+\.){3}\d+\/\d\d?)($spaceregex)(.*)$/;
68 my @new_lefts = expand_network($left);
69 foreach my $nl (@new_lefts)
71 print "$prefix$nl$space$right\n";
76 sub expand_network
78 my $left_input = shift;
79 my @rc = ($left_input);
80 my ($network,$mask) = split /\//, $left_input;
81 if (defined $mask)
83 my @parts = split /\./, $network;
84 while ($#parts < 3)
86 push @parts, "0";
88 my $clean_input = join '.', @parts;
89 $clean_input .= "/$mask";
90 my @octets = Net::CIDR::cidr2octets($clean_input);
91 @rc = @octets;
93 return @rc;