ldap.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright 2022 Peter Varkoly
  3. #
  4. # This library is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  16. """
  17. Authentication backend that checks credentials with a ldap server.
  18. Following parameters are needed in the configuration
  19. ldap_uri The ldap url to the server like ldap://localhost
  20. ldap_base The baseDN of the ldap server
  21. ldap_reader_dn The DN of a ldap user with read access to get the user accounts
  22. ldap_secret The password of the ldap_reader_dn
  23. ldap_filter The search filter to find the user to authenticate by the username
  24. ldap_load_groups If the groups of the authenticated users need to be loaded
  25. """
  26. import ldap
  27. from radicale import auth, config
  28. from radicale.log import logger
  29. class Auth(auth.BaseAuth):
  30. _ldap_uri: str
  31. _ldap_base: str
  32. _ldap_reader_dn: str
  33. _ldap_secret: str
  34. _ldap_filter: str
  35. _ldap_load_groups: bool
  36. def __init__(self, configuration: config.Configuration) -> None:
  37. super().__init__(configuration)
  38. self._ldap_uri = configuration.get("auth", "ldap_uri")
  39. self._ldap_base = configuration.get("auth", "ldap_base")
  40. self._ldap_reader_dn = configuration.get("auth", "ldap_reader_dn")
  41. self._ldap_load_groups = configuration.get("auth", "ldap_load_groups")
  42. self._ldap_secret = configuration.get("auth", "ldap_secret")
  43. self._ldap_filter = configuration.get("auth", "ldap_filter")
  44. def login(self, login: str, password: str) -> str:
  45. """Validate credentials.
  46. In first step we make a connection to the ldap server with the ldap_reader_dn credential.
  47. In next step the DN of the user to authenticate will be searched.
  48. In the last step the authentication of the user will be proceeded.
  49. """
  50. try:
  51. """Bind as reader dn"""
  52. conn = ldap.initialize(self._ldap_uri)
  53. conn.protocol_version = 3
  54. conn.set_option(ldap.OPT_REFERRALS, 0)
  55. conn.simple_bind_s(self._ldap_reader_dn, self._ldap_secret)
  56. """Search for the dn of user to authenticate"""
  57. res = conn.search_s(self._ldap_base, ldap.SCOPE_SUBTREE, filterstr=self._ldap_filter.format(login), attrlist=['memberOf'])
  58. if len(res) == 0:
  59. """User could not be find"""
  60. return ""
  61. user_dn = res[0][0]
  62. logger.debug("LDAP Auth user: %s",user_dn)
  63. """Close ldap connection"""
  64. conn.unbind()
  65. except Exception:
  66. raise RuntimeError("Invalide ldap configuration")
  67. try:
  68. """Bind as user to authenticate"""
  69. conn = ldap.initialize(self._ldap_uri)
  70. conn.protocol_version = 3
  71. conn.set_option(ldap.OPT_REFERRALS, 0)
  72. conn.simple_bind_s(user_dn,password)
  73. tmp = []
  74. if self._ldap_load_groups:
  75. tmp = []
  76. for t in res[0][1]['memberOf']:
  77. tmp.append(t.decode('utf-8').split(',')[0][3:])
  78. self._ldap_groups = set(tmp)
  79. logger.debug("LDAP Auth groups of user: %s",",".join(self._ldap_groups))
  80. conn.unbind()
  81. return login
  82. except ldap.INVALID_CREDENTIALS:
  83. return ""