dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone
Blob Blame History Raw
From 109c99463219be59fbf168a4075a74585193aef9 Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose@redhat.com>
Date: Wed, 25 Jan 2017 16:50:00 +0100
Subject: [PATCH 92/96] pam_test_client: add SSSD getpwnam lookup
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Related to https://pagure.io/SSSD/sssd/issue/3292

Reviewed-by: Pavel Březina <pbrezina@redhat.com>
(cherry picked from commit 435b3678de25d22eb8a6e892109d26c32f0760a4)
---
 Makefile.am                      | 10 ++++--
 src/sss_client/pam_test_client.c | 76 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+), 2 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 4a414f77df999b8b1d81f663fcc18dbd2d6d2dc4..368ebe54b8617cb5bafb079322582d5346b6c4df 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3460,8 +3460,14 @@ if BUILD_WITH_LIBCURL
 noinst_PROGRAMS += tcurl-test-tool
 endif
 
-pam_test_client_SOURCES = src/sss_client/pam_test_client.c
-pam_test_client_LDADD = $(PAM_LIBS) $(PAM_MISC_LIBS)
+pam_test_client_SOURCES = \
+    src/sss_client/pam_test_client.c \
+    $(NULL)
+pam_test_client_LDADD = \
+    $(PAM_LIBS) \
+    $(PAM_MISC_LIBS) \
+    $(LIBADD_DL) \
+    $(NULL)
 
 if BUILD_AUTOFS
 autofs_test_client_SOURCES = \
diff --git a/src/sss_client/pam_test_client.c b/src/sss_client/pam_test_client.c
index ea032a75b195a9bf8078ed7d248da154ab0c8430..69af612270492968b56d1c11de2bf56ebf57471f 100644
--- a/src/sss_client/pam_test_client.c
+++ b/src/sss_client/pam_test_client.c
@@ -25,6 +25,11 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
+#include <dlfcn.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <nss.h>
+#include <errno.h>
 
 #include <security/pam_appl.h>
 
@@ -51,6 +56,70 @@ static struct pam_conv conv = {
 #define DEFAULT_ACTION "acct"
 #define DEFAULT_SERVICE "system-auth"
 
+#define DEFAULT_BUFSIZE 4096
+
+static int sss_getpwnam_check(const char *user)
+{
+    void *dl_handle = NULL;
+    enum nss_status (*sss_getpwnam_r)(const char *name, struct passwd *result,
+                                      char *buffer, size_t buflen,
+                                      int *errnop);
+    struct passwd pwd = { 0 };
+    enum nss_status status;
+    char *buffer = NULL;
+    size_t buflen;
+    int nss_errno;
+    int ret;
+
+    dl_handle = dlopen("libnss_sss.so.2", RTLD_NOW);
+    if (dl_handle == NULL) {
+        fprintf(stderr, "dlopen failed with [%s].\n", dlerror());
+        ret = EIO;
+        goto done;
+    }
+
+    sss_getpwnam_r = dlsym(dl_handle, "_nss_sss_getpwnam_r");
+    if (sss_getpwnam_r == NULL) {
+        fprintf(stderr, "dlsym failed with [%s].\n", dlerror());
+        ret = EIO;
+        goto done;
+    }
+
+    buflen = DEFAULT_BUFSIZE;
+    buffer = malloc(buflen);
+    if (buffer == NULL) {
+        fprintf(stderr, "malloc failed.\n");
+        ret = ENOMEM;
+        goto done;
+    }
+
+    status = sss_getpwnam_r(user, &pwd, buffer, buflen, &nss_errno);
+    if (status != NSS_STATUS_SUCCESS) {
+        fprintf(stderr, "sss_getpwnam_r failed with [%d].\n", status);
+        ret = EIO;
+        goto done;
+    }
+
+    fprintf(stdout, "SSSD nss user lookup result:\n");
+    fprintf(stdout, " - user name: %s\n", pwd.pw_name);
+    fprintf(stdout, " - user id: %d\n", pwd.pw_uid);
+    fprintf(stdout, " - group id: %d\n", pwd.pw_gid);
+    fprintf(stdout, " - gecos: %s\n", pwd.pw_gecos);
+    fprintf(stdout, " - home directory: %s\n", pwd.pw_dir);
+    fprintf(stdout, " - shell: %s\n", pwd.pw_shell);
+
+    ret = 0;
+
+done:
+    if (dl_handle != NULL) {
+        dlclose(dl_handle);
+    }
+
+    free(buffer);
+
+    return ret;
+}
+
 int main(int argc, char *argv[]) {
 
     pam_handle_t *pamh;
@@ -85,6 +154,13 @@ int main(int argc, char *argv[]) {
     fprintf(stdout, "user: %s\naction: %s\nservice: %s\n",
                     user, action, service);
 
+    if (*user != '\0') {
+        ret = sss_getpwnam_check(user);
+        if (ret != 0) {
+            fprintf(stderr, "User name lookup with [%s] failed.\n", user);
+        }
+    }
+
     ret = pam_start(service, user, &conv, &pamh);
     if (ret != PAM_SUCCESS) {
         fprintf(stderr, "pam_start failed: %s\n", pam_strerror(pamh, ret));
-- 
2.9.3