3

I'm trying to enable a user in the Active Directory with LDAP and Java (1.4). However I keep getting the following error:

java.lang.NullPointerException at com.sun.jndi.ldap.LdapCtx.c_modifyAttributes(LdapCtx.java:1432) at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_modifyAttributes(ComponentDir Context.java:255) at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.modifyAttributes(Partial CompositeDirContext.java:172) at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.modifyAttributes(Partial CompositeDirContext.java:161) at javax.naming.directory.InitialDirContext.modifyAttributes(InitialDirContext. java:146) at be.ideal.LDAP.newuser.main(newuser.java:61) Exception in thread "main"

I already confirmed that my user has a password, I just can't seem to change his status to active

My code:

 public static void main(String[] args) {
        String userName = "cn=Albert Einstein,ou=Accounts,DC=PORTAL,DC=COMPANY,DC=BE";
        String groupName = "cn=Administrators,cn=Roles,DC=PORTAL,DC=COMPANY,DC=BE";
        boolean isDisabled = false;

        try {

            System.out.println("Creating initial directory context...");
            LdapContext ctx = new InitialLdapContext(X_Ldap.getEnvironment(),
                    null);

            Attributes attrs = new BasicAttributes(true);

            attrs.put("objectClass", "user");
            attrs.put("cn", "Albert Einstein");

String newQuotedPassword = "\"Pass123\"";
            byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");
            attrs.put(new BasicAttribute("unicodePwd", newUnicodePassword));

            attrs.put(new BasicAttribute("msDS-UserAccountDisabled", "FALSE"));

            System.out.println("Creating context...");
            Context result = ctx.createSubcontext(userName, attrs);
            System.out.println("Created account for: " + userName);

            System.out.println("Creating context...");
            Context result = ctx.createSubcontext(userName, attrs);
            System.out.println("Created account for: " + userName);

            try {
                ModificationItem member[] = new ModificationItem[1];
                member[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
                        new BasicAttribute("member", userName));

                ctx.modifyAttributes(groupName, member);
                System.out.println("Added user to group: " + groupName);

            } catch (NamingException e) {
                System.err.println("Problem adding user to group: " + e);
            }

            ctx.close();

            System.out.println("Successfully created User: " + userName);

        } catch (NamingException e) {
            System.err.println("Problem creating object: " + e);
        }

        catch (IOException e) {
            System.err.println("Problem creating object: " + e);
        }
    }

PS: I am using AD LDS for my active directory

Andreas
  • 2,007
  • 5
  • 26
  • 37

1 Answers1

2

Found it: Needed to use DirContext.ADD_ATTRIBUTE instead of DirContext.REPLACE_ATTRIBUTE

Andreas
  • 2,007
  • 5
  • 26
  • 37
  • For more information, see the further discussion at [Replacing the value of a multi-valued attribute](http://ff1959.wordpress.com/2011/07/28/replace-a-value-of-a-multi-valued-attribute/). – Terry Gardner Mar 22 '12 at 16:00
  • How multi-valued is a boolean in AD LDS/Active directory? – Andreas Mar 22 '12 at 16:14
  • 1
    You also needed to define a second member of 'enableUser[]', or reduce its dimension to 1. But I don't understand why you didn't just set all the attributes you wanted before creating the subcontext, instead of creating it and then modifying it. – user207421 Mar 22 '12 at 21:25
  • I can set the password and enable the account with the creation attributes, but adding it to a group only seems to work after I created the user – Andreas Mar 23 '12 at 07:40