diff options
-rw-r--r-- | pynslcd/alias.py | 12 | ||||
-rw-r--r-- | pynslcd/common.py | 13 | ||||
-rw-r--r-- | pynslcd/ether.py | 18 | ||||
-rw-r--r-- | pynslcd/group.py | 17 | ||||
-rw-r--r-- | pynslcd/host.py | 14 | ||||
-rw-r--r-- | pynslcd/netgroup.py | 28 | ||||
-rw-r--r-- | pynslcd/network.py | 14 | ||||
-rw-r--r-- | pynslcd/passwd.py | 22 | ||||
-rw-r--r-- | pynslcd/protocol.py | 12 | ||||
-rw-r--r-- | pynslcd/rpc.py | 16 | ||||
-rw-r--r-- | pynslcd/service.py | 23 | ||||
-rw-r--r-- | pynslcd/shadow.py | 36 |
12 files changed, 111 insertions, 114 deletions
diff --git a/pynslcd/alias.py b/pynslcd/alias.py index 84dbf97..cdae9da 100644 --- a/pynslcd/alias.py +++ b/pynslcd/alias.py @@ -35,15 +35,15 @@ class Search(common.Search): 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): diff --git a/pynslcd/common.py b/pynslcd/common.py index 375afed..7f41a11 100644 --- a/pynslcd/common.py +++ b/pynslcd/common.py @@ -101,14 +101,13 @@ class Search(object): 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 @@ class Request(object): """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) diff --git a/pynslcd/ether.py b/pynslcd/ether.py index 4d6ae03..20963c3 100644 --- a/pynslcd/ether.py +++ b/pynslcd/ether.py @@ -48,16 +48,14 @@ class Search(common.Search): 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): diff --git a/pynslcd/group.py b/pynslcd/group.py index 29a6fda..f5bb2b1 100644 --- a/pynslcd/group.py +++ b/pynslcd/group.py @@ -68,7 +68,13 @@ class GroupRequest(common.Request): 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 @@ class GroupRequest(common.Request): # 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): diff --git a/pynslcd/host.py b/pynslcd/host.py index fa15b58..23ab521 100644 --- a/pynslcd/host.py +++ b/pynslcd/host.py @@ -34,19 +34,17 @@ class Search(common.Search): 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): diff --git a/pynslcd/netgroup.py b/pynslcd/netgroup.py index 9ddba7a..31eb02c 100644 --- a/pynslcd/netgroup.py +++ b/pynslcd/netgroup.py @@ -42,23 +42,25 @@ class Search(common.Search): 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): diff --git a/pynslcd/network.py b/pynslcd/network.py index d0778c0..bccc788 100644 --- a/pynslcd/network.py +++ b/pynslcd/network.py @@ -35,19 +35,17 @@ class Search(common.Search): 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): diff --git a/pynslcd/passwd.py b/pynslcd/passwd.py index 6dce2ce..9b222d0 100644 --- a/pynslcd/passwd.py +++ b/pynslcd/passwd.py @@ -45,8 +45,16 @@ class Search(common.Search): 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 @@ class PasswdRequest(common.Request): 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): diff --git a/pynslcd/protocol.py b/pynslcd/protocol.py index 3c91a7e..3f536ee 100644 --- a/pynslcd/protocol.py +++ b/pynslcd/protocol.py @@ -35,17 +35,15 @@ class Search(common.Search): 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): diff --git a/pynslcd/rpc.py b/pynslcd/rpc.py index 5676b27..e743960 100644 --- a/pynslcd/rpc.py +++ b/pynslcd/rpc.py @@ -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 Search(common.Search): 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): diff --git a/pynslcd/service.py b/pynslcd/service.py index 6923236..c89ac6f 100644 --- a/pynslcd/service.py +++ b/pynslcd/service.py @@ -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 Search(common.Search): 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): diff --git a/pynslcd/shadow.py b/pynslcd/shadow.py index 9b74190..2a4a1ab 100644 --- a/pynslcd/shadow.py +++ b/pynslcd/shadow.py @@ -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 Search(common.Search): 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 @@ class ShadowRequest(common.Request): 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): |