PAM.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2011 Henry-Nicolas Tourneur
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. PAM authentication.
  20. Authentication based on the ``pam-python`` module.
  21. """
  22. import grp
  23. import pam
  24. import pwd
  25. from .. import config, log
  26. GROUP_MEMBERSHIP = config.get("auth", "pam_group_membership")
  27. # Compatibility for old versions of python-pam.
  28. if hasattr(pam, "pam"):
  29. def pam_authenticate(*args, **kwargs):
  30. return pam.pam().authenticate(*args, **kwargs)
  31. else:
  32. def pam_authenticate(*args, **kwargs):
  33. return pam.authenticate(*args, **kwargs)
  34. def is_authenticated(user, password):
  35. """Check if ``user``/``password`` couple is valid."""
  36. if user is None or password is None:
  37. return False
  38. # Check whether the user exists in the PAM system
  39. try:
  40. pwd.getpwnam(user).pw_uid
  41. except KeyError:
  42. log.LOGGER.debug("User %s not found" % user)
  43. return False
  44. else:
  45. log.LOGGER.debug("User %s found" % user)
  46. # Check whether the group exists
  47. try:
  48. # Obtain supplementary groups
  49. members = grp.getgrnam(GROUP_MEMBERSHIP).gr_mem
  50. except KeyError:
  51. log.LOGGER.debug(
  52. "The PAM membership required group (%s) doesn't exist" %
  53. GROUP_MEMBERSHIP)
  54. return False
  55. # Check whether the user exists
  56. try:
  57. # Get user primary group
  58. primary_group = grp.getgrgid(pwd.getpwnam(user).pw_gid).gr_name
  59. except KeyError:
  60. log.LOGGER.debug("The PAM user (%s) doesn't exist" % user)
  61. return False
  62. # Check whether the user belongs to the required group
  63. # (primary or supplementary)
  64. if primary_group == GROUP_MEMBERSHIP or user in members:
  65. log.LOGGER.debug(
  66. "The PAM user belongs to the required group (%s)" %
  67. GROUP_MEMBERSHIP)
  68. # Check the password
  69. if pam_authenticate(user, password, service='radicale'):
  70. return True
  71. else:
  72. log.LOGGER.debug("Wrong PAM password")
  73. else:
  74. log.LOGGER.debug(
  75. "The PAM user doesn't belong to the required group (%s)" %
  76. GROUP_MEMBERSHIP)
  77. return False