Created
January 28, 2015 18:25
-
-
Save artschwagerb/35130c93b276d6aa2b05 to your computer and use it in GitHub Desktop.
Python LDAP - Change Password
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
@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
@dwade0o ; I think user_dn is not a username because username it came from sAMAccountName. So user_dn is distinguishedName of entry