- Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathsql_data_context.h
198 lines (161 loc) · 7.6 KB
/
sql_data_context.h
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
/*
* Copyright (c) 2015, 2025, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.0,
* as published by the Free Software Foundation.
*
* This program is designed to work with certain software (including
* but not limited to OpenSSL) that is licensed under separate terms,
* as designated in a particular file or component or in included license
* documentation. The authors of MySQL hereby grant you an additional
* permission to link the program and your derivative works with the
* separately licensed software that they have either included with
* the program or referenced in the documentation.
*
* This program 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 General Public License, version 2.0, for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef PLUGIN_X_SRC_SQL_DATA_CONTEXT_H_
#definePLUGIN_X_SRC_SQL_DATA_CONTEXT_H_
#include<stdio.h>
#include<string>
#ifndef _WIN32
#include<netdb.h>
#endif
#include"my_hostname.h"
#include"mysql/service_command.h"
#include"mysql_com.h"
#include"plugin/x/src/buffering_command_delegate.h"
#include"plugin/x/src/interface/protocol_encoder.h"
#include"plugin/x/src/interface/sql_session.h"
#include"plugin/x/src/io/connection_type.h"
#include"plugin/x/src/streaming_command_delegate.h"
// Use an internal MySQL server user
#defineMYSQL_SESSION_USER"mysql.session"
#defineMYSQLXSYS_HOST"localhost"
#defineMYSQLXSYS_ACCOUNT"'" MYSQL_SESSION_USER "'@'" MYSQLXSYS_HOST "'"
namespacexpl {
typedef std::function<bool(const std::string &password_hash)>
On_user_password_hash;
typedef Buffering_command_delegate::Field_value Field_value;
typedef Buffering_command_delegate::Row_data Row_data;
classAccount_verification_handler;
classSql_data_context : publiciface::Sql_session {
public:
Sql_data_context()
: m_mysql_session(nullptr),
m_last_sql_errno(0),
m_password_expired(false),
m_using_password(false),
m_pre_authenticate_event_fired(false) {}
~Sql_data_context() override;
// The authentication process of the user connecting to the X Plugin.
//
// @param user User name.
// @param host Host name.
// @param ip IP value.
// @param db Database name.
// @param passwd User password.
// @param account_verification User authentication object.
// @param allow_expired_passwords Ignore password
//
// @return Error_code instance that holds authentication result.
ngs::Error_code authenticate(
constchar *user, constchar *host, constchar *ip, constchar *db,
const std::string &passwd,
const iface::Authentication &account_verification,
bool allow_expired_passwords) override;
uint64_tmysql_session_id() constoverride;
ngs::Error_code set_connection_type(const Connection_type type) override;
boolis_killed() constoverride;
boolpassword_expired() constoverride { return m_password_expired; }
// Get data which are part of string printed by
// CURRENT_USER() function
std::string get_authenticated_user_name() constoverride;
std::string get_authenticated_user_host() constoverride;
boolhas_authenticated_user_a_super_priv() constoverride;
ngs::Error_code execute_kill_sql_session(uint64_t mysql_session_id) override;
// can only be executed once authenticated
ngs::Error_code execute(constchar *sql, std::size_t sql_len,
iface::Resultset *rset) override;
ngs::Error_code execute_sql(constchar *sql, std::size_t sql_len,
iface::Resultset *rset) override;
ngs::Error_code prepare_prep_stmt(constchar *sql, std::size_t sql_len,
iface::Resultset *rset) override;
ngs::Error_code deallocate_prep_stmt(constuint32_t stmt_id,
iface::Resultset *rset) override;
ngs::Error_code execute_prep_stmt(constuint32_t stmt_id,
constbool has_cursor,
const PS_PARAM *parameters,
const std::size_t parameters_count,
iface::Resultset *rset) override;
ngs::Error_code fetch_cursor(constuint32_t id, constuint32_t row_count,
iface::Resultset *rset) override;
ngs::Error_code attach() override;
ngs::Error_code detach() override;
ngs::Error_code reset() override;
boolis_sql_mode_set(const std::string &mode) override;
ngs::Error_code init(constbool is_admin = false);
ngs::Error_code init(constint client_port, const Connection_type type,
constbool is_admin = false);
voiddeinit();
MYSQL_THD get_thd() const;
boolkill();
boolis_acl_disabled();
voidswitch_to_local_user(const std::string &username);
staticboolwait_api_ready(std::function<bool()> exiting);
private:
Sql_data_context(const Sql_data_context &) = delete;
Sql_data_context &operator=(const Sql_data_context &) = delete;
// The real authentication process implementation, without generation
// of the audit events. For argument description see @ref authenticate.
ngs::Error_code authenticate_internal(
constchar *user, constchar *host, constchar *ip, constchar *db,
const std::string &passwd,
const iface::Authentication &account_verification,
bool allow_expired_passwords);
MYSQL_SESSION mysql_session() const { return m_mysql_session; }
staticboolis_api_ready();
// Get data which are parts of the string printed by
// USER() function
std::string get_user_name() const;
std::string get_host_or_ip() const;
ngs::Error_code switch_to_user(constchar *username, constchar *hostname,
constchar *address, constchar *db);
staticvoiddefault_completion_handler(void *ctx, unsignedint sql_errno,
constchar *err_msg);
ngs::Error_code execute_server_command(const enum_server_command cmd,
const COM_DATA &cmd_data,
iface::Resultset *rset);
// We need to keep pointers to std::string to guarantee that pointer
// of the buffer holding the string changes. This is due to the security
// context implementation, which do not update internal data, when
// the pointer does not change.
char m_username[USERNAME_LENGTH + 1];
char m_hostname[HOSTNAME_LENGTH + 1];
char m_address[NI_MAXHOST];
char m_db[NAME_LEN + 1];
MYSQL_SESSION m_mysql_session;
int m_last_sql_errno;
std::string m_last_sql_error;
bool m_password_expired;
// Flag indicating whether a password is used during the authentication
// process.
bool m_using_password;
// Single X plugin user connection can be authenticated multiple times
// using different connection methods. This flag assures that there is
// only one MYSQL_AUDIT_CONNECTION_PRE_AUTHENTICATE event generated.
bool m_pre_authenticate_event_fired;
// Last authentication process error code. The code is used to pass error
// code to the generated MYSQL_AUDIT_CONNECTION_CONNECT event.
ngs::Error_code m_authentication_code;
};
} // namespace xpl
#endif // PLUGIN_X_SRC_SQL_DATA_CONTEXT_H_