Merge from vendor branch GROFF:
[dragonfly.git] / contrib / sendmail-8.13.4 / contrib / cidrexpand
1 #!/usr/bin/perl -w
2
3 # $Id: cidrexpand,v 8.4 2002/11/22 21:13:14 ca Exp $
4 #
5 # v 0.4
6 #
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. 
11 #
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.
16 #
17 # the -r flag to makemap will make it "do the right thing"
18 #
19 # Modifications
20 # -------------
21 # 26 Jul 2001 Derek Balling (dredd@megacity.org)
22 #     Now uses Net::CIDR because it makes life a lot easier.
23 #
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
27 #
28 #     Added clarification in the notes for what to do if you have 
29 #     exceptions to a larger CIDR block.
30 #
31 # usage: 
32 #  cidrexpand < /etc/mail/access | makemap -r hash /etc/mail/access
33 #
34 #
35 # Report bugs to: <dredd@megacity.org>
36 #
37
38
39 use strict;
40 use Net::CIDR;
41
42 my $spaceregex = '\s+';
43
44 while (my $arg = shift @ARGV)
45 {
46     if ($arg eq '-t')
47     {
48         $spaceregex = shift;
49     }
50 }
51
52 use strict;
53
54 my $SENDMAIL = 1;
55
56 while (<>)
57 {
58     my ($prefix,$left,$right,$space);
59
60     if (! /^(|\S\S*:)(\d+\.){3}\d+\/\d\d?$spaceregex.*/ )
61     {
62         print;
63     }
64     else
65     {
66         ($prefix,$left,$space,$right) = /^(|\S\S*:)((?:\d+\.){3}\d+\/\d\d?)($spaceregex)(.*)$/;
67         
68         my @new_lefts = expand_network($left);
69         foreach my $nl (@new_lefts)
70         {
71             print "$prefix$nl$space$right\n";
72         }
73     }
74 }
75     
76 sub expand_network
77 {
78     my $left_input = shift;
79     my @rc = ($left_input);
80     my ($network,$mask) = split /\//, $left_input;
81     if (defined $mask)
82     {
83         my @parts = split /\./, $network;
84         while ($#parts < 3)
85         {
86             push @parts, "0";
87         }
88         my $clean_input = join '.', @parts;
89         $clean_input .= "/$mask";
90         my @octets = Net::CIDR::cidr2octets($clean_input);
91         @rc = @octets;
92     }
93     return @rc;
94 }