Commit ca7eee35 authored by sigiesec's avatar sigiesec

Problem: no ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL events emitted in plain_client_t

Solution: emit events at appropriate places
parent c66ae465
...@@ -35,9 +35,11 @@ ...@@ -35,9 +35,11 @@
#include "msg.hpp" #include "msg.hpp"
#include "err.hpp" #include "err.hpp"
#include "plain_client.hpp" #include "plain_client.hpp"
#include "session_base.hpp"
zmq::plain_client_t::plain_client_t (const options_t &options_) : zmq::plain_client_t::plain_client_t (session_base_t *const session_,
mechanism_t (options_), const options_t &options_) :
mechanism_base_t (session_, options_),
state (sending_hello) state (sending_hello)
{ {
} }
...@@ -84,8 +86,9 @@ int zmq::plain_client_t::process_handshake_command (msg_t *msg_) ...@@ -84,8 +86,9 @@ int zmq::plain_client_t::process_handshake_command (msg_t *msg_)
if (data_size >= 6 && !memcmp (cmd_data, "\5ERROR", 6)) if (data_size >= 6 && !memcmp (cmd_data, "\5ERROR", 6))
rc = process_error (cmd_data, data_size); rc = process_error (cmd_data, data_size);
else { else {
// Temporary support for security debugging // TODO see comment in curve_server_t::process_handshake_command
puts ("PLAIN I: invalid handshake command"); session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNSPECIFIED);
errno = EPROTO; errno = EPROTO;
rc = -1; rc = -1;
} }
...@@ -146,10 +149,15 @@ int zmq::plain_client_t::process_welcome ( ...@@ -146,10 +149,15 @@ int zmq::plain_client_t::process_welcome (
LIBZMQ_UNUSED (cmd_data); LIBZMQ_UNUSED (cmd_data);
if (state != waiting_for_welcome) { if (state != waiting_for_welcome) {
session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
errno = EPROTO; errno = EPROTO;
return -1; return -1;
} }
if (data_size != 8) { if (data_size != 8) {
session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (),
ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_WELCOME);
errno = EPROTO; errno = EPROTO;
return -1; return -1;
} }
...@@ -168,12 +176,18 @@ int zmq::plain_client_t::process_ready ( ...@@ -168,12 +176,18 @@ int zmq::plain_client_t::process_ready (
const unsigned char *cmd_data, size_t data_size) const unsigned char *cmd_data, size_t data_size)
{ {
if (state != waiting_for_ready) { if (state != waiting_for_ready) {
session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
errno = EPROTO; errno = EPROTO;
return -1; return -1;
} }
const int rc = parse_metadata (cmd_data + 6, data_size - 6); const int rc = parse_metadata (cmd_data + 6, data_size - 6);
if (rc == 0) if (rc == 0)
state = ready; state = ready;
else
session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_INVALID_METADATA);
return rc; return rc;
} }
...@@ -181,15 +195,23 @@ int zmq::plain_client_t::process_error ( ...@@ -181,15 +195,23 @@ int zmq::plain_client_t::process_error (
const unsigned char *cmd_data, size_t data_size) const unsigned char *cmd_data, size_t data_size)
{ {
if (state != waiting_for_welcome && state != waiting_for_ready) { 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; errno = EPROTO;
return -1; return -1;
} }
if (data_size < 7) { if (data_size < 7) {
session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (),
ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_ERROR);
errno = EPROTO; errno = EPROTO;
return -1; return -1;
} }
const size_t error_reason_len = static_cast <size_t> (cmd_data [6]); const size_t error_reason_len = static_cast <size_t> (cmd_data [6]);
if (error_reason_len > data_size - 7) { 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; errno = EPROTO;
return -1; return -1;
} }
......
...@@ -38,11 +38,11 @@ namespace zmq ...@@ -38,11 +38,11 @@ namespace zmq
class msg_t; class msg_t;
class plain_client_t : public mechanism_t class plain_client_t : public mechanism_base_t
{ {
public: public:
plain_client_t (session_base_t *const session_,
plain_client_t (const options_t &options_); const options_t &options_);
virtual ~plain_client_t (); virtual ~plain_client_t ();
// mechanism implementation // mechanism implementation
...@@ -50,7 +50,7 @@ namespace zmq ...@@ -50,7 +50,7 @@ namespace zmq
virtual int process_handshake_command (msg_t *msg_); virtual int process_handshake_command (msg_t *msg_);
virtual status_t status () const; virtual status_t status () const;
private: private:
enum state_t { enum state_t {
sending_hello, sending_hello,
......
...@@ -674,7 +674,7 @@ bool zmq::stream_engine_t::handshake () ...@@ -674,7 +674,7 @@ bool zmq::stream_engine_t::handshake ()
plain_server_t (session, peer_address, options); plain_server_t (session, peer_address, options);
else else
mechanism = new (std::nothrow) mechanism = new (std::nothrow)
plain_client_t (options); plain_client_t (session, options);
alloc_assert (mechanism); alloc_assert (mechanism);
} }
#ifdef ZMQ_HAVE_CURVE #ifdef ZMQ_HAVE_CURVE
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment