pam.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 based on the ``pam-python`` module.
  23. """
  24. import grp
  25. import pam
  26. import pwd
  27. from .. import config, log
  28. GROUP_MEMBERSHIP = config.get("auth", "pam_group_membership")
  29. # Compatibility for old versions of python-pam.
  30. if hasattr(pam, "pam"):
  31. def pam_authenticate(*args, **kwargs):
  32. return pam.pam().authenticate(*args, **kwargs)
  33. else:
  34. def pam_authenticate(*args, **kwargs):
  35. return pam.authenticate(*args, **kwargs)
  36. def is_authenticated(user, password):
  37. """Check if ``user``/``password`` couple is valid."""
  38. if user is None or password is None:
  39. return False
  40. # Check whether the user exists in the PAM system
  41. try:
  42. pwd.getpwnam(user).pw_uid
  43. except KeyError:
  44. log.LOGGER.debug("User %s not found" % user)
  45. return False
  46. else:
  47. log.LOGGER.debug("User %s found" % user)
  48. # Check whether the group exists
  49. try:
  50. # Obtain supplementary groups
  51. members = grp.getgrnam(GROUP_MEMBERSHIP).gr_mem
  52. except KeyError:
  53. log.LOGGER.debug(
  54. "The PAM membership required group (%s) doesn't exist" %
  55. GROUP_MEMBERSHIP)
  56. return False
  57. # Check whether the user exists
  58. try:
  59. # Get user primary group
  60. primary_group = grp.getgrgid(pwd.getpwnam(user).pw_gid).gr_name
  61. except KeyError:
  62. log.LOGGER.debug("The PAM user (%s) doesn't exist" % user)
  63. return False
  64. # Check whether the user belongs to the required group
  65. # (primary or supplementary)
  66. if primary_group == GROUP_MEMBERSHIP or user in members:
  67. log.LOGGER.debug(
  68. "The PAM user belongs to the required group (%s)" %
  69. GROUP_MEMBERSHIP)
  70. # Check the password
  71. if pam_authenticate(user, password, service='radicale'):
  72. return True
  73. else:
  74. log.LOGGER.debug("Wrong PAM password")
  75. else:
  76. log.LOGGER.debug(
  77. "The PAM user doesn't belong to the required group (%s)" %
  78. GROUP_MEMBERSHIP)
  79. return False