#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ldap.h>
#include <time.h>
#if defined(N_PLAT_NLM) && defined(LIBC)
#include <screen.h>
#endif
static char usage[] =
"\nUsage: operationalAttrs <host name> <port number> <login dn> <password>"
"\n <entryDN>"
"\nExample: operationalAttrs Acme.com 389 cn=admin,o=Acme secret"
"\n cn=James,o=Acme\n";
int printOperationalAttrs( LDAP* , char* );
void printTime( char * );
void displayEntryFlags( int );
#define DS_ALIAS_ENTRY 0x0001
#define DS_PARTITION_ROOT 0x0002
#define DS_CONTAINER_ENTRY 0x0004
#define DS_CONTAINER_ALIAS 0x0008
#define DS_MATCHES_LIST_FILTER 0x0010
#define DS_REFERENCE_ENTRY 0x0020
#define DS_40X_REFERENCE_ENTRY 0x0040
#define DS_BACKLINKED 0x0080
#define DS_NEW_ENTRY 0x0100
#define DS_TEMPORARY_REFERENCE 0x0200
#define DS_AUDITED 0x0400
#define DS_ENTRY_NOT_PRESENT 0x0800
#define DS_ENTRY_VERIFY_CTS 0x1000
#define DS_ENTRY_DAMAGED 0x2000
int main( int argc, char **argv) {
int rc, ldapPort, version;
char *ldapHost, *loginDN, *password, *testObjDN;
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];
testObjDN = 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.\n");
return( 1 );
}
printf ( "\n New LDAP session initialized.\n");
rc = ldap_simple_bind_s( ld, loginDN, password );
if ( rc == LDAP_SUCCESS ) {
printf("\n Bind successful.\n");
}
else {
printf("\n ldap_simple_bind_s: %s\n", ldap_err2string( rc ));
ldap_unbind_s ( ld );
return( 1 );
}
if ( (rc = printOperationalAttrs( ld, testObjDN )) != LDAP_SUCCESS )
printf("\n Unable to read entryDN object: %s\n",
ldap_err2string( rc ));
ldap_unbind_s( ld );
return ( 0 );
}
int printOperationalAttrs( LDAP *ld, char * dn ) {
int rc, flags;
char **values, *attribute;
struct timeval timeOut = {10,0};
BerElement *ber;
LDAPMessage *searchResult, *entry;
char *attrs[] = { "createTimeStamp",
"creatorsName",
"entryFlags",
"federationBoundary",
"localEntryID",
"modifiersName",
"modifyTimeStamp",
"structuralObjectClass",
"subordinateCount",
"subschemaSubentry",
NULL };
rc = ldap_search_ext_s(
ld,
dn,
LDAP_SCOPE_BASE,
"(objectclass=*)",
attrs,
0,
NULL,
NULL,
&timeOut,
LDAP_NO_LIMIT,
&searchResult );
if ( rc != LDAP_SUCCESS ){
ldap_msgfree( searchResult );
return ( rc );
}
entry = ldap_first_entry( ld, searchResult );
printf("\n Entry: %s\n", dn);
printf("\n LDAP Operational Attributes: ");
for ( attribute = ldap_first_attribute( ld, entry, &ber );
attribute != NULL;
attribute = ldap_next_attribute( ld, entry, ber ) ) {
if( (values = ldap_get_values( ld, entry, attribute)) != NULL ) {
if ( (strcmp( attribute, "modifyTimeStamp" ) == 0)
&& (values[0] != NULL)) {
printf("\n %s:", attribute );
printf("\n %s (UTC)", values[0] );
printTime( values[0] );
}
else if ( (strcmp( attribute, "createTimeStamp" ) == 0)
&& (values[0] != NULL)) {
printf("\n %s:", attribute );
printf("\n %s (UTC)", values[0] );
printTime( values[0] );
}
else if ( (strcmp( attribute, "entryFlags" ) == 0)
&& (values[0] != NULL)) {
sscanf( values[0], "%d", &flags);
if ( flags != 0 )
displayEntryFlags( flags );
}
else if ( values[0] != NULL )
printf("\n %s: %s", attribute, values[0] );
ldap_value_free( values );
ldap_memfree( attribute );
}
}
printf("\n\n");
ber_free(ber, 0);
ldap_msgfree( searchResult );
return rc;
}
void displayEntryFlags( int flags )
{
char entryFlags[128];
strcpy( entryFlags, "" );
if ( flags & DS_ALIAS_ENTRY )
strcat( entryFlags, "AliasEntry ");
if ( flags & DS_PARTITION_ROOT )
strcat( entryFlags, "PartionRoot ");
if ( flags & DS_CONTAINER_ENTRY )
strcat( entryFlags, "ContainerEntry ");
if ( flags & DS_CONTAINER_ALIAS )
strcat( entryFlags, "ContainerAlias ");
if ( flags & DS_MATCHES_LIST_FILTER )
strcat( entryFlags, "MatchesListFilter ");
if ( flags & DS_REFERENCE_ENTRY )
strcat( entryFlags, "ReferenceEntry ");
if ( flags & DS_40X_REFERENCE_ENTRY )
strcat( entryFlags, "40XReferenceEntry ");
if ( flags & DS_BACKLINKED )
strcat( entryFlags, "Backlinked ");
if ( flags & DS_NEW_ENTRY )
strcat( entryFlags, "NewEntry ");
if ( flags & DS_TEMPORARY_REFERENCE )
strcat( entryFlags, "TemporaryEntry ");
if ( flags & DS_AUDITED )
strcat( entryFlags, "Audited ");
if ( flags & DS_ENTRY_NOT_PRESENT )
strcat( entryFlags, "EntryNotPresent ");
if ( flags & DS_ENTRY_VERIFY_CTS )
strcat( entryFlags, "EntryVarifyCTS ");
if ( flags & DS_ENTRY_DAMAGED )
strcat( entryFlags, "EntryDamaged");
printf("\n entryFlags: %s", &entryFlags);
}
void printTime( char *utc ) {
char *time;
struct tm UTC;
sscanf( utc, "%4d%02d%02d%02d%02d%02d", &UTC.tm_year, &UTC.tm_mon,
&UTC.tm_mday, &UTC.tm_hour, &UTC.tm_min, &UTC.tm_sec );
UTC.tm_year -= 1900;
UTC.tm_mon -= 1;
UTC.tm_isdst = 0;
if ( mktime(&UTC) != (time_t)-1 ) {
time = asctime(&UTC);
time[strlen(time)-1] = '\0';
printf( "\n %s (UTC)", time );
}
}