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/>.
|
|
|
|
*/
|
|
|
|
|
2016-02-18 17:56:52 +01:00
|
|
|
#include "precompiled.hpp"
|
2013-05-14 10:41:37 +02:00
|
|
|
|
|
|
|
#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_) :
|
2017-08-18 10:04:58 +02:00
|
|
|
mechanism_base_t (session_, options_),
|
2017-08-16 15:48:59 +02:00
|
|
|
zap_client_common_handshake_t (
|
|
|
|
session_, peer_address_, options_, sending_welcome)
|
2013-05-14 10:41:37 +02:00
|
|
|
{
|
2017-09-18 15:24:10 +02:00
|
|
|
// Note that there is no point to PLAIN if ZAP is not set up to handle the
|
|
|
|
// username and password, so if ZAP is not configured it is considered a
|
|
|
|
// failure.
|
2017-10-07 19:34:18 +02:00
|
|
|
// Given this is a backward-incompatible change, it's behind a socket
|
|
|
|
// option disabled by default.
|
|
|
|
if (options.zap_enforce_domain)
|
2018-02-01 11:46:09 +01:00
|
|
|
zmq_assert (zap_required ());
|
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)
|
2017-08-16 15:25:08 +02:00
|
|
|
state = error_sent;
|
2014-05-14 06:23:23 +02:00
|
|
|
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:
|
2017-08-17 18:16:31 +02:00
|
|
|
// 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);
|
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
|
|
|
int zmq::plain_server_t::process_hello (msg_t *msg_)
|
2013-05-14 10:41:37 +02:00
|
|
|
{
|
2017-08-17 18:16:31 +02:00
|
|
|
int rc = check_basic_command_structure (msg_);
|
|
|
|
if (rc == -1)
|
2018-02-01 11:46:09 +01:00
|
|
|
return -1;
|
2017-08-17 18:16:31 +02:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
const unsigned char *ptr = static_cast<unsigned char *> (msg_->data ());
|
2017-08-28 15:03:46 +02:00
|
|
|
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 < 6 || memcmp (ptr, "\x05HELLO", 6)) {
|
2017-08-17 18:16:31 +02:00
|
|
|
session->get_socket ()->event_handshake_failed_protocol (
|
|
|
|
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
|
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) {
|
2017-08-17 18:16:31 +02:00
|
|
|
// PLAIN I: invalid PLAIN client, did not send username
|
|
|
|
session->get_socket ()->event_handshake_failed_protocol (
|
2018-02-01 11:46:09 +01:00
|
|
|
session->get_endpoint (),
|
|
|
|
ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_HELLO);
|
2013-05-14 10:41:37 +02:00
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
2017-08-17 18:16:31 +02:00
|
|
|
const uint8_t username_length = *ptr++;
|
2013-05-14 10:41:37 +02:00
|
|
|
bytes_left -= 1;
|
|
|
|
|
2017-08-28 15:03:46 +02:00
|
|
|
if (bytes_left < username_length) {
|
2017-08-17 18:16:31 +02:00
|
|
|
// PLAIN I: invalid PLAIN client, sent malformed username
|
|
|
|
session->get_socket ()->event_handshake_failed_protocol (
|
2018-02-01 11:46:09 +01:00
|
|
|
session->get_endpoint (),
|
|
|
|
ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_HELLO);
|
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) {
|
2017-08-17 18:16:31 +02:00
|
|
|
// PLAIN I: invalid PLAIN client, did not send password
|
|
|
|
session->get_socket ()->event_handshake_failed_protocol (
|
2018-02-01 11:46:09 +01:00
|
|
|
session->get_endpoint (),
|
|
|
|
ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_HELLO);
|
2013-05-14 10:41:37 +02:00
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-04-29 22:21:58 +02:00
|
|
|
|
2017-08-17 18:16:31 +02:00
|
|
|
const uint8_t password_length = *ptr++;
|
2013-05-14 10:41:37 +02:00
|
|
|
bytes_left -= 1;
|
2017-08-28 15:03:46 +02:00
|
|
|
if (bytes_left < password_length) {
|
2017-08-17 18:16:31 +02:00
|
|
|
// PLAIN I: invalid PLAIN client, sent malformed password
|
|
|
|
session->get_socket ()->event_handshake_failed_protocol (
|
2018-02-01 11:46:09 +01:00
|
|
|
session->get_endpoint (),
|
|
|
|
ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_HELLO);
|
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) {
|
2017-08-17 18:16:31 +02:00
|
|
|
// PLAIN I: invalid PLAIN client, sent extraneous data
|
|
|
|
session->get_socket ()->event_handshake_failed_protocol (
|
2018-02-01 11:46:09 +01:00
|
|
|
session->get_endpoint (),
|
|
|
|
ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_HELLO);
|
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.
|
2017-08-17 18:16:31 +02:00
|
|
|
rc = session->zap_connect ();
|
2017-09-18 15:24:10 +02:00
|
|
|
if (rc != 0) {
|
|
|
|
session->get_socket ()->event_handshake_failed_no_detail (
|
|
|
|
session->get_endpoint (), EFAULT);
|
2017-03-29 23:30:00 +02:00
|
|
|
return -1;
|
2017-09-18 11:48:14 +02:00
|
|
|
}
|
2017-09-18 15:24:10 +02:00
|
|
|
|
2017-08-17 12:54:05 +02:00
|
|
|
send_zap_request (username, password);
|
2017-09-18 15:24:10 +02:00
|
|
|
state = waiting_for_zap_reply;
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
// TODO actually, it is quite unlikely that we can read the ZAP
|
2017-09-18 15:24:10 +02:00
|
|
|
// reply already, but removing this has some strange side-effect
|
2018-02-01 11:46:09 +01:00
|
|
|
// (probably because the pipe's in_active flag is true until a read
|
2017-09-18 15:24:10 +02:00
|
|
|
// is attempted)
|
2017-08-16 17:12:49 +02:00
|
|
|
return receive_and_process_zap_reply () == -1 ? -1 : 0;
|
2013-05-14 10:41:37 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2018-02-01 11:46:09 +01: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)) {
|
2017-08-17 18:16:31 +02:00
|
|
|
session->get_socket ()->event_handshake_failed_protocol (
|
|
|
|
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
|
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
|
|
|
{
|
2017-08-15 19:42:31 +02:00
|
|
|
make_command_with_basic_properties (msg_, "\5READY", 6);
|
2013-05-14 10:41:37 +02:00
|
|
|
|
|
|
|
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);
|
2018-02-01 11:46:09 +01:00
|
|
|
char *msg_data = static_cast<char *> (msg_->data ());
|
2014-05-14 06:23:23 +02:00
|
|
|
memcpy (msg_data, "\5ERROR", 6);
|
2018-05-18 15:54:00 +02:00
|
|
|
msg_data[6] = static_cast<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;
|
|
|
|
}
|
|
|
|
|
2017-08-17 12:54:05 +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
|
|
|
{
|
2017-08-16 12:50:46 +02:00
|
|
|
const uint8_t *credentials[] = {
|
|
|
|
reinterpret_cast<const uint8_t *> (username.c_str ()),
|
|
|
|
reinterpret_cast<const uint8_t *> (password.c_str ())};
|
|
|
|
size_t credentials_sizes[] = {username.size (), password.size ()};
|
2017-08-17 12:54:05 +02:00
|
|
|
zap_client_t::send_zap_request ("PLAIN", 5, credentials, credentials_sizes,
|
|
|
|
2);
|
2013-06-06 13:13:10 +02:00
|
|
|
}
|