1998-07-24 19:32:08 +04:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#############################################################################
|
1998-08-01 01:16:32 +04:00
|
|
|
# $Id: search.pl,v 1.3 1998-07-31 21:16:31 clayton Exp $
|
1998-07-24 19:32:08 +04:00
|
|
|
#
|
|
|
|
# The contents of this file are subject to the Mozilla Public License
|
|
|
|
# Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
# compliance with the License. You may obtain a copy of the License at
|
|
|
|
# http://www.mozilla.org/MPL/
|
|
|
|
#
|
|
|
|
# Software distributed under the License is distributed on an "AS IS"
|
|
|
|
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing rights and limitations
|
|
|
|
# under the License.
|
|
|
|
#
|
|
|
|
# The Original Code is PerlDAP. The Initial Developer of the Original
|
|
|
|
# Code is Netscape Communications Corp. and Clayton Donley. Portions
|
|
|
|
# created by Netscape are Copyright (C) Netscape Communications
|
|
|
|
# Corp., portions created by Clayton Donley are Copyright (C) Clayton
|
|
|
|
# Donley. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Contributor(s):
|
|
|
|
#
|
|
|
|
# DESCRIPTION
|
|
|
|
# Test the search capabilities of the API, similar to write.pl.
|
|
|
|
#
|
|
|
|
#############################################################################
|
|
|
|
|
1998-08-01 01:16:32 +04:00
|
|
|
use Mozilla::LDAP::API qw(:api :constant);
|
1998-07-24 19:32:08 +04:00
|
|
|
use strict;
|
|
|
|
|
|
|
|
|
1998-07-24 23:01:51 +04:00
|
|
|
my $ldap_host = "localhost";
|
|
|
|
my $BASEDN = "o=Org,c=US";
|
1998-07-24 19:32:08 +04:00
|
|
|
my $filter = $ARGV[0];
|
|
|
|
|
|
|
|
my $attrs = [];
|
|
|
|
|
|
|
|
my ($ld,$result,$count);
|
|
|
|
|
|
|
|
##
|
|
|
|
## Initialize LDAP Connection
|
|
|
|
##
|
|
|
|
|
|
|
|
if (($ld = ldap_init($ldap_host,LDAP_PORT)) == -1)
|
|
|
|
{
|
|
|
|
die "Can not open LDAP connection to $ldap_host";
|
|
|
|
}
|
|
|
|
|
|
|
|
##
|
|
|
|
## Bind as DN, PASSWORD (NULL,NULL) on LDAP connection $ld
|
|
|
|
##
|
|
|
|
|
|
|
|
if (ldap_simple_bind_s($ld,"","") != LDAP_SUCCESS)
|
|
|
|
{
|
|
|
|
ldap_perror($ld,"bind_s");
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
|
|
|
##
|
|
|
|
## ldap_search_s - Synchronous Search
|
|
|
|
##
|
|
|
|
|
|
|
|
if (ldap_search_s($ld,$BASEDN,LDAP_SCOPE_SUBTREE,$filter,$attrs,0,$result) != LDAP_SUCCESS)
|
|
|
|
{
|
|
|
|
ldap_perror($ld,"search_s");
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
|
|
|
##
|
|
|
|
## ldap_count_entries - Count Matched Entries
|
|
|
|
##
|
|
|
|
|
|
|
|
if (($count = ldap_count_entries($ld,$result)) == -1)
|
|
|
|
{
|
|
|
|
ldap_perror($ld,"count_entry");
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
|
|
|
##
|
|
|
|
## first_entry - Get First Matched Entry
|
|
|
|
## next_entry - Get Next Matched Entry
|
|
|
|
##
|
|
|
|
|
|
|
|
for (my $ent = ldap_first_entry($ld,$result); $ent; $ent = ldap_next_entry($ld,$ent))
|
|
|
|
{
|
|
|
|
|
|
|
|
##
|
|
|
|
## ldap_get_dn - Get DN for Matched Entries
|
|
|
|
##
|
|
|
|
|
|
|
|
my ($dn,$attr,@vals,$val,$ber);
|
|
|
|
if (($dn = ldap_get_dn($ld,$ent)) ne "")
|
|
|
|
{
|
|
|
|
print "dn: $dn\n";
|
|
|
|
} else {
|
|
|
|
ldap_perror($ld,"get_dn");
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($attr = ldap_first_attribute($ld,$ent,$ber); $attr; $attr = ldap_next_attribute($ld,$ent,$ber))
|
|
|
|
{
|
|
|
|
|
|
|
|
##
|
|
|
|
## ldap_get_values
|
|
|
|
##
|
|
|
|
|
|
|
|
@vals = ldap_get_values($ld,$ent,$attr);
|
|
|
|
if ($#vals >= 0)
|
|
|
|
{
|
|
|
|
foreach $val (@vals)
|
|
|
|
{
|
|
|
|
print "$attr: $val\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ldap_ber_free($ber,0);
|
|
|
|
}
|
|
|
|
ldap_msgfree($result);
|
|
|
|
|
|
|
|
##
|
|
|
|
## Unbind LDAP Connection
|
|
|
|
##
|
|
|
|
|
|
|
|
ldap_unbind($ld);
|
|
|
|
|