2010-09-17 12:23:49 +02:00
|
|
|
##@file
|
|
|
|
# Access control library for lemonldap::ng portal
|
|
|
|
|
|
|
|
##@class
|
|
|
|
# Access control library for lemonldap::ng portal
|
|
|
|
package Lemonldap::NG::Portal::_LibAccess;
|
|
|
|
|
|
|
|
use strict;
|
2011-02-07 11:27:36 +01:00
|
|
|
|
2011-09-15 11:07:46 +02:00
|
|
|
our $VERSION = '1.1.2';
|
2010-09-17 12:23:49 +02:00
|
|
|
|
|
|
|
# Global variables
|
|
|
|
our ( $defaultCondition, $locationCondition, $locationRegexp, $cfgNum ) =
|
|
|
|
( undef, undef, undef, 0 );
|
|
|
|
|
2010-10-30 21:25:38 +02:00
|
|
|
BEGIN {
|
|
|
|
eval {
|
|
|
|
require threads::shared;
|
|
|
|
threads::shared::share($defaultCondition);
|
|
|
|
threads::shared::share($locationCondition);
|
|
|
|
threads::shared::share($locationRegexp);
|
|
|
|
threads::shared::share($cfgNum);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-09-17 12:23:49 +02:00
|
|
|
## @method private boolean _grant(string uri)
|
|
|
|
# Check user's authorization for $uri.
|
|
|
|
# @param $uri URL string
|
|
|
|
# @return True if granted
|
|
|
|
sub _grant {
|
|
|
|
my ( $self, $uri ) = splice @_;
|
2011-09-15 11:07:46 +02:00
|
|
|
$self->lmLog( "Evaluate access right on $uri", 'debug' );
|
2010-09-17 12:23:49 +02:00
|
|
|
$uri =~ m{(\w+)://([^/:]+)(:\d+)?(/.*)?$} or return 0;
|
|
|
|
my ( $protocol, $vhost, $port, $path );
|
|
|
|
( $protocol, $vhost, $port, $path ) = ( $1, $2, $3, $4 );
|
|
|
|
$path ||= '/';
|
2011-09-15 11:07:46 +02:00
|
|
|
$self->lmLog( "Evaluation for vhost $vhost and path $path", 'debug' );
|
2010-09-17 12:23:49 +02:00
|
|
|
$self->_compileRules()
|
|
|
|
if ( $cfgNum != $self->{cfgNum} );
|
|
|
|
|
|
|
|
if ( defined $locationRegexp->{$vhost} ) { # Not just a default rule
|
2011-09-15 11:07:46 +02:00
|
|
|
$self->lmLog( "Applying access rule from $vhost", 'debug' );
|
2010-09-17 12:23:49 +02:00
|
|
|
for ( my $i = 0 ; $i < @{ $locationRegexp->{$vhost} } ; $i++ ) {
|
|
|
|
if ( $path =~ $locationRegexp->{$vhost}->[$i] ) {
|
2011-09-15 11:07:46 +02:00
|
|
|
$self->lmLog(
|
|
|
|
"Applying access rule "
|
|
|
|
. $locationCondition->{$vhost}->[$i]
|
|
|
|
. " for path $path",
|
|
|
|
'debug'
|
|
|
|
);
|
2010-09-17 12:23:49 +02:00
|
|
|
return &{ $locationCondition->{$vhost}->[$i] }($self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-15 11:07:46 +02:00
|
|
|
else {
|
|
|
|
$self->lmLog( "Applying default access rule from $vhost", 'debug' );
|
|
|
|
}
|
2010-09-17 12:23:49 +02:00
|
|
|
unless ( $defaultCondition->{$vhost} ) {
|
|
|
|
$self->lmLog(
|
|
|
|
"Application $uri did not match any configured virtual host",
|
|
|
|
'warn' );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return &{ $defaultCondition->{$vhost} }($self);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
## @method private boolean _compileRules()
|
|
|
|
# Parse configured rules and compile them
|
|
|
|
# @return True
|
|
|
|
sub _compileRules {
|
|
|
|
my $self = shift;
|
|
|
|
foreach my $vhost ( keys %{ $self->{locationRules} } ) {
|
|
|
|
my $i = 0;
|
2011-09-15 11:07:46 +02:00
|
|
|
$self->lmLog( "Compiling rules for $vhost", 'debug' );
|
2010-09-17 12:23:49 +02:00
|
|
|
foreach ( keys %{ $self->{locationRules}->{$vhost} } ) {
|
|
|
|
if ( $_ eq 'default' ) {
|
|
|
|
$defaultCondition->{$vhost} =
|
|
|
|
$self->_conditionSub(
|
|
|
|
$self->{locationRules}->{$vhost}->{$_} );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$locationCondition->{$vhost}->[$i] =
|
|
|
|
$self->_conditionSub(
|
|
|
|
$self->{locationRules}->{$vhost}->{$_} );
|
|
|
|
$locationRegexp->{$vhost}->[$i] = qr/$_/;
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Default policy
|
|
|
|
$defaultCondition->{$vhost} ||= $self->_conditionSub('accept');
|
|
|
|
}
|
|
|
|
$cfgNum = $self->{cfgNum};
|
|
|
|
1;
|
|
|
|
}
|
|
|
|
|
|
|
|
## @method private CODE _conditionSub(string cond)
|
|
|
|
# Return subroutine giving authorization condition.
|
|
|
|
# @param $cond boolean expression
|
|
|
|
# @return Compiled routine
|
|
|
|
sub _conditionSub {
|
|
|
|
my ( $self, $cond ) = splice @_;
|
|
|
|
return sub { 1 }
|
2012-02-02 13:00:30 +01:00
|
|
|
if ( $cond =~ /^(?:accept|unprotect|skip)$/i );
|
2010-09-17 12:23:49 +02:00
|
|
|
return sub { 0 }
|
|
|
|
if ( $cond =~ /^(?:deny$|logout)/i );
|
|
|
|
my $sub = "sub {my \$self = shift; return ( $cond )}";
|
2011-02-07 11:27:36 +01:00
|
|
|
return $self->safe->reval($sub);
|
2010-09-17 12:23:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|