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

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