Blame SOURCES/0012-nss-idmap-add-sss_nss_getlistbycert.patch

bb7cd1
From f2a81a22124e93a026ec0f06b77eab50998ecba5 Mon Sep 17 00:00:00 2001
bb7cd1
From: Sumit Bose <sbose@redhat.com>
bb7cd1
Date: Wed, 15 Mar 2017 14:21:26 +0100
bb7cd1
Subject: [PATCH 12/15] nss-idmap: add sss_nss_getlistbycert()
bb7cd1
bb7cd1
This patch adds a getlistbycert() call to libsss_nss_idmap to make it on
bb7cd1
par with InfoPipe.
bb7cd1
bb7cd1
Related to https://pagure.io/SSSD/sssd/issue/3050
bb7cd1
bb7cd1
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
bb7cd1
---
bb7cd1
 Makefile.am                                |   2 +-
bb7cd1
 src/python/pysss_nss_idmap.c               | 103 ++++++++++++++++++-
bb7cd1
 src/responder/nss/nss_cmd.c                |   7 ++
bb7cd1
 src/responder/nss/nss_protocol.h           |   6 ++
bb7cd1
 src/responder/nss/nss_protocol_sid.c       |  63 ++++++++++++
bb7cd1
 src/sss_client/idmap/sss_nss_idmap.c       | 110 +++++++++++++++++++-
bb7cd1
 src/sss_client/idmap/sss_nss_idmap.exports |   6 ++
bb7cd1
 src/sss_client/idmap/sss_nss_idmap.h       |  17 +++-
bb7cd1
 src/sss_client/sss_cli.h                   |   5 +
bb7cd1
 src/tests/cmocka/test_nss_srv.c            | 158 +++++++++++++++++++++++++++++
bb7cd1
 10 files changed, 471 insertions(+), 6 deletions(-)
bb7cd1
bb7cd1
diff --git a/Makefile.am b/Makefile.am
bb7cd1
index bd0ca0d303e1742ad26c7648cd24e2c0135af34e..7516338bc6fd95045d20db8155a0c82fd7003358 100644
bb7cd1
--- a/Makefile.am
bb7cd1
+++ b/Makefile.am
bb7cd1
@@ -1128,7 +1128,7 @@ libsss_nss_idmap_la_LIBADD = \
bb7cd1
     $(CLIENT_LIBS)
bb7cd1
 libsss_nss_idmap_la_LDFLAGS = \
bb7cd1
     -Wl,--version-script,$(srcdir)/src/sss_client/idmap/sss_nss_idmap.exports \
bb7cd1
-    -version-info 2:0:2
bb7cd1
+    -version-info 3:0:3
bb7cd1
 
bb7cd1
 dist_noinst_DATA += src/sss_client/idmap/sss_nss_idmap.exports
bb7cd1
 
bb7cd1
diff --git a/src/python/pysss_nss_idmap.c b/src/python/pysss_nss_idmap.c
bb7cd1
index c57cc10a86a7a9a22a791c1eae027a1aafa8f780..2e5851c7a6e48629fd93e428aada499fcbe36ebb 100644
bb7cd1
--- a/src/python/pysss_nss_idmap.c
bb7cd1
+++ b/src/python/pysss_nss_idmap.c
bb7cd1
@@ -36,9 +36,37 @@ enum lookup_type {
bb7cd1
     SIDBYID,
bb7cd1
     NAMEBYSID,
bb7cd1
     IDBYSID,
bb7cd1
-    NAMEBYCERT
bb7cd1
+    NAMEBYCERT,
bb7cd1
+    LISTBYCERT
bb7cd1
 };
bb7cd1
 
bb7cd1
+static int add_dict_to_list(PyObject *py_list, PyObject *res_type,
bb7cd1
+                            PyObject *res, PyObject *id_type)
bb7cd1
+{
bb7cd1
+    int ret;
bb7cd1
+    PyObject *py_dict;
bb7cd1
+
bb7cd1
+    py_dict =  PyDict_New();
bb7cd1
+    if (py_dict == NULL) {
bb7cd1
+        return ENOMEM;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    ret = PyDict_SetItem(py_dict, res_type, res);
bb7cd1
+    if (ret != 0) {
bb7cd1
+        Py_XDECREF(py_dict);
bb7cd1
+        return ret;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    ret = PyDict_SetItem(py_dict, PyBytes_FromString(SSS_TYPE_KEY), id_type);
bb7cd1
+    if (ret != 0) {
bb7cd1
+        Py_XDECREF(py_dict);
bb7cd1
+        return ret;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    ret = PyList_Append(py_list, py_dict);
bb7cd1
+
bb7cd1
+    return ret;
bb7cd1
+}
bb7cd1
 static int add_dict(PyObject *py_result, PyObject *key, PyObject *res_type,
bb7cd1
                     PyObject *res, PyObject *id_type)
bb7cd1
 {
bb7cd1
@@ -191,6 +219,57 @@ static int do_getnamebycert(PyObject *py_result, PyObject *py_cert)
bb7cd1
     return ret;
bb7cd1
 }
bb7cd1
 
bb7cd1
+static int do_getlistbycert(PyObject *py_result, PyObject *py_cert)
bb7cd1
+{
bb7cd1
+    int ret;
bb7cd1
+    const char *cert;
bb7cd1
+    char **names = NULL;
bb7cd1
+    enum sss_id_type *id_types = NULL;
bb7cd1
+    size_t c;
bb7cd1
+
bb7cd1
+    cert = py_string_or_unicode_as_string(py_cert);
bb7cd1
+    if (cert == NULL) {
bb7cd1
+        return EINVAL;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    ret = sss_nss_getlistbycert(cert, &names, &id_types);
bb7cd1
+    if (ret == 0) {
bb7cd1
+
bb7cd1
+        PyObject *py_list;
bb7cd1
+
bb7cd1
+        py_list =  PyList_New(0);
bb7cd1
+        if (py_list == NULL) {
bb7cd1
+            return ENOMEM;
bb7cd1
+        }
bb7cd1
+
bb7cd1
+        for (c = 0; names[c] != NULL; c++) {
bb7cd1
+            ret = add_dict_to_list(py_list,
bb7cd1
+                                   PyBytes_FromString(SSS_NAME_KEY),
bb7cd1
+                                   PyUnicode_FromString(names[c]),
bb7cd1
+                                   PYNUMBER_FROMLONG(id_types[c]));
bb7cd1
+            if (ret != 0) {
bb7cd1
+                goto done;
bb7cd1
+            }
bb7cd1
+        }
bb7cd1
+        ret = PyDict_SetItem(py_result, py_cert, py_list);
bb7cd1
+        if (ret != 0) {
bb7cd1
+            goto done;
bb7cd1
+        }
bb7cd1
+    }
bb7cd1
+
bb7cd1
+done:
bb7cd1
+    free(id_types);
bb7cd1
+    if (names != NULL) {
bb7cd1
+        for (c = 0; names[c] != NULL; c++) {
bb7cd1
+            free(names[c]);
bb7cd1
+        }
bb7cd1
+        free(names);
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    return ret;
bb7cd1
+}
bb7cd1
+
bb7cd1
+
bb7cd1
 static int do_getidbysid(PyObject *py_result, PyObject *py_sid)
bb7cd1
 {
bb7cd1
     const char *sid;
bb7cd1
@@ -231,6 +310,9 @@ static int do_lookup(enum lookup_type type, PyObject *py_result,
bb7cd1
     case NAMEBYCERT:
bb7cd1
         return do_getnamebycert(py_result, py_inp);
bb7cd1
         break;
bb7cd1
+    case LISTBYCERT:
bb7cd1
+        return do_getlistbycert(py_result, py_inp);
bb7cd1
+        break;
bb7cd1
     default:
bb7cd1
         return ENOSYS;
bb7cd1
     }
bb7cd1
@@ -368,7 +450,7 @@ static PyObject * py_getidbysid(PyObject *module, PyObject *args)
bb7cd1
 }
bb7cd1
 
bb7cd1
 PyDoc_STRVAR(getnamebycert_doc,
bb7cd1
-"getnamebycert(sid or list/tuple of certificates) -> dict(sid => dict(results))\n\
bb7cd1
+"getnamebycert(certificate or list/tuple of certificates) -> dict(certificate => dict(results))\n\
bb7cd1
 \n\
bb7cd1
 Returns a dictionary with a dictonary of results for each given certificates.\n\
bb7cd1
 The result dictonary contain the name and the type of the object which can be\n\
bb7cd1
@@ -382,6 +464,21 @@ static PyObject * py_getnamebycert(PyObject *module, PyObject *args)
bb7cd1
     return check_args(NAMEBYCERT, args);
bb7cd1
 }
bb7cd1
 
bb7cd1
+PyDoc_STRVAR(getlistbycert_doc,
bb7cd1
+"getnamebycert(certificate or list/tuple of certificates) -> dict(certificate => dict(results))\n\
bb7cd1
+\n\
bb7cd1
+Returns a dictionary with a dictonary of results for each given certificates.\n\
bb7cd1
+The result dictonary contain the name and the type of the object which can be\n\
bb7cd1
+accessed with the key constants NAME_KEY and TYPE_KEY, respectively.\n\
bb7cd1
+\n\
bb7cd1
+NOTE: getlistbycert currently works only with id_provider set as \"ad\" or \"ipa\""
bb7cd1
+);
bb7cd1
+
bb7cd1
+static PyObject * py_getlistbycert(PyObject *module, PyObject *args)
bb7cd1
+{
bb7cd1
+    return check_args(LISTBYCERT, args);
bb7cd1
+}
bb7cd1
+
bb7cd1
 static PyMethodDef methods[] = {
bb7cd1
     { sss_py_const_p(char, "getsidbyname"), (PyCFunction) py_getsidbyname,
bb7cd1
       METH_VARARGS, getsidbyname_doc },
bb7cd1
@@ -393,6 +490,8 @@ static PyMethodDef methods[] = {
bb7cd1
       METH_VARARGS, getidbysid_doc },
bb7cd1
     { sss_py_const_p(char, "getnamebycert"), (PyCFunction) py_getnamebycert,
bb7cd1
       METH_VARARGS, getnamebycert_doc },
bb7cd1
+    { sss_py_const_p(char, "getlistbycert"), (PyCFunction) py_getlistbycert,
bb7cd1
+      METH_VARARGS, getlistbycert_doc },
bb7cd1
     { NULL,NULL, 0, NULL }
bb7cd1
 };
bb7cd1
 
bb7cd1
diff --git a/src/responder/nss/nss_cmd.c b/src/responder/nss/nss_cmd.c
bb7cd1
index 08b3d32f2662efc1cc803f6e9e5f2d064f7d3033..1931bf62a686c7f30852dac547866609cf54a81b 100644
bb7cd1
--- a/src/responder/nss/nss_cmd.c
bb7cd1
+++ b/src/responder/nss/nss_cmd.c
bb7cd1
@@ -932,6 +932,12 @@ static errno_t nss_cmd_getnamebycert(struct cli_ctx *cli_ctx)
bb7cd1
                           nss_protocol_fill_single_name);
bb7cd1
 }
bb7cd1
 
bb7cd1
+static errno_t nss_cmd_getlistbycert(struct cli_ctx *cli_ctx)
bb7cd1
+{
bb7cd1
+    return nss_getby_cert(cli_ctx, CACHE_REQ_USER_BY_CERT,
bb7cd1
+                          nss_protocol_fill_name_list);
bb7cd1
+}
bb7cd1
+
bb7cd1
 struct sss_cmd_table *get_nss_cmds(void)
bb7cd1
 {
bb7cd1
     static struct sss_cmd_table nss_cmds[] = {
bb7cd1
@@ -961,6 +967,7 @@ struct sss_cmd_table *get_nss_cmds(void)
bb7cd1
         { SSS_NSS_GETIDBYSID, nss_cmd_getidbysid },
bb7cd1
         { SSS_NSS_GETORIGBYNAME, nss_cmd_getorigbyname },
bb7cd1
         { SSS_NSS_GETNAMEBYCERT, nss_cmd_getnamebycert },
bb7cd1
+        { SSS_NSS_GETLISTBYCERT, nss_cmd_getlistbycert },
bb7cd1
         { SSS_CLI_NULL, NULL }
bb7cd1
     };
bb7cd1
 
bb7cd1
diff --git a/src/responder/nss/nss_protocol.h b/src/responder/nss/nss_protocol.h
bb7cd1
index c94e7b911eb3c0f97b8c06b1766573311cde41ae..e4c0e52c0e642e885ef2c8423ea564beff7242cf 100644
bb7cd1
--- a/src/responder/nss/nss_protocol.h
bb7cd1
+++ b/src/responder/nss/nss_protocol.h
bb7cd1
@@ -175,6 +175,12 @@ nss_protocol_fill_single_name(struct nss_ctx *nss_ctx,
bb7cd1
                               struct cache_req_result *result);
bb7cd1
 
bb7cd1
 errno_t
bb7cd1
+nss_protocol_fill_name_list(struct nss_ctx *nss_ctx,
bb7cd1
+                            struct nss_cmd_ctx *cmd_ctx,
bb7cd1
+                            struct sss_packet *packet,
bb7cd1
+                            struct cache_req_result *result);
bb7cd1
+
bb7cd1
+errno_t
bb7cd1
 nss_protocol_fill_id(struct nss_ctx *nss_ctx,
bb7cd1
                      struct nss_cmd_ctx *cmd_ctx,
bb7cd1
                      struct sss_packet *packet,
bb7cd1
diff --git a/src/responder/nss/nss_protocol_sid.c b/src/responder/nss/nss_protocol_sid.c
bb7cd1
index 0b97e65f75412d40832d861568d8e2f9de5e1732..a6a4e27d039c67ef98f6d5900d5e3fcadb3ee717 100644
bb7cd1
--- a/src/responder/nss/nss_protocol_sid.c
bb7cd1
+++ b/src/responder/nss/nss_protocol_sid.c
bb7cd1
@@ -498,3 +498,66 @@ nss_protocol_fill_id(struct nss_ctx *nss_ctx,
bb7cd1
 
bb7cd1
     return EOK;
bb7cd1
 }
bb7cd1
+
bb7cd1
+errno_t
bb7cd1
+nss_protocol_fill_name_list(struct nss_ctx *nss_ctx,
bb7cd1
+                            struct nss_cmd_ctx *cmd_ctx,
bb7cd1
+                            struct sss_packet *packet,
bb7cd1
+                            struct cache_req_result *result)
bb7cd1
+{
bb7cd1
+    enum sss_id_type *id_types;
bb7cd1
+    size_t rp = 0;
bb7cd1
+    size_t body_len;
bb7cd1
+    uint8_t *body;
bb7cd1
+    errno_t ret;
bb7cd1
+    struct sized_string *sz_names;
bb7cd1
+    size_t len;
bb7cd1
+    size_t c;
bb7cd1
+    const char *tmp_str;
bb7cd1
+
bb7cd1
+    sz_names = talloc_array(cmd_ctx, struct sized_string, result->count);
bb7cd1
+    if (sz_names == NULL) {
bb7cd1
+        return ENOMEM;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    id_types = talloc_array(cmd_ctx, enum sss_id_type, result->count);
bb7cd1
+    if (id_types == NULL) {
bb7cd1
+        return ENOMEM;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    len = 0;
bb7cd1
+    for (c = 0; c < result->count; c++) {
bb7cd1
+        ret = nss_get_id_type(cmd_ctx, result, &(id_types[c]));
bb7cd1
+        if (ret != EOK) {
bb7cd1
+            return ret;
bb7cd1
+        }
bb7cd1
+
bb7cd1
+        tmp_str = nss_get_name_from_msg(result->domain, result->msgs[c]);
bb7cd1
+        if (tmp_str == NULL) {
bb7cd1
+            return EINVAL;
bb7cd1
+        }
bb7cd1
+        to_sized_string(&(sz_names[c]), tmp_str);
bb7cd1
+
bb7cd1
+        len += sz_names[c].len;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    len += (2 + result->count) * sizeof(uint32_t);
bb7cd1
+
bb7cd1
+    ret = sss_packet_grow(packet, len);
bb7cd1
+    if (ret != EOK) {
bb7cd1
+        DEBUG(SSSDBG_OP_FAILURE, "sss_packet_grow failed.\n");
bb7cd1
+        return ret;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    sss_packet_get_body(packet, &body, &body_len);
bb7cd1
+
bb7cd1
+    SAFEALIGN_SET_UINT32(&body[rp], result->count, &rp); /* Num results. */
bb7cd1
+    SAFEALIGN_SET_UINT32(&body[rp], 0, &rp); /* Reserved. */
bb7cd1
+    for (c = 0; c < result->count; c++) {
bb7cd1
+        SAFEALIGN_SET_UINT32(&body[rp], id_types[c], &rp);
bb7cd1
+        SAFEALIGN_SET_STRING(&body[rp], sz_names[c].str, sz_names[c].len,
bb7cd1
+                             &rp);
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    return EOK;
bb7cd1
+}
bb7cd1
diff --git a/src/sss_client/idmap/sss_nss_idmap.c b/src/sss_client/idmap/sss_nss_idmap.c
bb7cd1
index fa5a499e3606f7e45a406de4d63002ba35365cb1..6f3af267a1e763e7dce77e3862be377ae2bfe984 100644
bb7cd1
--- a/src/sss_client/idmap/sss_nss_idmap.c
bb7cd1
+++ b/src/sss_client/idmap/sss_nss_idmap.c
bb7cd1
@@ -31,6 +31,7 @@
bb7cd1
 #include "util/strtonum.h"
bb7cd1
 
bb7cd1
 #define DATA_START (3 * sizeof(uint32_t))
bb7cd1
+#define LIST_START (2 * sizeof(uint32_t))
bb7cd1
 union input {
bb7cd1
     const char *str;
bb7cd1
     uint32_t id;
bb7cd1
@@ -38,10 +39,12 @@ union input {
bb7cd1
 
bb7cd1
 struct output {
bb7cd1
     enum sss_id_type type;
bb7cd1
+    enum sss_id_type *types;
bb7cd1
     union {
bb7cd1
         char *str;
bb7cd1
         uint32_t id;
bb7cd1
         struct sss_nss_kv *kv_list;
bb7cd1
+        char **names;
bb7cd1
     } d;
bb7cd1
 };
bb7cd1
 
bb7cd1
@@ -72,6 +75,63 @@ void sss_nss_free_kv(struct sss_nss_kv *kv_list)
bb7cd1
     }
bb7cd1
 }
bb7cd1
 
bb7cd1
+void sss_nss_free_list(char **l)
bb7cd1
+{
bb7cd1
+    size_t c;
bb7cd1
+
bb7cd1
+    if (l != NULL) {
bb7cd1
+        for (c = 0; l[c] != NULL; c++) {
bb7cd1
+            free(l[c]);
bb7cd1
+        }
bb7cd1
+        free(l);
bb7cd1
+    }
bb7cd1
+}
bb7cd1
+
bb7cd1
+static int buf_to_name_type_list(uint8_t *buf, size_t buf_len, uint32_t num,
bb7cd1
+                                 char ***names, enum sss_id_type **types)
bb7cd1
+{
bb7cd1
+    int ret;
bb7cd1
+    size_t c;
bb7cd1
+    char **n = NULL;
bb7cd1
+    enum sss_id_type *t = NULL;
bb7cd1
+    size_t rp = 0;
bb7cd1
+
bb7cd1
+    n = calloc(num + 1, sizeof(char *));
bb7cd1
+    if (n == NULL) {
bb7cd1
+        ret = ENOMEM;
bb7cd1
+        goto done;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    t = calloc(num + 1, sizeof(enum sss_id_type));
bb7cd1
+    if (t == NULL) {
bb7cd1
+        ret = ENOMEM;
bb7cd1
+        goto done;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    for (c = 0; c < num; c++) {
bb7cd1
+        SAFEALIGN_COPY_UINT32(&(t[c]), buf + rp, &rp);
bb7cd1
+        n[c] = strdup((char *) buf + rp);
bb7cd1
+        if (n[c] == NULL) {
bb7cd1
+            ret = ENOMEM;
bb7cd1
+            goto done;
bb7cd1
+        }
bb7cd1
+        rp += strlen(n[c]) + 1;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    ret = EOK;
bb7cd1
+
bb7cd1
+done:
bb7cd1
+    if (ret != EOK) {
bb7cd1
+        sss_nss_free_list(n);
bb7cd1
+        free(t);
bb7cd1
+    } else {
bb7cd1
+        *names = n;
bb7cd1
+        *types = t;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    return ret;
bb7cd1
+}
bb7cd1
+
bb7cd1
 static int  buf_to_kv_list(uint8_t *buf, size_t buf_len,
bb7cd1
                            struct sss_nss_kv **kv_list)
bb7cd1
 {
bb7cd1
@@ -153,13 +213,14 @@ static int sss_nss_getyyybyxxx(union input inp, enum sss_cli_command cmd ,
bb7cd1
     size_t data_len;
bb7cd1
     uint32_t c;
bb7cd1
     struct sss_nss_kv *kv_list;
bb7cd1
+    char **names;
bb7cd1
+    enum sss_id_type *types;
bb7cd1
 
bb7cd1
     switch (cmd) {
bb7cd1
     case SSS_NSS_GETSIDBYNAME:
bb7cd1
     case SSS_NSS_GETNAMEBYSID:
bb7cd1
     case SSS_NSS_GETIDBYSID:
bb7cd1
     case SSS_NSS_GETORIGBYNAME:
bb7cd1
-    case SSS_NSS_GETNAMEBYCERT:
bb7cd1
         ret = sss_strnlen(inp.str, 2048, &inp_len);
bb7cd1
         if (ret != EOK) {
bb7cd1
             return EINVAL;
bb7cd1
@@ -169,6 +230,17 @@ static int sss_nss_getyyybyxxx(union input inp, enum sss_cli_command cmd ,
bb7cd1
         rd.data = inp.str;
bb7cd1
 
bb7cd1
         break;
bb7cd1
+    case SSS_NSS_GETNAMEBYCERT:
bb7cd1
+    case SSS_NSS_GETLISTBYCERT:
bb7cd1
+        ret = sss_strnlen(inp.str, 10 * 1024 , &inp_len);
bb7cd1
+        if (ret != EOK) {
bb7cd1
+            return EINVAL;
bb7cd1
+        }
bb7cd1
+
bb7cd1
+        rd.len = inp_len + 1;
bb7cd1
+        rd.data = inp.str;
bb7cd1
+
bb7cd1
+        break;
bb7cd1
     case SSS_NSS_GETSIDBYID:
bb7cd1
         rd.len = sizeof(uint32_t);
bb7cd1
         rd.data = &inp.id;
bb7cd1
@@ -195,7 +267,7 @@ static int sss_nss_getyyybyxxx(union input inp, enum sss_cli_command cmd ,
bb7cd1
     if (num_results == 0) {
bb7cd1
         ret = ENOENT;
bb7cd1
         goto done;
bb7cd1
-    } else if (num_results > 1) {
bb7cd1
+    } else if (num_results > 1 && cmd != SSS_NSS_GETLISTBYCERT) {
bb7cd1
         ret = EBADMSG;
bb7cd1
         goto done;
bb7cd1
     }
bb7cd1
@@ -237,6 +309,18 @@ static int sss_nss_getyyybyxxx(union input inp, enum sss_cli_command cmd ,
bb7cd1
         out->d.id = c;
bb7cd1
 
bb7cd1
         break;
bb7cd1
+    case SSS_NSS_GETLISTBYCERT:
bb7cd1
+        ret = buf_to_name_type_list(repbuf + LIST_START, replen - LIST_START,
bb7cd1
+                                    num_results,
bb7cd1
+                                    &names, &types);
bb7cd1
+        if (ret != EOK) {
bb7cd1
+            goto done;
bb7cd1
+        }
bb7cd1
+
bb7cd1
+        out->types = types;
bb7cd1
+        out->d.names = names;
bb7cd1
+
bb7cd1
+        break;
bb7cd1
     case SSS_NSS_GETORIGBYNAME:
bb7cd1
         ret = buf_to_kv_list(repbuf + DATA_START, data_len, &kv_list);
bb7cd1
         if (ret != EOK) {
bb7cd1
@@ -392,3 +476,25 @@ int sss_nss_getnamebycert(const char *cert, char **fq_name,
bb7cd1
 
bb7cd1
     return ret;
bb7cd1
 }
bb7cd1
+
bb7cd1
+int sss_nss_getlistbycert(const char *cert, char ***fq_name,
bb7cd1
+                          enum sss_id_type **type)
bb7cd1
+{
bb7cd1
+    int ret;
bb7cd1
+    union input inp;
bb7cd1
+    struct output out;
bb7cd1
+
bb7cd1
+    if (fq_name == NULL || cert == NULL || *cert == '\0') {
bb7cd1
+        return EINVAL;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    inp.str = cert;
bb7cd1
+
bb7cd1
+    ret = sss_nss_getyyybyxxx(inp, SSS_NSS_GETLISTBYCERT, &out;;
bb7cd1
+    if (ret == EOK) {
bb7cd1
+        *fq_name = out.d.names;
bb7cd1
+        *type = out.types;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    return ret;
bb7cd1
+}
bb7cd1
diff --git a/src/sss_client/idmap/sss_nss_idmap.exports b/src/sss_client/idmap/sss_nss_idmap.exports
bb7cd1
index bd5d80212017d38334c3cdeefa47d6029f42aebb..49dac6fc9351b0ca98cd46e83b85ec8ef0075a0d 100644
bb7cd1
--- a/src/sss_client/idmap/sss_nss_idmap.exports
bb7cd1
+++ b/src/sss_client/idmap/sss_nss_idmap.exports
bb7cd1
@@ -25,3 +25,9 @@ SSS_NSS_IDMAP_0.2.0 {
bb7cd1
     global:
bb7cd1
         sss_nss_getnamebycert;
bb7cd1
 } SSS_NSS_IDMAP_0.1.0;
bb7cd1
+
bb7cd1
+SSS_NSS_IDMAP_0.3.0 {
bb7cd1
+    # public functions
bb7cd1
+    global:
bb7cd1
+        sss_nss_getlistbycert;
bb7cd1
+} SSS_NSS_IDMAP_0.2.0;
bb7cd1
diff --git a/src/sss_client/idmap/sss_nss_idmap.h b/src/sss_client/idmap/sss_nss_idmap.h
bb7cd1
index 8a6299194e7b91e084b26c0c96e2f93875a832e7..cbf19479ff9ec6e0d6e07e1f7e48a1571e147740 100644
bb7cd1
--- a/src/sss_client/idmap/sss_nss_idmap.h
bb7cd1
+++ b/src/sss_client/idmap/sss_nss_idmap.h
bb7cd1
@@ -130,7 +130,7 @@ int sss_nss_getorigbyname(const char *fq_name, struct sss_nss_kv **kv_list,
bb7cd1
  * @param[in] cert     base64 encoded certificate
bb7cd1
  * @param[out] fq_name Fully qualified name of a user or a group,
bb7cd1
  *                     must be freed by the caller
bb7cd1
- * @param[out] type    Type of the object related to the SID
bb7cd1
+ * @param[out] type    Type of the object related to the cert
bb7cd1
  *
bb7cd1
  * @return
bb7cd1
  *  - see #sss_nss_getsidbyname
bb7cd1
@@ -139,6 +139,21 @@ int sss_nss_getnamebycert(const char *cert, char **fq_name,
bb7cd1
                           enum sss_id_type *type);
bb7cd1
 
bb7cd1
 /**
bb7cd1
+ * @brief Return a list of fully qualified names for the given base64 encoded
bb7cd1
+ * X.509 certificate in DER format
bb7cd1
+ *
bb7cd1
+ * @param[in] cert     base64 encoded certificate
bb7cd1
+ * @param[out] fq_name List of fully qualified name of users or groups,
bb7cd1
+ *                     must be freed by the caller
bb7cd1
+ * @param[out] type    List of types of the objects related to the cert
bb7cd1
+ *
bb7cd1
+ * @return
bb7cd1
+ *  - see #sss_nss_getsidbyname
bb7cd1
+ */
bb7cd1
+int sss_nss_getlistbycert(const char *cert, char ***fq_name,
bb7cd1
+                          enum sss_id_type **type);
bb7cd1
+
bb7cd1
+/**
bb7cd1
  * @brief Free key-value list returned by sss_nss_getorigbyname()
bb7cd1
  *
bb7cd1
  * @param[in] kv_list Key-value list returned by sss_nss_getorigbyname().
bb7cd1
diff --git a/src/sss_client/sss_cli.h b/src/sss_client/sss_cli.h
bb7cd1
index 8091e11515184dc9b7f32eed535055d9eee3143f..59fee7a4eceb2c185e156e812af7f2f4c6b2a0dd 100644
bb7cd1
--- a/src/sss_client/sss_cli.h
bb7cd1
+++ b/src/sss_client/sss_cli.h
bb7cd1
@@ -260,6 +260,11 @@ SSS_NSS_GETNAMEBYCERT = 0x0116, /**< Takes the zero terminated string
bb7cd1
                                      of a X509 certificate and returns the zero
bb7cd1
                                      terminated fully qualified name of the
bb7cd1
                                      related object. */
bb7cd1
+SSS_NSS_GETLISTBYCERT = 0x0117, /**< Takes the zero terminated string
bb7cd1
+                                     of the base64 encoded DER representation
bb7cd1
+                                     of a X509 certificate and returns a list
bb7cd1
+                                     of zero terminated fully qualified names
bb7cd1
+                                     of the related objects. */
bb7cd1
 };
bb7cd1
 
bb7cd1
 /**
bb7cd1
diff --git a/src/tests/cmocka/test_nss_srv.c b/src/tests/cmocka/test_nss_srv.c
bb7cd1
index 76b9c6fb05673130de0957e93291919c263a28f3..50714715cc80338640f2a77ecbe17bd5e0d6e911 100644
bb7cd1
--- a/src/tests/cmocka/test_nss_srv.c
bb7cd1
+++ b/src/tests/cmocka/test_nss_srv.c
bb7cd1
@@ -3454,6 +3454,16 @@ struct passwd testbycert = {
bb7cd1
     .pw_passwd = discard_const("*"),
bb7cd1
 };
bb7cd1
 
bb7cd1
+struct passwd testbycert2 = {
bb7cd1
+    .pw_name = discard_const("testcertuser2"),
bb7cd1
+    .pw_uid = 23457,
bb7cd1
+    .pw_gid = 6890,
bb7cd1
+    .pw_dir = discard_const("/home/testcertuser2"),
bb7cd1
+    .pw_gecos = discard_const("test cert user2"),
bb7cd1
+    .pw_shell = discard_const("/bin/sh"),
bb7cd1
+    .pw_passwd = discard_const("*"),
bb7cd1
+};
bb7cd1
+
bb7cd1
 #define TEST_TOKEN_CERT \
bb7cd1
 "MIIECTCCAvGgAwIBAgIBCDANBgkqhkiG9w0BAQsFADA0MRIwEAYDVQQKDAlJUEEu" \
bb7cd1
 "REVWRUwxHjAcBgNVBAMMFUNlcnRpZmljYXRlIEF1dGhvcml0eTAeFw0xNTA2MjMx" \
bb7cd1
@@ -3495,6 +3505,57 @@ static int test_nss_getnamebycert_check(uint32_t status, uint8_t *body, size_t b
bb7cd1
     return EOK;
bb7cd1
 }
bb7cd1
 
bb7cd1
+static int test_nss_getlistbycert_check(uint32_t status, uint8_t *body, size_t blen)
bb7cd1
+{
bb7cd1
+    size_t rp = 0;
bb7cd1
+    uint32_t id_type;
bb7cd1
+    uint32_t num;
bb7cd1
+    uint32_t reserved;
bb7cd1
+    const char *name;
bb7cd1
+    int found = 0;
bb7cd1
+    const char *fq_name1 = "testcertuser@"TEST_DOM_NAME ;
bb7cd1
+    const char *fq_name2 = "testcertuser2@"TEST_DOM_NAME;
bb7cd1
+
bb7cd1
+    assert_int_equal(status, EOK);
bb7cd1
+
bb7cd1
+    /* num_results and reserved */
bb7cd1
+    SAFEALIGN_COPY_UINT32(&num, body + rp, &rp);
bb7cd1
+    assert_in_range(num, 1, 2);
bb7cd1
+    SAFEALIGN_COPY_UINT32(&reserved, body + rp, &rp);
bb7cd1
+    assert_int_equal(reserved, 0);
bb7cd1
+
bb7cd1
+    SAFEALIGN_COPY_UINT32(&id_type, body + rp, &rp);
bb7cd1
+    assert_int_equal(id_type, SSS_ID_TYPE_UID);
bb7cd1
+
bb7cd1
+    name = (const char *)body + rp;
bb7cd1
+    if (num == 1) {
bb7cd1
+        assert_string_equal(name, fq_name1);
bb7cd1
+        return EOK;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    rp += strlen(name) + 1;
bb7cd1
+    if (strcmp(name, fq_name1) == 0) {
bb7cd1
+        found = 1;
bb7cd1
+    } else if (strcmp(name, fq_name2) == 0) {
bb7cd1
+        found = 2;
bb7cd1
+    }
bb7cd1
+    assert_in_range(found, 1, 2);
bb7cd1
+
bb7cd1
+    SAFEALIGN_COPY_UINT32(&id_type, body + rp, &rp);
bb7cd1
+    assert_int_equal(id_type, SSS_ID_TYPE_UID);
bb7cd1
+
bb7cd1
+    name = (const char *)body + rp;
bb7cd1
+    if (found == 1) {
bb7cd1
+        assert_string_equal(name, fq_name2);
bb7cd1
+    } else {
bb7cd1
+        assert_string_equal(name, fq_name1);
bb7cd1
+    }
bb7cd1
+
bb7cd1
+
bb7cd1
+    return EOK;
bb7cd1
+}
bb7cd1
+
bb7cd1
+
bb7cd1
 static void test_nss_getnamebycert(void **state)
bb7cd1
 {
bb7cd1
     errno_t ret;
bb7cd1
@@ -3572,6 +3633,99 @@ void test_nss_getnamebycert_neg(void **state)
bb7cd1
     assert_int_equal(nss_test_ctx->ncache_hits, 1);
bb7cd1
 }
bb7cd1
 
bb7cd1
+static void test_nss_getlistbycert(void **state)
bb7cd1
+{
bb7cd1
+    errno_t ret;
bb7cd1
+    struct sysdb_attrs *attrs;
bb7cd1
+    unsigned char *der = NULL;
bb7cd1
+    size_t der_size;
bb7cd1
+
bb7cd1
+    attrs = sysdb_new_attrs(nss_test_ctx);
bb7cd1
+    assert_non_null(attrs);
bb7cd1
+
bb7cd1
+    der = sss_base64_decode(nss_test_ctx, TEST_TOKEN_CERT, &der_size);
bb7cd1
+    assert_non_null(der);
bb7cd1
+
bb7cd1
+    ret = sysdb_attrs_add_mem(attrs, SYSDB_USER_CERT, der, der_size);
bb7cd1
+    talloc_free(der);
bb7cd1
+    assert_int_equal(ret, EOK);
bb7cd1
+
bb7cd1
+    /* Prime the cache with a valid user */
bb7cd1
+    ret = store_user(nss_test_ctx, nss_test_ctx->tctx->dom,
bb7cd1
+                     &testbycert, attrs, 0);
bb7cd1
+    assert_int_equal(ret, EOK);
bb7cd1
+    talloc_free(attrs);
bb7cd1
+
bb7cd1
+    mock_input_cert(TEST_TOKEN_CERT);
bb7cd1
+    will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETLISTBYCERT);
bb7cd1
+    mock_fill_bysid();
bb7cd1
+
bb7cd1
+    /* Query for that user, call a callback when command finishes */
bb7cd1
+    /* Should go straight to back end, without contacting DP. */
bb7cd1
+    /* If there is only a single user mapped the result will look like the */
bb7cd1
+    /* result of getnamebycert. */
bb7cd1
+    set_cmd_cb(test_nss_getlistbycert_check);
bb7cd1
+    ret = sss_cmd_execute(nss_test_ctx->cctx, SSS_NSS_GETLISTBYCERT,
bb7cd1
+                          nss_test_ctx->nss_cmds);
bb7cd1
+    assert_int_equal(ret, EOK);
bb7cd1
+
bb7cd1
+    /* Wait until the test finishes with EOK */
bb7cd1
+    ret = test_ev_loop(nss_test_ctx->tctx);
bb7cd1
+    assert_int_equal(ret, EOK);
bb7cd1
+}
bb7cd1
+
bb7cd1
+static void test_nss_getlistbycert_multi(void **state)
bb7cd1
+{
bb7cd1
+    errno_t ret;
bb7cd1
+    struct sysdb_attrs *attrs;
bb7cd1
+    unsigned char *der = NULL;
bb7cd1
+    size_t der_size;
bb7cd1
+
bb7cd1
+    der = sss_base64_decode(nss_test_ctx, TEST_TOKEN_CERT, &der_size);
bb7cd1
+    assert_non_null(der);
bb7cd1
+
bb7cd1
+    attrs = sysdb_new_attrs(nss_test_ctx);
bb7cd1
+    assert_non_null(attrs);
bb7cd1
+
bb7cd1
+    ret = sysdb_attrs_add_mem(attrs, SYSDB_USER_CERT, der, der_size);
bb7cd1
+    assert_int_equal(ret, EOK);
bb7cd1
+
bb7cd1
+    /* Prime the cache with two valid user */
bb7cd1
+    ret = store_user(nss_test_ctx, nss_test_ctx->tctx->dom,
bb7cd1
+                     &testbycert, attrs, 0);
bb7cd1
+    assert_int_equal(ret, EOK);
bb7cd1
+    talloc_free(attrs);
bb7cd1
+
bb7cd1
+    /* Looks like attrs is modified during store_user() makes sure we start
bb7cd1
+     * with fresh data. */
bb7cd1
+    attrs = sysdb_new_attrs(nss_test_ctx);
bb7cd1
+    assert_non_null(attrs);
bb7cd1
+
bb7cd1
+    ret = sysdb_attrs_add_mem(attrs, SYSDB_USER_CERT, der, der_size);
bb7cd1
+    talloc_free(der);
bb7cd1
+    assert_int_equal(ret, EOK);
bb7cd1
+
bb7cd1
+    ret = store_user(nss_test_ctx, nss_test_ctx->tctx->dom,
bb7cd1
+                     &testbycert2, attrs, 0);
bb7cd1
+    assert_int_equal(ret, EOK);
bb7cd1
+    talloc_free(attrs);
bb7cd1
+
bb7cd1
+    mock_input_cert(TEST_TOKEN_CERT);
bb7cd1
+    will_return(__wrap_sss_packet_get_cmd, SSS_NSS_GETLISTBYCERT);
bb7cd1
+    mock_fill_bysid();
bb7cd1
+
bb7cd1
+    /* Query for that user, call a callback when command finishes */
bb7cd1
+    /* Should go straight to back end, without contacting DP */
bb7cd1
+    set_cmd_cb(test_nss_getlistbycert_check);
bb7cd1
+    ret = sss_cmd_execute(nss_test_ctx->cctx, SSS_NSS_GETLISTBYCERT,
bb7cd1
+                          nss_test_ctx->nss_cmds);
bb7cd1
+    assert_int_equal(ret, EOK);
bb7cd1
+
bb7cd1
+    /* Wait until the test finishes with EOK */
bb7cd1
+    ret = test_ev_loop(nss_test_ctx->tctx);
bb7cd1
+    assert_int_equal(ret, EOK);
bb7cd1
+}
bb7cd1
+
bb7cd1
 struct passwd sid_user = {
bb7cd1
     .pw_name = discard_const("testusersid"),
bb7cd1
     .pw_uid = 1234,
bb7cd1
@@ -3818,6 +3972,10 @@ int main(int argc, const char *argv[])
bb7cd1
                                         nss_test_setup, nss_test_teardown),
bb7cd1
         cmocka_unit_test_setup_teardown(test_nss_getnamebycert,
bb7cd1
                                         nss_test_setup, nss_test_teardown),
bb7cd1
+        cmocka_unit_test_setup_teardown(test_nss_getlistbycert,
bb7cd1
+                                        nss_test_setup, nss_test_teardown),
bb7cd1
+        cmocka_unit_test_setup_teardown(test_nss_getlistbycert_multi,
bb7cd1
+                                        nss_test_setup, nss_test_teardown),
bb7cd1
         cmocka_unit_test_setup_teardown(test_nss_getsidbyname,
bb7cd1
                                         nss_test_setup, nss_test_teardown),
bb7cd1
         cmocka_unit_test_setup_teardown(test_nss_getsidbyupn,
bb7cd1
-- 
bb7cd1
2.9.3
bb7cd1