Categories
Uncategorized

PHP change password Active Directory (AD)

You need

  1. login – password to connect AD
  2. xampp
  3. Self-signed x509 CA
  4. some example php code

Configuration for XAMPP

php.ini

extension=php_ldap.dll
extension=php_openssl.dll

[openssl]
openssl.capath=c:\openldap

Self-Signed x509 CA

visit http://www.samltool.com/self_signed_certs.php

Create some folder and file

  • folder: c:\openldap\sysconf
  • file: c:\openldap\sysconf\openldap.conf

openldap.conf

BASE dc=exampledomain,dc=com
TLS_REQCERT never
TLS_CACERT c:\openldap\ca.pem

RESTART XAMPP

Example of PHP Code

$ldap_server = "ldaps://10.10.10.1";
$ldap_login = "[email protected]";
$ldap_pass = 'examplepassword';
ldap_connect($ldap_server);
$ad = ldap_connect($ldap_server);
ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3) ;
ldap_set_option($ad, LDAP_OPT_REFERRALS, 0);
$bind = ldap_bind($ad, $ldap_login, $ldap_pass);
# SEARCH for RS
$rs = ldap_search($ad,'DC=rd,DC=go,DC=th',"(&(objectCategory=person)(objectClass=user)(sAMAccountName=*$username))");
$rs_first = ldap_first_entry($ad, $rs);
$user_dn = ldap_get_dn($ad, $rs_first);
$password = 'examplenewpasword';
$newpassword = '"'.$password.'"';
$newpassword =iconv('UTF-8','UTF-16LE',$newpassword );
$entry = array();
$entry["unicodePwd"] = $newpassword;
$rs = ldap_modify($ad, $user_dn, $entry);
# DONE