Blame SOURCES/0016-pam-introduce-prompt_config-struct.patch

841ac7
From a5e15f1e5af7d7c41717c18566ea0f2a01c086ec Mon Sep 17 00:00:00 2001
841ac7
From: Sumit Bose <sbose@redhat.com>
841ac7
Date: Wed, 27 Mar 2019 09:02:27 +0100
841ac7
Subject: [PATCH 16/21] pam: introduce prompt_config struct
841ac7
841ac7
prompt_config is the internal struct to control the prompting of
841ac7
pam_sss. To make it easy to change internal details when more options
841ac7
are added it should be opaque and only accessed by getters and setter.
841ac7
841ac7
Related to https://pagure.io/SSSD/sssd/issue/3264
841ac7
841ac7
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
841ac7
(cherry picked from commit fa8ef7c6db19a160d807f05b08bbc66c0c25ebfe)
841ac7
---
841ac7
 Makefile.am                            |  17 +
841ac7
 src/sss_client/pam_sss_prompt_config.c | 547 +++++++++++++++++++++++++
841ac7
 src/sss_client/sss_cli.h               |  29 ++
841ac7
 src/tests/cmocka/test_prompt_config.c  | 215 ++++++++++
841ac7
 4 files changed, 808 insertions(+)
841ac7
 create mode 100644 src/sss_client/pam_sss_prompt_config.c
841ac7
 create mode 100644 src/tests/cmocka/test_prompt_config.c
841ac7
841ac7
diff --git a/Makefile.am b/Makefile.am
841ac7
index d09f50aa2..f7f55e96a 100644
841ac7
--- a/Makefile.am
841ac7
+++ b/Makefile.am
841ac7
@@ -241,6 +241,7 @@ if HAVE_CMOCKA
841ac7
         test-negcache \
841ac7
         negcache_2-tests \
841ac7
         test-authtok \
841ac7
+        test_prompt_config \
841ac7
         sss_nss_idmap-tests \
841ac7
         deskprofile_utils-tests \
841ac7
         dyndns-tests \
841ac7
@@ -2644,6 +2645,21 @@ test_authtok_LDADD = \
841ac7
     libsss_debug.la \
841ac7
     $(NULL)
841ac7
 
841ac7
+test_prompt_config_SOURCES = \
841ac7
+    src/tests/cmocka/test_prompt_config.c \
841ac7
+    src/sss_client/pam_sss_prompt_config.c \
841ac7
+    $(NULL)
841ac7
+test_prompt_config_CFLAGS = \
841ac7
+    $(AM_CFLAGS) \
841ac7
+    $(POPT_CFLAGS) \
841ac7
+    $(NULL)
841ac7
+test_prompt_config_LDADD = \
841ac7
+    $(CMOCKA_LIBS) \
841ac7
+    $(POPT_LIBS) \
841ac7
+    libsss_debug.la \
841ac7
+    $(TALLOC_LIBS) \
841ac7
+    $(NULL)
841ac7
+
841ac7
 sss_nss_idmap_tests_SOURCES = \
841ac7
     src/tests/cmocka/sss_nss_idmap-tests.c
841ac7
 sss_nss_idmap_tests_CFLAGS = \
841ac7
@@ -3820,6 +3836,7 @@ endif
841ac7
 pamlib_LTLIBRARIES = pam_sss.la
841ac7
 pam_sss_la_SOURCES = \
841ac7
     src/sss_client/pam_sss.c \
841ac7
+    src/sss_client/pam_sss_prompt_config.c \
841ac7
     src/sss_client/pam_message.c \
841ac7
     src/sss_client/common.c \
841ac7
     src/sss_client/sss_cli.h \
841ac7
diff --git a/src/sss_client/pam_sss_prompt_config.c b/src/sss_client/pam_sss_prompt_config.c
841ac7
new file mode 100644
841ac7
index 000000000..35094b406
841ac7
--- /dev/null
841ac7
+++ b/src/sss_client/pam_sss_prompt_config.c
841ac7
@@ -0,0 +1,547 @@
841ac7
+/*
841ac7
+    Authors:
841ac7
+        Sumit Bose <sbose@redhat.com>
841ac7
+
841ac7
+    Copyright (C) 2019 Red Hat
841ac7
+
841ac7
+    This program is free software; you can redistribute it and/or modify
841ac7
+    it under the terms of the GNU Lesser General Public License as published by
841ac7
+    the Free Software Foundation; either version 3 of the License, or
841ac7
+    (at your option) any later version.
841ac7
+
841ac7
+    This program is distributed in the hope that it will be useful,
841ac7
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
841ac7
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
841ac7
+    GNU Lesser General Public License for more details.
841ac7
+
841ac7
+    You should have received a copy of the GNU Lesser General Public License
841ac7
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
841ac7
+*/
841ac7
+
841ac7
+#include "config.h"
841ac7
+#include <stdlib.h>
841ac7
+#include <errno.h>
841ac7
+
841ac7
+#include "sss_cli.h"
841ac7
+
841ac7
+#include <libintl.h>
841ac7
+#define _(STRING) dgettext (PACKAGE, STRING)
841ac7
+
841ac7
+struct prompt_config_password {
841ac7
+    char *prompt;
841ac7
+};
841ac7
+
841ac7
+struct prompt_config_2fa {
841ac7
+    char *prompt_1st;
841ac7
+    char *prompt_2nd;
841ac7
+};
841ac7
+
841ac7
+struct prompt_config_2fa_single {
841ac7
+    char *prompt;
841ac7
+};
841ac7
+
841ac7
+struct prompt_config_sc_pin {
841ac7
+    char *prompt; /* Currently not used */
841ac7
+};
841ac7
+
841ac7
+struct prompt_config {
841ac7
+    enum prompt_config_type type;
841ac7
+    union {
841ac7
+        struct prompt_config_password password;
841ac7
+        struct prompt_config_2fa two_fa;
841ac7
+        struct prompt_config_2fa_single two_fa_single;
841ac7
+        struct prompt_config_sc_pin sc_pin;
841ac7
+    } data;
841ac7
+};
841ac7
+
841ac7
+enum prompt_config_type pc_get_type(struct prompt_config *pc)
841ac7
+{
841ac7
+    if (pc != NULL && pc->type > PC_TYPE_INVALID && pc->type < PC_TYPE_LAST) {
841ac7
+        return pc->type;
841ac7
+    }
841ac7
+    return PC_TYPE_INVALID;
841ac7
+}
841ac7
+
841ac7
+const char *pc_get_password_prompt(struct prompt_config *pc)
841ac7
+{
841ac7
+    if (pc != NULL && pc_get_type(pc) == PC_TYPE_PASSWORD) {
841ac7
+        return pc->data.password.prompt;
841ac7
+    }
841ac7
+    return NULL;
841ac7
+}
841ac7
+
841ac7
+const char *pc_get_2fa_1st_prompt(struct prompt_config *pc)
841ac7
+{
841ac7
+    if (pc != NULL && pc_get_type(pc) == PC_TYPE_2FA) {
841ac7
+        return pc->data.two_fa.prompt_1st;
841ac7
+    }
841ac7
+    return NULL;
841ac7
+}
841ac7
+
841ac7
+const char *pc_get_2fa_2nd_prompt(struct prompt_config *pc)
841ac7
+{
841ac7
+    if (pc != NULL && pc_get_type(pc) == PC_TYPE_2FA) {
841ac7
+        return pc->data.two_fa.prompt_2nd;
841ac7
+    }
841ac7
+    return NULL;
841ac7
+}
841ac7
+
841ac7
+const char *pc_get_2fa_single_prompt(struct prompt_config *pc)
841ac7
+{
841ac7
+    if (pc != NULL && pc_get_type(pc) == PC_TYPE_2FA_SINGLE) {
841ac7
+        return pc->data.two_fa_single.prompt;
841ac7
+    }
841ac7
+    return NULL;
841ac7
+}
841ac7
+
841ac7
+static void pc_free_password(struct prompt_config *pc)
841ac7
+{
841ac7
+    if (pc != NULL && pc_get_type(pc) == PC_TYPE_PASSWORD) {
841ac7
+        free(pc->data.password.prompt);
841ac7
+    }
841ac7
+    return;
841ac7
+}
841ac7
+
841ac7
+static void pc_free_2fa(struct prompt_config *pc)
841ac7
+{
841ac7
+    if (pc != NULL && pc_get_type(pc) == PC_TYPE_2FA) {
841ac7
+        free(pc->data.two_fa.prompt_1st);
841ac7
+        free(pc->data.two_fa.prompt_2nd);
841ac7
+    }
841ac7
+    return;
841ac7
+}
841ac7
+
841ac7
+static void pc_free_2fa_single(struct prompt_config *pc)
841ac7
+{
841ac7
+    if (pc != NULL && pc_get_type(pc) == PC_TYPE_2FA_SINGLE) {
841ac7
+        free(pc->data.two_fa_single.prompt);
841ac7
+    }
841ac7
+    return;
841ac7
+}
841ac7
+
841ac7
+static void pc_free_sc_pin(struct prompt_config *pc)
841ac7
+{
841ac7
+    if (pc != NULL && pc_get_type(pc) == PC_TYPE_SC_PIN) {
841ac7
+        free(pc->data.sc_pin.prompt);
841ac7
+    }
841ac7
+    return;
841ac7
+}
841ac7
+
841ac7
+void pc_list_free(struct prompt_config **pc_list)
841ac7
+{
841ac7
+    size_t c;
841ac7
+
841ac7
+    if (pc_list == NULL) {
841ac7
+        return;
841ac7
+    }
841ac7
+
841ac7
+    for (c = 0; pc_list[c] != NULL; c++) {
841ac7
+        switch (pc_list[c]->type) {
841ac7
+        case PC_TYPE_PASSWORD:
841ac7
+            pc_free_password(pc_list[c]);
841ac7
+            break;
841ac7
+        case PC_TYPE_2FA:
841ac7
+            pc_free_2fa(pc_list[c]);
841ac7
+            break;
841ac7
+        case PC_TYPE_2FA_SINGLE:
841ac7
+            pc_free_2fa_single(pc_list[c]);
841ac7
+            break;
841ac7
+        case PC_TYPE_SC_PIN:
841ac7
+            pc_free_sc_pin(pc_list[c]);
841ac7
+            break;
841ac7
+        default:
841ac7
+            return;
841ac7
+        }
841ac7
+        free(pc_list[c]);
841ac7
+    }
841ac7
+    free(pc_list);
841ac7
+}
841ac7
+
841ac7
+static errno_t pc_list_add_pc(struct prompt_config ***pc_list,
841ac7
+                              struct prompt_config *pc)
841ac7
+{
841ac7
+    size_t c = 0;
841ac7
+    struct prompt_config **pcl;
841ac7
+
841ac7
+    for (c = 0; *pc_list != NULL && (*pc_list)[c] != NULL; c++); /* just counting */
841ac7
+
841ac7
+    pcl = realloc(*pc_list, (c + 2) * sizeof(struct prompt_config *));
841ac7
+    if (pcl == NULL) {
841ac7
+        return ENOMEM;
841ac7
+    }
841ac7
+    pcl[c] = pc;
841ac7
+    pcl[c + 1] = NULL;
841ac7
+
841ac7
+    *pc_list = pcl;
841ac7
+
841ac7
+    return EOK;
841ac7
+}
841ac7
+
841ac7
+#define DEFAULT_PASSWORD_PROMPT _("Password: ")
841ac7
+#define DEFAULT_2FA_SINGLE_PROMPT _("Password + Token value: ")
841ac7
+#define DEFAULT_2FA_PROMPT_1ST _("First Factor: ")
841ac7
+#define DEFAULT_2FA_PROMPT_2ND _("Second Factor: ")
841ac7
+
841ac7
+errno_t pc_list_add_password(struct prompt_config ***pc_list,
841ac7
+                             const char *prompt)
841ac7
+{
841ac7
+    struct prompt_config *pc;
841ac7
+    int ret;
841ac7
+
841ac7
+    if (pc_list == NULL) {
841ac7
+        return EINVAL;
841ac7
+    }
841ac7
+
841ac7
+    pc = calloc(1, sizeof(struct prompt_config));
841ac7
+    if (pc == NULL) {
841ac7
+        return ENOMEM;
841ac7
+    }
841ac7
+
841ac7
+    pc->type = PC_TYPE_PASSWORD;
841ac7
+    pc->data.password.prompt = strdup(prompt != NULL ? prompt
841ac7
+                                                     : DEFAULT_PASSWORD_PROMPT);
841ac7
+    if (pc->data.password.prompt == NULL) {
841ac7
+        ret = ENOMEM;
841ac7
+        goto done;
841ac7
+    }
841ac7
+
841ac7
+    ret = pc_list_add_pc(pc_list, pc);
841ac7
+    if (ret != EOK) {
841ac7
+        goto done;
841ac7
+    }
841ac7
+
841ac7
+    ret = EOK;
841ac7
+
841ac7
+done:
841ac7
+    if (ret != EOK) {
841ac7
+        free(pc->data.password.prompt);
841ac7
+        free(pc);
841ac7
+    }
841ac7
+
841ac7
+    return ret;
841ac7
+}
841ac7
+
841ac7
+errno_t pc_list_add_2fa(struct prompt_config ***pc_list,
841ac7
+                        const char *prompt_1st, const char *prompt_2nd)
841ac7
+{
841ac7
+    struct prompt_config *pc;
841ac7
+    int ret;
841ac7
+
841ac7
+    if (pc_list == NULL) {
841ac7
+        return EINVAL;
841ac7
+    }
841ac7
+
841ac7
+    pc = calloc(1, sizeof(struct prompt_config));
841ac7
+    if (pc == NULL) {
841ac7
+        return ENOMEM;
841ac7
+    }
841ac7
+
841ac7
+    pc->type = PC_TYPE_2FA;
841ac7
+    pc->data.two_fa.prompt_1st = strdup(prompt_1st != NULL ? prompt_1st
841ac7
+                                                   : DEFAULT_2FA_PROMPT_1ST);
841ac7
+    if (pc->data.two_fa.prompt_1st == NULL) {
841ac7
+        ret = ENOMEM;
841ac7
+        goto done;
841ac7
+    }
841ac7
+    pc->data.two_fa.prompt_2nd = strdup(prompt_2nd != NULL ? prompt_2nd
841ac7
+                                                   : DEFAULT_2FA_PROMPT_2ND);
841ac7
+    if (pc->data.two_fa.prompt_2nd == NULL) {
841ac7
+        ret = ENOMEM;
841ac7
+        goto done;
841ac7
+    }
841ac7
+
841ac7
+    ret = pc_list_add_pc(pc_list, pc);
841ac7
+    if (ret != EOK) {
841ac7
+        goto done;
841ac7
+    }
841ac7
+
841ac7
+    ret = EOK;
841ac7
+
841ac7
+done:
841ac7
+    if (ret != EOK) {
841ac7
+        free(pc->data.two_fa.prompt_1st);
841ac7
+        free(pc->data.two_fa.prompt_2nd);
841ac7
+        free(pc);
841ac7
+    }
841ac7
+
841ac7
+    return ret;
841ac7
+}
841ac7
+
841ac7
+errno_t pc_list_add_2fa_single(struct prompt_config ***pc_list,
841ac7
+                               const char *prompt)
841ac7
+{
841ac7
+    struct prompt_config *pc;
841ac7
+    int ret;
841ac7
+
841ac7
+    if (pc_list == NULL) {
841ac7
+        return EINVAL;
841ac7
+    }
841ac7
+
841ac7
+    pc = calloc(1, sizeof(struct prompt_config));
841ac7
+    if (pc == NULL) {
841ac7
+        return ENOMEM;
841ac7
+    }
841ac7
+
841ac7
+    pc->type = PC_TYPE_2FA_SINGLE;
841ac7
+    pc->data.two_fa_single.prompt = strdup(prompt != NULL ? prompt
841ac7
+                                                   : DEFAULT_2FA_SINGLE_PROMPT);
841ac7
+    if (pc->data.two_fa_single.prompt == NULL) {
841ac7
+        ret = ENOMEM;
841ac7
+        goto done;
841ac7
+    }
841ac7
+
841ac7
+    ret = pc_list_add_pc(pc_list, pc);
841ac7
+    if (ret != EOK) {
841ac7
+        goto done;
841ac7
+    }
841ac7
+
841ac7
+    ret = EOK;
841ac7
+
841ac7
+done:
841ac7
+    if (ret != EOK) {
841ac7
+        free(pc->data.two_fa_single.prompt);
841ac7
+        free(pc);
841ac7
+    }
841ac7
+
841ac7
+    return ret;
841ac7
+}
841ac7
+
841ac7
+errno_t pam_get_response_prompt_config(struct prompt_config **pc_list, int *len,
841ac7
+                                       uint8_t **data)
841ac7
+{
841ac7
+    size_t c;
841ac7
+    size_t l = 0;
841ac7
+    uint8_t *d = NULL;
841ac7
+    uint32_t uint32_val;
841ac7
+    size_t rp;
841ac7
+
841ac7
+    if (pc_list == NULL || *pc_list == NULL) {
841ac7
+        return ENOENT;
841ac7
+    }
841ac7
+
841ac7
+    l += sizeof(uint32_t);
841ac7
+    for (c = 0; pc_list[c] != NULL; c++) {
841ac7
+        l += sizeof(uint32_t);
841ac7
+        switch (pc_list[c]->type) {
841ac7
+        case PC_TYPE_PASSWORD:
841ac7
+            l += sizeof(uint32_t);
841ac7
+            l += strlen(pc_list[c]->data.password.prompt);
841ac7
+            break;
841ac7
+        case PC_TYPE_2FA:
841ac7
+            l += sizeof(uint32_t);
841ac7
+            l += strlen(pc_list[c]->data.two_fa.prompt_1st);
841ac7
+            l += sizeof(uint32_t);
841ac7
+            l += strlen(pc_list[c]->data.two_fa.prompt_2nd);
841ac7
+            break;
841ac7
+        case PC_TYPE_2FA_SINGLE:
841ac7
+            l += sizeof(uint32_t);
841ac7
+            l += strlen(pc_list[c]->data.two_fa_single.prompt);
841ac7
+            break;
841ac7
+        case PC_TYPE_SC_PIN:
841ac7
+            break;
841ac7
+        default:
841ac7
+            return EINVAL;
841ac7
+        }
841ac7
+    }
841ac7
+
841ac7
+    d = malloc(l * sizeof(uint8_t));
841ac7
+    if (d == NULL) {
841ac7
+        return ENOMEM;
841ac7
+    }
841ac7
+
841ac7
+    rp = 0;
841ac7
+    uint32_val = c;
841ac7
+    SAFEALIGN_COPY_UINT32(&d[rp], &uint32_val, &rp);
841ac7
+
841ac7
+    for (c = 0; pc_list[c] != NULL; c++) {
841ac7
+        uint32_val = pc_list[c]->type;
841ac7
+        SAFEALIGN_COPY_UINT32(&d[rp], &uint32_val, &rp);
841ac7
+
841ac7
+        switch (pc_list[c]->type) {
841ac7
+        case PC_TYPE_PASSWORD:
841ac7
+            SAFEALIGN_SET_UINT32(&d[rp],
841ac7
+                                 strlen(pc_list[c]->data.password.prompt), &rp);
841ac7
+            safealign_memcpy(&d[rp], pc_list[c]->data.password.prompt,
841ac7
+                             strlen(pc_list[c]->data.password.prompt), &rp);
841ac7
+            break;
841ac7
+        case PC_TYPE_2FA:
841ac7
+            SAFEALIGN_SET_UINT32(&d[rp],
841ac7
+                                 strlen(pc_list[c]->data.two_fa.prompt_1st),
841ac7
+                                 &rp);
841ac7
+            safealign_memcpy(&d[rp], pc_list[c]->data.two_fa.prompt_1st,
841ac7
+                             strlen(pc_list[c]->data.two_fa.prompt_1st), &rp);
841ac7
+            SAFEALIGN_SET_UINT32(&d[rp],
841ac7
+                                 strlen(pc_list[c]->data.two_fa.prompt_2nd),
841ac7
+                                 &rp);
841ac7
+            safealign_memcpy(&d[rp], pc_list[c]->data.two_fa.prompt_2nd,
841ac7
+                             strlen(pc_list[c]->data.two_fa.prompt_2nd), &rp);
841ac7
+            break;
841ac7
+        case PC_TYPE_2FA_SINGLE:
841ac7
+            SAFEALIGN_SET_UINT32(&d[rp],
841ac7
+                                 strlen(pc_list[c]->data.two_fa_single.prompt),
841ac7
+                                 &rp);
841ac7
+            safealign_memcpy(&d[rp], pc_list[c]->data.two_fa_single.prompt,
841ac7
+                             strlen(pc_list[c]->data.two_fa_single.prompt),
841ac7
+                             &rp);
841ac7
+            break;
841ac7
+        case PC_TYPE_SC_PIN:
841ac7
+            break;
841ac7
+        default:
841ac7
+            free(d);
841ac7
+            return EINVAL;
841ac7
+        }
841ac7
+    }
841ac7
+
841ac7
+    if (rp != l) {
841ac7
+        free(d);
841ac7
+        return EFAULT;
841ac7
+    }
841ac7
+
841ac7
+    *data = d;
841ac7
+    *len = l;
841ac7
+
841ac7
+    return EOK;
841ac7
+}
841ac7
+
841ac7
+errno_t pc_list_from_response(int size, uint8_t *buf,
841ac7
+                              struct prompt_config ***pc_list)
841ac7
+{
841ac7
+    int ret;
841ac7
+    uint32_t count;
841ac7
+    uint32_t type;
841ac7
+    uint32_t l;
841ac7
+    size_t rp;
841ac7
+    size_t c;
841ac7
+    struct prompt_config **pl = NULL;
841ac7
+    char *str;
841ac7
+    char *str2;
841ac7
+
841ac7
+    if (buf == NULL || size < 3 * sizeof(uint32_t)) {
841ac7
+        return EINVAL;
841ac7
+    }
841ac7
+
841ac7
+    rp = 0;
841ac7
+    SAFEALIGN_COPY_UINT32_CHECK(&count, buf + rp, size, &rp);
841ac7
+
841ac7
+    for (c = 0; c < count; c++) {
841ac7
+        /* Since we already know size < 3 * sizeof(uint32_t) this check should
841ac7
+         * be safe and without over- or underflow. */
841ac7
+        if (rp > size - sizeof(uint32_t)) {
841ac7
+            ret = EINVAL;
841ac7
+            goto done;
841ac7
+        }
841ac7
+        SAFEALIGN_COPY_UINT32(&type, buf + rp, &rp);
841ac7
+
841ac7
+        switch (type) {
841ac7
+        case PC_TYPE_PASSWORD:
841ac7
+            if (rp > size - sizeof(uint32_t)) {
841ac7
+                ret = EINVAL;
841ac7
+                goto done;
841ac7
+            }
841ac7
+            SAFEALIGN_COPY_UINT32(&l, buf + rp, &rp);
841ac7
+
841ac7
+            if (l > size || rp > size - l) {
841ac7
+                ret = EINVAL;
841ac7
+                goto done;
841ac7
+            }
841ac7
+            str = strndup((char *) buf + rp, l);
841ac7
+            if (str == NULL) {
841ac7
+                ret = ENOMEM;
841ac7
+                goto done;
841ac7
+            }
841ac7
+            rp += l;
841ac7
+
841ac7
+            ret = pc_list_add_password(&pl, str);
841ac7
+            free(str);
841ac7
+            if (ret != EOK) {
841ac7
+                goto done;
841ac7
+            }
841ac7
+            break;
841ac7
+        case PC_TYPE_2FA:
841ac7
+            if (rp > size - sizeof(uint32_t)) {
841ac7
+                ret = EINVAL;
841ac7
+                goto done;
841ac7
+            }
841ac7
+            SAFEALIGN_COPY_UINT32(&l, buf + rp, &rp);
841ac7
+
841ac7
+            if (l > size || rp > size - l) {
841ac7
+                ret = EINVAL;
841ac7
+                goto done;
841ac7
+            }
841ac7
+            str = strndup((char *) buf + rp, l);
841ac7
+            if (str == NULL) {
841ac7
+                ret = ENOMEM;
841ac7
+                goto done;
841ac7
+            }
841ac7
+            rp += l;
841ac7
+
841ac7
+            if (rp > size - sizeof(uint32_t)) {
841ac7
+                free(str);
841ac7
+                ret = EINVAL;
841ac7
+                goto done;
841ac7
+            }
841ac7
+            SAFEALIGN_COPY_UINT32(&l, buf + rp, &rp);
841ac7
+
841ac7
+            if (l > size || rp > size - l) {
841ac7
+                free(str);
841ac7
+                ret = EINVAL;
841ac7
+                goto done;
841ac7
+            }
841ac7
+            str2 = strndup((char *) buf + rp, l);
841ac7
+            if (str2 == NULL) {
841ac7
+                free(str);
841ac7
+                ret = ENOMEM;
841ac7
+                goto done;
841ac7
+            }
841ac7
+            rp += l;
841ac7
+
841ac7
+            ret = pc_list_add_2fa(&pl, str, str2);
841ac7
+            free(str);
841ac7
+            free(str2);
841ac7
+            if (ret != EOK) {
841ac7
+                goto done;
841ac7
+            }
841ac7
+            break;
841ac7
+        case PC_TYPE_2FA_SINGLE:
841ac7
+            if (rp > size - sizeof(uint32_t)) {
841ac7
+                ret = EINVAL;
841ac7
+                goto done;
841ac7
+            }
841ac7
+            SAFEALIGN_COPY_UINT32(&l, buf + rp, &rp);
841ac7
+
841ac7
+            if (l > size || rp > size - l) {
841ac7
+                ret = EINVAL;
841ac7
+                goto done;
841ac7
+            }
841ac7
+            str = strndup((char *) buf + rp, l);
841ac7
+            if (str == NULL) {
841ac7
+                ret = ENOMEM;
841ac7
+                goto done;
841ac7
+            }
841ac7
+            rp += l;
841ac7
+
841ac7
+            ret = pc_list_add_2fa_single(&pl, str);
841ac7
+            free(str);
841ac7
+            if (ret != EOK) {
841ac7
+                goto done;
841ac7
+            }
841ac7
+            break;
841ac7
+        case PC_TYPE_SC_PIN:
841ac7
+            break;
841ac7
+        default:
841ac7
+            ret = EINVAL;
841ac7
+            goto done;
841ac7
+        }
841ac7
+    }
841ac7
+
841ac7
+    *pc_list = pl;
841ac7
+
841ac7
+    ret = EOK;
841ac7
+
841ac7
+done:
841ac7
+    if (ret != EOK) {
841ac7
+        pc_list_free(pl);
841ac7
+    }
841ac7
+
841ac7
+    return ret;
841ac7
+}
841ac7
diff --git a/src/sss_client/sss_cli.h b/src/sss_client/sss_cli.h
841ac7
index 24d28ed4b..7e748c281 100644
841ac7
--- a/src/sss_client/sss_cli.h
841ac7
+++ b/src/sss_client/sss_cli.h
841ac7
@@ -561,6 +561,35 @@ enum user_info_type {
841ac7
  * @}
841ac7
  */ /* end of group sss_pam_cli */
841ac7
 
841ac7
+
841ac7
+enum prompt_config_type {
841ac7
+    PC_TYPE_INVALID = 0,
841ac7
+    PC_TYPE_PASSWORD,
841ac7
+    PC_TYPE_2FA,
841ac7
+    PC_TYPE_2FA_SINGLE,
841ac7
+    PC_TYPE_SC_PIN,
841ac7
+    PC_TYPE_LAST
841ac7
+};
841ac7
+
841ac7
+struct prompt_config;
841ac7
+
841ac7
+enum prompt_config_type pc_get_type(struct prompt_config *pc);
841ac7
+const char *pc_get_password_prompt(struct prompt_config *pc);
841ac7
+const char *pc_get_2fa_1st_prompt(struct prompt_config *pc);
841ac7
+const char *pc_get_2fa_2nd_prompt(struct prompt_config *pc);
841ac7
+const char *pc_get_2fa_single_prompt(struct prompt_config *pc);
841ac7
+void pc_list_free(struct prompt_config **pc_list);
841ac7
+errno_t pc_list_add_password(struct prompt_config ***pc_list,
841ac7
+                             const char *prompt);
841ac7
+errno_t pc_list_add_2fa(struct prompt_config ***pc_list,
841ac7
+                        const char *prompt_1st, const char *prompt_2nd);
841ac7
+errno_t pc_list_add_2fa_single(struct prompt_config ***pc_list,
841ac7
+                               const char *prompt);
841ac7
+errno_t pam_get_response_prompt_config(struct prompt_config **pc_list, int *len,
841ac7
+                                       uint8_t **data);
841ac7
+errno_t pc_list_from_response(int size, uint8_t *buf,
841ac7
+                              struct prompt_config ***pc_list);
841ac7
+
841ac7
 enum sss_netgr_rep_type {
841ac7
     SSS_NETGR_REP_TRIPLE = 1,
841ac7
     SSS_NETGR_REP_GROUP
841ac7
diff --git a/src/tests/cmocka/test_prompt_config.c b/src/tests/cmocka/test_prompt_config.c
841ac7
new file mode 100644
841ac7
index 000000000..0b761ae4c
841ac7
--- /dev/null
841ac7
+++ b/src/tests/cmocka/test_prompt_config.c
841ac7
@@ -0,0 +1,215 @@
841ac7
+/*
841ac7
+    SSSD
841ac7
+
841ac7
+    prompt config - Utilities tests
841ac7
+
841ac7
+    Authors:
841ac7
+        Sumit bose <sbose@redhat.com>
841ac7
+
841ac7
+    Copyright (C) 2019 Red Hat
841ac7
+
841ac7
+    This program is free software; you can redistribute it and/or modify
841ac7
+    it under the terms of the GNU General Public License as published by
841ac7
+    the Free Software Foundation; either version 3 of the License, or
841ac7
+    (at your option) any later version.
841ac7
+
841ac7
+    This program is distributed in the hope that it will be useful,
841ac7
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
841ac7
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
841ac7
+    GNU General Public License for more details.
841ac7
+
841ac7
+    You should have received a copy of the GNU General Public License
841ac7
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
841ac7
+*/
841ac7
+
841ac7
+#include <string.h>
841ac7
+#include <popt.h>
841ac7
+
841ac7
+#include "tests/cmocka/common_mock.h"
841ac7
+
841ac7
+#include "sss_client/sss_cli.h"
841ac7
+
841ac7
+void test_pc_list_add_password(void **state)
841ac7
+{
841ac7
+    int ret;
841ac7
+    struct prompt_config **pc_list = NULL;
841ac7
+
841ac7
+    ret = pc_list_add_password(&pc_list, "Hello");
841ac7
+    assert_int_equal(ret, EOK);
841ac7
+    assert_non_null(pc_list);
841ac7
+    assert_non_null(pc_list[0]);
841ac7
+    assert_int_equal(PC_TYPE_PASSWORD, pc_get_type(pc_list[0]));
841ac7
+    assert_string_equal("Hello", pc_get_password_prompt(pc_list[0]));
841ac7
+    assert_null(pc_list[1]);
841ac7
+
841ac7
+    ret = pc_list_add_password(&pc_list, "Hello2");
841ac7
+    assert_int_equal(ret, EOK);
841ac7
+    assert_non_null(pc_list);
841ac7
+    assert_non_null(pc_list[0]);
841ac7
+    assert_int_equal(PC_TYPE_PASSWORD, pc_get_type(pc_list[0]));
841ac7
+    assert_string_equal("Hello", pc_get_password_prompt(pc_list[0]));
841ac7
+    assert_non_null(pc_list[1]);
841ac7
+    assert_int_equal(PC_TYPE_PASSWORD, pc_get_type(pc_list[1]));
841ac7
+    assert_string_equal("Hello2", pc_get_password_prompt(pc_list[1]));
841ac7
+    assert_null(pc_list[2]);
841ac7
+
841ac7
+    pc_list_free(pc_list);
841ac7
+}
841ac7
+
841ac7
+void test_pc_list_add_2fa_single(void **state)
841ac7
+{
841ac7
+    int ret;
841ac7
+    struct prompt_config **pc_list = NULL;
841ac7
+
841ac7
+    ret = pc_list_add_2fa_single(&pc_list, "Hello");
841ac7
+    assert_int_equal(ret, EOK);
841ac7
+    assert_non_null(pc_list);
841ac7
+    assert_non_null(pc_list[0]);
841ac7
+    assert_int_equal(PC_TYPE_2FA_SINGLE, pc_get_type(pc_list[0]));
841ac7
+    assert_string_equal("Hello", pc_get_2fa_single_prompt(pc_list[0]));
841ac7
+    assert_null(pc_list[1]);
841ac7
+
841ac7
+    ret = pc_list_add_2fa_single(&pc_list, "Hello2");
841ac7
+    assert_int_equal(ret, EOK);
841ac7
+    assert_non_null(pc_list);
841ac7
+    assert_non_null(pc_list[0]);
841ac7
+    assert_int_equal(PC_TYPE_2FA_SINGLE, pc_get_type(pc_list[0]));
841ac7
+    assert_string_equal("Hello", pc_get_2fa_single_prompt(pc_list[0]));
841ac7
+    assert_non_null(pc_list[1]);
841ac7
+    assert_int_equal(PC_TYPE_2FA_SINGLE, pc_get_type(pc_list[1]));
841ac7
+    assert_string_equal("Hello2", pc_get_2fa_single_prompt(pc_list[1]));
841ac7
+    assert_null(pc_list[2]);
841ac7
+
841ac7
+    pc_list_free(pc_list);
841ac7
+}
841ac7
+
841ac7
+void test_pc_list_add_2fa(void **state)
841ac7
+{
841ac7
+    int ret;
841ac7
+    struct prompt_config **pc_list = NULL;
841ac7
+
841ac7
+    ret = pc_list_add_2fa(&pc_list, "Hello", "Good Bye");
841ac7
+    assert_int_equal(ret, EOK);
841ac7
+    assert_non_null(pc_list);
841ac7
+    assert_non_null(pc_list[0]);
841ac7
+    assert_int_equal(PC_TYPE_2FA, pc_get_type(pc_list[0]));
841ac7
+    assert_string_equal("Hello", pc_get_2fa_1st_prompt(pc_list[0]));
841ac7
+    assert_string_equal("Good Bye", pc_get_2fa_2nd_prompt(pc_list[0]));
841ac7
+    assert_null(pc_list[1]);
841ac7
+
841ac7
+    pc_list_free(pc_list);
841ac7
+}
841ac7
+
841ac7
+void test_pam_get_response_prompt_config(void **state)
841ac7
+{
841ac7
+    int ret;
841ac7
+    struct prompt_config **pc_list = NULL;
841ac7
+    int len;
841ac7
+    uint8_t *data;
841ac7
+
841ac7
+    ret = pc_list_add_password(&pc_list, "password");
841ac7
+    assert_int_equal(ret, EOK);
841ac7
+
841ac7
+    ret = pc_list_add_2fa(&pc_list, "first", "second");
841ac7
+    assert_int_equal(ret, EOK);
841ac7
+
841ac7
+    ret = pc_list_add_2fa_single(&pc_list, "single");
841ac7
+    assert_int_equal(ret, EOK);
841ac7
+
841ac7
+    ret = pam_get_response_prompt_config(pc_list, &len, &data);
841ac7
+    pc_list_free(pc_list);
841ac7
+    assert_int_equal(ret, EOK);
841ac7
+    assert_int_equal(len, 57);
841ac7
+
841ac7
+#if __BYTE_ORDER == __LITTLE_ENDIAN
841ac7
+    assert_memory_equal(data, "\3\0\0\0\1\0\0\0\10\0\0\0" "password\2\0\0\0\5\0\0\0" "first\6\0\0\0" "second\3\0\0\0\6\0\0\0" "single", len);
841ac7
+#else
841ac7
+    assert_memory_equal(data, "\0\0\0\3\0\0\0\1\0\0\0\10" "password\0\0\0\2\0\0\0\5" "first\0\0\0\6" "second\0\0\0\3\0\0\0\6" "single", len);
841ac7
+#endif
841ac7
+
841ac7
+    free(data);
841ac7
+}
841ac7
+
841ac7
+void test_pc_list_from_response(void **state)
841ac7
+{
841ac7
+    int ret;
841ac7
+    struct prompt_config **pc_list = NULL;
841ac7
+    int len;
841ac7
+    uint8_t *data;
841ac7
+
841ac7
+    ret = pc_list_add_password(&pc_list, "password");
841ac7
+    assert_int_equal(ret, EOK);
841ac7
+
841ac7
+    ret = pc_list_add_2fa(&pc_list, "first", "second");
841ac7
+    assert_int_equal(ret, EOK);
841ac7
+
841ac7
+    ret = pc_list_add_2fa_single(&pc_list, "single");
841ac7
+    assert_int_equal(ret, EOK);
841ac7
+
841ac7
+    ret = pam_get_response_prompt_config(pc_list, &len, &data);
841ac7
+    pc_list_free(pc_list);
841ac7
+    assert_int_equal(ret, EOK);
841ac7
+    assert_int_equal(len, 57);
841ac7
+
841ac7
+    pc_list = NULL;
841ac7
+
841ac7
+    ret = pc_list_from_response(len, data, &pc_list);
841ac7
+    free(data);
841ac7
+    assert_int_equal(ret, EOK);
841ac7
+    assert_non_null(pc_list);
841ac7
+
841ac7
+    assert_non_null(pc_list[0]);
841ac7
+    assert_int_equal(PC_TYPE_PASSWORD, pc_get_type(pc_list[0]));
841ac7
+    assert_string_equal("password", pc_get_password_prompt(pc_list[0]));
841ac7
+
841ac7
+    assert_non_null(pc_list[1]);
841ac7
+    assert_int_equal(PC_TYPE_2FA, pc_get_type(pc_list[1]));
841ac7
+    assert_string_equal("first", pc_get_2fa_1st_prompt(pc_list[1]));
841ac7
+    assert_string_equal("second", pc_get_2fa_2nd_prompt(pc_list[1]));
841ac7
+
841ac7
+    assert_non_null(pc_list[2]);
841ac7
+    assert_int_equal(PC_TYPE_2FA_SINGLE, pc_get_type(pc_list[2]));
841ac7
+    assert_string_equal("single", pc_get_2fa_single_prompt(pc_list[2]));
841ac7
+
841ac7
+    assert_null(pc_list[3]);
841ac7
+
841ac7
+    pc_list_free(pc_list);
841ac7
+}
841ac7
+
841ac7
+int main(int argc, const char *argv[])
841ac7
+{
841ac7
+    poptContext pc;
841ac7
+    int opt;
841ac7
+    struct poptOption long_options[] = {
841ac7
+        POPT_AUTOHELP
841ac7
+        SSSD_DEBUG_OPTS
841ac7
+        POPT_TABLEEND
841ac7
+    };
841ac7
+
841ac7
+    const struct CMUnitTest tests[] = {
841ac7
+        cmocka_unit_test(test_pc_list_add_password),
841ac7
+        cmocka_unit_test(test_pc_list_add_2fa_single),
841ac7
+        cmocka_unit_test(test_pc_list_add_2fa),
841ac7
+        cmocka_unit_test(test_pam_get_response_prompt_config),
841ac7
+        cmocka_unit_test(test_pc_list_from_response),
841ac7
+    };
841ac7
+
841ac7
+    /* Set debug level to invalid value so we can decide if -d 0 was used. */
841ac7
+    debug_level = SSSDBG_INVALID;
841ac7
+
841ac7
+    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
841ac7
+    while((opt = poptGetNextOpt(pc)) != -1) {
841ac7
+        switch(opt) {
841ac7
+        default:
841ac7
+            fprintf(stderr, "\nInvalid option %s: %s\n\n",
841ac7
+                    poptBadOption(pc, 0), poptStrerror(opt));
841ac7
+            poptPrintUsage(pc, stderr, 0);
841ac7
+            return 1;
841ac7
+        }
841ac7
+    }
841ac7
+    poptFreeContext(pc);
841ac7
+
841ac7
+    DEBUG_CLI_INIT(debug_level);
841ac7
+
841ac7
+    return cmocka_run_group_tests(tests, NULL, NULL);
841ac7
+}
841ac7
-- 
841ac7
2.19.1
841ac7