MySQL 8.4.8
Source Code Documentation
sql_authentication.h
Go to the documentation of this file.
1/* Copyright (c) 2000, 2025, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef SQL_AUTHENTICATION_INCLUDED
25#define SQL_AUTHENTICATION_INCLUDED
26
27#include <openssl/rsa.h>
28#include <stddef.h>
29#include <sys/types.h>
30#include <vector>
31
32#include "lex_string.h"
33#include "my_thread_local.h" // my_thread_id
34#include "mysql/plugin_auth.h" // MYSQL_SERVER_AUTH_INFO
37#include "sql/sql_plugin_ref.h" // plugin_ref
38
39class ACL_USER;
41class THD;
42class Restrictions;
43struct MEM_ROOT;
44struct SHOW_VAR;
45
46/* Classes */
47
50
51 public:
52 Thd_charset_adapter(THD *thd_arg) : thd(thd_arg) {}
53 bool init_client_charset(uint cs_number);
54
55 const CHARSET_INFO *charset();
56};
57
58/**
59 The internal version of what plugins know as MYSQL_PLUGIN_VIO,
60 basically the context of the authentication session
61*/
62struct MPVIO_EXT : public MYSQL_PLUGIN_VIO {
66 plugin_ref plugin; ///< what plugin we're under
67 LEX_STRING db; ///< db name from the handshake packet
68 /** when restarting a plugin this caches the last client reply */
69 struct {
70 const char *plugin, *pkt; ///< pointers into NET::buff
71 uint pkt_len;
73 /** this caches the first plugin packet for restart request on the client */
74 struct {
75 char *pkt;
76 uint pkt_len;
78 int packets_read, packets_written; ///< counters for send/received packets
79 /** when plugin returns a failure this tells us what really happened */
81
82 /* encapsulation members */
83 char *scramble;
90 const char *ip;
91 const char *host;
95 bool can_authenticate();
96};
97
98class String;
99
100bool init_rsa_keys(void);
101void deinit_rsa_keys(void);
102int show_rsa_public_key(THD *thd, SHOW_VAR *var, char *buff);
103
104typedef struct rsa_st RSA;
106 private:
107#if OPENSSL_VERSION_NUMBER >= 0x30000000L
108 EVP_PKEY *m_public_key;
109 EVP_PKEY *m_private_key;
110#else /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
113#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
118
119 void get_key_file_path(char *key, String *key_file_path);
120
121#if OPENSSL_VERSION_NUMBER >= 0x30000000L
122 bool read_key_file(EVP_PKEY **key_ptr, bool is_priv_key,
123 char **key_text_buffer);
124#else /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
125 bool read_key_file(RSA **key_ptr, bool is_priv_key, char **key_text_buffer);
126#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
127
128 public:
129 Rsa_authentication_keys(char **private_key_path, char **public_key_path)
132 m_cipher_len(0),
134 m_private_key_path(private_key_path),
135 m_public_key_path(public_key_path) {}
137
138 void free_memory();
139 void *allocate_pem_buffer(size_t buffer_len);
140
141#if OPENSSL_VERSION_NUMBER >= 0x30000000L
142 EVP_PKEY *get_private_key() { return m_private_key; }
143 EVP_PKEY *get_public_key() { return m_public_key; }
144#else /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
147#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
148
149 int get_cipher_length();
150 bool read_rsa_keys();
151 const char *get_public_key_as_pem(void) { return m_pem_public_key; }
152};
153
154/* Data Structures */
155
157
158extern bool allow_all_hosts;
159
160typedef enum {
164 /* Add new plugin before this */
167
169 public:
172 /**
173 List of cached plugins that are active (loaded and enabled)
174 @sa @ref decoy_user
175 */
176 std::vector<cached_plugins_enum> enabled_plugins;
177 /**
178 Compare given plugin against one of the cached ones
179
180 @param [in] plugin_index Cached plugin index
181 @param [in] plugin Plugin to be compared
182
183 @returns status of comparison
184 @retval true Match
185 @retval false Not a match
186 */
187 static bool compare_plugin(cached_plugins_enum plugin_index,
188 LEX_CSTRING plugin) {
189 if (plugin_index < PLUGIN_LAST && plugin.str) {
191 return (plugin.str == cached_plugins_names[plugin_index].str);
192 }
193 return false;
194 }
195
196 /**
197 Check if given plugin is a builtin
198
199 @param [in] plugin Plugin name
200
201 @returns true if builtin, false otherwise
202 */
204 for (uint i = 0; i < (uint)PLUGIN_LAST; ++i) {
205 if (plugin->str == cached_plugins_names[i].str) return true;
206 }
207 return false;
208 }
209
210 /**
211 Get name of the plugin at given index
212
213 @param [in] plugin_index Cached plugin index
214
215 @returns name of the cached plugin at given index
216 */
217 static const char *get_plugin_name(cached_plugins_enum plugin_index) {
218 if (plugin_index < PLUGIN_LAST)
219 return cached_plugins_names[plugin_index].str;
220 return nullptr;
221 }
222
225
227
228 /**
229 Fetch cached plugin handle
230
231 @param plugin_index Cached plugin index
232
233 @returns cached plugin_ref if found, 0 otherwise
234 */
236 if (plugin_index < PLUGIN_LAST) return cached_plugins[plugin_index];
237 return nullptr;
238 }
239
241 bool is_valid() { return m_valid; }
242
243 private:
245};
246
248
249ACL_USER *decoy_user(const LEX_CSTRING &username, const LEX_CSTRING &hostname,
250 MEM_ROOT *mem, struct rand_struct *rand,
251 bool is_initialized);
252#define AUTH_DEFAULT_RSA_PRIVATE_KEY "private_key.pem"
253#define AUTH_DEFAULT_RSA_PUBLIC_KEY "public_key.pem"
254
255#endif /* SQL_AUTHENTICATION_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
Definition: sql_auth_cache.h:247
Definition: sql_authentication.h:168
bool m_valid
Definition: sql_authentication.h:244
Cached_authentication_plugins()
Cached_authentication_plugins constructor.
Definition: sql_authentication.cc:1194
static const LEX_CSTRING cached_plugins_names[(uint) PLUGIN_LAST]
Definition: sql_authentication.h:170
static bool compare_plugin(cached_plugins_enum plugin_index, LEX_CSTRING plugin)
Compare given plugin against one of the cached ones.
Definition: sql_authentication.h:187
bool is_valid()
Definition: sql_authentication.h:241
static const char * get_plugin_name(cached_plugins_enum plugin_index)
Get name of the plugin at given index.
Definition: sql_authentication.h:217
plugin_ref cached_plugins[(uint) PLUGIN_LAST]
Definition: sql_authentication.h:240
plugin_ref get_cached_plugin_ref(cached_plugins_enum plugin_index)
Fetch cached plugin handle.
Definition: sql_authentication.h:235
plugin_ref get_cached_plugin_ref(const LEX_CSTRING *plugin)
Get plugin_ref if plugin is cached.
Definition: sql_authentication.cc:1231
static bool auth_plugin_is_built_in(LEX_CSTRING *plugin)
Check if given plugin is a builtin.
Definition: sql_authentication.h:203
static void optimize_plugin_compare_by_pointer(LEX_CSTRING *plugin)
Use known pointers for cached plugins to improve comparison time.
Definition: sql_authentication.cc:1176
std::vector< cached_plugins_enum > enabled_plugins
List of cached plugins that are active (loaded and enabled)
Definition: sql_authentication.h:176
~Cached_authentication_plugins()
Cached_authentication_plugins destructor.
Definition: sql_authentication.cc:1217
Definition: protocol_classic.h:54
Container of all restrictions for a given user.
Definition: partial_revokes.h:155
Definition: sql_authentication.h:105
bool read_key_file(RSA **key_ptr, bool is_priv_key, char **key_text_buffer)
Read a key file and store its value in RSA structure.
Definition: sql_authentication.cc:1366
void * allocate_pem_buffer(size_t buffer_len)
Definition: sql_authentication.cc:1454
char ** m_public_key_path
Definition: sql_authentication.h:117
RSA * get_public_key()
Definition: sql_authentication.h:146
void get_key_file_path(char *key, String *key_file_path)
Set key file path.
Definition: sql_authentication.cc:1326
int get_cipher_length()
Definition: sql_authentication.cc:1459
int m_cipher_len
Definition: sql_authentication.h:114
RSA * m_private_key
Definition: sql_authentication.h:112
RSA * get_private_key()
Definition: sql_authentication.h:145
~Rsa_authentication_keys()=default
bool read_rsa_keys()
Read RSA private key and public key from file and store them in m_private_key and m_public_key.
Definition: sql_authentication.cc:1476
char ** m_private_key_path
Definition: sql_authentication.h:116
char * m_pem_public_key
Definition: sql_authentication.h:115
const char * get_public_key_as_pem(void)
Definition: sql_authentication.h:151
RSA * m_public_key
Definition: sql_authentication.h:111
Rsa_authentication_keys(char **private_key_path, char **public_key_path)
Definition: sql_authentication.h:129
void free_memory()
Definition: sql_authentication.cc:1434
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: sql_authentication.h:48
Thd_charset_adapter(THD *thd_arg)
Definition: sql_authentication.h:52
const CHARSET_INFO * charset()
Definition: sql_authentication.cc:1316
bool init_client_charset(uint cs_number)
Definition: sql_authentication.cc:1310
THD * thd
Definition: sql_authentication.h:49
A better implementation of the UNIX ctype(3) library.
uint32 my_thread_id
Definition: my_thread_local.h:34
Authentication Plugin API.
This file defines constants and data structures that are the same for both client- and server-side au...
required string key
Definition: replication_asynchronous_connection_failover.proto:60
int show_rsa_public_key(THD *thd, SHOW_VAR *var, char *buff)
Definition: sql_authentication.cc:4497
ACL_USER * decoy_user(const LEX_CSTRING &username, const LEX_CSTRING &hostname, MEM_ROOT *mem, struct rand_struct *rand, bool is_initialized)
When authentication is attempted using an unknown username a dummy user account with no authenticatio...
Definition: sql_authentication.cc:2203
cached_plugins_enum
Definition: sql_authentication.h:160
@ PLUGIN_LAST
Definition: sql_authentication.h:165
@ PLUGIN_CACHING_SHA2_PASSWORD
Definition: sql_authentication.h:161
@ PLUGIN_SHA256_PASSWORD
Definition: sql_authentication.h:163
@ PLUGIN_MYSQL_NATIVE_PASSWORD
Definition: sql_authentication.h:162
bool allow_all_hosts
Definition: sql_auth_cache.cc:165
Cached_authentication_plugins * g_cached_authentication_plugins
Definition: sql_authentication.cc:1282
void deinit_rsa_keys(void)
Definition: sql_authentication.cc:4503
struct rsa_st RSA
Definition: sql_authentication.h:104
bool init_rsa_keys(void)
Loads the RSA key pair from disk and store them in a global variable.
Definition: sql_authentication.cc:4537
LEX_CSTRING validate_password_plugin_name
Definition: sql_authentication.cc:1160
static MEM_ROOT mem
Definition: sql_servers.cc:100
Definition: m_ctype.h:423
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
The internal version of what plugins know as MYSQL_PLUGIN_VIO, basically the context of the authentic...
Definition: sql_authentication.h:62
ulong max_client_packet_length
Definition: sql_authentication.h:89
@ FAILURE
Definition: sql_authentication.h:80
@ START_MFA
Definition: sql_authentication.h:80
@ SUCCESS
Definition: sql_authentication.h:80
@ RESTART
Definition: sql_authentication.h:80
uint pkt_len
Definition: sql_authentication.h:71
struct MPVIO_EXT::@43 cached_server_packet
this caches the first plugin packet for restart request on the client
enum MPVIO_EXT::@44 status
when plugin returns a failure this tells us what really happened
char * pkt
Definition: sql_authentication.h:75
const ACL_USER * acl_user
Definition: sql_authentication.h:64
int vio_is_encrypted
Definition: sql_authentication.h:94
const char * ip
Definition: sql_authentication.h:90
int packets_written
counters for send/received packets
Definition: sql_authentication.h:78
Protocol_classic * protocol
Definition: sql_authentication.h:88
int packets_read
Definition: sql_authentication.h:78
LEX_STRING db
db name from the handshake packet
Definition: sql_authentication.h:67
LEX_CSTRING acl_user_plugin
Definition: sql_authentication.h:93
my_thread_id thread_id
Definition: sql_authentication.h:86
const char * pkt
pointers into NET::buff
Definition: sql_authentication.h:70
bool can_authenticate()
Definition: sql_authentication.cc:5904
struct rand_struct * rand
Definition: sql_authentication.h:85
plugin_ref plugin
what plugin we're under
Definition: sql_authentication.h:66
const char * host
Definition: sql_authentication.h:91
char * scramble
Definition: sql_authentication.h:83
uint * server_status
Definition: sql_authentication.h:87
MYSQL_SERVER_AUTH_INFO auth_info
Definition: sql_authentication.h:63
Thd_charset_adapter * charset_adapter
Definition: sql_authentication.h:92
struct MPVIO_EXT::@42 cached_client_reply
when restarting a plugin this caches the last client reply
Restrictions * restrictions
Definition: sql_authentication.h:65
MEM_ROOT * mem_root
Definition: sql_authentication.h:84
Definition: mysql_lex_string.h:40
const char * str
Definition: mysql_lex_string.h:41
Definition: mysql_lex_string.h:35
Provides plugin access to communication channel.
Definition: plugin_auth_common.h:146
Provides server plugin access to authentication information.
Definition: plugin_auth.h:71
SHOW STATUS Server status variable.
Definition: status_var.h:79
Definition: mysql_com.h:1109
Definition: sql_plugin_ref.h:45
std::atomic< bool > is_initialized(false)