diff src/imap-login/client-authenticate.c @ 1702:43815588dd6b HEAD

Moved client side code for auth process handling to lib-auth. Some other login process cleanups.
author Timo Sirainen <tss@iki.fi>
date Fri, 22 Aug 2003 05:42:12 +0300
parents e850252cdc7e
children 96dab004a87a
line wrap: on
line diff
--- a/src/imap-login/client-authenticate.c	Fri Aug 22 02:31:50 2003 +0300
+++ b/src/imap-login/client-authenticate.c	Fri Aug 22 05:42:12 2003 +0300
@@ -9,26 +9,27 @@
 #include "safe-memset.h"
 #include "str.h"
 #include "imap-parser.h"
-#include "auth-connection.h"
+#include "auth-client.h"
 #include "../auth/auth-mech-desc.h"
 #include "client.h"
 #include "client-authenticate.h"
 #include "auth-common.h"
 #include "master.h"
 
-static enum auth_mech auth_mechs = 0;
-static char *auth_mechs_capability = NULL;
-
 const char *client_authenticate_get_capabilities(int tls)
 {
+	static enum auth_mech cached_auth_mechs = 0;
+	static char *cached_capability = NULL;
+        enum auth_mech auth_mechs;
 	string_t *str;
 	int i;
 
-	if (auth_mechs == available_auth_mechs)
-		return auth_mechs_capability;
+	auth_mechs = auth_client_get_available_mechs(auth_client);
+	if (auth_mechs == cached_auth_mechs)
+		return cached_capability;
 
-	auth_mechs = available_auth_mechs;
-	i_free(auth_mechs_capability);
+	cached_auth_mechs = auth_mechs;
+	i_free(cached_capability);
 
 	str = t_str_new(128);
 
@@ -43,8 +44,8 @@
 		}
 	}
 
-	auth_mechs_capability = i_strdup_empty(str_c(str));
-	return auth_mechs_capability;
+	cached_capability = i_strdup_empty(str_c(str));
+	return cached_capability;
 }
 
 static struct auth_mech_desc *auth_mech_find(const char *name)
@@ -65,8 +66,7 @@
 	client->authenticating = FALSE;
 
 	if (client->common.auth_request != NULL) {
-		auth_abort_request(client->common.auth_request);
-                auth_request_unref(client->common.auth_request);
+		auth_client_request_abort(client->common.auth_request);
 		client->common.auth_request = NULL;
 	}
 
@@ -80,8 +80,6 @@
 		io_remove(client->common.io);
 	client->common.io = client->common.fd == -1 ? NULL :
 		io_add(client->common.fd, IO_READ, client_input, client);
-
-	client_unref(client);
 }
 
 static void master_callback(struct client *_client, int success)
@@ -99,7 +97,6 @@
 	}
 
 	client_destroy(client, reason);
-	client_unref(client);
 }
 
 static void client_send_auth_data(struct imap_client *client,
@@ -122,15 +119,15 @@
 }
 
 static void login_callback(struct auth_request *request,
-			   struct auth_login_reply *reply,
-			   const unsigned char *data, struct client *_client)
+			   struct auth_client_request_reply *reply,
+			   const unsigned char *data, void *context)
 {
-	struct imap_client *client = (struct imap_client *) _client;
+	struct imap_client *client = context;
 	const char *error;
 	const void *ptr;
 	size_t size;
 
-	switch (auth_callback(request, reply, data, _client,
+	switch (auth_callback(request, reply, data, &client->common,
 			      master_callback, &error)) {
 	case -1:
 		/* login failed */
@@ -140,7 +137,7 @@
 	case 0:
 		/* continue */
 		ptr = buffer_get_data(client->plain_login, &size);
-		auth_continue_request(request, ptr, size);
+		auth_client_request_continue(request, ptr, size);
 
 		buffer_set_used_size(client->plain_login, 0);
 		break;
@@ -184,33 +181,34 @@
 	buffer_append_c(client->plain_login, '\0');
 	buffer_append(client->plain_login, pass, strlen(pass));
 
-	client_ref(client);
-	if (auth_init_request(AUTH_MECH_PLAIN, AUTH_PROTOCOL_IMAP,
-			      login_callback, &client->common, &error)) {
-		/* don't read any input from client until login is finished */
-		if (client->common.io != NULL) {
-			io_remove(client->common.io);
-			client->common.io = NULL;
-		}
-                client->authenticating = TRUE;
-		return TRUE;
-	} else {
+	client->common.auth_request =
+		auth_client_request_new(auth_client, AUTH_MECH_PLAIN,
+					AUTH_PROTOCOL_IMAP, login_callback,
+					client, &error);
+	if (client->common.auth_request == NULL) {
 		client_send_tagline(client, t_strconcat(
 			"NO Login failed: ", error, NULL));
-		client_unref(client);
 		return TRUE;
 	}
+
+	/* don't read any input from client until login is finished */
+	if (client->common.io != NULL) {
+		io_remove(client->common.io);
+		client->common.io = NULL;
+	}
+
+	client->authenticating = TRUE;
+	return TRUE;
 }
 
 static void authenticate_callback(struct auth_request *request,
-				  struct auth_login_reply *reply,
-				  const unsigned char *data,
-				  struct client *_client)
+				  struct auth_client_request_reply *reply,
+				  const unsigned char *data, void *context)
 {
-	struct imap_client *client = (struct imap_client *) _client;
+	struct imap_client *client = context;
 	const char *error;
 
-	switch (auth_callback(request, reply, data, _client,
+	switch (auth_callback(request, reply, data, &client->common,
 			      master_callback, &error)) {
 	case -1:
 		/* login failed */
@@ -266,9 +264,9 @@
 	} else if (client->common.auth_request == NULL) {
 		client_auth_abort(client, "Don't send unrequested data");
 	} else {
-		auth_continue_request(client->common.auth_request,
-				      buffer_get_data(buf, NULL),
-				      buffer_get_used_size(buf));
+		auth_client_request_continue(client->common.auth_request,
+					     buffer_get_data(buf, NULL),
+					     buffer_get_used_size(buf));
 	}
 
 	/* clear sensitive data */
@@ -306,9 +304,12 @@
 		return TRUE;
 	}
 
-	client_ref(client);
-	if (auth_init_request(mech->mech, AUTH_PROTOCOL_IMAP,
-			      authenticate_callback, &client->common, &error)) {
+	client->common.auth_request =
+		auth_client_request_new(auth_client, mech->mech,
+					AUTH_PROTOCOL_IMAP,
+					authenticate_callback,
+					client, &error);
+	if (client->common.auth_request != NULL) {
 		/* following input data will go to authentication */
 		if (client->common.io != NULL)
 			io_remove(client->common.io);
@@ -318,7 +319,6 @@
 	} else {
 		client_send_tagline(client, t_strconcat(
 			"NO Authentication failed: ", error, NULL));
-		client_unref(client);
 	}
 
 	return TRUE;