gssapi_server.cpp 9.86 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
5

6 7 8
    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
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    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.
25 26 27 28 29

    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/>.
*/

30
#include "precompiled.hpp"
Chris Laws's avatar
Chris Laws committed
31 32 33

#ifdef HAVE_LIBGSSAPI_KRB5

34 35 36 37 38 39
#include <string.h>
#include <string>

#include "msg.hpp"
#include "session_base.hpp"
#include "err.hpp"
40
#include "gssapi_server.hpp"
41 42
#include "wire.hpp"

43 44
#include <gssapi/gssapi.h>

45
zmq::gssapi_server_t::gssapi_server_t (session_base_t *session_,
46 47
                                       const std::string &peer_address_,
                                       const options_t &options_) :
48
    gssapi_mechanism_base_t (options_),
49 50
    session (session_),
    peer_address (peer_address_),
51 52
    state (recv_next_token),
    security_context_established (false)
53
{
Chris Busbey's avatar
Chris Busbey committed
54
    maj_stat = GSS_S_CONTINUE_NEEDED;
Chris Busbey's avatar
Chris Busbey committed
55
    if(!options_.gss_principal.empty())
Chris Busbey's avatar
Chris Busbey committed
56
    {
Chris Busbey's avatar
Chris Busbey committed
57 58 59 60
        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 );
Chris Busbey's avatar
Chris Busbey committed
61

Chris Busbey's avatar
Chris Busbey committed
62
        if (acquire_credentials (principal_name, &cred) != 0)
Chris Busbey's avatar
Chris Busbey committed
63 64
            maj_stat = GSS_S_FAILURE;
    }
65 66
}

67
zmq::gssapi_server_t::~gssapi_server_t ()
68
{
69 70
    if(cred)
        gss_release_cred(&min_stat, &cred);
Chris Busbey's avatar
Chris Busbey committed
71 72 73

    if(target_name)
        gss_release_name(&min_stat, &target_name);
74 75
}

76
int zmq::gssapi_server_t::next_handshake_command (msg_t *msg_)
77
{
78 79 80 81 82 83 84 85
    if (state == send_ready) {
        int rc = produce_ready(msg_);
        if (rc == 0)
            state = recv_ready;

        return rc;
    }

86 87 88 89 90 91 92 93 94 95 96 97 98
    if (state != send_next_token) {
        errno = EAGAIN;
        return -1;
    }

    if (produce_next_token (msg_) < 0)
        return -1;

    if (maj_stat != GSS_S_CONTINUE_NEEDED && maj_stat != GSS_S_COMPLETE)
        return -1;

    if (maj_stat == GSS_S_COMPLETE) {
        security_context_established = true;
99
    }
100

101 102
    state = recv_next_token;

103
    return 0;
104 105
}

106
int zmq::gssapi_server_t::process_handshake_command (msg_t *msg_)
107
{
108 109
    if (state == recv_ready) {
        int rc = process_ready(msg_);
Chris Laws's avatar
Chris Laws committed
110
        if (rc == 0)
111 112 113 114 115
            state = connected;

        return rc;
    }

116 117 118
    if (state != recv_next_token) {
        errno = EPROTO;
        return -1;
119
    }
120

121
    if (security_context_established) {
122
        //  Use ZAP protocol (RFC 27) to authenticate the user.
123 124 125 126 127 128 129 130 131 132 133 134
        bool expecting_zap_reply = false;
        int rc = session->zap_connect ();
        if (rc == 0) {
            send_zap_request();
            rc = receive_and_process_zap_reply ();
            if (rc != 0) {
                if (errno != EAGAIN)
                    return -1;
                expecting_zap_reply = true;
            }
        }
        state = expecting_zap_reply? expect_zap_reply: send_ready;
135 136 137
        return 0;
    }

138 139 140 141 142 143 144 145 146 147
    if (process_next_token (msg_) < 0)
        return -1;

    accept_context ();
    state = send_next_token;

    errno_assert (msg_->close () == 0);
    errno_assert (msg_->init () == 0);

    return 0;
148 149
}

Chris Laws's avatar
Chris Laws committed
150
void zmq::gssapi_server_t::send_zap_request ()
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
{
    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);

Chris Busbey's avatar
Chris Busbey committed
210 211 212
    //  Principal frame
    gss_buffer_desc principal;
    gss_display_name(&min_stat, target_name, &principal, NULL);
213

Chris Busbey's avatar
Chris Busbey committed
214
    rc = msg.init_size (principal.length);
215
    errno_assert (rc == 0);
Chris Busbey's avatar
Chris Busbey committed
216
    memcpy (msg.data (), principal.value, principal.length);
217 218
    rc = session->write_zap_msg (&msg);
    errno_assert (rc == 0);
Chris Busbey's avatar
Chris Busbey committed
219
    gss_release_buffer(&min_stat, &principal);
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
}

int zmq::gssapi_server_t::receive_and_process_zap_reply ()
{
    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;
    }

275 276 277
    //  Save user id
    set_user_id (msg [5].data (), msg [5].size ());

278 279
    //  Process metadata frame
    rc = parse_metadata (static_cast <const unsigned char*> (msg [6].data ()),
280
                         msg [6].size (), true);
281 282 283 284 285 286 287 288 289 290 291

error:
    for (int i = 0; i < 7; i++) {
        const int rc2 = msg [i].close ();
        errno_assert (rc2 == 0);
    }

    return rc;
}


292 293
int zmq::gssapi_server_t::encode (msg_t *msg_)
{
294
    zmq_assert (state == connected);
295 296 297 298 299

    if (do_encryption)
      return encode_message (msg_);

    return 0;
300 301 302 303
}

int zmq::gssapi_server_t::decode (msg_t *msg_)
{
304
    zmq_assert (state == connected);
305 306 307 308 309

    if (do_encryption)
      return decode_message (msg_);

    return 0;
310 311
}

312
int zmq::gssapi_server_t::zap_msg_available ()
313
{
314 315 316 317 318 319 320 321
    if (state != expect_zap_reply) {
        errno = EFSM;
        return -1;
    }
    const int rc = receive_and_process_zap_reply ();
    if (rc == 0)
        state = send_ready;
    return rc;
322 323
}

Martin Hurton's avatar
Martin Hurton committed
324
zmq::mechanism_t::status_t zmq::gssapi_server_t::status () const
325
{
Martin Hurton's avatar
Martin Hurton committed
326
    return state == connected? mechanism_t::ready: mechanism_t::handshaking;
327 328
}

329
int zmq::gssapi_server_t::produce_next_token (msg_t *msg_)
330
{
331 332
    if (send_tok.length != 0) { // Client expects another token
        if (produce_initiate(msg_, send_tok.value, send_tok.length) < 0)
333 334 335
            return -1;
        gss_release_buffer(&min_stat, &send_tok);
    }
336

337 338 339 340
    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);
341 342 343 344 345 346
        return -1;
    }

    return 0;
}

347
int zmq::gssapi_server_t::process_next_token (msg_t *msg_)
348
{
349
    if (maj_stat == GSS_S_CONTINUE_NEEDED) {
350
        if (process_initiate(msg_, &recv_tok.value, recv_tok.length) < 0) {
351 352 353
            if (target_name != GSS_C_NO_NAME)
                gss_release_name(&min_stat, &target_name);
            return -1;
354 355
        }
    }
356 357 358 359 360 361

    return 0;
}

void zmq::gssapi_server_t::accept_context ()
{
362 363 364 365
    maj_stat = gss_accept_sec_context(&init_sec_min_stat, &context, cred,
                                      &recv_tok, GSS_C_NO_CHANNEL_BINDINGS,
                                      &target_name, &doid, &send_tok,
                                      &ret_flags, NULL, NULL);
366

367 368 369
    if (recv_tok.value) {
        free (recv_tok.value);
        recv_tok.value = NULL;
370 371
    }
}
372

Chris Laws's avatar
Chris Laws committed
373
#endif