3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek#
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek# Module for simulation of utility "getent group -s sss" from coreutils
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek#
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek# Copyright (c) 2016 Red Hat, Inc.
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek#
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek# This is free software; you can redistribute it and/or modify it
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek# under the terms of the GNU General Public License as published by
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek# the Free Software Foundation; version 2 only
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek#
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek# This program is distributed in the hope that it will be useful, but
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek# WITHOUT ANY WARRANTY; without even the implied warranty of
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek# General Public License for more details.
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek#
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek# You should have received a copy of the GNU General Public License
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek# along with this program. If not, see <http://www.gnu.org/licenses/>.
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek#
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozekfrom ctypes import (c_int, c_char_p, c_ulong, POINTER, Structure,
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek create_string_buffer)
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozekfrom sssd_nss import NssReturnCode, SssdNssError, nss_sss_ctypes_loader
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub HrozekGROUP_BUFLEN = 1024
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozekclass Group(Structure):
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek _fields_ = [("gr_name", c_char_p),
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek ("gr_passwd", c_char_p),
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek ("gr_gid", c_int),
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek ("gr_mem", POINTER(c_char_p))]
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozekdef getgrnam_r(name, result_p, buffer_p, buflen):
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek """
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek ctypes wrapper for:
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek enum nss_status _nss_sss_getgrnam_r(const char *name,
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek struct group *result,
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek char *buffer,
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek size_t buflen,
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek int *errnop)
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek """
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek func = nss_sss_ctypes_loader("_nss_sss_getgrnam_r")
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek func.restype = c_int
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek func.argtypes = [c_char_p, POINTER(Group),
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek c_char_p, c_ulong, POINTER(c_int)]
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek errno = POINTER(c_int)(c_int(0))
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
bac4458c89a589055ae3daf4f72cc7dba886264aLukas Slebodnik name = name.encode('utf-8')
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek res = func(c_char_p(name), result_p, buffer_p, buflen, errno)
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek return (int(res), int(errno[0]), result_p)
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozekdef getgrgid_r(gid, result_p, buffer_p, buflen):
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek """
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek ctypes wrapper for:
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek enum nss_status _nss_sss_getgrgid_r(gid_t gid,
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek struct passwd *result,
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek char *buffer,
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek size_t buflen,
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek int *errnop)
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek """
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek func = nss_sss_ctypes_loader("_nss_sss_getgrgid_r")
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek func.restype = c_int
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek func.argtypes = [c_ulong, POINTER(Group),
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek c_char_p, c_ulong, POINTER(c_int)]
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek errno = POINTER(c_int)(c_int(0))
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek res = func(gid, result_p, buffer_p, buflen, errno)
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek return (int(res), int(errno[0]), result_p)
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozekdef set_group_dict(res, result_p):
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek if res != NssReturnCode.SUCCESS:
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek return dict()
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek group_dict = dict()
bac4458c89a589055ae3daf4f72cc7dba886264aLukas Slebodnik group_dict['name'] = result_p[0].gr_name.decode('utf-8')
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek group_dict['gid'] = result_p[0].gr_gid
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek group_dict['mem'] = list()
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek i = 0
4c3ddbb119c1ca0e12c49f58b914559e2510e2f1Fabiano Fidêncio while result_p[0].gr_mem[i] is not None:
bac4458c89a589055ae3daf4f72cc7dba886264aLukas Slebodnik grp_name = result_p[0].gr_mem[i].decode('utf-8')
bac4458c89a589055ae3daf4f72cc7dba886264aLukas Slebodnik group_dict['mem'].append(grp_name)
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek i = i+1
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek return group_dict
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozekdef call_sssd_getgrnam(name):
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek """
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek A Python wrapper to retrieve a group by name. Returns:
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek (res, group_dict)
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek if res is NssReturnCode.SUCCESS, then group_dict contains the keys
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek corresponding to the C passwd structure fields. Otherwise, the dictionary
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek is empty and errno indicates the error code
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek """
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek result = Group()
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek result_p = POINTER(Group)(result)
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek buff = create_string_buffer(GROUP_BUFLEN)
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek res, errno, result_p = getgrnam_r(name, result_p, buff, GROUP_BUFLEN)
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek if errno != 0:
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek raise SssdNssError(errno, "getgrnam_r")
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek group_dict = set_group_dict(res, result_p)
3728db53ac32da51fcaae96b132e8e56ebbaebfaJakub Hrozek return res, group_dict
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozekdef call_sssd_getgrgid(gid):
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek """
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek A Python wrapper to retrieve a group by GID. Returns:
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek (res, group_dict)
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek if res is NssReturnCode.SUCCESS, then group_dict contains the keys
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek corresponding to the C passwd structure fields. Otherwise, the dictionary
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek is empty and errno indicates the error code
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek """
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek result = Group()
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek result_p = POINTER(Group)(result)
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek buff = create_string_buffer(GROUP_BUFLEN)
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek res, errno, result_p = getgrgid_r(gid, result_p, buff, GROUP_BUFLEN)
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek if errno != 0:
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek raise SssdNssError(errno, "getgrgid_r")
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek group_dict = set_group_dict(res, result_p)
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek return res, group_dict