#!/usr/local/bin/perl  
#$Author: et $
#$Date: 1997/02/21 15:34:39 $
#$Header: /users/et/Work/SWIG/module/Version/Perldap-0.02/examples/RCS/ldapsearch.pl,v 1.2 1997/02/21 15:34:39 et Exp $
#$Id: ldapsearch.pl,v 1.2 1997/02/21 15:34:39 et Exp $
#$Locker:  $
#$Log: ldapsearch.pl,v $
#Revision 1.2  1997/02/21 15:34:39  et
#*** empty log message ***
#
#Revision 1.1  1997/02/21 15:20:47  et
#1ere Version
#
#$RCSfile: ldapsearch.pl,v $
#$Revision: 1.2 $
#$Source: /users/et/Work/SWIG/module/Version/Perldap-0.02/examples/RCS/ldapsearch.pl,v $
#$State: Exp $

require 'ldap.ph';

use ldap;

require 'ldapconfig.ph';

$res=ldap::new_LDAPMessage();
$ber=ldap::new_BerElement();
$timeout=ldap::new_struct_timeval();

@attrs=("cn","telephoneNumber");

#
# Open connection to DSA
#

# LDAPPtr  ldap_open(string, int );

$ld=ldap::ldap_open("cli58ab",LDAP_PORT ());

if ($$ld == NULL) {
    #ldap::ldap_perror( $ld, "ldap_open" );
    die "Error during ldap_open\n";
}

print "Open ok \n";

#
# Bind connection
#

#int ldap_bind_s(LDAPPtr,string, string,int );

if (ldap::ldap_bind_s($ld,"c=fr,o=edfgdf,ou=der",0,LDAP_AUTH_SIMPLE()) != LDAP_SUCCESS()) {
  ldap::ldap_perror( $ld, "ldap_bind" );
    die "Error during ldap_bind_s\n";
}

print "Bind ok \n";
#
#Search request.
#

# SEARCH ALL ATTRIBUTS : attrs=NULL
#int ldap_search(LDAPPtr,string,int ,string,array value,int );
#if (ldap::ldap_search_s($ld,'cn=Poiret Emmanuel,ou=der,o=edfgdf,c=fr',LDAP_SCOPE_SUBTREE(),"cn=*poiret*",NULL,0,$res)==-1) {
#  ldap::ldap_perror($ld,"ldap_search");
#    die "search error \n";
#}
#int ldap_search(LDAPPtr,string,int ,string,array value,int );
if (ldap::ldap_search_s($ld,'cn=Poiret Emmanuel,ou=der,o=edfgdf,c=fr',LDAP_SCOPE_SUBTREE(),"cn=*poiret*",@attrs,0,$res)==-1) {
  ldap::ldap_perror($ld,"ldap_search");
    die "search error \n";
}

print ("End of Search \n");

#
# Printing results...
#

#LDAPMessagePtr ldap_first_entry(LDAPPtr,LDAPMessagePtr);
$mess=ldap::ldap_first_entry( $ld, ldap::get_LDAPMessagePtr ($res) );

while ( $$mess !=0 ) {
    #string ldap_get_dn(LDAPPtr ,LDAPMessagePtr );
    $dn = ldap::ldap_get_dn($ld, $mess);
    print "$dn \n";
    #array value ldap_explode_dn(string,int );
    print "EX DN ",ldap::ldap_explode_dn($dn,1),"\n";
   
    #string ldap_first_attribute(LDAPPtr,LDAPMessagePtr,BerElementPtrPtr);
    $ATTR=ldap::ldap_first_attribute ($ld,$mess,$ber);
   
    while ($ATTR ne "") {
	print "ATTRIBUT : $ATTR\n";
	#array value ldap_get_values(LDAPPtr,LDAPMessagePtr,string);
	@tab=ldap::ldap_get_values ($ld,$mess,$ATTR);
	#int ldap_count_values(arry value);
	print "nombre de valeur : $#tab ",ldap::ldap_count_values(@tab),"\n";
	foreach $value (@tab) {
		print ("\t\tValeur : $value \n");
	}	
	#string ldap_next_attribute(LDAPPtr,LDAPMessagePtr,BerElementPtr);
	#BerElementPtr get_BerElementPtr(BerElementPtrPtr);
	$ATTR=ldap::ldap_next_attribute ($ld,$mess,ldap::get_BerElementPtr($ber));	   
    }
    #LDAPMessagePtr ldap_next_entry(LDAPPtr,LDAPMessagePtr);
    $mess=ldap::ldap_next_entry ($ld,$mess);
}

ldap::ldap_unbind( $ld );

