Blame SOURCES/0196-sssd_client-add-mutex-protected-call-to-the-PAC-resp.patch

ecf709
From daa59b79602cfeff81223a7461e18f513178c9d4 Mon Sep 17 00:00:00 2001
ecf709
From: Sumit Bose <sbose@redhat.com>
ecf709
Date: Mon, 18 Sep 2017 15:00:53 +0200
ecf709
Subject: [PATCH 196/196] sssd_client: add mutex protected call to the PAC
ecf709
 responder
ecf709
MIME-Version: 1.0
ecf709
Content-Type: text/plain; charset=UTF-8
ecf709
Content-Transfer-Encoding: 8bit
ecf709
ecf709
SSSD's plugin for MIT Kerberos to send the PAC to the PAC responder
ecf709
currently uses sss_pac_make_request() which does not protect the
ecf709
communication with the PAC responder with a mutex as e.g. the NSS and
ecf709
PAM clients.
ecf709
ecf709
If an application using threads loads this plugin via libkrb5 in
ecf709
different threads and is heavily processing Kerberos tickets with PACs
ecf709
chances are that two threads try to communicate with SSSD at once. In
ecf709
this case one of the threads will miss a reply and will wait for it
ecf709
until the default client timeout of 300s is passed.
ecf709
ecf709
This patch adds a call which uses a mutex to protect the communication
ecf709
which will avoid the 300s delay mentioned above.
ecf709
ecf709
Resolves:
ecf709
https://pagure.io/SSSD/sssd/issue/3518
ecf709
ecf709
Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com>
ecf709
Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
ecf709
(cherry picked from commit 1f331476e7d33bb03cc35a2a9064ee1cc5bed6cf)
ecf709
---
ecf709
 Makefile.am                               |  16 ++++
ecf709
 src/sss_client/common.c                   |  30 +++++++
ecf709
 src/sss_client/sss_cli.h                  |   7 ++
ecf709
 src/sss_client/sss_pac_responder_client.c | 137 ++++++++++++++++++++++++++++++
ecf709
 src/sss_client/sssd_pac.c                 |   4 +-
ecf709
 src/tests/intg/Makefile.am                |   1 +
ecf709
 src/tests/intg/test_pac_responder.py      | 120 ++++++++++++++++++++++++++
ecf709
 7 files changed, 313 insertions(+), 2 deletions(-)
ecf709
 create mode 100644 src/sss_client/sss_pac_responder_client.c
ecf709
 create mode 100644 src/tests/intg/test_pac_responder.py
ecf709
ecf709
diff --git a/Makefile.am b/Makefile.am
ecf709
index 907c3256a154ebe2aae5a1667744e1dfbe8abaae..cdd517d50679b876814303fb7d6c63d49bcd8d38 100644
ecf709
--- a/Makefile.am
ecf709
+++ b/Makefile.am
ecf709
@@ -3501,6 +3501,9 @@ endif
ecf709
 if BUILD_WITH_LIBCURL
ecf709
 noinst_PROGRAMS += tcurl-test-tool
ecf709
 endif
ecf709
+if BUILD_PAC_RESPONDER
ecf709
+    noinst_PROGRAMS += sssd_pac_test_client
ecf709
+endif
ecf709
 
ecf709
 if BUILD_AUTOFS
ecf709
 autofs_test_client_SOURCES = \
ecf709
@@ -4210,6 +4213,19 @@ sssd_pac_plugin_la_LDFLAGS = \
ecf709
     -avoid-version \
ecf709
     -module
ecf709
 
ecf709
+sssd_pac_test_client_SOURCES = \
ecf709
+    src/sss_client/sss_pac_responder_client.c \
ecf709
+    src/sss_client/common.c \
ecf709
+    src/util/strtonum.c \
ecf709
+    $(NULL)
ecf709
+sssd_pac_test_client_CFLAGS = \
ecf709
+    $(AM_CFLAGS) \
ecf709
+    $(NULL)
ecf709
+sssd_pac_test_client_LDADD = \
ecf709
+    $(CLIENT_LIBS) \
ecf709
+    -lpthread \
ecf709
+    $(NULL)
ecf709
+
ecf709
 # python[23] bindings
ecf709
 pysss_la_SOURCES = \
ecf709
     $(SSSD_TOOLS_OBJ) \
ecf709
diff --git a/src/sss_client/common.c b/src/sss_client/common.c
ecf709
index b7a5ed760ca379acdfd8f1d2bf95cee1aa271fd8..b527c046e2e3369934b4f9ea7efc1b52eb8c57ea 100644
ecf709
--- a/src/sss_client/common.c
ecf709
+++ b/src/sss_client/common.c
ecf709
@@ -821,6 +821,22 @@ int sss_pac_make_request(enum sss_cli_command cmd,
ecf709
     }
ecf709
 }
ecf709
 
ecf709
+int sss_pac_make_request_with_lock(enum sss_cli_command cmd,
ecf709
+                                   struct sss_cli_req_data *rd,
ecf709
+                                   uint8_t **repbuf, size_t *replen,
ecf709
+                                   int *errnop)
ecf709
+{
ecf709
+    int ret;
ecf709
+
ecf709
+    sss_pac_lock();
ecf709
+
ecf709
+    ret = sss_pac_make_request(cmd, rd, repbuf, replen, errnop);
ecf709
+
ecf709
+    sss_pac_unlock();
ecf709
+
ecf709
+    return ret;
ecf709
+}
ecf709
+
ecf709
 errno_t check_server_cred(int sockfd)
ecf709
 {
ecf709
 #ifdef HAVE_UCRED
ecf709
@@ -1079,6 +1095,8 @@ static struct sss_mutex sss_pam_mtx = { .mtx  = PTHREAD_MUTEX_INITIALIZER };
ecf709
 
ecf709
 static struct sss_mutex sss_nss_mc_mtx = { .mtx  = PTHREAD_MUTEX_INITIALIZER };
ecf709
 
ecf709
+static struct sss_mutex sss_pac_mtx = { .mtx  = PTHREAD_MUTEX_INITIALIZER };
ecf709
+
ecf709
 static void sss_mt_lock(struct sss_mutex *m)
ecf709
 {
ecf709
     pthread_mutex_lock(&m->mtx);
ecf709
@@ -1121,6 +1139,16 @@ void sss_nss_mc_unlock(void)
ecf709
     sss_mt_unlock(&sss_nss_mc_mtx);
ecf709
 }
ecf709
 
ecf709
+/* PAC mutex wrappers */
ecf709
+void sss_pac_lock(void)
ecf709
+{
ecf709
+    sss_mt_lock(&sss_pac_mtx);
ecf709
+}
ecf709
+void sss_pac_unlock(void)
ecf709
+{
ecf709
+    sss_mt_unlock(&sss_pac_mtx);
ecf709
+}
ecf709
+
ecf709
 #else
ecf709
 
ecf709
 /* sorry no mutexes available */
ecf709
@@ -1130,6 +1158,8 @@ void sss_pam_lock(void) { return; }
ecf709
 void sss_pam_unlock(void) { return; }
ecf709
 void sss_nss_mc_lock(void) { return; }
ecf709
 void sss_nss_mc_unlock(void) { return; }
ecf709
+void sss_pac_lock(void) { return; }
ecf709
+void sss_pac_unlock(void) { return; }
ecf709
 #endif
ecf709
 
ecf709
 
ecf709
diff --git a/src/sss_client/sss_cli.h b/src/sss_client/sss_cli.h
ecf709
index d4198407f2f86c6594aee6a2a43775e429692df0..337fe9803d2df3167cd2da77107dbd077f35a51b 100644
ecf709
--- a/src/sss_client/sss_cli.h
ecf709
+++ b/src/sss_client/sss_cli.h
ecf709
@@ -585,6 +585,11 @@ int sss_pac_make_request(enum sss_cli_command cmd,
ecf709
                          uint8_t **repbuf, size_t *replen,
ecf709
                          int *errnop);
ecf709
 
ecf709
+int sss_pac_make_request_with_lock(enum sss_cli_command cmd,
ecf709
+                                   struct sss_cli_req_data *rd,
ecf709
+                                   uint8_t **repbuf, size_t *replen,
ecf709
+                                   int *errnop);
ecf709
+
ecf709
 int sss_sudo_make_request(enum sss_cli_command cmd,
ecf709
                           struct sss_cli_req_data *rd,
ecf709
                           uint8_t **repbuf, size_t *replen,
ecf709
@@ -634,6 +639,8 @@ void sss_pam_lock(void);
ecf709
 void sss_pam_unlock(void);
ecf709
 void sss_nss_mc_lock(void);
ecf709
 void sss_nss_mc_unlock(void);
ecf709
+void sss_pac_lock(void);
ecf709
+void sss_pac_unlock(void);
ecf709
 
ecf709
 errno_t sss_readrep_copy_string(const char *in,
ecf709
                                 size_t *offset,
ecf709
diff --git a/src/sss_client/sss_pac_responder_client.c b/src/sss_client/sss_pac_responder_client.c
ecf709
new file mode 100644
ecf709
index 0000000000000000000000000000000000000000..9eb0cbea6175ee273b23d9a975529d85c02fc603
ecf709
--- /dev/null
ecf709
+++ b/src/sss_client/sss_pac_responder_client.c
ecf709
@@ -0,0 +1,137 @@
ecf709
+
ecf709
+#include <stdio.h>
ecf709
+#include <stdbool.h>
ecf709
+#include <pthread.h>
ecf709
+#include <pwd.h>
ecf709
+#include <unistd.h>
ecf709
+#include <sys/types.h>
ecf709
+#include <errno.h>
ecf709
+
ecf709
+#include <unistd.h>
ecf709
+#include <sys/syscall.h>
ecf709
+
ecf709
+#include "sss_client/sss_cli.h"
ecf709
+
ecf709
+const uint8_t pac[] = {
ecf709
+0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10,
ecf709
+0x02, 0x00, 0x00, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00,
ecf709
+0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x68, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
ecf709
+0x00, 0x0c, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x78, 0x02, 0x00, 0x00,
ecf709
+0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0xb8,
ecf709
+0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x10, 0x00,
ecf709
+0x00, 0x00, 0xc8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x10, 0x08,
ecf709
+0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
ecf709
+0x00, 0x00, 0x02, 0x00, 0x30, 0xe3, 0xd6, 0x9e, 0x99, 0x2b, 0xd3, 0x01, 0xff,
ecf709
+0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
ecf709
+0xff, 0x7f, 0xe2, 0xf7, 0x8a, 0xaf, 0x00, 0x0f, 0xd0, 0x01, 0xe2, 0xb7, 0xf4,
ecf709
+0xd9, 0xc9, 0x0f, 0xd0, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
ecf709
+0x06, 0x00, 0x06, 0x00, 0x04, 0x00, 0x02, 0x00, 0x06, 0x00, 0x06, 0x00, 0x08,
ecf709
+0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x02, 0x00, 0x00, 0x00,
ecf709
+0x00, 0x00, 0x10, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x02,
ecf709
+0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x02, 0x00, 0x45, 0x02, 0x00, 0x00,
ecf709
+0x50, 0x04, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x1c,
ecf709
+0x00, 0x02, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
ecf709
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x14,
ecf709
+0x00, 0x20, 0x00, 0x02, 0x00, 0x04, 0x00, 0x06, 0x00, 0x24, 0x00, 0x02, 0x00,
ecf709
+0x28, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
ecf709
+0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
ecf709
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
ecf709
+0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x02, 0x00,
ecf709
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
ecf709
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x74, 0x00,
ecf709
+0x75, 0x00, 0x31, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
ecf709
+0x00, 0x03, 0x00, 0x00, 0x00, 0x74, 0x00, 0x20, 0x00, 0x75, 0x00, 0x00, 0x00,
ecf709
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
ecf709
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
ecf709
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
ecf709
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
ecf709
+0xfd, 0xa2, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x07,
ecf709
+0x00, 0x00, 0x00, 0x5c, 0x04, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x56, 0x04,
ecf709
+0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x89, 0xa6, 0x00, 0x00, 0x07, 0x00, 0x00,
ecf709
+0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
ecf709
+0x41, 0x00, 0x44, 0x00, 0x2d, 0x00, 0x53, 0x00, 0x45, 0x00, 0x52, 0x00, 0x56,
ecf709
+0x00, 0x45, 0x00, 0x52, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
ecf709
+0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x41, 0x00, 0x44, 0x00, 0x04, 0x00, 0x00,
ecf709
+0x00, 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x15, 0x00, 0x00, 0x00,
ecf709
+0xf8, 0x12, 0x13, 0xdc, 0x47, 0xf3, 0x1c, 0x76, 0x47, 0x2f, 0x2e, 0xd7, 0x02,
ecf709
+0x00, 0x00, 0x00, 0x30, 0x00, 0x02, 0x00, 0x07, 0x00, 0x00, 0x00, 0x34, 0x00,
ecf709
+0x02, 0x00, 0x07, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x01, 0x05, 0x00,
ecf709
+0x00, 0x00, 0x00, 0x00, 0x05, 0x15, 0x00, 0x00, 0x00, 0x29, 0xc9, 0x4f, 0xd9,
ecf709
+0xc2, 0x3c, 0xc3, 0x78, 0x36, 0x55, 0x87, 0xf8, 0x54, 0x04, 0x00, 0x00, 0x05,
ecf709
+0x00, 0x00, 0x00, 0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x15, 0x00,
ecf709
+0x00, 0x00, 0x25, 0xe1, 0xff, 0x1c, 0xf7, 0x87, 0x6b, 0x2c, 0x25, 0xd2, 0x0c,
ecf709
+0xe3, 0xf2, 0x03, 0x00, 0x00, 0x00, 0x2c, 0x29, 0x89, 0x65, 0x2d, 0xd3, 0x01,
ecf709
+0x06, 0x00, 0x74, 0x00, 0x75, 0x00, 0x31, 0x00, 0x20, 0x00, 0x10, 0x00, 0x10,
ecf709
+0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0x00,
ecf709
+0x75, 0x00, 0x31, 0x00, 0x74, 0x00, 0x65, 0x00, 0x73, 0x00, 0x74, 0x00, 0x40,
ecf709
+0x00, 0x61, 0x00, 0x64, 0x00, 0x2e, 0x00, 0x64, 0x00, 0x65, 0x00, 0x76, 0x00,
ecf709
+0x65, 0x00, 0x6c, 0x00, 0x41, 0x00, 0x44, 0x00, 0x2e, 0x00, 0x44, 0x00, 0x45,
ecf709
+0x00, 0x56, 0x00, 0x45, 0x00, 0x4c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x76, 0x8e,
ecf709
+0x25, 0x32, 0x7c, 0x85, 0x00, 0x32, 0xac, 0x8f, 0x02, 0x2c, 0x10, 0x00, 0x00,
ecf709
+0x00, 0x6b, 0xe8, 0x51, 0x03, 0x30, 0xed, 0xca, 0x7d, 0xe2, 0x12, 0xa5, 0xde};
ecf709
+
ecf709
+enum nss_status _nss_sss_getpwuid_r(uid_t uid, struct passwd *result,
ecf709
+                                    char *buffer, size_t buflen, int *errnop);
ecf709
+static void *pac_client(void *arg)
ecf709
+{
ecf709
+    struct sss_cli_req_data sss_data = { sizeof(pac), pac };
ecf709
+    int errnop = -1;
ecf709
+    int ret;
ecf709
+    size_t c;
ecf709
+
ecf709
+    fprintf(stderr, "[%ld][%d][%ld][%s] started\n", time(NULL), getpid(),
ecf709
+                                                    syscall(SYS_gettid),
ecf709
+                                                    (char *) arg);
ecf709
+    for (c = 0; c < 1000; c++) {
ecf709
+        /* sss_pac_make_request() does not protect the client's file
ecf709
+         * descriptor to the PAC responder. With this one thread will miss a
ecf709
+         * reply for a SSS_GET_VERSION request and will wait until
ecf709
+         * SSS_CLI_SOCKET_TIMEOUT is passed.
ecf709
+
ecf709
+        ret = sss_pac_make_request(SSS_PAC_ADD_PAC_USER, &sss_data,
ecf709
+                                   NULL, NULL, &errnop);
ecf709
+         */
ecf709
+        ret = sss_pac_make_request_with_lock(SSS_PAC_ADD_PAC_USER, &sss_data,
ecf709
+                                             NULL, NULL, &errnop);
ecf709
+        if (ret != NSS_STATUS_SUCCESS
ecf709
+                && !(ret == NSS_STATUS_UNAVAIL && errnop != ECONNREFUSED)) {
ecf709
+                /* NSS_STATUS_UNAVAIL is returned if the PAC responder rejects
ecf709
+                 * the request which is ok becasue the client is waiting for a
ecf709
+                 * response here as well. Only errnop == ECONNREFUSED should
ecf709
+                 * be treated as error becasue this means that the PAC
ecf709
+                 * responder is not running. */
ecf709
+            fprintf(stderr, "pac: [%s][%d][%d]\n", (char *)arg, ret, errnop);
ecf709
+            return ((void *)((uintptr_t)("X")));
ecf709
+        }
ecf709
+    }
ecf709
+
ecf709
+    fprintf(stderr, "[%ld][%s] done\n", time(NULL),(char *) arg);
ecf709
+    return NULL;
ecf709
+}
ecf709
+
ecf709
+int main(void)
ecf709
+{
ecf709
+    pthread_t thread1;
ecf709
+    pthread_t thread2;
ecf709
+    int ret;
ecf709
+    void *t_ret;
ecf709
+
ecf709
+    pthread_create(&thread1, NULL, pac_client,
ecf709
+                   ((void *)((uintptr_t)("Thread 1"))));
ecf709
+    pthread_create(&thread2, NULL, pac_client,
ecf709
+                   ((void *)((uintptr_t)("Thread 2"))));
ecf709
+
ecf709
+    ret = pthread_join(thread1, &t_ret);
ecf709
+    if (ret != 0 || t_ret != NULL) {
ecf709
+        fprintf(stderr, "Thread 1 failed.\n");
ecf709
+        return EIO;
ecf709
+    }
ecf709
+
ecf709
+    ret = pthread_join(thread2, &t_ret);
ecf709
+    if (ret != 0 || t_ret != NULL) {
ecf709
+        fprintf(stderr, "Thread 1 failed.\n");
ecf709
+        return EIO;
ecf709
+    }
ecf709
+
ecf709
+    return 0;
ecf709
+}
ecf709
diff --git a/src/sss_client/sssd_pac.c b/src/sss_client/sssd_pac.c
ecf709
index 1d98e38826b36aed199b32880a7e27de905a4592..8444834a7f148787e847f5e8e21186c8701b2de7 100644
ecf709
--- a/src/sss_client/sssd_pac.c
ecf709
+++ b/src/sss_client/sssd_pac.c
ecf709
@@ -169,8 +169,8 @@ static krb5_error_code sssdpac_verify(krb5_context kcontext,
ecf709
     sss_data.len = sssdctx->data.length;
ecf709
     sss_data.data = sssdctx->data.data;
ecf709
 
ecf709
-    ret = sss_pac_make_request(SSS_PAC_ADD_PAC_USER, &sss_data,
ecf709
-                               NULL, NULL, &errnop);
ecf709
+    ret = sss_pac_make_request_with_lock(SSS_PAC_ADD_PAC_USER, &sss_data,
ecf709
+                                         NULL, NULL, &errnop);
ecf709
     if (ret != 0) {
ecf709
         /* Ignore the error */
ecf709
     }
ecf709
diff --git a/src/tests/intg/Makefile.am b/src/tests/intg/Makefile.am
ecf709
index 8566106e9017a8d3c9e7a3898a3a886e2966e346..0af7c62ca243822d919619f3d0ebc852a317efc4 100644
ecf709
--- a/src/tests/intg/Makefile.am
ecf709
+++ b/src/tests/intg/Makefile.am
ecf709
@@ -29,6 +29,7 @@ dist_noinst_DATA = \
ecf709
     kdc.py \
ecf709
     krb5utils.py \
ecf709
     test_kcm.py \
ecf709
+    test_pac_responder.py \
ecf709
     $(NULL)
ecf709
 
ecf709
 config.py: config.py.m4
ecf709
diff --git a/src/tests/intg/test_pac_responder.py b/src/tests/intg/test_pac_responder.py
ecf709
new file mode 100644
ecf709
index 0000000000000000000000000000000000000000..4354a5d78da6a6627a27d0ca85c8a1d47419cedf
ecf709
--- /dev/null
ecf709
+++ b/src/tests/intg/test_pac_responder.py
ecf709
@@ -0,0 +1,120 @@
ecf709
+#
ecf709
+# SSSD PAC responder tests
ecf709
+#
ecf709
+# Copyright (c) 2017 Red Hat, Inc.
ecf709
+# Author: Sumit Bose <sbose@redhat.com>
ecf709
+#
ecf709
+# This is free software; you can redistribute it and/or modify it
ecf709
+# under the terms of the GNU General Public License as published by
ecf709
+# the Free Software Foundation; version 2 only
ecf709
+#
ecf709
+# This program is distributed in the hope that it will be useful, but
ecf709
+# WITHOUT ANY WARRANTY; without even the implied warranty of
ecf709
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ecf709
+# General Public License for more details.
ecf709
+#
ecf709
+# You should have received a copy of the GNU General Public License
ecf709
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
ecf709
+#
ecf709
+import os
ecf709
+import stat
ecf709
+import time
ecf709
+import config
ecf709
+import signal
ecf709
+import subprocess
ecf709
+import pytest
ecf709
+from util import unindent
ecf709
+
ecf709
+
ecf709
+def stop_sssd():
ecf709
+    with open(config.PIDFILE_PATH, "r") as pid_file:
ecf709
+        pid = int(pid_file.read())
ecf709
+    os.kill(pid, signal.SIGTERM)
ecf709
+    while True:
ecf709
+        try:
ecf709
+            os.kill(pid, signal.SIGCONT)
ecf709
+        except:
ecf709
+            break
ecf709
+        time.sleep(1)
ecf709
+
ecf709
+
ecf709
+def create_conf_fixture(request, contents):
ecf709
+    """Generate sssd.conf and add teardown for removing it"""
ecf709
+    conf = open(config.CONF_PATH, "w")
ecf709
+    conf.write(contents)
ecf709
+    conf.close()
ecf709
+    os.chmod(config.CONF_PATH, stat.S_IRUSR | stat.S_IWUSR)
ecf709
+    request.addfinalizer(lambda: os.unlink(config.CONF_PATH))
ecf709
+
ecf709
+
ecf709
+def create_sssd_fixture(request):
ecf709
+    """Start sssd and add teardown for stopping it and removing state"""
ecf709
+    if subprocess.call(["sssd", "-D", "-f"]) != 0:
ecf709
+        raise Exception("sssd start failed")
ecf709
+
ecf709
+    def teardown():
ecf709
+        try:
ecf709
+            stop_sssd()
ecf709
+        except:
ecf709
+            pass
ecf709
+        for path in os.listdir(config.DB_PATH):
ecf709
+            os.unlink(config.DB_PATH + "/" + path)
ecf709
+        for path in os.listdir(config.MCACHE_PATH):
ecf709
+            os.unlink(config.MCACHE_PATH + "/" + path)
ecf709
+    request.addfinalizer(teardown)
ecf709
+
ecf709
+
ecf709
+@pytest.fixture
ecf709
+def local_domain_only(request):
ecf709
+    conf = unindent("""\
ecf709
+        [sssd]
ecf709
+        domains = LOCAL
ecf709
+        services = nss, pac
ecf709
+
ecf709
+        [nss]
ecf709
+        memcache_timeout = 0
ecf709
+
ecf709
+        [domain/LOCAL]
ecf709
+        id_provider = local
ecf709
+        min_id = 10000
ecf709
+        max_id = 20000
ecf709
+    """).format(**locals())
ecf709
+    create_conf_fixture(request, conf)
ecf709
+    create_sssd_fixture(request)
ecf709
+    return None
ecf709
+
ecf709
+
ecf709
+@pytest.fixture
ecf709
+def sssd_pac_test_client(request):
ecf709
+    path = os.path.join(config.ABS_BUILDDIR,
ecf709
+                        "..", "..", "..", "sssd_pac_test_client")
ecf709
+    if os.access(path, os.X_OK):
ecf709
+        return path
ecf709
+
ecf709
+    return None
ecf709
+
ecf709
+
ecf709
+def timeout_handler(signum, frame):
ecf709
+    raise Exception("Timeout")
ecf709
+
ecf709
+
ecf709
+def test_multithreaded_pac_client(local_domain_only, sssd_pac_test_client):
ecf709
+    """
ecf709
+    Test for ticket
ecf709
+    https://pagure.io/SSSD/sssd/issue/3518
ecf709
+    """
ecf709
+
ecf709
+    if not sssd_pac_test_client:
ecf709
+        pytest.skip("The sssd_pac_test_client is not available, skipping test")
ecf709
+
ecf709
+    signal.signal(signal.SIGALRM, timeout_handler)
ecf709
+    signal.alarm(10)
ecf709
+
ecf709
+    try:
ecf709
+        subprocess.check_call(sssd_pac_test_client)
ecf709
+    except:
ecf709
+        # cancel alarm
ecf709
+        signal.alarm(0)
ecf709
+        raise Exception("sssd_pac_test_client failed")
ecf709
+
ecf709
+    signal.alarm(0)
ecf709
-- 
ecf709
2.13.5
ecf709