#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: listrepl <host name> <port number> <login dn> <password>"
"\n\t <serverDN>\n"
"\n Example: listrepl acme.com 389 cn=admin,o=acme secret cn=myServer,o=acme\n";
int main(int argc, char **argv)
{
int i, rc, version;
char *ldapHost;
int ldapPort;
char *loginDN;
char *password;
char *serverDN;
char *msg;
char **replicaList = NULL;
LDAP *ld;
struct timeval timeOut = {10,0};
#if defined(N_PLAT_NLM) && defined(LIBC)
setscreenmode(SCR_NO_MODE);
#endif
if ( argc != 6 )
{
printf("%s", usage);
return(1);
}
ldapHost = argv[1];
ldapPort = atoi(argv[2]);
loginDN = argv[3];
password = argv[4];
serverDN = argv[5];
version = LDAP_VERSION3;
ldap_set_option( NULL, LDAP_OPT_PROTOCOL_VERSION, &version);
ldap_set_option( NULL, LDAP_OPT_NETWORK_TIMEOUT, &timeOut);
printf( " Initializing the LDAP session handle ...\n" );
if ( (ld = ldap_init( ldapHost, ldapPort )) == NULL )
{
perror( "ldap_init" );
return( 1 );
}
printf( " Binding to the directory...\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( " Calling ldap_list_replicas...\n" );
rc = ldap_list_replicas(
ld,
serverDN,
&replicaList);
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_list_replicas:"
" %s (%d)\n", ldap_err2string( rc ), rc);
printf("\n ldap_list_replicas failed\n\n");
ldap_unbind_s( ld );
return( 1 );
}
printf("\n ldap_list_replicas succeeded\n\n");
if(replicaList)
{
printf("\n replicas existing in %s: \n", serverDN);
for ( i = 0; replicaList[i] != NULL; i++)
if ( strcmp( replicaList[i], "") == 0)
printf(" [Root]\n");
else
printf(" %s\n", replicaList[i] );
}
printf("\n\n");
ldap_value_free( replicaList );
ldap_unbind_s( ld );
return 0;
}