Sun logo      Previous      Contents      Index      Next     

Sun Java System LDAP SDK for C Programming Guide

Chapter 2
Sample Client

The Sun™ Java System LDAP SDK for C provides a simple example of a Lightweight Directory Access Protocol (LDAP) client. This chapter explains the details. It contains the following sections:


The Source Code

Code Example 2-1 is the source code for a command-line program that retrieves the full name, last name, email address, and telephone number of Barbara Jensen.

Code Example 2-1  Source Code to Retrieve LDAP Entry 

#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,dc=example,dc=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 );

}


Compiling the Client

The method used to compile the source code depends on the operating system on which you will run the application. The following sections include instructions for compiling on the Solaris Operating System and Microsoft® Windows NT.

Compiling on Solaris

The examples directory (located in DSRK_base/lib/ldapcsdk) contains a UNIX® Makefile. You can modify the Makefile to compile the sample by adjusting the flags specified in the file. The Makefile assumes that the LDAP SDK for C header files are located in the ../include directory. Code Example 2-2 details the Makefile flags.

Code Example 2-2  Solaris Makefile for Sample Client 

#

# Makefile for the example in this chapter.

#

EXTRACFLAGS=

EXTRALDFLAGS=-lsocket -lnsl

LDAPLIB=ldap41 -lplc3 -lplds3 -lnspr3

INCDIR=../include

LIBDIR=../lib

LIBS=-L$(LIBDIR) $(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 core

Compiling on Windows NT

If you are compiling the sample client on Windows NT, set up a makefile for this application. Make sure to:


Running the Client

Before running the sample client, make sure that your LDAP server is set up with the entry the sample will attempt to find. Unless you change the source code in Code Example 2-1, the entry would be for the full name, last name, email address, and telephone number of Barbara Jensen.

Running on UNIX

If you have compiled and linked the client on a UNIX platform, make sure to set your LD_LIBRARY_PATH environment variable to the location of the libldap41.so, plc3, plds3, and nspr3 library files.


Note

As an alternative, when linking the file, specify the option that identifies the library directories for which the runtime linker should search. For example, on Solaris, use the -R to specify the location of the libldap41.so file.


Running on Windows NT

If you have linked the client with the nsldap32v41.lib import library on Windows NT, make sure to copy the nsldap32v41.dll file to one of the following directories:



Previous      Contents      Index      Next     


Copyright 2004 Sun Microsystems, Inc. All rights reserved.