[evolution-patches] Address book - ldap backend: mutex lock for ldap handler



Hi,

Here is the patch for locking the LDAP handler while reading/writing.
This is needed, as poll_ldap calls reconnect call, which refreshes
the handler.

Please review.
Thanks,
Sushma.
Index: addressbook/backends/ldap/e-book-backend-ldap.c
===================================================================
RCS file: /cvs/gnome/evolution-data-server/addressbook/backends/ldap/e-book-backend-ldap.c,v
retrieving revision 1.49
diff -u -p -r1.49 e-book-backend-ldap.c
--- addressbook/backends/ldap/e-book-backend-ldap.c	31 Aug 2005 04:21:50 -0000	1.49
+++ addressbook/backends/ldap/e-book-backend-ldap.c	1 Sep 2005 13:07:19 -0000
@@ -165,6 +165,8 @@ struct LDAPOp {
 	int            id;   /* the ldap msg id */
 };
 
+static GStaticMutex global_ldap_lock = G_STATIC_MUTEX_INIT;
+
 static void     ldap_op_add (LDAPOp *op, EBookBackend *backend, EDataBook *book,
 			     EDataBookView *view, int opid, int msgid, LDAPOpHandler handler, LDAPOpDtor dtor);
 static void     ldap_op_finished (LDAPOp *op);
@@ -477,11 +479,16 @@ check_schema_support (EBookBackendLDAP *
 {
 	char *attrs[2];
 	LDAPMessage *resp;
-	LDAP *ldap = bl->priv->ldap;
+	LDAP *ldap;
 	struct timeval timeout;
 
-	if (!ldap)
+	g_static_mutex_lock (&global_ldap_lock);
+	ldap = bl->priv->ldap;
+	if (!ldap) {
+		g_static_mutex_unlock (&global_ldap_lock);
 		return;
+	}
+	g_static_mutex_unlock (&global_ldap_lock);
 
 	if (!bl->priv->schema_dn)
 		return;
@@ -494,12 +501,14 @@ check_schema_support (EBookBackendLDAP *
 	timeout.tv_sec = 30;
 	timeout.tv_usec = 0;
 
+	g_static_mutex_lock (&global_ldap_lock);
 	if (ldap_search_ext_s (ldap, bl->priv->schema_dn, LDAP_SCOPE_BASE,
 			       "(objectClass=subschema)", attrs, 0,
 			       NULL, NULL, &timeout, LDAP_NO_LIMIT, &resp) == LDAP_SUCCESS) {
 		char **values;
 
 		values = ldap_get_values (ldap, resp, "objectClasses");
+		g_static_mutex_unlock (&global_ldap_lock);
 
 		if (values) {
 			int i;
@@ -554,6 +563,9 @@ check_schema_support (EBookBackendLDAP *
 
 		ldap_msgfree (resp);
 	}
+	else {
+		g_static_mutex_unlock (&global_ldap_lock);
+	}
 }
 
 static void
@@ -598,15 +610,20 @@ static int
 query_ldap_root_dse (EBookBackendLDAP *bl)
 {
 #define MAX_DSE_ATTRS 20
-	LDAP *ldap = bl->priv->ldap;
+	LDAP *ldap;
 	LDAPMessage *resp;
 	int ldap_error = LDAP_OTHER;
 	char *attrs[MAX_DSE_ATTRS], **values;
 	int i = 0;
 	struct timeval timeout;
 
-	if (!ldap)
+	g_static_mutex_lock (&global_ldap_lock);
+	ldap = bl->priv->ldap;
+	if (!ldap) {
+		g_static_mutex_unlock (&global_ldap_lock);
 		return ldap_error;
+	}
+	g_static_mutex_unlock (&global_ldap_lock);
 
 	attrs[i++] = "supportedControl";
 	attrs[i++] = "supportedExtension";
@@ -620,23 +637,29 @@ query_ldap_root_dse (EBookBackendLDAP *b
 	timeout.tv_sec = 30;
 	timeout.tv_usec = 0;
 
+	g_static_mutex_lock (&global_ldap_lock);
 	ldap_error = ldap_search_ext_s (ldap,
 					LDAP_ROOT_DSE, LDAP_SCOPE_BASE,
 					"(objectclass=*)",
 					attrs, 0, NULL, NULL, &timeout, LDAP_NO_LIMIT, &resp);
+	g_static_mutex_unlock (&global_ldap_lock);
 	if (ldap_error != LDAP_SUCCESS) {
 		g_warning ("could not perform query on Root DSE (ldap_error 0x%02x)", ldap_error);
 		return ldap_error;
 	}
 
+	g_static_mutex_lock (&global_ldap_lock);
 	values = ldap_get_values (ldap, resp, "supportedControl");
+	g_static_mutex_unlock (&global_ldap_lock);
 	if (values) {
 		for (i = 0; values[i]; i++)
 			g_message ("supported server control: %s", values[i]);
 		ldap_value_free (values);
 	}
 
+	g_static_mutex_lock (&global_ldap_lock);
 	values = ldap_get_values (ldap, resp, "supportedExtension");
+	g_static_mutex_unlock (&global_ldap_lock);
 	if (values) {
 		for (i = 0; values[i]; i++) {
 			g_message ("supported server extension: %s", values[i]);
@@ -647,7 +670,9 @@ query_ldap_root_dse (EBookBackendLDAP *b
 		ldap_value_free (values);
 	}
 
+	g_static_mutex_lock (&global_ldap_lock);
 	values = ldap_get_values (ldap, resp, "supportedSASLMechanisms");
+	g_static_mutex_unlock (&global_ldap_lock);
 	if (values) {
 		char *auth_method;
 		if (bl->priv->supported_auth_methods) {
@@ -670,11 +695,14 @@ query_ldap_root_dse (EBookBackendLDAP *b
 		ldap_value_free (values);
 	}
 
-
+	g_static_mutex_lock (&global_ldap_lock);
 	values = ldap_get_values (ldap, resp, "subschemaSubentry");
+	g_static_mutex_unlock (&global_ldap_lock);
 	if (!values || !values[0]) {
 		if (values) ldap_value_free (values);
+		g_static_mutex_lock (&global_ldap_lock);
 		values = ldap_get_values (ldap, resp, "schemaNamingContext");
+		g_static_mutex_unlock (&global_ldap_lock);
 	}
 	if (values && values[0]) {
 		g_free (bl->priv->schema_dn);
@@ -698,21 +726,27 @@ e_book_backend_ldap_connect (EBookBacken
 	int protocol_version = LDAP_VERSION3;
 
 	/* close connection first if it's open first */
-	if (blpriv->ldap)
+	g_static_mutex_lock (&global_ldap_lock);
+	if (blpriv->ldap) {
 		ldap_unbind_ext_s (blpriv->ldap, NULL, NULL);
-
+	}
 	blpriv->ldap = ldap_init (blpriv->ldap_host, blpriv->ldap_port);
+	g_static_mutex_unlock (&global_ldap_lock);
+
 #if defined (DEBUG) && defined (LDAP_OPT_DEBUG_LEVEL)
 	{
 		int debug_level = 4;
+		g_static_mutex_lock (&global_ldap_lock);
 		ldap_set_option (blpriv->ldap, LDAP_OPT_DEBUG_LEVEL, &debug_level);
+		g_static_mutex_unlock (&global_ldap_lock);
 	}
 #endif
-
+	g_static_mutex_lock (&global_ldap_lock);
 	if (NULL != blpriv->ldap) {
 		int ldap_error;
 
 		ldap_error = ldap_set_option (blpriv->ldap, LDAP_OPT_PROTOCOL_VERSION, &protocol_version);
+		g_static_mutex_unlock (&global_ldap_lock);
 		if (LDAP_OPT_SUCCESS != ldap_error) {
 			g_warning ("failed to set protocol version to LDAPv3");
 			bl->priv->ldap_v3 = FALSE;
@@ -725,22 +759,28 @@ e_book_backend_ldap_connect (EBookBacken
 
 			if (!bl->priv->ldap_v3 && bl->priv->use_tls == E_BOOK_BACKEND_LDAP_TLS_ALWAYS) {
 				g_message ("TLS not available (fatal version), v3 protocol could not be established (ldap_error 0x%02x)", ldap_error);
+				g_static_mutex_lock (&global_ldap_lock);
 				ldap_unbind_ext_s (blpriv->ldap, NULL, NULL);
 				blpriv->ldap = NULL;
+				g_static_mutex_unlock (&global_ldap_lock);
 				return GNOME_Evolution_Addressbook_TLSNotAvailable;
 			}
 
 			if (bl->priv->ldap_port == LDAPS_PORT && bl->priv->use_tls == E_BOOK_BACKEND_LDAP_TLS_ALWAYS) {
 				tls_level = LDAP_OPT_X_TLS_HARD;
+				g_static_mutex_lock (&global_ldap_lock);
 				ldap_set_option (blpriv->ldap, LDAP_OPT_X_TLS, &tls_level);
+				g_static_mutex_unlock (&global_ldap_lock);
 			}
 			else if (bl->priv->use_tls) {
+				g_static_mutex_lock (&global_ldap_lock);
 				ldap_error = ldap_start_tls_s (blpriv->ldap, NULL, NULL);
 				if (LDAP_SUCCESS != ldap_error) {
 					if (bl->priv->use_tls == E_BOOK_BACKEND_LDAP_TLS_ALWAYS) {
 						g_message ("TLS not available (fatal version), (ldap_error 0x%02x)", ldap_error);
 						ldap_unbind_ext_s (blpriv->ldap, NULL, NULL);
 						blpriv->ldap = NULL;
+						g_static_mutex_unlock (&global_ldap_lock);
 						return GNOME_Evolution_Addressbook_TLSNotAvailable;
 					}
 					else {
@@ -749,6 +789,7 @@ e_book_backend_ldap_connect (EBookBacken
 				}
 				else
 					g_message ("TLS active");
+				g_static_mutex_unlock (&global_ldap_lock);
 			}
 		}
 
@@ -756,6 +797,7 @@ e_book_backend_ldap_connect (EBookBacken
 		   authenticate the user properly later (in
 		   authenticate_user) if they've selected
 		   authentication */
+		g_static_mutex_lock (&global_ldap_lock);
 		ldap_error = ldap_simple_bind_s (blpriv->ldap, NULL, NULL);
 		if (ldap_error == LDAP_PROTOCOL_ERROR) {
 			g_warning ("failed to bind using v3.  trying v2.");
@@ -768,14 +810,17 @@ e_book_backend_ldap_connect (EBookBacken
 
 			ldap_error = ldap_simple_bind_s (blpriv->ldap, NULL, NULL);
 		}
+		g_static_mutex_unlock (&global_ldap_lock);
 
 		if (ldap_error == LDAP_PROTOCOL_ERROR) {
 			g_warning ("failed to bind using either v3 or v2 binds.");
+			g_static_mutex_unlock (&global_ldap_lock);
 			return GNOME_Evolution_Addressbook_OtherError;
 		}
 		else if (ldap_error == LDAP_SERVER_DOWN) {
 			/* we only want this to be fatal if the server is down. */
 			g_warning ("failed to bind anonymously while connecting (ldap_error 0x%02x)", ldap_error);
+			g_static_mutex_unlock (&global_ldap_lock);
 			return GNOME_Evolution_Addressbook_RepositoryOffline;
 		}
 
@@ -802,11 +847,15 @@ e_book_backend_ldap_connect (EBookBacken
 				check_schema_support (bl);
 
 			e_book_backend_set_is_loaded (E_BOOK_BACKEND (bl), TRUE);
+			g_static_mutex_unlock (&global_ldap_lock);
 			return GNOME_Evolution_Addressbook_Success;
 		}
 		else
 			g_warning ("Failed to perform root dse query anonymously, (ldap_error 0x%02x)", ldap_error);
 	}
+	else {
+		g_static_mutex_unlock (&global_ldap_lock);
+	}
 
 	g_warning ("e_book_backend_ldap_connect failed for "
 		   "'ldap://%s:%d/%s'\n",
@@ -820,8 +869,12 @@ e_book_backend_ldap_connect (EBookBacken
 static gboolean
 e_book_backend_ldap_reconnect (EBookBackendLDAP *bl, EDataBookView *book_view, int ldap_status)
 {
-	if (!bl->priv->ldap)
+	g_static_mutex_lock (&global_ldap_lock);
+	if (!bl->priv->ldap) {
+		g_static_mutex_unlock (&global_ldap_lock);
 		return FALSE;
+	}
+	g_static_mutex_unlock (&global_ldap_lock);
 
 	/* we need to reconnect if we were previously connected */
 	if (bl->priv->connected && ldap_status == LDAP_SERVER_DOWN) {
@@ -839,10 +892,13 @@ e_book_backend_ldap_reconnect (EBookBack
 			return FALSE;
 		}
 
-		if (bl->priv->auth_dn)
+		if (bl->priv->auth_dn) {
+			g_static_mutex_lock (&global_ldap_lock);
 			ldap_error = ldap_simple_bind_s(bl->priv->ldap,
 							bl->priv->auth_dn,
 							bl->priv->auth_passwd);
+			g_static_mutex_unlock (&global_ldap_lock);
+		}
 		if (book_view)
 			book_view_notify_status (book_view, "");
 
@@ -898,8 +954,10 @@ ldap_op_finished (LDAPOp *op)
 	g_hash_table_remove (bl->priv->id_to_op, &op->id);
 
 	/* should handle errors here */
+	g_static_mutex_lock (&global_ldap_lock);
 	if (bl->priv->ldap)
 		ldap_abandon (bl->priv->ldap, op->id);
+	g_static_mutex_unlock (&global_ldap_lock);
 
 	op->dtor (op);
 
@@ -1219,11 +1277,13 @@ create_contact_handler (LDAPOp *op, LDAP
 {
 	LDAPCreateOp *create_op = (LDAPCreateOp*)op;
 	EBookBackendLDAP *bl = E_BOOK_BACKEND_LDAP (op->backend);
-	LDAP *ldap = bl->priv->ldap;
+	LDAP *ldap;
 	char *ldap_error_msg;
 	int ldap_error;
 	int response;
 
+	g_static_mutex_lock (&global_ldap_lock);
+	ldap = bl->priv->ldap;
 	if (!ldap) {
 		e_data_book_respond_create (op->book,
 					    op->opid,
@@ -1231,6 +1291,7 @@ create_contact_handler (LDAPOp *op, LDAP
 					    NULL);
 		ldap_op_finished (op);
 	}
+	g_static_mutex_unlock (&global_ldap_lock);
 
 	if (LDAP_RES_ADD != ldap_msgtype (res)) {
 		g_warning ("incorrect msg type %d passed to create_contact_handler", ldap_msgtype (res));
@@ -1242,8 +1303,10 @@ create_contact_handler (LDAPOp *op, LDAP
 		return;
 	}
 
+	g_static_mutex_lock (&global_ldap_lock);
 	ldap_parse_result (ldap, res, &ldap_error,
 			   NULL, &ldap_error_msg, NULL, NULL, 0);
+	g_static_mutex_unlock (&global_ldap_lock);
 	if (ldap_error != LDAP_SUCCESS) {
 		g_warning ("create_contact_handler: %02X (%s), additional info: %s",
 			   ldap_error,
@@ -1298,22 +1361,25 @@ e_book_backend_ldap_create_contact (EBoo
 		return;
 	case GNOME_Evolution_Addressbook_MODE_REMOTE : 
 
+		g_static_mutex_lock (&global_ldap_lock);
 		if (!bl->priv->ldap) {
 			e_data_book_respond_create (book, opid, GNOME_Evolution_Addressbook_OtherError, NULL);
+			g_static_mutex_unlock (&global_ldap_lock);
 			return;
 		}
+		g_static_mutex_unlock (&global_ldap_lock);
 
 		book_view = find_book_view (bl);
 
 		printf ("vcard = %s\n", vcard);
 		
 		create_op->new_contact = e_contact_new_from_vcard (vcard);
-		
+	
+		g_static_mutex_lock (&global_ldap_lock);	
 		create_op->dn = create_dn_from_contact (create_op->new_contact, bl->priv->ldap_rootdn);
+		g_static_mutex_unlock (&global_ldap_lock);
 		e_contact_set (create_op->new_contact, E_CONTACT_UID, create_op->dn);
 		
-		ldap = bl->priv->ldap;
-		
 		/* build our mods */
 		mod_array = build_mods_from_contacts (bl, NULL, create_op->new_contact, NULL);
 		
@@ -1377,12 +1443,17 @@ e_book_backend_ldap_create_contact (EBoo
 #endif
 	
 		ldap_mods = (LDAPMod**)mod_array->pdata;
-	
+
+		g_static_mutex_lock (&global_ldap_lock);
+		ldap = bl->priv->ldap;
+		g_static_mutex_unlock (&global_ldap_lock);
+		
 		do {
 			book_view_notify_status (book_view, _("Adding contact to LDAP server..."));
-		
+			g_static_mutex_lock (&global_ldap_lock);	
 			err = ldap_add_ext (ldap, create_op->dn, ldap_mods,
 					    NULL, NULL, &create_contact_msgid);
+			g_static_mutex_unlock (&global_ldap_lock);
 		
 		} while (e_book_backend_ldap_reconnect (bl, book_view, err));
 	
@@ -1422,11 +1493,14 @@ remove_contact_handler (LDAPOp *op, LDAP
 	int ldap_error;
 	GList *ids = NULL;
 
+	g_static_mutex_lock (&global_ldap_lock);
 	if (!bl->priv->ldap) {
+		g_static_mutex_unlock (&global_ldap_lock);
 		e_data_book_respond_remove_contacts (op->book, op->opid, GNOME_Evolution_Addressbook_OtherError, NULL);
 		ldap_op_finished (op);
 		return;
 	}
+	g_static_mutex_unlock (&global_ldap_lock);
 
 	if (LDAP_RES_DELETE != ldap_msgtype (res)) {
 		g_warning ("incorrect msg type %d passed to remove_contact_handler", ldap_msgtype (res));
@@ -1438,8 +1512,10 @@ remove_contact_handler (LDAPOp *op, LDAP
 		return;
 	}
 
+	g_static_mutex_lock (&global_ldap_lock);
 	ldap_parse_result (bl->priv->ldap, res, &ldap_error,
 			   NULL, &ldap_error_msg, NULL, NULL, 0);
+	g_static_mutex_unlock (&global_ldap_lock);
 	if (ldap_error != LDAP_SUCCESS) {
 		g_warning ("remove_contact_handler: %02X (%s), additional info: %s",
 			   ldap_error,
@@ -1488,11 +1564,14 @@ e_book_backend_ldap_remove_contacts (EBo
 		g_free (remove_op);
 		return;
 	case GNOME_Evolution_Addressbook_MODE_REMOTE : 
+		g_static_mutex_lock (&global_ldap_lock);
 		if (!bl->priv->ldap) {
+			g_static_mutex_unlock (&global_ldap_lock);
 			e_data_book_respond_remove_contacts (book, opid, GNOME_Evolution_Addressbook_OtherError, NULL);
 			g_free (remove_op);
 			return;
 		}
+		g_static_mutex_unlock (&global_ldap_lock);
 
 		book_view = find_book_view (bl);
 
@@ -1505,10 +1584,12 @@ e_book_backend_ldap_remove_contacts (EBo
 		
 		do {
 			book_view_notify_status (book_view, _("Removing contact from LDAP server..."));
-			
+		
+			g_static_mutex_lock (&global_ldap_lock);	
 			ldap_error = ldap_delete_ext (bl->priv->ldap,
 						      remove_op->id,
 						      NULL, NULL, &remove_msgid);
+			g_static_mutex_unlock (&global_ldap_lock);
 		} while (e_book_backend_ldap_reconnect (bl, book_view, ldap_error));
 		
 		if (ldap_error != LDAP_SUCCESS) {
@@ -1554,11 +1635,14 @@ modify_contact_modify_handler (LDAPOp *o
 {
 	LDAPModifyOp *modify_op = (LDAPModifyOp*)op;
 	EBookBackendLDAP *bl = E_BOOK_BACKEND_LDAP (op->backend);
-	LDAP *ldap = bl->priv->ldap;
+	LDAP *ldap;
 	char *ldap_error_msg;
 	int ldap_error;
 
+	g_static_mutex_lock (&global_ldap_lock);
+	ldap = bl->priv->ldap;
 	if (!ldap) {
+		g_static_mutex_unlock (&global_ldap_lock);
 		e_data_book_respond_modify (op->book,
 					    op->opid,
 					    GNOME_Evolution_Addressbook_OtherError,
@@ -1566,6 +1650,7 @@ modify_contact_modify_handler (LDAPOp *o
 		ldap_op_finished (op);
 		return;
 	}
+	g_static_mutex_unlock (&global_ldap_lock);
 
 	if (LDAP_RES_MODIFY != ldap_msgtype (res)) {
 		g_warning ("incorrect msg type %d passed to modify_contact_handler", ldap_msgtype (res));
@@ -1577,8 +1662,10 @@ modify_contact_modify_handler (LDAPOp *o
 		return;
 	}
 
+	g_static_mutex_lock (&global_ldap_lock);
 	ldap_parse_result (ldap, res, &ldap_error,
 			   NULL, &ldap_error_msg, NULL, NULL, 0);
+	g_static_mutex_unlock (&global_ldap_lock);
 	if (ldap_error != LDAP_SUCCESS) {
 		g_warning ("modify_contact_handler: %02X (%s), additional info: %s",
 			   ldap_error,
@@ -1602,15 +1689,19 @@ modify_contact_search_handler (LDAPOp *o
 {
 	LDAPModifyOp *modify_op = (LDAPModifyOp*)op;
 	EBookBackendLDAP *bl = E_BOOK_BACKEND_LDAP (op->backend);
-	LDAP *ldap = bl->priv->ldap;
+	LDAP *ldap;
 	int msg_type;
 
+	g_static_mutex_lock (&global_ldap_lock);
+	ldap = bl->priv->ldap;
 	if (!ldap) {
+		g_static_mutex_unlock (&global_ldap_lock);
 		e_data_book_respond_modify (op->book, op->opid,
 					    GNOME_Evolution_Addressbook_OtherError, NULL);
 		ldap_op_finished (op);
 		return;
 	}
+	g_static_mutex_unlock (&global_ldap_lock);
 
 	/* if it's successful, we should get called with a
 	   RES_SEARCH_ENTRY and a RES_SEARCH_RESULT.  if it's
@@ -1618,7 +1709,9 @@ modify_contact_search_handler (LDAPOp *o
 
 	msg_type = ldap_msgtype (res);
 	if (msg_type == LDAP_RES_SEARCH_ENTRY) {
+		g_static_mutex_lock (&global_ldap_lock);
 		LDAPMessage *e = ldap_first_entry(ldap, res);
+		g_static_mutex_unlock (&global_ldap_lock);
 
 		if (!e) {
 			g_warning ("uh, this shouldn't happen");
@@ -1630,8 +1723,10 @@ modify_contact_search_handler (LDAPOp *o
 			return;
 		}
 
+		g_static_mutex_lock (&global_ldap_lock);
 		modify_op->current_contact = build_contact_from_entry (ldap, e,
 								       &modify_op->existing_objectclasses);
+		g_static_mutex_unlock (&global_ldap_lock);
 	}
 	else if (msg_type == LDAP_RES_SEARCH_RESULT) {
 		char *ldap_error_msg;
@@ -1644,8 +1739,10 @@ modify_contact_search_handler (LDAPOp *o
 
 		/* grab the result code, and set up the actual modify
                    if it was successful */
+		g_static_mutex_lock (&global_ldap_lock);
 		ldap_parse_result (bl->priv->ldap, res, &ldap_error,
 				   NULL, &ldap_error_msg, NULL, NULL, 0);
+		g_static_mutex_unlock (&global_ldap_lock);
 		if (ldap_error != LDAP_SUCCESS) {
 			g_warning ("modify_contact_search_handler: %02X (%s), additional info: %s",
 				   ldap_error,
@@ -1681,8 +1778,10 @@ modify_contact_search_handler (LDAPOp *o
 			ldap_mods = (LDAPMod**)mod_array->pdata;
 
 			/* actually perform the ldap modify */
+			g_static_mutex_lock (&global_ldap_lock);
 			ldap_error = ldap_modify_ext (ldap, modify_op->id, ldap_mods,
 						      NULL, NULL, &modify_contact_msgid);
+			g_static_mutex_unlock (&global_ldap_lock);
 
 			if (ldap_error == LDAP_SUCCESS) {
 				op->handler = modify_contact_modify_handler;
@@ -1747,28 +1846,35 @@ e_book_backend_ldap_modify_contact (EBoo
 		e_data_book_respond_modify(book, opid, GNOME_Evolution_Addressbook_RepositoryOffline, NULL);
 		return;
 	case GNOME_Evolution_Addressbook_MODE_REMOTE :
+		g_static_mutex_lock (&global_ldap_lock);
 		if (!bl->priv->ldap) {
+			g_static_mutex_unlock (&global_ldap_lock);
 			e_data_book_respond_modify (book, opid, GNOME_Evolution_Addressbook_OtherError, NULL);
 			g_free (modify_op);
 			return;
 		}
+		g_static_mutex_unlock (&global_ldap_lock);
 
 		book_view = find_book_view (bl);
 
 		modify_op->contact = e_contact_new_from_vcard (vcard);
 		modify_op->id = e_contact_get_const (modify_op->contact, E_CONTACT_UID);
 
+		g_static_mutex_lock (&global_ldap_lock);
 		ldap = bl->priv->ldap;
+		g_static_mutex_unlock (&global_ldap_lock);
 
 		do {
 			book_view_notify_status (book_view, _("Modifying contact from LDAP server..."));
 
+			g_static_mutex_lock (&global_ldap_lock);
 			ldap_error = ldap_search_ext (ldap, modify_op->id,
 						      LDAP_SCOPE_BASE,
 						      "(objectclass=*)",
 						      NULL, 0, NULL, NULL,
 						      NULL, /* XXX timeout */
 						      1, &modify_contact_msgid);
+			g_static_mutex_unlock (&global_ldap_lock);
 
 		} while (e_book_backend_ldap_reconnect (bl, book_view, ldap_error));
 
@@ -1799,18 +1905,24 @@ get_contact_handler (LDAPOp *op, LDAPMes
 	EBookBackendLDAP *bl = E_BOOK_BACKEND_LDAP (op->backend);
 	int msg_type;
 
+	g_static_mutex_lock (&global_ldap_lock);
 	if (!bl->priv->ldap) {
+		g_static_mutex_unlock (&global_ldap_lock);
 		e_data_book_respond_get_contact (op->book, op->opid, GNOME_Evolution_Addressbook_OtherError, "");
 		ldap_op_finished (op);
 		return;
 	}
+	g_static_mutex_unlock (&global_ldap_lock);
 
 	/* the msg_type will be either SEARCH_ENTRY (if we're
 	   successful) or SEARCH_RESULT (if we're not), so we finish
 	   the op after either */
 	msg_type = ldap_msgtype (res);
 	if (msg_type == LDAP_RES_SEARCH_ENTRY) {
+		g_static_mutex_lock (&global_ldap_lock);
 		LDAPMessage *e = ldap_first_entry(bl->priv->ldap, res);
+		g_static_mutex_unlock (&global_ldap_lock);
+
 		EContact *contact;
 		char *vcard;
 
@@ -1824,7 +1936,10 @@ get_contact_handler (LDAPOp *op, LDAPMes
 			return;
 		}
 
+		g_static_mutex_lock (&global_ldap_lock);
 		contact = build_contact_from_entry (bl->priv->ldap, e, NULL);
+		g_static_mutex_unlock (&global_ldap_lock);
+
 		vcard = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_30);
 		e_data_book_respond_get_contact (op->book,
 						 op->opid,
@@ -1837,8 +1952,11 @@ get_contact_handler (LDAPOp *op, LDAPMes
 	else if (msg_type == LDAP_RES_SEARCH_RESULT) {
 		char *ldap_error_msg;
 		int ldap_error;
+
+		g_static_mutex_lock (&global_ldap_lock);
 		ldap_parse_result (bl->priv->ldap, res, &ldap_error,
 				   NULL, &ldap_error_msg, NULL, NULL, 0);
+		g_static_mutex_unlock (&global_ldap_lock);
 		if (ldap_error != LDAP_SUCCESS) {
 			g_warning ("get_contact_handler: %02X (%s), additional info: %s",
 				   ldap_error,
@@ -1879,7 +1997,7 @@ e_book_backend_ldap_get_contact (EBookBa
 {
 	LDAPGetContactOp *get_contact_op;
 	EBookBackendLDAP *bl = E_BOOK_BACKEND_LDAP (backend);
-	LDAP *ldap = bl->priv->ldap;
+	LDAP *ldap;
 	int get_contact_msgid;
 	EDataBookView *book_view;
 	int ldap_error;
@@ -1910,22 +2028,29 @@ e_book_backend_ldap_get_contact (EBookBa
 		e_data_book_respond_get_contact(book, opid, GNOME_Evolution_Addressbook_RepositoryOffline, "");
 		return;
 
-	case GNOME_Evolution_Addressbook_MODE_REMOTE : 	
+	case GNOME_Evolution_Addressbook_MODE_REMOTE : 
+		g_static_mutex_lock (&global_ldap_lock);
+		ldap = bl->priv->ldap;
+
 		if (!ldap) {
+			g_static_mutex_unlock (&global_ldap_lock);
 			e_data_book_respond_get_contact (book, opid, GNOME_Evolution_Addressbook_OtherError, "");
 			return;
 		}
+		g_static_mutex_unlock (&global_ldap_lock);
 
 		get_contact_op = g_new0 (LDAPGetContactOp, 1);
 		book_view = find_book_view (bl);
 
 		do {	
+			g_static_mutex_lock (&global_ldap_lock);
 			ldap_error = ldap_search_ext (ldap, id,
 						      LDAP_SCOPE_BASE,
 						      "(objectclass=*)",
 						      NULL, 0, NULL, NULL,
 						      NULL, /* XXX timeout */
 						      1, &get_contact_msgid);
+			g_static_mutex_unlock (&global_ldap_lock);
 		} while (e_book_backend_ldap_reconnect (bl, book_view, ldap_error));
 
 		if (ldap_error == LDAP_SUCCESS) {
@@ -1954,23 +2079,35 @@ contact_list_handler (LDAPOp *op, LDAPMe
 {
 	LDAPGetContactListOp *contact_list_op = (LDAPGetContactListOp*)op;
 	EBookBackendLDAP *bl = E_BOOK_BACKEND_LDAP (op->backend);
-	LDAP *ldap = bl->priv->ldap;
+	LDAP *ldap;
 	LDAPMessage *e;
 	int msg_type;
 
+	g_static_mutex_lock (&global_ldap_lock);
+	ldap = bl->priv->ldap;
 	if (!ldap) {
 		e_data_book_respond_get_contact_list (op->book, op->opid, GNOME_Evolution_Addressbook_OtherError, NULL);
 		ldap_op_finished (op);
+		g_static_mutex_unlock (&global_ldap_lock);
 		return;
 	}
+	g_static_mutex_unlock (&global_ldap_lock);
 
 	msg_type = ldap_msgtype (res);
 	if (msg_type == LDAP_RES_SEARCH_ENTRY) {
+		g_static_mutex_lock (&global_ldap_lock);
 		e = ldap_first_entry(ldap, res);
+		g_static_mutex_unlock (&global_ldap_lock);
 
 		while (NULL != e) {
-			EContact *contact = build_contact_from_entry (ldap, e, NULL);
-			char *vcard = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_30);
+			EContact *contact;
+			char *vcard;
+
+			g_static_mutex_lock (&global_ldap_lock);
+			contact = build_contact_from_entry (ldap, e, NULL);
+			g_static_mutex_unlock (&global_ldap_lock);
+
+			vcard = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_30);
 
 			printf ("vcard = %s\n", vcard);
  
@@ -1978,15 +2115,20 @@ contact_list_handler (LDAPOp *op, LDAPMe
 								   vcard);
 
 			g_object_unref (contact);
+
+			g_static_mutex_lock (&global_ldap_lock);
 			e = ldap_next_entry(ldap, e);
+			g_static_mutex_unlock (&global_ldap_lock);
 		}
 	}
 	else if (msg_type == LDAP_RES_SEARCH_RESULT) {
 		char *ldap_error_msg;
 		int ldap_error;
 
+		g_static_mutex_lock (&global_ldap_lock);
 		ldap_parse_result (ldap, res, &ldap_error,
 				   NULL, &ldap_error_msg, NULL, NULL, 0);
+		g_static_mutex_unlock (&global_ldap_lock);
 		if (ldap_error != LDAP_SUCCESS) {
 			g_warning ("contact_list_handler: %02X (%s), additional info: %s",
 				   ldap_error,
@@ -2047,7 +2189,7 @@ e_book_backend_ldap_get_contact_list (EB
 {
 	LDAPGetContactListOp *contact_list_op;
 	EBookBackendLDAP *bl = E_BOOK_BACKEND_LDAP (backend);
-	LDAP *ldap = bl->priv->ldap;
+	LDAP *ldap;
 	int contact_list_msgid;
 	EDataBookView *book_view;
 	int ldap_error;
@@ -2080,10 +2222,15 @@ e_book_backend_ldap_get_contact_list (EB
 		return;
 		
 	case GNOME_Evolution_Addressbook_MODE_REMOTE:
+		g_static_mutex_lock (&global_ldap_lock);
+		ldap = bl->priv->ldap;
+
 		if (!ldap) {
+			g_static_mutex_unlock (&global_ldap_lock);
 			e_data_book_respond_get_contact_list (book, opid, GNOME_Evolution_Addressbook_OtherError, NULL);
 			return;
 		}
+		g_static_mutex_unlock (&global_ldap_lock);
 
 		contact_list_op = g_new0 (LDAPGetContactListOp, 1);
 		book_view = find_book_view (bl);
@@ -2093,6 +2240,7 @@ e_book_backend_ldap_get_contact_list (EB
 		printf ("getting contact list with filter: %s\n", ldap_query);
 
 		do {	
+			g_static_mutex_lock (&global_ldap_lock);
 			ldap_error = ldap_search_ext (ldap,
 						      bl->priv->ldap_rootdn,
 						      bl->priv->ldap_scope,
@@ -2100,6 +2248,7 @@ e_book_backend_ldap_get_contact_list (EB
 						      NULL, 0, NULL, NULL,
 						      NULL, /* XXX timeout */
 						      LDAP_NO_LIMIT, &contact_list_msgid);
+			g_static_mutex_unlock (&global_ldap_lock);
 		} while (e_book_backend_ldap_reconnect (bl, book_view, ldap_error));
 
 		g_free (ldap_query);
@@ -3236,15 +3385,19 @@ build_contact_from_entry (LDAP *ldap, LD
 static gboolean
 poll_ldap (EBookBackendLDAP *bl)
 {
-	LDAP           *ldap = bl->priv->ldap;
+	LDAP           *ldap;
 	int            rc;
 	LDAPMessage    *res;
 	struct timeval timeout;
 
+	g_static_mutex_lock (&global_ldap_lock);
+	ldap = bl->priv->ldap;
 	if (!ldap) {
+		g_static_mutex_unlock (&global_ldap_lock);
 		bl->priv->poll_timeout = -1;
 		return FALSE;
 	}
+	g_static_mutex_unlock (&global_ldap_lock);
 
 	if (!bl->priv->active_ops) {
 		g_warning ("poll_ldap being called for backend with no active operations");
@@ -3255,7 +3408,9 @@ poll_ldap (EBookBackendLDAP *bl)
 	timeout.tv_sec = 0;
 	timeout.tv_usec = LDAP_RESULT_TIMEOUT_MILLIS * 1000;
 
+	g_static_mutex_lock (&global_ldap_lock);	
 	rc = ldap_result (ldap, LDAP_RES_ANY, 0, &timeout, &res);
+	g_static_mutex_unlock (&global_ldap_lock);
 	if (rc != 0) {/* rc == 0 means timeout exceeded */
 		if (rc == -1) {
 			EDataBookView *book_view = find_book_view (bl);
@@ -3298,17 +3453,21 @@ ldap_search_handler (LDAPOp *op, LDAPMes
 	LDAPSearchOp *search_op = (LDAPSearchOp*)op;
 	EDataBookView *view = search_op->view;
 	EBookBackendLDAP *bl = E_BOOK_BACKEND_LDAP (op->backend);
-	LDAP *ldap = bl->priv->ldap;
+	LDAP *ldap;
 	LDAPMessage *e;
 	int msg_type;
 
 	d(printf ("ldap_search_handler (%p)\n", view));
 
+	g_static_mutex_lock (&global_ldap_lock);
+	ldap = bl->priv->ldap;
 	if (!ldap) {
+		g_static_mutex_unlock (&global_ldap_lock);
 		e_data_book_view_notify_complete (view, GNOME_Evolution_Addressbook_OtherError);
 		ldap_op_finished (op);
 		return;
 	}
+	g_static_mutex_unlock (&global_ldap_lock);
 
 	if (!search_op->notified_receiving_results) {
 		search_op->notified_receiving_results = TRUE;
@@ -3317,23 +3476,31 @@ ldap_search_handler (LDAPOp *op, LDAPMes
 
 	msg_type = ldap_msgtype (res);
 	if (msg_type == LDAP_RES_SEARCH_ENTRY) {
+		g_static_mutex_lock (&global_ldap_lock);
 		e = ldap_first_entry(ldap, res);
+		g_static_mutex_unlock (&global_ldap_lock);
 
 		while (NULL != e) {
+			g_static_mutex_lock (&global_ldap_lock);
 			EContact *contact = build_contact_from_entry (ldap, e, NULL);
+			g_static_mutex_unlock (&global_ldap_lock);
 
 			e_data_book_view_notify_update (view, contact);
 			g_object_unref (contact);
 
+			g_static_mutex_lock (&global_ldap_lock);				
 			e = ldap_next_entry(ldap, e);
+			g_static_mutex_unlock (&global_ldap_lock);
 		}
 	}
 	else if (msg_type == LDAP_RES_SEARCH_RESULT) {
 		char *ldap_error_msg;
 		int ldap_error;
 
+		g_static_mutex_lock (&global_ldap_lock);
 		ldap_parse_result (ldap, res, &ldap_error,
 				   NULL, &ldap_error_msg, NULL, NULL, 0);
+		g_static_mutex_unlock (&global_ldap_lock);
 		if (ldap_error != LDAP_SUCCESS) {
 			g_warning ("ldap_search_handler: %02X (%s), additional info: %s",
 				   ldap_error,
@@ -3408,12 +3575,14 @@ e_book_backend_ldap_search (EBookBackend
 	case GNOME_Evolution_Addressbook_MODE_REMOTE :
 		ldap_query = e_book_backend_ldap_build_query (bl, e_data_book_view_get_card_query (view));
 
+		g_static_mutex_lock (&global_ldap_lock);
 		if (ldap_query != NULL && bl->priv->ldap) {
-			LDAP *ldap = bl->priv->ldap;
+			LDAP *ldap;
 			int ldap_err;
 			int search_msgid;
 			int view_limit;
 
+			g_static_mutex_unlock (&global_ldap_lock);
 			view_limit = e_data_book_view_get_max_results (view);
 			if (view_limit == -1 || view_limit > bl->priv->ldap_limit)
 				view_limit = bl->priv->ldap_limit;
@@ -3421,9 +3590,14 @@ e_book_backend_ldap_search (EBookBackend
 			printf ("searching server using filter: %s (expecting max %d results)\n", ldap_query,
 				view_limit);
 
+			g_static_mutex_lock (&global_ldap_lock);
+			ldap = bl->priv->ldap;
+			g_static_mutex_unlock (&global_ldap_lock);
+
 			do {
 				book_view_notify_status (view, _("Searching..."));
 
+				g_static_mutex_lock (&global_ldap_lock);
 				ldap_err = ldap_search_ext (ldap, bl->priv->ldap_rootdn,
 							    bl->priv->ldap_scope,
 							    ldap_query,
@@ -3432,6 +3606,7 @@ e_book_backend_ldap_search (EBookBackend
 							    NULL, /* XXX */
 							    NULL, /* XXX timeout */
 							    view_limit, &search_msgid);
+				g_static_mutex_unlock (&global_ldap_lock);
 			} while (e_book_backend_ldap_reconnect (bl, view, ldap_err));
 
 			g_free (ldap_query);
@@ -3468,6 +3643,7 @@ e_book_backend_ldap_search (EBookBackend
 							  GNOME_Evolution_Addressbook_InvalidQuery);
 			*/
 			/* Ignore NULL query */
+			g_static_mutex_lock (&global_ldap_lock);
 			e_data_book_view_notify_complete (view,
 							  GNOME_Evolution_Addressbook_Success);
 			return;
@@ -3521,27 +3697,38 @@ generate_cache_handler (LDAPOp *op, LDAP
 {
 	LDAPGetContactListOp *contact_list_op = (LDAPGetContactListOp *) op;
 	EBookBackendLDAP *bl = E_BOOK_BACKEND_LDAP (op->backend);
-	LDAP *ldap = bl->priv->ldap;
+	LDAP *ldap;
 	LDAPMessage *e;
 	gint msg_type;
 	EDataBookView *book_view;
 
+	g_static_mutex_lock (&global_ldap_lock);
+	ldap = bl->priv->ldap;
 	if (!ldap) {
+		g_static_mutex_unlock (&global_ldap_lock);
 		ldap_op_finished (op);
 		return;
 	}
+	g_static_mutex_unlock (&global_ldap_lock);
 
 	book_view = find_book_view (bl);
 
 	msg_type = ldap_msgtype (res);
 	if (msg_type == LDAP_RES_SEARCH_ENTRY) {
+		g_static_mutex_lock (&global_ldap_lock);
 		e = ldap_first_entry(ldap, res);
+		g_static_mutex_unlock (&global_ldap_lock);
 
 		while (e != NULL) {
+			g_static_mutex_lock (&global_ldap_lock);
 			EContact *contact = build_contact_from_entry (ldap, e, NULL);
+			g_static_mutex_unlock (&global_ldap_lock);
+
 			contact_list_op->contacts = g_list_prepend (contact_list_op->contacts, contact);
 
+			g_static_mutex_lock (&global_ldap_lock);
 			e = ldap_next_entry(ldap, e);
+			g_static_mutex_unlock (&global_ldap_lock);
 		}
 	} else {
 		GList *l;
@@ -3597,15 +3784,19 @@ generate_cache (EBookBackendLDAP *book_b
 
 	priv = book_backend_ldap->priv;
 
+	g_static_mutex_lock (&global_ldap_lock);
 	if (!priv->ldap) {
+		g_static_mutex_unlock (&global_ldap_lock);
 		g_free (contact_list_op);
 		return;
 	}
+	g_static_mutex_unlock (&global_ldap_lock);
 
 	ldap_query = e_book_backend_ldap_build_query (book_backend_ldap, 
 						      "(beginswith \"file_as\" \"\")");
 
-	do {	
+	do {
+		g_static_mutex_lock (&global_ldap_lock);	
 		ldap_error = ldap_search_ext (priv->ldap,
 					      priv->ldap_rootdn,
 					      priv->ldap_scope,
@@ -3613,6 +3804,7 @@ generate_cache (EBookBackendLDAP *book_b
 					      NULL, 0, NULL, NULL,
 					      NULL, /* XXX timeout */
 					      LDAP_NO_LIMIT, &contact_list_msgid);
+		g_static_mutex_unlock (&global_ldap_lock);
 	} while (e_book_backend_ldap_reconnect (book_backend_ldap, NULL, ldap_error));
 
 	g_free (ldap_query);
@@ -3647,7 +3839,11 @@ e_book_backend_ldap_authenticate_user (E
 						       GNOME_Evolution_Addressbook_Success);
 		return;
 	}
+
+	g_static_mutex_lock (&global_ldap_lock);
 	if (!bl->priv->connected || !bl->priv->ldap) {
+		g_static_mutex_unlock (&global_ldap_lock);
+
 		status = e_book_backend_ldap_connect (bl);
 		if (status != GNOME_Evolution_Addressbook_Success) {
 			e_data_book_respond_authenticate_user (book,
@@ -3656,23 +3852,31 @@ e_book_backend_ldap_authenticate_user (E
 		}
 						       
 	}
+	else {
+		g_static_mutex_unlock (&global_ldap_lock);
+	}
+
 	if (!strncasecmp (auth_method, LDAP_SIMPLE_PREFIX, strlen (LDAP_SIMPLE_PREFIX))) {
 
 		if (!strcmp (auth_method, "ldap/simple-email")) {
 			LDAPMessage    *res, *e;
 			char *query = g_strdup_printf ("(mail=%s)", user);
 
+			g_static_mutex_lock (&global_ldap_lock);
 			ldap_error = ldap_search_s (bl->priv->ldap,
 						    bl->priv->ldap_rootdn,
 						    bl->priv->ldap_scope,
 						    query,
 						    NULL, 0, &res);
+			g_static_mutex_unlock (&global_ldap_lock);
 			g_free (query);
 
 			if (ldap_error == LDAP_SUCCESS) {
 				char *entry_dn;
 
+				g_static_mutex_lock (&global_ldap_lock);
 				e = ldap_first_entry (bl->priv->ldap, res);
+				g_static_mutex_unlock (&global_ldap_lock);
 				if (!e) {
 					g_warning ("Failed to get the DN for %s", user);
 					ldap_msgfree (res);
@@ -3682,7 +3886,9 @@ e_book_backend_ldap_authenticate_user (E
 					return;
 				}
 
+				g_static_mutex_lock (&global_ldap_lock);
 				entry_dn = ldap_get_dn (bl->priv->ldap, e);
+				g_static_mutex_unlock (&global_ldap_lock);
 				dn = g_strdup(entry_dn);
 
 				ldap_memfree (entry_dn);
@@ -3701,9 +3907,11 @@ e_book_backend_ldap_authenticate_user (E
 
 		/* now authenticate against the DN we were either supplied or queried for */
 		printf ("simple auth as %s\n", dn);
+		g_static_mutex_lock (&global_ldap_lock);
 		ldap_error = ldap_simple_bind_s(bl->priv->ldap,
 						dn,
 						passwd);
+		g_static_mutex_unlock (&global_ldap_lock);
 		/* Some ldap servers are returning (ex active directory ones) LDAP_SERVER_DOWN
 		 * when we try to do an ldap operation  after being  idle 
 		 * for some time. This error is handled by poll_ldap in case of search operations
@@ -3726,6 +3934,7 @@ e_book_backend_ldap_authenticate_user (E
 #ifdef ENABLE_SASL_BINDS
 	else if (!strncasecmp (auth_method, SASL_PREFIX, strlen (SASL_PREFIX))) {
 		g_print ("sasl bind (mech = %s) as %s", auth_method + strlen (SASL_PREFIX), user);
+		g_static_mutex_lock (&global_ldap_lock);
 		ldap_error = ldap_sasl_bind_s (bl->priv->ldap,
 					       NULL,
 					       auth_method + strlen (SASL_PREFIX),
@@ -3733,6 +3942,7 @@ e_book_backend_ldap_authenticate_user (E
 					       NULL,
 					       NULL,
 					       NULL);
+		g_static_mutex_unlock (&global_ldap_lock);
 
 		if (ldap_error == LDAP_NOT_SUPPORTED)
 			e_data_book_respond_authenticate_user (book,
@@ -3838,8 +4048,10 @@ ldap_cancel_op(void *key, void *value, v
 	LDAPOp *op = value;
 
 	/* ignore errors, its only best effort? */
+	g_static_mutex_lock (&global_ldap_lock);
 	if (bl->priv->ldap)
 		ldap_abandon_ext (bl->priv->ldap, op->id, NULL, NULL);
+	g_static_mutex_unlock (&global_ldap_lock);
 }
 
 static GNOME_Evolution_Addressbook_CallStatus
@@ -4104,8 +4316,10 @@ call_dtor (int msgid, LDAPOp *op, gpoint
 
 	bl = E_BOOK_BACKEND_LDAP (op->backend);
 
+	g_static_mutex_lock (&global_ldap_lock);
 	if (bl->priv->ldap)
 		ldap_abandon (bl->priv->ldap, op->id);
+	g_static_mutex_unlock (&global_ldap_lock);
 
 	op->dtor (op);
 
@@ -4126,8 +4340,10 @@ e_book_backend_ldap_dispose (GObject *ob
 		g_static_rec_mutex_unlock (&bl->priv->op_hash_mutex);
 		g_static_rec_mutex_free (&bl->priv->op_hash_mutex);
 
+		g_static_mutex_lock (&global_ldap_lock);	
 		if (bl->priv->ldap)
 			ldap_unbind_ext_s (bl->priv->ldap, NULL, NULL);
+		g_static_mutex_unlock (&global_ldap_lock);
 
 		if (bl->priv->poll_timeout != -1) {
 			printf ("removing timeout\n");


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]