2013-09-30 14:13:03 -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"
|
|
|
|
#include "gssapi_client.hpp"
|
|
|
|
#include "wire.hpp"
|
|
|
|
|
|
|
|
zmq::gssapi_client_t::gssapi_client_t (const options_t &options_) :
|
2013-11-07 11:49:45 -08:00
|
|
|
gssapi_mechanism_base_t (options_),
|
2013-10-08 00:12:50 -05:00
|
|
|
state (call_next_init),
|
2013-10-03 13:43:20 -05:00
|
|
|
token_ptr (GSS_C_NO_BUFFER),
|
|
|
|
mechs (),
|
|
|
|
security_context_established (false)
|
2013-09-30 14:13:03 -05:00
|
|
|
{
|
2013-11-20 14:01:16 -08:00
|
|
|
const std::string::size_type service_size = options_.gss_service_principle.size();
|
2013-11-21 12:46:23 -06:00
|
|
|
service_name = static_cast <char *>(malloc(service_size+1));
|
|
|
|
assert(service_name);
|
2013-11-20 14:01:16 -08:00
|
|
|
memcpy(service_name, options_.gss_service_principle.c_str(), service_size+1 );
|
|
|
|
|
2013-11-21 12:46:23 -06:00
|
|
|
maj_stat = GSS_S_COMPLETE;
|
|
|
|
if(!options_.gss_principle.empty())
|
|
|
|
{
|
|
|
|
const std::string::size_type principle_size = options_.gss_principle.size();
|
|
|
|
principle_name = static_cast <char *>(malloc(principle_size+1));
|
|
|
|
assert(principle_name);
|
|
|
|
memcpy(principle_name, options_.gss_principle.c_str(), principle_size+1 );
|
|
|
|
|
|
|
|
if (acquire_credentials (principle_name, &cred) != 0)
|
|
|
|
maj_stat = GSS_S_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-10-03 13:43:20 -05:00
|
|
|
mechs.elements = NULL;
|
|
|
|
mechs.count = 0;
|
2013-09-30 14:13:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
zmq::gssapi_client_t::~gssapi_client_t ()
|
|
|
|
{
|
2013-10-03 13:43:20 -05:00
|
|
|
if(service_name)
|
|
|
|
free (service_name);
|
|
|
|
if(cred)
|
|
|
|
gss_release_cred(&min_stat, &cred);
|
2013-09-30 14:13:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::gssapi_client_t::next_handshake_command (msg_t *msg_)
|
|
|
|
{
|
2013-11-07 11:49:45 -08:00
|
|
|
if (state == send_ready) {
|
|
|
|
int rc = produce_ready(msg_);
|
|
|
|
if (rc == 0)
|
|
|
|
state = connected;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-10-08 00:12:50 -05:00
|
|
|
if (state != call_next_init) {
|
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (initialize_context () < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (produce_next_token (msg_) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (maj_stat != GSS_S_CONTINUE_NEEDED && maj_stat != GSS_S_COMPLETE)
|
|
|
|
return -1;
|
|
|
|
|
2013-11-07 11:49:45 -08:00
|
|
|
if (maj_stat == GSS_S_COMPLETE) {
|
2013-10-08 00:12:50 -05:00
|
|
|
security_context_established = true;
|
2013-11-07 11:49:45 -08:00
|
|
|
state = recv_ready;
|
|
|
|
}
|
2013-10-08 00:12:50 -05:00
|
|
|
else
|
|
|
|
state = recv_next_token;
|
|
|
|
|
|
|
|
return 0;
|
2013-09-30 14:13:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::gssapi_client_t::process_handshake_command (msg_t *msg_)
|
|
|
|
{
|
2013-11-07 11:49:45 -08:00
|
|
|
if (state == recv_ready) {
|
|
|
|
int rc = process_ready(msg_);
|
|
|
|
if (rc == 0)
|
|
|
|
state = send_ready;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-10-08 00:12:50 -05:00
|
|
|
if (state != recv_next_token) {
|
|
|
|
errno = EPROTO;
|
|
|
|
return -1;
|
2013-09-30 14:13:03 -05:00
|
|
|
}
|
2013-10-08 00:12:50 -05:00
|
|
|
|
|
|
|
if (process_next_token (msg_) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2013-11-07 11:49:45 -08:00
|
|
|
if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED)
|
2013-10-08 00:12:50 -05:00
|
|
|
return -1;
|
|
|
|
|
2013-11-07 11:49:45 -08:00
|
|
|
state = call_next_init;
|
|
|
|
|
2013-10-08 00:12:50 -05:00
|
|
|
errno_assert (msg_->close () == 0);
|
|
|
|
errno_assert (msg_->init () == 0);
|
|
|
|
|
|
|
|
return 0;
|
2013-09-30 14:13:03 -05:00
|
|
|
}
|
|
|
|
|
2013-10-03 15:44:26 -05:00
|
|
|
int zmq::gssapi_client_t::encode (msg_t *msg_)
|
|
|
|
{
|
2013-10-08 00:12:50 -05:00
|
|
|
zmq_assert (state == connected);
|
|
|
|
return encode_message (msg_);
|
2013-10-03 15:44:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::gssapi_client_t::decode (msg_t *msg_)
|
|
|
|
{
|
2013-10-08 00:12:50 -05:00
|
|
|
zmq_assert (state == connected);
|
|
|
|
return decode_message (msg_);
|
2013-10-03 15:44:26 -05:00
|
|
|
}
|
|
|
|
|
2013-09-30 14:13:03 -05:00
|
|
|
bool zmq::gssapi_client_t::is_handshake_complete () const
|
|
|
|
{
|
2013-10-08 00:12:50 -05:00
|
|
|
return state == connected;
|
2013-09-30 14:13:03 -05:00
|
|
|
}
|
|
|
|
|
2013-10-08 00:12:50 -05:00
|
|
|
int zmq::gssapi_client_t::initialize_context ()
|
2013-09-30 14:13:03 -05:00
|
|
|
{
|
2013-10-03 13:43:20 -05:00
|
|
|
// First time through, import service_name into target_name
|
|
|
|
if (target_name == GSS_C_NO_NAME) {
|
|
|
|
send_tok.value = service_name;
|
|
|
|
send_tok.length = strlen(service_name);
|
|
|
|
OM_uint32 maj = gss_import_name(&min_stat, &send_tok,
|
|
|
|
gss_nt_service_name, &target_name);
|
|
|
|
|
2013-10-08 00:12:50 -05:00
|
|
|
if (maj != GSS_S_COMPLETE)
|
2013-10-03 13:43:20 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2013-09-30 14:13:03 -05:00
|
|
|
|
2013-10-03 13:43:20 -05:00
|
|
|
maj_stat = gss_init_sec_context(&init_sec_min_stat, cred, &context,
|
|
|
|
target_name, mechs.elements,
|
|
|
|
gss_flags, 0, NULL, token_ptr, NULL,
|
|
|
|
&send_tok, &ret_flags, NULL);
|
|
|
|
|
|
|
|
if (token_ptr != GSS_C_NO_BUFFER)
|
|
|
|
free(recv_tok.value);
|
2013-10-08 00:12:50 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::gssapi_client_t::produce_next_token (msg_t *msg_)
|
|
|
|
{
|
|
|
|
if (send_tok.length != 0) { // Server expects another token
|
|
|
|
if (produce_initiate(msg_, send_tok.value, send_tok.length) < 0) {
|
2013-10-03 13:43:20 -05:00
|
|
|
gss_release_buffer(&min_stat, &send_tok);
|
|
|
|
gss_release_name(&min_stat, &target_name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gss_release_buffer(&min_stat, &send_tok);
|
2013-09-30 14:13:03 -05:00
|
|
|
|
2013-10-03 13:43:20 -05:00
|
|
|
if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED) {
|
|
|
|
gss_release_name(&min_stat, &target_name);
|
|
|
|
if (context != GSS_C_NO_CONTEXT)
|
|
|
|
gss_delete_sec_context(&min_stat, &context, GSS_C_NO_BUFFER);
|
2013-09-30 14:13:03 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2013-10-03 13:43:20 -05:00
|
|
|
|
2013-09-30 14:13:03 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-03 13:43:20 -05:00
|
|
|
int zmq::gssapi_client_t::process_next_token (msg_t *msg_)
|
2013-09-30 14:13:03 -05:00
|
|
|
{
|
2013-10-03 13:43:20 -05:00
|
|
|
if (maj_stat == GSS_S_CONTINUE_NEEDED) {
|
2013-10-08 00:12:50 -05:00
|
|
|
if (process_initiate(msg_, &recv_tok.value, recv_tok.length) < 0) {
|
2013-10-03 13:43:20 -05:00
|
|
|
gss_release_name(&min_stat, &target_name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
token_ptr = &recv_tok;
|
2013-09-30 14:13:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|