#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: modifyTimeStamp <host name> <port number> <login dn> <password>"
"\n <user DN> <day extension>"
"\nExample: modifyTimeStamp Acme.com 389 cn=admin,o=Acme secret"
"\n cn=James,o=Sales 90\n";
int getTime( LDAP *, char *, char *);
void printTime( char * );
int extendTime( char * , int );
int main( int argc, char **argv) {
int rc, ldapPort, version, days;
char *ldapHost, *loginDN, *password, *userDN, time[32], *timeValues[2];
LDAP *ld;
LDAPMod modTime, *modify[2];
struct timeval timeOut = {10,0};
#if defined(N_PLAT_NLM) && defined(LIBC)
setscreenmode(SCR_NO_MODE);
#endif
if (argc != 7) {
printf("%s", usage);
return 1;
}
ldapHost = argv[1];
ldapPort = atoi(argv[2]);
loginDN = argv[3];
password = argv[4];
userDN = argv[5];
days = atoi(argv[6]);
if ( days == 0 ) {
printf("\n day extension is zero. "
"Timestamp was not modified.\n\n");
return (0);
}
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("\n Bind successful.\n");
else {
printf("\n ldap_simple_bind_s: %s\n", ldap_err2string( rc ));
ldap_unbind_s ( ld );
return 1;
}
printf("\n Entry DN: %s", userDN);
printf("\n Day Extension: %d\n", days);
if ( ( rc = getTime( ld, userDN, time )) != LDAP_SUCCESS ) {
printf("\n Unable to read the entry: %s\n", ldap_err2string( rc ));
ldap_unbind_s( ld );
return 1;
}
if ( strlen(time) == 0 ) {
printf("\n passwordExpirationTime was not initialized.\n\n");
ldap_unbind_s( ld );
return 1;
}
else {
printf( "\n passwordExpirationTime before modification");
printf( "\n passwordExpirationTime: %s (UTC)", time );
printTime( time );
if ( (rc = extendTime( time, days )) != LDAP_SUCCESS ) {
printf( "\n\n Failed to extend time.\n" );
ldap_unbind_s( ld );
return 1;
}
modTime.mod_op = LDAP_MOD_REPLACE;
modTime.mod_type = "passwordExpirationTime";
timeValues[0] = time;
timeValues[1] = NULL;
modTime.mod_values = timeValues;
modify[0] = &modTime;
modify[1] = NULL;
printf("\n Modifying passwordExpirationtime..." );
rc = ldap_modify_ext_s( ld,
userDN,
modify,
NULL,
NULL);
if ( rc != LDAP_SUCCESS ) {
printf("ldap_modify_ext_s: %s\n", ldap_err2string( rc ));
ldap_unbind_s( ld );
return 1;
}
printf("\n passwordExpirationTime was modified successfully.\n");
if ( ( rc = getTime( ld, userDN, time )) != LDAP_SUCCESS ) {
printf( "\n Failed to get passwordExpirationTime attribute." );
ldap_unbind_s( ld );
return 1;
}
if ( strlen(time) == 0 ) {
printf("\n Failed to get passwordExpirationTime.\n\n");
ldap_unbind_s( ld );
return 1;
}
else {
printf( "\n passwordExpirationTime after modification" );
printf( "\n passwordExpirationTime: %s (UTC)", time );
printTime( time );
}
}
printf("\n");
ldap_unbind_s( ld );
return 0;
}
int getTime( LDAP *ld, char *dn, char *utc) {
int rc;
char **values, *attr;
char *attrs[] = { "passwordExpirationTime", NULL };
struct timeval timeOut = {10,0};
BerElement *ber;
LDAPMessage *searchResult, *entry;
rc = ldap_search_ext_s(
ld,
dn,
LDAP_SCOPE_BASE,
"(objectclass=*)",
attrs,
0,
NULL,
NULL,
&timeOut,
LDAP_NO_LIMIT,
&searchResult );
if ( rc != LDAP_SUCCESS )
return rc ;
if ( (entry = ldap_first_entry( ld, searchResult )) != NULL
&& (attr = ldap_first_attribute( ld, entry, &ber )) != NULL
&& (values = ldap_get_values( ld, entry, attr)) != NULL) {
strcpy( utc, values[0] );
ldap_value_free( values );
ldap_memfree( attr );
}
else
strcpy( utc, "");
ber_free(ber, 0);
ldap_msgfree( searchResult );
return 0;
}
void printTime( char * utc ) {
char *time;
struct tm UTC;
sscanf( utc, "%4d%2d%2d%2d%2d%2d", &UTC.tm_year, &UTC.tm_mon,
&UTC.tm_mday, &UTC.tm_hour, &UTC.tm_min, &UTC.tm_sec );
UTC.tm_mon -= 1;
UTC.tm_year -= 1900;
UTC.tm_isdst = 0;
if( mktime( &UTC ) != (time_t)-1 ) {
time = asctime(&UTC);
time[strlen(time)-1] = '\0';
printf("\n%60s (UTC)", time);
}
}
int extendTime( char * utc, int days) {
struct tm UTC;
sscanf( utc, "%4d%2d%2d%2d%2d%2d", &UTC.tm_year, &UTC.tm_mon,
&UTC.tm_mday, &UTC.tm_hour, &UTC.tm_min, &UTC.tm_sec );
UTC.tm_mon -= 1;
UTC.tm_year -= 1900;
UTC.tm_isdst = 0;
UTC.tm_mday += days;
if ( mktime( &UTC ) == (time_t)-1 )
return -1;
UTC.tm_year += 1900;
UTC.tm_mon += 1;
sprintf( utc, "%4d%02d%02d%02d%02d%02d%c", UTC.tm_year, UTC.tm_mon,
UTC.tm_mday,UTC.tm_hour, UTC.tm_min, UTC.tm_sec, 'Z');
return 0;
}