1
0
Fork 0
mirror of https://github.com/Kozea/Radicale.git synced 2025-06-26 16:45:52 +00:00
Radicale/radicale/acl/PAM.py

75 lines
2.1 KiB
Python
Raw Normal View History

2011-08-14 19:52:44 +02:00
# -*- coding: utf-8 -*-
#
# This file is part of Radicale Server - Calendar Server
2011-08-14 20:21:38 +02:00
# Copyright © 2011 Henry-Nicolas Tourneur
2011-08-14 19:52:44 +02:00
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
"""
PAM ACL.
2011-08-29 11:54:21 +02:00
Authentication based on the ``pam-python`` module.
2011-08-14 19:52:44 +02:00
"""
import grp
import pam
import pwd
2011-08-14 19:52:44 +02:00
from radicale import acl, config, log
2011-08-29 11:54:21 +02:00
GROUP_MEMBERSHIP = config.get("acl", "pam_group_membership")
2011-08-14 19:52:44 +02:00
def is_authenticated(user, password):
2011-08-14 19:52:44 +02:00
"""Check if ``user``/``password`` couple is valid."""
2011-08-29 11:54:21 +02:00
# Check whether the user exists in the PAM system
try:
pwd.getpwnam(user).pw_uid
except KeyError:
log.LOGGER.debug("User %s not found" % user)
return False
else:
log.LOGGER.debug("User %s found" % user)
# Check whether the group exists
try:
2012-08-08 16:37:18 +02:00
members = grp.getgrnam(GROUP_MEMBERSHIP).gr_mem
2011-08-14 19:52:44 +02:00
except KeyError:
2011-08-29 11:54:21 +02:00
log.LOGGER.debug(
"The PAM membership required group (%s) doesn't exist" %
GROUP_MEMBERSHIP)
return False
# Check whether the user belongs to the required group
for member in members:
if member == user:
log.LOGGER.debug(
"The PAM user belongs to the required group (%s)" %
GROUP_MEMBERSHIP)
# Check the password
if pam.authenticate(user, password):
return True
else:
log.LOGGER.debug("Wrong PAM password")
break
else:
log.LOGGER.debug(
"The PAM user doesn't belong to the required group (%s)" %
GROUP_MEMBERSHIP)
return False