1

I have a problem adding an entry in LDAP, precisely I want to add a user.

class AddUser {

    public static void main(String[] args) {

        String userName = "manager";
        String password = "pass";
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://192.168.10.45:389/dc=mydc,dc=local");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, new String("mydc" + "\\" + userName));
        env.put(Context.SECURITY_CREDENTIALS, password);
        // env.put(Context.REFERRAL, "follow");
        // entry's DN
        String entryDN = "cn=NewUser, dc=mydc, dc=local";
        // entry's attributes
        Attribute cn = new BasicAttribute("cn", "NewUser");
        Attribute sn = new BasicAttribute("sn", "Smith");
        Attribute mail = new BasicAttribute("mail", "newuser@foo.com");
        Attribute phone = new BasicAttribute("telephoneNumber", "+1 222 3334444");
        Attribute uid = new BasicAttribute("uid", "nsmith");
        Attribute userPassword = new BasicAttribute("userPassword", "pwd1");
        Attribute oc = new BasicAttribute("objectClass");
        oc.add("dcObject");
        oc.add("person");
        oc.add("inetOrgPerson");

        DirContext ctx = null;

        try {
            // get a handle to an Initial DirContext
            ctx = new InitialDirContext(env);

            // build the entry
            Attributes entry = new BasicAttributes();
            entry.put(cn);
            entry.put(sn);
            entry.put(mail);
            entry.put(phone);
            entry.put(uid);
            entry.put(userPassword);
            entry.put(oc);

            // Add the entry
            ctx.createSubcontext(entryDN, entry);
            System.out.println("AddUser: added entry " + entryDN + ".");

        } catch (NamingException e) {
            System.err.println("AddUser: error adding entry." + e);
        }
    }
}

I got the following error:

AddUser: error adding
entry.javax.naming.directory.NoSuchAttributeException: [LDAP: error code 16 - 00000057: LdapErr: DSID-0C090C3E, comment: Error in attribute conversion operation, data 0, v1db1 ]; remaining name cn=NewUser, dc=mydc, dc=local

I don't know where is the error. Can be only a bad input about the attributes?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user840718
  • 1,563
  • 6
  • 29
  • 54
  • 1
    You are using `dcObject` objectclass. This OC requires `dc` attribute to be present on the object, which does not make much sense for persons (you should not use that objectclass). I am pretty sure this information is burried somewhere in the error... you should use proper logging mechanism (i.e. SLF4J with Logback) and not throw exception stack-traces away. – Pavel Horal Jun 19 '14 at 16:08
  • Well, removing that object I got the following: [LDAP: error code 32 - 0000208D: NameErr: DSID-0310020A, problem 2001 (NO_OBJECT), data 0, best match of: 'DC=MYDC,DC=LOCAL' – user840718 Jun 19 '14 at 16:13
  • This seems like there is something wrong with your root context... are you sure it is properly defined? – Pavel Horal Jun 19 '14 at 20:38

1 Answers1

0

The right code is the following:

public static void main(String[] args) {

    String userName = "admin";
    String password = "s3cret";
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://192.168.10.10:389/DC=SOFTWAREDEV,DC=LOCAL");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, new String("softwaredev" + "\\" + userName));
    env.put(Context.SECURITY_CREDENTIALS, password);
    String path = "OU=SoftwareV3,OU=SOFTWARE";
    String newUser = "myUser"; // insert user here
    String entryDN = "CN=" + newUser + "," + path;
    Attribute cn = new BasicAttribute("cn", newUser);
    Attribute oc = new BasicAttribute("objectClass");
    oc.add("top");
    oc.add("person");
    oc.add("organizationalPerson");
    oc.add("user");

    DirContext ctx = null;

    try {
        ctx = new InitialDirContext(env);
        Attributes entry = new BasicAttributes();
        entry.put(cn);
        entry.put(oc);
        ctx.createSubcontext(entryDN, entry);
        System.out.println("AddUser: added entry " + entryDN + ".");

    } catch (NamingException e) {
        System.err.println("AddUser: error adding entry." + e);
    }
}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user840718
  • 1,563
  • 6
  • 29
  • 54
  • 1
    In other words the DN was wrong? There 's not much point in posting the corrected code unless you tell us how it was corrected. – user207421 Jun 23 '14 at 12:47
  • Yeah. I used wireshark and ldapadmin to see the correct packet header to readapt it correctly. – user840718 Jun 23 '14 at 13:19