Skip to content

Commit

Permalink
Merge pull request #1570 from petervarkoly/master
Browse files Browse the repository at this point in the history
Fix issue #197
  • Loading branch information
pbiering committed Sep 17, 2024
2 parents da844f4 + 15ed41f commit 7fbc0e7
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions radicale/auth/ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(self, configuration: config.Configuration) -> None:
def _login2(self, login: str, password: str) -> str:
try:
"""Bind as reader dn"""
logger.debug(f"_login2 {self._ldap_uri}, {self._ldap_reader_dn}")
conn = self.ldap.initialize(self._ldap_uri)
conn.protocol_version = 3
conn.set_option(self.ldap.OPT_REFERRALS, 0)
Expand All @@ -72,8 +73,8 @@ def _login2(self, login: str, password: str) -> str:
logger.debug("LDAP Auth user: %s", user_dn)
"""Close ldap connection"""
conn.unbind()
except Exception:
raise RuntimeError("Invalide ldap configuration")
except Exception as e:
raise RuntimeError(f"Invalid ldap configuration:{e}")

try:
"""Bind as user to authenticate"""
Expand All @@ -96,43 +97,52 @@ def _login2(self, login: str, password: str) -> str:
def _login3(self, login: str, password: str) -> str:
"""Connect the server"""
try:
logger.debug(f"_login3 {self._ldap_uri}, {self._ldap_reader_dn}")
server = self.ldap3.Server(self._ldap_uri)
conn = self.ldap3.Connection(server, self._ldap_reader_dn, password=self._ldap_secret)
except self.ldap3.core.exceptions.LDAPSocketOpenError:
raise RuntimeError("Unable to reach ldap server")
except Exception:
except Exception as e:
logger.debug(f"_login3 error 1 {e}")
pass

if not conn.bind():
logger.debug("_login3 can not bind")
raise RuntimeError("Unable to read from ldap server")

logger.debug(f"_login3 bind as {self._ldap_reader_dn}")
"""Search the user dn"""
conn.search(
search_base=self._ldap_base,
search_filter=self._ldap_filter.format(login),
search_scope='SUBTREE',
search_scope=self.ldap3.SUBTREE,
attributes=['memberOf']
)
if len(conn.entries) == 0:
logger.debug(f"_login3 user '{login}' can not be find")
"""User could not be find"""
return ""

user_entry = conn.entries[0].entry_to_json()
user_entry = conn.response[0]
conn.unbind()
user_dn = user_entry['dn']
logger.debug(f"_login3 found user_dn {user_dn}")
try:
"""Try to bind as the user itself"""
conn = self.ldap3.Connection(server, user_dn, password=password)
if not conn.bind():
logger.debug(f"_login3 user '{login}' can not be find")
return ""
if self._ldap_load_groups:
tmp = []
for g in user_entry['attributes']['memberOf']:
tmp.append(g)
tmp.append(g.split(',')[0][3:])
self._ldap_groups = set(tmp)
conn.unbind()
logger.debug(f"_login3 {login} successfully authorized")
return login
except Exception:
except Exception as e:
logger.debug(f"_login3 error 2 {e}")
pass
return ""

Expand Down

0 comments on commit 7fbc0e7

Please sign in to comment.