Mohammad Amir Mohammad Amir - 1 month ago 11
Java Question

how to create active directory users with accountExpires attribute from java

I want to create an AD user with accountExpires attribute. I am doing like this

public boolean addUser(
String firstName,
String lastName,
String userName,
String password,
String organisationUnit) throws NamingException {
if (findUser(userName, firstName, lastName, organisationUnit)) {
return false;
} else {
// Create a container set of attributes
BasicAttributes container = new BasicAttributes();

// Create the objectclass to add
Attribute objClasses = new BasicAttribute("objectClass");
objClasses.add("top");
objClasses.add("person");
objClasses.add("organizationalPerson");
objClasses.add("user");

// Assign the username, first name, and last name
String cnValue = new StringBuffer(firstName).append(" ").append(lastName).toString();
Attribute cn = new BasicAttribute("cn", cnValue);
Attribute sAMAccountName = new BasicAttribute("sAMAccountName", userName);
Attribute mac = new BasicAttribute("msNPCallingStationID", "ab-ab-ab-b7-6t");
Attribute principalName = new BasicAttribute("userPrincipalName", userName + "@atamunet.com");
Attribute givenName = new BasicAttribute("givenName", firstName);
Attribute sn = new BasicAttribute("sn", lastName);
Attribute uid = new BasicAttribute("uid", userName);
Attribute fullName = new BasicAttribute("displayName", "fullName");
Attribute gender = new BasicAttribute("initials", "gender");
Attribute dob = new BasicAttribute("description", "dob");
Attribute FatherName = new BasicAttribute("physicalDeliveryOfficeName", "FatherName");
Attribute Email = new BasicAttribute("mail", "Email");
Attribute mobile = new BasicAttribute("mobile", "mobile");
Attribute department = new BasicAttribute("department", "department");
Attribute HallName = new BasicAttribute("streetAddress", "HallName");
Attribute FacultyName = new BasicAttribute("company", "FacultyName");
Attribute CourseName = new BasicAttribute("title", "CourseName");

Attribute accountExpires = new BasicAttribute("accountExpires", new Date());

//some useful constants from lmaccess.h
int UF_ACCOUNTENABLE = 0x0001;
//int UF_ACCOUNTDISABLE = 0x0002;
int UF_PASSWD_NOTREQD = 0x0020;
int UF_PASSWD_CANT_CHANGE = 0x0040;
int UF_NORMAL_ACCOUNT = 0x0200;
int UF_DONT_EXPIRE_PASSWD = 0x10000;
//int UF_PASSWORD_EXPIRED = 0x800000;

Attribute enabled = new BasicAttribute("userAccountControl", Integer.toString(UF_NORMAL_ACCOUNT + UF_PASSWD_NOTREQD + UF_DONT_EXPIRE_PASSWD + UF_ACCOUNTENABLE));
// Add password
Attribute userPassword = new BasicAttribute("userpassword", password);

// Add these to the container
container.put(objClasses);
container.put(sAMAccountName);
container.put(principalName);
container.put(cn);
container.put(sn);
container.put(givenName);
container.put(uid);
container.put(userPassword);
container.put(mac);
container.put(gender);
container.put(dob);
container.put(FatherName);
container.put(Email);
container.put(mobile);
container.put(department);
container.put(HallName);
container.put(FacultyName);
container.put(CourseName);
container.put(fullName);
container.put(enabled);
container.put(accountExpires);

// Create the entry
try {
ctx.createSubcontext(getUserDN(cnValue, organisationUnit), container);
return true;
} catch (Exception e) {
System.out.println(e.getMessage() + "add");
return false;
}
}
}


How can I add user with accountExpires attribute. Is there anybody can help me. Without this line

Attribute accountExpires = new BasicAttribute("accountExpires", new Date());

Everything goes fine but I want the expiry date as well.

Answer

Thanks for your help what gave me the idea and this code worked for me

/**
     * Difference between Filetime epoch and Unix epoch (in ms).
     */
    private static final long FILETIME_EPOCH_DIFF = 11644473600000L;

    /**
     * One millisecond expressed in units of 100s of nanoseconds.
     */
    private static final long FILETIME_ONE_MILLISECOND = 10 * 1000;

    public static long filetimeToMillis(final long filetime) {
        return (filetime / FILETIME_ONE_MILLISECOND) - FILETIME_EPOCH_DIFF;
    }

    public static long millisToFiletime(final long millis) {
        return (millis + FILETIME_EPOCH_DIFF) * FILETIME_ONE_MILLISECOND;
    } 

SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
            String dateInString = "01-07-2017 10:20:56";
            Date date = sdf.parse(dateInString);
            final long dd = date.getTime();
            Attribute accountExpires = new BasicAttribute("accountExpires", Long.toString(millisToFiletime(dd)));
Comments