1
0
mirror of https://github.com/kakwa/ldapcherry synced 2024-11-25 18:54:29 +01:00

* support for python-ldap 2 and 3

* python-ldap 3 is slightly different than 2 on how it handles modify
the modified attributes used to be transmitted as a dict, now it should
be transmitted as a list of dict)
This commit is contained in:
kakwa 2019-02-09 11:58:09 +01:00
parent 60d57d8530
commit b9437abefb
2 changed files with 29 additions and 17 deletions

View File

@ -192,13 +192,13 @@ class Backend(ldapcherry.backend.backendLdap.Backend):
attrs = {} attrs = {}
attrs['unicodePwd'] = self._str(password_value) attrs['unicodePwd'] = self._modlist(self._str(password_value))
ldif = modlist.modifyModlist({'unicodePwd': 'tmp'}, attrs) ldif = modlist.modifyModlist({'unicodePwd': 'tmp'}, attrs)
ldap_client.modify_s(dn, ldif) ldap_client.modify_s(dn, ldif)
del(attrs['unicodePwd']) del(attrs['unicodePwd'])
attrs['UserAccountControl'] = str(NORMAL_ACCOUNT) attrs['UserAccountControl'] = self._modlist(str(NORMAL_ACCOUNT))
ldif = modlist.modifyModlist({'UserAccountControl': 'tmp'}, attrs) ldif = modlist.modifyModlist({'UserAccountControl': 'tmp'}, attrs)
ldap_client.modify_s(dn, ldif) ldap_client.modify_s(dn, ldif)

View File

@ -20,6 +20,7 @@ import re
if sys.version < '3': if sys.version < '3':
from sets import Set as set from sets import Set as set
PYTHON_LDAP_MAJOR_VERSION = ldap.__version__[0]
class CaFileDontExist(Exception): class CaFileDontExist(Exception):
def __init__(self, cafile): def __init__(self, cafile):
@ -362,10 +363,19 @@ class Backend(ldapcherry.backend.Backend):
else: else:
return False return False
if PYTHON_LDAP_MAJOR_VERSION == '2':
@staticmethod
def _modlist(in_attr):
return in_attr
else:
@staticmethod
def _modlist(in_attr):
return [in_attr]
def attrs_pretreatment(self, attrs): def attrs_pretreatment(self, attrs):
attrs_str = {} attrs_str = {}
for a in attrs: for a in attrs:
attrs_str[self._str(a)] = self._str(attrs[a]) attrs_str[self._str(a)] = self._modlist(self._str(attrs[a]))
return attrs_str return attrs_str
def add_user(self, attrs): def add_user(self, attrs):
@ -374,17 +384,18 @@ class Backend(ldapcherry.backend.Backend):
# encoding crap # encoding crap
attrs_str = self.attrs_pretreatment(attrs) attrs_str = self.attrs_pretreatment(attrs)
attrs_str['objectClass'] = self.objectclasses attrs_str[self._str('objectClass')] = self.objectclasses
# construct is DN # construct is DN
dn = \ dn = \
self._str(self.dn_user_attr) + \ self._str(self.dn_user_attr) + \
'=' + \ self._str('=') + \
ldap.dn.escape_dn_chars( self._str(ldap.dn.escape_dn_chars(
self._str(attrs[self.dn_user_attr]) attrs[self.dn_user_attr]
)
) + \ ) + \
',' + \ self._str(',') + \
self._str(self.userdn) self._str(self.userdn)
# gen the ldif fir add_s and add the user # gen the ldif first add_s and add the user
ldif = modlist.addModlist(attrs_str) ldif = modlist.addModlist(attrs_str)
try: try:
ldap_client.add_s(dn, ldif) ldap_client.add_s(dn, ldif)
@ -419,7 +430,7 @@ class Backend(ldapcherry.backend.Backend):
for attr in attrs: for attr in attrs:
bcontent = self._str(attrs[attr]) bcontent = self._str(attrs[attr])
battr = self._str(attr) battr = self._str(attr)
new = {battr: bcontent} new = {battr: self._modlist(bcontent)}
# if attr is dn entry, use rename # if attr is dn entry, use rename
if attr.lower() == self.dn_user_attr.lower(): if attr.lower() == self.dn_user_attr.lower():
ldap_client.rename_s( ldap_client.rename_s(
@ -439,17 +450,18 @@ class Backend(ldapcherry.backend.Backend):
tmp.append(self._str(value)) tmp.append(self._str(value))
bold_value = tmp bold_value = tmp
else: else:
bold_value = self._str(old_attrs[attr]) bold_value = self._modlist(self._str(old_attrs[attr]))
old = {battr: bold_value} old = {battr: bold_value}
# attribute is not set, just add it # attribute is not set, just add it
else: else:
old = {} old = {}
ldif = modlist.modifyModlist(old, new) ldif = modlist.modifyModlist(old, new)
try: if ldif:
ldap_client.modify_s(dn, ldif) try:
except Exception as e: ldap_client.modify_s(dn, ldif)
ldap_client.unbind_s() except Exception as e:
self._exception_handler(e) ldap_client.unbind_s()
self._exception_handler(e)
ldap_client.unbind_s() ldap_client.unbind_s()
@ -482,7 +494,7 @@ class Backend(ldapcherry.backend.Backend):
'backend': self.backend_name 'backend': self.backend_name
} }
) )
ldif = modlist.modifyModlist({}, {attr: content}) ldif = modlist.modifyModlist({}, {attr: self._modlist(content)})
try: try:
ldap_client.modify_s(group, ldif) ldap_client.modify_s(group, ldif)
# if already member, not a big deal, just log it and continue # if already member, not a big deal, just log it and continue