1#pragma once
2#include <Common/config.h>
3#include <Poco/Net/TCPServerConnection.h>
4#include <Common/getFQDNOrHostName.h>
5#include <Core/MySQLProtocol.h>
6#include "IServer.h"
7
8#if USE_POCO_NETSSL
9#include <Poco/Net/SecureStreamSocket.h>
10#endif
11
12namespace DB
13{
14/// Handler for MySQL wire protocol connections. Allows to connect to ClickHouse using MySQL client.
15class MySQLHandler : public Poco::Net::TCPServerConnection
16{
17public:
18 MySQLHandler(IServer & server_, const Poco::Net::StreamSocket & socket_, bool ssl_enabled, size_t connection_id_);
19
20 void run() final;
21
22private:
23 /// Enables SSL, if client requested.
24 void finishHandshake(MySQLProtocol::HandshakeResponse &);
25
26 void comQuery(ReadBuffer & payload);
27
28 void comFieldList(ReadBuffer & payload);
29
30 void comPing();
31
32 void comInitDB(ReadBuffer & payload);
33
34 void authenticate(const String & user_name, const String & auth_plugin_name, const String & auth_response);
35
36 virtual void authPluginSSL();
37 virtual void finishHandshakeSSL(size_t packet_size, char * buf, size_t pos, std::function<void(size_t)> read_bytes, MySQLProtocol::HandshakeResponse & packet);
38
39 IServer & server;
40
41protected:
42 Poco::Logger * log;
43
44 Context connection_context;
45
46 std::shared_ptr<MySQLProtocol::PacketSender> packet_sender;
47
48private:
49 size_t connection_id = 0;
50
51 size_t server_capability_flags = 0;
52 size_t client_capability_flags = 0;
53
54protected:
55 std::unique_ptr<MySQLProtocol::Authentication::IPlugin> auth_plugin;
56
57 std::shared_ptr<ReadBuffer> in;
58 std::shared_ptr<WriteBuffer> out;
59
60 bool secure_connection = false;
61
62private:
63 static const String show_table_status_replacement_query;
64};
65
66#if USE_SSL && USE_POCO_NETSSL
67class MySQLHandlerSSL : public MySQLHandler
68{
69public:
70 MySQLHandlerSSL(IServer & server_, const Poco::Net::StreamSocket & socket_, bool ssl_enabled, size_t connection_id_, RSA & public_key_, RSA & private_key_);
71
72private:
73 void authPluginSSL() override;
74 void finishHandshakeSSL(size_t packet_size, char * buf, size_t pos, std::function<void(size_t)> read_bytes, MySQLProtocol::HandshakeResponse & packet) override;
75
76 RSA & public_key;
77 RSA & private_key;
78 std::shared_ptr<Poco::Net::SecureStreamSocket> ss;
79};
80#endif
81
82}
83