Tuesday, August 26, 2014

Tivoli LDAP V6.1 Retrieve LDAP attribute pwdChangedTime & modifytimestamp

There is requirement to show alert message to user before expire password. We have set up password policy in LDAP. User password will expire after 45 days. Before password expire alert message show on screen to change password.

I am retrieving pwdChangedTime timestamp from LDAP.  pwdChangedTime attribute value give us date and time of when password has been changed. After it will easy to find out next password expire date and according shown message to user. Make sure you have to use root and password to retrieve data


import com.sun.jndi.ldap.LdapCtxFactory;
import java.io.*;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.*;


public class LdapTest1 {

    public static void main(String[] args)  {
        try{
         DirContext ctx=null;
        // Create your search string
         String userId = "mytestuser";
        // String INITIAL_ENTRY = "uid" + "=" + userId + ",cn=" + "users" + "," + "dc=ibm,dc=com";
        
         String INITIAL_ENTRY = "uid=testuser,cn=users,DC=companyname,DC=COM";

        String pwdChangedTime = "";
        //InitialLdapContext ctx = null;
  
       
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "Ldap://127.0.0.0:1389");
           
         env.put(Context.SECURITY_PRINCIPAL, "cn=root");
         env.put(Context.SECURITY_CREDENTIALS, "password");
       
       
        env.put("java.naming.ldap.version", "3");
        ctx =new InitialDirContext(env); //new InitialLdapContext(env, null);
        Attribute attr = null;
    
            // Set up Search Controls
            SearchControls sc = new SearchControls();
            sc.setSearchScope(SearchControls.OBJECT_SCOPE);
                        String[] userAttrList = {"pwdChangedTime", "modifytimestamp"};
          //  String[] userAttrList = {"modifytimestamp"};
            sc.setReturningAttributes(userAttrList);
        
           NamingEnumeration ne = ctx.search(INITIAL_ENTRY,"(objectclass=*)", sc);

                     while(ne.hasMore()){
                        SearchResult searchresult = (SearchResult) ne.next();
                        Attributes attrs = searchresult.getAttributes();
                       /* Attribute value= attrs.get("pwdChangedTime");
                        System.out.println(value.get());
                        StringBuffer sbdate =new StringBuffer((String)value.get());
                        System.out.println("Year:"+sbdate.substring(0, 4));
                        System.out.println("Month:"+sbdate.substring(4, 6));
                        System.out.println("Day:"+sbdate.substring(6, 8));*/
                     
                       
                       
                       NamingEnumeration ae = attrs.getAll();
                        while (ae.hasMore()) {
                       
                            System.out.println(ae.nextElement());
                           
                        }
                    }
                    
                     ctx.close();
        }catch(Exception e){
            e.printStackTrace();
        }
                }// end method

    }// end class

Followers