MySQL 8.4.2
Source Code Documentation
auth_ldap_sasl_mechanism.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2024, 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 AUTH_LDAP_SASL_MECHANISM_H_
25#define AUTH_LDAP_SASL_MECHANISM_H_
26
27#include "my_config.h"
28
29#ifdef HAVE_SASL_SASL_H
30#include <sys/types.h>
31#endif
32#include <sasl/sasl.h>
33
34#include <string>
35
36#if defined(KERBEROS_LIB_CONFIGURED)
37#include "auth_ldap_kerberos.h"
38#endif
39
40namespace auth_ldap_sasl_client {
41
43
44/**
45 Base class representing SASL mechanism. The child classes are used to perform
46 all mechanism specific SASL operations.
47*/
49 public:
50 /** GSSAPI string */
51 static const char SASL_GSSAPI[];
52 /** SCRAM-SHA-1 string */
53 static const char SASL_SCRAM_SHA1[];
54 /** SCRAM-SHA-256 string */
55 static const char SASL_SCRAM_SHA256[];
56
57 /**
58 Destructor.
59 */
60 virtual ~Sasl_mechanism() = default;
61 /**
62 Preauthentication step, e.g. obtaining Kerberos ticket. Not needed by most
63 methods, so the default implementation just returns success.
64
65 @param user [in] user mname
66 @param password [in] user password
67
68 @return true -success
69 */
70 bool virtual preauthenticate([[maybe_unused]] const char *user,
71 [[maybe_unused]] const char *password) {
72 return true;
73 }
74 /**
75 Get LDAP host. Not needed by most methods, return nullptr by default.
76
77 @return LDAP host URL or nullptr on failure
78 */
79 virtual const char *get_ldap_host() { return nullptr; }
80
81 /**
82 Get default user name. Called if no user name was provided as parameter to
83 the client. Most methods don't provide default user name.
84
85 @param name [out] default user name
86 @return false -failure
87 */
88 virtual bool get_default_user([[maybe_unused]] std::string &name) {
89 return false;
90 }
91
92 /**
93 Get list of supported SASL callbacks.
94
95 @return List of callbacks.
96 */
97 virtual const sasl_callback_t *get_callbacks() { return nullptr; }
98
99 /**
100 Gets constans string describing mechanism name.
101
102 @return mechanism name
103 */
104 const char *get_mechanism_name() { return m_mechanism_name; }
105
106 /**
107 Check if the authentication method requires conclusion message from the
108 server. Most authentication mechanisms don't require to be concluded by MySQL
109 server, so the base class implementation always returns false.
110
111 @return false
112 */
113 virtual bool require_conclude_by_server() { return false; }
114
115 /**
116 SASL mechanism factory function. Creates mechanism object based on mechanism
117 name.
118
119 @param mechanism_name [in] name of the mechanism
120 @param mechanism [out] created mechanism object
121
122 @retval true success
123 @retval false failure
124 */
125 static bool create_sasl_mechanism(const char *mechanism_name,
126 Sasl_mechanism *&mechanism);
127
128 protected:
129 /**
130 Constructor. Made protected to avoid creating direct objects of this class.
131
132 @param mechanism_name [in] name of the mechanism
133 */
134 Sasl_mechanism(const char *mechanism_name)
135 : m_mechanism_name(mechanism_name) {}
136
137 private:
138 /** array of SASL callbacks */
139 static const sasl_callback_t callbacks[];
140 /** name of the mechanism */
141 const char *m_mechanism_name;
142};
143
144#if defined(KERBEROS_LIB_CONFIGURED)
145/**
146 Class representing GSSAPI/Kerberos mechanism
147*/
149 public:
150 /**
151 Constructor.
152 */
154 /**
155 Destructor.
156 */
157 ~Sasl_mechanism_kerberos() override = default;
158 /**
159 Preauthentication step. Obtains Kerberos ticket.
160
161 @param user [in] user mname
162 @param password [in] user password
163
164 @retval true success
165 @retval false failure
166 */
167 bool preauthenticate(const char *user, const char *password) override;
168 /**
169 Get LDAP host.
170
171 @return LDAP host URL or nullptr on failure
172 */
173 const char *get_ldap_host() override;
174
175 /**
176 Get default user name. Called if no user name was provided as parameter to
177 the client. The name is the default principal.
178
179 @param name [out] default user name
180
181 @retval true success
182 @retval false failure
183 */
184 bool get_default_user(std::string &name) override;
185 /**
186 Gets array of SASL callbacks supported by the mechanism.
187
188 @return array of callbacks
189 */
190 const sasl_callback_t *get_callbacks() override { return callbacks; }
191 /**
192 GSSAPI authentication must be concluded by MySQL server.
193
194 @return true
195 */
196 bool require_conclude_by_server() override { return true; }
197
198 private:
199 /** URL of the LDAP server */
201 /** Kerberos object used to perform Kerberos operations */
203 /** Array of SASL callbacks supported by this mechanism */
204 static const sasl_callback_t callbacks[];
205};
206#endif
207
208#if defined(SCRAM_LIB_CONFIGURED)
209/**
210 Class representing SCRAM family of SASL mechanisms (currently SCRAM-SHA-1 and
211 SCRAM-SHA-256).
212*/
214 public:
215 /**
216 Constructor.
217
218 @param mechanism_name [in] mame of the mechanism
219 */
220 Sasl_mechanism_scram(const char *mechanism_name)
221 : Sasl_mechanism(mechanism_name) {}
222 /**
223 Destructor.
224 */
225 ~Sasl_mechanism_scram() override = default;
226 /**
227 Gets array of SASL callbacks supported by the mechanism.
228
229 @return array of callbacks
230 */
231 const sasl_callback_t *get_callbacks() override { return callbacks; }
232
233 private:
234 /** Array of SASL callbacks supported by this mechanism */
235 static const sasl_callback_t callbacks[];
236};
237#endif
238} // namespace auth_ldap_sasl_client
239#endif // AUTH_LDAP_SASL_MECHANISM_H_
Kerberos class is built around kerberos library.
Definition: auth_ldap_kerberos.h:58
Class representing GSSAPI/Kerberos mechanism.
Definition: auth_ldap_sasl_mechanism.h:148
bool require_conclude_by_server() override
GSSAPI authentication must be concluded by MySQL server.
Definition: auth_ldap_sasl_mechanism.h:196
const char * get_ldap_host() override
Get LDAP host.
Definition: auth_ldap_sasl_mechanism.cc:115
Kerberos m_kerberos
Kerberos object used to perform Kerberos operations.
Definition: auth_ldap_sasl_mechanism.h:202
std::string m_ldap_server_host
URL of the LDAP server.
Definition: auth_ldap_sasl_mechanism.h:200
bool preauthenticate(const char *user, const char *password) override
Preauthentication step.
Definition: auth_ldap_sasl_mechanism.cc:67
Sasl_mechanism_kerberos()
Constructor.
Definition: auth_ldap_sasl_mechanism.h:153
bool get_default_user(std::string &name) override
Get default user name.
Definition: auth_ldap_sasl_mechanism.cc:63
const sasl_callback_t * get_callbacks() override
Gets array of SASL callbacks supported by the mechanism.
Definition: auth_ldap_sasl_mechanism.h:190
~Sasl_mechanism_kerberos() override=default
Destructor.
static const sasl_callback_t callbacks[]
Array of SASL callbacks supported by this mechanism.
Definition: auth_ldap_sasl_mechanism.h:204
Class representing SCRAM family of SASL mechanisms (currently SCRAM-SHA-1 and SCRAM-SHA-256).
Definition: auth_ldap_sasl_mechanism.h:213
const sasl_callback_t * get_callbacks() override
Gets array of SASL callbacks supported by the mechanism.
Definition: auth_ldap_sasl_mechanism.h:231
Sasl_mechanism_scram(const char *mechanism_name)
Constructor.
Definition: auth_ldap_sasl_mechanism.h:220
~Sasl_mechanism_scram() override=default
Destructor.
static const sasl_callback_t callbacks[]
Array of SASL callbacks supported by this mechanism.
Definition: auth_ldap_sasl_mechanism.h:235
Base class representing SASL mechanism.
Definition: auth_ldap_sasl_mechanism.h:48
virtual bool get_default_user(std::string &name)
Get default user name.
Definition: auth_ldap_sasl_mechanism.h:88
const char * m_mechanism_name
name of the mechanism
Definition: auth_ldap_sasl_mechanism.h:141
virtual const char * get_ldap_host()
Get LDAP host.
Definition: auth_ldap_sasl_mechanism.h:79
virtual bool require_conclude_by_server()
Check if the authentication method requires conclusion message from the server.
Definition: auth_ldap_sasl_mechanism.h:113
Sasl_mechanism(const char *mechanism_name)
Constructor.
Definition: auth_ldap_sasl_mechanism.h:134
static bool create_sasl_mechanism(const char *mechanism_name, Sasl_mechanism *&mechanism)
SASL mechanism factory function.
Definition: auth_ldap_sasl_mechanism.cc:121
static const sasl_callback_t callbacks[]
array of SASL callbacks
Definition: auth_ldap_sasl_mechanism.h:139
static const char SASL_SCRAM_SHA256[]
SCRAM-SHA-256 string.
Definition: auth_ldap_sasl_mechanism.h:55
virtual ~Sasl_mechanism()=default
Destructor.
const char * get_mechanism_name()
Gets constans string describing mechanism name.
Definition: auth_ldap_sasl_mechanism.h:104
virtual bool preauthenticate(const char *user, const char *password)
Preauthentication step, e.g.
Definition: auth_ldap_sasl_mechanism.h:70
virtual const sasl_callback_t * get_callbacks()
Get list of supported SASL callbacks.
Definition: auth_ldap_sasl_mechanism.h:97
static const char SASL_SCRAM_SHA1[]
SCRAM-SHA-1 string.
Definition: auth_ldap_sasl_mechanism.h:53
static const char SASL_GSSAPI[]
GSSAPI string.
Definition: auth_ldap_sasl_mechanism.h:51
static char * password
Definition: mysql_secure_installation.cc:58
char * user
Definition: mysqladmin.cc:66
Definition: auth_ldap_kerberos.cc:30
const int SASL_ERROR_INVALID_METHOD
Definition: auth_ldap_sasl_mechanism.h:42
case opt name
Definition: sslopt-case.h:29