#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ldap.h>
#include <ldapx.h>
#if defined(N_PLAT_NLM) && defined(LIBC)
#include <screen.h>
#endif
static char usage[] =
"\n Usage: getbinddn <host name> <port number> <login dn> <password>\n"
"\n Example: getbinddn acme.com 389 cn=admin,o=acme secret\n";
int main(int argc, char **argv)
{
int rc, version, ldapPort;
char *ldapHost;
char *loginDN;
char *password;
char *bindDN = NULL;
char *msg;
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 LDAP session initialization failed\n");
return( 1 );
}
printf ( "\n LDAP session initialized\n");
rc = ldap_simple_bind_s( ld, loginDN, password );
if (rc != LDAP_SUCCESS ) {
printf("ldap_simple_bind_s: %s\n", ldap_err2string( rc ));
ldap_unbind_s ( ld );
return( 1 );
}
printf("\n Bind successful\n");
printf( " Calling ldap_get_bind_dn...\n" );
rc = ldap_get_bind_dn( ld,
&bindDN);
if ( rc != LDAP_SUCCESS )
{
if(ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &msg ) ==
LDAP_OPT_SUCCESS ) {
if ( (msg != NULL) && (strlen(msg) != 0) )
{
printf("\n LDAP server message: %s", msg);
ldap_memfree(msg);
}
}
printf(" LDAP error message: ldap_get_bind_dn:"
" %s (%d)\n", ldap_err2string( rc ), rc);
printf("\n ldap_get_bind_dn failed\n\n");
ldap_unbind_s( ld );
return( 1 );
}
printf("\n ldap_get_bind_dn succeeded\n");
printf("\n bind dn: %s\n\n", bindDN);
ldapx_memfree( bindDN );
ldap_unbind_s( ld );
return 0;
}