2019-03-16 02:08:21 +00:00
|
|
|
#pragma once
|
2020-04-16 12:31:57 +00:00
|
|
|
|
2019-03-16 02:08:21 +00:00
|
|
|
#include <Poco/Net/TCPServerConnection.h>
|
2020-03-18 18:54:27 +00:00
|
|
|
#include <common/getFQDNOrHostName.h>
|
2020-02-05 14:06:46 +00:00
|
|
|
#include <Common/CurrentMetrics.h>
|
2019-03-26 18:30:41 +00:00
|
|
|
#include <Core/MySQLProtocol.h>
|
2019-03-16 02:08:21 +00:00
|
|
|
#include "IServer.h"
|
|
|
|
|
2020-04-16 12:31:57 +00:00
|
|
|
#if !defined(ARCADIA_BUILD)
|
|
|
|
# include <Common/config.h>
|
|
|
|
#endif
|
|
|
|
|
2020-05-08 14:11:19 +00:00
|
|
|
#if USE_SSL
|
2020-04-16 12:31:57 +00:00
|
|
|
# include <Poco/Net/SecureStreamSocket.h>
|
2019-11-02 10:20:46 +00:00
|
|
|
#endif
|
2019-03-16 02:08:21 +00:00
|
|
|
|
2020-02-05 14:06:46 +00:00
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
|
|
|
extern const Metric MySQLConnection;
|
|
|
|
}
|
|
|
|
|
2019-03-16 02:08:21 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
/// Handler for MySQL wire protocol connections. Allows to connect to ClickHouse using MySQL client.
|
2019-04-01 09:59:49 +00:00
|
|
|
class MySQLHandler : public Poco::Net::TCPServerConnection
|
|
|
|
{
|
2019-03-16 02:08:21 +00:00
|
|
|
public:
|
2019-11-02 10:20:46 +00:00
|
|
|
MySQLHandler(IServer & server_, const Poco::Net::StreamSocket & socket_, bool ssl_enabled, size_t connection_id_);
|
2019-03-16 02:08:21 +00:00
|
|
|
|
|
|
|
void run() final;
|
|
|
|
|
2019-04-22 10:57:50 +00:00
|
|
|
private:
|
2020-02-05 14:06:46 +00:00
|
|
|
CurrentMetrics::Increment metric_increment{CurrentMetrics::MySQLConnection};
|
|
|
|
|
2019-04-29 06:05:30 +00:00
|
|
|
/// Enables SSL, if client requested.
|
2020-08-13 08:17:33 +00:00
|
|
|
void finishHandshake(MySQLProtocol::ConnectionPhase::HandshakeResponse &);
|
2019-04-29 06:05:30 +00:00
|
|
|
|
2019-07-14 22:13:56 +00:00
|
|
|
void comQuery(ReadBuffer & payload);
|
2019-03-16 02:08:21 +00:00
|
|
|
|
2019-07-14 22:13:56 +00:00
|
|
|
void comFieldList(ReadBuffer & payload);
|
2019-03-16 02:08:21 +00:00
|
|
|
|
|
|
|
void comPing();
|
|
|
|
|
2019-07-14 22:13:56 +00:00
|
|
|
void comInitDB(ReadBuffer & payload);
|
2019-04-01 09:59:49 +00:00
|
|
|
|
2019-07-28 13:12:26 +00:00
|
|
|
void authenticate(const String & user_name, const String & auth_plugin_name, const String & auth_response);
|
2019-04-22 10:57:50 +00:00
|
|
|
|
2019-11-02 10:20:46 +00:00
|
|
|
virtual void authPluginSSL();
|
2020-08-13 08:17:33 +00:00
|
|
|
virtual void finishHandshakeSSL(size_t packet_size, char * buf, size_t pos, std::function<void(size_t)> read_bytes, MySQLProtocol::ConnectionPhase::HandshakeResponse & packet);
|
2019-11-02 10:20:46 +00:00
|
|
|
|
2019-03-16 02:08:21 +00:00
|
|
|
IServer & server;
|
2019-11-02 10:20:46 +00:00
|
|
|
|
|
|
|
protected:
|
2019-03-16 02:08:21 +00:00
|
|
|
Poco::Logger * log;
|
2019-11-02 10:20:46 +00:00
|
|
|
|
2019-03-16 02:08:21 +00:00
|
|
|
Context connection_context;
|
|
|
|
|
2020-08-13 08:17:33 +00:00
|
|
|
std::shared_ptr<MySQLProtocol::PacketEndpoint> packet_sender;
|
2019-03-16 02:08:21 +00:00
|
|
|
|
2019-11-02 10:20:46 +00:00
|
|
|
private:
|
2019-05-26 06:52:29 +00:00
|
|
|
size_t connection_id = 0;
|
2019-03-16 02:08:21 +00:00
|
|
|
|
2019-06-16 18:12:14 +00:00
|
|
|
size_t server_capability_flags = 0;
|
|
|
|
size_t client_capability_flags = 0;
|
2019-03-16 02:08:21 +00:00
|
|
|
|
2019-11-02 10:20:46 +00:00
|
|
|
protected:
|
2019-07-28 13:12:26 +00:00
|
|
|
std::unique_ptr<MySQLProtocol::Authentication::IPlugin> auth_plugin;
|
|
|
|
|
2019-04-29 06:05:30 +00:00
|
|
|
std::shared_ptr<ReadBuffer> in;
|
|
|
|
std::shared_ptr<WriteBuffer> out;
|
|
|
|
|
|
|
|
bool secure_connection = false;
|
2019-12-02 11:32:45 +00:00
|
|
|
|
|
|
|
private:
|
2020-07-06 02:07:38 +00:00
|
|
|
using ReplacementFn = std::function<String(const String & query)>;
|
|
|
|
using Replacements = std::unordered_map<std::string, ReplacementFn>;
|
|
|
|
Replacements replacements;
|
2019-03-16 02:08:21 +00:00
|
|
|
};
|
|
|
|
|
2020-05-08 14:11:19 +00:00
|
|
|
#if USE_SSL
|
2019-11-02 10:20:46 +00:00
|
|
|
class MySQLHandlerSSL : public MySQLHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MySQLHandlerSSL(IServer & server_, const Poco::Net::StreamSocket & socket_, bool ssl_enabled, size_t connection_id_, RSA & public_key_, RSA & private_key_);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void authPluginSSL() override;
|
2020-08-13 08:17:33 +00:00
|
|
|
|
|
|
|
void finishHandshakeSSL(
|
|
|
|
size_t packet_size, char * buf, size_t pos,
|
|
|
|
std::function<void(size_t)> read_bytes, MySQLProtocol::ConnectionPhase::HandshakeResponse & packet) override;
|
2019-11-02 10:20:46 +00:00
|
|
|
|
|
|
|
RSA & public_key;
|
|
|
|
RSA & private_key;
|
|
|
|
std::shared_ptr<Poco::Net::SecureStreamSocket> ss;
|
|
|
|
};
|
2019-09-11 11:21:54 +00:00
|
|
|
#endif
|
2019-11-02 10:20:46 +00:00
|
|
|
|
|
|
|
}
|