Merge from vendor branch OPENSSL:
[dragonfly.git] / contrib / sendmail-8.13.8 / contrib / cidrexpand
1 #!/usr/bin/perl -w
2
3 # $Id: cidrexpand,v 8.4.2.1 2006/08/07 17:22:10 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 #  3 August 2006
32 #
33 #     Corrected a bug to have it handle the special case of "0.0.0.0/0"
34 #     since Net::CIDR doesn't handle it properly.
35 #
36 # usage:
37 #  cidrexpand < /etc/mail/access | makemap -r hash /etc/mail/access
38 #
39 #
40 # Report bugs to: <dredd@megacity.org>
41 #
42
43
44 use strict;
45 use Net::CIDR;
46
47 my $spaceregex = '\s+';
48
49 while (my $arg = shift @ARGV)
50 {
51     if ($arg eq '-t')
52     {
53         $spaceregex = shift;
54     }
55 }
56
57 use strict;
58
59 my $SENDMAIL = 1;
60
61 while (<>)
62 {
63     my ($prefix,$left,$right,$space);
64
65     if (! /^(|\S\S*:)(\d+\.){3}\d+\/\d\d?$spaceregex.*/ )
66     {
67         print;
68     }
69     else
70     {
71         ($prefix,$left,$space,$right) = /^(|\S\S*:)((?:\d+\.){3}\d+\/\d\d?)($spaceregex)(.*)$/;
72         
73         my @new_lefts = expand_network($left);
74         foreach my $nl (@new_lefts)
75         {
76             print "$prefix$nl$space$right\n";
77         }
78     }
79 }
80
81 sub expand_network
82 {
83     my $left_input = shift;
84     my @rc = ($left_input);
85     my ($network,$mask) = split /\//, $left_input;
86     if (defined $mask)
87     {
88         return (0..255) if $mask == 0;
89
90         my @parts = split /\./, $network;
91         while ($#parts < 3)
92         {
93             push @parts, "0";
94         }
95         my $clean_input = join '.', @parts;
96         $clean_input .= "/$mask";
97         my @octets = Net::CIDR::cidr2octets($clean_input);
98         @rc = @octets;
99     }
100     return @rc;
101 }