lists.arthurdejong.org
RSS feed

nss-pam-ldapd commit: r1613 - nss-pam-ldapd/pynslcd

[Date Prev][Date Next] [Thread Prev][Thread Next]

nss-pam-ldapd commit: r1613 - nss-pam-ldapd/pynslcd



Author: arthur
Date: Sun Jan 29 15:39:25 2012
New Revision: 1613
URL: http://arthurdejong.org/viewvc/nss-pam-ldapd?revision=1613&view=revision

Log:
split the result handling into a convert() and write() step

Modified:
   nss-pam-ldapd/pynslcd/alias.py
   nss-pam-ldapd/pynslcd/common.py
   nss-pam-ldapd/pynslcd/ether.py
   nss-pam-ldapd/pynslcd/group.py
   nss-pam-ldapd/pynslcd/host.py
   nss-pam-ldapd/pynslcd/netgroup.py
   nss-pam-ldapd/pynslcd/network.py
   nss-pam-ldapd/pynslcd/passwd.py
   nss-pam-ldapd/pynslcd/protocol.py
   nss-pam-ldapd/pynslcd/rpc.py
   nss-pam-ldapd/pynslcd/service.py
   nss-pam-ldapd/pynslcd/shadow.py

Modified: nss-pam-ldapd/pynslcd/alias.py
==============================================================================
--- nss-pam-ldapd/pynslcd/alias.py      Sat Jan 21 17:20:01 2012        (r1612)
+++ nss-pam-ldapd/pynslcd/alias.py      Sun Jan 29 15:39:25 2012        (r1613)
@@ -35,15 +35,15 @@
 
 class AliasRequest(common.Request):
 
-    def write(self, dn, attributes, parameters):
-        # get values
+    def write(self, name, members):
+        self.fp.write_string(name)
+        self.fp.write_stringlist(members)
+
+    def convert(self, dn, attributes, parameters):
         names = attributes['cn']
         members = attributes['rfc822MailMember']
-        # write results
         for name in names:
-            self.fp.write_int32(constants.NSLCD_RESULT_BEGIN)
-            self.fp.write_string(name)
-            self.fp.write_stringlist(members)
+            yield (name, members)
 
 
 class AliasByNameRequest(AliasRequest):

Modified: nss-pam-ldapd/pynslcd/common.py
==============================================================================
--- nss-pam-ldapd/pynslcd/common.py     Sat Jan 21 17:20:01 2012        (r1612)
+++ nss-pam-ldapd/pynslcd/common.py     Sun Jan 29 15:39:25 2012        (r1613)
@@ -101,14 +101,13 @@
         self.attributes = attributes or self.attmap.attributes()
 
     def __iter__(self):
-        return self()
+        return self.items()
 
-    def __call__(self):
-        # get search results
+    def items(self):
+        """Return the results from the search."""
         filter = self.mk_filter()
         for base in self.bases:
             logging.debug('SEARCHING %s', base)
-            # do the LDAP search
             try:
                 for entry in self.conn.search_s(base, self.scope, filter, 
self.attributes):
                     if entry[0]:
@@ -198,13 +197,15 @@
         """This method handles the request based on the parameters read
         with read_parameters()."""
         for dn, attributes in self.search(conn=self.conn, 
parameters=parameters):
-            self.write(dn, attributes, parameters)
+            for values in self.convert(dn, attributes, parameters):
+                self.fp.write_int32(constants.NSLCD_RESULT_BEGIN)
+                self.write(*values)
         # write the final result code
         self.fp.write_int32(constants.NSLCD_RESULT_END)
 
     def __call__(self):
         parameters = self.read_parameters(self.fp) or {}
-        # TODO: log call with parameters
+        logging.debug('%s(%r)', self.__class__.__name__, parameters)
         self.fp.write_int32(constants.NSLCD_VERSION)
         self.fp.write_int32(self.action)
         self.handle_request(parameters)

Modified: nss-pam-ldapd/pynslcd/ether.py
==============================================================================
--- nss-pam-ldapd/pynslcd/ether.py      Sat Jan 21 17:20:01 2012        (r1612)
+++ nss-pam-ldapd/pynslcd/ether.py      Sun Jan 29 15:39:25 2012        (r1613)
@@ -48,16 +48,14 @@
 
 class EtherRequest(common.Request):
 
-    def write(self, dn, attributes, parameters):
-        # get values
-        names = attributes['cn']
-        addresses = [ether_aton(x) for x in attributes['macAddress']]
-        # write results
-        for name in names:
-            for ether in addresses:
-                self.fp.write_int32(constants.NSLCD_RESULT_BEGIN)
-                self.fp.write_string(name)
-                self.fp.write(ether)
+    def write(self, name, ether):
+        self.fp.write_string(name)
+        self.fp.write(ether_aton(ether))
+
+    def convert(self, dn, attributes, parameters):
+        for name in attributes['cn']:
+            for ether in attributes['macAddress']:
+                yield (name, ether)
 
 
 class EtherByNameRequest(EtherRequest):

Modified: nss-pam-ldapd/pynslcd/group.py
==============================================================================
--- nss-pam-ldapd/pynslcd/group.py      Sat Jan 21 17:20:01 2012        (r1612)
+++ nss-pam-ldapd/pynslcd/group.py      Sun Jan 29 15:39:25 2012        (r1613)
@@ -68,7 +68,13 @@
 
     wantmembers = True
 
-    def write(self, dn, attributes, parameters):
+    def write(self, name, passwd, gid, members):
+        self.fp.write_string(name)
+        self.fp.write_string(passwd)
+        self.fp.write_gid_t(gid)
+        self.fp.write_stringlist(members)
+
+    def convert(self, dn, attributes, parameters):
         # get group names and check against requested group name
         names = attributes['cn']
         # get group group password
@@ -90,14 +96,11 @@
         # actually return the results
         for name in names:
             if not common.isvalidname(name):
-                logging.warning('%s: %s: denied by validnames option', dn, 
attmap['cn'])
+                logging.warning('%s: %s: denied by validnames option', dn,
+                                attmap['cn'])
             else:
                 for gid in gids:
-                    self.fp.write_int32(constants.NSLCD_RESULT_BEGIN)
-                    self.fp.write_string(name)
-                    self.fp.write_string(passwd)
-                    self.fp.write_gid_t(gid)
-                    self.fp.write_stringlist(members)
+                    yield (name, passwd, gid, members)
 
 
 class GroupByNameRequest(GroupRequest):

Modified: nss-pam-ldapd/pynslcd/host.py
==============================================================================
--- nss-pam-ldapd/pynslcd/host.py       Sat Jan 21 17:20:01 2012        (r1612)
+++ nss-pam-ldapd/pynslcd/host.py       Sun Jan 29 15:39:25 2012        (r1613)
@@ -34,19 +34,17 @@
 
 class HostRequest(common.Request):
 
-    def write(self, dn, attributes, parameters):
-        # get values
-        hostnames = attributes['cn']
-        hostname = hostnames.pop(0)
-        addresses = attributes['ipHostNumber']
-        # write result
-        self.fp.write_int32(constants.NSLCD_RESULT_BEGIN)
+    def write(self, hostname, aliases, addresses):
         self.fp.write_string(hostname)
-        self.fp.write_stringlist(hostnames)
+        self.fp.write_stringlist(aliases)
         self.fp.write_int32(len(addresses))
         for address in addresses:
             self.fp.write_address(address)
 
+    def convert(self, dn, attributes, parameters):
+        hostnames = attributes['cn']
+        yield (hostnames[0], hostnames[1:], attributes['ipHostNumber'])
+
 
 class HostByNameRequest(HostRequest):
 

Modified: nss-pam-ldapd/pynslcd/netgroup.py
==============================================================================
--- nss-pam-ldapd/pynslcd/netgroup.py   Sat Jan 21 17:20:01 2012        (r1612)
+++ nss-pam-ldapd/pynslcd/netgroup.py   Sun Jan 29 15:39:25 2012        (r1613)
@@ -42,23 +42,25 @@
 
 class NetgroupRequest(common.Request):
 
-    def write(self, dn, attributes, parameters):
+    def write(self, name, member):
+        m = _netgroup_triple_re.match(member)
+        if m:
+            self.fp.write_int32(constants.NSLCD_NETGROUP_TYPE_TRIPLE)
+            self.fp.write_string(m.group('host'))
+            self.fp.write_string(m.group('user'))
+            self.fp.write_string(m.group('domain'))
+        else:
+            self.fp.write_int32(constants.NSLCD_NETGROUP_TYPE_NETGROUP)
+            self.fp.write_string(member)
+
+    def convert(self, dn, attributes, parameters):
         # write the netgroup triples
+        name = attributes['cn'][0]
         for triple in attributes['nisNetgroupTriple']:
-            m = _netgroup_triple_re.match(triple)
-            if not m:
-                logging.warning('%s: %s: invalid value: %r', dn, 
attmap['nisNetgroupTriple'], triple)
-            else:
-                self.fp.write_int32(constants.NSLCD_RESULT_BEGIN)
-                self.fp.write_int32(constants.NSLCD_NETGROUP_TYPE_TRIPLE)
-                self.fp.write_string(m.group('host'))
-                self.fp.write_string(m.group('user'))
-                self.fp.write_string(m.group('domain'))
+            yield (name, triple)
         # write netgroup members
         for member in attributes['memberNisNetgroup']:
-            self.fp.write_int32(constants.NSLCD_RESULT_BEGIN)
-            self.fp.write_int32(constants.NSLCD_NETGROUP_TYPE_NETGROUP)
-            self.fp.write_string(member)
+            yield (name, member)
 
 
 class NetgroupByNameRequest(NetgroupRequest):

Modified: nss-pam-ldapd/pynslcd/network.py
==============================================================================
--- nss-pam-ldapd/pynslcd/network.py    Sat Jan 21 17:20:01 2012        (r1612)
+++ nss-pam-ldapd/pynslcd/network.py    Sun Jan 29 15:39:25 2012        (r1613)
@@ -35,19 +35,17 @@
 
 class NetworkRequest(common.Request):
 
-    def write(self, dn, attributes, parameters):
-        # get values
-        networknames = attributes['cn']
-        networkname = networknames.pop(0)
-        addresses = attributes['ipNetworkNumber']
-        # write result
-        self.fp.write_int32(constants.NSLCD_RESULT_BEGIN)
+    def write(self, networkname, aliases, addresses):
         self.fp.write_string(networkname)
-        self.fp.write_stringlist(networknames)
+        self.fp.write_stringlist(aliases)
         self.fp.write_int32(len(addresses))
         for address in addresses:
             self.fp.write_address(address)
 
+    def convert(self, dn, attributes, parameters):
+        netnames = attributes['cn']
+        yield (netnames[0], netnames[1:], attributes['ipNetworkNumber'])
+
 
 class NetworkByNameRequest(NetworkRequest):
 

Modified: nss-pam-ldapd/pynslcd/passwd.py
==============================================================================
--- nss-pam-ldapd/pynslcd/passwd.py     Sat Jan 21 17:20:01 2012        (r1612)
+++ nss-pam-ldapd/pynslcd/passwd.py     Sun Jan 29 15:39:25 2012        (r1613)
@@ -45,8 +45,16 @@
 
 class PasswdRequest(common.Request):
 
-    def write(self, dn, attributes, parameters):
-        # get values
+    def write(self, name, passwd, uid, gid, gecos, home, shell):
+        self.fp.write_string(name)
+        self.fp.write_string(passwd)
+        self.fp.write_uid_t(uid)
+        self.fp.write_gid_t(gid)
+        self.fp.write_string(gecos)
+        self.fp.write_string(home)
+        self.fp.write_string(shell)
+
+    def convert(self, dn, attributes, parameters):
         names = attributes['uid']
         if 'shadowAccount' in attributes['objectClass']:
             passwd = 'x'
@@ -57,20 +65,12 @@
         gecos = attributes['gecos'][0]
         home = attributes['homeDirectory'][0]
         shell = attributes['loginShell'][0]
-        # write results
         for name in names:
             if not common.isvalidname(name):
                 logging.warning('%s: %s: denied by validnames option', dn, 
attmap['uid'])
             else:
                 for uid in uids:
-                    self.fp.write_int32(constants.NSLCD_RESULT_BEGIN)
-                    self.fp.write_string(name)
-                    self.fp.write_string(passwd)
-                    self.fp.write_uid_t(uid)
-                    self.fp.write_gid_t(gid)
-                    self.fp.write_string(gecos)
-                    self.fp.write_string(home)
-                    self.fp.write_string(shell)
+                    yield (name, passwd, uid, gid, gecos, home, shell)
 
 
 class PasswdByNameRequest(PasswdRequest):

Modified: nss-pam-ldapd/pynslcd/protocol.py
==============================================================================
--- nss-pam-ldapd/pynslcd/protocol.py   Sat Jan 21 17:20:01 2012        (r1612)
+++ nss-pam-ldapd/pynslcd/protocol.py   Sun Jan 29 15:39:25 2012        (r1613)
@@ -35,17 +35,15 @@
 
 class ProtocolRequest(common.Request):
 
-    def write(self, dn, attributes, parameters):
-        # get values
-        names = attributes['cn']
-        name = names.pop(0)
-        number = int(attributes['ipProtocolNumber'][0])
-        # write result
-        self.fp.write_int32(constants.NSLCD_RESULT_BEGIN)
+    def write(self, name, names, number):
         self.fp.write_string(name)
         self.fp.write_stringlist(names)
         self.fp.write_int32(number)
 
+    def convert(self, dn, attributes, parameters):
+        names = attributes['cn']
+        yield (names[0], names[1:], int(attributes['ipProtocolNumber'][0]))
+
 
 class ProtocolByNameRequest(ProtocolRequest):
 

Modified: nss-pam-ldapd/pynslcd/rpc.py
==============================================================================
--- nss-pam-ldapd/pynslcd/rpc.py        Sat Jan 21 17:20:01 2012        (r1612)
+++ nss-pam-ldapd/pynslcd/rpc.py        Sun Jan 29 15:39:25 2012        (r1613)
@@ -18,8 +18,8 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301 USA
 
-import constants
 import common
+import constants
 
 
 attmap = common.Attributes(cn='cn', oncRpcNumber='oncRpcNumber')
@@ -35,17 +35,15 @@
 
 class RpcRequest(common.Request):
 
-    def write(self, dn, attributes, parameters):
-        # get values
-        names = attributes['cn']
-        name = names.pop(0)
-        number = int(attributes['oncRpcNumber'][0])
-        # write result
-        self.fp.write_int32(constants.NSLCD_RESULT_BEGIN)
+    def write(self, name, aliases, number):
         self.fp.write_string(name)
-        self.fp.write_stringlist(names)
+        self.fp.write_stringlist(aliases)
         self.fp.write_int32(number)
 
+    def convert(self, dn, attributes, parameters):
+        names = attributes['cn']
+        yield (names[0], names[1:], int(attributes['oncRpcNumber'][0]))
+
 
 class RpcByNameRequest(RpcRequest):
 

Modified: nss-pam-ldapd/pynslcd/service.py
==============================================================================
--- nss-pam-ldapd/pynslcd/service.py    Sat Jan 21 17:20:01 2012        (r1612)
+++ nss-pam-ldapd/pynslcd/service.py    Sun Jan 29 15:39:25 2012        (r1613)
@@ -1,7 +1,7 @@
 
 # service.py - service entry lookup routines
 #
-# Copyright (C) 2011 Arthur de Jong
+# Copyright (C) 2011, 2012 Arthur de Jong
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -18,11 +18,11 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301 USA
 
-import logging
 import ldap.filter
+import logging
 
-import constants
 import common
+import constants
 
 
 attmap = common.Attributes(cn='cn',
@@ -41,19 +41,18 @@
 
 class ServiceRequest(common.Request):
 
-    def write(self, dn, attributes, parameters):
-        # get values
+    def write(self, name, aliases, port, protocol):
+        self.fp.write_string(name)
+        self.fp.write_stringlist(aliases)
+        self.fp.write_int32(port)
+        self.fp.write_string(protocol)
+
+    def convert(self, dn, attributes, parameters):
         names = attributes['cn']
-        name = names.pop(0)
         port = int(attributes['ipServicePort'][0])
         protocols = attributes['ipServiceProtocol']
-        # write result
         for protocol in protocols:
-            self.fp.write_int32(constants.NSLCD_RESULT_BEGIN)
-            self.fp.write_string(name)
-            self.fp.write_stringlist(names)
-            self.fp.write_int32(port)
-            self.fp.write_string(protocol)
+            yield (names[0], names[1:], port, protocol)
 
 
 class ServiceByNameRequest(ServiceRequest):

Modified: nss-pam-ldapd/pynslcd/shadow.py
==============================================================================
--- nss-pam-ldapd/pynslcd/shadow.py     Sat Jan 21 17:20:01 2012        (r1612)
+++ nss-pam-ldapd/pynslcd/shadow.py     Sun Jan 29 15:39:25 2012        (r1613)
@@ -1,7 +1,7 @@
 
 # shadow.py - lookup functions for shadownet addresses
 #
-# Copyright (C) 2010, 2011 Arthur de Jong
+# Copyright (C) 2010, 2011, 2012 Arthur de Jong
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -20,8 +20,8 @@
 
 import logging
 
-import constants
 import common
+import constants
 
 
 attmap = common.Attributes(uid='uid',
@@ -45,11 +45,21 @@
 
 class ShadowRequest(common.Request):
 
-    def write(self, dn, attributes, parameters):
-        # get name and check against requested name
+    def write(self, name, passwd, lastchangedate, mindays, maxdays, warndays,
+              inactdays, expiredate, flag):
+        self.fp.write_string(name)
+        self.fp.write_string(passwd)
+        self.fp.write_int32(lastchangedate)
+        self.fp.write_int32(mindays)
+        self.fp.write_int32(maxdays)
+        self.fp.write_int32(warndays)
+        self.fp.write_int32(inactdays)
+        self.fp.write_int32(expiredate)
+        self.fp.write_int32(flag)
+
+    def convert(self, dn, attributes, parameters):
         names = attributes['uid']
-        # get password
-        (passwd, ) = attributes['userPassword']
+        passwd = attributes['userPassword'][0]
         if not passwd or self.calleruid != 0:
             passwd = '*'
         # function for making an int
@@ -78,18 +88,10 @@
             if flag & 0x10000:
                 maxdays = -1
             flag = 0
-        # write results
+        # return results
         for name in names:
-            self.fp.write_int32(constants.NSLCD_RESULT_BEGIN)
-            self.fp.write_string(name)
-            self.fp.write_string(passwd)
-            self.fp.write_int32(lastchangedate)
-            self.fp.write_int32(mindays)
-            self.fp.write_int32(maxdays)
-            self.fp.write_int32(warndays)
-            self.fp.write_int32(inactdays)
-            self.fp.write_int32(expiredate)
-            self.fp.write_int32(flag)
+            yield (name, passwd, lastchangedate, mindays, maxdays, warndays,
+                    inactdays, expiredate, flag)
 
 
 class ShadowByNameRequest(ShadowRequest):
-- 
To unsubscribe send an email to
nss-pam-ldapd-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/nss-pam-ldapd-commits/