在Python-LDAP中添加条目

2 投票
1 回答
7358 浏览
提问于 2025-04-18 13:54

我正在尝试用 Python 的 LDAP 库添加条目,但遇到了命名规则错误。我的代码是:

import ldap 
import ldap.modlist as modlist

LOGIN = "" 
PASSWORD = '' 
LDAP_URL = "ldap://127.0.0.1:389" 
user='grant'
l = ldap.initialize(LDAP_URL) 
l.bind(LOGIN, PASSWORD) 
dn="ou=Enki Users,dc=enki,dc=local" 

attrs = {}
attrs['objectclass'] = ['top','organizationalRole','simpleSecurityObject']
attrs['cn'] = 'test'
attrs['userPassword'] = 'test'
attrs['description'] = 'User object for replication using slurpd'

# 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
l.add_s(dn,ldif)

# Its nice to the server to disconnect and free resources when done
l.unbind_s()

错误信息是:

ldap.NAMING_VIOLATION: {'info': "00002099: NameErr: DSID-0305109C, problem 2005 (NAMING_VIOLATION), data 0, best match of:\n\t'dc=enki,dc=local'\n", 'desc': 'Naming violation'}

这段代码可以运行,但用户没有被插入到正确的组织单位。尽管代码能运行,我还是找不到在活动目录中的用户。请帮我找出问题所在。我基本上是在为用户管理制作一个 Django 的网页表单。

import ldap 
import ldap.modlist as modlist

LOGIN = "" 
PASSWORD = '' 
LDAP_URL = "ldap://127.0.0.1:389" 
user='grant'
l = ldap.initialize(LDAP_URL) 
l.bind(LOGIN, PASSWORD) 

dn="cn=test,ou=Enki Users,dc=enki,dc=local" 

attrs = {}
attrs['objectclass'] = ['top','organizationalRole','simpleSecurityObject']
attrs['cn'] = 'test'
attrs['userPassword'] = 'test'
attrs['description'] = 'User object for replication using slurpd'

# 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
l.add_s(dn,ldif)

# Its nice to the server to disconnect and free resources when done
l.unbind_s()

1 个回答

0

我推测(但还没有测试来证明)你错误的根本原因是你的条目中缺少一个“命名属性”,这个属性需要和你条目中最左边的属性匹配。在你的情况下,这个属性是 ou=Enki Users。要把这个命名属性添加到条目中,你可以在代码中填充 attrs 字典的部分加上以下这一行。

attrs['ou'] = 'Enki Users'

撰写回答