plain_server.cpp 8.52 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"
31 32 33 34

#include <string>

#include "msg.hpp"
35
#include "session_base.hpp"
36
#include "err.hpp"
37
#include "plain_server.hpp"
38 39
#include "wire.hpp"

40 41 42
zmq::plain_server_t::plain_server_t (session_base_t *session_,
                                     const std::string &peer_address_,
                                     const options_t &options_) :
43
    mechanism_base_t (session_, options_),
44 45
    zap_client_common_handshake_t (
      session_, peer_address_, options_, sending_welcome)
46
{
47 48 49
    //  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.
50 51 52
    //  Given this is a backward-incompatible change, it's behind a socket
    //  option disabled by default.
    if (options.zap_enforce_domain)
53
        zmq_assert (zap_required ());
54 55
}

56
zmq::plain_server_t::~plain_server_t ()
57 58 59
{
}

60
int zmq::plain_server_t::next_handshake_command (msg_t *msg_)
61 62 63 64
{
    int rc = 0;

    switch (state) {
65
        case sending_welcome:
66
            rc = produce_welcome (msg_);
67 68 69 70
            if (rc == 0)
                state = waiting_for_initiate;
            break;
        case sending_ready:
71
            rc = produce_ready (msg_);
72 73 74
            if (rc == 0)
                state = ready;
            break;
75 76 77
        case sending_error:
            rc = produce_error (msg_);
            if (rc == 0)
78
                state = error_sent;
79
            break;
80 81 82
        default:
            errno = EAGAIN;
            rc = -1;
83 84 85 86
    }
    return rc;
}

87
int zmq::plain_server_t::process_handshake_command (msg_t *msg_)
88 89 90 91
{
    int rc = 0;

    switch (state) {
92
        case waiting_for_hello:
93
            rc = process_hello (msg_);
94 95
            break;
        case waiting_for_initiate:
96
            rc = process_initiate (msg_);
97 98
            break;
        default:
99 100 101
            //  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);
102
            errno = EPROTO;
103
            rc = -1;
104
            break;
105 106 107 108 109 110 111
    }
    if (rc == 0) {
        rc = msg_->close ();
        errno_assert (rc == 0);
        rc = msg_->init ();
        errno_assert (rc == 0);
    }
112
    return rc;
113 114
}

115
int zmq::plain_server_t::process_hello (msg_t *msg_)
116
{
117 118
    int rc = check_basic_command_structure (msg_);
    if (rc == -1)
119
        return -1;
120

121
    const unsigned char *ptr = static_cast<unsigned char *> (msg_->data ());
122
    size_t bytes_left = msg_->size ();
123

124
    if (bytes_left < 6 || memcmp (ptr, "\x05HELLO", 6)) {
125 126
        session->get_socket ()->event_handshake_failed_protocol (
          session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
127 128 129
        errno = EPROTO;
        return -1;
    }
130 131
    ptr += 6;
    bytes_left -= 6;
132 133

    if (bytes_left < 1) {
134 135
        //  PLAIN I: invalid PLAIN client, did not send username
        session->get_socket ()->event_handshake_failed_protocol (
136 137
          session->get_endpoint (),
          ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_HELLO);
138 139 140
        errno = EPROTO;
        return -1;
    }
141
    const uint8_t username_length = *ptr++;
142 143
    bytes_left -= 1;

144
    if (bytes_left < username_length) {
145 146
        //  PLAIN I: invalid PLAIN client, sent malformed username
        session->get_socket ()->event_handshake_failed_protocol (
147 148
          session->get_endpoint (),
          ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_HELLO);
149 150 151 152 153 154 155
        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) {
156 157
        //  PLAIN I: invalid PLAIN client, did not send password
        session->get_socket ()->event_handshake_failed_protocol (
158 159
          session->get_endpoint (),
          ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_HELLO);
160 161 162
        errno = EPROTO;
        return -1;
    }
163

164
    const uint8_t password_length = *ptr++;
165
    bytes_left -= 1;
166
    if (bytes_left < password_length) {
167 168
        //  PLAIN I: invalid PLAIN client, sent malformed password
        session->get_socket ()->event_handshake_failed_protocol (
169 170
          session->get_endpoint (),
          ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_HELLO);
171 172 173
        errno = EPROTO;
        return -1;
    }
174

175 176 177 178
    const std::string password = std::string ((char *) ptr, password_length);
    ptr += password_length;
    bytes_left -= password_length;
    if (bytes_left > 0) {
179 180
        //  PLAIN I: invalid PLAIN client, sent extraneous data
        session->get_socket ()->event_handshake_failed_protocol (
181 182
          session->get_endpoint (),
          ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_HELLO);
183 184 185
        errno = EPROTO;
        return -1;
    }
186

187
    //  Use ZAP protocol (RFC 27) to authenticate the user.
188
    rc = session->zap_connect ();
189 190 191
    if (rc != 0) {
        session->get_socket ()->event_handshake_failed_no_detail (
          session->get_endpoint (), EFAULT);
192
        return -1;
193
    }
194

195
    send_zap_request (username, password);
196 197
    state = waiting_for_zap_reply;

198
    //  TODO actually, it is quite unlikely that we can read the ZAP
199
    //  reply already, but removing this has some strange side-effect
200
    //  (probably because the pipe's in_active flag is true until a read
201
    //  is attempted)
202
    return receive_and_process_zap_reply () == -1 ? -1 : 0;
203 204
}

205
int zmq::plain_server_t::produce_welcome (msg_t *msg_) const
206 207 208
{
    const int rc = msg_->init_size (8);
    errno_assert (rc == 0);
209
    memcpy (msg_->data (), "\x07WELCOME", 8);
210 211 212
    return 0;
}

213
int zmq::plain_server_t::process_initiate (msg_t *msg_)
214
{
215
    const unsigned char *ptr = static_cast<unsigned char *> (msg_->data ());
216
    const size_t bytes_left = msg_->size ();
217

218
    if (bytes_left < 9 || memcmp (ptr, "\x08INITIATE", 9)) {
219 220
        session->get_socket ()->event_handshake_failed_protocol (
          session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
221 222 223
        errno = EPROTO;
        return -1;
    }
224 225 226 227
    const int rc = parse_metadata (ptr + 9, bytes_left - 9);
    if (rc == 0)
        state = sending_ready;
    return rc;
228 229
}

230
int zmq::plain_server_t::produce_ready (msg_t *msg_) const
231
{
232
    make_command_with_basic_properties (msg_, "\5READY", 6);
233 234 235 236

    return 0;
}

237 238 239
int zmq::plain_server_t::produce_error (msg_t *msg_) const
{
    zmq_assert (status_code.length () == 3);
240
    const int rc = msg_->init_size (6 + 1 + status_code.length ());
241
    zmq_assert (rc == 0);
242
    char *msg_data = static_cast<char *> (msg_->data ());
243
    memcpy (msg_data, "\5ERROR", 6);
244
    msg_data[6] = static_cast<char> (status_code.length ());
245
    memcpy (msg_data + 7, status_code.c_str (), status_code.length ());
246 247 248
    return 0;
}

249 250
void zmq::plain_server_t::send_zap_request (const std::string &username,
                                            const std::string &password)
251
{
252 253 254 255
    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 ()};
256 257
    zap_client_t::send_zap_request ("PLAIN", 5, credentials, credentials_sizes,
                                    2);
258
}