forked from mariadb-corporation/MaxScale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.hh
426 lines (358 loc) · 12.4 KB
/
listener.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
210
211
212
213
214
215
216
217
218
219
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
* Copyright (c) 2018 MariaDB Corporation Ab
* Copyright (c) 2023 MariaDB plc, Finnish Branch
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2025-09-12
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#pragma once
#include <maxscale/ccdefs.hh>
#include <unordered_map>
#include <maxscale/authenticator.hh>
#include <maxscale/buffer.hh>
#include <maxscale/protocol/mariadb/query_classifier.hh>
#include <maxscale/ssl.hh>
#include <maxscale/workerlocal.hh>
class SERVICE;
namespace maxscale
{
class ProtocolModule;
/**
* Increment the number of authentication failures from the remote address. If the number
* exceeds the configured limit, future attempts to connect from the remote are be rejected.
*
* @param remote The address where the connection originated
*/
void mark_auth_as_failed(const std::string& remote);
/**
* Listener settings and other data that is shared with all sessions created by the listener.
* Should be referred to with shared_ptr.
*
* The contents should not change once a session with the data has been created, as this could
* create concurrency issues. If listener settings are changed, the listener should create a new
* shared data object and share that with new sessions. The old sessions will keep using the
* previous settings.
*/
class ListenerData
{
public:
using SProtocol = std::unique_ptr<mxs::ProtocolModule>;
using SAuthenticator = std::unique_ptr<mxs::AuthenticatorModule>;
struct ConnectionInitSql
{
ConnectionInitSql() = default;
ConnectionInitSql(const ConnectionInitSql& rhs) = default;
std::vector<std::string> queries;
GWBUF buffer_contents;
};
struct UserCreds
{
std::string password;
std::string plugin;
};
struct MappingInfo
{
std::unordered_map<std::string, std::string> user_map; /**< user -> user */
std::unordered_map<std::string, std::string> group_map; /**< Linux group -> user */
std::unordered_map<std::string, UserCreds> credentials; /**< user -> plugin & pw */
};
using SMappingInfo = std::unique_ptr<const MappingInfo>;
ListenerData(SSLContext ssl, qc_sql_mode_t default_sql_mode, SERVICE* service,
SProtocol protocol_module, const std::string& listener_name,
std::vector<SAuthenticator>&& authenticators, ConnectionInitSql&& init_sql,
SMappingInfo mapping);
ListenerData(const ListenerData&) = delete;
ListenerData& operator=(const ListenerData&) = delete;
const SSLContext m_ssl; /**< SSL settings */
const qc_sql_mode_t m_default_sql_mode; /**< Default sql mode for the listener */
SERVICE& m_service; /**< The service the listener feeds */
const SProtocol m_proto_module; /**< Protocol module */
const std::string m_listener_name; /**< Name of the owning listener */
/**
* Authenticator modules used by the sessions created from the listener. The session will select
* an authenticator module during authentication.
*/
const std::vector<SAuthenticator> m_authenticators;
/** Connection init sql queries. Only used by MariaDB-protocol module .*/
const ConnectionInitSql m_conn_init_sql;
const std::unique_ptr<const MappingInfo> m_mapping_info; /**< Backend user mapping and passwords */
};
}
// TODO: Move inside MaxScale namespace.
/**
* The Listener class is used to link a network port to a service. It defines the name of the
* protocol module that should be loaded as well as the authenticator that is used.
*/
class Listener : public mxb::Pollable
{
public:
using SData = std::shared_ptr<const mxs::ListenerData>;
enum class Type
{
UNIX_SOCKET, // UNIX domain socket shared between workers
SHARED_TCP, // TCP listening socket shared between workers
UNIQUE_TCP, // Unique TCP listening socket for each worker
};
struct Config : public mxs::config::Configuration
{
Config(const std::string& name, Listener* listener);
std::string type;
const MXS_MODULE* protocol;
std::string authenticator;
std::string authenticator_options;
std::string address;
std::string socket;
int64_t port;
SERVICE* service;
qc_sql_mode_t sql_mode;
std::string connection_init_sql_file;
std::string user_mapping_file;
// TLS configuration parameters
bool ssl;
std::string ssl_cert;
std::string ssl_key;
std::string ssl_ca;
std::string ssl_cipher;
std::string ssl_crl;
int64_t ssl_cert_verify_depth;
bool ssl_verify_peer_certificate;
bool ssl_verify_peer_host;
uint32_t ssl_version;
bool configure(const mxs::ConfigParameters& params,
mxs::ConfigParameters* pUnrecognized = nullptr) override final;
bool configure(json_t* json, std::set<std::string>* pUnrecognized = nullptr) override final;
protected:
bool post_configure(const std::map<std::string, mxs::ConfigParameters>& nested_params) override;
Listener* m_listener;
};
virtual ~Listener() override;
/**
* Create a new listener
*
* @param name Name of the listener
* @param params Parameters for the listener
*
* @return New listener or nullptr on error
*/
static std::shared_ptr<Listener> create(const std::string& name, const mxs::ConfigParameters& params);
static std::shared_ptr<Listener> create(const std::string& name, json_t* params);
/**
* Destroy a listener
*
* This removes the listener from the global list of active listeners. Once destroyed, the port used
* by a listener is open for immediate reuse.
*
* @param listener Listener to destroy
*/
static void destroy(const std::shared_ptr<Listener>& listener);
// Stop all listeners
static void stop_all();
/**
* Reloads TLS certificates for all listeners
*
* @return True if certificate reload succeeded on all listeners
*/
static bool reload_tls();
/**
* Get listener config
*/
mxs::config::Configuration& config()
{
return m_config;
}
/**
* Start listening on the configured port
*
* @return True if the listener was able to start listening
*/
bool listen();
/**
* Stop the listener
*
* @return True if the listener was successfully stopped
*/
bool stop();
/**
* Start a stopped listener
*
* @return True if the listener was successfully started
*/
bool start();
/**
* Listener name
*/
const char* name() const;
/**
* Network address the listener listens on
*/
const char* address() const;
/**
* Network port the listener listens on
*/
uint16_t port() const;
/**
* Service the listener points to
*/
SERVICE* service() const;
/**
* The protocol module name
*/
const char* protocol() const;
/**
* The state of the listener
*/
const char* state() const;
/**
* The service that the listener points to
*/
SERVICE* service()
{
return m_config.service;
}
/**
* Convert to JSON
*
* @param host The hostname of this server
*
* @return JSON representation of the object
*/
json_t* to_json(const char* host) const;
/**
* Get listener as a JSON API resource
*
* @param host The hostname of this server
*
* @return JSON API resource representation of the object
*/
json_t* to_json_resource(const char* host) const;
/**
* Get all listeners as a JSON API resource collection
*
* @param host The hostname of this server
*
* @return The listeners resource collection
*/
static json_t* to_json_collection(const char* host);
Type type() const
{
return m_type;
}
/**
* Persist listener configuration into a stream
*
* @param os Output stream where the listener is persisted
*
* @return The output stream given as the parameter
*/
std::ostream& persist(std::ostream& os) const;
bool post_configure(const mxs::ConfigParameters& protocol_params);
/**
* Create listener data object for test purposes. The parameters should still be valid listener
* settings, as they are parsed normally. Returns a shared_ptr as that is typically used by tests.
*
* @param params Associated listener settings
*
* @return New listener data object for test sessions
*/
static SData create_test_data(const mxs::ConfigParameters& params);
static mxs::config::Specification* specification();
mxb::SSLConfig ssl_config() const
{
return create_ssl_config();
}
// Pollable
int poll_fd() const override;
private:
friend class ListenerManager;
enum State
{
CREATED,
STARTED,
STOPPED,
FAILED,
DESTROYED
};
Config m_config; /**< The listener configuration */
std::string m_name; /**< Name of the listener */
State m_state; /**< Listener state */
// The configuration parameters given to the listener. These are not validated and are only
// used to construct the authenticators.
mxs::ConfigParameters m_params;
Type m_type; /**< The type of the listener */
mxs::WorkerLocal<int> m_local_fd {-1}; /**< File descriptor the listener listens on */
int m_shared_fd {-1}; /**< File descriptor the listener listens on */
SData m_shared_data; /**< Data shared with sessions */
/**
* Creates a new listener that points to a service
*
* @param service Service where the listener points to
* @param name Name of the listener
* @param address The address where the listener listens
* @param port The port on which the listener listens
* @param protocol The protocol module to use
*/
Listener(const std::string& name);
/**
* Listen on a file descriptor shared between all workers
*
* @return True if the listening was started successfully
*/
bool listen_shared();
/**
* Listen with a unique file descriptor for each worker
*
* @return True if the listening was started successfully
*/
bool listen_unique();
/**
* Close all opened file descriptors for this listener
*/
void close_all_fds();
/**
* Accept a single client connection
*
* @param fd The opened file descriptor to which the client is connected to
* @param addr The network information
* @param host The host where the client is connecting from
*
* @return The new DCB or nullptr on error
*/
ClientDCB* accept_one_dcb(int fd, const sockaddr_storage* addr, const char* host);
/**
* Accept all available client connections
*/
void accept_connections();
/**
* Reject a client connection
*
* Writes an error message to the fd if the protocol supports it and then closes it.
*
* @param fd The file descriptor to close
* @param host The host where the connection originated from
*/
void reject_connection(int fd, const char* host);
/**
* The file descriptor for accepting new connections
*
* @return The worker-local file descriptor
*/
int fd() const
{
return m_type == Type::UNIQUE_TCP ? *m_local_fd : m_shared_fd;
}
uint32_t handle_poll_events(mxb::Worker* worker, uint32_t events, Pollable::Context context) override;
static bool read_connection_init_sql(const std::string& filepath,
mxs::ListenerData::ConnectionInitSql* output);
bool read_user_mapping(mxs::ListenerData::SMappingInfo& output);
SData create_shared_data(const mxs::ConfigParameters& protocol_params);
mxb::SSLConfig create_ssl_config() const;
void set_type();
json_t* json_parameters() const;
bool force_config_reload();
};