| 1 | # | ||||||||||||||
| 2 | # $HeadURL: https://svn.oucs.ox.ac.uk/people/oliver/pub/librpc-serialized-perl/trunk/lib/RPC/Serialized/AuthzHandler/ACL.pm $ | ||||||||||||||
| 3 | # $LastChangedRevision: 1281 $ | ||||||||||||||
| 4 | # $LastChangedDate: 2008-10-01 16:16:56 +0100 (Wed, 01 Oct 2008) $ | ||||||||||||||
| 5 | # $LastChangedBy: oliver $ | ||||||||||||||
| 6 | # | ||||||||||||||
| 7 | package RPC::Serialized::AuthzHandler::ACL; | ||||||||||||||
| 8 | { | ||||||||||||||
| 9 | 1 | $RPC::Serialized::AuthzHandler::ACL::VERSION = '1.123630'; | |||||||||||||
| 10 | } | ||||||||||||||
| 11 | |||||||||||||||
| 12 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 1 | ▶ | use warnings FATAL => 'all'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 1 | ▶ | use base 'RPC::Serialized::AuthzHandler'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 1 | ▶ | use Readonly; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 1 | ▶ | use IO::File; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 1 | ▶ | use RPC::Serialized::ACL; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 1 | ▶ | use RPC::Serialized::ACL::Group; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 1 | ▶ | use RPC::Serialized::Exceptions; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | |||||||||||||||
| 23 | 1 | Readonly my $GROUP_RX => qr/^define\s+group\s+(\S+)\s+(.+)$/; | |||||||||||||
| 24 | 1 | Readonly my $ACL_RX => qr/^(allow|deny)\s+(\S+)\s+by\s+(\S+)\s+on\s+(\S+)$/; | |||||||||||||
| 25 | |||||||||||||||
| 26 | 3 | ▶ | sub _parse_acls { | ||||||||||||
|
Subroutine
_parse_acls: called CC=10 SCAR=25.1
| |||||||||||||||
| 27 | 3 | my $acl_path = shift; | |||||||||||||
| 28 | |||||||||||||||
| 29 | 3 | ▶ | my $acl_fh = IO::File->new($acl_path) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | or throw_system "Open $acl_path failed: $!"; | ||||||||||||||
| 31 | |||||||||||||||
| 32 | 2 | my ( @acls, %groups ); | |||||||||||||
| 33 | 2 | ▶ | while (<$acl_fh>) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 34 | 8 | s/#.*$//; | |||||||||||||
| 35 | 8 | s/^\s+//; | |||||||||||||
| 36 | 8 | s/\s+$//; | |||||||||||||
| 37 | 8 | ▶ | next unless length($_); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | |||||||||||||||
| 39 | 5 | ▶ | if ( my ( $action, $operation, $subject, $target ) = $_ =~ $ACL_RX ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 40 | 4 | ▶ | if ( $subject =~ s/^group:// ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | 0 | ▶ | $subject = $groups{$subject} | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | or throw_app | ||||||||||||||
| 43 | "Reference to undefined group at '$acl_path' line $."; | ||||||||||||||
| 44 | } | ||||||||||||||
| 45 | 4 | ▶ | if ( $target =~ s/^group:// ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | 0 | ▶ | $target = $groups{$target} | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | or throw_app | ||||||||||||||
| 48 | "Reference to undefined group at '$acl_path' line $."; | ||||||||||||||
| 49 | } | ||||||||||||||
| 50 | 4 | push @acls, | |||||||||||||
| 51 | RPC::Serialized::ACL->new( | ||||||||||||||
| 52 | operation => $operation, | ||||||||||||||
| 53 | subject => $subject, | ||||||||||||||
| 54 | target => $target, | ||||||||||||||
| 55 | action => $action, | ||||||||||||||
| 56 | ); | ||||||||||||||
| 57 | } | ||||||||||||||
| 58 | elsif ( my ( $name, $uri ) = $_ =~ $GROUP_RX ) { | ||||||||||||||
| 59 | 0 | $groups{$name} = RPC::Serialized::ACL::Group->new($uri); | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | else { | ||||||||||||||
| 62 | 1 | throw_app "Failed to parse ACLs at '$acl_path' line $."; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 1 | return \@acls; | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 4 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 70 | 4 | my $class = shift; | |||||||||||||
| 71 | |||||||||||||||
| 72 | 4 | ▶ | my $acl_path = shift | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | or throw_app 'ACL path not specified'; | ||||||||||||||
| 74 | |||||||||||||||
| 75 | 3 | return bless { | |||||||||||||
| 76 | ACLS => _parse_acls($acl_path), | ||||||||||||||
| 77 | }, $class; | ||||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | 8 | ▶ | sub acls { | ||||||||||||
|
Subroutine
acls: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 81 | 8 | my $self = shift; | |||||||||||||
| 82 | 8 | $self->{ACLS}; | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | 8 | ▶ | sub check_authz { | ||||||||||||
|
Subroutine
check_authz: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 86 | 8 | my $self = shift; | |||||||||||||
| 87 | 8 | my ( $subject, $operation, $target ) = @_; | |||||||||||||
| 88 | |||||||||||||||
| 89 | 8 | foreach my $acl ( @{ $self->acls } ) { | |||||||||||||
| 90 | 25 | my $rc = $acl->check( $subject, $operation, $target ); | |||||||||||||
| 91 | 25 | ▶ | next if $rc == $acl->DECLINE; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | 6 | ▶ | return $rc == $acl->ALLOW ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | } | ||||||||||||||
| 94 | |||||||||||||||
| 95 | 2 | return 0; | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | 1 | 1; | |||||||||||||
| 99 | |||||||||||||||