2016-07-27 434 views
0

基本上試圖用LDAP python重置用戶的密碼。我已經通過各個崗位了這裏,但沒有運氣:(使用ldap更新Active Directory密碼python

嘗試使用:

  • 一)modify_s() - 返回 「沒有這樣的對象」 每次。試用不同的用戶DN。

    {'info':「0000208D:NameErr:DSID-0310020A,問題2001(NO_OBJECT),數據0,最佳匹配:\ n \ t'DC = mydomain,DC = com'\ n」,'匹配「: 'DC = MYDOMAIN,DC = COM', '降序': '沒有這樣的對象'}

    下面是代碼段:

    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' 
    
  • b)中passwd(userEmail, oldPassword, newPassword)。它得到很好的執行,但密碼沒有更新。

需要幫助確定問題。

參考鏈接: Python+LDAP+SSL

python-ldap and Microsoft Active Directory: connect and delete user

how to set lockoutTime and password of a user of Active Directory

How can I change password for domain user(windows Active Directory) using Python?

https://groups.google.com/forum/#!topic/macromedia.coldfusion.security/Rq7xx15OeBs

http://www.grotan.com/ldap/python-ldap-samples.html#add

http://marcitland.blogspot.in/2011/02/python-active-directory-linux.html

https://snipt.net/Fotinakis/change-active-directory-password-via-ldap-modify-call/

回答

0

我認爲下面的程序對你有幫助.. Windows活動目錄使用密碼屬性爲Unicode方法 https://technet.microsoft.com/en-us/magazine/ff848710.aspx

import ldap 
import ldap.modlist as modlist 
import base64 
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) 
l = ldap.initialize('ldaps://exam.local') 
l.simple_bind_s('[email protected]', '[email protected]') 
dn="cn=map6,ou=Police,dc=exam,dc=local" 
new_password='[email protected]' 
unicode_pass = unicode('\"' + new_password + '\"', 'iso-8859-1') 
print (unicode_pass) 
password_value = unicode_pass.encode('utf-16-le') 
add_pass = [(ldap.MOD_REPLACE, 'unicodePwd', [password_value])] 
print (password_value) 
l.modify_s(dn, add_pass) 
l.modify_s(dn, add_pass) 
l.unbind_s()  
0

從我所看到的是您user_dn未正確設置。請仔細檢查並確保您在Directory Server中實際存在完整的DN。檢查您的用戶名變量是否被正確解析(無換行符或製表符),並驗證了基本DN。

sys.stderr.write('Error setting AD password for: ' + username + '\n') 
sys.stderr.write('DN: ' + user_dn + '\n') 
sys.stderr.write('Message: ' + str(e) + '\n') 

該錯誤消息是很清楚,AD找不到該對象(DN)它希望修改**(NO_OBJECT)

{'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'}