Subject: | Add support for baseObject DN and scope in searches |
This module would be a lot more useful if it supported the baseObject DN and scope in a search request, especially for using it as a back-end server for testing LDAP clients against. These fields are present in the request passed from Net::LDAP::Server to the search() function, but it currently ignores them and applies the search filter to the entire content of the data store.
This is really an enhancement request, but it could be argued that the baseObject and scope are inherent parts of the search request and so ignoring them as it does now could be seen as a bug.
Adding support for these in the search() function is not difficult and I have attached a simple patch that makes the necessary additions.
Subject: | add-srch-scope.patch |
diff -Naur Net-LDAP-SimpleServer-0.0.17-1/lib/Net/LDAP/SimpleServer/ProtocolHandler.pm Net-LDAP-SimpleServer-0.0.17/lib/Net/LDAP/SimpleServer/ProtocolHandler.pm
--- Net-LDAP-SimpleServer-0.0.17-1/lib/Net/LDAP/SimpleServer/ProtocolHandler.pm 2015-01-06 18:04:15.349728581 -0800
+++ Net-LDAP-SimpleServer-0.0.17/lib/Net/LDAP/SimpleServer/ProtocolHandler.pm 2015-01-06 18:02:11.966306605 -0800
@@ -118,17 +118,55 @@
return [ grep { $f->match($_) } @{$elems} ];
}
+sub _in_scope {
+ my ($dn, $basedn, $scope) = @_;
+
+ my $in_scope = 0;
+ if ($scope eq 'baseObj') {
+ $in_scope = ($dn eq $basedn);
+ } elsif ($scope eq 'oneLevel') {
+ if ($dn =~ m/([^,]*),(.*)$/) {
+ my $locn = $2;
+ $in_scope = ($locn eq $basedn);
+ }
+ }
+ elsif ($scope eq 'subTree') {
+ my $tail_len = length($basedn) * -1;
+ $in_scope = (substr($dn, $tail_len) eq $basedn);
+ }
+
+ return $in_scope;
+}
+
+sub _elem_in_scope {
+ my ($elem, $uc_basedn, $scope) = @_;
+
+ return _in_scope(uc($elem->{asn}->{'objectName'}), $uc_basedn, $scope);
+}
+
sub search {
my ( $self, $request ) = @_;
my $list = $self->{store}->list;
- #my $basedn = $request->{baseObject};
+ my @scopes = qw(baseObj oneLevel subTree);
+
+ my $basedn = $request->{baseObject};
+ my $scope = $scopes[ (defined $request->{scope} && $request->{scope} <= 2) ? $request->{scope} : 2 ];
#print STDERR '=' x 50 . "\n";
#print STDERR Dumper($request);
#print STDERR Dumper($list);
+ if (defined($basedn))
+ {
+ my $uc_basedn = uc($basedn);
+ my @sublist = grep { _elem_in_scope($_, $uc_basedn, $scope) } @{$list};
+
+ #print STDERR Dumper(@sublist);
+ $list = [ @sublist ];
+ }
+
my $res = _match( $request->{filter}, $list );
#print STDERR Dumper($res);
@@ -190,6 +228,9 @@
Performs a search in the data store.
+The search filter, baseObject and scope are supported, with other search fields
+currently being ignored.
+
=head1 SEE ALSO
Please see those modules/websites for more information related to this module.