簡體   English   中英

python-ldap和Microsoft Active Directory:連接並刪除用戶

[英]python-ldap and Microsoft Active Directory: connect and delete user

python-ldap newb在這里。 我正在嘗試使用以下示例代碼來做到這一點:

import ldap

## first you must bind so we're doing a simple bind first
try:
l = ldap.open("valid ip")
l.set_option(ldap.OPT_REFERRALS, 0)

l.protocol_version = ldap.VERSION3  
# Pass in a valid username and password to get 
# privileged directory access.
# If you leave them as empty strings or pass an invalid value
# you will still bind to the server but with limited privileges.

username = "cn=administrator, o=joe.local"
password  = "password"

# Any errors will throw an ldap.LDAPError exception 
# or related exception so you can ignore the result
l.simple_bind(username, password)
      except ldap.LDAPError, e:
print e
# handle error however you like


      # The next lines will also need to be changed to support your requirements and directory
      deleteDN = "uid=hihihi, ou=LoginUsers,o=joe.local"
      try:
# you can safely ignore the results returned as an exception 
# will be raised if the delete doesn't work.
l.delete_s(deleteDN)
      except ldap.LDAPError, e:
print e
## handle error however you like

我收到各種錯誤:

使用虛擬機IP:

{'info': '000004DC: LdapErr: DSID-0C0909A2, comment: In order to perform this op
eration a successful bind must be completed on the connection., data 0, v1db1',
'desc': 'Operations error'}

使用localhost或127.0.0.1:

{'desc': "Can't contact LDAP server"}
{'desc': "Can't contact LDAP server"}

我看了以下沒有解決的SO帖子:

Python-ldap身份驗證 Python-ldap microsoft

根據文檔 ,不建議使用ldap.open 您應該嘗試ldap.initialize ,就像您提供的兩個鏈接一樣。 另外,請確保您的專有名稱中沒有空格: "cn=administrator, o=joe.local"

如果那不能解決問題,請確保提及錯誤出自哪一行。

您使用的是哪個版本的python? 代碼很舊。 現在打開是初始化的,不要使用simple_bind,請使用simple_bind_s。

如果要在AD中進行諸如刪除,更改密碼之類的操作,則必須首先配置TLS連接。 http://araihan.wordpress.com/2009/10/05/windows-server-2008-active-directory-certificate-services-ad-cs/

這是成功的連接。

import ldap

LDAP_SERVER_EMG = "ldaps://192.168.0.250"
BIND_DN = "Administrador@emgS.local"
BIND_PASS = "xxxXXXxxxXXXxxx"
USER_BASE = "dc=emgS,dc=local"
try:
   ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, 0)
   lcon_emg = ldap.initialize(LDAP_SERVER_EMG)
   lcon_emg.simple_bind_s(BIND_DN, BIND_PASS)
except ldap.LDAPError, e:
   print e

然后,您可以刪除和更改用戶密碼。

lcon_emg.passwd_s,不能使用。 您需要簡單的change de unicodepwd屬性來更改Active Directory中的用戶密碼。

#firs is a good practice to create a dict of all atributes of the user
ad_u = {
        'objectClass': ['top', 'person', 'organizationalPerson', 'user'],  
        'cn': 'User gecos or name',
       'displayName': 'User gecos or name',
       'User Gecos or Name',
       'distinguishedName': 'user distin name',
       'givenName': 'First name i guest',
       'sAMAccountName': 'user_login_name',
       'sn': 'middle name i guest',
        #USER PRIVILEGE, SEE THE DOCUMENTATION OF AD FOR MORE INFORMATION, BECAUSE I DON'T REMEMBER :)
       'userAccountControl': '514',
        #user_login_name, with domain extension
       'userPrincipalName': '%s@emg.local' % 'user_login_name',
       'mail': 'user_login_name@emaildomainorwhatever',
       'employeeID': 'unique_user_number'
       }
mods = ldap.modlist.addModlist(ad_u)

try:
   lcon_emg.add_s(ad_u.get('distinguishedName'),
                  mods)
except Exception, e:
   response.update({'error_ad': 'ActiveD: Error to add user %s' % str(e)})
else:
   response.update({'success_ad': 'ActiveD: Success add user'})

#HERE YOU MAKE THE utf-16-le encode password
unicode_pass = unicode('\"' + kwargs.get('cclara') + '\"', 'iso-8859-1')
password_value = unicode_pass.encode('utf-16-le')
#just change the atribute in the entry you just create
add_pass = [(ldap.MOD_REPLACE, 'unicodePwd', [password_value])]

# 512 will set user account to enabled
#change the user to enabled
mod_acct = [(ldap.MOD_REPLACE, 'userAccountControl', '512')]

try:
    lcon_emg.modify_s(ad_u.get('distinguishedName'), add_pass)
except ldap.LDAPError, error_message:
    response.update({'error_ad_clave': 'ActiveD: Error to gen the pass %s' % str(error_message)})
else:
    response.update({'success_ad_clave': 'ActiveD: Success gen pass'})

try:
    lcon_emg.modify_s(ad_u.get('distinguishedName'), mod_acct)
except ldap.LDAPError, error_message:
    response.update({'error_ad_hab': 'Error to enable user %s' % str(error_message)})
else:
    response.update({'success_ad_hab': 'SUccess enable user'})
lcon_emg.unbind_s()

如果要稍后更改密碼。

pad = ('"%s"' % password).encode("utf-16-le")

try:
   mod_attrs = [(ldap.MOD_REPLACE, 'unicodePwd', pad),
                (ldap.MOD_REPLACE,'unicodePwd',pad)]
   lcon_emg.modify_s(rdnad, mod_attrs)
except Exception, e:
     response.update({'error_ad': 'No se pudo cambiar la clave %s' % str(e)})
else:
     response.update({'success_ad': 'Cambio exito en Active Directory'})

希望這個答案對您有幫助

暫無
暫無

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

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