kartu

LDAP - AD password update

Jul 26th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. def changePassword(userEmail, oldPassword, newPassword):
  2.     try:
  3.         ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
  4.        
  5.         ldap_client = ldap.initialize("ldap://127.0.01.1:389")
  6.         ldap_client.set_option(ldap.OPT_REFERRALS, 0)
  7.         ldap_client.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
  8.         ldap_client.set_option(ldap.OPT_X_TLS,ldap.OPT_X_TLS_DEMAND)
  9.         ldap_client.set_option( ldap.OPT_X_TLS_DEMAND, True )
  10.         ldap_client.set_option( ldap.OPT_DEBUG_LEVEL, 255 )
  11.         ldap_client.simple_bind_s(ADMIN_EMAIL, ADMIN_PASSWORD)
  12.        
  13.         # Set AD password
  14.         #unicode_pass = unicode('\"' + newPassword + '\"', "iso-8859-1")
  15.         unicode_pass = newPassword
  16.         password_value = unicode_pass.encode("utf-16-le")
  17.         add_pass = [(ldap.MOD_REPLACE, 'unicodePwd', [password_value]),( ldap.MOD_REPLACE, 'unicodePwd', [password_value])]
  18.  
  19.         # Replace password
  20.         try:
  21.           user_dn = 'CN=%s,DC=mydomain,DC=com' % username
  22.           ldap_client.modify_s(user_dn, add_pass)
  23.           print "Active Directory password for", username, \
  24.                 "was set successfully!"
  25.         except ldap.LDAPError, e:
  26.           sys.stderr.write('Error setting AD password for: ' + username + '\n')
  27.           sys.stderr.write('Message: ' + str(e) + '\n')
  28.           ldap_client.unbind_s()
  29.           return 'SOME_PROBLEM'
  30.         ldap_client.unbind_s()
  31.         return 'AUTHENTICATED'
  32.     except ldap.INVALID_CREDENTIALS:
  33.         ldap_client.unbind()
  34.         return 'INVALID_CREDENTIALS'
  35.     except ldap.SERVER_DOWN:
  36.         return 'SERVER_UNAVAILABLE'
Add Comment
Please, Sign In to add comment