Blame SOURCES/0040-Ticket-47559-hung-server-related-to-sasl-and-initial.patch

ba46c7
From a9870f937ae83728326957c408608eff3b9b7b26 Mon Sep 17 00:00:00 2001
ba46c7
From: Rich Megginson <rmeggins@redhat.com>
ba46c7
Date: Mon, 14 Oct 2013 12:43:51 -0600
ba46c7
Subject: [PATCH 40/41] Ticket #47559 hung server - related to sasl and initialize
ba46c7
ba46c7
https://fedorahosted.org/389/ticket/47559
ba46c7
Reviewed by: nhosoi (Thanks!)
ba46c7
Branch: rhel-7.0
ba46c7
Fix Description: Use a mutex to protect calls to openldap functions that do
ba46c7
anything with crypto - bind, unbind, start_tls, other calls.
ba46c7
Platforms tested: RHEL6 x86_64
ba46c7
Flag Day: no
ba46c7
Doc impact: no
ba46c7
(cherry picked from commit da3e4aa40b04094d0e77052b894b0f0c335ea1ef)
ba46c7
(cherry picked from commit 7b3b2fe9d4a7f73a12b4f2d499b2e6a2f80e454b)
ba46c7
(cherry picked from commit 8a7ee90d6a770f1732bcd03b20471de3a6162b2b)
ba46c7
---
ba46c7
 ldap/servers/slapd/ldaputil.c |   51 +++++++++++++++++++++++++++++++++++++---
ba46c7
 1 files changed, 47 insertions(+), 4 deletions(-)
ba46c7
ba46c7
diff --git a/ldap/servers/slapd/ldaputil.c b/ldap/servers/slapd/ldaputil.c
ba46c7
index edc8267..aa78d3d 100644
ba46c7
--- a/ldap/servers/slapd/ldaputil.c
ba46c7
+++ b/ldap/servers/slapd/ldaputil.c
ba46c7
@@ -99,10 +99,16 @@
ba46c7
 #if !defined(USE_OPENLDAP)
ba46c7
 #include <ldap_ssl.h>
ba46c7
 #include <ldappr.h>
ba46c7
+#define BIND_LOCK (void)0
ba46c7
+#define BIND_UNLOCK (void)0
ba46c7
 #else
ba46c7
 /* need mutex around ldap_initialize - see https://fedorahosted.org/389/ticket/348 */
ba46c7
 static PRCallOnceType ol_init_callOnce = {0,0};
ba46c7
 static PRLock *ol_init_lock = NULL;
ba46c7
+/* need mutex around ldap_sasl_bind - see https://fedorahosted.org/389/ticket/47599 */
ba46c7
+static PRLock *ol_bind_lock = NULL;
ba46c7
+#define BIND_LOCK PR_Lock(ol_bind_lock)
ba46c7
+#define BIND_UNLOCK PR_Unlock(ol_bind_lock)
ba46c7
 
ba46c7
 static PRStatus
ba46c7
 internal_ol_init_init(void)
ba46c7
@@ -110,12 +116,20 @@ internal_ol_init_init(void)
ba46c7
     PR_ASSERT(NULL == ol_init_lock);
ba46c7
     if ((ol_init_lock = PR_NewLock()) == NULL) {
ba46c7
         PRErrorCode errorCode = PR_GetError();
ba46c7
-        slapi_log_error(SLAPI_LOG_FATAL, "internal_ol_init_init", "PR_NewLock failed %d:%s\n",
ba46c7
+        slapi_log_error(SLAPI_LOG_FATAL, "internal_ol_init_init", "PR_NewLock init_lock failed %d:%s\n",
ba46c7
                         errorCode, slapd_pr_strerror(errorCode));
ba46c7
         return PR_FAILURE;
ba46c7
     }
ba46c7
 
ba46c7
-    return PR_SUCCESS;
ba46c7
+    PR_ASSERT(NULL == ol_bind_lock);
ba46c7
+    if ((ol_bind_lock = PR_NewLock()) == NULL) {
ba46c7
+        PRErrorCode errorCode = PR_GetError();
ba46c7
+        slapi_log_error(SLAPI_LOG_FATAL, "internal_ol_init_init", "PR_NewLock bind_lock failed %d:%s\n",
ba46c7
+                        errorCode, slapd_pr_strerror(errorCode));
ba46c7
+        return PR_FAILURE;
ba46c7
+    }
ba46c7
+
ba46c7
+   return PR_SUCCESS;
ba46c7
 }
ba46c7
 #endif
ba46c7
 
ba46c7
@@ -145,7 +159,16 @@ void
ba46c7
 slapi_ldap_unbind( LDAP *ld )
ba46c7
 {
ba46c7
     if ( ld != NULL ) {
ba46c7
+#if defined(USE_OPENLDAP)
ba46c7
+	if (PR_SUCCESS != PR_CallOnce(&ol_init_callOnce, internal_ol_init_init)) {
ba46c7
+		slapi_log_error(SLAPI_LOG_FATAL, "slapi_ldap_unbind",
ba46c7
+		                "Could not perform internal ol_init init\n");
ba46c7
+		return;
ba46c7
+	}
ba46c7
+#endif
ba46c7
+	BIND_LOCK;
ba46c7
 	ldap_unbind_ext( ld, NULL, NULL );
ba46c7
+	BIND_UNLOCK;
ba46c7
     }
ba46c7
 }
ba46c7
 
ba46c7
@@ -1031,11 +1054,22 @@ slapi_ldap_bind(
ba46c7
     ldap_controls_free(clientctrls);
ba46c7
     ldap_set_option(ld, LDAP_OPT_CLIENT_CONTROLS, NULL);
ba46c7
 
ba46c7
+#if defined(USE_OPENLDAP)
ba46c7
+    if (PR_SUCCESS != PR_CallOnce(&ol_init_callOnce, internal_ol_init_init)) {
ba46c7
+        slapi_log_error(SLAPI_LOG_FATAL, "slapi_ldap_bind",
ba46c7
+           "Could not perform internal ol_init init\n");
ba46c7
+        rc = -1;
ba46c7
+        goto done;
ba46c7
+    }
ba46c7
+#endif
ba46c7
+
ba46c7
     if ((secure > 0) && mech && !strcmp(mech, LDAP_SASL_EXTERNAL)) {
ba46c7
 #if defined(USE_OPENLDAP)
ba46c7
 	/* we already set up a tls context in slapi_ldap_init_ext() - this will
ba46c7
 	   free those old settings and context and create a new one */
ba46c7
+	PR_Lock(ol_bind_lock);
ba46c7
 	rc = setup_ol_tls_conn(ld, 1);
ba46c7
+	PR_Unlock(ol_bind_lock);
ba46c7
 #else
ba46c7
 	/* SSL connections will use the server's security context
ba46c7
 	   and cert for client auth */
ba46c7
@@ -1060,7 +1094,9 @@ slapi_ldap_bind(
ba46c7
     }
ba46c7
 
ba46c7
     if (secure == 2) { /* send start tls */
ba46c7
+	BIND_LOCK;
ba46c7
 	rc = ldap_start_tls_s(ld, NULL /* serverctrls?? */, NULL);
ba46c7
+	BIND_UNLOCK;
ba46c7
 	if (LDAP_SUCCESS != rc) {
ba46c7
 	    slapi_log_error(SLAPI_LOG_FATAL, "slapi_ldap_bind",
ba46c7
 			    "Error: could not send startTLS request: "
ba46c7
@@ -1082,8 +1118,11 @@ slapi_ldap_bind(
ba46c7
 			"attempting %s bind with id [%s] creds [%s]\n",
ba46c7
 			mech ? mech : "SIMPLE",
ba46c7
 			bindid, creds);
ba46c7
-	if ((rc = ldap_sasl_bind(ld, bindid, mech, &bvcreds, serverctrls,
ba46c7
-	                         NULL /* clientctrls */, &mymsgid))) {
ba46c7
+	BIND_LOCK;
ba46c7
+	rc = ldap_sasl_bind(ld, bindid, mech, &bvcreds, serverctrls,
ba46c7
+	                    NULL /* clientctrls */, &mymsgid);
ba46c7
+	BIND_UNLOCK;
ba46c7
+	if (rc) {
ba46c7
 	    char *myhostname = NULL;
ba46c7
 	    char *copy = NULL;
ba46c7
 	    char *ptr = NULL;
ba46c7
@@ -1139,7 +1178,9 @@ slapi_ldap_bind(
ba46c7
                     /* take the one provided by the caller. It should be the one defined in the protocol */
ba46c7
                     bind_timeout = timeout;
ba46c7
             }
ba46c7
+	    BIND_LOCK;
ba46c7
 	    rc = ldap_result(ld, mymsgid, LDAP_MSG_ALL, bind_timeout, &result);
ba46c7
+	    BIND_UNLOCK;
ba46c7
 	    if (-1 == rc) { /* error */
ba46c7
 		rc = slapi_ldap_get_lderrno(ld, NULL, NULL);
ba46c7
 		slapi_log_error(SLAPI_LOG_FATAL, "slapi_ldap_bind",
ba46c7
@@ -1203,9 +1244,11 @@ slapi_ldap_bind(
ba46c7
 	    ldap_set_option(ld, LDAP_OPT_X_SASL_SSF_MAX, &max_ssf);
ba46c7
 	}
ba46c7
 #endif
ba46c7
+	BIND_LOCK;
ba46c7
 	rc = slapd_ldap_sasl_interactive_bind(ld, bindid, creds, mech,
ba46c7
 					      serverctrls, returnedctrls,
ba46c7
 					      msgidp);
ba46c7
+	BIND_UNLOCK;
ba46c7
 	if (LDAP_SUCCESS != rc) {
ba46c7
 	    slapi_log_error(SLAPI_LOG_FATAL, "slapi_ldap_bind",
ba46c7
 			    "Error: could not perform interactive bind for id "
ba46c7
-- 
ba46c7
1.7.1
ba46c7