dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0042-intg_test-Add-module-for-simulation-of-utility-id.patch

6cf099
From b47156c92867daaa2c30a7f4ad884a27273e54ff Mon Sep 17 00:00:00 2001
6cf099
From: Lukas Slebodnik <lslebodn@redhat.com>
6cf099
Date: Wed, 29 Jul 2015 14:35:52 +0200
6cf099
Subject: [PATCH 42/47] intg_test: Add module for simulation of utility id
6cf099
MIME-Version: 1.0
6cf099
Content-Type: text/plain; charset=UTF-8
6cf099
Content-Transfer-Encoding: 8bit
6cf099
6cf099
Reviewed-by: Michal Židek <mzidek@redhat.com>
6cf099
(cherry picked from commit 38b07019861240cf5107f5d51fc0027519e21619)
6cf099
---
6cf099
 src/tests/intg/Makefile.am |   1 +
6cf099
 src/tests/intg/sssd_id.py  | 119 +++++++++++++++++++++++++++++++++++++++++++++
6cf099
 2 files changed, 120 insertions(+)
6cf099
 create mode 100644 src/tests/intg/sssd_id.py
6cf099
6cf099
diff --git a/src/tests/intg/Makefile.am b/src/tests/intg/Makefile.am
6cf099
index 9383e112002baa7b0d200a3dc205aa2856089c97..7488022ed59dd894ba7b6808c5573706412098c7 100644
6cf099
--- a/src/tests/intg/Makefile.am
6cf099
+++ b/src/tests/intg/Makefile.am
6cf099
@@ -1,5 +1,6 @@
6cf099
 dist_noinst_DATA = \
6cf099
     config.py.m4 \
6cf099
+    sssd_id.py \
6cf099
     ds.py \
6cf099
     ds_openldap.py \
6cf099
     ent.py \
6cf099
diff --git a/src/tests/intg/sssd_id.py b/src/tests/intg/sssd_id.py
6cf099
new file mode 100644
6cf099
index 0000000000000000000000000000000000000000..45f2822b5b33d99b6a7cbbbba0450c774e05ff11
6cf099
--- /dev/null
6cf099
+++ b/src/tests/intg/sssd_id.py
6cf099
@@ -0,0 +1,119 @@
6cf099
+#
6cf099
+# Module for simulation of utility "id" from coreutils
6cf099
+#
6cf099
+# Copyright (c) 2015 Red Hat, Inc.
6cf099
+# Author: Lukas Slebodnik <lslebodn@redhat.com>
6cf099
+#
6cf099
+# This is free software; you can redistribute it and/or modify it
6cf099
+# under the terms of the GNU General Public License as published by
6cf099
+# the Free Software Foundation; version 2 only
6cf099
+#
6cf099
+# This program is distributed in the hope that it will be useful, but
6cf099
+# WITHOUT ANY WARRANTY; without even the implied warranty of
6cf099
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6cf099
+# General Public License for more details.
6cf099
+#
6cf099
+# You should have received a copy of the GNU General Public License
6cf099
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
6cf099
+#
6cf099
+import config
6cf099
+import pwd
6cf099
+import grp
6cf099
+from ctypes import (cdll, c_int, c_char, c_uint32, c_long, c_char_p,
6cf099
+                    POINTER, pointer)
6cf099
+
6cf099
+
6cf099
+class NssReturnCode(object):
6cf099
+    """ 'enum' class for name service switch retrn code """
6cf099
+    TRYAGAIN = -2,
6cf099
+    UNAVAIL = -1
6cf099
+    NOTFOUND = 0
6cf099
+    SUCCESS = 1
6cf099
+    RETURN = 2
6cf099
+
6cf099
+
6cf099
+def call_sssd_initgroups(user, gid):
6cf099
+    """
6cf099
+    Function will initialize the supplementary group access list
6cf099
+    for given user. It will gather groups only provided by sssd.
6cf099
+
6cf099
+    Arguments are the same as for C function initgroups
6cf099
+    @param string user name of user
6cf099
+    @param int gid the additional gid will be also added to the list.
6cf099
+
6cf099
+    @return (int, int, List[int]) (err, errno, gids)
6cf099
+        gids shoudl contain user group IDs if err is NssReturnCode.SUCCESS
6cf099
+        otherwise errno will contain non-zero vlaue.
6cf099
+    """
6cf099
+    libnss_sss_path = config.PREFIX + "/lib/libnss_sss.so.2"
6cf099
+    libnss_sss = cdll.LoadLibrary(libnss_sss_path)
6cf099
+
6cf099
+    func = libnss_sss._nss_sss_initgroups_dyn
6cf099
+    func.restype = c_int
6cf099
+    func.argtypes = [POINTER(c_char), c_uint32, POINTER(c_long),
6cf099
+                     POINTER(c_long), POINTER(POINTER(c_uint32)), c_long,
6cf099
+                     POINTER(c_int)]
6cf099
+
6cf099
+    start = POINTER(c_long)(c_long(0))
6cf099
+    size = POINTER(c_long)(c_long(0))
6cf099
+    groups = POINTER(c_uint32)()
6cf099
+    p_groups = pointer(groups)
6cf099
+    limit = c_long(-1)
6cf099
+    errno = POINTER(c_int)(c_int(0))
6cf099
+
6cf099
+    res = func(c_char_p(user), c_uint32(gid), start, size, p_groups, limit,
6cf099
+               errno)
6cf099
+
6cf099
+    gids = []
6cf099
+    if res == NssReturnCode.SUCCESS:
6cf099
+        gids_count = size[0]
6cf099
+        assert gids_count > 0, "_nss_sss_initgroups_dyn shoulld return " \
6cf099
+                               "one gid"
6cf099
+
6cf099
+        for i in range(0, gids_count):
6cf099
+            gids.append(int(p_groups.contents[i]))
6cf099
+
6cf099
+    return (int(res), errno[0], gids)
6cf099
+
6cf099
+
6cf099
+def get_user_gids(user):
6cf099
+    """
6cf099
+    Function will initialize the supplementary group access list
6cf099
+    for given user. It will gather groups only provided by sssd.
6cf099
+
6cf099
+    Arguments are the same as for C function initgroups
6cf099
+    @param string user name of user
6cf099
+
6cf099
+    @return (int, int, List[int]) (err, errno, gids)
6cf099
+        gids shoudl contain user group IDs if err is NssReturnCode.SUCCESS
6cf099
+        otherwise errno will contain non-zero vlaue.
6cf099
+    """
6cf099
+    pwd_user = pwd.getpwnam(user)
6cf099
+    uid = pwd_user.pw_uid
6cf099
+    gid = pwd_user.pw_gid
6cf099
+
6cf099
+    user = pwd.getpwuid(uid).pw_name
6cf099
+
6cf099
+    return call_sssd_initgroups(user, gid)
6cf099
+
6cf099
+
6cf099
+def get_user_groups(user):
6cf099
+    """
6cf099
+    Function will initialize the supplementary group access list
6cf099
+    for given user. It will gather groups only provided by sssd.
6cf099
+
6cf099
+    Arguments are the same as for C function initgroups
6cf099
+    @param string user name of user
6cf099
+
6cf099
+    @return (int, int, List[string]) (err, errno, groups)
6cf099
+        roups shoudl contain names of user groups
6cf099
+        if err is NssReturnCode.SUCCESS
6cf099
+        otherwise errno will contain non-zero vlaue.
6cf099
+    """
6cf099
+    (res, errno, gids) = get_user_gids(user)
6cf099
+    groups = []
6cf099
+
6cf099
+    if res == NssReturnCode.SUCCESS:
6cf099
+        groups = [grp.getgrgid(gid).gr_name for gid in gids]
6cf099
+
6cf099
+    return (res, errno, groups)
6cf099
-- 
6cf099
2.4.3
6cf099