By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,399 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

Error while trying to add a user in LDAP Directory

P: 7
Hi,

I have got the following error while trying to add a user in the LDAP Directory.

javax.naming.NameNotFoundException: [LDAP: error code 32 - No Such Object]; remaining name 'uid=vassila,ou=People,dc=cs,dc=uno,dc=edu'

I have given all the attributes which are needed, for the user, in the code and also the proper path where the user has to be added. Please have a look at my code

[CODE]
// This is a class file which stores all the info required for the user
//userInformationObject.java
//package code;
import java.util.Properties;

public final class UserInformationObject {

public static final String FIRST_NAME = "1";
public static final String USER_ID = "2";
public static final String HOMEDIRECTORY = "3";
public static final String UIDNUMBER = "4" ;
public static final String GIDNUMBER = "5" ;

private Properties props;


private UserInformationObject() {

}


public UserInformationObject( String firstName, String userid, String homeDirectory, String gidNumber, String uidNumber ) {
props = new Properties();
props.setProperty(FIRST_NAME, firstName);
props.setProperty(USER_ID, userid);
props.setProperty(HOMEDIRECTORY, homeDirectory);
props.setProperty(UIDNUMBER , uidNumber);
props.setProperty(GIDNUMBER , gidNumber);
}

//userid

public UserInformationObject(String userid) {
props = new Properties();
props.setProperty(USER_ID, userid);
}

//First Name
public void setFirstname(String firstName) {
props.setProperty(FIRST_NAME, firstName);
}

//home Directory
public void homeDirectory(String homeDirectory) {
props.setProperty(HOMEDIRECTORY , homeDirectory);
}


//GID NUMBER
public void gidNumber( String gidNumber) {
props.setProperty(GIDNUMBER, gidNumber);
}

//UID NUMBER
public void uidNumber( String uidNumber) {
props.setProperty(UIDNUMBER, uidNumber);
}



public String getProperty(String propertyName) {
return props.getProperty(propertyName);
}

public int size() {
return props.size();
}
}

// This is the mail class of the code
//LDAPTest.java
//package code;

import java.util.*;
import java.util.Properties;
import javax.naming.*;
import javax.naming.directory.*;


public final class LDAPTest {

private DirContext ctx;
private Hashtable env;


private static final String LDAP_URL = "ldap://137.30.122.145 : 6789";
//dn
private static final String MANAGER_DN = "cn=admin,cn=administrators,cn=dscc";

private static final String MANAGER_PASSWORD = "testpc01";

private static final String AUTH_TYPE = "simple";

private static final String CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";

private static final String BASE_DN = "ou=People,dc=cs,dc=uno,dc=edu";

private static final String BASE_DN1 = "ou=Group,dc=cs,dc=uno,dc=edu";

//ldap
public LDAPTest() throws NamingException {
setEnvironment();
ctx = new InitialDirContext(env);
//createGroup();
System.out.println("Conn Successful");
}

//?????LDAP??????
private void setEnvironment() {
env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY);
env.put(Context.PROVIDER_URL, LDAP_URL);
env.put(Context.SECURITY_AUTHENTICATION, AUTH_TYPE);
env.put(Context.SECURITY_PRINCIPAL, MANAGER_DN);
env.put(Context.SECURITY_CREDENTIALS, MANAGER_PASSWORD);
}


//???LDAP??????
private void closeConnection() {
try {
ctx.close();
} catch (NamingException ne) {
System.out.println(ne);
}
}


//*****************************USER***************** ************

//????????
private boolean isUserexist(String uid) {

System.out.println("User existing?");
try {
System.out.println("User existing?");
Attributes attrs = findUser(uid);
if (attrs != null) { return true; }
else { return false; }
} catch (NamingException ne) {
return false;
}
}

private void putAttribute(Attributes attrs, String attrName, String attrValue) {
if (attrValue != null && attrValue.length()!= 0) {
Attribute attr = new BasicAttribute(attrName, attrValue);
attrs.put(attr);
}
}

private String getAttribute(Attributes attrs, String attrName)
throws NamingException {
Attribute attr = attrs.get(attrName);
if (attr == null)
{ return ""; }
else
{ return (String)attr.get(); }
}

//Finding a user

private Attributes findUser(String uid) throws NamingException {

System.out.println("trying to find the user");
return ctx.getAttributes("uid=" + uid + "," + BASE_DN);
}

//create a user

public void createUser(UserInformationObject userobj) throws NamingException {
System.out.println("trying to create a user");

if (userobj == null) {
throw new NamingException("No user information");
}

//check for existing uid

String uid = userobj.getProperty(UserInformationObject.USER_ID) ;
if (uid == null && uid.length() == 0) {
throw new NamingException("No uid you specifyn");
}
if (isUserexist(uid)) {
throw new NamingException("The user(uid: " + uid + " is exist!n");
}

//firstName
String firstName = userobj.getProperty(UserInformationObject.FIRST_NA ME);
if (firstName == null || firstName.length() == 0) {
throw new NamingException("No first name you specify!n");
}


System.out.println("User found2");
String homeDir = userobj.getProperty(UserInformationObject.HOMEDIRE CTORY );
String gidNumber = userobj.getProperty(UserInformationObject.GIDNUMBE R);
String uidNumber = userobj.getProperty(UserInformationObject.UIDNUMBE R);


Attributes attrs = new BasicAttributes();

Attribute objclass = new BasicAttribute("objectclass");
objclass.add("top");
objclass.add("account");
//objclass.add("organizationalUnit");
objclass.add("posixAccount");

attrs.put(objclass);

System.out.println("User found1");

putAttribute(attrs, "uid", uid);
System.out.println(attrs);
putAttribute(attrs, "cn", firstName);
System.out.println(attrs);
putAttribute(attrs, "homeDirectory", homeDir );
System.out.println(attrs);

putAttribute(attrs, "gidNumber", gidNumber);
putAttribute(attrs, "uidNumber", uidNumber);

System.out.println("User found");

ctx.bind("uid=" + uid + "," + BASE_DN, null, attrs);

System.out.println("binding?");

System.out.println("User(uid: " + uid + ") created.n");

}

//modify the user

public void modifyUser(UserInformationObject userobj) throws NamingException {
System.out.println("binding?");
if (userobj == null) {
throw new NamingException("No user information!n");
}

String uid = userobj.getProperty(UserInformationObject.USER_ID) ;
if (uid == null && uid.length() == 0) {
throw new NamingException("No uid you specify!n");
}
if (!isUserexist(uid)) {
throw new NamingException("The user(uid: " + uid + ") does not exist!n");
}

int size = userobj.size();

if (size > 1) {

/*String password = userobj.getProperty(UserInformationObject.PASSWORD );
String email = userobj.getProperty(UserInformationObject.EMAIL);
String phone = userobj.getProperty(UserInformationObject.PHONE);
String fax = userobj.getProperty(UserInformationObject.FAX);
String commonName = userobj.getProperty(UserInformationObject.COMMON_N AME);
String firstName = userobj.getProperty(UserInformationObject.FIRST_NA ME);
String lastName = userobj.getProperty(UserInformationObject.LAST_NAM E);

Attributes attrs = new BasicAttributes();
putAttribute(attrs, "cn", commonName);
putAttribute(attrs, "givenname", firstName);
putAttribute(attrs, "sn", lastName);
putAttribute(attrs, "userpassword", password);
putAttribute(attrs, "mail", email);
putAttribute(attrs, "telephonenumber", phone);
putAttribute(attrs, "facsimiletelephonenumber", fax);


ctx.modifyAttributes("uid=" + uid + "," + BASE_DN, DirContext.REPLACE_ATTRIBUTE, attrs);
*/
System.out.println("User(uid: " + uid + ") information modified.n");
} else {
throw new NamingException("No modify information you specify!n");
}
}

//delete the user
public void deleteUser(String uid) throws NamingException {
if (!isUserexist(uid)) {
throw new NamingException("The user(uid: " + uid + ") does not exist!n");
}
ctx.destroySubcontext( "uid=" + uid + "," + BASE_DN);
System.out.println("User(uid: " + uid + ") deleted.n");
}

//uid
public void selectUser(String uid) throws NamingException {
Attributes attrs;
System.out.println("select user(uid: " + uid + ")...");
try {
attrs = findUser(uid);
System.out.println("-----------------------------");
System.out.println("User(uid: " + uid + ") listing...");

System.out.println("First Name: " + getAttribute(attrs, "givenname"));
System.out.println("Last Name: " + getAttribute(attrs, "sn"));
System.out.println("Common Name: " + getAttribute(attrs, "cn"));
System.out.println("User ID: " + getAttribute(attrs, "uid"));
System.out.println("E-Mail: " + getAttribute(attrs, "mail"));
System.out.println("Phone: " + getAttribute(attrs, "telephonenumber"));
System.out.println("Fax: " + getAttribute(attrs, "facsimiletelephonenumber"));
System.out.println("List completed");
System.out.println("-----------------------------n");
}
catch (NamingException ne) {
System.out.println("The user(uid: " + uid + ") does not exist!n");
}
}

public void selectUser(String[] uid) {
for (int i = 0; i < uid.length; i++) {
//try {
selectUser(uid);
//}
//catch (NamingException ne) {
//System.out.println(ne);
continue;
//}
}
}

protected void finalize() {
closeConnection();
}



//*****************************GROUP**************** **************

//Check if a Group exists

private boolean isGroupexist(String groupid ) {
try {
Attributes attrs = findGroup(groupid);
if (attrs != null) { return true; }
else { return false; }
} catch (NamingException ne) {
return false;
}
}


//Find a Group

private Attributes findGroup(String groupid ) throws NamingException {
return ctx.getAttributes("groupid =" + groupid + "," + BASE_DN1);
}


//create a group

public void createGroup(GroupInformationObject groupobj) throws NamingException {

if (groupobj == null) {
throw new NamingException("No group information");
}

//check for existing groupid

String groupid = groupobj.getProperty(GroupInformationObject.GROUP_ ID);
if (groupid == null && groupid.length() == 0) {
throw new NamingException("No uid you specifyn");
}
if (isGroupexist(groupid)) {
throw new NamingException("The group(groupid: " + groupid + " is exist!n");
}

//GROUP Name
String groupName = groupobj.getProperty(GroupInformationObject.GROUP_ NAME);
if (groupName == null || groupName.length() == 0) {
throw new NamingException("Group name not specified");
}

String password = groupobj.getProperty(GroupInformationObject.PASSWO RD);
String email = groupobj.getProperty(GroupInformationObject.EMAIL) ;

Attributes attrs = new BasicAttributes();

//Attribute objclass = new BasicAttribute("objectclass");
//objclass.add("top");
//objclass.add("people");
//objclass.add("organizationalunit");
//objclass.add("inetorgperson");
//attrs.put(objclass);

putAttribute(attrs, "groupName", groupName);
putAttribute(attrs, "groupId", groupid);
putAttribute(attrs, "groupPassword", password);
putAttribute(attrs, "groupMail", email);

ctx.bind("groupid=" + groupid + "," + BASE_DN1 , null, attrs);

System.out.println("Group (groupid: " + groupid + ") created");

}

}

//http://java.sun.com/products/jndi/tutorial/ldap/misc/attrs.html


//This contains the main() class
//Test.java
//package code;

import java.util.*;
import java.lang.*;
import java.util.Properties;
import javax.naming.*;
import javax.naming.directory.*;

public class Test {
public static void main(String[] args) {

UserInformationObject vassila = new UserInformationObject( "Vassila Roussev", "vassila", "/home/vassila", "896", "274");

try {
LDAPTest ldap = new LDAPTest();

//ldap.createGroup(abc37rama);

ldap.createUser(vassila);

System.out.println("User found");
//ldap.createUser(lisi);

//String[] user = {"zhangsan"};

//ldap.selectUser(user);


//UserInformationObject zhangsang=new UserInformationObject( "zhang", "sang", "zhang sang", "zhangsang");

//zhangsang.setPhone("02814911234");
//zhangsang.setEmail("[email]hanldap@gap.com[email]");

//ldap.modifyUser(zhangsang);


//ldap.selectUser("zhangsang");
//while modifying the user, the object of the user should be explicitly called??

//ldap.deleteUser("zhangsangh");

//ldap.selectUser(user);

} catch (Exception e) {
System.out.println(e);
}
}
}
[CODE]

Please let me know where I went wrong.
Dec 13 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.