blib/lib/Lemonldap/NG/Common/Conf/Backends/LDAP.pm

filter: Statementstmt 18.5% 25 / 135 Branchbran 2.0% 1 / 50 Conditioncond 12.5% 3 / 24 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 41.1% 7 / 17 Podpod 0.0% 0 / 11 totaltotal 14.4% 36 / 250 CC 40 SCAR 52.1
CC
40
Cov
14%
getLdapConnection
462.0
load
42.0
store
30.0
CRAP 183.6
« blib/lib/Lemonldap/NG/Common/Session/REST.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Conf/RESTServer.pm »
1 ##@file
2 # LDAP configuration backend
3  
4 ##@class
5 # Implements LDAP backend for Lemonldap::NG
6 package Lemonldap::NG::Common::Conf::Backends::LDAP;
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Net::LDAP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Lemonldap::NG::Common::Conf::Constants; #inherits
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Lemonldap::NG::Common::Conf::Serializer;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = '2.23.0';
15  
16 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 *Lemonldap::NG::Common::Conf::getLdapConnection = \&getLdapConnection;
18 }
19  
20 1 ▶sub prereq {
Subroutine prereq: called CC=6 SCAR=20.3
Pod undocumented
21 1 my $self = shift;
22 1 foreach ( 'ldapServer', 'ldapConfBase', 'ldapBindDN', 'ldapBindPassword' ) {
23 4 ▶ unless ( $self->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$_}) 0 4 4
24 0 $Lemonldap::NG::Common::Conf::msg .=
25 "$_ is required in LDAP configuration type \n";
26 0 return 0;
27 }
28 }
29 1 ▶ $self->{ldapObjectClass} ||= "applicationProcess";
Condition50%1/2
$self->{"ldapObjectClass"} ||= "applicationProcess"
l!l
01
MC/DC0%0/1
$self->{"ldapObjectClass"} ||= "applicationProcess"
$self->{"ldapObjectClass"}
Truth table50%1/2
A
A= $self->{"ldapObjectClass"}
Aresult
11
00
30 1 ▶ $self->{ldapAttributeId} ||= "cn";
Condition50%1/2
$self->{"ldapAttributeId"} ||= "cn"
l!l
01
MC/DC0%0/1
$self->{"ldapAttributeId"} ||= "cn"
$self->{"ldapAttributeId"}
Truth table50%1/2
A
A= $self->{"ldapAttributeId"}
Aresult
11
00
31 1 ▶ $self->{ldapAttributeContent} ||= "description";
Condition50%1/2
$self->{"ldapAttributeContent"} ||= "description"
l!l
01
MC/DC0%0/1
$self->{"ldapAttributeContent"} ||= "description"
$self->{"ldapAttributeContent"}
Truth table50%1/2
A
A= $self->{"ldapAttributeContent"}
Aresult
11
00
32 1 1;
33 }
34  
35 0 ▶sub available {
Subroutine available: not called CC=4 SCAR=30.0
Pod undocumented
36 0 my $self = shift;
37  
38 0 my $ldap = $self->getLdapConnection;
39 0 ▶ unless ($ldap) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ldap) 0 0 0
40 0 return 0;
41 }
42  
43 my $search = $ldap->search(
44 base => $self->{ldapConfBase},
45 filter => '(objectClass=' . $self->{ldapObjectClass} . ')',
46 scope => 'one',
47 0 attrs => [ $self->{ldapAttributeId} ],
48 );
49 0 $ldap->unbind;
50  
51 0 ▶ if ( $search->code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search->code) 0 0 0
52 0 $self->logError($search);
53 0 return 0;
54 }
55  
56 0 my @entries = $search->entries();
57 0 my @conf;
58 0 foreach (@entries) {
59 0 my $cn = $_->get_value( $self->{ldapAttributeId} );
60 0 my ($cfgNum) = ( $cn =~ /lmConf-(\d*)/ );
61 0 push @conf, $cfgNum;
62 }
63 0 return sort { $a <=> $b } @conf;
64 }
65  
66 0 ▶sub lastCfg {
Subroutine lastCfg: not called CC=1 SCAR=6.9
Pod undocumented
67 0 my $self = shift;
68 0 my @avail = $self->available;
69 0 return $avail[$#avail];
70 }
71  
72 0 ▶sub getLdapConnection {
Subroutine getLdapConnection: not called CC=21 SCAR=61.4
Pod undocumented
73 0 my $self = shift;
74  
75 # Parse servers configuration
76 0 my $useTls = 0;
77 0 my $tlsParam;
78 0 my @servers = ();
79 0 foreach my $server ( split /[\s,]+/, $self->{ldapServer} ) {
80 0 ▶ if ( $server =~ m{^ldap\+tls://([^/]+)/?\??(.*)$} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server =~ m[^ldap\+tls://([^/]+)/?\??(.*)$]) { } 0 0 0
81 0 $useTls = 1;
82 0 $server = $1;
83 0 ▶ $tlsParam = $2 || "";
Condition0%0/2
$2 || ''
l!l
00
MC/DC0%0/1
$2 || ''
$2
Truth table0%0/2
A
A= $2
Aresult
11
00
84 }
85 else {
86 0 $useTls = 0;
87 }
88 0 push @servers, $server;
89 }
90  
91 # Connect
92 my $ldap = Net::LDAP->new(
93 \@servers,
94 keepalive => 1,
95 onerror => undef,
96 verify => ( $self->{ldapVerify} || "require" ),
97 ( $self->{ldapCAFile} ? ( cafile => $self->{ldapCAFile} ) : () ),
98 ( $self->{ldapCAPath} ? ( capath => $self->{ldapCAPath} ) : () ),
99 0 ▶ ( $self->{ldapPort} ? ( port => $self->{ldapPort} ) : () ),
Branch: 3 branches
BranchTrueFalseTotal
$self->{'ldapCAFile'} ? : 0 0 0
$self->{'ldapCAPath'} ? : 0 0 0
$self->{'ldapPort'} ? : 0 0 0
Condition0%0/2
$self->{'ldapVerify'} || 'require'
l!l
00
MC/DC0%0/1
$self->{'ldapVerify'} || 'require'
$self->{'ldapVerify'}
Truth table0%0/2
A
A= $self->{'ldapVerify'}
Aresult
11
00
100 raw => qr/(?i:^jpegPhoto|;binary)/
101 );
102  
103 0 ▶ unless ($ldap) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ldap) { } 0 0 0
if (do { $Net::LDAP::VERSION < "0.64" }) 0 0 0
104 0 $Lemonldap::NG::Common::Conf::msg .= "$@\n";
105 0 return;
106 }
107 0 elsif ( $Net::LDAP::VERSION < '0.64' ) {
108  
109 # CentOS7 has a bug in which IO::Socket::SSL will return a broken
110 # socket when certificate validation fails. Net::LDAP does not catch
111 # it, and the process ends up crashing.
112 # As a precaution, make sure the underlying socket is doing fine:
113 0 ▶ if ( $ldap->socket->isa('IO::Socket::SSL')
Branch: 1 branch
BranchTrueFalseTotal
if ($ldap->socket->isa("IO::Socket::SSL") and $ldap->socket->errstr < 0) 0 0 0
Condition0%0/3
$ldap->socket->isa("IO::Socket::SSL") and $ldap->socket->errstr < 0
!ll&&!rl&&r
000
MC/DC0%0/2
$ldap->socket->isa("IO::Socket::SSL") and $ldap->socket->errstr < 0
$ldap->socket->isa("IO::Socket::SSL") $ldap->socket->errstr < 0
Truth table0%0/3
A and B
A= $ldap->socket->isa("IO::Socket::SSL")B= $ldap->socket->errstr < 0
ABresult
0X0
100
111
114 and $ldap->socket->errstr < 0 )
115 {
116 0 $Lemonldap::NG::Common::Conf::msg .=
117 "SSL connection error: " . $ldap->socket->errstr;
118 0 return;
119 }
120 }
121  
122 # Start TLS if needed
123 0 ▶ if ($useTls) {
Branch: 1 branch
BranchTrueFalseTotal
if ($useTls) 0 0 0
124 0 my %h = split( /[&=]/, $tlsParam );
125 0 ▶ $h{verify} ||= $self->{ldapVerify} || "require";
Condition0%0/5
$h{"verify"} ||= $self->{"ldapVerify"} || "require"
l!l&&r!l&&!r
000
$self->{'ldapVerify'} || 'require'
l!l
00
MC/DC0%0/2
$h{"verify"} ||= $self->{"ldapVerify"} || "require"
$h{"verify"} $self->{'ldapVerify'}
Truth table0%0/3
A ||= B
A= $h{"verify"}B= $self->{'ldapVerify'}
ABresult
1X1
011
000
126 0 ▶ $h{cafile} ||= $self->{ldapCAFile} if ( $self->{ldapCAFile} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ldapCAFile"} 0 0 0
Condition0%0/3
$h{"cafile"} ||= $self->{"ldapCAFile"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$h{"cafile"} ||= $self->{"ldapCAFile"}
$h{"cafile"} $self->{"ldapCAFile"}
Truth table0%0/3
A ||= B
A= $h{"cafile"}B= $self->{"ldapCAFile"}
ABresult
1X1
011
000
127 0 ▶ $h{capath} ||= $self->{ldapCAPath} if ( $self->{ldapCAPath} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ldapCAPath"} 0 0 0
Condition0%0/3
$h{"capath"} ||= $self->{"ldapCAPath"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$h{"capath"} ||= $self->{"ldapCAPath"}
$h{"capath"} $self->{"ldapCAPath"}
Truth table0%0/3
A ||= B
A= $h{"capath"}B= $self->{"ldapCAPath"}
ABresult
1X1
011
000
128 0 my $start_tls = $ldap->start_tls(%h);
129 0 ▶ if ( $start_tls->code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start_tls->code) 0 0 0
130 0 $self->logError($start_tls);
131 0 return;
132 }
133 }
134  
135 # Bind with credentials
136 my $bind =
137 0 $ldap->bind( $self->{ldapBindDN}, password => $self->{ldapBindPassword} );
138 0 ▶ if ( $bind->code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bind->code) 0 0 0
139 0 $self->logError($bind);
140 0 return;
141 }
142  
143 0 return $ldap;
144 }
145  
146 0 ▶sub lock {
Subroutine lock: not called CC=1 SCAR=6.9
Pod undocumented
147  
148 # No lock for LDAP
149 0 return 1;
150 }
151  
152 0 ▶sub isLocked {
Subroutine isLocked: not called CC=1 SCAR=6.9
Pod undocumented
153  
154 # No lock for LDAP
155 0 return 0;
156 }
157  
158 0 ▶sub unlock {
Subroutine unlock: not called CC=1 SCAR=6.9
Pod undocumented
159  
160 # No lock for LDAP
161 0 return 1;
162 }
163  
164 0 ▶sub store {
Subroutine store: not called CC=5 SCAR=34.0
Pod undocumented
165 0 my ( $self, $fields ) = @_;
166  
167 0 my $ldap = $self->getLdapConnection;
168 0 ▶ unless ($ldap) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ldap) 0 0 0
169 0 return 0;
170 }
171  
172 0 $fields = $self->serialize($fields);
173  
174 0 my $lastCfg = $self->lastCfg;
175  
176 0 my $confName = "lmConf-" . $fields->{cfgNum};
177 my $confDN =
178 0 $self->{ldapAttributeId} . "=$confName," . $self->{ldapConfBase};
179  
180 # Store values as {key}value
181 0 my @confValues;
182 0 ▶ while ( my ( $k, $v ) = each(%$fields) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$fields) 0 0 0
183 0 push @confValues, "{$k}$v";
184 }
185  
186 0 my $operation;
187  
188 0 ▶ if ( $lastCfg == $fields->{cfgNum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lastCfg == $fields->{'cfgNum'}) { } 0 0 0
189 $operation =
190 $ldap->modify( $confDN,
191 0 replace => { $self->{ldapAttributeContent} => \@confValues } );
192 }
193 else {
194 $operation = $ldap->add(
195 $confDN,
196 attrs => [
197 objectClass => [ 'top', $self->{ldapObjectClass} ],
198 $self->{ldapAttributeId} => $confName,
199 0 $self->{ldapAttributeContent} => \@confValues,
200 ]
201 );
202 }
203 0 $ldap->unbind;
204  
205 0 ▶ if ( $operation->code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operation->code) 0 0 0
206 0 $self->logError($operation);
207 0 return 0;
208 }
209  
210 0 return $fields->{cfgNum};
211 }
212  
213 0 ▶sub load {
Subroutine load: not called CC=6 SCAR=37.4
Pod undocumented
214 0 my ( $self, $cfgNum, $fields ) = @_;
215  
216 0 my $ldap = $self->getLdapConnection;
217 0 ▶ unless ($ldap) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ldap) 0 0 0
218 0 return;
219 }
220  
221 0 my $f;
222 0 my $confName = "lmConf-" . $cfgNum;
223 my $confDN =
224 0 $self->{ldapAttributeId} . "=$confName," . $self->{ldapConfBase};
225  
226 my $search = $ldap->search(
227 base => $confDN,
228 filter => '(objectClass=' . $self->{ldapObjectClass} . ')',
229 scope => 'base',
230 0 attrs => [ $self->{ldapAttributeContent} ],
231 );
232 0 $ldap->unbind;
233  
234 0 ▶ if ( $search->code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search->code) 0 0 0
235 0 $self->logError($search);
236 0 return;
237 }
238  
239 0 my $entry = $search->shift_entry();
240 0 my @confValues = $entry->get_value( $self->{ldapAttributeContent} );
241 0 foreach (@confValues) {
242 0 my ( $k, $v ) = ( $_ =~ /\{(.*?)\}(.*)/s );
243 0 ▶ if ($fields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields) { } 0 0 0
244 0 ▶ $f->{$k} = $v if ( grep { $_ eq $k } @$fields );
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $k;} @$fields 0 0 0
245 }
246 else {
247 0 $f->{$k} = $v;
248 }
249 }
250 0 return $self->unserialize($f);
251 }
252  
253 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
Pod undocumented
254 0 my ( $self, $cfgNum ) = @_;
255  
256 0 my $ldap = $self->getLdapConnection;
257 0 ▶ unless ($ldap) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ldap) 0 0 0
258 0 return 0;
259 }
260  
261 my $confDN =
262 $self->{ldapAttributeId}
263 . "=lmConf-"
264 . $cfgNum . ","
265 0 . $self->{ldapConfBase};
266 0 my $delete = $ldap->delete($confDN);
267 0 $ldap->unbind;
268 0 ▶ $self->logError($delete) if ( $delete->code );
Branch: 1 branch
BranchTrueFalseTotal
if $delete->code 0 0 0
269 }
270  
271 0 ▶sub logError {
Subroutine logError: not called CC=1 SCAR=6.9
Pod undocumented
272 0 my $self = shift;
273 0 my $ldap_operation = shift;
274 0 $Lemonldap::NG::Common::Conf::msg .=
275 "LDAP error "
276 . $ldap_operation->code . ": "
277 . $ldap_operation->error . " \n";
278 }
279  
280 1 1;
281 __END__
« blib/lib/Lemonldap/NG/Common/Session/REST.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Conf/RESTServer.pm »