Commit 57546f4e authored by Martin Hurton's avatar Martin Hurton

PLAIN: Implement ERROR handling in server

parent 8651b557
...@@ -36,7 +36,6 @@ zmq::plain_server_t::plain_server_t (session_base_t *session_, ...@@ -36,7 +36,6 @@ zmq::plain_server_t::plain_server_t (session_base_t *session_,
mechanism_t (options_), mechanism_t (options_),
session (session_), session (session_),
peer_address (peer_address_), peer_address (peer_address_),
expecting_zap_reply (false),
state (waiting_for_hello) state (waiting_for_hello)
{ {
} }
...@@ -60,6 +59,11 @@ int zmq::plain_server_t::next_handshake_command (msg_t *msg_) ...@@ -60,6 +59,11 @@ int zmq::plain_server_t::next_handshake_command (msg_t *msg_)
if (rc == 0) if (rc == 0)
state = ready; state = ready;
break; break;
case sending_error:
rc = produce_error (msg_);
if (rc == 0)
state = error_command_sent;
break;
default: default:
errno = EAGAIN; errno = EAGAIN;
rc = -1; rc = -1;
...@@ -74,13 +78,9 @@ int zmq::plain_server_t::process_handshake_command (msg_t *msg_) ...@@ -74,13 +78,9 @@ int zmq::plain_server_t::process_handshake_command (msg_t *msg_)
switch (state) { switch (state) {
case waiting_for_hello: case waiting_for_hello:
rc = process_hello (msg_); rc = process_hello (msg_);
if (rc == 0)
state = expecting_zap_reply? waiting_for_zap_reply: sending_welcome;
break; break;
case waiting_for_initiate: case waiting_for_initiate:
rc = process_initiate (msg_); rc = process_initiate (msg_);
if (rc == 0)
state = sending_ready;
break; break;
default: default:
// Temporary support for security debugging // Temporary support for security debugging
...@@ -100,7 +100,13 @@ int zmq::plain_server_t::process_handshake_command (msg_t *msg_) ...@@ -100,7 +100,13 @@ int zmq::plain_server_t::process_handshake_command (msg_t *msg_)
zmq::mechanism_t::status_t zmq::plain_server_t::status () const zmq::mechanism_t::status_t zmq::plain_server_t::status () const
{ {
return state == ready? mechanism_t::ready: mechanism_t::handshaking; if (state == ready)
return mechanism_t::ready;
else
if (state == error_command_sent)
return mechanism_t::error;
else
return mechanism_t::handshaking;
} }
int zmq::plain_server_t::zap_msg_available () int zmq::plain_server_t::zap_msg_available ()
...@@ -111,7 +117,9 @@ int zmq::plain_server_t::zap_msg_available () ...@@ -111,7 +117,9 @@ int zmq::plain_server_t::zap_msg_available ()
} }
const int rc = receive_and_process_zap_reply (); const int rc = receive_and_process_zap_reply ();
if (rc == 0) if (rc == 0)
state = sending_welcome; state = status_code == "200"
? sending_welcome
: sending_error;
return rc; return rc;
} }
...@@ -178,12 +186,18 @@ int zmq::plain_server_t::process_hello (msg_t *msg_) ...@@ -178,12 +186,18 @@ int zmq::plain_server_t::process_hello (msg_t *msg_)
if (rc == 0) { if (rc == 0) {
send_zap_request (username, password); send_zap_request (username, password);
rc = receive_and_process_zap_reply (); rc = receive_and_process_zap_reply ();
if (rc != 0) { if (rc == 0)
if (errno != EAGAIN) state = status_code == "200"
? sending_welcome
: sending_error;
else
if (errno == EAGAIN)
state = waiting_for_zap_reply;
else
return -1; return -1;
expecting_zap_reply = true;
}
} }
else
state = sending_welcome;
return 0; return 0;
} }
...@@ -207,7 +221,10 @@ int zmq::plain_server_t::process_initiate (msg_t *msg_) ...@@ -207,7 +221,10 @@ int zmq::plain_server_t::process_initiate (msg_t *msg_)
errno = EPROTO; errno = EPROTO;
return -1; return -1;
} }
return parse_metadata (ptr + 9, bytes_left - 9); const int rc = parse_metadata (ptr + 9, bytes_left - 9);
if (rc == 0)
state = sending_ready;
return rc;
} }
int zmq::plain_server_t::produce_ready (msg_t *msg_) const int zmq::plain_server_t::produce_ready (msg_t *msg_) const
...@@ -241,6 +258,18 @@ int zmq::plain_server_t::produce_ready (msg_t *msg_) const ...@@ -241,6 +258,18 @@ int zmq::plain_server_t::produce_ready (msg_t *msg_) const
return 0; return 0;
} }
int zmq::plain_server_t::produce_error (msg_t *msg_) const
{
printf ("producing error\n");
zmq_assert (status_code.length () == 3);
const int rc = msg_->init_size (6 + status_code.length ());
zmq_assert (rc == 0);
char *msg_data = static_cast <char *> (msg_->data ());
memcpy (msg_data, "\5ERROR", 6);
memcpy (msg_data + 6, status_code.c_str (), status_code.length ());
return 0;
}
void zmq::plain_server_t::send_zap_request (const std::string &username, void zmq::plain_server_t::send_zap_request (const std::string &username,
const std::string &password) const std::string &password)
{ {
...@@ -373,7 +402,7 @@ int zmq::plain_server_t::receive_and_process_zap_reply () ...@@ -373,7 +402,7 @@ int zmq::plain_server_t::receive_and_process_zap_reply ()
} }
// Status code frame // Status code frame
if (msg [3].size () != 3 || memcmp (msg [3].data (), "200", 3)) { if (msg [3].size () != 3) {
// Temporary support for security debugging // Temporary support for security debugging
puts ("PLAIN I: ZAP handler rejected client authentication"); puts ("PLAIN I: ZAP handler rejected client authentication");
errno = EACCES; errno = EACCES;
...@@ -381,6 +410,9 @@ int zmq::plain_server_t::receive_and_process_zap_reply () ...@@ -381,6 +410,9 @@ int zmq::plain_server_t::receive_and_process_zap_reply ()
goto error; goto error;
} }
// Save status code
status_code.assign (static_cast <char *> (msg [3].data ()), 3);
// Save user id // Save user id
set_user_id (msg [5].data (), msg [5].size ()); set_user_id (msg [5].data (), msg [5].size ());
......
...@@ -52,6 +52,8 @@ namespace zmq ...@@ -52,6 +52,8 @@ namespace zmq
waiting_for_initiate, waiting_for_initiate,
sending_ready, sending_ready,
waiting_for_zap_reply, waiting_for_zap_reply,
sending_error,
error_command_sent,
ready ready
}; };
...@@ -59,13 +61,14 @@ namespace zmq ...@@ -59,13 +61,14 @@ namespace zmq
const std::string peer_address; const std::string peer_address;
// True iff we are awaiting reply from ZAP reply. // Status code as received from ZAP handler
bool expecting_zap_reply; std::string status_code;
state_t state; state_t state;
int produce_welcome (msg_t *msg_) const; int produce_welcome (msg_t *msg_) const;
int produce_ready (msg_t *msg_) const; int produce_ready (msg_t *msg_) const;
int produce_error (msg_t *msg_) const;
int process_hello (msg_t *msg_); int process_hello (msg_t *msg_);
int process_initiate (msg_t *msg_); int process_initiate (msg_t *msg_);
......
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