Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: ModPerl: ModPerl

setting a server variable

 

 

ModPerl modperl RSS feed   Index | Next | Previous | View Threaded


jckdnk111 at yahoo

Jun 13, 2008, 7:56 PM

Post #1 of 5 (1996 views)
Permalink
setting a server variable

Hi,

I'm new to mod_perl and I'm having some difficulty understanding a few things.
I'd like to write an Apache module which authenticates a request based on the URL.
I only want the module to deny invalid requests and allow valid requests to be processed as normal.

A more specific example would be like:

Request URL: http://myhost.com/REALLY-SECURE-TOKEN/file2download
Module logic: if REALLY-SECURE-TOKEN is valid, allow the request to continue - else, stop request with an error

External application logic: if request got here without error then
find the file2download and write it to the output stream - else, show
custom error


I think the best way to do this is something like:

1) Write a module which evaluates the URL and places a variable in the request's scope
2)
Use mod_rewrite to evaluate the newly set variable and pass execution
to the proper place with any error code that might have been placed in
the variable

I've been reading books, howto's, and on-line documentation for the past two days and I still have no idea where to begin.
Any advice would be greatly appreciated.

Thanks,

Ty


aw at ice-sa

Jun 14, 2008, 12:57 AM

Post #2 of 5 (1674 views)
Permalink
Re: setting a server variable [In reply to]

tyju tiui wrote:
> Hi,
>
> I'm new to mod_perl and I'm having some difficulty understanding a few things.
> I'd like to write an Apache module which authenticates a request based on the URL.
> I only want the module to deny invalid requests and allow valid requests to be processed as normal.
>
> A more specific example would be like:
>
> Request URL: http://myhost.com/REALLY-SECURE-TOKEN/file2download
> Module logic: if REALLY-SECURE-TOKEN is valid, allow the request to continue - else, stop request with an error
>
> External application logic: if request got here without error then
> find the file2download and write it to the output stream - else, show
> custom error
>
>
> I think the best way to do this is something like:
>
> 1) Write a module which evaluates the URL and places a variable in the request's scope
> 2)
> Use mod_rewrite to evaluate the newly set variable and pass execution
> to the proper place with any error code that might have been placed in
> the variable
>
With mod_perl, it might not be so complicated.
What you probably want is a PerlAccessHandler module.
This will check if the request URL is ok (valid token).
If it is, it returns Apache2::Const::OK, and Apache will continue
processing the request (e.g., sending the file).
If the token is not ok, it returns Apache2::Const::FORBIDDEN, and Apache
will (automatically) return an error page telling the user he is not
allowed to do that.

Look there for an explanation and an example :
http://perl.apache.org/docs/2.0/user/handlers/http.html#PerlAccessHandler

In your case, forget the Apache2::Connection and the IP-linked stuff,
and replace it with your code to check the URL.
In the Apache configuration, you would have something like this :

<Location />
.. general rules for allowing things like html pages, gifs etc..
</Location>
<Location /downloads>
# where your files are
SetHandler mod_perl
PerlAccessHandler MyModule
...
</Location>


And that's basically it.
Now, if this is your first mod_perl Apache add-on module, you'll have to
figure out some more stuff, but it's fun.

André


frank at wiles

Jun 16, 2008, 10:32 AM

Post #3 of 5 (1655 views)
Permalink
Re: setting a server variable [In reply to]

On Fri, 13 Jun 2008 19:56:14 -0700 (PDT)
tyju tiui <jckdnk111 [at] yahoo> wrote:

>
> Hi,
>
> I'm new to mod_perl and I'm having some difficulty understanding a
> few things. I'd like to write an Apache module which authenticates a
> request based on the URL. I only want the module to deny invalid
> requests and allow valid requests to be processed as normal.
>
> A more specific example would be like:
>
> Request URL: http://myhost.com/REALLY-SECURE-TOKEN/file2download
> Module logic: if REALLY-SECURE-TOKEN is valid, allow the request
> to continue - else, stop request with an error
> External application logic: if request got here without error then
> find the file2download and write it to the output stream - else, show
> custom error
>
>
> I think the best way to do this is something like:
>
> 1) Write a module which evaluates the URL and places a variable in
> the request's scope
> 2)
> Use mod_rewrite to evaluate the newly set variable and pass execution
> to the proper place with any error code that might have been placed in
> the variable
>
> I've been reading books, howto's, and on-line documentation for the
> past two days and I still have no idea where to begin. Any advice
> would be greatly appreciated.

My advice would be to change your URLs to be:

http://myhost.com/securefiles/REALLY-SECURE-TOKEN/filename

Then write a handler that does something along these lines:

use Apache2::RequestRec;
use Apache2::RequestUtil;
use Apache2::RequestIO;

sub handler {
my $r = shift;

# Get the parts of the URI we are interested in
my $uri = $r->uri;
my $root = $r->location;

$uri =~ s!^$root!!; # Strip off http://myhose.com/securefiles
$uri =~ s!//!/!og; # Remove any double slashes
$uri =~ s!^/!!o; # Remove the first slash

# Now that we're left with just REALLY-SECURE-KEY/filename,
# split it up
my ( $secure_key, $filename ) = split( '/', $uri );

# Verify the secure key
if( verify( $secure_key ) ) {
$r->sendfile( $filename );
return( Apache2::Const::OK );
}
else {
return( Apache2::Const::FORBIDDEN );
}

}

}

It would be configured as:

<Location /securefiles>
SetHandler modperl
PerlResponseHandler YourHandlerNameHere
</Location>

You could also do this as an AuthHandler as was previously
mentioned, but for something this simple I don't see much
point in breaking it up unless you're going to use these
secure keys for many different things.

-------------------------------------------------------
Frank Wiles, Revolution Systems, LLC.
Personal : frank [at] wiles http://www.wiles.org
Work : frank [at] revsys http://www.revsys.com


jckdnk111 at yahoo

Jun 16, 2008, 10:45 AM

Post #4 of 5 (1659 views)
Permalink
Re: setting a server variable [In reply to]

André,

Thanks so much for your kind advice.
I tried the example you suggested and I think this is exactly what I need.

I have one problem now though.
The file download script I am currently using is written in php and it is quite elaborate (in other words I'd like to keep if I can).
My PerlAccessHandler does it's job nicely, but now the php script isn't found.

My config looks like:

<Location /downloads>
SetHandler perl-script
PerlAccessHandler MyApache2::DLAuth
</Location>

My index.php file is also in /downloads and it handles everything after the auth is done.
I'm thinking the 'SetHandler' directive has something to do with my php script no longer working but I'm unsure how to work around the problem.
Any ideas? Perhaps this a question for the apache http forums?

Thanks again for all your help,

Ty



----- Original Message ----
From: André Warnier <aw [at] ice-sa>
To: modperl [at] perl
Cc: tyju tiui <jckdnk111 [at] yahoo>
Sent: Saturday, June 14, 2008 3:57:31 AM
Subject: Re: setting a server variable



tyju tiui wrote:
> Hi,
>
> I'm new to mod_perl and I'm having some difficulty understanding a few things.
> I'd like to write an Apache module which authenticates a request based on the URL.
> I only want the module to deny invalid requests and allow valid requests to be processed as normal.
>
> A more specific example would be like:
>
> Request URL: http://myhost.com/REALLY-SECURE-TOKEN/file2download
> Module logic: if REALLY-SECURE-TOKEN is valid, allow the request to continue - else, stop request with an error
>
> External application logic: if request got here without error then
> find the file2download and write it to the output stream - else, show
> custom error
>
>
> I think the best way to do this is something like:
>
> 1) Write a module which evaluates the URL and places a variable in the request's scope
> 2)
> Use mod_rewrite to evaluate the newly set variable and pass execution
> to the proper place with any error code that might have been placed in
> the variable
>
With mod_perl, it might not be so complicated.
What you probably want is a PerlAccessHandler module.
This will check if the request URL is ok (valid token).
If it is, it returns Apache2::Const::OK, and Apache will continue
processing the request (e.g., sending the file).
If the token is not ok, it returns Apache2::Const::FORBIDDEN, and Apache
will (automatically) return an error page telling the user he is not
allowed to do that.

Look there for an explanation and an example :
http://perl.apache.org/docs/2.0/user/handlers/http.html#PerlAccessHandler

In your case, forget the Apache2::Connection and the IP-linked stuff,
and replace it with your code to check the URL.
In the Apache configuration, you would have something like this :

<Location />
.. general rules for allowing things like html pages, gifs etc..
</Location>
<Location /downloads>
# where your files are
SetHandler mod_perl
PerlAccessHandler MyModule
...
</Location>


And that's basically it.
Now, if this is your first mod_perl Apache add-on module, you'll have to
figure out some more stuff, but it's fun.

André


jckdnk111 at yahoo

Jun 16, 2008, 11:10 AM

Post #5 of 5 (1649 views)
Permalink
Re: setting a server variable [In reply to]

I found a solution. Not sure if it is the most efficient solution or not, but it works.

<Location /downloads>
SetHandler perl-script
PerlAccessHandler MyApache2:DLAuth
RewriteEngine On
RewriteCond %{ENV:DL_OK} ^VALID$
RewriteRule ^(.*)$ /ext/download/index.php [L]
RewriteCond %{ENV:DL_OK} !^VALID$
RewriteRule ^(.*)$ /ext/download/index.php?err=%{ENV:DL_OK} [L]
</Location>

Thanks again for everyone's help!

----- Original Message ----
From: tyju tiui <jckdnk111 [at] yahoo>
To: André Warnier <aw [at] ice-sa>; modperl [at] perl
Sent: Monday, June 16, 2008 1:45:30 PM
Subject: Re: setting a server variable

André,

Thanks so much for your kind advice.
I tried the example you suggested and I think this is exactly what I need.

I have one problem now though.
The file download script I am currently using is written in php and it is quite elaborate (in other words I'd like to keep if I can).
My PerlAccessHandler does it's job nicely, but now the php script isn't found.

My config looks like:

<Location /downloads>
SetHandler perl-script
PerlAccessHandler MyApache2::DLAuth
</Location>

My index.php file is also in /downloads and it handles everything after the auth is done.
I'm thinking the 'SetHandler' directive has something to do with my php script no longer working but I'm unsure how to work around the problem.
Any ideas? Perhaps this a question for the apache http forums?

Thanks again for all your help,

Ty



----- Original Message ----
From: André Warnier <aw [at] ice-sa>
To: modperl [at] perl
Cc: tyju tiui <jckdnk111 [at] yahoo>
Sent: Saturday, June 14, 2008 3:57:31 AM
Subject: Re: setting a server variable



tyju tiui wrote:
> Hi,
>
> I'm new to mod_perl and I'm having some difficulty understanding a few things.
> I'd like to write an Apache module which authenticates a request based on the URL.
> I only want the module to deny invalid requests and allow valid requests to be processed as normal.
>
> A more specific example would be like:
>
> Request URL: http://myhost.com/REALLY-SECURE-TOKEN/file2download
> Module logic: if REALLY-SECURE-TOKEN is valid, allow the request to continue - else, stop request with an error
>
> External application logic: if request got here without error then
> find the file2download and write it to the output stream - else, show
> custom error
>
>
> I think the best way to do this is something like:
>
> 1) Write a module which evaluates the URL and places a variable in the request's scope
> 2)
> Use mod_rewrite to evaluate the newly set variable and pass execution
> to the proper place with any error code that might have been placed in
> the variable
>
With mod_perl, it might not be so complicated.
What you probably want is a PerlAccessHandler module.
This will check if the request URL is ok (valid token).
If it is, it returns Apache2::Const::OK, and Apache will continue
processing the request (e.g., sending the file).
If the token is not ok, it returns Apache2::Const::FORBIDDEN, and Apache
will (automatically) return an error page telling the user he is not
allowed to do that.

Look there for an explanation and an example :
http://perl.apache.org/docs/2.0/user/handlers/http.html#PerlAccessHandler

In your case, forget the Apache2::Connection and the IP-linked stuff,
and replace it with your code to check the URL.
In the Apache configuration, you would have something like this :

<Location />
.. general rules for allowing things like html pages, gifs etc..
</Location>
<Location /downloads>
# where your files are
SetHandler mod_perl
PerlAccessHandler MyModule
...
</Location>


And that's basically it.
Now, if this is your first mod_perl Apache add-on module, you'll have to
figure out some more stuff, but it's fun.

André

ModPerl modperl RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.