Apache/iNcom/UserDBAuthz.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 133 CC 19 SCAR 55.9
CC
19
Cov
0%
handler
380.0
BEGIN
2.0
BEGIN
2.0
CRAP 268.0
1 #
2 # UserDBAuthz.pm - Apache authorization handler using the UserDB.
3 #
4 # This file is part of Apache::iNcom.
5 #
6 # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM>
7 #
8 # Copyright (C) 1999 Francis J. Lacoste, iNsu Innovations
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #
24 package Apache::iNcom::UserDBAuthz;
25  
26 use strict;
27  
28 require 5.005; # For the semantics of Text::ParseWords
29  
30 use Apache;
31 use Apache::Constants qw( :common :response );
32  
33 use Apache::iNcom;
34  
35 use DBIx::SearchProfiles;
36 use DBIx::UserDB;
37  
38 use Text::ParseWords ();
39  
40 sub handler {
41 my $r = shift;
42  
43 my $requires = $r->requires;
44 return DECLINED unless $requires;
45  
46 # Load the UserDB and the current user
47 my $sql_profile = $r->dir_config( "INCOM_SEARCH_PROFILE" )
48 || "conf/search_profiles.pl";
49 $sql_profile = $r->server_root_relative( $sql_profile );
50  
51 my $db = new DBIx::SearchProfiles( $r->pnotes( "INCOM_DBH" ),
52 $sql_profile );
53  
54 # Setup the UserDB object
55 my $userdb = new DBIx::UserDB( $db,
56 $r->dir_config( "INCOM_USERDB_PROFILE" ),
57 $r->dir_config( "INCOM_GROUPDB_PROFILE" )
58 );
59  
60 my $user;
61 if ( $r->connection->user ) {
62 $user = $userdb->user_search( { username => $r->connection->user }
63 )->[0];
64 unless ( $user ) {
65 $r->log_reason( "No such user " . $r->connection->user,
66 $r->filename );
67 return Apache::iNcom::return_error( $r, AUTH_REQUIRED );
68 }
69 } else {
70 my $session = $r->pnotes( "INCOM_SESSION" );
71 if ( exists $session->{_incom_logged_in} ) {
72 $user = $userdb->user_get( $session->{_incom_logged_in} );
73 } else {
74 $r->log_reason( "User is not logged in", $r->filename );
75 return Apache::iNcom::return_error( $r, AUTH_REQUIRED );
76 }
77 }
78  
79 # Requires are a disjonction, which means that ONLY one is needed
80 # to give access. Each conditions in a require is a conjonction.
81 REQUIRES:
82 foreach my $req ( @$requires ) {
83 # Split the requirements in its part
84 my @expr = Text::ParseWords::quotewords( '\s*(and|;|,)\s*', 0,
85 $req->{requirement} );
86 next REQUIRES unless @expr;
87  
88 # Each expr is than parse as a series of words
89 @expr = map { [ Text::ParseWords::quotewords('\s+',0, $_ ) ] } @expr;
90  
91 foreach my $e ( @expr ) {
92 unless ( @$e ) {
93 $r->warn( "Invalid requirements expression in " .
94 $req->{requirement} );
95 next REQUIRES;
96 }
97  
98 # valid-user
99 next if ( $e->[0] eq "valid-user" );
100  
101 # user <username>
102 if ( $e->[0] eq "user" ) {
103 if ( @$e != 2 ) {
104 $r->warn( "user expression takes only one args in " .
105 $req->{requirement}
106 );
107 next REQUIRES;
108 }
109 next if $e->[1] eq $user->{username};
110 # Username doesn't match
111 next REQUIRES;
112 }
113  
114  
115 # group <groupname>
116 if ( $e->[0] eq "group" ) {
117 if ( @$e != 2 ) {
118 $r->warn( "group expression takes only one args in " .
119 $req->{requirement}
120 );
121 next REQUIRES;
122 }
123 next if grep {
124 $e->[1] eq $_->{groupname}
125 } @{ $user->{groups} };
126 # Group doesn't match
127 next REQUIRES;
128 }
129  
130 # perm on URL
131 if ( @$e == 1 ) {
132 my $target = $r->uri;
133 my $prefix = $r->dir_config( "INCOM_URL_PREFIX" );
134 $target =~ s/^$prefix//;
135  
136 next if $userdb->allowed( $user, $target, $e->[0] );
137  
138 next REQUIRES;
139 } else {
140 if ( @$e > 3 ) {
141 $r->warn( "syntax of ACL is perm or perm on target in " .
142 $req->{requirement}
143 );
144 next REQUIRES;
145 }
146 # Allow noise word between perm and target
147 # Ex: perm on target
148 my $target = @$e == 3 ? $e->[2] : $e->[1];
149  
150 next if $userdb->allowed( $user, $target, $e->[0] );
151  
152 next REQUIRES;
153 }
154 }
155  
156 # All were match success
157 return OK;
158 }
159  
160 $r->log_reason( "Not authorized", $r->filename );
161 return Apache::iNcom::return_error( $r, FORBIDDEN );
162 }
163  
164 1;
165  
166 __END__