#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ldap.h>
#if defined(N_PLAT_NLM) && defined(LIBC)
#include <screen.h>
#endif
static char usage[] =
"\n Usage: cmpattrs <host name> <port number> <login dn> <password>"
" <compare dn>"
"\n Example: cmpattrs acme.com 389 cn=admin,o=acme secret"
"\n\t cn=james,ou=sales,o=acme\n";
void printout( int rc, char *dn, char *attr, char *vals );
int main( int argc, char **argv)
{
int version, ldapPort, rc;
char *ldapHost, *loginDN, *password;
char *compareDN, *compareAttribute, *compareValue;
LDAP *ld;
struct berval bvalue;
struct timeval timeOut = {10,0};
#if defined(N_PLAT_NLM) && defined(LIBC)
setscreenmode(SCR_NO_MODE);
#endif
if (argc != 6)
{
printf("%s", usage);
exit ( 0 );
}
ldapHost = argv[1];
ldapPort = atoi(argv[2]);
loginDN = argv[3];
password = argv[4];
compareDN = 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\tLDAP session initialization failed\n");
return( 1 );
}
printf("\n\tLDAP 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\tBind successful\n");
compareAttribute = "objectclass";
compareValue = "inetOrgPerson";
bvalue.bv_val = compareValue;
bvalue.bv_len = strlen( compareValue );
rc = ldap_compare_ext_s( ld,
compareDN,
compareAttribute,
&bvalue,
NULL,
NULL);
printout( rc, compareDN, compareAttribute, compareValue );
compareAttribute = "cn";
compareValue = "Mary";
bvalue.bv_val = compareValue;
bvalue.bv_len = strlen( compareValue );
rc = ldap_compare_ext_s( ld,
compareDN,
compareAttribute,
&bvalue,
NULL,
NULL);
printout( rc, compareDN, compareAttribute, compareValue );
ldap_unbind_s( ld );
return( 0 );
}
void printout( int rc, char *dn, char *attr, char *vals )
{
switch ( rc )
{
case LDAP_COMPARE_TRUE:
printf("\n\t%s has the value \"%s\"\n\t"
"in the \"%s\" attribute.\n", dn, vals, attr );
break;
case LDAP_COMPARE_FALSE:
printf("\n\t%s does NOT have the value \"%s\"\n\t"
"in the \"%s\" attribute.\n", dn, vals, attr );
break;
default:
printf("ldap_compare_ext_s: %s\n", ldap_err2string( rc ) );
break;
}
}