You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by jo...@porsche.co.at on 2002/02/07 10:46:24 UTC

scripts running under Apache::Registry and tie()/untie()

Hi,

I'm very new to modperl and I've got a question regarding the usage of tie
() and untie().
I've written a perl-script for authenticating a user's IP-address
against a DBM-database which has stored a certain valid IP-range
for each user.

I put it into a directory where every .perl-file runs under
Apache::Registry.
Now I do a tie() once in the .perl-script. Do I have to untie() in the same
script
or is this not necessary because as I understood the perl-script keeps
"living" as long as the child-process who started it?!

Did I get something wrong here?!

best regards,
Johannes Grumböck

PS: Here's the source code - I know it's a little bit weak - but I'm still
beginner :)

======================
#!/usr/local/bin/perl -W
# file: test.perl

use strict;
use Apache::Constants qw(:response);

my $r = Apache->request;

my %params = $r->args;
my $username = $params{'hdl'} || undef;
if (!$username) {
  $r->status(BAD_REQUEST);
  return;
}

$username = 'hdl' . $username;

my @FirstIP = ();
my @LastIP =();
my @IP = split('\.',$r->connection->remote_ip());

tie(my %DB, "NDBM_File","/www/apache/auth/crossrs.user",'0_RDONLY',0664) ||
die "Kann DB nicht öffnen.";

my @rest = split(/:/,$DB{$username});
foreach (@rest) {
  my ($n,$v) = split('=');
  @FirstIP = split('\.',$v) if ($n eq "FirstIP");
  @LastIP = split('\.',$v) if ($n eq "LastIP");
}

untie %DB;

my $inrange = "true";
if ($FirstIP[0] > $IP[0] || $IP[0] > $LastIP[0]){ $inrange = "false1";}
elsif ($FirstIP[1] > $IP[1] || $IP[1] > $LastIP[1]){ $inrange = "false2";}
elsif ($FirstIP[2] > $IP[2] || $IP[2] > $LastIP[2]){ $inrange = "false3";}
elsif ($FirstIP[3] > $IP[3] || $IP[3] > $LastIP[3]){ $inrange = "false4";}

# Interne Adresse auch gelten lassen
if ($IP[0] == 10 && $IP[1] == 222) { $inrange = "true";}

if ($inrange eq "true" && @rest) {
  $r->status(OK);
  return;
} else {
  $r->status(FORBIDDEN);
  return;
}
================


Re: scripts running under Apache::Registry and tie()/untie()

Posted by Stas Bekman <st...@stason.org>.
johannes.grumboeck@porsche.co.at wrote:
> Hi,
> 
> I'm very new to modperl and I've got a question regarding the usage of tie
> () and untie().
> I've written a perl-script for authenticating a user's IP-address
> against a DBM-database which has stored a certain valid IP-range
> for each user.
> 
> I put it into a directory where every .perl-file runs under
> Apache::Registry.
> Now I do a tie() once in the .perl-script. Do I have to untie() in the same
> script
> or is this not necessary because as I understood the perl-script keeps
> "living" as long as the child-process who started it?!

If the dbm file access is read-only it's the best to tie it during 
startup and then simply access it. If you need to read/write, read:
http://perl.apache.org/guide/dbm.html

e.g. startup.pl:

tie %Foo::Bar::dbm, "NDBM_File", ...

and then in your code:

print $Foo::Bar::dbm{taz};

beware that if you use each(), keys(), values() on this dbm it won't be 
shared anymore between the processes (though still work properly, but 
more memory will be used). For more info read:
http://perl.apache.org/guide/performance.html#Sharing_Memory


_____________________________________________________________________
Stas Bekman             JAm_pH      --   Just Another mod_perl Hacker
http://stason.org/      mod_perl Guide   http://perl.apache.org/guide
mailto:stas@stason.org  http://ticketmaster.com http://apacheweek.com
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/