Unexpected checking with OpenLdap
Hi.
I'm trying to check user and password of uid into an OpenLdap session.
This is my method.
privatestaticboolean checkPsw(String host, String user, String psw){
boolean result =true;
Properties env =new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,INITIAL_CONTEXT_FACTORY);
env.put(Context.PROVIDER_URL,host.trim());
env.put(Context.SECURITY_PRINCIPAL,user.trim());//"uid=master,cn=Users,o=myorganization,c=it");
env.put(Context.SECURITY_CREDENTIALS,psw.trim());//"qwertyui");
try{
// The following row (together with the catch block) is the only requested.
DirContext ctx =new InitialDirContext(env);
// The following rows are here for testing only
Attributes attrs = ctx.getAttributes(user,new String[]{"userPassword"});
if (attrs !=null){
Attribute attr = attrs.get("userPassword");
NamingEnumeration vals = attr.getAll();
while (vals.hasMoreElements()){
Object o = vals.nextElement();
System.out.println("userPassword:" +new String((byte[]) o));
}
}
}catch(Exception e){
result =false;
}
return result;
}
Now I print a piece of the .ldif which I used to load the server LDAP.
dn: cn=Users,o=myorganization,c=it
objectclass: container
objectclass: top
cn: Users
dn: cn=Groups,o=myorganization,c=it
objectclass: container
objectclass: top
cn: Groups
dn: uid=master,cn=Users,o=myorganization,c=it
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: master
givenName: master
sn: Master Master
uid: master
userPassword:: qwertyui
dn: uid=johndoe,cn=Users,o=myorganization,c=it
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Doe
givenName: johndoe
sn: John Doe
uid: johndoe
userPassword:: abcdefgh
The problem is this:
If I try the authentication with the user "master" (and his password), I works correctly, i.e. I don't throw any Exception. I remember that "master" is the user root of my ldap.
On the other side, if I try the authentication with the user "johndoe" (and his password) I catch an AutenthicationException while, it works with the password equals to userid (userid=johndoe and password=johndoe).
In fact, if I "force" Context.SECURITY_PRINCIPAL and Context.SECURITY_CREDENTIALS with the user root values (look at the commented lines) I can explore the ldap with my user (johndoe).
The result is that the
System.out.println("userPassword:" + new String((byte[]) o));
returns "userPassword:johndoe".
If you think that I've loaded the LDAP uncorrectly, it's wrong.
I've browsed my server and re-exported into an ldif file into which the passwords are correct.
Could you help me?
Thanks a lot.

