Update Active Directory Password using ldap python
<p>Basically trying to reset the user's password using LDAP python. I've gone through various posts here but no luck :(. </p>

<p>Tried using :</p>

<ul>
<li><p>a) <strong>modify_s()</strong> - returns "No such object" every time. Tried with different user DN.</p>

<p>{'info': "0000208D: NameErr: DSID-0310020A, problem 2001 (NO_OBJECT), data 0, best match of:\n\t'DC=mydomain,DC=com'\n", 'matched': 'DC=mydomain,DC=com', 'desc': 'No such object'}</p>

<p>Here is the code Snippet:</p>

<pre><code>def changePassword(userEmail, oldPassword, newPassword):
 try:
    ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)

    ldap_client = ldap.initialize("ldap://127.0.01.1:389")
    ldap_client.set_option(ldap.OPT_REFERRALS, 0)
    ldap_client.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
    ldap_client.set_option(ldap.OPT_X_TLS,ldap.OPT_X_TLS_DEMAND)
    ldap_client.set_option( ldap.OPT_X_TLS_DEMAND, True )
    ldap_client.set_option( ldap.OPT_DEBUG_LEVEL, 255 )
    ldap_client.simple_bind_s(ADMIN_EMAIL, ADMIN_PASSWORD)

    # Set AD password
    #unicode_pass = unicode('\"' + newPassword + '\"', "iso-8859-1")
    unicode_pass = newPassword
    password_value = unicode_pass.encode("utf-16-le")
    add_pass = [(ldap.MOD_REPLACE, 'unicodePwd', [password_value]),( ldap.MOD_REPLACE, 'unicodePwd', [password_value])]

    # Replace password
    try:
      user_dn = 'CN=%s,DC=mydomain,DC=com' % username
      ldap_client.modify_s(user_dn, add_pass)
      print "Active Directory password for", username, \
            "was set successfully!"
    except ldap.LDAPError, e:
      sys.stderr.write('Error setting AD password for: ' + username + '\n')
      sys.stderr.write('Message: ' + str(e) + '\n')
      ldap_client.unbind_s()
      return 'SOME_PROBLEM'
    ldap_client.unbind_s()
    return 'AUTHENTICATED'
except ldap.INVALID_CREDENTIALS:
    ldap_client.unbind()
    return 'INVALID_CREDENTIALS'
except ldap.SERVER_DOWN:
    return 'SERVER_UNAVAILABLE'
</code></pre></li>
<li><p>b) <strong><code>passwd(userEmail, oldPassword, newPassword)</code></strong>. It gets executed well but password is not updated.</p></li>
</ul>

<p>Need help in identifying the problem. </p>

<p>Reference Links:
<a href="https://stackoverflow.com/questions/7716562/pythonldapssl">Python+LDAP+SSL</a></p>

<p><a href="https://stackoverflow.com/questions/13636117/python-ldap-and-microsoft-active-directory-connect-and-delete-user">python-ldap and Microsoft Active Directory: connect and delete user</a></p>

<p><a href="https://stackoverflow.com/questions/16051839/how-to-set-lockouttime-and-password-of-a-user-of-active-directory">how to set lockoutTime and password of a user of Active Directory</a></p>

<p><a href="https://stackoverflow.com/questions/20875708/how-can-i-change-password-for-domain-userwindows-active-directory-using-python">How can I change password for domain user(windows Active Directory) using Python?</a></p>

<p><a href="https://groups.google.com/forum/#!topic/macromedia.coldfusion.security/Rq7xx15OeBs" rel="nofollow noreferrer">https://groups.google.com/forum/#!topic/macromedia.coldfusion.security/Rq7xx15OeBs</a></p>

<p><a href="http://www.grotan.com/ldap/python-ldap-samples.html#add" rel="nofollow noreferrer">http://www.grotan.com/ldap/python-ldap-samples.html#add</a></p>

<p><a href="http://marcitland.blogspot.in/2011/02/python-active-directory-linux.html" rel="nofollow noreferrer">http://marcitland.blogspot.in/2011/02/python-active-directory-linux.html</a></p>

<p><a href="https://snipt.net/Fotinakis/change-active-directory-password-via-ldap-modify-call/" rel="nofollow noreferrer">https://snipt.net/Fotinakis/change-active-directory-password-via-ldap-modify-call/</a></p>