For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > January 2006 > IP Filtering with Apache and Perl









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author IP Filtering with Apache and Perl
eyji67@gmail.com

2006-01-31, 6:55 pm

I would appreciate some help with an IP Filter question. I have
configured an apache web server with the following Handler for all HTML
files for a site:

Action checkIP /cgi-bin/ipFilter.pl
AddHandler checkIP .html

The Handler redirects the call to a Perl script that checks if the
users IP address matches the list of allowed users stored in a file.
The script runs well, except for if the user is allowed to view the
pages. If I use a redirect using Location, then we get an indefinite
loop, so what I'd need is to just send the requested page to the
Browser. Is there a simple way to do this without writing a loop?? I
would just want the program to exit() and the server to continue
sending the content to the client as if nothing had happened. Here's
the Perl script (still under construction, so be gentle):

#!/usr/bin/perl -w

# Used to debug - output directed to Browser
use strict;
use diagnostics;

# If script fails, output to Browser
use CGI::Carp qw/fatalsToBrowser/;

# Direct output to Browser
print "Content-type: text/html", "\n\n";

# Read file containing authorized IP-adresses into array
open(IPFILE, "ip_addresses.conf")
or die("Unable to open ip_addresses.conf");
my @allow = <IPFILE>;
close(IPFILE);

# Get the Users IP Address
my $userAddr=$ENV{'REMOTE_ADDR'};

# Set Default to Deny
my $TestVar="0";

# This loop checks the Users IP against the Given List #
foreach my $i (@allow){
# If the IP is found in the list, change switch
chop($i);
if($i eq $userAddr){
$TestVar="1";
}
}

# If the IP address is accepted, redirect the request
## THIS IS WHAT CAUSES THE INDEFINITE LOOP ##
if($TestVar == 1){
print "Location: [url]https://[/ url]$ENV{'HTTP_HOST'}$ENV{'REQUEST_URI'}
\n\n";
exit();
}
if($TestVar == 0){
# DOH! Denial page
print "Status: 403\n\n";
}


Thanks!

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com