#include <stdio.h>
#include "ldap.h"
/* Adjust these setting for your own LDAP server */
#define HOSTNAME "localhost"
#define PORT_NUMBER LDAP_PORT
#define FIND_DN "uid=bjensen, ou=People, o=Airius.com"
int
main( int argc, char **argv )
{LDAP *ld;
LDAPMessage *result, *e;
BerElement *ber;
char *a;
char **vals;
int i, rc;
/* Get a handle to an LDAP connection. */
if ( (ld = ldap_init( HOSTNAME, PORT_NUMBER )) == NULL ) {perror( "ldap_init" );
return( 1 );
}
/* Bind anonymously to the LDAP server. */
rc = ldap_simple_bind_s( ld, NULL, NULL );
if ( rc != LDAP_SUCCESS ) {fprintf(stderr, "ldap_simple_bind_s: %s\n", ldap_err2string(rc));
return( 1 );
}
/* Search for the entry. */
if ( ( rc = ldap_search_ext_s( ld, FIND_DN, LDAP_SCOPE_BASE,
"(objectclass=*)", NULL, 0, NULL, NULL, LDAP_NO_LIMIT,
LDAP_NO_LIMIT, &result ) ) != LDAP_SUCCESS ) {fprintf(stderr, "ldap_search_ext_s: %s\n", ldap_err2string(rc));
return( 1 );
}
/* Since we are doing a base search, there should be only
one matching entry. */
e = ldap_first_entry( ld, result );
if ( e != NULL ) {printf( "\nFound %s:\n\n", FIND_DN );
/* Iterate through each attribute in the entry. */
for ( a = ldap_first_attribute( ld, e, &ber );
a != NULL; a = ldap_next_attribute( ld, e, ber ) ) {/* For each attribute, print the attribute name and values. */
if ((vals = ldap_get_values( ld, e, a)) != NULL ) { for ( i = 0; vals[i] != NULL; i++ ) {printf( "%s: %s\n", a, vals[i] );
}
ldap_value_free( vals );
}
ldap_memfree( a );
}
if ( ber != NULL ) {ber_free( ber, 0 );
}
}
ldap_msgfree( result );
ldap_unbind( ld );
return( 0 );
}
examples directory contains a sample UNIX makefile. You can modify this to compile the example in this section. (Adjust the flags specified in this file as needed.) The examples assume that the LDAP API header files are in the ../include directory and
For example, you can use the following Solaris makefile for this example:
#
# Makefile for the example in this chapter.
#
EXTRACFLAGS=
EXTRALDFLAGS=-lsocket -lnsl
LDAPLIB=ldap30
INCDIR=../include
LIBDIR=../lib
LIBS=-L$(LIBDIR) -l$(LDAPLIB) $(EXTRALDFLAGS)
OPTFLAGS=-g
CFLAGS=$(OPTFLAGS) -I$(INCDIR) $(EXTRACFLAGS)
CC=cc
PROGS=rdentry
all: $(PROGS)
rdentry: rdentry.o
$(CC) -o rdentry rdentry.o $(LIBS)
clean:
/bin/rm -f $(PROGS) *.o a.out coreIf you are compiling this on Windows NT, set up a makefile for this application. Make sure to do the following:
lib as one of the directories containing library files and include as one of the directories containing include files. Airius.ldif file is imported in the database.
If you have compiled and linked the client on a UNIX platform, make sure to set your LD_LIBRARY_PATH variable to the location of the libldap30.so file. (As an alternative, when linking the file, specify the flag that identifies the library directories that the run-time linker should search. For example, on Solaris, use the -R flag to specify the location of the libldap30.so file.)
If you have linked the client with the nsldap32v31b1.lib or nsldap32v31b1bc.lib import libraries on Windows NT, make sure to copy the nsldap32v31b1.dll dynamic link library file to one of the following directories:
windows\system. In Windows NT, this directory is typically winnt\system32.) winnt\system.) PATH environment variable. Last Updated: 10/01/98 17:02:22