Blame SOURCES/slapi-0016-schema-compat-add-support-for-timeout-based-NSS-quer.patch

ad2a64
From 6fbf5891e9169142fc0ea37eb8f897a645b82d6f Mon Sep 17 00:00:00 2001
ad2a64
From: Alexander Bokovoy <abokovoy@redhat.com>
ad2a64
Date: Wed, 1 Nov 2017 10:29:41 +0200
ad2a64
Subject: [PATCH 16/17] schema-compat: add support for timeout-based NSS
ad2a64
 queries with libsss_nss_idmap
ad2a64
ad2a64
In case libsss_nss_idmap provides timeout-enabled NSS API, use it.
ad2a64
This solves a problem of too long queries to an NSS backend with
ad2a64
traditional POSIX NSS API. In case SSSD takes too long to respond
ad2a64
to a query, corresponding 389-ds thread running schema-compat plugin
ad2a64
would stuck waiting that response. It can lead to an exhaustion of
ad2a64
389-ds threads.
ad2a64
ad2a64
A refactored interface to NSS backends is introduced with this commit.
ad2a64
A backend API looks like an API an NSS plugin has to implement in glibc
ad2a64
but also allows to handle timeout-based requests internally.
ad2a64
ad2a64
If backend implements timeout-enabled calls, then
ad2a64
backend_nss_set_timeout() function can be used to modify a per-context
ad2a64
state. There is no need for a caller to know whether backend supports
ad2a64
timeout-enabled calls because either way these calls are synchronous
ad2a64
and backend choice is done at compile-time.
ad2a64
ad2a64
schema-compat plugin uses 10 seconds as its default timeout. One can
ad2a64
change it via 'slapi-nss-timeout' attribute in the plugin config entry.
ad2a64
---
ad2a64
 src/Makefile.am          |  11 ++-
ad2a64
 src/back-sch-nss.c       | 187 +++++-------------------------------
ad2a64
 src/back-sch-nss.h       |  70 ++++++++++++++
ad2a64
 src/back-sch-nss_sss.c   | 231 +++++++++++++++++++++++++++++++++++++++++++++
ad2a64
 src/back-sch-sss_idmap.c | 239 +++++++++++++++++++++++++++++++++++++++++++++++
ad2a64
 src/back-sch.h           |   9 +-
ad2a64
 src/plug-sch.c           |   7 ++
ad2a64
 7 files changed, 588 insertions(+), 166 deletions(-)
ad2a64
 create mode 100644 src/back-sch-nss.h
ad2a64
 create mode 100644 src/back-sch-nss_sss.c
ad2a64
 create mode 100644 src/back-sch-sss_idmap.c
ad2a64
ad2a64
diff --git a/src/Makefile.am b/src/Makefile.am
ad2a64
index 6f4926e..cd1efc2 100644
ad2a64
--- a/src/Makefile.am
ad2a64
+++ b/src/Makefile.am
ad2a64
@@ -68,8 +68,17 @@ schemacompat_plugin_la_LIBADD = $(LDAP_LIBS) $(RUNTIME_LIBS) $(LIBPTHREAD) $(CON
ad2a64
 
ad2a64
 if USE_NSSWITCH
ad2a64
 schemacompat_plugin_la_CFLAGS += $(SSS_NSS_IDMAP_CFLAGS)
ad2a64
-schemacompat_plugin_la_SOURCES += back-sch-nss.c
ad2a64
+schemacompat_plugin_la_SOURCES += back-sch-nss.c back-sch-nss.h
ad2a64
 schemacompat_plugin_la_LIBADD += $(SSS_NSS_IDMAP_LIBS)
ad2a64
+# We have two backends for nss operations:
ad2a64
+# (1) directly loading nss_sss.so.2
ad2a64
+# (2) using timeout-enabled API from libsss_nss_idmap
ad2a64
+# We prefer (2) if available
ad2a64
+if USE_SSS_NSS_TIMEOUT
ad2a64
+schemacompat_plugin_la_SOURCES += back-sch-sss_idmap.c
ad2a64
+else
ad2a64
+schemacompat_plugin_la_SOURCES += back-sch-nss_sss.c
ad2a64
+endif
ad2a64
 endif
ad2a64
 
ad2a64
 if USE_PAM
ad2a64
diff --git a/src/back-sch-nss.c b/src/back-sch-nss.c
ad2a64
index e4d027e..e5f91b2 100644
ad2a64
--- a/src/back-sch-nss.c
ad2a64
+++ b/src/back-sch-nss.c
ad2a64
@@ -28,7 +28,6 @@
ad2a64
 #include <string.h>
ad2a64
 #include <time.h>
ad2a64
 #include <unistd.h>
ad2a64
-#include <dlfcn.h>
ad2a64
 #include <errno.h>
ad2a64
 #include <pwd.h>
ad2a64
 #include <grp.h>
ad2a64
@@ -52,6 +51,7 @@
ad2a64
 #include "map.h"
ad2a64
 #include "back-sch.h"
ad2a64
 #include "format.h"
ad2a64
+#include "back-sch-nss.h"
ad2a64
 
ad2a64
 static int
ad2a64
 bvstrprefix(const struct berval *bval, const char *s)
ad2a64
@@ -294,143 +294,6 @@ backend_make_user_entry_from_nsswitch_passwd(struct passwd *pwd,
ad2a64
 	return entry;
ad2a64
 }
ad2a64
 
ad2a64
-/* Possible results of lookup using a nss_* function.
ad2a64
- * Note: don't include nss.h as its path gets overriden by NSS library */
ad2a64
-enum nss_status
ad2a64
-{
ad2a64
-  NSS_STATUS_TRYAGAIN = -2,
ad2a64
-  NSS_STATUS_UNAVAIL,
ad2a64
-  NSS_STATUS_NOTFOUND,
ad2a64
-  NSS_STATUS_SUCCESS,
ad2a64
-  NSS_STATUS_RETURN
ad2a64
-};
ad2a64
-
ad2a64
-struct nss_ops_ctx {
ad2a64
-	void *dl_handle;
ad2a64
-
ad2a64
-	enum nss_status (*getpwnam_r)(const char *name, struct passwd *result,
ad2a64
-			  char *buffer, size_t buflen, int *errnop);
ad2a64
-	enum nss_status (*getpwuid_r)(uid_t uid, struct passwd *result,
ad2a64
-			  char *buffer, size_t buflen, int *errnop);
ad2a64
-	enum nss_status (*setpwent)(void);
ad2a64
-	enum nss_status (*getpwent_r)(struct passwd *result,
ad2a64
-			  char *buffer, size_t buflen, int *errnop);
ad2a64
-	enum nss_status (*endpwent)(void);
ad2a64
-
ad2a64
-	enum nss_status (*getgrnam_r)(const char *name, struct group *result,
ad2a64
-			  char *buffer, size_t buflen, int *errnop);
ad2a64
-	enum nss_status (*getgrgid_r)(gid_t gid, struct group *result,
ad2a64
-			  char *buffer, size_t buflen, int *errnop);
ad2a64
-	enum nss_status (*setgrent)(void);
ad2a64
-	enum nss_status (*getgrent_r)(struct group *result,
ad2a64
-			  char *buffer, size_t buflen, int *errnop);
ad2a64
-	enum nss_status (*endgrent)(void);
ad2a64
-
ad2a64
-	enum nss_status (*initgroups_dyn)(const char *user, gid_t group,
ad2a64
-			      long int *start, long int *size,
ad2a64
-			      gid_t **groups, long int limit,
ad2a64
-			      int *errnop);
ad2a64
-};
ad2a64
-
ad2a64
-void backend_nss_init_context(struct nss_ops_ctx **nss_context)
ad2a64
-{
ad2a64
-	struct nss_ops_ctx *ctx = NULL;
ad2a64
-
ad2a64
-	if (nss_context == NULL) {
ad2a64
-		return;
ad2a64
-	}
ad2a64
-
ad2a64
-	ctx = calloc(1, sizeof(struct nss_ops_ctx));
ad2a64
-
ad2a64
-	*nss_context = ctx;
ad2a64
-	if (ctx == NULL) {
ad2a64
-		return;
ad2a64
-	}
ad2a64
-
ad2a64
-	ctx->dl_handle = dlopen("libnss_sss.so.2", RTLD_NOW);
ad2a64
-	if (ctx->dl_handle == NULL) {
ad2a64
-		goto fail;
ad2a64
-	}
ad2a64
-
ad2a64
-	ctx->getpwnam_r = dlsym(ctx->dl_handle, "_nss_sss_getpwnam_r");
ad2a64
-	if (ctx->getpwnam_r == NULL) {
ad2a64
-		goto fail;
ad2a64
-	}
ad2a64
-
ad2a64
-	ctx->getpwuid_r = dlsym(ctx->dl_handle, "_nss_sss_getpwuid_r");
ad2a64
-	if (ctx->getpwuid_r == NULL) {
ad2a64
-		goto fail;
ad2a64
-	}
ad2a64
-
ad2a64
-	ctx->setpwent = dlsym(ctx->dl_handle, "_nss_sss_setpwent");
ad2a64
-	if (ctx->setpwent == NULL) {
ad2a64
-		goto fail;
ad2a64
-	}
ad2a64
-
ad2a64
-	ctx->getpwent_r = dlsym(ctx->dl_handle, "_nss_sss_getpwent_r");
ad2a64
-	if (ctx->getpwent_r == NULL) {
ad2a64
-		goto fail;
ad2a64
-	}
ad2a64
-
ad2a64
-	ctx->endpwent = dlsym(ctx->dl_handle, "_nss_sss_endpwent");
ad2a64
-	if (ctx->endpwent == NULL) {
ad2a64
-		goto fail;
ad2a64
-	}
ad2a64
-
ad2a64
-	ctx->getgrnam_r = dlsym(ctx->dl_handle, "_nss_sss_getgrnam_r");
ad2a64
-	if (ctx->getgrnam_r == NULL) {
ad2a64
-		goto fail;
ad2a64
-	}
ad2a64
-
ad2a64
-	ctx->getgrgid_r = dlsym(ctx->dl_handle, "_nss_sss_getgrgid_r");
ad2a64
-	if (ctx->getgrgid_r == NULL) {
ad2a64
-		goto fail;
ad2a64
-	}
ad2a64
-
ad2a64
-	ctx->setgrent = dlsym(ctx->dl_handle, "_nss_sss_setgrent");
ad2a64
-	if (ctx->setgrent == NULL) {
ad2a64
-		goto fail;
ad2a64
-	}
ad2a64
-
ad2a64
-	ctx->getgrent_r = dlsym(ctx->dl_handle, "_nss_sss_getgrent_r");
ad2a64
-	if (ctx->getgrent_r == NULL) {
ad2a64
-		goto fail;
ad2a64
-	}
ad2a64
-
ad2a64
-	ctx->endgrent = dlsym(ctx->dl_handle, "_nss_sss_endgrent");
ad2a64
-	if (ctx->endgrent == NULL) {
ad2a64
-		goto fail;
ad2a64
-	}
ad2a64
-
ad2a64
-	ctx->initgroups_dyn = dlsym(ctx->dl_handle, "_nss_sss_initgroups_dyn");
ad2a64
-	if (ctx->initgroups_dyn == NULL) {
ad2a64
-		goto fail;
ad2a64
-	}
ad2a64
-
ad2a64
-	return;
ad2a64
-
ad2a64
-fail:
ad2a64
-	backend_nss_free_context(nss_context);
ad2a64
-
ad2a64
-	return;
ad2a64
-}
ad2a64
-
ad2a64
-void
ad2a64
-backend_nss_free_context(struct nss_ops_ctx **nss_context)
ad2a64
-{
ad2a64
-	if (nss_context == NULL) {
ad2a64
-		return;
ad2a64
-	}
ad2a64
-
ad2a64
-	if ((*nss_context)->dl_handle != NULL) {
ad2a64
-		dlclose((*nss_context)->dl_handle);
ad2a64
-	}
ad2a64
-
ad2a64
-	free((*nss_context));
ad2a64
-	*nss_context = NULL;
ad2a64
-}
ad2a64
-
ad2a64
-
ad2a64
 
ad2a64
 static Slapi_Entry **
ad2a64
 backend_retrieve_user_entry_from_nsswitch(char *user_name, bool_t is_uid,
ad2a64
@@ -456,13 +319,13 @@ repeat:
ad2a64
 	}
ad2a64
 
ad2a64
 	if (is_uid) {
ad2a64
-		rc = ctx->getpwuid_r((uid_t) atoll(user_name), &pwd,
ad2a64
-				     cbdata->nsswitch_buffer,
ad2a64
-				     cbdata->nsswitch_buffer_len, &lerrno);
ad2a64
+		rc = backend_nss_getpwuid(ctx, (uid_t) atoll(user_name), &pwd,
ad2a64
+					  cbdata->nsswitch_buffer,
ad2a64
+					  cbdata->nsswitch_buffer_len, &result, &lerrno);
ad2a64
 	} else {
ad2a64
-		rc = ctx->getpwnam_r(user_name, &pwd,
ad2a64
-				     cbdata->nsswitch_buffer,
ad2a64
-				     cbdata->nsswitch_buffer_len, &lerrno);
ad2a64
+		rc = backend_nss_getpwnam(ctx, user_name, &pwd,
ad2a64
+					  cbdata->nsswitch_buffer,
ad2a64
+					  cbdata->nsswitch_buffer_len, &result, &lerrno);
ad2a64
 	}
ad2a64
 
ad2a64
 	if ((rc != NSS_STATUS_SUCCESS)) {
ad2a64
@@ -591,13 +454,13 @@ repeat:
ad2a64
 	}
ad2a64
 
ad2a64
 	if (is_gid) {
ad2a64
-		rc = ctx->getgrgid_r((gid_t) atoll(group_name), &grp,
ad2a64
-				     cbdata->nsswitch_buffer,
ad2a64
-				     cbdata->nsswitch_buffer_len, &lerrno);
ad2a64
+		rc = backend_nss_getgrgid(ctx, (gid_t) atoll(group_name), &grp,
ad2a64
+					  cbdata->nsswitch_buffer,
ad2a64
+					  cbdata->nsswitch_buffer_len, &result, &lerrno);
ad2a64
 	} else {
ad2a64
-		rc = ctx->getgrnam_r(group_name, &grp,
ad2a64
-				     cbdata->nsswitch_buffer,
ad2a64
-				     cbdata->nsswitch_buffer_len, &lerrno);
ad2a64
+		rc = backend_nss_getgrnam(ctx, group_name, &grp,
ad2a64
+					  cbdata->nsswitch_buffer,
ad2a64
+					  cbdata->nsswitch_buffer_len, &result, &lerrno);
ad2a64
 	}
ad2a64
 	if ((rc != NSS_STATUS_SUCCESS)) {
ad2a64
 		if (lerrno == ERANGE) {
ad2a64
@@ -651,9 +514,9 @@ repeat:
ad2a64
 		return NULL;
ad2a64
 	}
ad2a64
 
ad2a64
-	rc = ctx->getgrgid_r(gid, &grp,
ad2a64
-			     cbdata->nsswitch_buffer,
ad2a64
-			     cbdata->nsswitch_buffer_len, &lerrno);
ad2a64
+	rc = backend_nss_getgrgid(ctx, gid, &grp,
ad2a64
+				  cbdata->nsswitch_buffer,
ad2a64
+				  cbdata->nsswitch_buffer_len, &result, &lerrno);
ad2a64
 
ad2a64
 	if ((rc != NSS_STATUS_SUCCESS)) {
ad2a64
 		if (lerrno == ERANGE) {
ad2a64
@@ -689,7 +552,7 @@ backend_retrieve_group_list_from_nsswitch(char *user_name, char *container_sdn,
ad2a64
 	int i, idx;
ad2a64
 	struct nss_ops_ctx *ctx = NULL;
ad2a64
 	int lerrno = 0;
ad2a64
-	long int ngroups = 0;
ad2a64
+	int ngroups = 0;
ad2a64
 	long int start = 0;
ad2a64
 	enum nss_status rc;
ad2a64
 
ad2a64
@@ -702,9 +565,9 @@ repeat:
ad2a64
 		return NULL;
ad2a64
 	}
ad2a64
 
ad2a64
-	rc = ctx->getpwnam_r(user_name, &pwd,
ad2a64
-			     cbdata->nsswitch_buffer,
ad2a64
-			     cbdata->nsswitch_buffer_len, &lerrno);
ad2a64
+	rc = backend_nss_getpwnam(ctx, user_name, &pwd,
ad2a64
+				  cbdata->nsswitch_buffer,
ad2a64
+				  cbdata->nsswitch_buffer_len, &pwd_result, &lerrno);
ad2a64
 
ad2a64
 	if ((rc != NSS_STATUS_SUCCESS)) {
ad2a64
 		if (lerrno == ERANGE) {
ad2a64
@@ -723,19 +586,15 @@ repeat:
ad2a64
 	}
ad2a64
 
ad2a64
 	ngroups = 32;
ad2a64
-	start = 0;
ad2a64
 	grouplist = malloc(sizeof(gid_t) * ngroups);
ad2a64
 	if (grouplist == NULL) {
ad2a64
 		return NULL;
ad2a64
 	}
ad2a64
 
ad2a64
-	grouplist[0] = pwd.pw_gid;
ad2a64
-	start++;
ad2a64
-
ad2a64
 	do {
ad2a64
-		rc = ctx->initgroups_dyn(user_name, pwd.pw_gid,
ad2a64
-					 &start, &ngroups, &grouplist,
ad2a64
-					 -1, &lerrno);
ad2a64
+		rc = backend_nss_getgrouplist(ctx, user_name, pwd.pw_gid,
ad2a64
+					      grouplist, &ngroups,
ad2a64
+					      &lerrno);
ad2a64
 		if ((rc != NSS_STATUS_SUCCESS)) {
ad2a64
 			tmp_list = realloc(grouplist, ngroups * sizeof(gid_t));
ad2a64
 			if (tmp_list == NULL) {
ad2a64
diff --git a/src/back-sch-nss.h b/src/back-sch-nss.h
ad2a64
new file mode 100644
ad2a64
index 0000000..54a3c07
ad2a64
--- /dev/null
ad2a64
+++ b/src/back-sch-nss.h
ad2a64
@@ -0,0 +1,70 @@
ad2a64
+/*
ad2a64
+ * Copyright 2017 Red Hat, Inc.
ad2a64
+ *
ad2a64
+ * This Program is free software; you can redistribute it and/or modify
ad2a64
+ * it under the terms of the GNU General Public License as published by
ad2a64
+ * the Free Software Foundation; version 2 of the License.
ad2a64
+ *
ad2a64
+ * This Program is distributed in the hope that it will be useful, but
ad2a64
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
ad2a64
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ad2a64
+ * General Public License for more details.
ad2a64
+ *
ad2a64
+ * You should have received a copy of the GNU General Public License
ad2a64
+ * along with this Program; if not, write to the
ad2a64
+ *
ad2a64
+ *   Free Software Foundation, Inc.
ad2a64
+ *   59 Temple Place, Suite 330
ad2a64
+ *   Boston, MA 02111-1307 USA
ad2a64
+ *
ad2a64
+ */
ad2a64
+
ad2a64
+#ifndef back_sch_nss_h
ad2a64
+#define back_sch_nss_h
ad2a64
+#include <unistd.h>
ad2a64
+#include <pwd.h>
ad2a64
+#include <grp.h>
ad2a64
+
ad2a64
+/* Possible results of lookup using a nss_* function.
ad2a64
+ * Note: don't include nss.h as its path gets overriden by NSS library */
ad2a64
+enum nss_status
ad2a64
+{
ad2a64
+  NSS_STATUS_TRYAGAIN = -2,
ad2a64
+  NSS_STATUS_UNAVAIL,
ad2a64
+  NSS_STATUS_NOTFOUND,
ad2a64
+  NSS_STATUS_SUCCESS,
ad2a64
+  NSS_STATUS_RETURN
ad2a64
+};
ad2a64
+
ad2a64
+struct nss_ops_ctx;
ad2a64
+
ad2a64
+enum nss_status backend_nss_getpwnam(struct nss_ops_ctx *nss_context,
ad2a64
+				     const char *name, struct passwd *pwd,
ad2a64
+				     char *buffer, size_t buflen,
ad2a64
+				     struct passwd **result,
ad2a64
+				     int *lerrno);
ad2a64
+
ad2a64
+enum nss_status backend_nss_getpwuid(struct nss_ops_ctx *nss_context,
ad2a64
+				     uid_t uid, struct passwd *pwd,
ad2a64
+				     char *buffer, size_t buflen,
ad2a64
+				     struct passwd **result,
ad2a64
+				     int *lerrno);
ad2a64
+
ad2a64
+enum nss_status backend_nss_getgrnam(struct nss_ops_ctx *nss_context,
ad2a64
+				     const char *name, struct group *grp,
ad2a64
+				     char *buffer, size_t buflen,
ad2a64
+				     struct group **result,
ad2a64
+				     int *lerrno);
ad2a64
+
ad2a64
+enum nss_status backend_nss_getgrgid(struct nss_ops_ctx *nss_context,
ad2a64
+				     gid_t gid, struct group *grp,
ad2a64
+				     char *buffer, size_t buflen,
ad2a64
+				     struct group **result,
ad2a64
+				     int *lerrno);
ad2a64
+
ad2a64
+enum nss_status backend_nss_getgrouplist(struct nss_ops_ctx *nss_context,
ad2a64
+					 const char *name, gid_t group,
ad2a64
+					 gid_t *groups, int *ngroups,
ad2a64
+					 int *lerrno);
ad2a64
+
ad2a64
+#endif /* back_sch_nss_h */
ad2a64
diff --git a/src/back-sch-nss_sss.c b/src/back-sch-nss_sss.c
ad2a64
new file mode 100644
ad2a64
index 0000000..e3e6628
ad2a64
--- /dev/null
ad2a64
+++ b/src/back-sch-nss_sss.c
ad2a64
@@ -0,0 +1,231 @@
ad2a64
+/*
ad2a64
+ * Copyright 2013-2017 Red Hat, Inc.
ad2a64
+ *
ad2a64
+ * This Program is free software; you can redistribute it and/or modify
ad2a64
+ * it under the terms of the GNU General Public License as published by
ad2a64
+ * the Free Software Foundation; version 2 of the License.
ad2a64
+ *
ad2a64
+ * This Program is distributed in the hope that it will be useful, but
ad2a64
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
ad2a64
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ad2a64
+ * General Public License for more details.
ad2a64
+ *
ad2a64
+ * You should have received a copy of the GNU General Public License
ad2a64
+ * along with this Program; if not, write to the
ad2a64
+ *
ad2a64
+ *   Free Software Foundation, Inc.
ad2a64
+ *   59 Temple Place, Suite 330
ad2a64
+ *   Boston, MA 02111-1307 USA
ad2a64
+ *
ad2a64
+ */
ad2a64
+
ad2a64
+#ifdef HAVE_CONFIG_H
ad2a64
+#include "config.h"
ad2a64
+#endif
ad2a64
+
ad2a64
+#include <sys/types.h>
ad2a64
+#include <stdlib.h>
ad2a64
+#include <string.h>
ad2a64
+#include <time.h>
ad2a64
+#include <unistd.h>
ad2a64
+#include <dlfcn.h>
ad2a64
+#include <errno.h>
ad2a64
+#include <pwd.h>
ad2a64
+#include <grp.h>
ad2a64
+#include "back-sch-nss.h"
ad2a64
+
ad2a64
+struct nss_ops_ctx {
ad2a64
+	void *dl_handle;
ad2a64
+	long int initgroups_start;
ad2a64
+
ad2a64
+	enum nss_status (*getpwnam_r)(const char *name, struct passwd *result,
ad2a64
+			  char *buffer, size_t buflen, int *errnop);
ad2a64
+	enum nss_status (*getpwuid_r)(uid_t uid, struct passwd *result,
ad2a64
+			  char *buffer, size_t buflen, int *errnop);
ad2a64
+	enum nss_status (*getgrnam_r)(const char *name, struct group *result,
ad2a64
+			  char *buffer, size_t buflen, int *errnop);
ad2a64
+	enum nss_status (*getgrgid_r)(gid_t gid, struct group *result,
ad2a64
+			  char *buffer, size_t buflen, int *errnop);
ad2a64
+	enum nss_status (*initgroups_dyn)(const char *user, gid_t group,
ad2a64
+			      long int *start, long int *size,
ad2a64
+			      gid_t **groups, long int limit,
ad2a64
+			      int *errnop);
ad2a64
+};
ad2a64
+
ad2a64
+void backend_nss_init_context(struct nss_ops_ctx **nss_context)
ad2a64
+{
ad2a64
+	struct nss_ops_ctx *ctx = NULL;
ad2a64
+
ad2a64
+	if (nss_context == NULL) {
ad2a64
+		return;
ad2a64
+	}
ad2a64
+
ad2a64
+	ctx = calloc(1, sizeof(struct nss_ops_ctx));
ad2a64
+
ad2a64
+	*nss_context = ctx;
ad2a64
+	if (ctx == NULL) {
ad2a64
+		return;
ad2a64
+	}
ad2a64
+
ad2a64
+	ctx->dl_handle = dlopen("libnss_sss.so.2", RTLD_NOW);
ad2a64
+	if (ctx->dl_handle == NULL) {
ad2a64
+		goto fail;
ad2a64
+	}
ad2a64
+
ad2a64
+	ctx->getpwnam_r = dlsym(ctx->dl_handle, "_nss_sss_getpwnam_r");
ad2a64
+	if (ctx->getpwnam_r == NULL) {
ad2a64
+		goto fail;
ad2a64
+	}
ad2a64
+
ad2a64
+	ctx->getpwuid_r = dlsym(ctx->dl_handle, "_nss_sss_getpwuid_r");
ad2a64
+	if (ctx->getpwuid_r == NULL) {
ad2a64
+		goto fail;
ad2a64
+	}
ad2a64
+
ad2a64
+	ctx->getgrnam_r = dlsym(ctx->dl_handle, "_nss_sss_getgrnam_r");
ad2a64
+	if (ctx->getgrnam_r == NULL) {
ad2a64
+		goto fail;
ad2a64
+	}
ad2a64
+
ad2a64
+	ctx->getgrgid_r = dlsym(ctx->dl_handle, "_nss_sss_getgrgid_r");
ad2a64
+	if (ctx->getgrgid_r == NULL) {
ad2a64
+		goto fail;
ad2a64
+	}
ad2a64
+
ad2a64
+	ctx->initgroups_dyn = dlsym(ctx->dl_handle, "_nss_sss_initgroups_dyn");
ad2a64
+	if (ctx->initgroups_dyn == NULL) {
ad2a64
+		goto fail;
ad2a64
+	}
ad2a64
+
ad2a64
+	return;
ad2a64
+
ad2a64
+fail:
ad2a64
+	backend_nss_free_context(nss_context);
ad2a64
+
ad2a64
+	return;
ad2a64
+}
ad2a64
+
ad2a64
+void
ad2a64
+backend_nss_free_context(struct nss_ops_ctx **nss_context)
ad2a64
+{
ad2a64
+	if (nss_context == NULL) {
ad2a64
+		return;
ad2a64
+	}
ad2a64
+
ad2a64
+	if ((*nss_context)->dl_handle != NULL) {
ad2a64
+		dlclose((*nss_context)->dl_handle);
ad2a64
+	}
ad2a64
+
ad2a64
+	free((*nss_context));
ad2a64
+	*nss_context = NULL;
ad2a64
+}
ad2a64
+
ad2a64
+
ad2a64
+/* Following three functions cannot be implemented with nss_sss.so.2
ad2a64
+ * As result, we simply do nothing here */
ad2a64
+
ad2a64
+void backend_nss_set_timeout(struct nss_ops_ctx **nss_context,
ad2a64
+			     unsigned int timeout) {
ad2a64
+	/* no operation */
ad2a64
+}
ad2a64
+
ad2a64
+void backend_nss_evict_user(struct nss_ops_ctx **nss_context,
ad2a64
+			    const char *name) {
ad2a64
+	/* no operation */
ad2a64
+}
ad2a64
+
ad2a64
+void backend_nss_evict_group(struct nss_ops_ctx **nss_context,
ad2a64
+			     const char *name) {
ad2a64
+	/* no operation */
ad2a64
+}
ad2a64
+
ad2a64
+enum nss_status backend_nss_getpwnam(struct nss_ops_ctx *nss_context,
ad2a64
+				     const char *name, struct passwd *pwd,
ad2a64
+				     char *buffer, size_t buflen,
ad2a64
+				     struct passwd **result,
ad2a64
+				     int *lerrno) {
ad2a64
+
ad2a64
+	if (nss_context == NULL) {
ad2a64
+		return NSS_STATUS_UNAVAIL;
ad2a64
+	}
ad2a64
+
ad2a64
+	return (enum nss_status)
ad2a64
+		nss_context->getpwnam_r(name, pwd,
ad2a64
+					buffer, buflen,
ad2a64
+					result, lerrno);
ad2a64
+}
ad2a64
+
ad2a64
+enum nss_status backend_nss_getpwuid(struct nss_ops_ctx *nss_context,
ad2a64
+				     uid_t uid, struct passwd *pwd,
ad2a64
+				     char *buffer, size_t buflen,
ad2a64
+				     struct passwd **result,
ad2a64
+				     int *lerrno) {
ad2a64
+
ad2a64
+	if (nss_context == NULL) {
ad2a64
+		return NSS_STATUS_UNAVAIL;
ad2a64
+	}
ad2a64
+
ad2a64
+	return (enum nss_status)
ad2a64
+		nss_context->getpwuid_r(uid, pwd,
ad2a64
+					buffer, buflen,
ad2a64
+					result, lerrno);
ad2a64
+}
ad2a64
+
ad2a64
+enum nss_status backend_nss_getgrnam(struct nss_ops_ctx *nss_context,
ad2a64
+				     const char *name, struct group *grp,
ad2a64
+				     char *buffer, size_t buflen,
ad2a64
+				     struct group **result,
ad2a64
+				     int *lerrno) {
ad2a64
+
ad2a64
+	if (nss_context == NULL) {
ad2a64
+		return NSS_STATUS_UNAVAIL;
ad2a64
+	}
ad2a64
+
ad2a64
+	return (enum nss_status)
ad2a64
+		nss_context->getgrnam_r(name, grp,
ad2a64
+					buffer, buflen,
ad2a64
+					result, lerrno);
ad2a64
+}
ad2a64
+
ad2a64
+enum nss_status backend_nss_getgrgid(struct nss_ops_ctx *nss_context,
ad2a64
+				     gid_t gid, struct group *grp,
ad2a64
+				     char *buffer, size_t buflen,
ad2a64
+				     struct group **result,
ad2a64
+				     int *lerrno) {
ad2a64
+
ad2a64
+	if (nss_context == NULL) {
ad2a64
+		return NSS_STATUS_UNAVAIL;
ad2a64
+	}
ad2a64
+
ad2a64
+	return (enum nss_status)
ad2a64
+		nss_context->getgrgid_r(gid, grp,
ad2a64
+					buffer, buflen,
ad2a64
+					result, lerrno);
ad2a64
+}
ad2a64
+
ad2a64
+enum nss_status backend_nss_getgrouplist(struct nss_ops_ctx *nss_context,
ad2a64
+					 const char *name, gid_t group,
ad2a64
+					 gid_t *groups, int *ngroups,
ad2a64
+					 int *lerrno) {
ad2a64
+	enum nss_status ret = NSS_STATUS_UNAVAIL;
ad2a64
+
ad2a64
+	if (nss_context == NULL) {
ad2a64
+		return NSS_STATUS_UNAVAIL;
ad2a64
+	}
ad2a64
+
ad2a64
+	if (nss_context->initgroups_start == 0) {
ad2a64
+		groups[0] = group;
ad2a64
+		nss_context->initgroups_start++;
ad2a64
+	}
ad2a64
+
ad2a64
+	ret = nss_context->initgroups_dyn(name, group,
ad2a64
+					  &nss_context->initgroups_start,
ad2a64
+					  &ngroups, &groups,
ad2a64
+					  -1, &lerrno);
ad2a64
+	if (ret == NSS_STATUS_SUCCESS) {
ad2a64
+		nss_context->initgroups_start = 0;
ad2a64
+	}
ad2a64
+
ad2a64
+	return ret;
ad2a64
+}
ad2a64
+
ad2a64
diff --git a/src/back-sch-sss_idmap.c b/src/back-sch-sss_idmap.c
ad2a64
new file mode 100644
ad2a64
index 0000000..6a31267
ad2a64
--- /dev/null
ad2a64
+++ b/src/back-sch-sss_idmap.c
ad2a64
@@ -0,0 +1,239 @@
ad2a64
+/*
ad2a64
+ * Copyright 2013-2017 Red Hat, Inc.
ad2a64
+ *
ad2a64
+ * This Program is free software; you can redistribute it and/or modify
ad2a64
+ * it under the terms of the GNU General Public License as published by
ad2a64
+ * the Free Software Foundation; version 2 of the License.
ad2a64
+ *
ad2a64
+ * This Program is distributed in the hope that it will be useful, but
ad2a64
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
ad2a64
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ad2a64
+ * General Public License for more details.
ad2a64
+ *
ad2a64
+ * You should have received a copy of the GNU General Public License
ad2a64
+ * along with this Program; if not, write to the
ad2a64
+ *
ad2a64
+ *   Free Software Foundation, Inc.
ad2a64
+ *   59 Temple Place, Suite 330
ad2a64
+ *   Boston, MA 02111-1307 USA
ad2a64
+ *
ad2a64
+ */
ad2a64
+
ad2a64
+#ifdef HAVE_CONFIG_H
ad2a64
+#include "config.h"
ad2a64
+#endif
ad2a64
+
ad2a64
+#include <sys/types.h>
ad2a64
+#include <stdlib.h>
ad2a64
+#include <string.h>
ad2a64
+#include <time.h>
ad2a64
+#include <unistd.h>
ad2a64
+#include <errno.h>
ad2a64
+#include <pwd.h>
ad2a64
+#include <grp.h>
ad2a64
+#include "back-sch-nss.h"
ad2a64
+
ad2a64
+/* SSSD only exposes *_timeout() variants if the following symbol is defined */
ad2a64
+#define IPA_389DS_PLUGIN_HELPER_CALLS
ad2a64
+#include <sss_nss_idmap.h>
ad2a64
+
ad2a64
+struct nss_ops_ctx {
ad2a64
+	unsigned int timeout;
ad2a64
+};
ad2a64
+
ad2a64
+static enum nss_status __convert_sss_nss2nss_status(int errcode) {
ad2a64
+	enum nss_status ret = NSS_STATUS_UNAVAIL;
ad2a64
+
ad2a64
+	if (errcode == 0) {
ad2a64
+		ret = NSS_STATUS_SUCCESS;
ad2a64
+	} else if (errcode == ENOENT) {
ad2a64
+		ret = NSS_STATUS_NOTFOUND;
ad2a64
+	} else if(errcode == ERANGE) {
ad2a64
+		ret = NSS_STATUS_TRYAGAIN;
ad2a64
+	} else if(errcode == ETIMEDOUT) {
ad2a64
+		ret = NSS_STATUS_UNAVAIL;
ad2a64
+	} else if(errcode == ETIME) {
ad2a64
+		ret = NSS_STATUS_TRYAGAIN;
ad2a64
+	};
ad2a64
+
ad2a64
+	return ret;
ad2a64
+}
ad2a64
+
ad2a64
+void backend_nss_init_context(struct nss_ops_ctx **nss_context)
ad2a64
+{
ad2a64
+	struct nss_ops_ctx *ctx = NULL;
ad2a64
+
ad2a64
+	if (nss_context == NULL) {
ad2a64
+		return;
ad2a64
+	}
ad2a64
+
ad2a64
+	ctx = calloc(1, sizeof(struct nss_ops_ctx));
ad2a64
+
ad2a64
+	*nss_context = ctx;
ad2a64
+}
ad2a64
+
ad2a64
+void backend_nss_free_context(struct nss_ops_ctx **nss_context)
ad2a64
+{
ad2a64
+	if (nss_context == NULL) {
ad2a64
+		return;
ad2a64
+	}
ad2a64
+
ad2a64
+	free((*nss_context));
ad2a64
+	*nss_context = NULL;
ad2a64
+}
ad2a64
+
ad2a64
+
ad2a64
+void backend_nss_set_timeout(struct nss_ops_ctx *nss_context,
ad2a64
+			     unsigned int timeout) {
ad2a64
+	if (nss_context == NULL) {
ad2a64
+		return;
ad2a64
+	}
ad2a64
+
ad2a64
+	nss_context->timeout = timeout;
ad2a64
+}
ad2a64
+
ad2a64
+/* TODO: handle buffers and memory allocation in this function */
ad2a64
+void backend_nss_evict_user(struct nss_ops_ctx *nss_context,
ad2a64
+			    const char *name) {
ad2a64
+	if (nss_context == NULL) {
ad2a64
+		return;
ad2a64
+	}
ad2a64
+
ad2a64
+	(void) sss_nss_getpwnam_timeout(name, NULL,
ad2a64
+					NULL, 0,
ad2a64
+					NULL,
ad2a64
+					SSS_NSS_EX_FLAG_INVALIDATE_CACHE,
ad2a64
+					nss_context->timeout);
ad2a64
+}
ad2a64
+
ad2a64
+/* TODO: handle buffers and memory allocation in this function */
ad2a64
+void backend_nss_evict_group(struct nss_ops_ctx *nss_context,
ad2a64
+			     const char *name) {
ad2a64
+	if (nss_context == NULL) {
ad2a64
+		return;
ad2a64
+	}
ad2a64
+
ad2a64
+	(void) sss_nss_getgrnam_timeout(name, NULL,
ad2a64
+					NULL, 0,
ad2a64
+					NULL,
ad2a64
+					SSS_NSS_EX_FLAG_INVALIDATE_CACHE,
ad2a64
+					nss_context->timeout);
ad2a64
+}
ad2a64
+
ad2a64
+enum nss_status backend_nss_getpwnam(struct nss_ops_ctx *nss_context,
ad2a64
+				     const char *name, struct passwd *pwd,
ad2a64
+				     char *buffer, size_t buflen,
ad2a64
+				     struct passwd **result,
ad2a64
+				     int *lerrno) {
ad2a64
+	int ret = 0;
ad2a64
+
ad2a64
+	if (nss_context == NULL) {
ad2a64
+		return NSS_STATUS_UNAVAIL;
ad2a64
+	}
ad2a64
+
ad2a64
+	ret = sss_nss_getpwnam_timeout(name, pwd,
ad2a64
+				       buffer, buflen,
ad2a64
+				       result,
ad2a64
+				       SSS_NSS_EX_FLAG_NO_FLAGS,
ad2a64
+				       nss_context->timeout);
ad2a64
+
ad2a64
+	if (ret != 0 && lerrno != NULL) {
ad2a64
+		/* SSSD translates errno into return code */
ad2a64
+		*lerrno = ret;
ad2a64
+	}
ad2a64
+	return __convert_sss_nss2nss_status(ret);
ad2a64
+}
ad2a64
+
ad2a64
+enum nss_status backend_nss_getpwuid(struct nss_ops_ctx *nss_context,
ad2a64
+				     uid_t uid, struct passwd *pwd,
ad2a64
+				     char *buffer, size_t buflen,
ad2a64
+				     struct passwd **result,
ad2a64
+				     int *lerrno) {
ad2a64
+
ad2a64
+	int ret = 0;
ad2a64
+
ad2a64
+	if (nss_context == NULL) {
ad2a64
+		return NSS_STATUS_UNAVAIL;
ad2a64
+	}
ad2a64
+
ad2a64
+	ret = sss_nss_getpwuid_timeout(uid, pwd,
ad2a64
+				       buffer, buflen,
ad2a64
+				       result,
ad2a64
+				       SSS_NSS_EX_FLAG_NO_FLAGS,
ad2a64
+				       nss_context->timeout);
ad2a64
+	if (ret != 0 && lerrno != NULL) {
ad2a64
+		/* SSSD translates errno into return code */
ad2a64
+		*lerrno = ret;
ad2a64
+	}
ad2a64
+	return __convert_sss_nss2nss_status(ret);
ad2a64
+}
ad2a64
+
ad2a64
+enum nss_status backend_nss_getgrnam(struct nss_ops_ctx *nss_context,
ad2a64
+				     const char *name, struct group *grp,
ad2a64
+				     char *buffer, size_t buflen,
ad2a64
+				     struct group **result,
ad2a64
+				     int *lerrno) {
ad2a64
+
ad2a64
+	int ret = 0;
ad2a64
+
ad2a64
+	if (nss_context == NULL) {
ad2a64
+		return NSS_STATUS_UNAVAIL;
ad2a64
+	}
ad2a64
+
ad2a64
+	ret = sss_nss_getgrnam_timeout(name, grp,
ad2a64
+				       buffer, buflen,
ad2a64
+				       result,
ad2a64
+				       SSS_NSS_EX_FLAG_NO_FLAGS,
ad2a64
+				       nss_context->timeout);
ad2a64
+	if (ret != 0 && lerrno != NULL) {
ad2a64
+		/* SSSD translates errno into return code */
ad2a64
+		*lerrno = ret;
ad2a64
+	}
ad2a64
+	return __convert_sss_nss2nss_status(ret);
ad2a64
+}
ad2a64
+
ad2a64
+enum nss_status backend_nss_getgrgid(struct nss_ops_ctx *nss_context,
ad2a64
+				     gid_t gid, struct group *grp,
ad2a64
+				     char *buffer, size_t buflen,
ad2a64
+				     struct group **result,
ad2a64
+				     int *lerrno) {
ad2a64
+
ad2a64
+	int ret = 0;
ad2a64
+
ad2a64
+	if (nss_context == NULL) {
ad2a64
+		return NSS_STATUS_UNAVAIL;
ad2a64
+	}
ad2a64
+
ad2a64
+	ret = sss_nss_getgrgid_timeout(gid, grp,
ad2a64
+				       buffer, buflen,
ad2a64
+				       result,
ad2a64
+				       SSS_NSS_EX_FLAG_NO_FLAGS,
ad2a64
+				       nss_context->timeout);
ad2a64
+	if (ret != 0 && lerrno != NULL) {
ad2a64
+		/* SSSD translates errno into return code */
ad2a64
+		*lerrno = ret;
ad2a64
+	}
ad2a64
+	return __convert_sss_nss2nss_status(ret);
ad2a64
+}
ad2a64
+
ad2a64
+enum nss_status backend_nss_getgrouplist(struct nss_ops_ctx *nss_context,
ad2a64
+					 const char *name, gid_t group,
ad2a64
+					 gid_t *groups, int *ngroups,
ad2a64
+					 int *lerrno) {
ad2a64
+	int ret = 0;
ad2a64
+
ad2a64
+	if (nss_context == NULL) {
ad2a64
+		return NSS_STATUS_UNAVAIL;
ad2a64
+	}
ad2a64
+
ad2a64
+	ret = sss_nss_getgrouplist_timeout(name, group,
ad2a64
+					   groups, ngroups,
ad2a64
+					   SSS_NSS_EX_FLAG_NO_FLAGS,
ad2a64
+					   nss_context->timeout);
ad2a64
+	if (ret != 0 && lerrno != NULL) {
ad2a64
+		/* SSSD translates errno into return code */
ad2a64
+		*lerrno = ret;
ad2a64
+	}
ad2a64
+	return __convert_sss_nss2nss_status(ret);
ad2a64
+}
ad2a64
+
ad2a64
diff --git a/src/back-sch.h b/src/back-sch.h
ad2a64
index 9a9abc7..a400419 100644
ad2a64
--- a/src/back-sch.h
ad2a64
+++ b/src/back-sch.h
ad2a64
@@ -152,10 +152,17 @@ typedef struct backend_extop_handlers {
ad2a64
 
ad2a64
 int backend_analyze_search_filter(Slapi_Filter *filter, struct backend_search_filter_config *config);
ad2a64
 
ad2a64
-/* Operations against nsswitch API */
ad2a64
+/* NSS backend operations implemented using either nss_sss.so.2 or libsss_nss_idmap API */
ad2a64
 struct nss_ops_ctx;
ad2a64
+
ad2a64
 void backend_nss_init_context(struct nss_ops_ctx **nss_context);
ad2a64
 void backend_nss_free_context(struct nss_ops_ctx **nss_context);
ad2a64
+void backend_nss_set_timeout(struct nss_ops_ctx **nss_context,
ad2a64
+			     unsigned int timeout);
ad2a64
+void backend_nss_evict_user(struct nss_ops_ctx **nss_context,
ad2a64
+			    const char *name);
ad2a64
+void backend_nss_evict_group(struct nss_ops_ctx **nss_context,
ad2a64
+			     const char *name);
ad2a64
 
ad2a64
 void backend_search_nsswitch(struct backend_set_data *set_data,
ad2a64
 			     struct backend_search_cbdata *cbdata);
ad2a64
diff --git a/src/plug-sch.c b/src/plug-sch.c
ad2a64
index 00e7041..6ee4042 100644
ad2a64
--- a/src/plug-sch.c
ad2a64
+++ b/src/plug-sch.c
ad2a64
@@ -104,6 +104,7 @@ plugin_startup(Slapi_PBlock *pb)
ad2a64
 	struct plugin_state *state;
ad2a64
 	Slapi_Entry *plugin_entry = NULL;
ad2a64
 	Slapi_DN *pluginsdn = NULL;
ad2a64
+	unsigned int nss_timeout = 10000;
ad2a64
 
ad2a64
 	slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &state);
ad2a64
 	slapi_pblock_get(pb, SLAPI_TARGET_SDN, &pluginsdn);
ad2a64
@@ -130,7 +131,13 @@ plugin_startup(Slapi_PBlock *pb)
ad2a64
 		state->use_entry_cache = backend_shr_get_vattr_boolean(state, plugin_entry,
ad2a64
 									"slapi-entry-cache",
ad2a64
 									1);
ad2a64
+		nss_timeout = backend_shr_get_vattr_uint(state, plugin_entry,
ad2a64
+							 "slapi-nss-timeout",
ad2a64
+							 10000);
ad2a64
+
ad2a64
 	}
ad2a64
+	backend_nss_set_timeout(state->nss_context, nss_timeout);
ad2a64
+
ad2a64
 	state->cached_entries_lock = wrap_new_rwlock();
ad2a64
 	wrap_rwlock_wrlock(state->cached_entries_lock);
ad2a64
 	state->cached_entries = PL_NewHashTable(0, PL_HashString, PL_CompareStrings, PL_CompareValues, 0, 0);
ad2a64
-- 
ad2a64
2.13.6
ad2a64