Projet

Général

Profil

0001-remove-vendored-dpam-library-fixes-29085.patch

Benjamin Dauvergne, 17 décembre 2018 10:16

Télécharger (11,9 ko)

Voir les différences:

Subject: [PATCH] remove vendored dpam library (fixes #29085)

 MANIFEST.in                            |   1 -
 doc/auth_pam.rst                       |  26 ------
 doc/configuration.rst                  |   2 -
 doc/installation.rst                   |   1 -
 doc/quick_pam.rst                      |  26 ------
 src/authentic2/__init__.py             |   6 --
 src/authentic2/vendor/__init__.py      |   0
 src/authentic2/vendor/dpam/LICENSE     |   8 --
 src/authentic2/vendor/dpam/__init__.py |   0
 src/authentic2/vendor/dpam/backends.py |  41 ---------
 src/authentic2/vendor/dpam/pam.py      | 123 -------------------------
 11 files changed, 234 deletions(-)
 delete mode 100644 doc/auth_pam.rst
 delete mode 100644 doc/quick_pam.rst
 delete mode 100644 src/authentic2/vendor/__init__.py
 delete mode 100644 src/authentic2/vendor/dpam/LICENSE
 delete mode 100644 src/authentic2/vendor/dpam/__init__.py
 delete mode 100644 src/authentic2/vendor/dpam/backends.py
 delete mode 100644 src/authentic2/vendor/dpam/pam.py
MANIFEST.in
59 59
include requirements.txt
60 60
include test_settings
61 61
include getlasso.sh
62
include src/authentic2/vendor/dpam/LICENSE
63 62
include src/authentic2/nonce/README.rst
64 63
include doc/conf.py doc/Makefile doc/README.rst.bak 
65 64
include local_settings.py.example
doc/auth_pam.rst
1
.. _auth_pam:
2

  
3
======================================
4
Authentication on Authentic 2 with PAM
5
======================================
6

  
7
This module is copied from https://bitbucket.org/wnielson/django-pam/ by Weston
8
Nielson and the pam ctype module by Chris Atlee http://atlee.ca/software/pam/.
9

  
10
Add 'authentic2.vendor.dpam.backends.PAMBackend' to your
11
``settings.py``::
12

  
13
  AUTHENTICATION_BACKENDS = (
14
      ...
15
      'authentic2.vendor.dpam.backends.PAMBackend',
16
      ...
17
  )
18

  
19
Now you can login via the system-login credentials.  If the user is
20
successfully authenticated but has never logged-in before, a new ``User``
21
object is created.  By default this new ``User`` has both ``is_staff`` and
22
``is_superuser`` set to ``False``.  You can change this behavior by adding
23
``PAM_IS_STAFF=True`` and ``PAM_IS_SUPERUSER`` in your ``settings.py`` file.
24

  
25
The default PAM service used is ``login`` but you can change it by setting the
26
``PAM_SERVICE`` variable in your ``settings.py`` file.
doc/configuration.rst
35 35

  
36 36
    auth_ldap
37 37

  
38
    auth_pam
39

  
40 38
SAML2
41 39
-----
42 40

  
doc/installation.rst
51 51
    quick_saml2_sp
52 52
    quick_cas_idp 
53 53
    quick_ldap_backend
54
    quick_pam
doc/quick_pam.rst
1
.. _quick_pam:
2

  
3
=================================
4
Quickstart for PAM Authentication
5
=================================
6

  
7
This module is copied from https://bitbucket.org/wnielson/django-pam/ by Weston
8
Nielson and the pam ctype module by Chris Atlee http://atlee.ca/software/pam/.
9

  
10
Add 'authentic2.vendor.dpam.backends.PAMBackend' to your
11
``settings.py``::
12

  
13
  AUTHENTICATION_BACKENDS = (
14
      ...
15
      'authentic2.vendor.dpam.backends.PAMBackend',
16
      ...
17
  )
18

  
19
Now you can login via the system-login credentials.  If the user is
20
successfully authenticated but has never logged-in before, a new ``User``
21
object is created.  By default this new ``User`` has both ``is_staff`` and
22
``is_superuser`` set to ``False``.  You can change this behavior by adding
23
``PAM_IS_STAFF=True`` and ``PAM_IS_SUPERUSER`` in your ``settings.py`` file.
24

  
25
The default PAM service used is ``login`` but you can change it by setting the
26
``PAM_SERVICE`` variable in your ``settings.py`` file.
src/authentic2/__init__.py
1
import sys
2
import os
3

  
4
# vendor contains incorporated dependencies
5
sys.path.append(os.path.join(os.path.dirname(__file__), 'vendor'))
6

  
7 1
default_app_config = 'authentic2.apps.Authentic2Config'
src/authentic2/vendor/dpam/LICENSE
1
Copyright (c) 2011, Weston Nielson <wnielson@gmail.com>
2
2All rights reserved.
3
3
4
4Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
5
6
6Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
7Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
8THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
src/authentic2/vendor/dpam/backends.py
1
import pam
2
import logging
3

  
4
from django.conf import settings
5

  
6
from authentic2.backends import is_user_authenticable
7
from authentic2.compat import get_user_model
8

  
9
logger = logging.getLogger(__name__)
10

  
11

  
12
class PAMBackend:
13
    def authenticate(self, username=None, password=None):
14
        User = get_user_model()
15
        service = getattr(settings, 'PAM_SERVICE', 'login')
16
        if pam.authenticate(username, password, service=service):
17
            try:
18
                user = User.objects.get(username=username)
19
            except:
20
                user = User(username=username, password='not stored here')
21

  
22
                if getattr(settings, 'PAM_IS_SUPERUSER', False):
23
                    user.is_superuser = True
24

  
25
                if getattr(settings, 'PAM_IS_STAFF', user.is_superuser):
26
                    user.is_staff = True
27

  
28
                user.save()
29
            if not is_user_authenticable(user):
30
                logger.info(u'auth_pam: authentication refused by user filters')
31
                return None
32

  
33
            return user
34
        return None
35

  
36
    def get_user(self, user_id):
37
        User = get_user_model()
38
        try:
39
            return User.objects.get(pk=user_id)
40
        except User.DoesNotExist:
41
            return None
src/authentic2/vendor/dpam/pam.py
1
# (c) 2007 Chris AtLee <chris@atlee.ca>
2
# Licensed under the MIT license:
3
# http://www.opensource.org/licenses/mit-license.php
4
"""
5
PAM module for python
6

  
7
Provides an authenticate function that will allow the caller to authenticate
8
a user against the Pluggable Authentication Modules (PAM) on the system.
9

  
10
Implemented using ctypes, so no compilation is necessary.
11
"""
12
__all__ = ['authenticate']
13

  
14
from ctypes import CDLL, POINTER, Structure, CFUNCTYPE, cast, pointer, sizeof
15
from ctypes import c_void_p, c_uint, c_char_p, c_char, c_int
16
from ctypes.util import find_library
17

  
18
LIBPAM = CDLL(find_library("pam"))
19
LIBC = CDLL(find_library("c"))
20

  
21
CALLOC = LIBC.calloc
22
CALLOC.restype = c_void_p
23
CALLOC.argtypes = [c_uint, c_uint]
24

  
25
STRDUP = LIBC.strdup
26
STRDUP.argstypes = [c_char_p]
27
STRDUP.restype = POINTER(c_char) # NOT c_char_p !!!!
28

  
29
# Various constants
30
PAM_PROMPT_ECHO_OFF = 1
31
PAM_PROMPT_ECHO_ON = 2
32
PAM_ERROR_MSG = 3
33
PAM_TEXT_INFO = 4
34

  
35
class PamHandle(Structure):
36
    """wrapper class for pam_handle_t"""
37
    _fields_ = [
38
            ("handle", c_void_p)
39
            ]
40

  
41
    def __init__(self):
42
        Structure.__init__(self)
43
        self.handle = 0
44

  
45
class PamMessage(Structure):
46
    """wrapper class for pam_message structure"""
47
    _fields_ = [
48
            ("msg_style", c_int),
49
            ("msg", c_char_p),
50
            ]
51

  
52
    def __repr__(self):
53
        return "<PamMessage %i '%s'>" % (self.msg_style, self.msg)
54

  
55
class PamResponse(Structure):
56
    """wrapper class for pam_response structure"""
57
    _fields_ = [
58
            ("resp", c_char_p),
59
            ("resp_retcode", c_int),
60
            ]
61

  
62
    def __repr__(self):
63
        return "<PamResponse %i '%s'>" % (self.resp_retcode, self.resp)
64

  
65
CONV_FUNC = CFUNCTYPE(c_int,
66
        c_int, POINTER(POINTER(PamMessage)),
67
               POINTER(POINTER(PamResponse)), c_void_p)
68

  
69
class PamConv(Structure):
70
    """wrapper class for pam_conv structure"""
71
    _fields_ = [
72
            ("conv", CONV_FUNC),
73
            ("appdata_ptr", c_void_p)
74
            ]
75

  
76
PAM_START = LIBPAM.pam_start
77
PAM_START.restype = c_int
78
PAM_START.argtypes = [c_char_p, c_char_p, POINTER(PamConv),
79
        POINTER(PamHandle)]
80

  
81
PAM_AUTHENTICATE = LIBPAM.pam_authenticate
82
PAM_AUTHENTICATE.restype = c_int
83
PAM_AUTHENTICATE.argtypes = [PamHandle, c_int]
84

  
85
def authenticate(username, password, service='login'):
86
    """Returns True if the given username and password authenticate for the
87
    given service.  Returns False otherwise
88
    
89
    ``username``: the username to authenticate
90
    
91
    ``password``: the password in plain text
92
    
93
    ``service``: the PAM service to authenticate against.
94
                 Defaults to 'login'"""
95
    @CONV_FUNC
96
    def my_conv(n_messages, messages, p_response, app_data):
97
        """Simple conversation function that responds to any
98
        prompt where the echo is off with the supplied password"""
99
        # Create an array of n_messages response objects
100
        addr = CALLOC(n_messages, sizeof(PamResponse))
101
        p_response[0] = cast(addr, POINTER(PamResponse))
102
        for i in range(n_messages):
103
            if messages[i].contents.msg_style == PAM_PROMPT_ECHO_OFF:
104
                pw_copy = STRDUP(str(password))
105
                p_response.contents[i].resp = cast(pw_copy, c_char_p)
106
                p_response.contents[i].resp_retcode = 0
107
        return 0
108

  
109
    handle = PamHandle()
110
    conv = PamConv(my_conv, 0)
111
    retval = PAM_START(service, username, pointer(conv), pointer(handle))
112

  
113
    if retval != 0:
114
        # TODO: This is not an authentication error, something
115
        # has gone wrong starting up PAM
116
        return False
117

  
118
    retval = PAM_AUTHENTICATE(handle, 0)
119
    return retval == 0
120

  
121
if __name__ == "__main__":
122
    import getpass
123
    print authenticate(getpass.getuser(), getpass.getpass())
124
-