[SSSD] [PATCH] add pysss.getgrouplist(username)

Alexander Bokovoy abokovoy at redhat.com
Fri Jul 19 13:29:37 UTC 2013


Hi!

Apparently, getgrouplist(3) call is not available in Python older than
Python 3.3. So I agreed with Jakub to have it bound to pysss Python
module. We need this call to obtain list of groups trusted domain user
belongs to for HBAC testing in FreeIPA.

Additionally, I've fixed bug with linking of pysss. This patch is
relevant to 1.10 as well, while the first one is needed in sssd 1.11.


-- 
/ Alexander Bokovoy
-------------- next part --------------
>From 37261eda5eddad3edf94e1aefc6d912bf1490a2d Mon Sep 17 00:00:00 2001
From: Alexander Bokovoy <abokovoy at redhat.com>
Date: Fri, 19 Jul 2013 16:22:10 +0300
Subject: [PATCH 1/2] build: fix dependencies for pysss module

https://fedorahosted.org/sssd/ticket/2025
---
 Makefile.am | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Makefile.am b/Makefile.am
index 5158434..9498d50 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1758,6 +1758,7 @@ pysss_la_CFLAGS = \
     $(AM_CFLAGS)  \
     $(PYTHON_CFLAGS)
 pysss_la_LIBADD = \
+    $(SSSD_INTERNAL_LTLIBS) \
     $(PYTHON_BINDINGS_LIBS) \
     $(PYTHON_LIBS)
 pysss_la_LDFLAGS = \
-- 
1.8.3.1

-------------- next part --------------
>From 01a744333edeefed5fd0e2958914c474821e7282 Mon Sep 17 00:00:00 2001
From: Alexander Bokovoy <abokovoy at redhat.com>
Date: Fri, 19 Jul 2013 16:22:50 +0300
Subject: [PATCH 2/2] pysss: add pysss.getgrouplist(username)

getgrouplist(3) call is missing from Python older than Python 3.3

Introduce supplementary binding to provide getgrouplist as part of pysss
interface. Since getgrouplist() can be run against any domain, place
it at top module rather than pysss.local namespace.

pysss.getgrouplist(username) -> tuple(group list as strings)
---
 src/python/pysss.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

diff --git a/src/python/pysss.c b/src/python/pysss.c
index 95cf781..a2924ff 100644
--- a/src/python/pysss.c
+++ b/src/python/pysss.c
@@ -746,6 +746,62 @@ fail:
     return NULL;
 }
 
+/*
+ * Get list of groups user belongs to
+ */
+PyDoc_STRVAR(py_sss_getgrouplist__doc__,
+    "Get list of groups user belongs to.\n\n"
+    ":param username: name of user to get list for\n");
+
+static PyObject *py_sss_getgrouplist(PyObject *self, PyObject *args)
+{
+    char *username = NULL;
+    gid_t *groups = NULL;
+    struct passwd *pw;
+    struct group *gr;
+    int ngroups;
+    int ret;
+    Py_ssize_t i;
+    PyObject *groups_tuple;
+
+    if(!PyArg_ParseTuple(args, discard_const_p(char, "s"), &username)) {
+        goto fail;
+    }
+
+    pw = getpwnam(username);
+    if (pw == NULL) {
+        goto fail;
+    }
+
+    ngroups = 32;
+    groups = malloc(sizeof(gid_t) * ngroups);
+    if (groups == NULL) {
+        goto fail;
+    }
+
+    do {
+        ret = getgrouplist(username, pw->pw_gid, groups, &ngroups);
+        if (ret < ngroups) {
+            groups = realloc(groups, ngroups * sizeof(gid_t));
+        }
+    } while (ret != ngroups);
+
+    groups_tuple = PyTuple_New((Py_ssize_t) ngroups);
+    if (groups_tuple == NULL) {
+        goto fail;
+    }
+
+    for (i = 0; i < ngroups; i++) {
+        gr = getgrgid(groups[i]);
+        PyTuple_SetItem(groups_tuple, i, PyString_FromString(gr->gr_name));
+    }
+
+    return groups_tuple;
+
+fail:
+    free(groups);
+    return NULL;
+}
 
 /*** python plumbing begins here ***/
 
@@ -1038,6 +1094,7 @@ static PyTypeObject pysss_password_type = {
  * Module methods
  */
 static PyMethodDef module_methods[] = {
+        {"getgrouplist", py_sss_getgrouplist, METH_VARARGS, py_sss_getgrouplist__doc__},
         {NULL, NULL, 0, NULL}  /* Sentinel */
 };
 
-- 
1.8.3.1



More information about the sssd-devel mailing list