| 1 | # =========================================================================== | ||||||||||||||||||||
| 2 | # Net::LDAP::Server | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # LDAP server side protocol handling | ||||||||||||||||||||
| 5 | # | ||||||||||||||||||||
| 6 | # Alessandro Ranellucci <aar@cpan.org> | ||||||||||||||||||||
| 7 | # Hans Klunder <hans.klunder@bigfoot.com> | ||||||||||||||||||||
| 8 | # Copyright (c) 2005-2007. | ||||||||||||||||||||
| 9 | # | ||||||||||||||||||||
| 10 | # See below for documentation. | ||||||||||||||||||||
| 11 | # | ||||||||||||||||||||
| 12 | package Net::LDAP::Server; | ||||||||||||||||||||
| 13 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 2 | ▶ | use Convert::ASN1 qw(asn_read); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 2 | ▶ | use Net::LDAP::ASN qw(LDAPRequest LDAPResponse); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 2 | ▶ | use Net::LDAP::Constant qw(LDAP_OPERATIONS_ERROR LDAP_UNWILLING_TO_PERFORM); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 2 | ▶ | use Net::LDAP::Entry; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 2 | our $VERSION = '0.43'; | |||||||||||||||||||
| 23 | 2 | ▶ | use fields qw(in out); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 2 | our %respTypes=( | |||||||||||||||||||
| 26 | 'bindRequest' => 'bindResponse', | ||||||||||||||||||||
| 27 | 'unbindRequest' => '', | ||||||||||||||||||||
| 28 | 'searchRequest' => 'searchResDone', | ||||||||||||||||||||
| 29 | 'modifyRequest' => 'modifyResponse', | ||||||||||||||||||||
| 30 | 'addRequest' => 'addResponse', | ||||||||||||||||||||
| 31 | 'delRequest' => 'delResponse', | ||||||||||||||||||||
| 32 | 'modDNRequest' => 'modDNResponse', | ||||||||||||||||||||
| 33 | 'compareRequest' => 'compareResponse', | ||||||||||||||||||||
| 34 | 'extendedReq' => 'extendedResp', | ||||||||||||||||||||
| 35 | 'abandonRequest' => '' | ||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||
| 37 | 2 | our %functions=( | |||||||||||||||||||
| 38 | 'bindRequest' => 'bind', | ||||||||||||||||||||
| 39 | 'unbindRequest' => 'unbind', | ||||||||||||||||||||
| 40 | 'searchRequest' => 'search', | ||||||||||||||||||||
| 41 | 'modifyRequest' => 'modify', | ||||||||||||||||||||
| 42 | 'addRequest' => 'add', | ||||||||||||||||||||
| 43 | 'delRequest' => 'delete', | ||||||||||||||||||||
| 44 | 'modDNRequest' => 'modifyDN', | ||||||||||||||||||||
| 45 | 'compareRequest' => 'compare', | ||||||||||||||||||||
| 46 | 'extendedReq' => 'extended', | ||||||||||||||||||||
| 47 | 'abandonRequest' => 'abandon' | ||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||
| 49 | 2 | our @reqTypes = keys %respTypes; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 52 | 0 | my ($proto, $input, $output) = @_; | |||||||||||||||||||
| 53 | 0 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 54 | 0 | my $self = fields::new($class); | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | #print STDERR Dumper($input); | ||||||||||||||||||||
| 57 | #print STDERR Dumper($output); | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 0 | $self->{in} = $input; | |||||||||||||||||||
| 60 | 0 | ▶ | $self->{out} = $output || $input; | ||||||||||||||||||
|
Condition0%0/3
$output || $input
MC/DC0%0/2
$output || $input
$output
$input
Truth table0%0/3
A || B
A= $outputB= $input
| |||||||||||||||||||||
| 61 | 0 | return $self; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 0 | ▶ | sub handle { | ||||||||||||||||||
|
Subroutine
handle: not called CC=14 SCAR=53.5
Pod
documented
| |||||||||||||||||||||
| 65 | 0 | my Net::LDAP::Server $self = shift; | |||||||||||||||||||
| 66 | 0 | my $in = $self->{in}; | |||||||||||||||||||
| 67 | 0 | my $out = $self->{out}; | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | #print STDERR Dumper($in); | ||||||||||||||||||||
| 70 | #print STDERR Dumper($out); | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 0 | asn_read($in, my $pdu); | |||||||||||||||||||
| 73 | #print '-' x 80,"\n"; | ||||||||||||||||||||
| 74 | #print "Received:\n"; | ||||||||||||||||||||
| 75 | #Convert::ASN1::asn_dump(\*STDOUT,$pdu); | ||||||||||||||||||||
| 76 | 0 | my $request = $LDAPRequest->decode($pdu); | |||||||||||||||||||
| 77 | 0 | ▶ | my $mid = $request->{'messageID'} | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | or return 1; | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | #print "messageID: $mid\n"; | ||||||||||||||||||||
| 81 | #print Dumper($request); | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 0 | my $reqType; | |||||||||||||||||||
| 84 | 0 | foreach my $type (@reqTypes) { | |||||||||||||||||||
| 85 | 0 | ▶ | if (defined $request->{$type}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 0 | $reqType = $type; | |||||||||||||||||||
| 87 | 0 | last; | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | 0 | ▶ | return 1 if !exists $respTypes{$reqType}; # unknown request type: let's hangup | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 0 | my $respType = $respTypes{$reqType}; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | # here we can do something with the request of type $reqType | ||||||||||||||||||||
| 94 | 0 | my $reqData = $request->{$reqType}; | |||||||||||||||||||
| 95 | 0 | my $method = $functions{$reqType}; | |||||||||||||||||||
| 96 | 0 | my $result; | |||||||||||||||||||
| 97 | 0 | ▶ | if ($self->can($method)){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 0 | ▶ | if ($method eq 'search') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 0 | my @entries; | |||||||||||||||||||
| 100 | 0 | eval { ($result,@entries) = $self->search($reqData, $request) }; | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 0 | foreach my $entry (@entries) { | |||||||||||||||||||
| 103 | 0 | my $data; | |||||||||||||||||||
| 104 | # default is to return a searchResEntry | ||||||||||||||||||||
| 105 | 0 | my $sResType = 'searchResEntry'; | |||||||||||||||||||
| 106 | 0 | ▶ | if (ref $entry eq 'Net::LDAP::Entry') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 107 | 0 | $data = $entry->{'asn'}; | |||||||||||||||||||
| 108 | } elsif (ref $entry eq 'Net::LDAP::Reference') { | ||||||||||||||||||||
| 109 | 0 | $data = $entry->{'asn'}; | |||||||||||||||||||
| 110 | 0 | $sResType = 'searchResRef'; | |||||||||||||||||||
| 111 | } else{ | ||||||||||||||||||||
| 112 | 0 | $data = $entry; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 0 | my $response; | |||||||||||||||||||
| 116 | # is the full message specified? | ||||||||||||||||||||
| 117 | 0 | ▶ | if (defined $data->{'protocolOp'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | 0 | $response = $data; | |||||||||||||||||||
| 119 | 0 | $response->{'messageID'} = $mid; | |||||||||||||||||||
| 120 | } else { | ||||||||||||||||||||
| 121 | 0 | $response = { | |||||||||||||||||||
| 122 | 'messageID' => $mid, | ||||||||||||||||||||
| 123 | 'protocolOp' => { | ||||||||||||||||||||
| 124 | $sResType => $data | ||||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | }; | ||||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | 0 | my $pdu = $LDAPResponse->encode($response); | |||||||||||||||||||
| 129 | 0 | ▶ | if ($pdu) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | 0 | print $out $pdu; | |||||||||||||||||||
| 131 | } else { | ||||||||||||||||||||
| 132 | 0 | $result = undef; | |||||||||||||||||||
| 133 | 0 | last; | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | } else { | ||||||||||||||||||||
| 137 | 0 | eval { $result = $self->$method($reqData, $request) }; | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | 0 | ▶ | $result = _operations_error() unless $result; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 140 | } else { | ||||||||||||||||||||
| 141 | 0 | $result = { | |||||||||||||||||||
| 142 | 'matchedDN' => '', | ||||||||||||||||||||
| 143 | 'errorMessage' => sprintf("%s operation is not supported by %s", $method, ref $self), | ||||||||||||||||||||
| 144 | 'resultCode' => LDAP_UNWILLING_TO_PERFORM | ||||||||||||||||||||
| 145 | }; | ||||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | # and now send the result to the client | ||||||||||||||||||||
| 149 | 0 | ▶ | print $out &_encode_result($mid, $respType, $result) if $respType; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 0 | return 0; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 0 | ▶ | sub _encode_result { | ||||||||||||||||||
|
Subroutine
_encode_result: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 155 | 0 | my ($mid, $respType, $result) = @_; | |||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 0 | my $response = { | |||||||||||||||||||
| 158 | 'messageID' => $mid, | ||||||||||||||||||||
| 159 | 'protocolOp' => { | ||||||||||||||||||||
| 160 | $respType => $result | ||||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | }; | ||||||||||||||||||||
| 163 | 0 | my $pdu = $LDAPResponse->encode($response); | |||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | # if response encoding failed return the error | ||||||||||||||||||||
| 166 | 0 | ▶ | if (!$pdu) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 167 | 0 | $response->{'protocolOp'}->{$respType} = _operations_error(); | |||||||||||||||||||
| 168 | 0 | $pdu = $LDAPResponse->encode($response); | |||||||||||||||||||
| 169 | }; | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 0 | return $pdu; | |||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 0 | ▶ | sub _operations_error { | ||||||||||||||||||
|
Subroutine
_operations_error: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 175 | 0 | my $err = $@; | |||||||||||||||||||
| 176 | 0 | $err =~ s/ at .+$//; | |||||||||||||||||||
| 177 | return { | ||||||||||||||||||||
| 178 | 0 | 'matchedDN' => '', | |||||||||||||||||||
| 179 | 'errorMessage' => $err, | ||||||||||||||||||||
| 180 | 'resultCode' => LDAP_OPERATIONS_ERROR | ||||||||||||||||||||
| 181 | }; | ||||||||||||||||||||
| 182 | } | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 2 | 1; | |||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | __END__ | ||||||||||||||||||||