lib/Net/LDAP/SimpleServer/ProtocolHandler.pm

filter: Statementstmt 98.0% 154 / 157 Branchbran 75.0% 39 / 52 Conditioncond 73.3% 11 / 15 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 29 / 29 Podpod 100% 4 / 4 totaltotal 90.5% 240 / 265 SCAR 35.8
CC
35
Cov
91%
bind
15.7
new
8.1
_encode_pw_attr
5.1
CRAP 35.9
« blib/lib/Net/LDAP/SimpleServer.pm ↑ Summary lib/Net/LDAP/SimpleServer/LDIFStore.pm »
1 package Net::LDAP::SimpleServer::ProtocolHandler;
2  
3 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: LDAP protocol handler used with Net::LDAP::SimpleServer
7  
8 40 our $VERSION = '0.0.20'; # VERSION
9  
10 40 ▶use constant MD5_PREFIX => '{md5}';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 ▶use Net::LDAP::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use base 'Net::LDAP::Server';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 use fields
15 40 ▶ qw(store root_dn root_pw allow_anon user_passwords user_id_attr user_pw_attr user_filter);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 40 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 40 ▶use Net::LDAP::LDIF;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 40 ▶use Net::LDAP::Util qw{canonical_dn};
Subroutine BEGIN: called CC=1 SCAR=0.0
20 40 ▶use Net::LDAP::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use Net::LDAP::FilterMatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 40 use Net::LDAP::Constant qw/
24 LDAP_SUCCESS LDAP_INVALID_CREDENTIALS LDAP_AUTH_METHOD_NOT_SUPPORTED
25 40 ▶ LDAP_INVALID_SYNTAX LDAP_NO_SUCH_OBJECT LDAP_INVALID_DN_SYNTAX/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 40 ▶use Net::LDAP::SimpleServer::LDIFStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 40 ▶use Net::LDAP::SimpleServer::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 40 ▶use Scalar::Util qw{reftype};
Subroutine BEGIN: called CC=1 SCAR=0.0
31 40 ▶use UNIVERSAL::isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 40 ▶use Digest::MD5 qw/md5/;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 40 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 sub _make_result {
37 26 ▶ my $code = shift;
Subroutine _make_result: called CC=3 SCAR=11.0
38 26 ▶ my $dn = shift // '';
Condition100%2/2
shift() // ""
l!l
224
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
39 26 ▶ my $msg = shift // '';
Condition100%2/2
shift() // ""
l!l
422
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
40  
41 return {
42 26 matchedDN => $dn,
43 errorMessage => $msg,
44 resultCode => $code,
45 };
46 }
47  
48 sub new {
49 20 ▶ my $class = shift;
Subroutine new: called CC=8 SCAR=20.9
Pod documented
50 20 ▶ my $params = shift || croak 'Must pass parameters!';
Condition66%2/3
shift() || croak('Must pass parameters!')
l!l&&r!l&&!r
1910
MC/DC0%0/2
shift() || croak('Must pass parameters!')
shift() croak('Must pass parameters!')
Truth table33%1/3
A || B
A= shift()B= croak('Must pass parameters!')
ABresult
1X1
011
000
51  
52 19 ▶ croak 'Parameter must be a HASHREF' unless reftype($params) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($params) eq "HASH" 2 17 19
53 17 for my $p (qw/store root_dn sock/) {
54 45 ▶ croak 'Must pass option {' . $p . '}' unless exists $params->{$p};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$p} 4 41 45
55 }
56 croak 'Not a LDIFStore'
57 13 ▶ unless $params->{store}->isa('Net::LDAP::SimpleServer::LDIFStore');
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"store"}->isa("Net::LDAP::SimpleServer::LDIFStore") 0 13 13
58  
59 13 ▶ croak 'Option {root_dn} can not be empty' unless $params->{root_dn};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"root_dn"} 0 13 13
60 croak 'Invalid root DN'
61 13 ▶ unless my $canon_dn = canonical_dn( $params->{root_dn} );
Branch: 1 branch
BranchTrueFalseTotal
unless my $canon_dn = &canonical_dn($params->{"root_dn"}) 0 13 13
62  
63 13 my $self = $class->SUPER::new( $params->{sock} );
64 13 $self->{store} = $params->{store};
65 13 $self->{root_dn} = $canon_dn;
66 13 $self->{root_pw} = $params->{root_pw};
67 13 $self->{allow_anon} = $params->{allow_anon};
68 13 $self->{user_passwords} = $params->{user_passwords};
69 13 $self->{user_id_attr} = $params->{user_id_attr};
70 13 $self->{user_pw_attr} = $params->{user_pw_attr};
71 13 $self->{user_filter} = $params->{user_filter};
72 13 chomp( $self->{root_pw} );
73 13 chomp( $self->{user_passwords} );
74  
75 13 return $self;
76 }
77  
78 sub unbind {
79 10 ▶ my $self = shift;
Subroutine unbind: called CC=1 SCAR=0.0
Pod documented
80  
81 10 $self->{store} = undef;
82 10 $self->{root_dn} = undef;
83 10 $self->{root_pw} = undef;
84  
85 10 return _make_result(LDAP_SUCCESS);
86 }
87  
88 sub _find_user_dn {
89 3 ▶ my ( $self, $username ) = @_;
Subroutine _find_user_dn: called CC=1 SCAR=0.0
90  
91 my $filter =
92 Net::LDAP::Filter->new( '(&'
93 . $self->{user_filter} . '('
94 3 . $self->{user_id_attr} . '='
95 . $username
96 . '))' );
97 3 return _match( $filter, $self->{store}->list() );
98 }
99  
100 sub _encode_password {
101 1 ▶ my $plain = shift;
Subroutine _encode_password: called CC=1 SCAR=0.0
102  
103 1 my $hashpw = encode_base64( md5($plain), '' );
104 1 return MD5_PREFIX . $hashpw;
105 }
106  
107 sub bind {
108 ## no critic (ProhibitBuiltinHomonyms)
109 12 ▶ my ( $self, $request ) = @_;
Subroutine bind: called CC=15 SCAR=27.5
Pod documented
110  
111 # anonymous bind
112 return _make_result(LDAP_SUCCESS)
113 if ( $self->{allow_anon}
114 and not $request->{name}
115 12 ▶ and exists $request->{authentication}->{simple} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"allow_anon"} and not $request->{"name"} and exists $request->{"authentication"}{"simple"} 4 8 12
Condition66%4/6
$self->{"allow_anon"} and not $request->{"name"} and exists $request->{"authentication"}{"simple"}
!ll&&!rl&&r
804
$self->{"allow_anon"} and not $request->{"name"}
!ll&&!rl&&r
084
MC/DC33%1/3
$self->{"allow_anon"} and not $request->{"name"} and exists $request->{"authentication"}{"simple"}
$self->{"allow_anon"} not $request->{"name"} exists $request->{"authentication"}{"simple"}
Truth table50%2/4
A and not(B) and C
A= $self->{"allow_anon"}B= not $request->{"name"}C= exists $request->{"authentication"}{"simple"}
ABCresult
0XX0
10X0
1100
1111
116  
117 # As of now, accepts only simple authentication
118 return _make_result(LDAP_AUTH_METHOD_NOT_SUPPORTED)
119 8 ▶ unless exists $request->{authentication}->{simple};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $request->{"authentication"}{"simple"} 1 7 8
120  
121 7 my $bind_pw = $request->{authentication}->{simple};
122 7 chomp($bind_pw);
123  
124 7 my $bind_dn = canonical_dn( $request->{name} );
125 7 ▶ unless ($bind_dn) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bind_dn) { } 4 3 7
(do { uc $bind_dn ne uc $self->{"root_dn"} }) 1 3 4
126 3 my $search_user_result = $self->_find_user_dn( $request->{name} );
127 3 my $size = scalar( @{$search_user_result} );
128  
129 return _make_result( LDAP_INVALID_DN_SYNTAX, '',
130 'Cannot find user: ' . $request->{name} )
131 3 ▶ if $size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size == 0 1 2 3
132 return _make_result( LDAP_INVALID_DN_SYNTAX, '',
133 'Cannot retrieve an unique user entry for id: ' . $request->{name} )
134 2 ▶ if $size > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $size > 1 0 2 2
135  
136 2 $bind_dn = $search_user_result->[0];
137 }
138 0 elsif ( uc($bind_dn) ne uc( $self->{root_dn} ) ) {
139 my $search_dn_result =
140 1 $self->{store}->list_with_dn_scope( $bind_dn, SCOPE_BASEOBJ );
141 return _make_result( LDAP_INVALID_DN_SYNTAX, '',
142 'Cannot find user: ' . $request->{name} )
143 1 ▶ unless $search_dn_result;
Branch: 1 branch
BranchTrueFalseTotal
unless $search_dn_result 0 1 1
144  
145 1 $bind_dn = $search_dn_result->[0];
146 }
147  
148 6 ▶ if ( $bind_dn->isa('Net::LDAP::Entry') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bind_dn->isa('Net::LDAP::Entry')) { } 3 3 6
elsif (uc $bind_dn eq uc $self->{'root_dn'}) { } 3 0 3
149  
150 # user was not a dn, but it was found
151 3 my $entry_pw = $bind_dn->get_value( $self->{user_pw_attr} );
152  
153 3 my $regexp = '^' . MD5_PREFIX;
154 3 ▶ $entry_pw = _encode_password($entry_pw) if $entry_pw =~ /$regexp/;
Branch: 1 branch
BranchTrueFalseTotal
if $entry_pw =~ /$regexp/ 0 3 3
155  
156 3 ▶ return _make_result( LDAP_INVALID_CREDENTIALS, undef,
Branch: 1 branch
BranchTrueFalseTotal
unless $entry_pw eq $bind_pw 1 2 3
157 'entry dn: ' . $bind_dn->dn() )
158 unless $entry_pw eq $bind_pw;
159 }
160 elsif ( uc($bind_dn) eq uc( $self->{root_dn} ) ) {
161 return _make_result( LDAP_INVALID_CREDENTIALS, undef,
162 'bind dn: ' . $bind_dn )
163 3 ▶ unless $bind_pw eq $self->{root_pw};
Branch: 1 branch
BranchTrueFalseTotal
unless $bind_pw eq $self->{"root_pw"} 1 2 3
164 }
165 else {
166 return _make_result( LDAP_INVALID_DN_SYNTAX, '',
167 0 'Cannot find user: ' . $request->{name} );
168 }
169  
170 4 return _make_result(LDAP_SUCCESS);
171 }
172  
173 sub _match {
174 6 ▶ my ( $filter_spec, $elems ) = @_;
Subroutine _match: called CC=1 SCAR=0.0
175  
176 6 my $f = bless $filter_spec, 'Net::LDAP::Filter';
177 6 return [ grep { $f->match($_) } @{$elems} ];
178 }
179  
180 sub _encode_pw_attr {
181 1 ▶ my ( $pw_attr, $entry ) = @_;
Subroutine _encode_pw_attr: called CC=5 SCAR=16.3
182  
183 1 ▶ return $entry unless grep { /person/ } $entry->get_value('objectclass');
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/person/;} $entry->get_value("objectclass") 0 1 1
184 1 ▶ return $entry unless $entry->exists($pw_attr);
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->exists($pw_attr) 0 1 1
185  
186 1 my $clone = $entry->clone();
187 1 my @pwlist = ();
188 1 my $regexp = '^' . MD5_PREFIX;
189 1 foreach ( $clone->get_value($pw_attr) ) {
190 1 ▶ next if /$regexp/;
Branch: 1 branch
BranchTrueFalseTotal
if /$regexp/ 0 1 1
191 1 push @pwlist, _encode_password($_);
192 }
193 1 $clone->delete($pw_attr);
194 1 $clone->add( $pw_attr => [@pwlist] );
195 1 return $clone;
196  
197 }
198  
199 sub _remove_pw_attr {
200 1 ▶ my ( $pw_attr, $entry ) = @_;
Subroutine _remove_pw_attr: called CC=2 SCAR=7.0
201  
202 1 my $clone = $entry->clone();
203 1 ▶ $clone->delete($pw_attr) if $clone->exists($pw_attr);
Branch: 1 branch
BranchTrueFalseTotal
if $clone->exists($pw_attr) 1 0 1
204 1 return $clone;
205 }
206  
207 sub _filter_attrs {
208 3 ▶ my ( $self, $list ) = @_;
Subroutine _filter_attrs: called CC=4 SCAR=13.9
209  
210 # TODO find a better way to keep the store read-only but not costly to return searches with filtered attributes
211 3 ▶ return $list if $self->{user_passwords} eq USER_PW_ALL;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"user_passwords"} eq "all" 1 2 3
212  
213 1 return [ map { _remove_pw_attr( $self->{user_pw_attr}, $_ ) } @{$list} ]
214 2 ▶ if $self->{user_passwords} eq USER_PW_NONE;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"user_passwords"} eq "none" 1 1 2
215  
216 1 return [ map { _encode_pw_attr( $self->{user_pw_attr}, $_ ) } @{$list} ]
217 1 ▶ if $self->{user_passwords} eq USER_PW_MD5;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"user_passwords"} eq "md5" 1 0 1
218 }
219  
220 sub search {
221 4 ▶ my ( $self, $request ) = @_;
Subroutine search: called CC=4 SCAR=14.1
Pod documented
222  
223 4 my $list;
224 4 ▶ if ( defined( $request->{baseObject} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $request->{'baseObject'}) { } 4 0 4
225 4 my $basedn = canonical_dn( $request->{baseObject} );
226 4 ▶ my $scope = $request->{scope} || SCOPE_SUBTREE;
Condition50%1/2
$request->{'scope'} || 2
l!l
40
MC/DC0%0/1
$request->{'scope'} || 2
$request->{'scope'}
Truth table50%1/2
A
A= $request->{'scope'}
Aresult
11
00
227  
228 4 $list = $self->{store}->list_with_dn_scope( $basedn, $scope );
229 4 ▶ return _make_result( LDAP_NO_SUCH_OBJECT, '',
Branch: 1 branch
BranchTrueFalseTotal
unless defined $list 1 3 4
230 'Cannot find BaseDN "' . $basedn . '"' )
231 unless defined($list);
232 }
233 else {
234 0 $list = $self->{store}->list();
235 }
236  
237 3 my $match = $self->_filter_attrs( _match( $request->{filter}, $list ) );
238  
239 3 return ( _make_result(LDAP_SUCCESS), @{$match} );
240 }
241  
242 40 1; # Magic true value required at end of module
243  
244 __END__
« blib/lib/Net/LDAP/SimpleServer.pm ↑ Summary lib/Net/LDAP/SimpleServer/LDIFStore.pm »