]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
doveadm-server: Refactor connection handshake and authentication
authorAki Tuomi <aki.tuomi@dovecot.fi>
Tue, 12 Sep 2017 10:43:30 +0000 (13:43 +0300)
committerTimo Sirainen <tss@dovecot.fi>
Thu, 5 Oct 2017 10:58:28 +0000 (13:58 +0300)
Simplifies next change

src/doveadm/client-connection.c
src/doveadm/client-connection.h
src/doveadm/doveadm-util.h
src/doveadm/server-connection.c

index 122a144e07959d4f271bbcbc048d81b7fef5e2e2..0692bed268968553c19846d7fde1077ad44d4aa9 100644 (file)
@@ -419,6 +419,11 @@ static void client_connection_input(struct client_connection *conn)
                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 {
index 4dae6477f9584748f4eaba4a4111f9b75bc7aab4..5def17000f01357de6789cd83ac2d12269e7b7ea 100644 (file)
@@ -19,6 +19,7 @@ struct client_connection {
        bool handshaked:1;
        bool authenticated:1;
        bool http:1;
+       bool io_setup:1;
 };
 
 struct client_connection *
index b5bbf2df0d5f251f4b58e11d1e426d4956480a6d..54fdf60d7ca62a6a51da928d2dfb078212e37c9f 100644 (file)
@@ -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;
 
index c22d60c93ec96fc7d7a242f95f67d2a8b27bc90d..a0a6de74080c251ac06474729d1a2a98594245fc 100644 (file)
@@ -307,30 +307,38 @@ static void server_connection_input(struct server_connection *conn)
 
        timeout_remove(&conn->to_input);
 
-       if (!conn->handshaked) {
-               if ((line = i_stream_read_next_line(conn->input)) == NULL) {
-                       if (conn->input->eof || conn->input->stream_errno != 0) {
-                               server_log_disconnect_error(conn);
+       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;
                        }
-                       return;
+                       conn->handshaked = TRUE;
                }
 
-               conn->handshaked = TRUE;
-               if (strcmp(line, "+") == 0)
-                       server_connection_authenticated(conn);
-               else if (strcmp(line, "-") == 0) {
-                       if (server_connection_authenticate(conn) < 0) {
+               if (line == NULL) {
+                       if (conn->input->eof || conn->input->stream_errno != 0) {
+                               server_log_disconnect_error(conn);
                                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) {
@@ -340,18 +348,6 @@ static void server_connection_input(struct server_connection *conn)
                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)) ;
 }