Mozilla LDAP SDK Programmer's Guide/Getting Started With LDAP C SDK

From MozillaWiki
Jump to: navigation, search

This section shows how to create a first client application.

Sample Directory Client Code

The following sample source code is for a command-line program that retrieves the full name, last name, email address, and telephone number of Barbara Jensen.

#include <stdio.h>
#include "ldap.h"

/* Adjust these setting for your own LDAP server */
#define HOSTNAME "localhost"
#define FIND_DN "uid=bjensen,ou=People,dc=example,dc=com"

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. */
  /* To get the handle on an IPv6 network, use prldap_init() instead. */
  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 LDAP C SDK Client Applications

The method used to compile the source code depends on the operating system on which you run the application. The following sections include instructions for compiling on UNIX and Windows systems.

Compiling Programs on UNIX Systems

The LDAP C SDK examples/ directory contains a UNIX Makefile. You can modify the Makefile to compile the sample by adjusting the flags in the file. The Makefile assumes that the LDAP C SDK header files are located in the ../include/ directory.

Compiling Programs on Windows Systems

  • If you are using Microsoft development tools, create a new project workspace for a console application. Then add the source file to the project.
  • Set your options to include lib\ as one of the directories for library files, and include\ as one of the directories for include files.
  • Link to nsldap32v60.lib, the LDAP API import library for Windows.

Running the Client

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

Running Programs on UNIX Systems

If you have linked the client on a UNIX platform, the client requires the SDK library file. Make sure to set your LD_LIBRARY_PATH to locate the library file and its dependencies.

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

Running Programs on Windows Systems

If you have linked the client with the nsldap32v60.lib library on a Windows system, copy the LDAP C SDK DLL files to one of the following directories:

  • The directory where the application was loaded
  • The current directory
  • The Windows system directory, such as winnt\system32\
  • The Windows directory
  • The directories listed in the PATH environment variable