From: Timo Sirainen Date: Wed, 18 Nov 2009 20:19:39 +0000 (-0500) Subject: dsync: Added handshaking to proxy protocol. X-Git-Tag: 2.0.beta1~118 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3d4ae2fbada5aedbb719289409eb4d836ba8bf73;p=thirdparty%2Fdovecot%2Fcore.git dsync: Added handshaking to proxy protocol. --HG-- branch : HEAD --- diff --git a/src/dsync/dsync-proxy-client.c b/src/dsync/dsync-proxy-client.c index 390b8137cd..67fc6bc17f 100644 --- a/src/dsync/dsync-proxy-client.c +++ b/src/dsync/dsync-proxy-client.c @@ -64,6 +64,7 @@ struct proxy_client_dsync_worker { ARRAY_DEFINE(request_array, struct proxy_client_request); struct aqueue *request_queue; + unsigned int handshake_received:1; unsigned int finished:1; }; @@ -101,7 +102,19 @@ proxy_client_worker_read_line(struct proxy_client_dsync_worker *worker, return -1; } } - return *line_r != NULL ? 1 : 0; + if (*line_r == NULL) + return 0; + + if (!worker->handshake_received) { + if (strcmp(*line_r, DSYNC_PROXY_SERVER_GREETING_LINE) != 0) { + i_error("Invalid server handshake: %s", *line_r); + dsync_worker_set_failure(&worker->worker); + return -1; + } + worker->handshake_received = TRUE; + return proxy_client_worker_read_line(worker, line_r); + } + return 1; } static void @@ -283,6 +296,7 @@ struct dsync_worker *dsync_worker_init_proxy_client(int fd_in, int fd_out) worker->io = io_add(fd_in, IO_READ, proxy_client_worker_input, worker); worker->input = i_stream_create_fd(fd_in, (size_t)-1, FALSE); worker->output = o_stream_create_fd(fd_out, (size_t)-1, FALSE); + o_stream_send_str(worker->output, DSYNC_PROXY_CLIENT_GREETING_LINE"\n"); /* we'll keep the output corked until flush is needed */ o_stream_cork(worker->output); o_stream_set_flush_callback(worker->output, proxy_client_worker_output, diff --git a/src/dsync/dsync-proxy-server.c b/src/dsync/dsync-proxy-server.c index e828172067..2d27da6681 100644 --- a/src/dsync/dsync-proxy-server.c +++ b/src/dsync/dsync-proxy-server.c @@ -31,7 +31,19 @@ proxy_server_read_line(struct dsync_proxy_server *server, return -1; } } - return *line_r != NULL ? 1 : 0; + if (*line_r == NULL) + return 0; + + if (!server->handshake_received) { + if (strcmp(*line_r, DSYNC_PROXY_CLIENT_GREETING_LINE) != 0) { + i_error("Invalid client handshake: %s", *line_r); + master_service_stop(master_service); + return -1; + } + server->handshake_received = TRUE; + return proxy_server_read_line(server, line_r); + } + return 1; } static int proxy_server_run_cmd(struct dsync_proxy_server *server) @@ -162,6 +174,7 @@ dsync_proxy_server_init(int fd_in, int fd_out, struct dsync_worker *worker) dsync_proxy_server_timeout, NULL); o_stream_set_flush_callback(server->output, proxy_server_output, server); + o_stream_send_str(server->output, DSYNC_PROXY_SERVER_GREETING_LINE"\n"); fd_set_nonblock(fd_in, TRUE); fd_set_nonblock(fd_out, TRUE); return server; diff --git a/src/dsync/dsync-proxy-server.h b/src/dsync/dsync-proxy-server.h index 4958a7c0a6..727ca33532 100644 --- a/src/dsync/dsync-proxy-server.h +++ b/src/dsync/dsync-proxy-server.h @@ -30,6 +30,7 @@ struct dsync_proxy_server { bool get_input_last_lf; uint32_t get_uid; + unsigned int handshake_received:1; unsigned int subs_sending_unsubscriptions:1; unsigned int finished:1; }; diff --git a/src/dsync/dsync-proxy.h b/src/dsync/dsync-proxy.h index e970f6ea44..f05c2355bc 100644 --- a/src/dsync/dsync-proxy.h +++ b/src/dsync/dsync-proxy.h @@ -4,6 +4,8 @@ #include "dsync-data.h" #define DSYNC_PROXY_TIMEOUT_MSECS (15*60*1000) +#define DSYNC_PROXY_CLIENT_GREETING_LINE "dsync-client\t1" +#define DSYNC_PROXY_SERVER_GREETING_LINE "dsync-server\t1" struct dsync_message; struct dsync_mailbox;