pam.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2011 Henry-Nicolas Tourneur
  5. # Copyright © 2021-2021 Unrud <unrud@outlook.com>
  6. # Copyright © 2025-2025 Peter Bieringer <pb@bieringer.de>
  7. #
  8. # This library is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  20. """
  21. PAM authentication.
  22. Authentication using the ``pam-python`` module.
  23. Important: radicale user need access to /etc/shadow by e.g.
  24. chgrp radicale /etc/shadow
  25. chmod g+r
  26. """
  27. import grp
  28. import pwd
  29. from radicale import auth
  30. from radicale.log import logger
  31. class Auth(auth.BaseAuth):
  32. def __init__(self, configuration) -> None:
  33. super().__init__(configuration)
  34. try:
  35. import pam
  36. self.pam = pam
  37. except ImportError as e:
  38. raise RuntimeError("PAM authentication requires the Python pam module") from e
  39. self._service = configuration.get("auth", "pam_service")
  40. logger.info("auth.pam_service: %s" % self._service)
  41. self._group_membership = configuration.get("auth", "pam_group_membership")
  42. if (self._group_membership):
  43. logger.info("auth.pam_group_membership: %s" % self._group_membership)
  44. else:
  45. logger.warning("auth.pam_group_membership: (empty, nothing to check / INSECURE)")
  46. def pam_authenticate(self, *args, **kwargs):
  47. return self.pam.authenticate(*args, **kwargs)
  48. def _login(self, login: str, password: str) -> str:
  49. """Check if ``user``/``password`` couple is valid."""
  50. if login is None or password is None:
  51. return ""
  52. # Check whether the user exists in the PAM system
  53. try:
  54. pwd.getpwnam(login).pw_uid
  55. except KeyError:
  56. logger.debug("PAM user not found: %r" % login)
  57. return ""
  58. else:
  59. logger.debug("PAM user found: %r" % login)
  60. # Check whether the user has a primary group (mandatory)
  61. try:
  62. # Get user primary group
  63. primary_group = grp.getgrgid(pwd.getpwnam(login).pw_gid).gr_name
  64. logger.debug("PAM user %r has primary group: %r" % (login, primary_group))
  65. except KeyError:
  66. logger.debug("PAM user has no primary group: %r" % login)
  67. return ""
  68. # Obtain supplementary groups
  69. members = []
  70. if (self._group_membership):
  71. try:
  72. members = grp.getgrnam(self._group_membership).gr_mem
  73. except KeyError:
  74. logger.debug(
  75. "PAM membership required group doesn't exist: %r" %
  76. self._group_membership)
  77. return ""
  78. # Check whether the user belongs to the required group
  79. # (primary or supplementary)
  80. if (self._group_membership):
  81. if (primary_group != self._group_membership) and (login not in members):
  82. logger.warning("PAM user %r belongs not to the required group: %r" % (login, self._group_membership))
  83. return ""
  84. else:
  85. logger.debug("PAM user %r belongs to the required group: %r" % (login, self._group_membership))
  86. # Check the password
  87. if self.pam_authenticate(login, password, service=self._service):
  88. return login
  89. else:
  90. logger.debug("PAM authentication not successful for user: %r (service %r)" % (login, self._service))
  91. return ""