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

« back to all changes in this revision

Viewing changes to pynslcd/passwd.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
# passwd.py - lookup functions for user account information
 
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 ldap
 
22
import ldap.filter
 
23
 
 
24
import constants
 
25
import common
 
26
 
 
27
 
 
28
attmap = common.Attributes(uid='uid',
 
29
                           userPassword='"*"',
 
30
                           uidNumber='uidNumber',
 
31
                           gidNumber='gidNumber',
 
32
                           gecos='"${gecos:-$cn}"',
 
33
                           homeDirectory='homeDirectory',
 
34
                           loginShell='loginShell',
 
35
                           objectClass='objectClass')
 
36
filter = '(objectClass=posixAccount)'
 
37
bases = ( 'ou=people,dc=test,dc=tld', )
 
38
 
 
39
 
 
40
class PasswdRequest(common.Request):
 
41
 
 
42
    def write(self, dn, attributes, parameters):
 
43
        # get uid attribute and check against requested user name
 
44
        names = attributes['uid']
 
45
        if 'uid' in parameters:
 
46
            if parameters['uid'] not in names:
 
47
                return
 
48
            names = ( parameters['uid'], )
 
49
        # get user password entry
 
50
        if 'shadowAccount' in attributes['objectClass']:
 
51
            passwd = 'x'
 
52
        else:
 
53
            passwd = attributes['userPassword'][0]
 
54
        # get numeric user and group ids
 
55
        uids = ( parameters['uidNumber'], ) if 'uidNumber' in parameters else attributes['uidNumber']
 
56
        uids = [ int(x) for x in uids ]
 
57
        # get other passwd properties
 
58
        gid = int(attributes['gidNumber'][0])
 
59
        gecos = attributes['gecos'][0]
 
60
        home = attributes['homeDirectory'][0]
 
61
        shell = attributes['loginShell'][0]
 
62
        # write results
 
63
        for name in names:
 
64
            if not common.isvalidname(name):
 
65
                print 'Warning: passwd entry %s contains invalid user name: "%s"' % ( dn, name )
 
66
            else:
 
67
                for uid in uids:
 
68
                    self.fp.write_int32(constants.NSLCD_RESULT_BEGIN)
 
69
                    self.fp.write_string(name)
 
70
                    self.fp.write_string(passwd)
 
71
                    self.fp.write_uid_t(uid)
 
72
                    self.fp.write_gid_t(gid)
 
73
                    self.fp.write_string(gecos)
 
74
                    self.fp.write_string(home)
 
75
                    self.fp.write_string(shell)
 
76
 
 
77
 
 
78
class PasswdByNameRequest(PasswdRequest):
 
79
 
 
80
    action = constants.NSLCD_ACTION_PASSWD_BYNAME
 
81
 
 
82
    def read_parameters(self, fp):
 
83
        name = fp.read_string()
 
84
        common.validate_name(name)
 
85
        return dict(uid=name)
 
86
 
 
87
 
 
88
class PasswdByUidRequest(PasswdRequest):
 
89
 
 
90
    action = constants.NSLCD_ACTION_PASSWD_BYUID
 
91
 
 
92
    def read_parameters(self, fp):
 
93
        return dict(uidNumber=fp.read_uid_t())
 
94
 
 
95
 
 
96
class PasswdAllRequest(PasswdRequest):
 
97
 
 
98
    action = constants.NSLCD_ACTION_PASSWD_ALL
 
99
 
 
100
 
 
101
# FIXME: have something in common that does this
 
102
def do_search(conn, flt=None, base=None):
 
103
    mybases = ( base, ) if base else bases
 
104
    flt = flt or filter
 
105
    import cfg
 
106
    # perform a search for each search base
 
107
    for base in mybases:
 
108
        # do the LDAP search
 
109
        try:
 
110
            scope = locals().get('scope', cfg.scope)
 
111
            res = conn.search_s(base, scope, flt, [attmap['uid']])
 
112
            for entry in res:
 
113
                if entry[0]:
 
114
                    yield entry
 
115
        except ldap.NO_SUCH_OBJECT:
 
116
            # FIXME: log message
 
117
            pass
 
118
 
 
119
def uid2entry(conn, uid):
 
120
    """Look up the user by uid and return the LDAP entry or None if the user
 
121
    was not found."""
 
122
    myfilter = '(&%s(%s=%s))' % ( filter,
 
123
                  attmap['uid'], ldap.filter.escape_filter_chars(uid) )
 
124
    for dn, attributes in do_search(conn, myfilter):
 
125
        if uid in attributes[attmap['uid']]:
 
126
            return dn, attributes
 
127
 
 
128
def uid2dn(conn, uid):
 
129
    """Look up the user by uid and return the DN or None if the user was
 
130
    not found."""
 
131
    x = uid2entry(conn, uid)
 
132
    if x is not None:
 
133
        return x[0]
 
134
 
 
135
# FIXME: use cache of dn2uid and try to use DN to get uid attribute
 
136
 
 
137
def dn2uid(conn, dn):
 
138
    """Look up the user by dn and return a uid or None if the user was
 
139
    not found."""
 
140
    try:
 
141
        for dn, attributes in do_search(conn, base=dn):
 
142
            return attributes[attmap['uid']][0]
 
143
    except ldap.NO_SUCH_OBJECT:
 
144
        return None