簡體   English   中英

使用ldap python更新Active Directory密碼

[英]Update Active Directory Password using ldap python

基本上是嘗試使用LDAP python重置用戶密碼。 我在這里瀏覽過各種文章,但沒有運氣:(。

嘗試使用:

  • a) Modify_s() -每次都返回“沒有這樣的對象”。 嘗試使用其他用戶DN。

    {'info':“ 0000208D:NameErr:DSID-0310020A,問題2001(NO_OBJECT),數據0,最佳匹配項:\\ n \\ t'DC = mydomain,DC = com'\\ n”,“ matched”:“ DC = mydomain,DC = com','desc':'沒有這樣的對象'}

    這是代碼段:

     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和Microsoft Active Directory:連接並刪除用戶

如何設置Active Directory用戶的鎖定時間和密碼

如何使用Python更改域用戶(Windows Active Directory)的密碼?

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/

我認為下面的程序對您有所幫助。Windows活動目錄使用密碼屬性作為Unicode方法https://technet.microsoft.com/zh-cn/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('Administrator@exam.local', 'p@ssw0rd1') 
dn="cn=map6,ou=Police,dc=exam,dc=local" 
new_password='p@ssw0rd3'
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()      

從我可以看到的是,您的user_dn設置不正確。 仔細檢查並確保目錄服務器中實際存在完整的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'}

我遇到了同樣的問題,並決定詢問服務器故障。 我得到的答案幫助我找出了代碼中的錯誤。 總而言之,有兩種更新AD密碼的方法:一種用於普通用戶更新自己的密碼,另一種用於管理員(或具有足夠訪問權限的帳戶)為另一用戶重置密碼。

方法1:用戶更新自己的密碼

ad_server = "ldaps://ad.xxx_domain.com"
ad_dn = "CN={0},OU=Users,OU=AF,DC=xxx_domain,DC=com"

username = 'my_username'
old_pwd = 'the_old_pa55word'
new_pwd = 'the_new_pa55word'

cert = os.path.join('/path', "to", 'server_cert.cer')

# LDAP connection initialization
l = ldap.initialize(ad_server)
# Set LDAP protocol version used
l.protocol_version = ldap.VERSION3
# Force cert validation
l.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_DEMAND)
# Set path name of file containing all trusted CA certificates
l.set_option(ldap.OPT_X_TLS_CACERTFILE, cert)
# Force libldap to create a new SSL context (must be last TLS option!)
l.set_option(ldap.OPT_X_TLS_NEWCTX, 0)

# Bind
l.simple_bind_s(ad_dn.format(username), old_pwd)

# Now, perform the password update
oldpwd_utf16 = '"{0}"'.format(old_pwd).encode('utf-16-le')
newpwd_utf16 = '"{0}"'.format(new_pwd).encode('utf-16-le')
mod_list = [
    (ldap.MOD_DELETE, "unicodePwd", oldpwd_utf16),
    (ldap.MOD_ADD, "unicodePwd", newpwd_utf16),
]
l.modify_s(ad_dn.format(username), mod_list)

方法2:管理員帳戶更新普通用戶的密碼

ad_server = "ldaps://ad.xxx_domain.com"
ad_dn = "CN={0},OU=Users,OU=AF,DC=xxx_domain,DC=com"

admin_username = "i_am_the_admin"
admin_password = "admin123"

username = 'my_username'
new_pwd = 'the_new_complicated_password'

cert = os.path.join('/path', "to", 'server_cert.cer')

# LDAP connection initialization
l = ldap.initialize(ad_server)
# Set LDAP protocol version used
l.protocol_version = ldap.VERSION3
# Force cert validation
l.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_DEMAND)
# Set path name of file containing all trusted CA certificates
l.set_option(ldap.OPT_X_TLS_CACERTFILE, cert)
# Force libldap to create a new SSL context (must be last TLS option!)
l.set_option(ldap.OPT_X_TLS_NEWCTX, 0)

# Bind (as admin user)
l.simple_bind_s(ad_dn.format(admin_username), admin_password)

# Now, perform the password update
newpwd_utf16 = '"{0}"'.format(new_pwd).encode('utf-16-le')
mod_list = [
    (ldap.MOD_REPLACE, "unicodePwd", newpwd_utf16),
]
l.modify_s(ad_dn.format(username), mod_list)

請注意,第二種方法需要與其他帳戶綁定(具有足夠的權限),但允許設置新密碼而無需重新鍵入舊密碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM