Skip to content

Commit

Permalink
doveadm-server: Refactor connection handshake and authentication
Browse files Browse the repository at this point in the history
Simplifies next change
  • Loading branch information
cmouse committed Oct 5, 2017
1 parent 7d2f1e7 commit b45b2e8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 29 deletions.
5 changes: 5 additions & 0 deletions src/doveadm/client-connection.c
Expand Up @@ -423,6 +423,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 {
Expand Down
1 change: 1 addition & 0 deletions src/doveadm/client-connection.h
Expand Up @@ -18,6 +18,7 @@ struct client_connection {

unsigned int handshaked:1;
unsigned int authenticated:1;
unsigned int io_setup:1;
};

struct client_connection *
Expand Down
2 changes: 2 additions & 0 deletions src/doveadm/doveadm-util.h
Expand Up @@ -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;

Expand Down
54 changes: 25 additions & 29 deletions src/doveadm/server-connection.c
Expand Up @@ -296,30 +296,38 @@ static void server_connection_input(struct server_connection *conn)
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->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) {
Expand All @@ -329,18 +337,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)) ;
}

Expand Down

0 comments on commit b45b2e8

Please sign in to comment.