> developer > dnu > courses > Web Services

LDAP Libraries for C

Reader Rating    from ratings rate this article
View an eBook Version of this course - LARGE FILE! Send this page to a friend

This section provides example sample code for how to enable Authentication against a Directory Services using LDAP Libraries for C


// Sample code file: bind.c
// Warning: This code has been marked up for HTML

/* $Novell: /ldap/src/cldap/samples/bind.c,v 1.3 2000/08/21 22:59:04 vtag Exp $ */
/*********************************************************************

Copyright 1999, 2000 Novell, Inc. All Rights Reserved.

With respect to this file, Novell hereby grants to Developer a
royalty-free, non-exclusive license to include this sample code
and derivative binaries in its product. Novell grants to Developer
worldwide distribution rights to market, distribute or sell this
sample code file and derivative binaries as a component of
Developer's product(s). Novell shall have no obligations to
Developer or Developer's customers with respect to this code.

DISCLAIMER:

Novell disclaims and excludes any and all express, implied, and
statutory warranties, including, without limitation, warranties
of good title, warranties against infringement, and the implied
warranties of merchantability and fitness for a particular purpose.
Novell does not warrant that the software will satisfy customer's
requirements or that the licensed works are without defect or error
or that the operation of the software will be uninterrupted.
Novell makes no warranties respecting any technical services or
support tools provided under the agreement, and disclaims all other
warranties, including the implied warranties of merchantability and
fitness for a particular purpose.

***********************************************************************
bind.c
***********************************************************************
Description: The bind.c sample demonstrates both an anonymous and
simple LDAP v3 bind to an LDAP server.
**********************************************************************/

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

static char usage[] =
"\nUsage: bind <host name> <port number> <login dn> <password>"
"\nExample: bind Acme.com cn=admin,o=Acme secret\n";

int main( int argc, char **argv)
{
int rc, ldapPort, version;
char *ldapHost, *loginDN, *password;
LDAP *ld;

if (argc != 5)
{
printf("%s", usage);
return (1);
}

ldapHost = argv[1];
ldapPort = atoi(argv[2]);
loginDN = argv[3];
password = argv[4];

       /*
* Set LDAP version to 3.
* Using a NULL session handle sets the global options.
* All subsequent sessions in this process will be version 3.
*/
version = LDAP_VERSION3;
ldap_set_option( NULL, LDAP_OPT_PROTOCOL_VERSION, &version);

       /*
* Initialize the LDAP session
*/
if (( ld = ldap_init( ldapHost, ldapPort )) == NULL)
{
printf ( "\n\tLDAP session initialization failed\n");
return( 1 );
}
printf ( "\n\tLDAP session initialized\n");

/*
* Bind to the server anonymously
*/
if(( rc = ldap_simple_bind_s( ld, NULL, NULL )) != LDAP_SUCCESS )
{
printf("\n\tldap_simple_bind_s: %s\n", ldap_err2string( rc ));
ldap_unbind_s( ld );
return ( 1 );
}
printf("\n\tAnonymous bind successful\n");

ldap_unbind_s( ld );


       /*
* Initialize a new LDAP session
*/
if (( ld = ldap_init( ldapHost, ldapPort )) == NULL)
{
printf ( "\n\tLDAP session initialization failed\n");
return( 1 );
}
printf ( "\n\tNew LDAP session initialized\n");

       /*
* Simple bind to the server
*/
if ((rc = ldap_simple_bind_s( ld, loginDN, password )) != LDAP_SUCCESS )
{
printf("\n\tldap_simple_bind_s: %s\n", ldap_err2string( rc ));
ldap_unbind_s( ld );
return ( 1 );
}
printf("\n\tBind and authentication to the server successful\n");

ldap_unbind_s( ld );

return ( 0 );
}



Previous blank Table of Contents blank Next