PageRenderTime 565ms CodeModel.GetById 35ms RepoModel.GetById 5ms app.codeStats 0ms

/sendmail-8.14.5/contrib/cidrexpand

#
Perl | 138 lines | 80 code | 7 blank | 51 comment | 7 complexity | 468fb319bea2596cf1c825b5f186e110 MD5 | raw file
Possible License(s): AGPL-1.0
  1. #!/usr/bin/perl -w
  2. # $Id: cidrexpand,v 8.8 2006/08/07 17:18:37 ca Exp $
  3. #
  4. # v 0.4
  5. #
  6. # 17 July 2000 Derek J. Balling (dredd@megacity.org)
  7. #
  8. # Acts as a preparser on /etc/mail/access_db to allow you to use address/bit
  9. # notation.
  10. #
  11. # If you have two overlapping CIDR blocks with conflicting actions
  12. # e.g. 10.2.3.128/25 REJECT and 10.2.3.143 ACCEPT
  13. # make sure that the exceptions to the more general block are specified
  14. # later in the access_db.
  15. #
  16. # the -r flag to makemap will make it "do the right thing"
  17. #
  18. # Modifications
  19. # -------------
  20. # 26 Jul 2001 Derek Balling (dredd@megacity.org)
  21. # Now uses Net::CIDR because it makes life a lot easier.
  22. #
  23. # 5 Nov 2002 Richard Rognlie (richard@sendmail.com)
  24. # Added code to deal with the prefix tags that may now be included in
  25. # the access_db
  26. #
  27. # Added clarification in the notes for what to do if you have
  28. # exceptions to a larger CIDR block.
  29. #
  30. # 26 Jul 2006 Richard Rognlie (richard@sendmail.com>
  31. # Added code to strip "comments" (anything after a non-escaped #)
  32. # # characters after a \ or within quotes (single and double) are
  33. # left intact.
  34. #
  35. # e.g.
  36. # From:1.2.3.4 550 Die spammer # spammed us 2006.07.26
  37. # becomes
  38. # From:1.2.3.4 550 Die spammer
  39. #
  40. # 3 August 2006
  41. #
  42. # Corrected a bug to have it handle the special case of "0.0.0.0/0"
  43. # since Net::CIDR doesn't handle it properly.
  44. #
  45. # usage:
  46. # cidrexpand < /etc/mail/access | makemap -r hash /etc/mail/access
  47. #
  48. #
  49. # Report bugs to: <dredd@megacity.org>
  50. #
  51. use strict;
  52. use Net::CIDR;
  53. use Getopt::Std;
  54. our ($opt_c,$opt_t);
  55. getopts('ct:');
  56. my $spaceregex = '\s+';
  57. if ($opt_t)
  58. {
  59. $spaceregex = $opt_t;
  60. }
  61. while (<>)
  62. {
  63. chomp;
  64. my ($prefix,$left,$right,$space);
  65. if ( (/\#/) && $opt_c )
  66. {
  67. # print "checking...\n";
  68. my $i;
  69. my $qtype='';
  70. for ($i=0 ; $i<length($_) ; $i++)
  71. {
  72. my $ch = substr($_,$i,1);
  73. if ($ch eq '\\')
  74. {
  75. $i++;
  76. next;
  77. }
  78. elsif ($qtype eq '' && $ch eq '#')
  79. {
  80. substr($_,$i) = '';
  81. last;
  82. }
  83. elsif ($qtype ne '' && $ch eq $qtype)
  84. {
  85. $qtype = '';
  86. }
  87. elsif ($qtype eq '' && $ch =~ /[\'\"]/)
  88. {
  89. $qtype = $ch;
  90. }
  91. }
  92. }
  93. if (! /^(|\S\S*:)(\d+\.){3}\d+\/\d\d?$spaceregex.*/ )
  94. {
  95. print "$_\n";
  96. }
  97. else
  98. {
  99. ($prefix,$left,$space,$right) =
  100. /^(|\S\S*:)((?:\d+\.){3}\d+\/\d\d?)($spaceregex)(.*)$/;
  101. my @new_lefts = expand_network($left);
  102. foreach my $nl (@new_lefts)
  103. {
  104. print "$prefix$nl$space$right\n";
  105. }
  106. }
  107. }
  108. sub expand_network
  109. {
  110. my $left_input = shift;
  111. my @rc = ($left_input);
  112. my ($network,$mask) = split /\//, $left_input;
  113. if (defined $mask)
  114. {
  115. return (0..255) if $mask == 0;
  116. my @parts = split /\./, $network;
  117. while ($#parts < 3)
  118. {
  119. push @parts, "0";
  120. }
  121. my $clean_input = join '.', @parts;
  122. $clean_input .= "/$mask";
  123. my @octets = Net::CIDR::cidr2octets($clean_input);
  124. @rc = @octets;
  125. }
  126. return @rc;
  127. }