Commit b8da03b pysss_nss_idmap: add python bindings for new sss_nss_idmap calls

2 files Authored by sbose 12 days ago , Committed by jhrozek 12 days ago ,
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>

    
  1 @@ -34,6 +34,8 @@
  2   enum lookup_type {
  3       SIDBYNAME,
  4       SIDBYID,
  5 +     SIDBYUID,
  6 +     SIDBYGID,
  7       NAMEBYSID,
  8       IDBYSID,
  9       NAMEBYCERT,
 10 @@ -155,7 +157,8 @@
 11       return ret;
 12   }
 13   
 14 - static int do_getsidbyid(PyObject *py_result, PyObject *py_id)
 15 + static int do_getsidbyid(enum lookup_type type, PyObject *py_result,
 16 +                          PyObject *py_id)
 17   {
 18       long id;
 19       const char *id_str;
 20 @@ -187,7 +190,19 @@
 21           return EINVAL;
 22       }
 23   
 24 -     ret = sss_nss_getsidbyid((uint32_t) id, &sid, &id_type);
 25 +     switch (type) {
 26 +     case SIDBYID:
 27 +         ret = sss_nss_getsidbyid((uint32_t) id, &sid, &id_type);
 28 +         break;
 29 +     case SIDBYUID:
 30 +         ret = sss_nss_getsidbyuid((uint32_t) id, &sid, &id_type);
 31 +         break;
 32 +     case SIDBYGID:
 33 +         ret = sss_nss_getsidbygid((uint32_t) id, &sid, &id_type);
 34 +         break;
 35 +     default:
 36 +         return EINVAL;
 37 +     }
 38       if (ret == 0) {
 39           ret = add_dict(py_result, py_id, PyUnicode_FromString(SSS_SID_KEY),
 40                          PyUnicode_FromString(sid), PYNUMBER_FROMLONG(id_type));
 41 @@ -302,7 +317,9 @@
 42           return do_getnamebysid(py_result, py_inp);
 43           break;
 44       case SIDBYID:
 45 -         return do_getsidbyid(py_result, py_inp);
 46 +     case SIDBYUID:
 47 +     case SIDBYGID:
 48 +         return do_getsidbyid(type, py_result, py_inp);
 49           break;
 50       case IDBYSID:
 51           return do_getidbysid(py_result, py_inp);
 52 @@ -334,7 +351,9 @@
 53   
 54       if (!(PyList_Check(obj) || PyTuple_Check(obj) ||
 55             PyBytes_Check(obj) || PyUnicode_Check(obj) ||
 56 -           (type == SIDBYID && (PYNUMBER_CHECK(obj))))) {
 57 +           ((type == SIDBYID
 58 +                 || type == SIDBYUID
 59 +                 || type == SIDBYGID) && (PYNUMBER_CHECK(obj))))) {
 60           PyErr_Format(PyExc_ValueError,
 61                        "Only string, long or list or tuples of them " \
 62                        "are accepted\n");
 63 @@ -355,7 +374,9 @@
 64               py_value = PySequence_GetItem(obj, i);
 65               if ((py_value != NULL) &&
 66                   (PyBytes_Check(py_value) || PyUnicode_Check(py_value) ||
 67 -                  (type == SIDBYID && PYNUMBER_CHECK(py_value)))) {
 68 +                  ((type == SIDBYID
 69 +                         || type == SIDBYUID
 70 +                         || type == SIDBYGID) && PYNUMBER_CHECK(py_value)))) {
 71                   ret = do_lookup(type, py_result, py_value);
 72                   if (ret != 0) {
 73                       /* Skip this name */
 74 @@ -418,6 +439,36 @@
 75       return check_args(SIDBYID, args);
 76   }
 77   
 78 + PyDoc_STRVAR(getsidbyuid_doc,
 79 + "getsidbyuid(uid or list/tuple of uid) -> dict(uid => dict(results))\n\
 80 + \n\
 81 + Returns a dictionary with a dictionary of results for each given POSIX UID.\n\
 82 + The result dictionary contain the SID and the type of the object which can be\n\
 83 + accessed with the key constants SID_KEY and TYPE_KEY, respectively. Since \n\
 84 + given ID is assumed to be a user ID is is not expected that group objects are\n\
 85 + returned."
 86 + );
 87 + 
 88 + static PyObject * py_getsidbyuid(PyObject *module, PyObject *args)
 89 + {
 90 +     return check_args(SIDBYUID, args);
 91 + }
 92 + 
 93 + PyDoc_STRVAR(getsidbygid_doc,
 94 + "getsidbygid(gid or list/tuple of gid) -> dict(gid => dict(results))\n\
 95 + \n\
 96 + Returns a dictionary with a dictionary of results for each given POSIX GID.\n\
 97 + The result dictionary contain the SID and the type of the object which can be\n\
 98 + accessed with the key constants SID_KEY and TYPE_KEY, respectively. Since \n\
 99 + given ID is assumed to be a group ID is is not expected that user objects are\n\
100 + returned."
101 + );
102 + 
103 + static PyObject * py_getsidbygid(PyObject *module, PyObject *args)
104 + {
105 +     return check_args(SIDBYGID, args);
106 + }
107 + 
108   PyDoc_STRVAR(getnamebysid_doc,
109   "getnamebysid(sid or list/tuple of sid) -> dict(sid => dict(results))\n\
110   \n\
111 @@ -484,6 +535,10 @@
112         METH_VARARGS, getsidbyname_doc },
113       { sss_py_const_p(char, "getsidbyid"), (PyCFunction) py_getsidbyid,
114         METH_VARARGS, getsidbyid_doc },
115 +     { sss_py_const_p(char, "getsidbyuid"), (PyCFunction) py_getsidbyuid,
116 +       METH_VARARGS, getsidbyuid_doc },
117 +     { sss_py_const_p(char, "getsidbygid"), (PyCFunction) py_getsidbygid,
118 +       METH_VARARGS, getsidbygid_doc },
119       { sss_py_const_p(char, "getnamebysid"), (PyCFunction) py_getnamebysid,
120         METH_VARARGS, getnamebysid_doc },
121       { sss_py_const_p(char, "getidbysid"), (PyCFunction) py_getidbysid,
 1 @@ -215,6 +215,13 @@
 2       assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_USER
 3       assert output[pysss_nss_idmap.SID_KEY] == user_sid
 4   
 5 +     output = pysss_nss_idmap.getsidbyuid(user_id)[user_id]
 6 +     assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_USER
 7 +     assert output[pysss_nss_idmap.SID_KEY] == user_sid
 8 + 
 9 +     output = pysss_nss_idmap.getsidbygid(user_id)
10 +     assert len(output) == 0
11 + 
12       output = pysss_nss_idmap.getidbysid(user_sid)[user_sid]
13       assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_USER
14       assert output[pysss_nss_idmap.ID_KEY] == user_id
15 @@ -237,6 +244,13 @@
16       assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
17       assert output[pysss_nss_idmap.SID_KEY] == group_sid
18   
19 +     output = pysss_nss_idmap.getsidbygid(group_id)[group_id]
20 +     assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
21 +     assert output[pysss_nss_idmap.SID_KEY] == group_sid
22 + 
23 +     output = pysss_nss_idmap.getsidbyuid(group_id)
24 +     assert len(output) == 0
25 + 
26       output = pysss_nss_idmap.getidbysid(group_sid)[group_sid]
27       assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
28       assert output[pysss_nss_idmap.ID_KEY] == group_id
29 @@ -260,6 +274,13 @@
30       assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
31       assert output[pysss_nss_idmap.SID_KEY] == group_sid
32   
33 +     output = pysss_nss_idmap.getsidbygid(group_id)[group_id]
34 +     assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
35 +     assert output[pysss_nss_idmap.SID_KEY] == group_sid
36 + 
37 +     output = pysss_nss_idmap.getsidbyuid(group_id)
38 +     assert len(output) == 0
39 + 
40       output = pysss_nss_idmap.getidbysid(group_sid)[group_sid]
41       assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
42       assert output[pysss_nss_idmap.ID_KEY] == group_id