#include <stdio.h>
#include <stdlib.h>
#include <ldap.h>
#if defined(N_PLAT_NLM) && defined(LIBC)
#include <screen.h>
#endif
static char usage[] =
"\nUsage: bind <host name> <port number> <login dn> <password>"
"\nExample: bind Acme.com 389 cn=admin,o=Acme secret\n";
int main( int argc, char **argv)
{
int rc, ldapPort, version;
char *ldapHost, *loginDN, *password;
LDAP *ld;
struct timeval timeOut = {10,0};
#if defined(N_PLAT_NLM) && defined(LIBC)
setscreenmode(SCR_NO_MODE);
#endif
if (argc != 5)
{
printf("%s", usage);
return (1);
}
ldapHost = argv[1];
ldapPort = atoi(argv[2]);
loginDN = argv[3];
password = argv[4];
version = LDAP_VERSION3;
ldap_set_option( NULL, LDAP_OPT_PROTOCOL_VERSION, &version);
ldap_set_option( NULL, LDAP_OPT_NETWORK_TIMEOUT, &timeOut);
if (( ld = ldap_init( ldapHost, ldapPort )) == NULL)
{
printf ( "\n\tLDAP session initialization failed\n");
return( 1 );
}
printf ( "\n\tLDAP session initialized\n");
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 );
if (( ld = ldap_init( ldapHost, ldapPort )) == NULL)
{
printf ( "\n\tLDAP session initialization failed\n");
return( 1 );
}
printf ( "\n\tNew LDAP session initialized\n");
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 );
}