diff options
author | Alexander Bokovoy <abokovoy@redhat.com> | 2013-07-19 16:22:50 +0300 |
---|---|---|
committer | Jakub Hrozek <jhrozek@redhat.com> | 2013-07-22 13:36:27 +0200 |
commit | 758ce3f01b4ed73c3bc35cd7039fac26fdf16386 (patch) | |
tree | ac8e9e260e5d15a9aae021c7dc0b1a08c314e0a3 | |
parent | 4b60fd691cc04455810d15da4f7f4044b92b07b3 (diff) | |
download | sssd-758ce3f01b4ed73c3bc35cd7039fac26fdf16386.tar.gz sssd-758ce3f01b4ed73c3bc35cd7039fac26fdf16386.tar.bz2 sssd-758ce3f01b4ed73c3bc35cd7039fac26fdf16386.zip |
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)
-rw-r--r-- | src/python/pysss.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/python/pysss.c b/src/python/pysss.c index 95cf781d..6ae9a252 100644 --- a/src/python/pysss.c +++ b/src/python/pysss.c @@ -746,6 +746,64 @@ 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" + "NOTE: The interface uses the system NSS calls and is not limited to " + "users served by the SSSD!\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 +1096,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 */ }; |