changeset 21824:d228cc8cf685

imapc: Fail user creation if login to imapc_host fails. This causes imapc to actually wait for the login to succeed or fail. Such a wait was already done by the imap code, which will be removed by the next patch.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Sun, 19 Mar 2017 15:32:53 +0200
parents c7975678d4dc
children f04be671add7
files src/lib-storage/index/imapc/imapc-storage.c src/lib-storage/index/imapc/imapc-storage.h
diffstat 2 files changed, 30 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/index/imapc/imapc-storage.c	Sat Mar 25 15:46:58 2017 +0200
+++ b/src/lib-storage/index/imapc/imapc-storage.c	Sun Mar 19 15:32:53 2017 +0200
@@ -216,11 +216,14 @@
 }
 
 static void
-imapc_storage_client_login(const struct imapc_command_reply *reply,
-			   void *context)
+imapc_storage_client_login_callback(const struct imapc_command_reply *reply,
+				    void *context)
 {
 	struct imapc_storage_client *client = context;
 
+	client->auth_returned = TRUE;
+	imapc_client_stop(client->client);
+
 	if (reply->state == IMAPC_COMMAND_STATE_OK)
 		return;
 	if (client->destroying &&
@@ -231,6 +234,7 @@
 	}
 
 	client->auth_failed = TRUE;
+	client->auth_error = i_strdup(reply->text_full);
 
 	if (client->_storage != NULL) {
 		if (reply->state == IMAPC_COMMAND_STATE_DISCONNECTED)
@@ -250,6 +254,24 @@
 	}
 }
 
+static void imapc_storage_client_login(struct imapc_storage_client *client,
+				       struct mail_user *user, const char *host)
+{
+	imapc_client_login(client->client, imapc_storage_client_login_callback, client);
+	if (!user->namespaces_created) {
+		/* we're still initializing the user. wait for the
+		   login to finish, so we can fail the user creation
+		   if it fails. */
+		while (!client->auth_returned)
+			imapc_client_run(client->client);
+		if (client->auth_failed) {
+			user->error = p_strdup_printf(user->pool,
+				"imapc: Login to %s failed: %s",
+				host, client->auth_error);
+		}
+	}
+}
+
 int imapc_storage_client_create(struct mail_namespace *ns,
 				const struct imapc_settings *imapc_set,
 				const struct mail_storage_settings *mail_set,
@@ -318,7 +340,7 @@
 				       imapc_storage_client_untagged_cb, client);
 	if ((ns->flags & NAMESPACE_FLAG_LIST_PREFIX) != 0) {
 		/* start logging in immediately */
-		imapc_client_login(client->client, imapc_storage_client_login, client);
+		imapc_storage_client_login(client, ns->user, set.host);
 	}
 
 	*client_r = client;
@@ -339,6 +361,7 @@
 	array_foreach_modifiable(&client->untagged_callbacks, cb)
 		i_free(cb->name);
 	array_free(&client->untagged_callbacks);
+	i_free(client->auth_error);
 	i_free(client);
 }
 
--- a/src/lib-storage/index/imapc/imapc-storage.h	Sat Mar 25 15:46:58 2017 +0200
+++ b/src/lib-storage/index/imapc/imapc-storage.h	Sun Mar 19 15:32:53 2017 +0200
@@ -51,6 +51,10 @@
 
 	ARRAY(struct imapc_storage_event_callback) untagged_callbacks;
 
+	char *auth_error;
+	/* Authentication reply was received (success or failure) */
+	bool auth_returned:1;
+	/* Authentication failed */
 	unsigned int auth_failed:1;
 	unsigned int destroying:1;
 };