From ca7eee357e6e1bc76e8f0f38ae37cc14f70ab0ef Mon Sep 17 00:00:00 2001 From: sigiesec Date: Fri, 18 Aug 2017 10:15:44 +0200 Subject: [PATCH] Problem: no ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL events emitted in plain_client_t Solution: emit events at appropriate places --- src/plain_client.cpp | 30 ++++++++++++++++++++++++++---- src/plain_client.hpp | 10 +++++----- src/stream_engine.cpp | 2 +- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/plain_client.cpp b/src/plain_client.cpp index 59ffec15..43918cab 100644 --- a/src/plain_client.cpp +++ b/src/plain_client.cpp @@ -35,9 +35,11 @@ #include "msg.hpp" #include "err.hpp" #include "plain_client.hpp" +#include "session_base.hpp" -zmq::plain_client_t::plain_client_t (const options_t &options_) : - mechanism_t (options_), +zmq::plain_client_t::plain_client_t (session_base_t *const session_, + const options_t &options_) : + mechanism_base_t (session_, options_), state (sending_hello) { } @@ -84,8 +86,9 @@ int zmq::plain_client_t::process_handshake_command (msg_t *msg_) if (data_size >= 6 && !memcmp (cmd_data, "\5ERROR", 6)) rc = process_error (cmd_data, data_size); else { - // Temporary support for security debugging - puts ("PLAIN I: invalid handshake command"); + // TODO see comment in curve_server_t::process_handshake_command + session->get_socket ()->event_handshake_failed_protocol ( + session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNSPECIFIED); errno = EPROTO; rc = -1; } @@ -146,10 +149,15 @@ int zmq::plain_client_t::process_welcome ( LIBZMQ_UNUSED (cmd_data); if (state != waiting_for_welcome) { + session->get_socket ()->event_handshake_failed_protocol ( + session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND); errno = EPROTO; return -1; } if (data_size != 8) { + session->get_socket ()->event_handshake_failed_protocol ( + session->get_endpoint (), + ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_WELCOME); errno = EPROTO; return -1; } @@ -168,12 +176,18 @@ int zmq::plain_client_t::process_ready ( const unsigned char *cmd_data, size_t data_size) { if (state != waiting_for_ready) { + session->get_socket ()->event_handshake_failed_protocol ( + session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND); errno = EPROTO; return -1; } const int rc = parse_metadata (cmd_data + 6, data_size - 6); if (rc == 0) state = ready; + else + session->get_socket ()->event_handshake_failed_protocol ( + session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_INVALID_METADATA); + return rc; } @@ -181,15 +195,23 @@ int zmq::plain_client_t::process_error ( const unsigned char *cmd_data, size_t data_size) { if (state != waiting_for_welcome && state != waiting_for_ready) { + session->get_socket ()->event_handshake_failed_protocol ( + session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND); errno = EPROTO; return -1; } if (data_size < 7) { + session->get_socket ()->event_handshake_failed_protocol ( + session->get_endpoint (), + ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_ERROR); errno = EPROTO; return -1; } const size_t error_reason_len = static_cast (cmd_data [6]); if (error_reason_len > data_size - 7) { + session->get_socket ()->event_handshake_failed_protocol ( + session->get_endpoint (), + ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_ERROR); errno = EPROTO; return -1; } diff --git a/src/plain_client.hpp b/src/plain_client.hpp index b6e42b4d..6e5b5586 100644 --- a/src/plain_client.hpp +++ b/src/plain_client.hpp @@ -38,11 +38,11 @@ namespace zmq class msg_t; - class plain_client_t : public mechanism_t + class plain_client_t : public mechanism_base_t { - public: - - plain_client_t (const options_t &options_); + public: + plain_client_t (session_base_t *const session_, + const options_t &options_); virtual ~plain_client_t (); // mechanism implementation @@ -50,7 +50,7 @@ namespace zmq virtual int process_handshake_command (msg_t *msg_); virtual status_t status () const; - private: + private: enum state_t { sending_hello, diff --git a/src/stream_engine.cpp b/src/stream_engine.cpp index 80f0e487..0a7b9961 100644 --- a/src/stream_engine.cpp +++ b/src/stream_engine.cpp @@ -674,7 +674,7 @@ bool zmq::stream_engine_t::handshake () plain_server_t (session, peer_address, options); else mechanism = new (std::nothrow) - plain_client_t (options); + plain_client_t (session, options); alloc_assert (mechanism); } #ifdef ZMQ_HAVE_CURVE