LDAP gidNumber 类似自动整数
我还在继续处理LDAP的问题...
今天我想提的难题是:我正在用Python和Django框架在一个LDAP服务器上创建一个posixGroup。我把方法的代码附在下面。
主要的问题是gidNumber是posixGroup类的必填属性,但通常在使用像phpLDAPadmin这样的图形LDAP客户端时,这个字段会自动填充,就像一个自动递增的数字一样。
这里的问题是:gidNumber默认是自动递增的属性,还是仅在使用像上面提到的客户端时才这样?在创建posixGroup时我必须手动指定它吗?
def ldap_cn_entry(self):
import ldap.modlist as modlist
dn = u"cn=myGroupName,ou=plant,dc=ldap,dc=dem2m,dc=it"
# A dict to help build the "body" of the object
attrs = {}
attrs['objectclass'] = ['posixGroup']
attrs['cn'] = 'myGroupName'
attrs['gidNumber'] = '508'
# Convert our dict to nice syntax for the add-function using modlist-module
ldif = modlist.addModlist(attrs)
# Do the actual synchronous add-operation to the ldapserver
self.connector.add_s(dn, ldif)
- 在这个方法所在的类的构造函数中,首先实例化了连接器。构造函数还负责LDAP的初始化和绑定。然后,连接会在析构函数中关闭。
- 使用这个方法时,我先实例化它所属的类,这样它就会连接到LDAP服务器。接着我使用这个方法,最后销毁之前实例化的对象以关闭连接。如果我按照这个流程创建其他条目,或者手动指定gidNumber,所有操作都是正常的。
问题是我不能每次创建组时都手动指定gidNumber来达到我的目的。我应该让它自动填充(如果可能的话),或者考虑其他方法来完成这个操作。
我不想发更多关于我写的类的代码,以免页面显得拥挤。
如果需要更多信息,我会提供的。谢谢。
2 个回答
根据@jeemster的建议,我找到了管理gidNumber的方法。
首先,我在我的LDAP中创建了一个新的条目,叫做“gidNumber”,并添加了一个可选的属性description,用来记录我最后使用的gidNumber(类别:组织单位,ou:gidNumber,描述:500)。
接着,我创建了以下几个函数:
def ldap_gid_finder(self):
# Locates the suport-entry with a simple query
self.baseDN = "ou=impianti,dc=ldap,dc=dem2m,dc=it"
self.searchScope = ldap.SCOPE_SUBTREE
self.retrieveAttributes = None
self.searchFilter = "ou=*gidNumber*"
# Results are putted in a dictionary
self.ldap_result = self.connector.search(
self.baseDN, self.searchScope, self.searchFilter, self.retrieveAttributes)
result_set = []
while 1:
result_type, result_data = self.connector.result(self.ldap_result, 0)
if (result_data == []):
break
else:
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)
# The attribute containing gidNumber is passed to an instanced variable
self.actual_gid_number = int(result_set[0][0][1]['description'][0])
# Provides to gidNumber incrementation
def ldap_gid_increment(self):
dn = "ou=gidNumber,ou=impianti,dc=ldap,dc=dem2m,dc=it"
old = {'description': str(self.actual_gid_number)}
new = {'description': str(self.actual_gid_number + 1)}
ldif = modlist.modifyModlist(old,new)
self.connector.modify_s(dn, ldif)
正如我之前所说,这些方法是在一个类中定义的,我重写了构造函数和析构函数,以便在我创建或删除实例时,能够自动连接或断开与LDAP服务器的连接。
然后,我在LDAP上执行了一个查询,找到了之前创建的名为gidNumber的对象,并用查询结果填充了一个字典。在这个字典中,我找到了表示gidNumber的变量,并进行了整数转换,以便对它进行操作和增加。就这样。
这个过程非常高效,因为服务器重启时,你不会丢失gidNumber的信息!再次感谢你,jeemster。
LDAP协议没有自动生成整数的方法。
在创建条目时,你需要手动指定这个值。
不过,有一些小技巧可以帮助你。我们通常会把最后使用的值放在一个组织单位(OU)上(我们在OU上添加一个带有自定义属性的辅助类),然后读取这个值,增加1,再使用这个值作为gidNumber。