dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

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

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