Blame SOURCES/cyrus-sasl-2.1.26-gssapi-use-per-connection-mutex.patch

bfd35c
From 70a144cc53d09b56aa088fa1f6d433acea31afa7 Mon Sep 17 00:00:00 2001
bfd35c
From: Alexander Bokovoy <abokovoy@redhat.com>
bfd35c
Date: Tue, 15 Sep 2015 12:21:22 +0300
bfd35c
Subject: [PATCH] gssapi: use per-connection mutex where possible
bfd35c
bfd35c
If the same application uses SASL GSSAPI for both client and server operations,
bfd35c
it may be possible to deadlock in plugins/gssapi.c due to use of a
bfd35c
global mutex by both client and server code. Multiple outstanding connections should
bfd35c
be possible, thus introduce per-context locking and use it where it
bfd35c
makes sense. Note that there are still multiple places where context is
bfd35c
not available and where a global lock should be in use.
bfd35c
bfd35c
Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1263017
bfd35c
---
bfd35c
 plugins/gssapi.c | 225 +++++++++++++++++++++++++++++++------------------------
bfd35c
 1 file changed, 126 insertions(+), 99 deletions(-)
bfd35c
bfd35c
diff --git a/plugins/gssapi.c b/plugins/gssapi.c
bfd35c
index 2fd1b3b..f5d3354 100644
bfd35c
--- a/plugins/gssapi.c
bfd35c
+++ b/plugins/gssapi.c
bfd35c
@@ -126,20 +126,29 @@ extern gss_OID gss_nt_service_name;
bfd35c
  */
bfd35c
 
bfd35c
 #ifdef GSS_USE_MUTEXES
bfd35c
-#define GSS_LOCK_MUTEX(utils)  \
bfd35c
-    if(((sasl_utils_t *)(utils))->mutex_lock(gss_mutex) != 0) { \
bfd35c
+#define GSS_LOCK_MUTEX_EXT(utils, mutex)  \
bfd35c
+    if(((sasl_utils_t *)(utils))->mutex_lock(mutex) != 0) { \
bfd35c
        return SASL_FAIL; \
bfd35c
     }
bfd35c
 
bfd35c
-#define GSS_UNLOCK_MUTEX(utils) \
bfd35c
-    if(((sasl_utils_t *)(utils))->mutex_unlock(gss_mutex) != 0) { \
bfd35c
+#define GSS_UNLOCK_MUTEX_EXT(utils, mutex) \
bfd35c
+    if(((sasl_utils_t *)(utils))->mutex_unlock(mutex) != 0) { \
bfd35c
         return SASL_FAIL; \
bfd35c
     }
bfd35c
 
bfd35c
+#define GSS_LOCK_MUTEX(utils) GSS_LOCK_MUTEX_EXT(utils, gss_mutex)
bfd35c
+#define GSS_UNLOCK_MUTEX(utils) GSS_UNLOCK_MUTEX_EXT(utils, gss_mutex)
bfd35c
+
bfd35c
+#define GSS_LOCK_MUTEX_CTX(utils, ctx) GSS_LOCK_MUTEX_EXT(utils, (ctx)->ctx_mutex)
bfd35c
+#define GSS_UNLOCK_MUTEX_CTX(utils, ctx) GSS_UNLOCK_MUTEX_EXT(utils, (ctx)->ctx_mutex)
bfd35c
+
bfd35c
+
bfd35c
 static void *gss_mutex = NULL;
bfd35c
 #else
bfd35c
 #define GSS_LOCK_MUTEX(utils)
bfd35c
 #define GSS_UNLOCK_MUTEX(utils)
bfd35c
+#define GSS_LOCK_MUTEX_CTX(utils, ctx)
bfd35c
+#define GSS_UNLOCK_MUTEX_CTX(utils, ctx)
bfd35c
 #endif
bfd35c
 
bfd35c
 typedef struct context {
bfd35c
@@ -176,6 +185,7 @@ typedef struct context {
bfd35c
     
bfd35c
     char *authid; /* hold the authid between steps - server */
bfd35c
     const char *user;   /* hold the userid between steps - client */
bfd35c
+    void *ctx_mutex; /* A per-context mutex */
bfd35c
 } context_t;
bfd35c
 
bfd35c
 enum {
bfd35c
@@ -355,7 +365,7 @@ sasl_gss_encode(void *context, const struct iovec *invec, unsigned numiov,
bfd35c
     output_token->value = NULL;
bfd35c
     output_token->length = 0;
bfd35c
     
bfd35c
-    GSS_LOCK_MUTEX(text->utils);
bfd35c
+    GSS_LOCK_MUTEX_CTX(text->utils, text);
bfd35c
     maj_stat = gss_wrap (&min_stat,
bfd35c
 			 text->gss_ctx,
bfd35c
 			 privacy,
bfd35c
@@ -363,14 +373,14 @@ sasl_gss_encode(void *context, const struct iovec *invec, unsigned numiov,
bfd35c
 			 input_token,
bfd35c
 			 NULL,
bfd35c
 			 output_token);
bfd35c
-    GSS_UNLOCK_MUTEX(text->utils);
bfd35c
+    GSS_UNLOCK_MUTEX_CTX(text->utils, text);
bfd35c
     
bfd35c
     if (GSS_ERROR(maj_stat)) {
bfd35c
 	sasl_gss_seterror(text->utils, maj_stat, min_stat);
bfd35c
 	if (output_token->value) {
bfd35c
-	    GSS_LOCK_MUTEX(text->utils);
bfd35c
+	    GSS_LOCK_MUTEX_CTX(text->utils, text);
bfd35c
 	    gss_release_buffer(&min_stat, output_token);
bfd35c
-	    GSS_UNLOCK_MUTEX(text->utils);
bfd35c
+	    GSS_UNLOCK_MUTEX_CTX(text->utils, text);
bfd35c
 	}
bfd35c
 	return SASL_FAIL;
bfd35c
     }
bfd35c
@@ -384,9 +394,9 @@ sasl_gss_encode(void *context, const struct iovec *invec, unsigned numiov,
bfd35c
 			      output_token->length + 4);
bfd35c
 	
bfd35c
 	if (ret != SASL_OK) {
bfd35c
-	    GSS_LOCK_MUTEX(text->utils);
bfd35c
+	    GSS_LOCK_MUTEX_CTX(text->utils, text);
bfd35c
 	    gss_release_buffer(&min_stat, output_token);
bfd35c
-	    GSS_UNLOCK_MUTEX(text->utils);
bfd35c
+	    GSS_UNLOCK_MUTEX_CTX(text->utils, text);
bfd35c
 	    return ret;
bfd35c
 	}
bfd35c
 
bfd35c
@@ -407,9 +417,9 @@ sasl_gss_encode(void *context, const struct iovec *invec, unsigned numiov,
bfd35c
     *output = text->encode_buf;
bfd35c
     
bfd35c
     if (output_token->value) {
bfd35c
-	GSS_LOCK_MUTEX(text->utils);
bfd35c
+	GSS_LOCK_MUTEX_CTX(text->utils, text);
bfd35c
 	gss_release_buffer(&min_stat, output_token);
bfd35c
-	GSS_UNLOCK_MUTEX(text->utils);
bfd35c
+	GSS_UNLOCK_MUTEX_CTX(text->utils, text);
bfd35c
     }
bfd35c
 
bfd35c
     return SASL_OK;
bfd35c
@@ -455,21 +465,21 @@ gssapi_decode_packet(void *context,
bfd35c
     output_token->value = NULL;
bfd35c
     output_token->length = 0;
bfd35c
     
bfd35c
-    GSS_LOCK_MUTEX(text->utils);
bfd35c
+    GSS_LOCK_MUTEX_CTX(text->utils, text);
bfd35c
     maj_stat = gss_unwrap (&min_stat,
bfd35c
 			   text->gss_ctx,
bfd35c
 			   input_token,
bfd35c
 			   output_token,
bfd35c
 			   NULL,
bfd35c
 			   NULL);
bfd35c
-    GSS_UNLOCK_MUTEX(text->utils);
bfd35c
+    GSS_UNLOCK_MUTEX_CTX(text->utils, text);
bfd35c
     
bfd35c
     if (GSS_ERROR(maj_stat)) {
bfd35c
 	sasl_gss_seterror(text->utils,maj_stat,min_stat);
bfd35c
 	if (output_token->value) {
bfd35c
-	    GSS_LOCK_MUTEX(text->utils);
bfd35c
+	    GSS_LOCK_MUTEX_CTX(text->utils, text);
bfd35c
 	    gss_release_buffer(&min_stat, output_token);
bfd35c
-	    GSS_UNLOCK_MUTEX(text->utils);
bfd35c
+	    GSS_UNLOCK_MUTEX_CTX(text->utils, text);
bfd35c
 	}
bfd35c
 	return SASL_FAIL;
bfd35c
     }
bfd35c
@@ -484,17 +494,17 @@ gssapi_decode_packet(void *context,
bfd35c
 				     &text->decode_once_buf_len,
bfd35c
 				     *outputlen);
bfd35c
 	    if (result != SASL_OK) {
bfd35c
-		GSS_LOCK_MUTEX(text->utils);
bfd35c
+		GSS_LOCK_MUTEX_CTX(text->utils, text);
bfd35c
 		gss_release_buffer(&min_stat, output_token);
bfd35c
-		GSS_UNLOCK_MUTEX(text->utils);
bfd35c
+		GSS_UNLOCK_MUTEX_CTX(text->utils, text);
bfd35c
 		return result;
bfd35c
 	    }
bfd35c
 	    *output = text->decode_once_buf;
bfd35c
 	    memcpy(*output, output_token->value, *outputlen);
bfd35c
 	}
bfd35c
-	GSS_LOCK_MUTEX(text->utils);
bfd35c
+	GSS_LOCK_MUTEX_CTX(text->utils, text);
bfd35c
 	gss_release_buffer(&min_stat, output_token);
bfd35c
-	GSS_UNLOCK_MUTEX(text->utils);
bfd35c
+	GSS_UNLOCK_MUTEX_CTX(text->utils, text);
bfd35c
     }
bfd35c
     
bfd35c
     return SASL_OK;
bfd35c
@@ -525,7 +535,14 @@ static context_t *sasl_gss_new_context(const sasl_utils_t *utils)
bfd35c
     
bfd35c
     memset(ret,0,sizeof(context_t));
bfd35c
     ret->utils = utils;
bfd35c
-    
bfd35c
+#ifdef GSS_USE_MUTEXES
bfd35c
+    ret->ctx_mutex = utils->mutex_alloc();
bfd35c
+    if (!ret->ctx_mutex) {
bfd35c
+           utils->free(ret);
bfd35c
+           return NULL;
bfd35c
+    }
bfd35c
+#endif
bfd35c
+
bfd35c
     return ret;
bfd35c
 }
bfd35c
 
bfd35c
@@ -535,7 +552,11 @@ static int sasl_gss_free_context_contents(context_t *text)
bfd35c
     
bfd35c
     if (!text) return SASL_OK;
bfd35c
     
bfd35c
-    GSS_LOCK_MUTEX(text->utils);
bfd35c
+#ifdef GSS_USE_MUTEXES
bfd35c
+    if (text->ctx_mutex) {
bfd35c
+        GSS_LOCK_MUTEX_CTX(text->utils, text);
bfd35c
+    }
bfd35c
+#endif
bfd35c
 
bfd35c
     if (text->gss_ctx != GSS_C_NO_CONTEXT) {
bfd35c
 	maj_stat = gss_delete_sec_context(&min_stat,&text->gss_ctx,
bfd35c
@@ -563,8 +584,6 @@ static int sasl_gss_free_context_contents(context_t *text)
bfd35c
 	text->client_creds = GSS_C_NO_CREDENTIAL;
bfd35c
     }
bfd35c
 
bfd35c
-    GSS_UNLOCK_MUTEX(text->utils);
bfd35c
-    
bfd35c
     if (text->out_buf) {
bfd35c
 	text->utils->free(text->out_buf);
bfd35c
 	text->out_buf = NULL;
bfd35c
@@ -598,6 +617,14 @@ static int sasl_gss_free_context_contents(context_t *text)
bfd35c
 	text->authid = NULL;
bfd35c
     }
bfd35c
 
bfd35c
+#ifdef GSS_USE_MUTEXES
bfd35c
+    if (text->ctx_mutex) {
bfd35c
+        GSS_UNLOCK_MUTEX_CTX(text->utils, text);
bfd35c
+        text->utils->mutex_free(text->ctx_mutex);
bfd35c
+        text->ctx_mutex = NULL;
bfd35c
+    }
bfd35c
+#endif
bfd35c
+
bfd35c
     return SASL_OK;
bfd35c
 
bfd35c
 }
bfd35c
@@ -692,12 +719,12 @@ gssapi_server_mech_authneg(context_t *text,
bfd35c
 	}
bfd35c
 	sprintf(name_token.value,"%s@%s", params->service, params->serverFQDN);
bfd35c
 
bfd35c
-	GSS_LOCK_MUTEX(params->utils);
bfd35c
+	GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	maj_stat = gss_import_name (&min_stat,
bfd35c
 				    &name_token,
bfd35c
 				    GSS_C_NT_HOSTBASED_SERVICE,
bfd35c
 				    &text->server_name);
bfd35c
-	GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 
bfd35c
 	params->utils->free(name_token.value);
bfd35c
 	name_token.value = NULL;
bfd35c
@@ -709,15 +736,15 @@ gssapi_server_mech_authneg(context_t *text,
bfd35c
 	}
bfd35c
 
bfd35c
 	if ( text->server_creds != GSS_C_NO_CREDENTIAL) {
bfd35c
-	    GSS_LOCK_MUTEX(params->utils);
bfd35c
+	    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    maj_stat = gss_release_cred(&min_stat, &text->server_creds);
bfd35c
-	    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    text->server_creds = GSS_C_NO_CREDENTIAL;
bfd35c
 	}
bfd35c
 
bfd35c
 	/* If caller didn't provide creds already */
bfd35c
 	if ( server_creds == GSS_C_NO_CREDENTIAL) {
bfd35c
-	    GSS_LOCK_MUTEX(params->utils);
bfd35c
+	    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    maj_stat = gss_acquire_cred(&min_stat, 
bfd35c
 					text->server_name,
bfd35c
 					GSS_C_INDEFINITE, 
bfd35c
@@ -726,7 +753,7 @@ gssapi_server_mech_authneg(context_t *text,
bfd35c
 					&text->server_creds, 
bfd35c
 					NULL, 
bfd35c
 					NULL);
bfd35c
-	    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 
bfd35c
 	    if (GSS_ERROR(maj_stat)) {
bfd35c
 		sasl_gss_seterror(text->utils, maj_stat, min_stat);
bfd35c
@@ -743,7 +770,7 @@ gssapi_server_mech_authneg(context_t *text,
bfd35c
     }
bfd35c
 
bfd35c
 
bfd35c
-    GSS_LOCK_MUTEX(params->utils);
bfd35c
+    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
     maj_stat =
bfd35c
 	gss_accept_sec_context(&min_stat,
bfd35c
 			       &(text->gss_ctx),
bfd35c
@@ -756,15 +783,15 @@ gssapi_server_mech_authneg(context_t *text,
bfd35c
 			       &out_flags,
bfd35c
 			       NULL,	/* context validity period */
bfd35c
 			       &(text->client_creds));
bfd35c
-    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 
bfd35c
     if (GSS_ERROR(maj_stat)) {
bfd35c
 	sasl_gss_log(text->utils, maj_stat, min_stat);
bfd35c
 	text->utils->seterror(text->utils->conn, SASL_NOLOG, "GSSAPI Failure: gss_accept_sec_context");
bfd35c
 	if (output_token->value) {
bfd35c
-	    GSS_LOCK_MUTEX(params->utils);
bfd35c
+	    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    gss_release_buffer(&min_stat, output_token);
bfd35c
-	    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	}
bfd35c
 	sasl_gss_free_context_contents(text);
bfd35c
 	return SASL_BADAUTH;
bfd35c
@@ -778,18 +805,18 @@ gssapi_server_mech_authneg(context_t *text,
bfd35c
 	    ret = _plug_buf_alloc(text->utils, &(text->out_buf),
bfd35c
 				  &(text->out_buf_len), *serveroutlen);
bfd35c
 	    if(ret != SASL_OK) {
bfd35c
-		GSS_LOCK_MUTEX(params->utils);
bfd35c
+		GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 		gss_release_buffer(&min_stat, output_token);
bfd35c
-		GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+		GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 		return ret;
bfd35c
 	    }
bfd35c
 	    memcpy(text->out_buf, output_token->value, *serveroutlen);
bfd35c
 	    *serverout = text->out_buf;
bfd35c
 	}
bfd35c
 
bfd35c
-	GSS_LOCK_MUTEX(params->utils);
bfd35c
+	GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	gss_release_buffer(&min_stat, output_token);
bfd35c
-	GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
     } else {
bfd35c
 	/* No output token, send an empty string */
bfd35c
 	*serverout = GSSAPI_BLANK_STRING;
bfd35c
@@ -832,12 +859,12 @@ gssapi_server_mech_authneg(context_t *text,
bfd35c
 	    /* continue with authentication */
bfd35c
 	}
bfd35c
 
bfd35c
-    GSS_LOCK_MUTEX(params->utils);
bfd35c
+    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
     maj_stat = gss_canonicalize_name(&min_stat,
bfd35c
 				     text->client_name,
bfd35c
 				     mech_type,
bfd35c
 				     &client_name_MN);
bfd35c
-    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 
bfd35c
     if (GSS_ERROR(maj_stat)) {
bfd35c
 	SETERROR(text->utils, "GSSAPI Failure: gss_canonicalize_name");
bfd35c
@@ -848,12 +875,12 @@ gssapi_server_mech_authneg(context_t *text,
bfd35c
     name_token.value = NULL;
bfd35c
     name_without_realm.value = NULL;
bfd35c
 
bfd35c
-    GSS_LOCK_MUTEX(params->utils);
bfd35c
+    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
     maj_stat = gss_display_name (&min_stat,
bfd35c
 				 client_name_MN,
bfd35c
 				 &name_token,
bfd35c
 				 NULL);
bfd35c
-    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 
bfd35c
     if (GSS_ERROR(maj_stat)) {
bfd35c
 	SETERROR(text->utils, "GSSAPI Failure: gss_display_name");
bfd35c
@@ -883,7 +910,7 @@ gssapi_server_mech_authneg(context_t *text,
bfd35c
 
bfd35c
 	name_without_realm.length = strlen( (char *) name_without_realm.value );
bfd35c
 
bfd35c
-	GSS_LOCK_MUTEX(params->utils);
bfd35c
+	GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	maj_stat = gss_import_name (&min_stat,
bfd35c
 				    &name_without_realm,
bfd35c
 	    /* Solaris 8/9 gss_import_name doesn't accept GSS_C_NULL_OID here,
bfd35c
@@ -894,7 +921,7 @@ gssapi_server_mech_authneg(context_t *text,
bfd35c
 				    GSS_C_NULL_OID,
bfd35c
 #endif
bfd35c
 				    &without);
bfd35c
-	GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 
bfd35c
 	if (GSS_ERROR(maj_stat)) {
bfd35c
 	    SETERROR(text->utils, "GSSAPI Failure: gss_import_name");
bfd35c
@@ -903,12 +930,12 @@ gssapi_server_mech_authneg(context_t *text,
bfd35c
 	    goto cleanup;
bfd35c
 	}
bfd35c
 
bfd35c
-	GSS_LOCK_MUTEX(params->utils);
bfd35c
+	GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	maj_stat = gss_compare_name(&min_stat,
bfd35c
 				    client_name_MN,
bfd35c
 				    without,
bfd35c
 				    &equal);
bfd35c
-	GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 
bfd35c
 	if (GSS_ERROR(maj_stat)) {
bfd35c
 	    SETERROR(text->utils, "GSSAPI Failure: gss_compare_name");
bfd35c
@@ -1053,7 +1080,7 @@ gssapi_server_mech_ssfcap(context_t *text,
bfd35c
     real_input_token.value = (void *)sasldata;
bfd35c
     real_input_token.length = 4;
bfd35c
 
bfd35c
-    GSS_LOCK_MUTEX(params->utils);
bfd35c
+    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
     maj_stat = gss_wrap(&min_stat,
bfd35c
 			text->gss_ctx,
bfd35c
 			0, /* Just integrity checking here */
bfd35c
@@ -1061,14 +1088,14 @@ gssapi_server_mech_ssfcap(context_t *text,
bfd35c
 			input_token,
bfd35c
 			NULL,
bfd35c
 			output_token);
bfd35c
-    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 
bfd35c
     if (GSS_ERROR(maj_stat)) {
bfd35c
 	sasl_gss_seterror(text->utils, maj_stat, min_stat);
bfd35c
 	if (output_token->value) {
bfd35c
-	    GSS_LOCK_MUTEX(params->utils);
bfd35c
+	    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    gss_release_buffer(&min_stat, output_token);
bfd35c
-	    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	}
bfd35c
 	sasl_gss_free_context_contents(text);
bfd35c
 	return SASL_FAIL;
bfd35c
@@ -1082,18 +1109,18 @@ gssapi_server_mech_ssfcap(context_t *text,
bfd35c
 	    ret = _plug_buf_alloc(text->utils, &(text->out_buf),
bfd35c
 				  &(text->out_buf_len), *serveroutlen);
bfd35c
 	    if(ret != SASL_OK) {
bfd35c
-		GSS_LOCK_MUTEX(params->utils);
bfd35c
+		GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 		gss_release_buffer(&min_stat, output_token);
bfd35c
-		GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+		GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 		return ret;
bfd35c
 	    }
bfd35c
 	    memcpy(text->out_buf, output_token->value, *serveroutlen);
bfd35c
 	    *serverout = text->out_buf;
bfd35c
 	}
bfd35c
 
bfd35c
-	GSS_LOCK_MUTEX(params->utils);
bfd35c
+	GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	gss_release_buffer(&min_stat, output_token);
bfd35c
-	GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
     }
bfd35c
 
bfd35c
     /* Wait for ssf request and authid */
bfd35c
@@ -1124,14 +1151,14 @@ gssapi_server_mech_ssfreq(context_t *text,
bfd35c
     real_input_token.value = (void *)clientin;
bfd35c
     real_input_token.length = clientinlen;
bfd35c
 
bfd35c
-    GSS_LOCK_MUTEX(params->utils);
bfd35c
+    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
     maj_stat = gss_unwrap(&min_stat,
bfd35c
 			  text->gss_ctx,
bfd35c
 			  input_token,
bfd35c
 			  output_token,
bfd35c
 			  NULL,
bfd35c
 			  NULL);
bfd35c
-    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 
bfd35c
     if (GSS_ERROR(maj_stat)) {
bfd35c
 	sasl_gss_seterror(text->utils, maj_stat, min_stat);
bfd35c
@@ -1142,9 +1169,9 @@ gssapi_server_mech_ssfreq(context_t *text,
bfd35c
     if (output_token->length < 4) {
bfd35c
 	SETERROR(text->utils,
bfd35c
 		 "token too short");
bfd35c
-	GSS_LOCK_MUTEX(params->utils);
bfd35c
+	GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	gss_release_buffer(&min_stat, output_token);
bfd35c
-	GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	sasl_gss_free_context_contents(text);
bfd35c
 	return SASL_FAIL;
bfd35c
     }
bfd35c
@@ -1175,9 +1202,9 @@ gssapi_server_mech_ssfreq(context_t *text,
bfd35c
 	/* Mark that we attempted negotiation */
bfd35c
 	oparams->mech_ssf = 2;
bfd35c
 	if (output_token->value) {
bfd35c
-	    GSS_LOCK_MUTEX(params->utils);
bfd35c
+	    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    gss_release_buffer(&min_stat, output_token);
bfd35c
-	    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	}
bfd35c
 	sasl_gss_free_context_contents(text);
bfd35c
 	return SASL_FAIL;
bfd35c
@@ -1221,9 +1248,9 @@ gssapi_server_mech_ssfreq(context_t *text,
bfd35c
 	}    
bfd35c
     }
bfd35c
 	
bfd35c
-    GSS_LOCK_MUTEX(params->utils);
bfd35c
+    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
     gss_release_buffer(&min_stat, output_token);
bfd35c
-    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 
bfd35c
     text->state = SASL_GSSAPI_STATE_AUTHENTICATED;
bfd35c
 
bfd35c
@@ -1547,12 +1574,12 @@ static int gssapi_client_mech_step(void *conn_context,
bfd35c
 	    
bfd35c
 	    sprintf(name_token.value,"%s@%s", params->service, params->serverFQDN);
bfd35c
 	    
bfd35c
-	    GSS_LOCK_MUTEX(params->utils);
bfd35c
+	    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    maj_stat = gss_import_name (&min_stat,
bfd35c
 					&name_token,
bfd35c
 					GSS_C_NT_HOSTBASED_SERVICE,
bfd35c
 					&text->server_name);
bfd35c
-	    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    
bfd35c
 	    params->utils->free(name_token.value);
bfd35c
 	    name_token.value = NULL;
bfd35c
@@ -1576,9 +1603,9 @@ static int gssapi_client_mech_step(void *conn_context,
bfd35c
 	     * and no input from the server.  However, thanks to Imap,
bfd35c
 	     * which discards our first output, this happens all the time.
bfd35c
 	     * Throw away the context and try again. */
bfd35c
-	    GSS_LOCK_MUTEX(params->utils);
bfd35c
+	    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    maj_stat = gss_delete_sec_context (&min_stat,&text->gss_ctx,GSS_C_NO_BUFFER);
bfd35c
-	    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    text->gss_ctx = GSS_C_NO_CONTEXT;
bfd35c
 	}
bfd35c
 
bfd35c
@@ -1600,7 +1627,7 @@ static int gssapi_client_mech_step(void *conn_context,
bfd35c
 	    req_flags = req_flags |  GSS_C_DELEG_FLAG;
bfd35c
 	}
bfd35c
 
bfd35c
-	GSS_LOCK_MUTEX(params->utils);
bfd35c
+	GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	maj_stat = gss_init_sec_context(&min_stat,
bfd35c
 					client_creds, /* GSS_C_NO_CREDENTIAL */
bfd35c
 					&text->gss_ctx,
bfd35c
@@ -1614,14 +1641,14 @@ static int gssapi_client_mech_step(void *conn_context,
bfd35c
 					output_token,
bfd35c
 					&out_req_flags,
bfd35c
 					NULL);
bfd35c
-	GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	
bfd35c
 	if (GSS_ERROR(maj_stat)) {
bfd35c
 	    sasl_gss_seterror(text->utils, maj_stat, min_stat);
bfd35c
 	    if (output_token->value) {
bfd35c
-		GSS_LOCK_MUTEX(params->utils);
bfd35c
+		GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 		gss_release_buffer(&min_stat, output_token);
bfd35c
-		GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+		GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    }
bfd35c
 	    sasl_gss_free_context_contents(text);
bfd35c
 	    return SASL_FAIL;
bfd35c
@@ -1652,22 +1679,22 @@ static int gssapi_client_mech_step(void *conn_context,
bfd35c
 		ret = _plug_buf_alloc(text->utils, &(text->out_buf),
bfd35c
 				      &(text->out_buf_len), *clientoutlen);
bfd35c
 		if(ret != SASL_OK) {
bfd35c
-		    GSS_LOCK_MUTEX(params->utils);
bfd35c
+		    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 		    gss_release_buffer(&min_stat, output_token);
bfd35c
-		    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+		    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 		    return ret;
bfd35c
 		}
bfd35c
 		memcpy(text->out_buf, output_token->value, *clientoutlen);
bfd35c
 		*clientout = text->out_buf;
bfd35c
 	    }
bfd35c
 	    
bfd35c
-	    GSS_LOCK_MUTEX(params->utils);
bfd35c
+	    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    gss_release_buffer(&min_stat, output_token);
bfd35c
-	    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	}
bfd35c
 	
bfd35c
 	if (maj_stat == GSS_S_COMPLETE) {
bfd35c
-	    GSS_LOCK_MUTEX(params->utils);
bfd35c
+	    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    maj_stat = gss_inquire_context(&min_stat,
bfd35c
 					   text->gss_ctx,
bfd35c
 					   &text->client_name,
bfd35c
@@ -1678,7 +1705,7 @@ static int gssapi_client_mech_step(void *conn_context,
bfd35c
 					   NULL,       /* flags */
bfd35c
 					   NULL,       /* local init */
bfd35c
 					   NULL);      /* open */
bfd35c
-	    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    
bfd35c
 	    if (GSS_ERROR(maj_stat)) {
bfd35c
 		sasl_gss_seterror(text->utils, maj_stat, min_stat);
bfd35c
@@ -1687,18 +1714,18 @@ static int gssapi_client_mech_step(void *conn_context,
bfd35c
 	    }
bfd35c
 	    
bfd35c
 	    name_token.length = 0;
bfd35c
-	    GSS_LOCK_MUTEX(params->utils);
bfd35c
+	    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    maj_stat = gss_display_name(&min_stat,
bfd35c
 					text->client_name,
bfd35c
 					&name_token,
bfd35c
 					NULL);
bfd35c
-	    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    
bfd35c
 	    if (GSS_ERROR(maj_stat)) {
bfd35c
 		if (name_token.value) {
bfd35c
-		    GSS_LOCK_MUTEX(params->utils);
bfd35c
+		    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 		    gss_release_buffer(&min_stat, &name_token);
bfd35c
-		    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+		    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 		}
bfd35c
 		SETERROR(text->utils, "GSSAPI Failure");
bfd35c
 		sasl_gss_free_context_contents(text);
bfd35c
@@ -1719,9 +1746,9 @@ static int gssapi_client_mech_step(void *conn_context,
bfd35c
 					 SASL_CU_AUTHID | SASL_CU_AUTHZID,
bfd35c
 					 oparams);
bfd35c
 	    }
bfd35c
-	    GSS_LOCK_MUTEX(params->utils);
bfd35c
+	    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    gss_release_buffer(&min_stat, &name_token);
bfd35c
-	    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    
bfd35c
 	    if (ret != SASL_OK) return ret;
bfd35c
 	    
bfd35c
@@ -1747,32 +1774,32 @@ static int gssapi_client_mech_step(void *conn_context,
bfd35c
 	real_input_token.value = (void *) serverin;
bfd35c
 	real_input_token.length = serverinlen;
bfd35c
 	
bfd35c
-	GSS_LOCK_MUTEX(params->utils);
bfd35c
+	GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	maj_stat = gss_unwrap(&min_stat,
bfd35c
 			      text->gss_ctx,
bfd35c
 			      input_token,
bfd35c
 			      output_token,
bfd35c
 			      NULL,
bfd35c
 			      NULL);
bfd35c
-	GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	
bfd35c
 	if (GSS_ERROR(maj_stat)) {
bfd35c
 	    sasl_gss_seterror(text->utils, maj_stat, min_stat);
bfd35c
-	    sasl_gss_free_context_contents(text);
bfd35c
 	    if (output_token->value) {
bfd35c
-		GSS_LOCK_MUTEX(params->utils);
bfd35c
+		GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 		gss_release_buffer(&min_stat, output_token);
bfd35c
-		GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+		GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    }
bfd35c
+	    sasl_gss_free_context_contents(text);
bfd35c
 	    return SASL_FAIL;
bfd35c
 	}
bfd35c
 	
bfd35c
 	if (output_token->length != 4) {
bfd35c
 	    SETERROR(text->utils,
bfd35c
 		     (output_token->length < 4) ? "token too short" : "token too long");
bfd35c
-	    GSS_LOCK_MUTEX(params->utils);
bfd35c
+	    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    gss_release_buffer(&min_stat, output_token);
bfd35c
-	    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    sasl_gss_free_context_contents(text);
bfd35c
 	    return SASL_FAIL;
bfd35c
 	}
bfd35c
@@ -1873,9 +1900,9 @@ static int gssapi_client_mech_step(void *conn_context,
bfd35c
 	    }
bfd35c
 	}
bfd35c
 	
bfd35c
-	GSS_LOCK_MUTEX(params->utils);
bfd35c
+	GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	gss_release_buffer(&min_stat, output_token);
bfd35c
-	GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	
bfd35c
 	/* oparams->user is always set, due to canon_user requirements.
bfd35c
 	 * Make sure the client actually requested it though, by checking
bfd35c
@@ -1921,7 +1948,7 @@ static int gssapi_client_mech_step(void *conn_context,
bfd35c
 	}
bfd35c
 	((unsigned char *)input_token->value)[0] = mychoice;
bfd35c
 	
bfd35c
-	GSS_LOCK_MUTEX(params->utils);
bfd35c
+	GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	maj_stat = gss_wrap (&min_stat,
bfd35c
 			     text->gss_ctx,
bfd35c
 			     0, /* Just integrity checking here */
bfd35c
@@ -1929,7 +1956,7 @@ static int gssapi_client_mech_step(void *conn_context,
bfd35c
 			     input_token,
bfd35c
 			     NULL,
bfd35c
 			     output_token);
bfd35c
-	GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	
bfd35c
 	params->utils->free(input_token->value);
bfd35c
 	input_token->value = NULL;
bfd35c
@@ -1937,9 +1964,9 @@ static int gssapi_client_mech_step(void *conn_context,
bfd35c
 	if (GSS_ERROR(maj_stat)) {
bfd35c
 	    sasl_gss_seterror(text->utils, maj_stat, min_stat);
bfd35c
 	    if (output_token->value) {
bfd35c
-		GSS_LOCK_MUTEX(params->utils);
bfd35c
+		GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 		gss_release_buffer(&min_stat, output_token);
bfd35c
-		GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+		GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    }
bfd35c
 	    sasl_gss_free_context_contents(text);
bfd35c
 	    return SASL_FAIL;
bfd35c
@@ -1955,18 +1982,18 @@ static int gssapi_client_mech_step(void *conn_context,
bfd35c
 				      &(text->out_buf_len),
bfd35c
 				      *clientoutlen);
bfd35c
 		if (ret != SASL_OK) {
bfd35c
-		    GSS_LOCK_MUTEX(params->utils);
bfd35c
+		    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 		    gss_release_buffer(&min_stat, output_token);
bfd35c
-		    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+		    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 		    return ret;
bfd35c
 		}
bfd35c
 		memcpy(text->out_buf, output_token->value, *clientoutlen);
bfd35c
 		*clientout = text->out_buf;
bfd35c
 	    }
bfd35c
 	    
bfd35c
-	    GSS_LOCK_MUTEX(params->utils);
bfd35c
+	    GSS_LOCK_MUTEX_CTX(params->utils, text);
bfd35c
 	    gss_release_buffer(&min_stat, output_token);
bfd35c
-	    GSS_UNLOCK_MUTEX(params->utils);
bfd35c
+	    GSS_UNLOCK_MUTEX_CTX(params->utils, text);
bfd35c
 
bfd35c
 	}
bfd35c
 	
bfd35c
-- 
bfd35c
2.4.3
bfd35c
bfd35c