changeset 22554:8c27d8d766bd

doveadm-server: Refactor connection handshake and authentication Simplifies next change
author Aki Tuomi <aki.tuomi@dovecot.fi>
date Tue, 12 Sep 2017 13:43:30 +0300
parents 7db517071db5
children e05ab5f8a8bc
files src/doveadm/client-connection.c src/doveadm/client-connection.h src/doveadm/doveadm-util.h src/doveadm/server-connection.c
diffstat 4 files changed, 35 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/doveadm/client-connection.c	Tue Aug 22 10:14:22 2017 +0300
+++ b/src/doveadm/client-connection.c	Tue Sep 12 13:43:30 2017 +0300
@@ -423,6 +423,11 @@
 		conn->authenticated = TRUE;
 	}
 
+	if (!conn->io_setup) {
+		conn->io_setup = TRUE;
+		doveadm_print_ostream = conn->output;
+	}
+
 	while (ok && !conn->input->closed &&
 	       (line = i_stream_read_next_line(conn->input)) != NULL) {
 		T_BEGIN {
--- a/src/doveadm/client-connection.h	Tue Aug 22 10:14:22 2017 +0300
+++ b/src/doveadm/client-connection.h	Tue Sep 12 13:43:30 2017 +0300
@@ -18,6 +18,7 @@
 
 	unsigned int handshaked:1;
 	unsigned int authenticated:1;
+	unsigned int io_setup:1;
 };
 
 struct client_connection *
--- a/src/doveadm/doveadm-util.h	Tue Aug 22 10:14:22 2017 +0300
+++ b/src/doveadm/doveadm-util.h	Tue Sep 12 13:43:30 2017 +0300
@@ -4,6 +4,8 @@
 #include "net.h"
 
 #define DOVEADM_SERVER_PROTOCOL_VERSION_MAJOR 1
+#define DOVEADM_SERVER_PROTOCOL_VERSION_MINOR 0
+#define DOVEADM_SERVER_PROTOCOL_VERSION_LINE "VERSION\tdoveadm-server\t1\t0"
 
 extern bool doveadm_verbose, doveadm_debug, doveadm_server;
 
--- a/src/doveadm/server-connection.c	Tue Aug 22 10:14:22 2017 +0300
+++ b/src/doveadm/server-connection.c	Tue Sep 12 13:43:30 2017 +0300
@@ -296,30 +296,38 @@
 	if (conn->to_input != NULL)
 		timeout_remove(&conn->to_input);
 
-	if (!conn->handshaked) {
-		if ((line = i_stream_read_next_line(conn->input)) == NULL) {
+	if (!conn->handshaked || !conn->authenticated) {
+		while((line = i_stream_read_next_line(conn->input)) != NULL) {
+			if (strcmp(line, "+") == 0) {
+				server_connection_authenticated(conn);
+				break;
+			} else if (strcmp(line, "-") == 0) {
+				if (!conn->handshaked &&
+				    server_connection_authenticate(conn) < 0) {
+					server_connection_destroy(&conn);
+					return;
+				} else if (conn->handshaked) {
+					i_error("doveadm authentication failed (%s)",
+						line+1);
+					server_connection_destroy(&conn);
+					return;
+				}
+			} else {
+				i_error("doveadm server sent invalid handshake: %s",
+					line);
+				server_connection_destroy(&conn);
+				return;
+			}
+			conn->handshaked = TRUE;
+		}
+
+		if (line == NULL) {
 			if (conn->input->eof || conn->input->stream_errno != 0) {
 				server_log_disconnect_error(conn);
 				server_connection_destroy(&conn);
 			}
-			return;
 		}
-
-		conn->handshaked = TRUE;
-		if (strcmp(line, "+") == 0)
-			server_connection_authenticated(conn);
-		else if (strcmp(line, "-") == 0) {
-			if (server_connection_authenticate(conn) < 0) {
-				server_connection_destroy(&conn);
-				return;
-			}
-			return;
-		} else {
-			i_error("doveadm server sent invalid handshake: %s",
-				line);
-			server_connection_destroy(&conn);
-			return;
-		}
+		return;
 	}
 
 	if (i_stream_read(conn->input) < 0) {
@@ -329,18 +337,6 @@
 		return;
 	}
 
-	if (!conn->authenticated) {
-		if ((line = i_stream_next_line(conn->input)) == NULL)
-			return;
-		if (strcmp(line, "+") == 0)
-			server_connection_authenticated(conn);
-		else {
-			i_error("doveadm authentication failed (%s)", line+1);
-			server_connection_destroy(&conn);
-			return;
-		}
-	}
-
 	while (server_connection_input_one(conn)) ;
 }