The code:
$ctrl1 = array(
// LDAP_SERVER_POLICY_HINTS_OID for Windows 2012 and above
"oid" => "1.2.840.113556.1.4.2239",
"value" => sprintf("%c%c%c%c%c", 48, 3, 2, 1, 1));
$ctrl2 = array(
// LDAP_SERVER_POLICY_HINTS_DEPRECATED_OID for Windows 2008 R2 SP1 and above
"oid" => "1.2.840.113556.1.4.2066",
"value" => sprintf("%c%c%c%c%c", 48, 3, 2, 1, 1));
if (!ldap_set_option($ds, LDAP_OPT_SERVER_CONTROLS, array($ctrl1, $ctrl2))) {
error_log("ERROR: Failed to set server controls");
}
$result = ldap_mod_replace($ds, $dn, $entry);
...
Details:
There are a couple of ways to reset Active Directory passwords using LDAP:- A delete operation on the unicodePwd attribute immediately followed by an add, which is a password change
- A modification of the unicodePwd attribute, which is considered an administrative password reset
- First, the control must be available on the server. It's present in Windows Server 2008 R2 Service Pack 1 and above. It can also be installed in 2008 R2 using this hotfix: http://support.microsoft.com/?id=2386717
- Next, the client must use the control to tell the AD server to enforce password history requirements.
I put both controls (the new one as well as the deprecated one) in my code and also didn't set the iscritical flag (which defaults to false) in order to make the code as flexible as possible.
In case you're looking to implement a solution to reset AD passwords using PHP, you may find these helpful: