Getting Started With &DirectorySDKForC; C SDK quick start This chapter shows how to start using &DirectorySDKForC;and covers the following topics: Sample Directory Client Code Compiling Directory SDK for C Client Applications Running the Client Sample Directory Client Code example programs C SDK directory entry retrieval 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. Retrieving a Directory 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. */ /* 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 &DirectorySDKForC; Client Applications C SDK compiling 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 &DirectorySDKForC; 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 &DirectorySDKForC; header files are located in the ../include/ directory. Compiling Programs on Windows Systems If you are compiling the sample client on Windows, set up the build framework for this application. Make sure to do the following: 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 Example 5–1, 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 libldap60.so 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 option to specify the location of the libldap60.so file. Running Programs on Windows Systems If you have linked the client with the nsldap32v60.lib library on a Windows system, copy the &DirectorySDKForC; 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