LDAP/AD是兩種應(yīng)用最廣泛的認(rèn)證服務(wù)器,AD是微軟基于LDAP開發(fā)而成的,應(yīng)用于Windows平臺,而LDAP主要應(yīng)用于Linux平臺(LDAP用在Windows平臺比較少)。既然AD是基于LDAP的擴展,則LDAP大部分協(xié)議,AD均可原生支持,這位我們操作和管理AD認(rèn)證服務(wù)器提供了大大的便利。
成都創(chuàng)新互聯(lián)專注于網(wǎng)站建設(shè),為客戶提供成都網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)頁設(shè)計開發(fā)服務(wù),多年建網(wǎng)站服務(wù)經(jīng)驗,各類網(wǎng)站都可以開發(fā),品牌網(wǎng)站建設(shè),公司官網(wǎng),公司展示網(wǎng)站,網(wǎng)站設(shè)計,建網(wǎng)站費用,建網(wǎng)站多少錢,價格優(yōu)惠,收費合理。
在軟件開發(fā)過程中,很多公司都采用AD/LDAP用于自己的用戶認(rèn)證體系,本文重點研究通過Python語言提供的Python-Ldap框架,來操作和管理AD/LDAP中的用戶,組織結(jié)構(gòu)等,希望對大家有所幫助。
基本概念:
o– organization(組織-公司)
ou – organization unit(組織單元/部門)
c - countryName(國家)
dc - domainComponent(域名組件)
sn – suer name(真實名稱)
cn - common name(常用名稱)
dn - distinguished name(唯一標(biāo)識)
AD和LDAP中的字段及含義:
字段描述 表示值
唯一標(biāo)識 dn
用戶名 userPrincipalName(AD)/cn(LDAP)
密碼 userPassword
真實姓名 displayName
工作地點 physicalDeliveryOfficeName
職務(wù) title
郵箱 mail
個人電話 telephoneNumber
公司電話 homePhone
字段描述 表示值
唯一標(biāo)識 dn
組織名稱 ou
組織描述 description
import ldap
def create_ad_user(username, unicode_password, org_dn):
l = ldap.initialize('ldap://172.16.1.163:636') #use secure port default:636
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS, 0)
l.simple_bind_s('Administrator', 'P@ssword')
user = {}
user['objectclass'] = ['top', 'person', 'organizationalPerson', 'user']
user_dn = 'cn=%s,%s' % (username,org_dn)
user['userPrincipalName'] = '%s@%s' % (username, domain)
user['userAccountControl'] = '66048' # active user account
user['unicodePwd'] = unicode_password
ldif = modlist.addModlist(user)
ret, _ = l.add_s(user_dn, ldif)
print ret
import ldap
def create_ldap_user(username, password, org_dn):
l = ldap.initialize('ldap://172.16.1.163:389')
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS, 0)
l.simple_bind_s('Administrator', 'P@ssword')
user = {}
user['objectclass'] = ['top', 'person', 'inetOrgPerson']
user['cn'] = username
user['sn'] = user['cn']
user['password'] = password
user_dn = 'cn=%s,%s' % (username,org_dn)
ldif = modlist.addModlist(user)
ret, _ = l.add_s(user_dn, ldif)
print ret
import ldap
def modify_user(username):
firstname = 'Abel'
lastname = 'Lee'
l = ldap.initialize('ldap://172.16.1.163:389')
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS, 0)
l.simple_bind_s('Administrator', 'P@ssword')
cn = username
dn = 'cn=%s,ou=org1,dc=testad,dc=com' % cn
old = {'description': 'old description'}
new = {'description': 'new description'}
ldif = ldap.modifyModlist(old, new)
ret = l.modify_s(dn, ldif)
l.unbind_s()
print ret
import ldap
def delete_users(user_dn):
l = ldap.initialize('ldap://172.16.1.163:389')
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS, 0)
l.simple_bind_s('Administrator', 'P@ssword')
ret = l.delete_s(user_dn)
l.unbind_s()
print ret
import ldap
def describe_ad_users(org_dn='', usernames = []):
l = ldap.initialize('ldap://172.16.1.163:389')
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS, 0)
l.simple_bind_s('Administrator', 'P@ssword')
USER_ATTRS = ['userAccountControl','displayName','description','homePhone','physicalDeliveryOfficeName','title','mail','telephoneNumber']
filterstr = '(&(objectclass=user)'
if len(usernames) > 0:
filterstr = filterstr + '(|'
for username in usernames:
username = '%s@%s' % (username, domain)
userPrincipalName = '(userPrincipalName=%s)' % username
filterstr += userPrincipalName
if len(usernames) > 0:
filterstr += '))'
else:
filterstr += ')'
if org_dn:
ret = l.search_s(org_dn, ldap.SCOPE_SUBTREE, filterstr,
attrlist=USER_ATTRS)
else:
ret = l.search_s(base_dn, ldap.SCOPE_SUBTREE, filterstr,
attrlist=USER_ATTRS)
print ret
import ldap
def describe_ldap_users(org_dn='', usernames = []):
l = ldap.initialize('ldap://172.16.1.163:389')
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS, 0)
l.simple_bind_s('Administrator', 'P@ssword')
USER_ATTRS = ['userAccountControl','displayName','description','homePhone','physicalDeliveryOfficeName','title','mail','telephoneNumber']
filterstr = '(&(objectclass=person)'
if len(usernames) > 0:
filterstr = filterstr + '(|'
for cn in usernames:
cn = '(cn=%s)' % cn
filterstr += cn
if len(usernames) > 0:
filterstr += '))'
else:
filterstr += ')'
if org_dn:
ret = l.search_s(org_dn, ldap.SCOPE_SUBTREE, filterstr,
attrlist=USER_ATTRS)
else:
ret = l.search_s(base_dn, ldap.SCOPE_SUBTREE, filterstr,
attrlist=USER_ATTRS)
print ret
import ldap
def login_ad(user_dn, password):
l = ldap.initialize('ldap://172.16.1.163:389')
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS, 0)
l.simple_bind_s(user_dn, password)
cn = user_dn.split(',')[0].split('=')
base_dn = 'dc=testad,dc=com'
domain = 'testad.com'
username = '%s@%s' % (cn[1], domain)
ret = l.search_s(base_dn, ldap.SCOPE_SUBTREE,"(userPrincipalName=%s)" % username, ["userPrincipalName"])
if ret is None or len(ret) == 0:
return False
return True
import ldap
def login_ldap(user_dn, password):
l = ldap.initialize('ldap://172.16.1.163:389')
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS, 0)
l.simple_bind_s(user_dn, password)
cn = user_dn.split(',')[0].split('=')
base_dn = 'dc=testad,dc=com'
ret = l.search_s(base_dn, ldap.SCOPE_SUBTREE,"%s=%s" % (cn[0], cn[1]))
if ret is None or len(ret) == 0:
return False
return True
import ldap
def set_ad_password(user_dn, unicode_password):
l = ldap.initialize('ldap://172.16.1.163:636') #use secure port
l.simple_bind_s('Administrator', 'P@ssword')
param_pwd = [(ldap.MOD_REPLACE, 'unicodePwd', [password_utf16]), (ldap.MOD_REPLACE, 'unicodePwd', [password_utf16])]
ret,_ = l.modify_s(user_dn, param_pwd)
print ret
import ldap
def set_ldap_password(user_dn, password):
l = ldap.initialize('ldap://172.16.1.163:389')
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS, 0)
l.simple_bind_s('Administrator', 'P@ssword')
l.passwd_s(user_dn, None, password)
import ldap
def modify_ldap_password(user_dn, old_password, new_password):
l = ldap.initialize('ldap://172.16.1.163:389')
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS, 0)
l.simple_bind_s('Administrator', 'P@ssword')
l.passwd_s(user_dn, old_password, new_password)
import ldap
def create_ou(parent_dn, ou):
l = ldap.initialize('ldap://172.16.1.163:389')
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS, 0)
l.simple_bind_s('Administrator', 'P@ssword')
attrs= {'ou': ou}
attrs['description'] = 'this is description'
attrs['objectClass'] = ['organizationalUnit','top']
dn = 'ou=%s,%s' % (attrs['ou'], parent_dn)
ldif = modlist.addModlist(attrs)
ret, _ = l.add_s(dn,ldif)
print ret
import ldap
def modify_ou(attrs={'description': 'new_description'}):
l = ldap.initialize('ldap://172.16.1.163:389')
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS, 0)
l.simple_bind_s('Administrator', 'P@ssword')
old_attrs = {'description': 'old_description'}
ldif = modlist.modifyModlist(old_attrs, attrs)
l.modify_s(dn,ldif)
import ldap
def delete_ou(dn):
l = ldap.initialize('ldap://172.16.1.163:389')
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS, 0)
l.simple_bind_s('Administrator', 'P@ssword')
l.delete_s(dn)
import ldap
def describe_ou(parent_dn='', org_dns=[]):
ORGANIZATION_ATTRS = ['ou', 'description']
l = ldap.initialize('ldap://172.16.1.163:389')
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS, 0)
l.simple_bind_s('Administrator', 'P@ssword')
filterstr = '(&(objectclass=organizationalUnit)'
for dn in org_dns:
objectGUID = '(ou=%s)' % dn
filterstr += objectGUID
filterstr += ')'
if parent_dn:
ret = l.search_s(parent_dn, ldap.SCOPE_SUBTREE, filterstr,
attrlist=ORGANIZATION_ATTRS)
else:
ret = l.search_s(base_dn, ldap.SCOPE_SUBTREE, filterstr,
attrlist=ORGANIZATION_ATTRS)
print ret
import ldap
def change_user_in_ou(user_dn, new_org_dn):
l = ldap.initialize('ldap://172.16.1.163:389')
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS, 0)
l.simple_bind_s('Administrator', 'P@ssword')
cn = user_dn.split(',')[0]
ret = l.rename_s(user_dn, cn, new_org_dn)
print ret
注意:AD和LDAP中如:創(chuàng)建用戶,查詢用戶等操作,其使用端口和查詢字段均有差異,還請格外注意,另外,代碼如有不明確指出,歡迎留言討論。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
文章名稱:通過python-ldap操作管理AD/LDAP用戶及組織結(jié)構(gòu)-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://muchs.cn/article0/iopoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站設(shè)計、關(guān)鍵詞優(yōu)化、企業(yè)網(wǎng)站制作、面包屑導(dǎo)航、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容