2013-05-14 10:41:37 +02:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
2013-05-14 10:41:37 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2013-05-14 10:41:37 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
libzmq is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License (LGPL) as published
|
|
|
|
by the Free Software Foundation; either version 3 of the License, or
|
2013-05-14 10:41:37 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
As a special exception, the Contributors give you permission to link
|
|
|
|
this library with independent modules to produce an executable,
|
|
|
|
regardless of the license terms of these independent modules, and to
|
|
|
|
copy and distribute the resulting executable under terms of your choice,
|
|
|
|
provided that you also meet, for each linked independent module, the
|
|
|
|
terms and conditions of the license of that module. An independent
|
|
|
|
module is a module which is not derived from or based on this library.
|
|
|
|
If you modify this library, you must extend this exception to your
|
|
|
|
version of the library.
|
|
|
|
|
|
|
|
libzmq is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
2013-05-14 10:41:37 +02:00
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "platform.hpp"
|
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
|
|
|
#include "windows.hpp"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "msg.hpp"
|
2013-06-06 13:13:10 +02:00
|
|
|
#include "session_base.hpp"
|
2013-05-14 10:41:37 +02:00
|
|
|
#include "err.hpp"
|
2014-05-12 06:08:28 +02:00
|
|
|
#include "plain_server.hpp"
|
2013-05-14 10:41:37 +02:00
|
|
|
#include "wire.hpp"
|
|
|
|
|
2014-05-12 06:08:28 +02:00
|
|
|
zmq::plain_server_t::plain_server_t (session_base_t *session_,
|
|
|
|
const std::string &peer_address_,
|
|
|
|
const options_t &options_) :
|
2013-05-14 10:41:37 +02:00
|
|
|
mechanism_t (options_),
|
2013-06-06 13:13:10 +02:00
|
|
|
session (session_),
|
2013-07-18 09:49:42 +02:00
|
|
|
peer_address (peer_address_),
|
2014-05-12 06:08:28 +02:00
|
|
|
state (waiting_for_hello)
|
2013-05-14 10:41:37 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-12 06:08:28 +02:00
|
|
|
zmq::plain_server_t::~plain_server_t ()
|
2013-05-14 10:41:37 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-12 06:08:28 +02:00
|
|
|
int zmq::plain_server_t::next_handshake_command (msg_t *msg_)
|
2013-05-14 10:41:37 +02:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
switch (state) {
|
2013-05-17 17:46:30 +02:00
|
|
|
case sending_welcome:
|
2013-09-04 17:59:45 +02:00
|
|
|
rc = produce_welcome (msg_);
|
2013-05-17 17:46:30 +02:00
|
|
|
if (rc == 0)
|
|
|
|
state = waiting_for_initiate;
|
|
|
|
break;
|
|
|
|
case sending_ready:
|
2013-09-04 17:59:45 +02:00
|
|
|
rc = produce_ready (msg_);
|
2013-05-17 17:46:30 +02:00
|
|
|
if (rc == 0)
|
|
|
|
state = ready;
|
|
|
|
break;
|
2014-05-14 06:23:23 +02:00
|
|
|
case sending_error:
|
|
|
|
rc = produce_error (msg_);
|
|
|
|
if (rc == 0)
|
|
|
|
state = error_command_sent;
|
|
|
|
break;
|
2013-05-17 17:46:30 +02:00
|
|
|
default:
|
|
|
|
errno = EAGAIN;
|
|
|
|
rc = -1;
|
2013-05-14 10:41:37 +02:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-05-12 06:08:28 +02:00
|
|
|
int zmq::plain_server_t::process_handshake_command (msg_t *msg_)
|
2013-05-14 10:41:37 +02:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
switch (state) {
|
2013-05-17 17:46:30 +02:00
|
|
|
case waiting_for_hello:
|
2013-09-04 17:59:45 +02:00
|
|
|
rc = process_hello (msg_);
|
2013-05-17 17:46:30 +02:00
|
|
|
break;
|
|
|
|
case waiting_for_initiate:
|
2013-09-04 17:59:45 +02:00
|
|
|
rc = process_initiate (msg_);
|
2013-05-17 17:46:30 +02:00
|
|
|
break;
|
|
|
|
default:
|
2014-04-29 22:21:58 +02:00
|
|
|
// Temporary support for security debugging
|
|
|
|
puts ("PLAIN I: invalid handshake command");
|
2013-06-22 08:11:55 +02:00
|
|
|
errno = EPROTO;
|
2013-05-17 17:46:30 +02:00
|
|
|
rc = -1;
|
2013-06-22 08:11:55 +02:00
|
|
|
break;
|
2013-05-14 10:41:37 +02:00
|
|
|
}
|
|
|
|
if (rc == 0) {
|
|
|
|
rc = msg_->close ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
rc = msg_->init ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
}
|
2013-06-06 13:13:10 +02:00
|
|
|
return rc;
|
2013-05-14 10:41:37 +02:00
|
|
|
}
|
|
|
|
|
2014-05-12 06:08:28 +02:00
|
|
|
zmq::mechanism_t::status_t zmq::plain_server_t::status () const
|
2013-05-14 10:41:37 +02:00
|
|
|
{
|
2014-05-14 06:23:23 +02:00
|
|
|
if (state == ready)
|
|
|
|
return mechanism_t::ready;
|
|
|
|
else
|
|
|
|
if (state == error_command_sent)
|
|
|
|
return mechanism_t::error;
|
|
|
|
else
|
|
|
|
return mechanism_t::handshaking;
|
2013-05-14 10:41:37 +02:00
|
|
|
}
|
|
|
|
|
2014-05-12 06:08:28 +02:00
|
|
|
int zmq::plain_server_t::zap_msg_available ()
|
2013-06-06 13:13:10 +02:00
|
|
|
{
|
|
|
|
if (state != waiting_for_zap_reply) {
|
|
|
|
errno = EFSM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const int rc = receive_and_process_zap_reply ();
|
|
|
|
if (rc == 0)
|
2014-05-14 06:23:23 +02:00
|
|
|
state = status_code == "200"
|
|
|
|
? sending_welcome
|
|
|
|
: sending_error;
|
2013-06-06 13:13:10 +02:00
|
|
|
return rc;
|
|
|
|
}
|
2013-05-17 17:46:30 +02:00
|
|
|
|
2014-05-12 06:08:28 +02:00
|
|
|
int zmq::plain_server_t::process_hello (msg_t *msg_)
|
2013-05-14 10:41:37 +02:00
|
|
|
{
|
|
|
|
const unsigned char *ptr = static_cast <unsigned char *> (msg_->data ());
|
|
|
|
size_t bytes_left = msg_->size ();
|
|
|
|
|
2013-09-04 17:59:45 +02:00
|
|
|
if (bytes_left < 6 || memcmp (ptr, "\x05HELLO", 6)) {
|
2014-04-29 22:21:58 +02:00
|
|
|
// Temporary support for security debugging
|
|
|
|
puts ("PLAIN I: invalid PLAIN client, did not send HELLO");
|
2013-05-14 10:41:37 +02:00
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-09-04 17:59:45 +02:00
|
|
|
ptr += 6;
|
|
|
|
bytes_left -= 6;
|
2013-05-14 10:41:37 +02:00
|
|
|
|
|
|
|
if (bytes_left < 1) {
|
2014-04-29 22:21:58 +02:00
|
|
|
// Temporary support for security debugging
|
|
|
|
puts ("PLAIN I: invalid PLAIN client, did not send username");
|
2013-05-14 10:41:37 +02:00
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-06-06 11:00:41 +02:00
|
|
|
const size_t username_length = static_cast <size_t> (*ptr++);
|
2013-05-14 10:41:37 +02:00
|
|
|
bytes_left -= 1;
|
|
|
|
|
|
|
|
if (bytes_left < username_length) {
|
2014-04-29 22:21:58 +02:00
|
|
|
// Temporary support for security debugging
|
|
|
|
puts ("PLAIN I: invalid PLAIN client, sent malformed username");
|
2013-05-14 10:41:37 +02:00
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const std::string username = std::string ((char *) ptr, username_length);
|
|
|
|
ptr += username_length;
|
|
|
|
bytes_left -= username_length;
|
|
|
|
if (bytes_left < 1) {
|
2014-04-29 22:21:58 +02:00
|
|
|
// Temporary support for security debugging
|
|
|
|
puts ("PLAIN I: invalid PLAIN client, did not send password");
|
2013-05-14 10:41:37 +02:00
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-04-29 22:21:58 +02:00
|
|
|
|
2013-06-06 11:00:41 +02:00
|
|
|
const size_t password_length = static_cast <size_t> (*ptr++);
|
2013-05-14 10:41:37 +02:00
|
|
|
bytes_left -= 1;
|
|
|
|
if (bytes_left < password_length) {
|
2014-04-29 22:21:58 +02:00
|
|
|
// Temporary support for security debugging
|
|
|
|
puts ("PLAIN I: invalid PLAIN client, sent malformed password");
|
2013-05-14 10:41:37 +02:00
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-04-29 22:21:58 +02:00
|
|
|
|
2013-05-14 10:41:37 +02:00
|
|
|
const std::string password = std::string ((char *) ptr, password_length);
|
|
|
|
ptr += password_length;
|
|
|
|
bytes_left -= password_length;
|
|
|
|
if (bytes_left > 0) {
|
2014-04-29 22:21:58 +02:00
|
|
|
// Temporary support for security debugging
|
|
|
|
puts ("PLAIN I: invalid PLAIN client, sent extraneous data");
|
2013-05-14 10:41:37 +02:00
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-06-06 13:13:10 +02:00
|
|
|
|
2013-06-22 16:05:34 +02:00
|
|
|
// Use ZAP protocol (RFC 27) to authenticate the user.
|
2013-06-06 13:13:10 +02:00
|
|
|
int rc = session->zap_connect ();
|
2013-06-22 16:05:34 +02:00
|
|
|
if (rc == 0) {
|
|
|
|
send_zap_request (username, password);
|
|
|
|
rc = receive_and_process_zap_reply ();
|
2014-05-14 06:23:23 +02:00
|
|
|
if (rc == 0)
|
|
|
|
state = status_code == "200"
|
|
|
|
? sending_welcome
|
|
|
|
: sending_error;
|
|
|
|
else
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
state = waiting_for_zap_reply;
|
|
|
|
else
|
|
|
|
return -1;
|
2013-06-06 13:13:10 +02:00
|
|
|
}
|
2014-05-14 06:23:23 +02:00
|
|
|
else
|
|
|
|
state = sending_welcome;
|
2013-06-06 13:13:10 +02:00
|
|
|
|
2013-05-14 10:41:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-12 06:08:28 +02:00
|
|
|
int zmq::plain_server_t::produce_welcome (msg_t *msg_) const
|
2013-05-14 10:41:37 +02:00
|
|
|
{
|
|
|
|
const int rc = msg_->init_size (8);
|
|
|
|
errno_assert (rc == 0);
|
2013-09-04 17:59:45 +02:00
|
|
|
memcpy (msg_->data (), "\x07WELCOME", 8);
|
2013-05-14 10:41:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-12 06:08:28 +02:00
|
|
|
int zmq::plain_server_t::process_initiate (msg_t *msg_)
|
2013-05-14 10:41:37 +02:00
|
|
|
{
|
|
|
|
const unsigned char *ptr = static_cast <unsigned char *> (msg_->data ());
|
2014-05-12 06:08:28 +02:00
|
|
|
const size_t bytes_left = msg_->size ();
|
2013-05-14 10:41:37 +02:00
|
|
|
|
2013-09-04 17:59:45 +02:00
|
|
|
if (bytes_left < 9 || memcmp (ptr, "\x08INITIATE", 9)) {
|
2014-04-29 22:21:58 +02:00
|
|
|
// Temporary support for security debugging
|
|
|
|
puts ("PLAIN I: invalid PLAIN client, did not send INITIATE");
|
2013-05-14 10:41:37 +02:00
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-14 06:23:23 +02:00
|
|
|
const int rc = parse_metadata (ptr + 9, bytes_left - 9);
|
|
|
|
if (rc == 0)
|
|
|
|
state = sending_ready;
|
|
|
|
return rc;
|
2013-05-14 10:41:37 +02:00
|
|
|
}
|
|
|
|
|
2014-05-12 06:08:28 +02:00
|
|
|
int zmq::plain_server_t::produce_ready (msg_t *msg_) const
|
2013-05-14 10:41:37 +02:00
|
|
|
{
|
|
|
|
unsigned char * const command_buffer = (unsigned char *) malloc (512);
|
|
|
|
alloc_assert (command_buffer);
|
|
|
|
|
|
|
|
unsigned char *ptr = command_buffer;
|
|
|
|
|
2013-06-28 11:42:54 +02:00
|
|
|
// Add command name
|
2013-09-04 17:59:45 +02:00
|
|
|
memcpy (ptr, "\x05READY", 6);
|
2013-06-28 11:42:54 +02:00
|
|
|
ptr += 6;
|
2013-05-14 10:41:37 +02:00
|
|
|
|
|
|
|
// Add socket type property
|
|
|
|
const char *socket_type = socket_type_string (options.type);
|
|
|
|
ptr += add_property (ptr, "Socket-Type", socket_type, strlen (socket_type));
|
|
|
|
|
|
|
|
// Add identity property
|
|
|
|
if (options.type == ZMQ_REQ
|
|
|
|
|| options.type == ZMQ_DEALER
|
2014-05-02 22:19:30 +02:00
|
|
|
|| options.type == ZMQ_ROUTER)
|
2014-05-12 06:08:28 +02:00
|
|
|
ptr += add_property (
|
|
|
|
ptr, "Identity", options.identity, options.identity_size);
|
2013-05-14 10:41:37 +02:00
|
|
|
|
|
|
|
const size_t command_size = ptr - command_buffer;
|
|
|
|
const int rc = msg_->init_size (command_size);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
memcpy (msg_->data (), command_buffer, command_size);
|
|
|
|
free (command_buffer);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-14 06:23:23 +02:00
|
|
|
int zmq::plain_server_t::produce_error (msg_t *msg_) const
|
|
|
|
{
|
|
|
|
zmq_assert (status_code.length () == 3);
|
2014-05-14 14:12:04 +02:00
|
|
|
const int rc = msg_->init_size (6 + 1 + status_code.length ());
|
2014-05-14 06:23:23 +02:00
|
|
|
zmq_assert (rc == 0);
|
|
|
|
char *msg_data = static_cast <char *> (msg_->data ());
|
|
|
|
memcpy (msg_data, "\5ERROR", 6);
|
2015-04-22 07:26:32 +02:00
|
|
|
msg_data [6] = (char) status_code.length ();
|
2014-05-14 14:12:04 +02:00
|
|
|
memcpy (msg_data + 7, status_code.c_str (), status_code.length ());
|
2014-05-14 06:23:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-12 06:08:28 +02:00
|
|
|
void zmq::plain_server_t::send_zap_request (const std::string &username,
|
|
|
|
const std::string &password)
|
2013-06-22 16:05:34 +02:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
msg_t msg;
|
|
|
|
|
|
|
|
// Address delimiter frame
|
|
|
|
rc = msg.init ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
msg.set_flags (msg_t::more);
|
|
|
|
rc = session->write_zap_msg (&msg);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
|
|
|
|
// Version frame
|
|
|
|
rc = msg.init_size (3);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
memcpy (msg.data (), "1.0", 3);
|
|
|
|
msg.set_flags (msg_t::more);
|
|
|
|
rc = session->write_zap_msg (&msg);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
|
2013-07-18 09:49:42 +02:00
|
|
|
// Request id frame
|
2013-06-22 16:05:34 +02:00
|
|
|
rc = msg.init_size (1);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
memcpy (msg.data (), "1", 1);
|
|
|
|
msg.set_flags (msg_t::more);
|
|
|
|
rc = session->write_zap_msg (&msg);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
|
|
|
|
// Domain frame
|
2013-09-09 20:40:34 +02:00
|
|
|
rc = msg.init_size (options.zap_domain.length ());
|
2013-06-22 16:05:34 +02:00
|
|
|
errno_assert (rc == 0);
|
2013-09-09 20:40:34 +02:00
|
|
|
memcpy (msg.data (), options.zap_domain.c_str (), options.zap_domain.length ());
|
2013-06-22 16:05:34 +02:00
|
|
|
msg.set_flags (msg_t::more);
|
|
|
|
rc = session->write_zap_msg (&msg);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
|
2013-07-18 09:49:42 +02:00
|
|
|
// Address frame
|
|
|
|
rc = msg.init_size (peer_address.length ());
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
memcpy (msg.data (), peer_address.c_str (), peer_address.length ());
|
|
|
|
msg.set_flags (msg_t::more);
|
|
|
|
rc = session->write_zap_msg (&msg);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
|
2013-09-09 20:40:34 +02:00
|
|
|
// Identity frame
|
2013-08-20 19:48:05 +02:00
|
|
|
rc = msg.init_size (options.identity_size);
|
2013-09-09 20:40:34 +02:00
|
|
|
errno_assert (rc == 0);
|
2013-08-20 19:48:05 +02:00
|
|
|
memcpy (msg.data (), options.identity, options.identity_size);
|
|
|
|
msg.set_flags (msg_t::more);
|
|
|
|
rc = session->write_zap_msg (&msg);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
|
2013-06-22 16:05:34 +02:00
|
|
|
// Mechanism frame
|
|
|
|
rc = msg.init_size (5);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
memcpy (msg.data (), "PLAIN", 5);
|
|
|
|
msg.set_flags (msg_t::more);
|
|
|
|
rc = session->write_zap_msg (&msg);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
|
|
|
|
// Username frame
|
|
|
|
rc = msg.init_size (username.length ());
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
memcpy (msg.data (), username.c_str (), username.length ());
|
|
|
|
msg.set_flags (msg_t::more);
|
|
|
|
rc = session->write_zap_msg (&msg);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
|
|
|
|
// Password frame
|
|
|
|
rc = msg.init_size (password.length ());
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
memcpy (msg.data (), password.c_str (), password.length ());
|
|
|
|
rc = session->write_zap_msg (&msg);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
2014-05-12 06:08:28 +02:00
|
|
|
int zmq::plain_server_t::receive_and_process_zap_reply ()
|
2013-06-06 13:13:10 +02:00
|
|
|
{
|
|
|
|
int rc = 0;
|
2013-07-18 09:49:42 +02:00
|
|
|
msg_t msg [7]; // ZAP reply consists of 7 frames
|
2013-06-06 13:13:10 +02:00
|
|
|
|
2013-07-18 09:49:42 +02:00
|
|
|
// Initialize all reply frames
|
|
|
|
for (int i = 0; i < 7; i++) {
|
2013-06-06 13:13:10 +02:00
|
|
|
rc = msg [i].init ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
2013-07-18 09:49:42 +02:00
|
|
|
for (int i = 0; i < 7; i++) {
|
2013-06-06 13:13:10 +02:00
|
|
|
rc = session->read_zap_msg (&msg [i]);
|
|
|
|
if (rc == -1)
|
|
|
|
break;
|
2013-07-18 09:49:42 +02:00
|
|
|
if ((msg [i].flags () & msg_t::more) == (i < 6? 0: msg_t::more)) {
|
2014-04-29 22:21:58 +02:00
|
|
|
// Temporary support for security debugging
|
|
|
|
puts ("PLAIN I: ZAP handler sent incomplete reply message");
|
2013-06-06 13:13:10 +02:00
|
|
|
errno = EPROTO;
|
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc != 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
// Address delimiter frame
|
|
|
|
if (msg [0].size () > 0) {
|
2014-04-29 22:21:58 +02:00
|
|
|
// Temporary support for security debugging
|
|
|
|
puts ("PLAIN I: ZAP handler sent malformed reply message");
|
2013-06-06 13:13:10 +02:00
|
|
|
errno = EPROTO;
|
2014-04-29 22:21:58 +02:00
|
|
|
rc = -1;
|
2013-06-06 13:13:10 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version frame
|
|
|
|
if (msg [1].size () != 3 || memcmp (msg [1].data (), "1.0", 3)) {
|
2014-04-29 22:21:58 +02:00
|
|
|
// Temporary support for security debugging
|
|
|
|
puts ("PLAIN I: ZAP handler sent bad version number");
|
2013-06-06 13:13:10 +02:00
|
|
|
errno = EPROTO;
|
2014-04-29 22:21:58 +02:00
|
|
|
rc = -1;
|
2013-06-06 13:13:10 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2013-07-18 09:49:42 +02:00
|
|
|
// Request id frame
|
2013-06-06 13:13:10 +02:00
|
|
|
if (msg [2].size () != 1 || memcmp (msg [2].data (), "1", 1)) {
|
2014-04-29 22:21:58 +02:00
|
|
|
// Temporary support for security debugging
|
|
|
|
puts ("PLAIN I: ZAP handler sent bad request ID");
|
2013-09-02 18:21:36 +02:00
|
|
|
rc = -1;
|
2013-06-06 13:13:10 +02:00
|
|
|
errno = EPROTO;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status code frame
|
2014-05-14 06:23:23 +02:00
|
|
|
if (msg [3].size () != 3) {
|
2014-04-29 22:21:58 +02:00
|
|
|
// Temporary support for security debugging
|
|
|
|
puts ("PLAIN I: ZAP handler rejected client authentication");
|
2013-06-06 13:13:10 +02:00
|
|
|
errno = EACCES;
|
2014-04-29 22:21:58 +02:00
|
|
|
rc = -1;
|
2013-06-06 13:13:10 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2014-05-14 06:23:23 +02:00
|
|
|
// Save status code
|
|
|
|
status_code.assign (static_cast <char *> (msg [3].data ()), 3);
|
|
|
|
|
2014-01-12 21:58:36 +01:00
|
|
|
// Save user id
|
|
|
|
set_user_id (msg [5].data (), msg [5].size ());
|
|
|
|
|
2013-07-18 09:49:42 +02:00
|
|
|
// Process metadata frame
|
|
|
|
rc = parse_metadata (static_cast <const unsigned char*> (msg [6].data ()),
|
2014-04-30 14:17:38 +02:00
|
|
|
msg [6].size (), true);
|
2013-07-18 09:49:42 +02:00
|
|
|
|
2013-06-06 13:13:10 +02:00
|
|
|
error:
|
2013-07-18 09:49:42 +02:00
|
|
|
for (int i = 0; i < 7; i++) {
|
2013-06-06 13:13:10 +02:00
|
|
|
const int rc2 = msg [i].close ();
|
|
|
|
errno_assert (rc2 == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|