Store/LDAP.pm

filter: Statementstmt 12.5% 11 / 88 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 30.0% 3 / 10 Podpod 0.0% 0 / 7 totaltotal 6.3% 14 / 221 CC 44 SCAR 56.8
CC
44
Cov
6%
ldap
600.0
materialize
56.0
insert
42.0
CRAP 291.6
↑ Summary LDAP.pm »
1 package Apache::Session::Store::LDAP;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Net::LDAP;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 $VERSION = '0.5';
8  
9 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
10 0 my $class = shift;
11 0 return bless {}, $class;
12 }
13  
14 0 ▶sub insert {
Subroutine insert: not called CC=6 SCAR=37.4
Pod undocumented
15 0 my $self = shift;
16 0 my $session = shift;
17 0 $self->{args} = $session->{args};
18 0 ▶ $self->{args}->{ldapObjectClass} ||= 'applicationProcess';
Condition0%0/2
$self->{"args"}{"ldapObjectClass"} ||= "applicationProcess"
l!l
00
MC/DC0%0/1
$self->{"args"}{"ldapObjectClass"} ||= "applicationProcess"
$self->{"args"}{"ldapObjectClass"}
Truth table0%0/2
A
A= $self->{"args"}{"ldapObjectClass"}
Aresult
11
00
19 0 ▶ $self->{args}->{ldapAttributeId} ||= 'cn';
Condition0%0/2
$self->{"args"}{"ldapAttributeId"} ||= "cn"
l!l
00
MC/DC0%0/1
$self->{"args"}{"ldapAttributeId"} ||= "cn"
$self->{"args"}{"ldapAttributeId"}
Truth table0%0/2
A
A= $self->{"args"}{"ldapAttributeId"}
Aresult
11
00
20 0 ▶ $self->{args}->{ldapAttributeContent} ||= 'description';
Condition0%0/2
$self->{"args"}{"ldapAttributeContent"} ||= "description"
l!l
00
MC/DC0%0/1
$self->{"args"}{"ldapAttributeContent"} ||= "description"
$self->{"args"}{"ldapAttributeContent"}
Truth table0%0/2
A
A= $self->{"args"}{"ldapAttributeContent"}
Aresult
11
00
21  
22 my $msg = $self->ldap->add(
23 $self->{args}->{ldapAttributeId} . "="
24 . $session->{data}->{_session_id} . ","
25 . $self->{args}->{ldapConfBase},
26 attrs => [
27 objectClass => $self->{args}->{ldapObjectClass},
28 $self->{args}->{ldapAttributeId} => $session->{data}->{_session_id},
29 $self->{args}->{ldapAttributeContent} => $session->{serialized},
30 0 ],
31 );
32  
33 0 ▶ $self->ldap->unbind() && delete $self->{ldap};
Branch: 1 branch
BranchTrueFalseTotal
$self->ldap->unbind and delete $self->{"ldap"} 0 0 0
34 0 ▶ $self->logError($msg) if ( $msg->code );
Branch: 1 branch
BranchTrueFalseTotal
if $msg->code 0 0 0
35 }
36  
37 0 ▶sub update {
Subroutine update: not called CC=6 SCAR=37.4
Pod undocumented
38 0 my $self = shift;
39 0 my $session = shift;
40 0 $self->{args} = $session->{args};
41 0 ▶ $self->{args}->{ldapObjectClass} ||= 'applicationProcess';
Condition0%0/2
$self->{"args"}{"ldapObjectClass"} ||= "applicationProcess"
l!l
00
MC/DC0%0/1
$self->{"args"}{"ldapObjectClass"} ||= "applicationProcess"
$self->{"args"}{"ldapObjectClass"}
Truth table0%0/2
A
A= $self->{"args"}{"ldapObjectClass"}
Aresult
11
00
42 0 ▶ $self->{args}->{ldapAttributeId} ||= 'cn';
Condition0%0/2
$self->{"args"}{"ldapAttributeId"} ||= "cn"
l!l
00
MC/DC0%0/1
$self->{"args"}{"ldapAttributeId"} ||= "cn"
$self->{"args"}{"ldapAttributeId"}
Truth table0%0/2
A
A= $self->{"args"}{"ldapAttributeId"}
Aresult
11
00
43 0 ▶ $self->{args}->{ldapAttributeContent} ||= 'description';
Condition0%0/2
$self->{"args"}{"ldapAttributeContent"} ||= "description"
l!l
00
MC/DC0%0/1
$self->{"args"}{"ldapAttributeContent"} ||= "description"
$self->{"args"}{"ldapAttributeContent"}
Truth table0%0/2
A
A= $self->{"args"}{"ldapAttributeContent"}
Aresult
11
00
44  
45 my $msg = $self->ldap->modify(
46 $self->{args}->{ldapAttributeId} . "="
47 . $session->{data}->{_session_id} . ","
48 . $self->{args}->{ldapConfBase},
49 replace =>
50 { $self->{args}->{ldapAttributeContent} => $session->{serialized}, },
51 0 );
52  
53 0 ▶ $self->ldap->unbind() && delete $self->{ldap};
Branch: 1 branch
BranchTrueFalseTotal
$self->ldap->unbind and delete $self->{"ldap"} 0 0 0
54 0 ▶ $self->logError($msg) if ( $msg->code );
Branch: 1 branch
BranchTrueFalseTotal
if $msg->code 0 0 0
55 }
56  
57 0 ▶sub materialize {
Subroutine materialize: not called CC=7 SCAR=40.3
Pod undocumented
58 0 my $self = shift;
59 0 my $session = shift;
60 0 $self->{args} = $session->{args};
61 0 ▶ $self->{args}->{ldapObjectClass} ||= 'applicationProcess';
Condition0%0/2
$self->{"args"}{"ldapObjectClass"} ||= "applicationProcess"
l!l
00
MC/DC0%0/1
$self->{"args"}{"ldapObjectClass"} ||= "applicationProcess"
$self->{"args"}{"ldapObjectClass"}
Truth table0%0/2
A
A= $self->{"args"}{"ldapObjectClass"}
Aresult
11
00
62 0 ▶ $self->{args}->{ldapAttributeId} ||= 'cn';
Condition0%0/2
$self->{"args"}{"ldapAttributeId"} ||= "cn"
l!l
00
MC/DC0%0/1
$self->{"args"}{"ldapAttributeId"} ||= "cn"
$self->{"args"}{"ldapAttributeId"}
Truth table0%0/2
A
A= $self->{"args"}{"ldapAttributeId"}
Aresult
11
00
63 0 ▶ $self->{args}->{ldapAttributeContent} ||= 'description';
Condition0%0/2
$self->{"args"}{"ldapAttributeContent"} ||= "description"
l!l
00
MC/DC0%0/1
$self->{"args"}{"ldapAttributeContent"} ||= "description"
$self->{"args"}{"ldapAttributeContent"}
Truth table0%0/2
A
A= $self->{"args"}{"ldapAttributeContent"}
Aresult
11
00
64  
65 my $msg = $self->ldap->search(
66 base => $self->{args}->{ldapAttributeId} . "="
67 . $session->{data}->{_session_id} . ","
68 . $self->{args}->{ldapConfBase},
69 filter => '(objectClass=' . $self->{args}->{ldapObjectClass} . ')',
70 scope => 'base',
71 0 attrs => [ $self->{args}->{ldapAttributeContent} ],
72 );
73  
74 0 ▶ $self->ldap->unbind() && delete $self->{ldap};
Branch: 1 branch
BranchTrueFalseTotal
$self->ldap->unbind and delete $self->{"ldap"} 0 0 0
75 0 ▶ $self->logError($msg) if ( $msg->code );
Branch: 1 branch
BranchTrueFalseTotal
if $msg->code 0 0 0
76  
77 0 eval {
78 $session->{serialized} = $msg->shift_entry()
79 0 ->get_value( $self->{args}->{ldapAttributeContent} );
80 };
81  
82 0 ▶ if ( !defined $session->{serialized} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $session->{"serialized"}) 0 0 0
83 0 die "Object does not exist in data store";
84 }
85 }
86  
87 0 ▶sub remove {
Subroutine remove: not called CC=5 SCAR=34.0
Pod undocumented
88 0 my $self = shift;
89 0 my $session = shift;
90 0 $self->{args} = $session->{args};
91 0 ▶ $self->{args}->{ldapObjectClass} ||= 'applicationProcess';
Condition0%0/2
$self->{"args"}{"ldapObjectClass"} ||= "applicationProcess"
l!l
00
MC/DC0%0/1
$self->{"args"}{"ldapObjectClass"} ||= "applicationProcess"
$self->{"args"}{"ldapObjectClass"}
Truth table0%0/2
A
A= $self->{"args"}{"ldapObjectClass"}
Aresult
11
00
92 0 ▶ $self->{args}->{ldapAttributeId} ||= 'cn';
Condition0%0/2
$self->{"args"}{"ldapAttributeId"} ||= "cn"
l!l
00
MC/DC0%0/1
$self->{"args"}{"ldapAttributeId"} ||= "cn"
$self->{"args"}{"ldapAttributeId"}
Truth table0%0/2
A
A= $self->{"args"}{"ldapAttributeId"}
Aresult
11
00
93 0 ▶ $self->{args}->{ldapAttributeContent} ||= 'description';
Condition0%0/2
$self->{"args"}{"ldapAttributeContent"} ||= "description"
l!l
00
MC/DC0%0/1
$self->{"args"}{"ldapAttributeContent"} ||= "description"
$self->{"args"}{"ldapAttributeContent"}
Truth table0%0/2
A
A= $self->{"args"}{"ldapAttributeContent"}
Aresult
11
00
94  
95 $self->ldap->delete( $self->{args}->{ldapAttributeId} . "="
96 . $session->{data}->{_session_id} . ","
97 0 . $self->{args}->{ldapConfBase} );
98  
99 0 ▶ $self->ldap->unbind() && delete $self->{ldap};
Branch: 1 branch
BranchTrueFalseTotal
$self->ldap->unbind and delete $self->{"ldap"} 0 0 0
100 }
101  
102 0 ▶sub ldap {
Subroutine ldap: not called CC=24 SCAR=64.0
Pod undocumented
103 0 my $self = shift;
104 0 ▶ return $self->{ldap} if ( $self->{ldap} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ldap"} 0 0 0
105  
106 # Parse servers configuration
107 0 my $useTls = 0;
108 0 my $tlsParam;
109 0 my @servers = ();
110 0 foreach my $server ( split /[\s,]+/, $self->{args}->{ldapServer} ) {
111 0 ▶ if ( $server =~ m{^ldap\+tls://([^/]+)/?\??(.*)$} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server =~ m[^ldap\+tls://([^/]+)/?\??(.*)$]) { } 0 0 0
112 0 $useTls = 1;
113 0 $server = $1;
114 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
115 }
116 else {
117 0 $useTls = 0;
118 }
119 0 push @servers, $server;
120 }
121  
122 # Compatibility
123 0 ▶ my $caFile = $self->{args}->{ldapCAFile} || $self->{args}->{caFile};
Condition0%0/3
$self->{'args'}{'ldapCAFile'} || $self->{'args'}{'caFile'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'args'}{'ldapCAFile'} || $self->{'args'}{'caFile'}
$self->{'args'}{'ldapCAFile'} $self->{'args'}{'caFile'}
Truth table0%0/3
A || B
A= $self->{'args'}{'ldapCAFile'}B= $self->{'args'}{'caFile'}
ABresult
1X1
011
000
124 0 ▶ my $caPath = $self->{args}->{ldapCAPath} || $self->{args}->{caPath};
Condition0%0/3
$self->{'args'}{'ldapCAPath'} || $self->{'args'}{'caPath'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'args'}{'ldapCAPath'} || $self->{'args'}{'caPath'}
$self->{'args'}{'ldapCAPath'} $self->{'args'}{'caPath'}
Truth table0%0/3
A || B
A= $self->{'args'}{'ldapCAPath'}B= $self->{'args'}{'caPath'}
ABresult
1X1
011
000
125  
126 # Connect
127 my $ldap = Net::LDAP->new(
128 \@servers,
129 onerror => undef,
130 verify => $self->{args}->{ldapVerify} || "require",
131 ( $caFile ? ( cafile => $caFile ) : () ),
132 ( $caPath ? ( capath => $caPath ) : () ),
133  
134 (
135 $self->{args}->{ldapPort}
136 ? ( port => $self->{args}->{ldapPort} )
137 0 ▶ : ()
Branch: 4 branches
BranchTrueFalseTotal
my $ldap = "Net::LDAP"->new(\@servers, "onerror", undef, "verify", $self->{"args"}{"ldapVerify"} || "require", $caFile ? ("cafile", $caFile) : (), $caPath ? ("capath", $caPath) : (), $self->{"args"}{"ldapPort"} ? ("port", $self->{"args"}{"ldapPort"}) : ()) or die 'Unable to connect to ' . join(" ", @servers) . ': ' . $@ 0 0 0
$caFile ? : 0 0 0
$caPath ? : 0 0 0
$self->{'args'}{'ldapPort'} ? : 0 0 0
Condition0%0/2
$self->{'args'}{'ldapVerify'} || 'require'
l!l
00
MC/DC0%0/1
$self->{'args'}{'ldapVerify'} || 'require'
$self->{'args'}{'ldapVerify'}
Truth table0%0/2
A
A= $self->{'args'}{'ldapVerify'}
Aresult
11
00
138 ),
139 ) or die( 'Unable to connect to ' . join( ' ', @servers ) . ': ' . $@ );
140  
141 # Check SSL error for old Net::LDAP versions
142 0 ▶ if ( $Net::LDAP::VERSION < '0.64' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::LDAP::VERSION < "0.64") 0 0 0
143  
144 # CentOS7 has a bug in which IO::Socket::SSL will return a broken
145 # socket when certificate validation fails. Net::LDAP does not catch
146 # it, and the process ends up crashing.
147 # As a precaution, make sure the underlying socket is doing fine:
148 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
149 and $ldap->socket->errstr < 0 )
150 {
151 0 die( "SSL connection error: " . $ldap->socket->errstr );
152 }
153 }
154  
155 # Start TLS if needed
156 0 ▶ if ($useTls) {
Branch: 1 branch
BranchTrueFalseTotal
if ($useTls) 0 0 0
157 0 my %h = split( /[&=]/, $tlsParam );
158 0 ▶ $h{verify} ||= ( $self->{args}->{ldapVerify} || "require" );
Condition0%0/5
$h{"verify"} ||= $self->{"args"}{"ldapVerify"} || "require"
l!l&&r!l&&!r
000
$self->{'args'}{'ldapVerify'} || 'require'
l!l
00
MC/DC0%0/2
$h{"verify"} ||= $self->{"args"}{"ldapVerify"} || "require"
$h{"verify"} $self->{'args'}{'ldapVerify'}
Truth table0%0/3
A ||= B
A= $h{"verify"}B= $self->{'args'}{'ldapVerify'}
ABresult
1X1
011
000
159 0 ▶ $h{cafile} ||= $caFile if ($caFile);
Branch: 1 branch
BranchTrueFalseTotal
if $caFile 0 0 0
Condition0%0/3
$h{"cafile"} ||= $caFile
l!l&&r!l&&!r
000
MC/DC0%0/2
$h{"cafile"} ||= $caFile
$h{"cafile"} $caFile
Truth table0%0/3
A ||= B
A= $h{"cafile"}B= $caFile
ABresult
1X1
011
000
160 0 ▶ $h{capath} ||= $caPath if ($caPath);
Branch: 1 branch
BranchTrueFalseTotal
if $caPath 0 0 0
Condition0%0/3
$h{"capath"} ||= $caPath
l!l&&r!l&&!r
000
MC/DC0%0/2
$h{"capath"} ||= $caPath
$h{"capath"} $caPath
Truth table0%0/3
A ||= B
A= $h{"capath"}B= $caPath
ABresult
1X1
011
000
161 0 my $start_tls = $ldap->start_tls(%h);
162 0 ▶ if ( $start_tls->code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start_tls->code) 0 0 0
163 0 $self->logError($start_tls);
164 0 return;
165 }
166 }
167  
168 # Bind with credentials
169 my $bind = $ldap->bind( $self->{args}->{ldapBindDN},
170 0 password => $self->{args}->{ldapBindPassword} );
171 0 ▶ if ( $bind->code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bind->code) 0 0 0
172 0 $self->logError($bind);
173 0 return;
174 }
175  
176 0 $self->{ldap} = $ldap;
177 0 return $ldap;
178 }
179  
180 0 ▶sub logError {
Subroutine logError: not called CC=1 SCAR=6.9
Pod undocumented
181 0 my $self = shift;
182 0 my $ldap_operation = shift;
183 0 die "LDAP error " . $ldap_operation->code . ": " . $ldap_operation->error;
184 }
185  
186 1 1;
187  
188 =pod
189  
190 =head1 NAME
191  
192 Apache::Session::Store::LDAP - Use LDAP to store persistent objects
193  
194 =head1 SYNOPSIS
195  
196 use Apache::Session::Store::LDAP;
197  
198 my $store = new Apache::Session::Store::LDAP;
199  
200 $store->insert($ref);
201 $store->update($ref);
202 $store->materialize($ref);
203 $store->remove($ref);
204  
205 =head1 DESCRIPTION
206  
207 This module fulfills the storage interface of Apache::Session. The serialized
208 objects are stored in an LDAP directory file using the Net::LDAP Perl module.
209  
210 =head1 OPTIONS
211  
212 This module requires one argument in the usual Apache::Session style. The
213 keys ldapServer, ldapBase, ldapBindDN, ldapBindPassword are required. The keys
214 ldapPort, ldapObjectClass, ldapAttributeId, ldapAttributeContent are optional.
215 Example:
216  
217 tie %s, 'Apache::Session::LDAP', undef,
218 {
219 ldapServer => 'localhost',
220 ldapBase => 'dc=example,dc=com',
221 ldapBindDN => 'cn=admin,dc=example,dc=com',
222 ldapBindPassword => 'pass',
223 ldapObjectClass => 'applicationProcess',
224 ldapAttributeId => 'cn',
225 ldapAttributeContent => 'description',
226 };
227  
228 =head1 AUTHOR
229  
230 Xavier Guimard, E<lt>guimard@E<gt>
231  
232 =head1 COPYRIGHT AND LICENSE
233  
234 Copyright (C) 2009, 2012 by Xavier Guimard
235 Copyright (C) 2014, 2015 by Clement Oudot
236  
237 This library is free software; you can redistribute it and/or modify
238 it under the same terms as Perl itself, either Perl version 5.10.0 or,
239 at your option, any later version of Perl 5 you may have available.
240  
241 =head1 SEE ALSO
242  
243 L<Apache::Session>
244  
245 =cut
↑ Summary LDAP.pm »