lib/Net/LDAP/SimpleServer/ProtocolHandler.pm

filter: Statementstmt 98.9% 97 / 98 Branchbran 75.0% 18 / 24 Conditioncond 73.3% 11 / 15 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 21 / 21 Podpod 100% 4 / 4 totaltotal 90.5% 154 / 170 CC 20 SCAR 14.7
CC
20
Cov
92%
bind
8.2
new
8.2
search
4.1
CRAP 4.3
« blib/lib/Net/LDAP/SimpleServer.pm ↑ Summary lib/Net/LDAP/SimpleServer/LDIFStore.pm »
1 package Net::LDAP::SimpleServer::ProtocolHandler;
2  
3 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: LDAP protocol handler used with Net::LDAP::SimpleServer
7  
8 27 our $VERSION = '0.0.19'; # VERSION
9  
10 27 ▶use Net::LDAP::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 27 ▶use base 'Net::LDAP::Server';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 ▶use fields qw(store root_dn root_pw allow_anon);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 27 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 27 ▶use Net::LDAP::LDIF;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 27 ▶use Net::LDAP::Util qw{canonical_dn};
Subroutine BEGIN: called CC=1 SCAR=0.0
17 27 ▶use Net::LDAP::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 27 ▶use Net::LDAP::FilterMatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 27 use Net::LDAP::Constant qw/
21 LDAP_SUCCESS LDAP_INVALID_CREDENTIALS LDAP_AUTH_METHOD_NOT_SUPPORTED
22 27 ▶ LDAP_INVALID_SYNTAX LDAP_NO_SUCH_OBJECT/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 27 ▶use Net::LDAP::SimpleServer::LDIFStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 27 ▶use Net::LDAP::SimpleServer::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 27 ▶use Scalar::Util qw{reftype};
Subroutine BEGIN: called CC=1 SCAR=0.0
28 27 ▶use UNIVERSAL::isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 sub _make_result {
31 16 ▶ my $code = shift;
Subroutine _make_result: called CC=3 SCAR=11.0
32 16 ▶ my $dn = shift // '';
Condition100%2/2
shift() // ""
l!l
115
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
33 16 ▶ my $msg = shift // '';
Condition100%2/2
shift() // ""
l!l
115
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
34  
35 return {
36 16 matchedDN => $dn,
37 errorMessage => $msg,
38 resultCode => $code,
39 };
40 }
41  
42 sub new {
43 14 ▶ my $class = shift;
Subroutine new: called CC=8 SCAR=21.0
Pod documented
44 14 ▶ my $params = shift || croak 'Must pass parameters!';
Condition66%2/3
shift() || croak('Must pass parameters!')
l!l&&r!l&&!r
1310
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
45  
46 13 ▶ croak 'Parameter must be a HASHREF' unless reftype($params) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($params) eq "HASH" 2 11 13
47 11 for my $p (qw/store root_dn sock/) {
48 27 ▶ croak 'Must pass option {' . $p . '}' unless exists $params->{$p};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$p} 4 23 27
49 }
50 croak 'Not a LDIFStore'
51 7 ▶ unless $params->{store}->isa('Net::LDAP::SimpleServer::LDIFStore');
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"store"}->isa("Net::LDAP::SimpleServer::LDIFStore") 0 7 7
52  
53 7 ▶ croak 'Option {root_dn} can not be empty' unless $params->{root_dn};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"root_dn"} 0 7 7
54 croak 'Invalid root DN'
55 7 ▶ unless my $canon_dn = canonical_dn( $params->{root_dn} );
Branch: 1 branch
BranchTrueFalseTotal
unless my $canon_dn = &canonical_dn($params->{"root_dn"}) 0 7 7
56  
57 7 my $self = $class->SUPER::new( $params->{sock} );
58 7 $self->{store} = $params->{store};
59 7 $self->{root_dn} = $canon_dn;
60 7 $self->{root_pw} = $params->{root_pw};
61 7 $self->{allow_anon} = $params->{allow_anon};
62 7 chomp( $self->{root_pw} );
63  
64 7 return $self;
65 }
66  
67 sub unbind {
68 6 ▶ my $self = shift;
Subroutine unbind: called CC=1 SCAR=0.0
Pod documented
69  
70 6 $self->{store} = undef;
71 6 $self->{root_dn} = undef;
72 6 $self->{root_pw} = undef;
73  
74 6 return _make_result(LDAP_SUCCESS);
75 }
76  
77 sub bind {
78 ## no critic (ProhibitBuiltinHomonyms)
79 6 ▶ my ( $self, $request ) = @_;
Subroutine bind: called CC=8 SCAR=21.1
Pod documented
80  
81 6 my $OK = _make_result(LDAP_SUCCESS);
82  
83 # anonymous bind
84 6 ▶ if ( not $request->{name}
Branch: 1 branch
BranchTrueFalseTotal
if (not $request->{"name"} and exists $request->{"authentication"}{"simple"} and $self->{"allow_anon"}) 2 4 6
Condition66%4/6
not $request->{"name"} and exists $request->{"authentication"}{"simple"} and $self->{"allow_anon"}
!ll&&!rl&&r
402
not $request->{"name"} and exists $request->{"authentication"}{"simple"}
!ll&&!rl&&r
402
MC/DC33%1/3
not $request->{"name"} and exists $request->{"authentication"}{"simple"} and $self->{"allow_anon"}
not $request->{"name"} exists $request->{"authentication"}{"simple"} $self->{"allow_anon"}
Truth table50%2/4
not(A) and B and C
A= not $request->{"name"}B= exists $request->{"authentication"}{"simple"}C= $self->{"allow_anon"}
ABCresult
0XX0
10X0
1100
1111
85 and exists $request->{authentication}->{simple}
86 and $self->{allow_anon} )
87 {
88 2 return $OK;
89 }
90  
91 # As of now, accepts only simple authentication
92 return _make_result(LDAP_AUTH_METHOD_NOT_SUPPORTED)
93 4 ▶ unless exists $request->{authentication}->{simple};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $request->{"authentication"}{"simple"} 1 3 4
94  
95 return _make_result(LDAP_INVALID_CREDENTIALS)
96 3 ▶ unless my $binddn = canonical_dn( $request->{name} );
Branch: 1 branch
BranchTrueFalseTotal
unless my $binddn = &canonical_dn($request->{"name"}) 0 3 3
97  
98 return _make_result(LDAP_INVALID_CREDENTIALS)
99 3 ▶ unless uc($binddn) eq uc( $self->{root_dn} );
Branch: 1 branch
BranchTrueFalseTotal
unless uc $binddn eq uc $self->{"root_dn"} 0 3 3
100  
101 3 my $bindpw = $request->{authentication}->{simple};
102 3 chomp($bindpw);
103  
104 return _make_result(LDAP_INVALID_CREDENTIALS)
105 3 ▶ unless $bindpw eq $self->{root_pw};
Branch: 1 branch
BranchTrueFalseTotal
unless $bindpw eq $self->{"root_pw"} 1 2 3
106  
107 2 return $OK;
108 }
109  
110 sub _match {
111 1 ▶ my ( $filter_spec, $elems ) = @_;
Subroutine _match: called CC=1 SCAR=0.0
112  
113 1 my $f = bless $filter_spec, 'Net::LDAP::Filter';
114 1 return [ grep { $f->match($_) } @{$elems} ];
115 }
116  
117 sub search {
118 2 ▶ my ( $self, $request ) = @_;
Subroutine search: called CC=4 SCAR=14.1
Pod documented
119  
120 2 my $list;
121 2 ▶ if ( defined( $request->{baseObject} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $request->{'baseObject'}) { } 2 0 2
122 2 my $basedn = canonical_dn( $request->{baseObject} );
123 2 ▶ my $scope = $request->{scope} || SCOPE_SUBTREE;
Condition50%1/2
$request->{'scope'} || 2
l!l
20
MC/DC0%0/1
$request->{'scope'} || 2
$request->{'scope'}
Truth table50%1/2
A
A= $request->{'scope'}
Aresult
11
00
124  
125 2 $list = $self->{store}->list_with_dn_scope( $basedn, $scope );
126 2 ▶ return _make_result( LDAP_NO_SUCH_OBJECT, '',
Branch: 1 branch
BranchTrueFalseTotal
unless defined $list 1 1 2
127 'Cannot find BaseDN "' . $basedn . '"' )
128 unless defined($list);
129 }
130 else {
131 0 $list = $self->{store}->list();
132 }
133  
134 1 my $match = _match( $request->{filter}, $list );
135 1 return ( _make_result(LDAP_SUCCESS), @{$match} );
136 }
137  
138 27 1; # Magic true value required at end of module
139  
140 __END__
« blib/lib/Net/LDAP/SimpleServer.pm ↑ Summary lib/Net/LDAP/SimpleServer/LDIFStore.pm »