Skip to content

Instantly share code, notes, and snippets.

@artschwagerb
Created January 28, 2015 18:25
Show Gist options
  • Save artschwagerb/35130c93b276d6aa2b05 to your computer and use it in GitHub Desktop.
Save artschwagerb/35130c93b276d6aa2b05 to your computer and use it in GitHub Desktop.
Python LDAP - Change Password
def changePassword(user_dn, old_password, new_password):
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
l = ldap.initialize("LDAPS://DOMAIN.COM")
l.set_option(ldap.OPT_REFERRALS,0)
l.set_option(ldap.OPT_PROTOCOL_VERSION,3)
l.set_option(ldap.OPT_X_TLS,ldap.OPT_X_TLS_DEMAND)
l.set_option(ldap.OPT_X_TLS_DEMAND,True)
l.set_option(ldap.OPT_DEBUG_LEVEL,255)
l.simple_bind_s("[email protected]", "PASSWORD")
# Reset Password
unicode_pass = unicode('\"' + str(new_password) + '\"', 'iso-8859-1')
password_value = unicode_pass.encode('utf-16-le')
add_pass = [(ldap.MOD_REPLACE, 'unicodePwd', [password_value])]
l.modify_s(user_dn,add_pass)
# Its nice to the server to disconnect and free resources when done
l.unbind_s()
@dwade0o
Copy link

dwade0o commented Nov 7, 2017

Is user_dn meaning username?

@thanonoc
Copy link

thanonoc commented Dec 6, 2017

@dwade0o ; I think user_dn is not a username because username it came from sAMAccountName. So user_dn is distinguishedName of entry

@prashantksharma
Copy link

prashantksharma commented Mar 6, 2018

@artschwagerb: can you please provide an explanation, on how it works. I am able to change LDAP password using "ldappasswd" (using ldap-utils). I want to do the same using python function. I am unable to understand your code. Useful pointers would be helpful. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment