#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ldap.h>
#include <ldapx.h>
#if defined(N_PLAT_NLM) && defined(LIBC)
#include <screen.h>
#endif
void printFilter(char*);
static char usage[] =
"\n Usage: getReplFilter <host name> <port number> <login dn>"
"\n <password> <server DN>\n"
"\n Example: getReplFilter acme.com 389 cn=admin,o=acme secret "
"\n cn=myServerName,o=acme\n\n";
int main(int argc, char **argv)
{
int rc, version, ldapPort;
char *ldapHost, *loginDN, *password, *serverDN, *filter, *msg;
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);
if (( ld = ldap_init( ldapHost, ldapPort )) == NULL) {
printf ( "\n LDAP session initialization failed");
return (1);
}
printf ("\n LDAP session initialized");
rc = ldap_simple_bind_s( ld, loginDN, password );
if (rc != LDAP_SUCCESS ) {
printf("\nldap_simple_bind_s: %s", ldap_err2string(rc));
ldap_unbind_s (ld);
return (1);
}
printf("\n Bind successful");
printf( "\n Calling ldap_get_replication_filter..." );
rc = ldap_get_replication_filter(
ld,
serverDN,
&filter);
if ( rc != LDAP_SUCCESS ) {
printf("\n ldap_get_replication_filter error %d: %s",
rc, ldap_err2string(rc));
if(ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &msg ) ==
LDAP_OPT_SUCCESS ) {
if (msg != NULL) {
if (strlen(msg) != 0)
printf("\n LDAP server message: %s", msg);
ldap_memfree(msg);
}
}
ldap_unbind_s(ld);
return (1);
}
printf("\n ldap_get_replication_filter succeeded\n");
if ( (strlen(filter) == 1) && (filter[0] == '$') ) {
printf("\n No filter returned.\n");
}
else {
printFilter(filter);
}
ldapx_memfree(filter);
ldap_unbind_s(ld);
return (0);
}
void printFilter(char *filter) {
size_t len = strlen(filter) - 3, i;
char *names, cName[64], aName[64], *pdest;
filter[len] = '\0';
names = (char*)malloc(len + 1);
for ( i=0; i<len-1; i++) {
if ( (filter[i] == '$') && (filter[i+1] == '$') ) {
filter[i] = ' ';
filter[i+1] = ' ';
}
}
printf("\n Filter consists of:");
do {
sscanf(filter, "%s", names);
len = strlen(names);
for ( i=0; i<len-1; i++) {
if ( names[i] == '$' )
names[i] = ' ';
}
sscanf(names, "%s", cName );
printf("\n Class name: %s", cName);
pdest = (char *) strchr(names, ' ');
if (pdest) {
++pdest;
do {
sscanf(pdest, "%s", aName);
if ( (strlen(aName)==1) && (aName[0]='*')) {
printf("\n Attribute name: "
"(All the attributes of the object class)");
}
else {
printf("\n Attribute name: %s", aName );
}
pdest = (char *) strchr(pdest, ' ');
if (pdest)
pdest++;
} while (pdest!=NULL);
}
else {
printf("\n Attribute name:"
" (no attribute name specified)");
}
pdest = (char *) strchr(filter, ' ');
if (pdest)
filter = pdest + 2;
} while (pdest);
printf("\n");
free(names);
}