dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

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

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