summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJakub Hrozek <jhrozek@redhat.com>2016-11-16 17:02:43 +0100
committerJakub Hrozek <jhrozek@redhat.com>2017-02-15 14:53:50 +0100
commit3728db53ac32da51fcaae96b132e8e56ebbaebfa (patch)
tree864f16f46d2fd33b15b413b7d3ba7d637d5379a7
parent8578fba1500d43ad9632784462c255bf8bb360fe (diff)
downloadsssd-3728db53ac32da51fcaae96b132e8e56ebbaebfa.tar.gz
sssd-3728db53ac32da51fcaae96b132e8e56ebbaebfa.tar.xz
sssd-3728db53ac32da51fcaae96b132e8e56ebbaebfa.zip
TESTS: Add a module to call nss_sss's getgr* from tests
Implements a python module that allows to load the nss_sss module and call functions that act like getgr* Reviewed-by: Pavel Březina <pbrezina@redhat.com> Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com>
-rw-r--r--src/tests/intg/Makefile.am1
-rw-r--r--src/tests/intg/sssd_group.py88
2 files changed, 89 insertions, 0 deletions
diff --git a/src/tests/intg/Makefile.am b/src/tests/intg/Makefile.am
index 6cd23967f..03245b35d 100644
--- a/src/tests/intg/Makefile.am
+++ b/src/tests/intg/Makefile.am
@@ -6,6 +6,7 @@ dist_noinst_DATA = \
sssd_ldb.py \
sssd_netgroup.py \
sssd_passwd.py \
+ sssd_group.py \
ds.py \
ds_openldap.py \
ent.py \
diff --git a/src/tests/intg/sssd_group.py b/src/tests/intg/sssd_group.py
new file mode 100644
index 000000000..a9cfb32d5
--- /dev/null
+++ b/src/tests/intg/sssd_group.py
@@ -0,0 +1,88 @@
+#
+# Module for simulation of utility "getent group -s sss" from coreutils
+#
+# Copyright (c) 2016 Red Hat, Inc.
+#
+# This is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 only
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+from ctypes import (c_int, c_char_p, c_ulong, POINTER, Structure,
+ create_string_buffer)
+from sssd_nss import NssReturnCode, SssdNssError, nss_sss_ctypes_loader
+
+GROUP_BUFLEN = 1024
+
+
+class Group(Structure):
+ _fields_ = [("gr_name", c_char_p),
+ ("gr_passwd", c_char_p),
+ ("gr_gid", c_int),
+ ("gr_mem", POINTER(c_char_p))]
+
+
+def getgrnam_r(name, result_p, buffer_p, buflen):
+ """
+ ctypes wrapper for:
+ enum nss_status _nss_sss_getgrnam_r(const char *name,
+ struct group *result,
+ char *buffer,
+ size_t buflen,
+ int *errnop)
+ """
+ func = nss_sss_ctypes_loader("_nss_sss_getgrnam_r")
+ func.restype = c_int
+ func.argtypes = [c_char_p, POINTER(Group),
+ c_char_p, c_ulong, POINTER(c_int)]
+
+ errno = POINTER(c_int)(c_int(0))
+
+ res = func(c_char_p(name), result_p, buffer_p, buflen, errno)
+
+ return (int(res), int(errno[0]), result_p)
+
+
+def set_group_dict(res, result_p):
+ if res != NssReturnCode.SUCCESS:
+ return dict()
+
+ group_dict = dict()
+ group_dict['name'] = result_p[0].gr_name
+ group_dict['gid'] = result_p[0].gr_gid
+ group_dict['mem'] = list()
+
+ i = 0
+ while result_p[0].gr_mem[i] != None:
+ group_dict['mem'].append(result_p[0].gr_mem[i])
+ i = i+1
+
+ return group_dict
+
+
+def call_sssd_getgrnam(name):
+ """
+ A Python wrapper to retrieve a group. Returns:
+ (res, group_dict)
+ if res is NssReturnCode.SUCCESS, then group_dict contains the keys
+ corresponding to the C passwd structure fields. Otherwise, the dictionary
+ is empty and errno indicates the error code
+ """
+ result = Group()
+ result_p = POINTER(Group)(result)
+ buff = create_string_buffer(GROUP_BUFLEN)
+
+ res, errno, result_p = getgrnam_r(name, result_p, buff, GROUP_BUFLEN)
+ if errno != 0:
+ raise SssdNssError(errno, "getgrnam_r")
+
+ group_dict = set_group_dict(res, result_p)
+ return res, group_dict