Blame SOURCES/0092-pam_test_client-add-SSSD-getpwnam-lookup.patch

ecf709
From 109c99463219be59fbf168a4075a74585193aef9 Mon Sep 17 00:00:00 2001
ecf709
From: Sumit Bose <sbose@redhat.com>
ecf709
Date: Wed, 25 Jan 2017 16:50:00 +0100
ecf709
Subject: [PATCH 92/96] pam_test_client: add SSSD getpwnam lookup
ecf709
MIME-Version: 1.0
ecf709
Content-Type: text/plain; charset=UTF-8
ecf709
Content-Transfer-Encoding: 8bit
ecf709
ecf709
Related to https://pagure.io/SSSD/sssd/issue/3292
ecf709
ecf709
Reviewed-by: Pavel Březina <pbrezina@redhat.com>
ecf709
(cherry picked from commit 435b3678de25d22eb8a6e892109d26c32f0760a4)
ecf709
---
ecf709
 Makefile.am                      | 10 ++++--
ecf709
 src/sss_client/pam_test_client.c | 76 ++++++++++++++++++++++++++++++++++++++++
ecf709
 2 files changed, 84 insertions(+), 2 deletions(-)
ecf709
ecf709
diff --git a/Makefile.am b/Makefile.am
ecf709
index 4a414f77df999b8b1d81f663fcc18dbd2d6d2dc4..368ebe54b8617cb5bafb079322582d5346b6c4df 100644
ecf709
--- a/Makefile.am
ecf709
+++ b/Makefile.am
ecf709
@@ -3460,8 +3460,14 @@ if BUILD_WITH_LIBCURL
ecf709
 noinst_PROGRAMS += tcurl-test-tool
ecf709
 endif
ecf709
 
ecf709
-pam_test_client_SOURCES = src/sss_client/pam_test_client.c
ecf709
-pam_test_client_LDADD = $(PAM_LIBS) $(PAM_MISC_LIBS)
ecf709
+pam_test_client_SOURCES = \
ecf709
+    src/sss_client/pam_test_client.c \
ecf709
+    $(NULL)
ecf709
+pam_test_client_LDADD = \
ecf709
+    $(PAM_LIBS) \
ecf709
+    $(PAM_MISC_LIBS) \
ecf709
+    $(LIBADD_DL) \
ecf709
+    $(NULL)
ecf709
 
ecf709
 if BUILD_AUTOFS
ecf709
 autofs_test_client_SOURCES = \
ecf709
diff --git a/src/sss_client/pam_test_client.c b/src/sss_client/pam_test_client.c
ecf709
index ea032a75b195a9bf8078ed7d248da154ab0c8430..69af612270492968b56d1c11de2bf56ebf57471f 100644
ecf709
--- a/src/sss_client/pam_test_client.c
ecf709
+++ b/src/sss_client/pam_test_client.c
ecf709
@@ -25,6 +25,11 @@
ecf709
 #include <stdio.h>
ecf709
 #include <unistd.h>
ecf709
 #include <string.h>
ecf709
+#include <dlfcn.h>
ecf709
+#include <sys/types.h>
ecf709
+#include <pwd.h>
ecf709
+#include <nss.h>
ecf709
+#include <errno.h>
ecf709
 
ecf709
 #include <security/pam_appl.h>
ecf709
 
ecf709
@@ -51,6 +56,70 @@ static struct pam_conv conv = {
ecf709
 #define DEFAULT_ACTION "acct"
ecf709
 #define DEFAULT_SERVICE "system-auth"
ecf709
 
ecf709
+#define DEFAULT_BUFSIZE 4096
ecf709
+
ecf709
+static int sss_getpwnam_check(const char *user)
ecf709
+{
ecf709
+    void *dl_handle = NULL;
ecf709
+    enum nss_status (*sss_getpwnam_r)(const char *name, struct passwd *result,
ecf709
+                                      char *buffer, size_t buflen,
ecf709
+                                      int *errnop);
ecf709
+    struct passwd pwd = { 0 };
ecf709
+    enum nss_status status;
ecf709
+    char *buffer = NULL;
ecf709
+    size_t buflen;
ecf709
+    int nss_errno;
ecf709
+    int ret;
ecf709
+
ecf709
+    dl_handle = dlopen("libnss_sss.so.2", RTLD_NOW);
ecf709
+    if (dl_handle == NULL) {
ecf709
+        fprintf(stderr, "dlopen failed with [%s].\n", dlerror());
ecf709
+        ret = EIO;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    sss_getpwnam_r = dlsym(dl_handle, "_nss_sss_getpwnam_r");
ecf709
+    if (sss_getpwnam_r == NULL) {
ecf709
+        fprintf(stderr, "dlsym failed with [%s].\n", dlerror());
ecf709
+        ret = EIO;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    buflen = DEFAULT_BUFSIZE;
ecf709
+    buffer = malloc(buflen);
ecf709
+    if (buffer == NULL) {
ecf709
+        fprintf(stderr, "malloc failed.\n");
ecf709
+        ret = ENOMEM;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    status = sss_getpwnam_r(user, &pwd, buffer, buflen, &nss_errno);
ecf709
+    if (status != NSS_STATUS_SUCCESS) {
ecf709
+        fprintf(stderr, "sss_getpwnam_r failed with [%d].\n", status);
ecf709
+        ret = EIO;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    fprintf(stdout, "SSSD nss user lookup result:\n");
ecf709
+    fprintf(stdout, " - user name: %s\n", pwd.pw_name);
ecf709
+    fprintf(stdout, " - user id: %d\n", pwd.pw_uid);
ecf709
+    fprintf(stdout, " - group id: %d\n", pwd.pw_gid);
ecf709
+    fprintf(stdout, " - gecos: %s\n", pwd.pw_gecos);
ecf709
+    fprintf(stdout, " - home directory: %s\n", pwd.pw_dir);
ecf709
+    fprintf(stdout, " - shell: %s\n", pwd.pw_shell);
ecf709
+
ecf709
+    ret = 0;
ecf709
+
ecf709
+done:
ecf709
+    if (dl_handle != NULL) {
ecf709
+        dlclose(dl_handle);
ecf709
+    }
ecf709
+
ecf709
+    free(buffer);
ecf709
+
ecf709
+    return ret;
ecf709
+}
ecf709
+
ecf709
 int main(int argc, char *argv[]) {
ecf709
 
ecf709
     pam_handle_t *pamh;
ecf709
@@ -85,6 +154,13 @@ int main(int argc, char *argv[]) {
ecf709
     fprintf(stdout, "user: %s\naction: %s\nservice: %s\n",
ecf709
                     user, action, service);
ecf709
 
ecf709
+    if (*user != '\0') {
ecf709
+        ret = sss_getpwnam_check(user);
ecf709
+        if (ret != 0) {
ecf709
+            fprintf(stderr, "User name lookup with [%s] failed.\n", user);
ecf709
+        }
ecf709
+    }
ecf709
+
ecf709
     ret = pam_start(service, user, &conv, &pamh);
ecf709
     if (ret != PAM_SUCCESS) {
ecf709
         fprintf(stderr, "pam_start failed: %s\n", pam_strerror(pamh, ret));
ecf709
-- 
ecf709
2.9.3
ecf709