b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik# Module for simulation of utility "getent netgroup -s sss" from coreutils
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik# Copyright (c) 2016 Red Hat, Inc.
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik# Author: Lukas Slebodnik <lslebodn@redhat.com>
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik# This is free software; you can redistribute it and/or modify it
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik# under the terms of the GNU General Public License as published by
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik# the Free Software Foundation; version 2 only
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik# This program is distributed in the hope that it will be useful, but
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik# WITHOUT ANY WARRANTY; without even the implied warranty of
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik# General Public License for more details.
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik# You should have received a copy of the GNU General Public License
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik# along with this program. If not, see <http://www.gnu.org/licenses/>.
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnikfrom ctypes import (cdll, c_int, c_char, c_char_p, c_size_t, c_void_p, c_ulong,
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik POINTER, Structure, Union, create_string_buffer, get_errno)
1921d739ff7b028baa591272cc8969e330c8f872Jakub Hrozekfrom sssd_nss import NssReturnCode, nss_sss_ctypes_loader
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik """ 'enum' class for type of netgroup """
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas SlebodnikNameList._fields_ = [("next", POINTER(NameList)),
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik This private method is ctypes wrapper for
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik enum nss_status _nss_sss_setnetgrent(const char *netgroup,
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik struct __netgrent *result)
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik @param string name name of netgroup
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik @return (int, POINTER(Netgrent)) (err, result_p)
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik err is a constant from class NssReturnCode and in case of SUCCESS
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik result_p will contain POINTER(Netgrent) which can be used in
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik _getnetgrent_r or _getnetgrent_r.
1921d739ff7b028baa591272cc8969e330c8f872Jakub Hrozek func = nss_sss_ctypes_loader('_nss_sss_setnetgrent')
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik func.argtypes = [c_char_p, POINTER(Netgrent)]
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik def _getnetgrent_r(result_p, buff, buff_len):
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik This private method is ctypes wrapper for
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik enum nss_status _nss_sss_getnetgrent_r(struct __netgrent *result,
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik char *buffer, size_t buflen,
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik @param POINTER(Netgrent) result_p pointer to initialized C structure
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik struct __netgrent
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik @param ctypes.c_char_Array buff buffer used by C functions
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik @param int buff_len size of c_char_Array passed as a paramere buff
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik @return (int, int, List[(string, string, string])
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik (err, errno, netgroups)
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik if err is NssReturnCode.SUCCESS netgroups will contain list of
49dd8ee2834d9477418961dbaffa4a03cfa9fd1eRené Genz touples. Each touple will consist of 3 elements either string or
1921d739ff7b028baa591272cc8969e330c8f872Jakub Hrozek func = nss_sss_ctypes_loader('_nss_sss_getnetgrent_r')
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik func.argtypes = [POINTER(Netgrent), POINTER(c_char), c_size_t,
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik This private method is ctypes wrapper for
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik enum nss_status _nss_sss_endnetgrent(struct __netgrent *result)
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik @param POINTER(Netgrent) result_p pointer to initialized C structure
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik struct __netgrent
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik @return int a constant from class NssReturnCode
1921d739ff7b028baa591272cc8969e330c8f872Jakub Hrozek func = nss_sss_ctypes_loader('_nss_sss_endnetgrent')
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik Function will return netgroup triplets for given user. All nested
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik netgroups will be retieved as part of executions and will content
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik will be merged with direct triplets.
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik Missing nested netgroups will not cause failure and are considered
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik as an empty netgroup without triplets.
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik @param string name name of netgroup
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik @return (int, int, List[(string, string, string])
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik (err, errno, netgroups)
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik if err is NssReturnCode.SUCCESS netgroups will contain list of
49dd8ee2834d9477418961dbaffa4a03cfa9fd1eRené Genz touples. Each touple will consist of 3 elements either string or
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik None (host, user, domain).
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik res, errno, result = self._flat_fetch_netgroups(self.name)
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik nest_res, nest_errno, result = self._flat_fetch_netgroups(name)
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik # do not fail for missing nested netgroup
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik if nest_res not in (NssReturnCode.SUCCESS, NssReturnCode.NOTFOUND):
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik return (nest_res, nest_errno, self.netgroups)
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik Function will return netgroup triplets for given user. The nested
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik netgroups will not be returned. Missing nested netgroups will be
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik appended to the array needed_groups
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik @param string name name of netgroup
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik @return (int, int, List[(string, string, string])
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik (err, errno, netgroups)
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik if err is NssReturnCode.SUCCESS netgroups will contain list of
49dd8ee2834d9477418961dbaffa4a03cfa9fd1eRené Genz touples. Each touple will consist of 3 elements either string or
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik None (host, user, domain).
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik res, errno, result_p = self._getnetgrent_r(result_p, buff, buff_len)
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik if result_p[0].type == NetgroupType.GROUP_VAL:
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik if nested_netgroup not in self.known_groups:
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik if result_p[0].type == NetgroupType.TRIPLE_VAL:
0f8add07b8257fcce9f62ad80d24e79b8013ae42Sumit Bose result.append((triple.host and triple.host.decode('utf-8')
c596fc4d75304ff224cbad0aa2aecd3cbe82d2ffLukas Slebodnik res, errno, result_p = self._getnetgrent_r(result_p, buff,
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik Function will return netgroup triplets for given user. It will gather
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik netgroups only provided by sssd.
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik The equivalent of "getent netgroup -s sss user"
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik @param string name name of netgroup
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik @return (int, int, List[(string, string, string]) (err, errno, netgroups)
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik if err is NssReturnCode.SUCCESS netgroups will contain list of touples.
49dd8ee2834d9477418961dbaffa4a03cfa9fd1eRené Genz Each touple will consist of 3 elements either string or None
b4633e73067d7bf3b0dbaf212569c123de88f306Lukas Slebodnik (host, user, domain).