[Date Prev][Date Next] [Chronological] [Thread] [Top]

userPassword Problem



hi all,
I have used LD AP Browser/Editor for creating userPassword attribute.
But i have a problem here while reading this attribute. When i read this
attribute with the openldap ldapsearch utility it works fine. But when i
try and read this userPassword attribute with JNDI it gives me some
garbage characters and then these characters change everytime i run the
program. All the other attributes are being read properly through JNDI.
Can anybody guess what the problem is ?? Can anybody  using JNDI for
reading  userPassword attribute  help me out by sending the code for
doing the same. My code is attached .
Thanks in advance
Anup Chopra
import LDAPBean;
 class call
{
	public static void main( String args[])
		{
		LDAPBean l = new LDAPBean();
		l.getData(null, "userpassword","chetan");
String pass=l.getAttribute("userpassword");
System.out.println(pass);
		if (pass.equalsIgnoreCase("chetan"))
		System.out.println("ok");
		}
}
	
import java.util.Hashtable;
import java.util.Date;
import java.util.Enumeration;
import javax.naming.*;
import javax.naming.directory.*;
import Env1;

public class LDAPBean

{
	Hashtable env = new Hashtable(5,0.75f);
	DirContext dirctx;
	String FilterCtx = "cn=";
	Attributes attrbs;

	public LDAPBean()
		{
				initialize();
		}
		
	public void initialize()
		{
				try
				{
					env.put(Context.INITIAL_CONTEXT_FACTORY, Env1.INITCTX);
					env.put(Context.PROVIDER_URL, Env1.MY_SERVICE);
					env.put(Context.SECURITY_AUTHENTICATION, Env1.SIMPLE);
					env.put(Context.SECURITY_PRINCIPAL, Env1.MGR_DN);
					env.put(Context.SECURITY_CREDENTIALS, Env1.MGR_PW);
					dirctx = new InitialDirContext(env);
				}
				catch(Exception e)
				{
					System.out.println("Naming Exception caught");
					e.printStackTrace();
				}
		}
	
	public String getData(String U_Identity,String FilterPar,String ParVal)
		{
			try
			{	
				int zflag = 0;
				SearchControls constraints = new SearchControls();
				constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
				FilterCtx += U_Identity;
				FilterCtx +=", dc=anup, dc=com";
				if(U_Identity == null)
					FilterCtx ="dc=anup, dc=com";
				String FilterExp = FilterPar + "=" + ParVal;
				NamingEnumeration results = dirctx.search(FilterCtx,FilterExp,constraints);
				while(results != null && results.hasMore())
				{	
					SearchResult si = (SearchResult) results.next();
				  if(zflag == 0)attrbs = si.getAttributes();
				  zflag++;
				  				  
				  String name = si.getName();
				  int startpoint = name.lastIndexOf(",cn=");
				  if(startpoint >= 0) 
				  {
				  	name = name.substring(startpoint+1);
				  	name += ", dc=anup, dc=com";
				  	String NewFilterCtx = name;
				  	System.out.println("new context = " + NewFilterCtx);
				  	FilterExp = "objectclass=*";
						NamingEnumeration newresults = dirctx.search(NewFilterCtx,FilterExp,constraints);
						if(newresults != null && zflag == 1) 
						{
							NamingEnumeration ne = attrbs.getIDs();
							while(ne != null && ne.hasMore())
							{
								attrbs.remove((String) ne.next());
							}
						}
								
						while(newresults != null && newresults.hasMore())	
				  	{
					  
					  	si = (SearchResult) newresults.next();
					  	String temp = si.getAttributes().toString();
					  	temp = temp.substring(1);
					  	int spoint = 0;
					  	int epoint = temp.indexOf("=");
					  	while(epoint >= 0)
				  		{
				  			String val = "";
				  			String key = temp.substring(spoint,epoint);
				  			spoint = temp.indexOf(":",epoint);
				  			epoint = temp.indexOf(",",spoint);
				  			
				  			if(epoint >= 0)
				  				val = temp.substring(spoint+2,epoint);
				  			else  
				  				val = temp.substring(spoint+2,(temp.length()-1));
				  			attrbs.put(key,val);
				  			if(epoint >= 0) 
				  			{
				  				spoint = epoint + 2;
				  				epoint = temp.indexOf("=",spoint);
				  			}
				  		}
				    }
				  }
				}
//				System.out.println(attrbs.toString());
				return "Object retrieved";	
				
			}
			catch(Exception e)
			{
				System.out.println("JNDISearch failed");
				e.printStackTrace();
				return null;
			}
		}

	public String getAttribute(String key)
		{
			try
			{
				if(attrbs != null) 
				{

/* if more than one user form the result 
		the problem won't ever come if each attribute irrespective of it's entry object is given 
		distinct name (objectclass attribute may retained with its same name)
*/		

					Attribute attr = attrbs.get(key);
					String attrvalue = "";
					if(attr != null)
					{
					for(NamingEnumeration vals =
attr.getAll();vals.hasMoreElements();attrvalue += (vals.nextElement()+ "\n"));
					} 			
	return attrvalue; 				}
				else
				{
					System.out.println("To initialise the search first run getdata()");
					System.out.println("Or no object found");
					return null;
				}
			}
			
			catch(NamingException e)
			{
				System.out.println("Naming Exception caught");
				e.printStackTrace();
				return null;
			}
			
		}
	
	public void modifyAttribute(String[] root,int i,String attr,Object val)
		{
			Attribute newattr = new BasicAttribute(attr,val);
			ModificationItem[] mods = new ModificationItem[1];
			if(i == 1)mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,newattr);
			else mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,newattr);
			
			/* 
			if value of i passed is equal to 1 then new value will be added to existing attribute 
			else (default) new value will replace older values 
			*/
			
			String distname = "";
			for(int j = root.length;j>0;j--)
				{
					distname += "cn=";
					distname += root[j-1];
					distname += ", ";
				}
			distname += "dc=anup, dc=com";
			
			try
			{
				dirctx.modifyAttributes(distname,mods);
				System.out.println("Modification successful");
			}
			catch(Exception e)
			{
				System.out.println("Modification failed");
				e.printStackTrace();
			}
		}

	public void addAttribute(String[] root,String attr,Object val)
		{
			Attribute newattr = new BasicAttribute(attr,val);
			ModificationItem[] mods = new ModificationItem[1];
			mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,newattr);
			
			String distname = "";
			for(int j = root.length;j>0;j--)
				{
					distname += "cn=";
					distname += root[j-1];
					distname += ", ";
				}
			distname += "dc=anup, dc=com";
			
			try
			{
				dirctx.modifyAttributes(distname,mods);
				System.out.println("Addition successful");
			}
			catch(Exception e)
			{
				System.out.println("Addition failed");
				e.printStackTrace();
			}
		}


	public void deleteAttribute(String[] root,String attr)
		{
			Attribute newattr = new BasicAttribute(attr);
			ModificationItem[] mods = new ModificationItem[1];
			mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,newattr);
			String distname = "";
			
			for(int i = root.length;i>0;i--)
				{
					distname += "cn=";
					distname += root[i-1];
					distname += ", ";
				}
			distname += "dc=anup, dc=com";
			
			try
			{
				dirctx.modifyAttributes(distname,mods);
				System.out.println("Deletion successful");
			}
			catch(Exception e)
			{
				System.out.println("Deletion failed");
				e.printStackTrace();
			}
		}

	
	public void addEntry(String[] root,Hashtable htable)
		{
			String distname = "";
			try
			{
				for(int i = root.length;i>0;i--)
				{
					distname += "cn=";
					distname += root[i-1];
					distname += ", ";
				}
				distname += "dc=anup, dc=com";

				Attribute objClasses = new BasicAttribute("objectclass");
				int i = root.length;
				objClasses.add(root[i-1]);
				Attributes orig = new BasicAttributes();
				orig.put(objClasses);
				
				Enumeration eattr = htable.keys();
				while(eattr.hasMoreElements())
				{
       		String attr = (String) eattr.nextElement();
       		Object val = htable.get(attr);
       				
					if(attr != null && val != null)
					{
						Attribute sno = new BasicAttribute(attr,val);
						orig.put(sno);
					}
				}
				
				dirctx.createSubcontext(distname, orig);
				System.out.println("Entry Added");
			}
			
			catch (NameAlreadyBoundException e) 
			{
			System.out.println("Entry " + distname + " already exists, no need to add");
			}
			
			catch(Exception ex)
			{
				System.out.println("Modification failed");
				ex.printStackTrace();
			}
		}
		
		public void deleteEntry(String[] root)
		{
			String distname = "";
			try
			{
				for(int i = root.length;i>0;i--)
				{
					distname += "cn=";
					distname += root[i-1];
					distname += ", ";
				}
			  distname += "dc=anup, dc=com";
				
				dirctx.destroySubcontext(distname);
				System.out.println("Entry Deleted");
			}
			
			catch (NameNotFoundException e) 
			{
				System.out.println("Entry " + distname + " not found");
			}
			
			catch(Exception ex)
			{
				System.out.println("Modification failed");
				ex.printStackTrace();
			}
		}
}