~ubuntu-branches/ubuntu/precise/nss-pam-ldapd/precise-security

« back to all changes in this revision

Viewing changes to pynslcd/pam.py

  • Committer: Package Import Robot
  • Author(s): Arthur de Jong
  • Date: 2011-09-04 21:00:00 UTC
  • mfrom: (14.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20110904210000-pe3u91iga88vtr16
Tags: 0.8.4
* Upload to unstable
* switch to using the member attribute by default instead of
  uniqueMember (backwards incompatible change)
* only return "x" as a password hash when the object has the shadowAccount
  objectClass and nsswitch.conf is configured to do shadow lookups using
  LDAP (this avoids some problems with pam_unix)
* fix problem with partial attribute name matches in DN (thanks Timothy
  White)
* fix a problem with objectSid mappings with recent versions of OpenLDAP
  (patch by Wesley Mason)
* set the socket timeout in a connection callback to avoid timeout
  issues during the SSL handshake (patch by Stefan Völkel)
* check for unknown variables in pam_authz_search
* only check password expiration when authenticating, only check account
  expiration when doing authorisation
* make buffer sizes consistent and grow all buffers holding string
  representations of numbers to be able to hold 64-bit numbers
* update AX_PTHREAD from autoconf-archive
* support querying DNS SRV records from a different domain than the current
  one (based on a patch by James M. Leddy)
* fix a problem with uninitialised memory while parsing the tls_ciphers
  option (closes: #638872) (but doesn't work yet due to #640384)
* implement bounds checking of numeric values read from LDAP (patch by
  Jakub Hrozek)
* correctly support large uid and gid values from LDAP (patch by Jakub
  Hrozek)
* improvements to the configure script (patch by Jakub Hrozek)
* switch to dh for debian/rules and bump debhelper compatibility to 8
* build Debian packages with multiarch support
* ship shlibs (but still no symbol files) for libnss-ldapd since that was
  the easiest way to support multiarch
* fix output in init script when restarting nslcd (closes: #637132)
* correctly handle leading and trailing spaces in preseeded debconf uri
  option (patch by Andreas B. Mundt) (closes: #637863)
* support spaces around database names in /etc/nsswitch.conf while
  configuring package (closes: #640185)
* updated Russian debconf translation by Yuri Kozlov (closes: #637751)
* updated French debconf translation by Christian Perrier (closes: #637756)
* added Slovak debconf translation by Slavko (closes: #637759)
* updated Danish debconf translation by Joe Hansen (closes :#637763)
* updated Brazilian Portuguese debconf translation by Denis Doria
* updated Portuguese debconf translation by Américo Monteiro
* updated Japanese debconf translation by Kenshi Muto (closes: #638195)
* updated Czech debconf translation by Miroslav Kure (closes: #639026)
* updated German debconf translation by Chris Leick (closes: #639107)
* updated Spanish debconf translation by Francisco Javier Cuadrado
  (closes: #639236)
* updated Dutch debconf translation by Arthur de Jong with help from Paul
  Gevers and Jeroen Schot

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
# pam.py - functions authentication, authorisation and session handling
 
3
#
 
4
# Copyright (C) 2010, 2011 Arthur de Jong
 
5
#
 
6
# This library is free software; you can redistribute it and/or
 
7
# modify it under the terms of the GNU Lesser General Public
 
8
# License as published by the Free Software Foundation; either
 
9
# version 2.1 of the License, or (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 GNU
 
14
# Lesser General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU Lesser General Public
 
17
# License along with this library; if not, write to the Free Software
 
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
19
# 02110-1301 USA
 
20
 
 
21
import logging
 
22
import ldap
 
23
 
 
24
import constants
 
25
import common
 
26
import cfg
 
27
import passwd
 
28
 
 
29
 
 
30
def try_bind(userdn, password):
 
31
    # open a new connection
 
32
    conn = ldap.initialize(cfg.ldap_uri)
 
33
    # bind using the specified credentials
 
34
    conn.simple_bind_s(userdn, password)
 
35
    # perform search for own object (just to do any kind of search)
 
36
    res = conn.search_s(userdn, ldap.SCOPE_BASE, '(objectClass=*)', [ 'dn', ])
 
37
    for entry in res:
 
38
        if entry[0] == userdn:
 
39
            return
 
40
    raise ldap.NO_SUCH_OBJECT()
 
41
 
 
42
 
 
43
class PAMRequest(common.Request):
 
44
 
 
45
    def validate_request(self, parameters):
 
46
        """This method checks the provided username for validity and fills
 
47
        in the DN if needed."""
 
48
        # check username for validity
 
49
        common.validate_name(parameters['username'])
 
50
        # look up user DN if not known
 
51
        if not parameters['userdn']:
 
52
            entry = passwd.uid2entry(self.conn, parameters['username'])
 
53
            if not entry:
 
54
                raise ValueError('%r: user not found' % parameters['username'])
 
55
            # save the DN
 
56
            parameters['userdn'] = entry[0]
 
57
            # get the "real" username
 
58
            value = common.get_rdn_value(entry[0], passwd.attmap['uid'])
 
59
            if not value:
 
60
                # get the username from the uid attribute
 
61
                values = myldap_get_values(entry, passwd.attmap['uid'])
 
62
                if not values or not values[0]:
 
63
                    logging.warn('%s: is missing a %s attribute', dn, passwd.attmap['uid'])
 
64
                value = values[0]
 
65
            # check the username
 
66
            if value and not common.isvalidname(value):
 
67
                raise ValueError('%s: has invalid %s attribute', dn, passwd.attmap['uid'])
 
68
            # check if the username is different and update it if needed
 
69
            if value != parameters['username']:
 
70
                logging.info('username changed from %r to %r', parameters['username'], value)
 
71
                parameters['username'] = value
 
72
 
 
73
 
 
74
class PAMAuthenticationRequest(PAMRequest):
 
75
 
 
76
    action = constants.NSLCD_ACTION_PAM_AUTHC
 
77
 
 
78
    def read_parameters(self, fp):
 
79
        return dict(username=fp.read_string(),
 
80
                    userdn=fp.read_string(),
 
81
                    servicename=fp.read_string(),
 
82
                    password=fp.read_string())
 
83
        #self.validate_request()
 
84
        # TODO: log call with parameters
 
85
 
 
86
    def write(self, parameters, code=constants.NSLCD_PAM_SUCCESS, msg=''):
 
87
        self.fp.write_int32(constants.NSLCD_RESULT_BEGIN)
 
88
        self.fp.write_string(parameters['username'])
 
89
        self.fp.write_string(parameters['userdn'])
 
90
        self.fp.write_int32(code)  # authc
 
91
        self.fp.write_int32(constants.NSLCD_PAM_SUCCESS)  # authz
 
92
        self.fp.write_string(msg) # authzmsg
 
93
        self.fp.write_int32(constants.NSLCD_RESULT_END)
 
94
 
 
95
    def handle_request(self, parameters):
 
96
        # if the username is blank and rootpwmoddn is configured, try to
 
97
        # authenticate as administrator, otherwise validate request as usual
 
98
        if not parameters['username'] and cfg.rootpwmoddn:
 
99
            # authenticate as rootpwmoddn
 
100
            userdn = cfg.rootpwmoddn
 
101
            # if the caller is root we will allow the use of rootpwmodpw
 
102
            if not parameters['password'] and self.calleruid == 0 and cfg.rootpwmodpw:
 
103
                password = cfg.rootpwmodpw
 
104
        else:
 
105
            self.validate_request(parameters)
 
106
            userdn = parameters['userdn']
 
107
            password = parameters['password']
 
108
        # try authentication
 
109
        try:
 
110
            try_bind(userdn, password)
 
111
            logging.debug('bind successful')
 
112
            self.write(parameters)
 
113
        except ldap.INVALID_CREDENTIALS, e:
 
114
            try:
 
115
                msg = e[0]['desc']
 
116
            except:
 
117
                msg = str(e)
 
118
            logging.debug('bind failed: %s', msg)
 
119
            self.write(parameters, constants.NSLCD_PAM_AUTH_ERR, msg)
 
120
 
 
121
#class PAMAuthorisationRequest(PAMRequest):
 
122
 
 
123
#    action = constants.NSLCD_ACTION_PAM_AUTHZ
 
124
 
 
125
#    def handle_request(self):
 
126
 
 
127
 
 
128
#NSLCD_ACTION_PAM_SESS_O
 
129
#NSLCD_ACTION_PAM_SESS_C
 
130
#NSLCD_ACTION_PAM_PWMOD