2013-09-30 14:13:03 -05:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
2013-09-30 14:13:03 -05:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2013-09-30 14:13:03 -05: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-09-30 14:13:03 -05: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-09-30 14:13:03 -05: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 10:56:52 -06:00
|
|
|
#include "precompiled.hpp"
|
2014-04-25 13:48:39 +09:30
|
|
|
|
|
|
|
#ifdef HAVE_LIBGSSAPI_KRB5
|
|
|
|
|
2013-09-30 14:13:03 -05:00
|
|
|
#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
|
|
|
{
|
2014-04-23 10:20:22 -07:00
|
|
|
const std::string::size_type service_size = options_.gss_service_principal.size();
|
2013-11-21 12:46:23 -06:00
|
|
|
service_name = static_cast <char *>(malloc(service_size+1));
|
|
|
|
assert(service_name);
|
2014-04-23 10:20:22 -07:00
|
|
|
memcpy(service_name, options_.gss_service_principal.c_str(), service_size+1 );
|
2017-04-20 16:28:30 -07:00
|
|
|
service_name_type = convert_nametype (options_.gss_service_principal_nt);
|
2013-11-20 14:01:16 -08:00
|
|
|
|
2013-11-21 12:46:23 -06:00
|
|
|
maj_stat = GSS_S_COMPLETE;
|
2014-04-23 10:20:22 -07:00
|
|
|
if(!options_.gss_principal.empty())
|
2013-11-21 12:46:23 -06:00
|
|
|
{
|
2014-04-23 10:20:22 -07:00
|
|
|
const std::string::size_type principal_size = options_.gss_principal.size();
|
|
|
|
principal_name = static_cast <char *>(malloc(principal_size+1));
|
|
|
|
assert(principal_name);
|
|
|
|
memcpy(principal_name, options_.gss_principal.c_str(), principal_size+1 );
|
2013-11-21 12:46:23 -06:00
|
|
|
|
2017-04-20 16:28:30 -07:00
|
|
|
if (acquire_credentials (principal_name, &cred,
|
|
|
|
options_.gss_principal_nt) != 0)
|
2013-11-21 12:46:23 -06:00
|
|
|
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_);
|
2014-04-25 13:48:39 +09:30
|
|
|
if (rc == 0)
|
2013-11-07 11:49:45 -08:00
|
|
|
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;
|
2014-04-25 13:48:39 +09:30
|
|
|
|
2013-10-08 00:12:50 -05:00
|
|
|
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;
|
2014-04-25 13:48:39 +09:30
|
|
|
|
2013-10-08 00:12:50 -05:00
|
|
|
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);
|
2014-04-25 13:48:39 +09:30
|
|
|
|
2013-10-08 00:12:50 -05:00
|
|
|
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);
|
2014-04-23 11:01:54 -07:00
|
|
|
|
|
|
|
if (do_encryption)
|
|
|
|
return encode_message (msg_);
|
|
|
|
|
|
|
|
return 0;
|
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);
|
2014-04-23 11:01:54 -07:00
|
|
|
|
|
|
|
if (do_encryption)
|
|
|
|
return decode_message (msg_);
|
|
|
|
|
|
|
|
return 0;
|
2013-10-03 15:44:26 -05:00
|
|
|
}
|
|
|
|
|
2014-05-06 17:49:26 +02:00
|
|
|
zmq::mechanism_t::status_t zmq::gssapi_client_t::status () const
|
2013-09-30 14:13:03 -05:00
|
|
|
{
|
2014-05-06 17:49:26 +02:00
|
|
|
return state == connected? mechanism_t::ready: mechanism_t::handshaking;
|
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
|
|
|
{
|
2017-04-19 16:04:41 -07:00
|
|
|
// principal was specified but credentials could not be acquired
|
|
|
|
if (principal_name != NULL && cred == NULL)
|
|
|
|
return -1;
|
|
|
|
|
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;
|
2017-04-19 17:08:14 -07:00
|
|
|
send_tok.length = strlen(service_name) + 1;
|
2013-10-03 13:43:20 -05:00
|
|
|
OM_uint32 maj = gss_import_name(&min_stat, &send_tok,
|
2017-04-20 16:28:30 -07:00
|
|
|
service_name_type,
|
2014-05-05 21:59:11 +02:00
|
|
|
&target_name);
|
2014-04-25 13:48:39 +09:30
|
|
|
|
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);
|
2014-04-25 13:48:39 +09:30
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-25 13:48:39 +09:30
|
|
|
#endif
|