472,783 Members | 900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,783 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 3129

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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.