Validate based on whether $ENV{REMOTE_ADDR} is present in a list of IP's or in the IP ranges listed in CIDR notation. Real-world application was within a subfunction, used to restrict signup for an email account.
#!/usr/bin/perl
use CGI;
use Net::CIDR;
open ACL, "< /usr/local/atmail/ACL.txt";
my (@ACL, @CIDRs, $entry, $entry1, $offset, $offset1, $ip, $found);
# Create an array of the entries from the list file
while(<ACL>){
push(@ACL, $_);
}
# Push the CIDR's into an array and undef them (avoiding splice() prob
+lems with the foreach and offset) in @ACL so they can be easily remov
+ed later...
foreach $entry (@ACL){
if ($entry =~ /(\d{1,3}\.){3}\d+\//){
push(@CIDRs, $entry);
undef $ACL[$offset];
}
$offset++;
}
# Anything left in the list that's not an IP gets whacked
foreach $entry1 (@ACL){
if (!(Net::CIDR::cidrvalidate($entry1))){
undef $ACL[$offset1];
}
$offset1++;
}
# Iterate ACL, finishing up if the IP in the HTTP request can be found
+ in any of the CIDRs or if it matches the current IP in the list.
foreach $ip (@ACL){
chomp $ip;
if ($ENV{REMOTE_ADDR} eq "$ip" || Net::CIDR::cidrlookup($ENV{REMOT
+E_ADDR}, @CIDRs)){
$found = 1;
last;
}
}
# If your IP is allowed, do nothing. If not, display error message.
if ($found){
}
else{
print "Content-Type: text/html\n\n";
print "ERROR YOU CANT SIGNUP";
exit;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.