blib/lib/Lemonldap/NG/Portal/Lib/LDAP.pm

filter: Statementstmt 15.6% 20 / 128 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 25.0% 4 / 16 Podpod 0.0% 0 / 8 totaltotal 10.4% 24 / 229 CC 34 SCAR 43.4
CC
34
Cov
10%
findUser
210.0
getUser
72.0
validateLdap
20.0
CRAP 77.0
« blib/lib/Lemonldap/NG/Portal/Plugins/OIDC/NativeSso.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/CrowdSecAgent.pm »
1 package Lemonldap::NG::Portal::Lib::LDAP;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Lemonldap::NG::Portal::Lib::Net::LDAP;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Lemonldap::NG::Portal::Main::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
7 PE_OK
8 PE_LDAPERROR
9 PE_USERNOTFOUND
10 PE_BADCREDENTIALS
11 PE_LDAPCONNECTFAILED
12 2 );
13  
14 2 extends 'Lemonldap::NG::Common::Module';
15  
16 2 our $VERSION = '2.19.0';
17  
18 # PROPERTIES
19  
20 2 has ldap => (
21 is => 'rw',
22 lazy => 1,
23 builder => 'newLdap',
24 );
25  
26 has attrs => (
27 is => 'rw',
28 lazy => 1,
29 0 ▶ builder => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 return [
31 0 values %{ $_[0]->{conf}->{exportedVars} },
32 0 values %{ $_[0]->{conf}->{ldapExportedVars} }
33 ];
34 }
35 2 );
36  
37 0 ▶sub newLdap {
Subroutine newLdap: not called CC=3 SCAR=24.8
Pod undocumented
38 0 my $self = $_[0];
39 0 my $ldap;
40  
41 # Build object and test LDAP connection
42 0 $self->logger->debug(
43 "Try to build new LDAP connection with: $self->{conf}->{ldapServer}");
44  
45 return undef
46 unless (
47 $ldap = Lemonldap::NG::Portal::Lib::Net::LDAP->new(
48 { p => $self->{p}, conf => $self->{conf} }
49 )
50 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
unless $ldap = "Lemonldap::NG::Portal::Lib::Net::LDAP"->new({"p", $self->{"p"}, "conf", $self->{"conf"}}) 0 0 0
51  
52 # Test connection
53 0 my $msg = $ldap->bind;
54 0 ▶ $self->logger->error( 'LDAP test has failed: ' . $msg->error )
Branch: 1 branch
BranchTrueFalseTotal
if $msg->code 0 0 0
55 if $msg->code;
56  
57 0 return $ldap;
58 }
59  
60 2 has filter => (
61 is => 'rw',
62 lazy => 1,
63 builder => 'buildFilter',
64 );
65  
66 2 has mailFilter => (
67 is => 'rw',
68 lazy => 1,
69 builder => 'buildMailFilter',
70 );
71  
72 has findUserFilter => (
73 is => 'ro',
74 lazy => 1,
75 0 ▶ builder => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
76 $_[0]->conf->{AuthLDAPFilter}
77 || $_[0]->conf->{LDAPFilter}
78 0 ▶ || '(&(uid=$user)(objectClass=inetOrgPerson))';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"AuthLDAPFilter"} or $_[0]->conf->{"LDAPFilter"} or "(&(uid=\$user)(objectClass=inetOrgPerson))" 0 0 0
Condition0%0/3
$_[0]->conf->{"AuthLDAPFilter"} or $_[0]->conf->{"LDAPFilter"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]->conf->{"AuthLDAPFilter"} or $_[0]->conf->{"LDAPFilter"}
$_[0]->conf->{"AuthLDAPFilter"} $_[0]->conf->{"LDAPFilter"}
Truth table0%0/3
A or B
A= $_[0]->conf->{"AuthLDAPFilter"}B= $_[0]->conf->{"LDAPFilter"}
ABresult
1X1
011
000
79 }
80 2 );
81  
82 0 ▶sub buildFilter {
Subroutine buildFilter: not called CC=3 SCAR=24.8
Pod undocumented
83 return $_[0]->_buildFilter( $_[0]->conf->{AuthLDAPFilter}
84 || $_[0]->conf->{LDAPFilter}
85 0 ▶ || '(&(uid=$user)(objectClass=inetOrgPerson))' );
Condition0%0/5
$_[0]->conf->{'AuthLDAPFilter'} || $_[0]->conf->{'LDAPFilter'} || '(&(uid=$user)(objectClass=inetOrgPerson))'
l!l
00
$_[0]->conf->{"AuthLDAPFilter"} or $_[0]->conf->{"LDAPFilter"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]->conf->{'AuthLDAPFilter'} || $_[0]->conf->{'LDAPFilter'} || '(&(uid=$user)(objectClass=inetOrgPerson))'
$_[0]->conf->{"AuthLDAPFilter"} $_[0]->conf->{"LDAPFilter"}
Truth table0%0/3
A or B
A= $_[0]->conf->{"AuthLDAPFilter"}B= $_[0]->conf->{"LDAPFilter"}
ABresult
1X1
011
000
86 }
87  
88 0 ▶sub buildMailFilter {
Subroutine buildMailFilter: not called CC=2 SCAR=17.9
Pod undocumented
89 my $f = $_[0]->conf->{mailLDAPFilter}
90 0 ▶ || '(&(mail=$user)(objectClass=inetOrgPerson))';
Condition0%0/2
$_[0]->conf->{'mailLDAPFilter'} || '(&(mail=$user)(objectClass=inetOrgPerson))'
l!l
00
MC/DC0%0/1
$_[0]->conf->{'mailLDAPFilter'} || '(&(mail=$user)(objectClass=inetOrgPerson))'
$_[0]->conf->{'mailLDAPFilter'}
Truth table0%0/2
A
A= $_[0]->conf->{'mailLDAPFilter'}
Aresult
11
00
91 0 $f =~ s/\$mail\b/\$user/g;
92 0 return $_[0]->_buildFilter($f);
93 }
94  
95 0 ▶sub _buildFilter {
Subroutine _buildFilter: not called CC=2 SCAR=17.9
96 0 my ( $self, $filter ) = @_;
97 0 my $conf = $self->{conf};
98 0 $self->{p}->logger->debug("LDAP Search base: $_[0]->{conf}->{ldapBase}");
99 0 $filter =~ s/"/\\"/g;
100 0 $filter =~ s/\$(\w+)/".\$req->{sessionInfo}->{$1}."/g;
101 0 $filter =~ s/\$req->\{sessionInfo\}->\{user\}/\$req->{user}/g;
102 0 $filter =~
103 s/\$req->\{sessionInfo\}->\{(_?password|mail)\}/\$req->{data}->{$1}/g;
104 0 $self->{p}->logger->debug("LDAP transformed filter: $filter");
105 0 $filter = "sub{my(\$req)=\$_[0];return \"$filter\";}";
106 0 my $res = eval $filter;
107 0 ▶ $self->error("Unable to build fiter: $@") if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
108  
109 0 return $res;
110 }
111  
112 # INITIALIZATION
113  
114 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
115 0 my ($self) = @_;
116  
117 # #2932: no need to initialize LDAP connection at this step,
118 # it will be done later, in other ldap methods
119 0 $self->filter;
120 }
121  
122 # RUNNING METHODS
123  
124 0 ▶sub getUser {
Subroutine getUser: not called CC=8 SCAR=42.8
Pod undocumented
125 0 my ( $self, $req, %args ) = @_;
126  
127 0 $self->validateLdap;
128 0 ▶ unless ( $self->ldap ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->ldap) 0 0 0
129 0 eval { $self->p->_authentication->setSecurity($req) };
130 0 return PE_LDAPCONNECTFAILED;
131 }
132 0 ▶ unless ( $self->bind() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->bind) 0 0 0
133 0 eval { $self->p->_authentication->setSecurity($req) };
134 0 return PE_LDAPERROR;
135 }
136  
137 my $mesg = $self->ldap->search(
138 base => $self->conf->{ldapBase},
139 scope => 'sub',
140 filter => (
141 $args{useMail}
142 ? $self->mailFilter->($req)
143 : $self->filter->($req)
144 ),
145 0 ▶ deref => $self->conf->{ldapSearchDeref} || 'find',
Branch: 1 branch
BranchTrueFalseTotal
$args{'useMail'} ? : 0 0 0
Condition0%0/2
$self->conf->{'ldapSearchDeref'} || 'find'
l!l
00
MC/DC0%0/1
$self->conf->{'ldapSearchDeref'} || 'find'
$self->conf->{'ldapSearchDeref'}
Truth table0%0/2
A
A= $self->conf->{'ldapSearchDeref'}
Aresult
11
00
146 attrs => $self->attrs,
147 );
148 0 ▶ if ( $mesg->code() != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mesg->code != 0) 0 0 0
149 0 $self->logger->error(
150 'LDAP Search error ' . $mesg->code . ': ' . $mesg->error );
151 0 eval { $self->p->_authentication->setSecurity($req) };
152 0 return PE_LDAPERROR;
153 }
154 0 ▶ if ( $mesg->count() > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mesg->count > 1) 0 0 0
155 0 $self->logger->error('More than one entry returned by LDAP directory');
156 0 eval { $self->p->_authentication->setSecurity($req) };
157 0 return PE_BADCREDENTIALS;
158 }
159 0 ▶ unless ( $req->data->{ldapentry} = $mesg->entry(0) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->data->{"ldapentry"} = $mesg->entry(0)) 0 0 0
160 0 $self->userLogger->warn(
161 "$req->{user} was not found in LDAP directory ("
162 . $req->address
163 . ')' );
164 0 eval { $self->p->_authentication->setSecurity($req) };
165 0 return PE_BADCREDENTIALS;
166 }
167 0 $req->data->{dn} = $req->data->{ldapentry}->dn();
168  
169 0 return PE_OK;
170 }
171  
172 0 ▶sub findUser {
Subroutine findUser: not called CC=14 SCAR=53.5
Pod undocumented
173 0 my ( $self, $req, %args ) = @_;
174 my $plugin =
175 0 $self->p->loadedModules->{"Lemonldap::NG::Portal::Plugins::FindUser"};
176 0 my ( $searching, $excluding ) = $plugin->retreiveFindUserParams($req);
177 0 eval { $self->p->_authentication->setSecurity($req) };
178 0 ▶ return PE_OK unless scalar @$searching;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$searching 0 0 0
179  
180 0 $self->validateLdap;
181 0 ▶ return PE_LDAPCONNECTFAILED unless $self->ldap;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ldap 0 0 0
182  
183 0 ▶ my $filter =
Branch: 1 branch
BranchTrueFalseTotal
$self->findUserFilter =~ /\bobjectClass=(\w+)\b/ ? : 0 0 0
184 $self->findUserFilter =~ /\bobjectClass=(\w+)\b/
185 ? "(&(objectClass=$1)"
186 : '(&';
187 0 my $wildcard = $self->conf->{findUserWildcard};
188 0 ▶ $self->logger->info("LDAP UserDB with wildcard ($wildcard)") if $wildcard;
Branch: 1 branch
BranchTrueFalseTotal
if $wildcard 0 0 0
189 0 foreach (@$searching) {
190 0 ▶ if ($wildcard) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wildcard) { } 0 0 0
191 0 $_->{value} =~ s/\Q$wildcard\E+/*/g;
192 }
193 else {
194 0 $_->{value} =~ s/\Q*\E+//g;
195 }
196 0 $filter .= "($_->{key}=$_->{value})";
197 }
198 0 $filter .= "(!($_->{key}=$_->{value}))" foreach (@$excluding);
199 0 $filter .= ')';
200 0 $self->logger->debug("LDAP UserDB built filter: $filter");
201  
202 0 $self->bind();
203 my $mesg = $self->ldap->search(
204 base => $self->conf->{ldapBase},
205 scope => 'sub',
206 filter => $filter,
207 0 ▶ deref => $self->conf->{ldapSearchDeref} || 'find',
Condition0%0/2
$self->conf->{'ldapSearchDeref'} || 'find'
l!l
00
MC/DC0%0/1
$self->conf->{'ldapSearchDeref'} || 'find'
$self->conf->{'ldapSearchDeref'}
Truth table0%0/2
A
A= $self->conf->{'ldapSearchDeref'}
Aresult
11
00
208 attrs => $self->attrs,
209 sizelimit => 50
210 );
211  
212 0 ▶ if ( $mesg->code() != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mesg->code != 0) 0 0 0
213 0 $self->logger->error(
214 'LDAP Search error ' . $mesg->code . ": " . $mesg->error );
215 0 return PE_LDAPERROR;
216 }
217  
218 $self->logger->debug(
219 0 'LDAP UserDB number of result(s): ' . $mesg->count() );
220 0 ▶ if ( $mesg->count() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mesg->count) 0 0 0
221 my @results =
222 0 ▶ map { ( $_->dn() =~ /\b(?:uid|sAMAccountName)\x3d(.+?),/ )[0] || () }
Condition0%0/3
($_->dn =~ /\b(?:uid|sAMAccountName)\x3d(.+?),/)[0] || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
($_->dn =~ /\b(?:uid|sAMAccountName)\x3d(.+?),/)[0] || ()
($_->dn =~ /\b(?:uid|sAMAccountName)\x3d(.+?),/)[0] ()
Truth table0%0/3
A || B
A= ($_->dn =~ /\b(?:uid|sAMAccountName)\x3d(.+?),/)[0]B= ()
ABresult
1X1
011
000
223 $mesg->entries;
224 0 ▶ if (@results) {
Branch: 1 branch
BranchTrueFalseTotal
if (@results) 0 0 0
225 0 $req->data->{findUserAll} = \@results;
226 0 my $rank = int( rand(@results) );
227 0 $self->logger->debug("LDAP UserDB random rank: $rank");
228 0 my $entry = $results[$rank];
229 0 ▶ $self->userLogger->info("FindUser: LDAP UserDB returns $entry")
Branch: 1 branch
BranchTrueFalseTotal
if $entry 0 0 0
230 if $entry;
231 0 $req->data->{findUser} = $entry;
232 }
233 0 return PE_OK;
234 }
235  
236 0 return PE_USERNOTFOUND;
237 }
238  
239 # Validate LDAP connection before use
240 0 ▶sub validateLdap {
Subroutine validateLdap: not called CC=4 SCAR=30.0
Pod undocumented
241 0 my ($self) = @_;
242 0 ▶ local $SIG{'PIPE'} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
243 0 $self->logger->info('Reconnecting to LDAP server due to broken socket');
244 0 };
245  
246 0 ▶ unless ($self->ldap
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->ldap and $self->ldap->root_dse("attrs", ["supportedLDAPVersion"])) 0 0 0
Condition0%0/3
$self->ldap and $self->ldap->root_dse("attrs", ["supportedLDAPVersion"])
!ll&&!rl&&r
000
MC/DC0%0/2
$self->ldap and $self->ldap->root_dse("attrs", ["supportedLDAPVersion"])
$self->ldap $self->ldap->root_dse("attrs", ["supportedLDAPVersion"])
Truth table0%0/3
A and B
A= $self->ldapB= $self->ldap->root_dse("attrs", ["supportedLDAPVersion"])
ABresult
0X0
100
111
247 and $self->ldap->root_dse( attrs => ['supportedLDAPVersion'] ) )
248 {
249 0 ▶ $self->ldap->DESTROY if $self->ldap;
Branch: 1 branch
BranchTrueFalseTotal
if $self->ldap 0 0 0
250 0 $self->ldap( $self->newLdap );
251 }
252 }
253  
254 # Bind
255 0 ▶sub bind {
Subroutine bind: not called CC=3 SCAR=24.8
Pod undocumented
256 0 my $self = shift;
257  
258 0 $self->validateLdap;
259 0 ▶ return undef unless $self->ldap;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ldap 0 0 0
260  
261 0 my $msg = $self->ldap->bind(@_);
262 0 ▶ if ( $msg->code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->code) 0 0 0
263 0 $self->logger->error( $msg->error );
264 0 return undef;
265 }
266  
267 0 return 1;
268 }
269  
270 2 1;
« blib/lib/Lemonldap/NG/Portal/Plugins/OIDC/NativeSso.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/CrowdSecAgent.pm »