473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Error while trying to add a user in LDAP Directory

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
0 3194

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

Similar topics

0
by: Lorenzo | last post by:
I'm trying to access to a LDAP server by a VB 6 application in order to get info about user. The following code works fine running on Windows 2000 professional but fails on Windows XP. Can...
4
by: David Moore | last post by:
Hello I am using the System.DirectoryServices namespace classes to access Active Directory. We connect using the LDAP://DOMAIN method. The code works on local dev boxes, and in staging, but...
0
by: David Moore | last post by:
Hello I posted a thread about this a while back, but I can't actually find it again so I can reply to it with the solution I found, so I'm making a new thread and hoping it goes to the top of...
0
by: microsoft | last post by:
Hi People, when I try to modify an active directory user programatically, I receive the following exception: The server is unwilling to process the request Reading the microsoft web site, I...
3
by: mrwoopey | last post by:
Hi, I am using the example "Authenticate against the Active Directory by Using Forms Authentication and Visual Basic .NET": http://support.microsoft.com/default.aspx?scid=KB;EN-US;326340 ...
1
by: Brad | last post by:
I'm trying to use Active Directory to retrieve information for a user...for display purposes.. I am not using windows authentication or impersonation....I am using a fixed user id and password for...
9
by: Patrick | last post by:
I have an ASP.NET page that searches for someone in the corporate Active Directory. It had been working fine until recently when I changed from Basic Authentication on IIS6 back to Integrated...
9
by: webrod | last post by:
Hi all, how can I check a user/password in a LDAP ? I don't want to connect with this user, I would like to connect to LDAP with a ADMIN_LOG/ADMIN_PWD, then do a query to find the user and...
0
by: choukse | last post by:
Hi All, I am trying to bind to ADAM instance with a windows user through JNDI and it keeps failing. My ADAM and AD is running on same Windows 2k3 server. But, through LDP I am able to bind with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.