|
| 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 | +""" |
| 22 | +PAM authentication. |
| 23 | +
|
| 24 | +Authentication using the ``pam-python`` module. |
| 25 | +
|
| 26 | +Important: radicale user need access to /etc/shadow by e.g. |
| 27 | + chgrp radicale /etc/shadow |
| 28 | + chmod g+r |
| 29 | +""" |
| 30 | + |
| 31 | +import grp |
| 32 | +import pwd |
| 33 | + |
| 34 | +from radicale import auth |
| 35 | +from radicale.log import logger |
| 36 | + |
| 37 | + |
| 38 | +class Auth(auth.BaseAuth): |
| 39 | + def __init__(self, configuration) -> None: |
| 40 | + super().__init__(configuration) |
| 41 | + try: |
| 42 | + import pam |
| 43 | + self.pam = pam |
| 44 | + except ImportError as e: |
| 45 | + raise RuntimeError("PAM authentication requires the Python pam module") from e |
| 46 | + self._service = configuration.get("auth", "pam_service") |
| 47 | + logger.info("auth.pam_service: %s" % self._service) |
| 48 | + self._group_membership = configuration.get("auth", "pam_group_membership") |
| 49 | + if (self._group_membership): |
| 50 | + logger.info("auth.pam_group_membership: %s" % self._group_membership) |
| 51 | + else: |
| 52 | + logger.info("auth.pam_group_membership: (empty, nothing to check / INSECURE)") |
| 53 | + |
| 54 | + def pam_authenticate(self, *args, **kwargs): |
| 55 | + return self.pam.authenticate(*args, **kwargs) |
| 56 | + |
| 57 | + def _login(self, login: str, password: str) -> str: |
| 58 | + """Check if ``user``/``password`` couple is valid.""" |
| 59 | + if login is None or password is None: |
| 60 | + return "" |
| 61 | + |
| 62 | + # Check whether the user exists in the PAM system |
| 63 | + try: |
| 64 | + pwd.getpwnam(login).pw_uid |
| 65 | + except KeyError: |
| 66 | + logger.debug("PAM user not found: %r" % login) |
| 67 | + return "" |
| 68 | + else: |
| 69 | + logger.debug("PAM user found: %r" % login) |
| 70 | + |
| 71 | + # Check whether the user has a primary group (mandatory) |
| 72 | + try: |
| 73 | + # Get user primary group |
| 74 | + primary_group = grp.getgrgid(pwd.getpwnam(login).pw_gid).gr_name |
| 75 | + logger.debug("PAM user %r has primary group: %r" % (login, primary_group)) |
| 76 | + except KeyError: |
| 77 | + logger.debug("PAM user has no primary group: %r" % login) |
| 78 | + return "" |
| 79 | + |
| 80 | + # Obtain supplementary groups |
| 81 | + members = [] |
| 82 | + if (self._group_membership): |
| 83 | + try: |
| 84 | + members = grp.getgrnam(self._group_membership).gr_mem |
| 85 | + except KeyError: |
| 86 | + logger.debug( |
| 87 | + "PAM membership required group doesn't exist: %r" % |
| 88 | + self._group_membership) |
| 89 | + return "" |
| 90 | + |
| 91 | + # Check whether the user belongs to the required group |
| 92 | + # (primary or supplementary) |
| 93 | + if (self._group_membership): |
| 94 | + if (primary_group != self._group_membership) and (login not in members): |
| 95 | + logger.warning("PAM user %r belongs not to the required group: %r" % (login, self._group_membership)) |
| 96 | + return "" |
| 97 | + else: |
| 98 | + logger.debug("PAM user %r belongs to the required group: %r" % (login, self._group_membership)) |
| 99 | + |
| 100 | + # Check the password |
| 101 | + if self.pam_authenticate(login, password, service=self._service): |
| 102 | + return login |
| 103 | + else: |
| 104 | + logger.debug("PAM authentication not successful for user: %r (service %r)" % (login, self._service)) |
| 105 | + return "" |
0 commit comments