File 0025-pysss_nss_idmap-add-python-bindings-for-new-sss_nss_.patch of Package sssd.27545

From 5cb8daac9f22d0a944c2aa5c6a9f00663b5c756b Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose@redhat.com>
Date: Fri, 25 May 2018 21:34:24 +0200
Subject: [PATCH 7/8] pysss_nss_idmap: add python bindings for new
 sss_nss_idmap calls

Related to https://pagure.io/SSSD/sssd/issue/3629

Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
(cherry picked from commit b8da03b4234ea5536dc08c1627c710f0b64afc64)
---
 src/python/pysss_nss_idmap.c           | 65 ++++++++++++++++++++++++--
 src/tests/intg/test_pysss_nss_idmap.py | 21 +++++++++
 2 files changed, 81 insertions(+), 5 deletions(-)

diff --git a/src/python/pysss_nss_idmap.c b/src/python/pysss_nss_idmap.c
index 66d6dcc93..2bbec7d5e 100644
--- a/src/python/pysss_nss_idmap.c
+++ b/src/python/pysss_nss_idmap.c
@@ -34,6 +34,8 @@
 enum lookup_type {
     SIDBYNAME,
     SIDBYID,
+    SIDBYUID,
+    SIDBYGID,
     NAMEBYSID,
     IDBYSID,
     NAMEBYCERT,
@@ -155,7 +157,8 @@ static int do_getnamebysid(PyObject *py_result, PyObject *py_sid)
     return ret;
 }
 
-static int do_getsidbyid(PyObject *py_result, PyObject *py_id)
+static int do_getsidbyid(enum lookup_type type, PyObject *py_result,
+                         PyObject *py_id)
 {
     long id;
     const char *id_str;
@@ -187,7 +190,19 @@ static int do_getsidbyid(PyObject *py_result, PyObject *py_id)
         return EINVAL;
     }
 
-    ret = sss_nss_getsidbyid((uint32_t) id, &sid, &id_type);
+    switch (type) {
+    case SIDBYID:
+        ret = sss_nss_getsidbyid((uint32_t) id, &sid, &id_type);
+        break;
+    case SIDBYUID:
+        ret = sss_nss_getsidbyuid((uint32_t) id, &sid, &id_type);
+        break;
+    case SIDBYGID:
+        ret = sss_nss_getsidbygid((uint32_t) id, &sid, &id_type);
+        break;
+    default:
+        return EINVAL;
+    }
     if (ret == 0) {
         ret = add_dict(py_result, py_id, PyUnicode_FromString(SSS_SID_KEY),
                        PyUnicode_FromString(sid), PYNUMBER_FROMLONG(id_type));
@@ -302,7 +317,9 @@ static int do_lookup(enum lookup_type type, PyObject *py_result,
         return do_getnamebysid(py_result, py_inp);
         break;
     case SIDBYID:
-        return do_getsidbyid(py_result, py_inp);
+    case SIDBYUID:
+    case SIDBYGID:
+        return do_getsidbyid(type, py_result, py_inp);
         break;
     case IDBYSID:
         return do_getidbysid(py_result, py_inp);
@@ -334,7 +351,9 @@ static PyObject *check_args(enum lookup_type type, PyObject *args)
 
     if (!(PyList_Check(obj) || PyTuple_Check(obj) ||
           PyBytes_Check(obj) || PyUnicode_Check(obj) ||
-          (type == SIDBYID && (PYNUMBER_CHECK(obj))))) {
+          ((type == SIDBYID
+                || type == SIDBYUID
+                || type == SIDBYGID) && (PYNUMBER_CHECK(obj))))) {
         PyErr_Format(PyExc_ValueError,
                      "Only string, long or list or tuples of them " \
                      "are accepted\n");
@@ -355,7 +374,9 @@ static PyObject *check_args(enum lookup_type type, PyObject *args)
             py_value = PySequence_GetItem(obj, i);
             if ((py_value != NULL) &&
                 (PyBytes_Check(py_value) || PyUnicode_Check(py_value) ||
-                 (type == SIDBYID && PYNUMBER_CHECK(py_value)))) {
+                 ((type == SIDBYID
+                        || type == SIDBYUID
+                        || type == SIDBYGID) && PYNUMBER_CHECK(py_value)))) {
                 ret = do_lookup(type, py_result, py_value);
                 if (ret != 0) {
                     /* Skip this name */
@@ -418,6 +439,36 @@ static PyObject * py_getsidbyid(PyObject *module, PyObject *args)
     return check_args(SIDBYID, args);
 }
 
+PyDoc_STRVAR(getsidbyuid_doc,
+"getsidbyuid(uid or list/tuple of uid) -> dict(uid => dict(results))\n\
+\n\
+Returns a dictionary with a dictionary of results for each given POSIX UID.\n\
+The result dictionary contain the SID and the type of the object which can be\n\
+accessed with the key constants SID_KEY and TYPE_KEY, respectively. Since \n\
+given ID is assumed to be a user ID is is not expected that group objects are\n\
+returned."
+);
+
+static PyObject * py_getsidbyuid(PyObject *module, PyObject *args)
+{
+    return check_args(SIDBYUID, args);
+}
+
+PyDoc_STRVAR(getsidbygid_doc,
+"getsidbygid(gid or list/tuple of gid) -> dict(gid => dict(results))\n\
+\n\
+Returns a dictionary with a dictionary of results for each given POSIX GID.\n\
+The result dictionary contain the SID and the type of the object which can be\n\
+accessed with the key constants SID_KEY and TYPE_KEY, respectively. Since \n\
+given ID is assumed to be a group ID is is not expected that user objects are\n\
+returned."
+);
+
+static PyObject * py_getsidbygid(PyObject *module, PyObject *args)
+{
+    return check_args(SIDBYGID, args);
+}
+
 PyDoc_STRVAR(getnamebysid_doc,
 "getnamebysid(sid or list/tuple of sid) -> dict(sid => dict(results))\n\
 \n\
@@ -484,6 +535,10 @@ static PyMethodDef methods[] = {
       METH_VARARGS, getsidbyname_doc },
     { sss_py_const_p(char, "getsidbyid"), (PyCFunction) py_getsidbyid,
       METH_VARARGS, getsidbyid_doc },
+    { sss_py_const_p(char, "getsidbyuid"), (PyCFunction) py_getsidbyuid,
+      METH_VARARGS, getsidbyuid_doc },
+    { sss_py_const_p(char, "getsidbygid"), (PyCFunction) py_getsidbygid,
+      METH_VARARGS, getsidbygid_doc },
     { sss_py_const_p(char, "getnamebysid"), (PyCFunction) py_getnamebysid,
       METH_VARARGS, getnamebysid_doc },
     { sss_py_const_p(char, "getidbysid"), (PyCFunction) py_getidbysid,
diff --git a/src/tests/intg/test_pysss_nss_idmap.py b/src/tests/intg/test_pysss_nss_idmap.py
index aed2a8cf9..8d0d9b794 100644
--- a/src/tests/intg/test_pysss_nss_idmap.py
+++ b/src/tests/intg/test_pysss_nss_idmap.py
@@ -215,6 +215,13 @@ def test_user_operations(ldap_conn, simple_ad):
     assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_USER
     assert output[pysss_nss_idmap.SID_KEY] == user_sid
 
+    output = pysss_nss_idmap.getsidbyuid(user_id)[user_id]
+    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_USER
+    assert output[pysss_nss_idmap.SID_KEY] == user_sid
+
+    output = pysss_nss_idmap.getsidbygid(user_id)
+    assert len(output) == 0
+
     output = pysss_nss_idmap.getidbysid(user_sid)[user_sid]
     assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_USER
     assert output[pysss_nss_idmap.ID_KEY] == user_id
@@ -237,6 +244,13 @@ def test_group_operations(ldap_conn, simple_ad):
     assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
     assert output[pysss_nss_idmap.SID_KEY] == group_sid
 
+    output = pysss_nss_idmap.getsidbygid(group_id)[group_id]
+    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
+    assert output[pysss_nss_idmap.SID_KEY] == group_sid
+
+    output = pysss_nss_idmap.getsidbyuid(group_id)
+    assert len(output) == 0
+
     output = pysss_nss_idmap.getidbysid(group_sid)[group_sid]
     assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
     assert output[pysss_nss_idmap.ID_KEY] == group_id
@@ -260,6 +274,13 @@ def test_case_insensitive(ldap_conn, simple_ad):
     assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
     assert output[pysss_nss_idmap.SID_KEY] == group_sid
 
+    output = pysss_nss_idmap.getsidbygid(group_id)[group_id]
+    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
+    assert output[pysss_nss_idmap.SID_KEY] == group_sid
+
+    output = pysss_nss_idmap.getsidbyuid(group_id)
+    assert len(output) == 0
+
     output = pysss_nss_idmap.getidbysid(group_sid)[group_sid]
     assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
     assert output[pysss_nss_idmap.ID_KEY] == group_id
-- 
2.23.0

openSUSE Build Service is sponsored by