2013-09-24 22:20:01 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file
|
|
|
|
|
|
|
|
This file is part of 0MQ.
|
|
|
|
|
|
|
|
0MQ is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
0MQ 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.
|
|
|
|
|
|
|
|
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.h>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "msg.hpp"
|
|
|
|
#include "session_base.hpp"
|
|
|
|
#include "err.hpp"
|
2013-09-30 14:13:03 -05:00
|
|
|
#include "gssapi_server.hpp"
|
2013-09-24 22:20:01 -05:00
|
|
|
#include "wire.hpp"
|
|
|
|
|
2013-09-30 14:13:03 -05:00
|
|
|
zmq::gssapi_server_t::gssapi_server_t (session_base_t *session_,
|
2013-09-24 22:20:01 -05:00
|
|
|
const std::string &peer_address_,
|
|
|
|
const options_t &options_) :
|
|
|
|
mechanism_t (options_),
|
|
|
|
session (session_),
|
|
|
|
peer_address (peer_address_),
|
|
|
|
expecting_zap_reply (false),
|
2013-09-27 10:34:53 -05:00
|
|
|
expecting_another_token (true),
|
2013-09-30 14:13:03 -05:00
|
|
|
state (waiting_for_hello)
|
2013-09-24 22:20:01 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-09-30 14:13:03 -05:00
|
|
|
zmq::gssapi_server_t::~gssapi_server_t ()
|
2013-09-24 22:20:01 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-09-30 14:13:03 -05:00
|
|
|
int zmq::gssapi_server_t::next_handshake_command (msg_t *msg_)
|
2013-09-24 22:20:01 -05:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case sending_welcome:
|
|
|
|
rc = produce_welcome (msg_);
|
|
|
|
if (rc == 0)
|
|
|
|
state = waiting_for_initiate;
|
|
|
|
break;
|
2013-09-27 10:34:53 -05:00
|
|
|
case sending_token:
|
|
|
|
rc = produce_token (msg_);
|
|
|
|
if (rc == 0)
|
2013-09-30 14:13:03 -05:00
|
|
|
state = waiting_for_token; //state = expecting_another_token? waiting_for_token: waiting_for_ready;
|
2013-09-24 22:20:01 -05:00
|
|
|
break;
|
|
|
|
case sending_ready:
|
|
|
|
rc = produce_ready (msg_);
|
|
|
|
if (rc == 0)
|
|
|
|
state = ready;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errno = EAGAIN;
|
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-09-30 14:13:03 -05:00
|
|
|
int zmq::gssapi_server_t::process_handshake_command (msg_t *msg_)
|
2013-09-24 22:20:01 -05:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case waiting_for_hello:
|
|
|
|
rc = process_hello (msg_);
|
|
|
|
if (rc == 0)
|
|
|
|
state = expecting_zap_reply? waiting_for_zap_reply: sending_welcome;
|
|
|
|
break;
|
|
|
|
case waiting_for_initiate:
|
|
|
|
rc = process_initiate (msg_);
|
|
|
|
if (rc == 0)
|
2013-09-27 10:34:53 -05:00
|
|
|
state = sending_token;
|
|
|
|
break;
|
|
|
|
case waiting_for_token:
|
|
|
|
rc = process_token (msg_);
|
|
|
|
if (rc == 0)
|
|
|
|
state = sending_ready; // state = expecting_another_token? sending_token: sending_ready;
|
2013-09-24 22:20:01 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errno = EPROTO;
|
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (rc == 0) {
|
|
|
|
rc = msg_->close ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
rc = msg_->init ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-09-30 14:13:03 -05:00
|
|
|
bool zmq::gssapi_server_t::is_handshake_complete () const
|
2013-09-24 22:20:01 -05:00
|
|
|
{
|
|
|
|
return state == ready;
|
|
|
|
}
|
|
|
|
|
2013-09-30 14:13:03 -05:00
|
|
|
int zmq::gssapi_server_t::zap_msg_available ()
|
2013-09-24 22:20:01 -05:00
|
|
|
{
|
|
|
|
if (state != waiting_for_zap_reply) {
|
|
|
|
errno = EFSM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const int rc = receive_and_process_zap_reply ();
|
|
|
|
if (rc == 0)
|
|
|
|
state = sending_welcome;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-09-30 14:13:03 -05:00
|
|
|
int zmq::gssapi_server_t::process_hello (msg_t *msg_)
|
2013-09-24 22:20:01 -05:00
|
|
|
{
|
|
|
|
const unsigned char *ptr = static_cast <unsigned char *> (msg_->data ());
|
|
|
|
size_t bytes_left = msg_->size ();
|
|
|
|
|
|
|
|
if (bytes_left < 6 || memcmp (ptr, "\x05HELLO", 6)) {
|
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ptr += 6;
|
|
|
|
bytes_left -= 6;
|
|
|
|
|
|
|
|
if (bytes_left < 1) {
|
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const size_t username_length = static_cast <size_t> (*ptr++);
|
|
|
|
bytes_left -= 1;
|
|
|
|
|
|
|
|
if (bytes_left < username_length) {
|
|
|
|
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) {
|
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const size_t password_length = static_cast <size_t> (*ptr++);
|
|
|
|
bytes_left -= 1;
|
|
|
|
|
|
|
|
if (bytes_left < password_length) {
|
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const std::string password = std::string ((char *) ptr, password_length);
|
|
|
|
ptr += password_length;
|
|
|
|
bytes_left -= password_length;
|
|
|
|
|
|
|
|
if (bytes_left > 0) {
|
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use ZAP protocol (RFC 27) to authenticate the user.
|
|
|
|
int rc = session->zap_connect ();
|
|
|
|
if (rc == 0) {
|
|
|
|
send_zap_request (username, password);
|
|
|
|
rc = receive_and_process_zap_reply ();
|
|
|
|
if (rc != 0) {
|
|
|
|
if (errno != EAGAIN)
|
|
|
|
return -1;
|
|
|
|
expecting_zap_reply = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-30 14:13:03 -05:00
|
|
|
int zmq::gssapi_server_t::produce_welcome (msg_t *msg_) const
|
2013-09-24 22:20:01 -05:00
|
|
|
{
|
|
|
|
const int rc = msg_->init_size (8);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
memcpy (msg_->data (), "\x07WELCOME", 8);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-30 14:13:03 -05:00
|
|
|
int zmq::gssapi_server_t::process_initiate (msg_t *msg_)
|
2013-09-24 22:20:01 -05:00
|
|
|
{
|
|
|
|
const unsigned char *ptr = static_cast <unsigned char *> (msg_->data ());
|
|
|
|
size_t bytes_left = msg_->size ();
|
|
|
|
|
|
|
|
if (bytes_left < 9 || memcmp (ptr, "\x08INITIATE", 9)) {
|
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ptr += 9;
|
|
|
|
bytes_left -= 9;
|
|
|
|
return parse_metadata (ptr, bytes_left);
|
|
|
|
}
|
|
|
|
|
2013-09-30 14:13:03 -05:00
|
|
|
int zmq::gssapi_server_t::produce_token (msg_t *msg_) const
|
2013-09-27 10:34:53 -05:00
|
|
|
{
|
|
|
|
unsigned char * const command_buffer = (unsigned char *) malloc (512);
|
|
|
|
alloc_assert (command_buffer);
|
|
|
|
|
|
|
|
unsigned char *ptr = command_buffer;
|
|
|
|
|
|
|
|
// Add command name
|
|
|
|
memcpy (ptr, "\x05TOKEN", 6);
|
|
|
|
ptr += 6;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|| options.type == ZMQ_ROUTER) {
|
|
|
|
ptr += add_property (ptr, "Identity",
|
|
|
|
options.identity, options.identity_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-09-30 14:13:03 -05:00
|
|
|
int zmq::gssapi_server_t::process_token (msg_t *msg_)
|
2013-09-27 10:34:53 -05:00
|
|
|
{
|
|
|
|
const unsigned char *ptr = static_cast <unsigned char *> (msg_->data ());
|
|
|
|
size_t bytes_left = msg_->size ();
|
|
|
|
|
|
|
|
if (bytes_left < 6 || memcmp (ptr, "\x05TOKEN", 6)) {
|
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ptr += 6;
|
|
|
|
bytes_left -= 6;
|
|
|
|
|
|
|
|
expecting_another_token = false;
|
|
|
|
|
|
|
|
return parse_metadata (ptr, bytes_left);
|
|
|
|
}
|
|
|
|
|
2013-09-30 14:13:03 -05:00
|
|
|
int zmq::gssapi_server_t::produce_ready (msg_t *msg_) const
|
2013-09-24 22:20:01 -05:00
|
|
|
{
|
|
|
|
unsigned char * const command_buffer = (unsigned char *) malloc (512);
|
|
|
|
alloc_assert (command_buffer);
|
|
|
|
|
|
|
|
unsigned char *ptr = command_buffer;
|
|
|
|
|
|
|
|
// Add command name
|
|
|
|
memcpy (ptr, "\x05READY", 6);
|
|
|
|
ptr += 6;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|| options.type == ZMQ_ROUTER) {
|
|
|
|
ptr += add_property (ptr, "Identity",
|
|
|
|
options.identity, options.identity_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-09-30 14:13:03 -05:00
|
|
|
void zmq::gssapi_server_t::send_zap_request (const std::string &username,
|
2013-09-24 22:20:01 -05:00
|
|
|
const std::string &password)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Request id frame
|
|
|
|
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
|
|
|
|
rc = msg.init_size (options.zap_domain.length ());
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
memcpy (msg.data (), options.zap_domain.c_str (), options.zap_domain.length ());
|
|
|
|
msg.set_flags (msg_t::more);
|
|
|
|
rc = session->write_zap_msg (&msg);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// Identity frame
|
|
|
|
rc = msg.init_size (options.identity_size);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
memcpy (msg.data (), options.identity, options.identity_size);
|
|
|
|
msg.set_flags (msg_t::more);
|
|
|
|
rc = session->write_zap_msg (&msg);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
|
|
|
|
// Mechanism frame
|
|
|
|
rc = msg.init_size (6);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
memcpy (msg.data (), "GSSAPI", 6);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-09-30 14:13:03 -05:00
|
|
|
int zmq::gssapi_server_t::receive_and_process_zap_reply ()
|
2013-09-24 22:20:01 -05:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
msg_t msg [7]; // ZAP reply consists of 7 frames
|
|
|
|
|
|
|
|
// Initialize all reply frames
|
|
|
|
for (int i = 0; i < 7; i++) {
|
|
|
|
rc = msg [i].init ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 7; i++) {
|
|
|
|
rc = session->read_zap_msg (&msg [i]);
|
|
|
|
if (rc == -1)
|
|
|
|
break;
|
|
|
|
if ((msg [i].flags () & msg_t::more) == (i < 6? 0: msg_t::more)) {
|
|
|
|
errno = EPROTO;
|
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc != 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
// Address delimiter frame
|
|
|
|
if (msg [0].size () > 0) {
|
|
|
|
rc = -1;
|
|
|
|
errno = EPROTO;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version frame
|
|
|
|
if (msg [1].size () != 3 || memcmp (msg [1].data (), "1.0", 3)) {
|
|
|
|
rc = -1;
|
|
|
|
errno = EPROTO;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request id frame
|
|
|
|
if (msg [2].size () != 1 || memcmp (msg [2].data (), "1", 1)) {
|
|
|
|
rc = -1;
|
|
|
|
errno = EPROTO;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status code frame
|
|
|
|
if (msg [3].size () != 3 || memcmp (msg [3].data (), "200", 3)) {
|
|
|
|
rc = -1;
|
|
|
|
errno = EACCES;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process metadata frame
|
|
|
|
rc = parse_metadata (static_cast <const unsigned char*> (msg [6].data ()),
|
|
|
|
msg [6].size ());
|
|
|
|
|
|
|
|
error:
|
|
|
|
for (int i = 0; i < 7; i++) {
|
|
|
|
const int rc2 = msg [i].close ();
|
|
|
|
errno_assert (rc2 == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|