MySQL 8.4.0
Source Code Documentation
auth_ldap_kerberos.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_KERBEROS_H_
25#define AUTH_LDAP_KERBEROS_H_
26
27#include <assert.h>
28#include <krb5/krb5.h>
29
30#include <string>
31
32#include "krb5_interface.h"
33
34namespace auth_ldap_sasl_client {
35
36/**
37 Kerberos class is built around kerberos library.
38 This class should/can be used for different part of code as standalone
39 class.
40 This class performs following operations:
41 1. Authentication with kerberos server and store the credentials in cache.
42 2. Get the default configured kerberos user in the OS, from default principal.
43
44 Credentials:
45 A ticket plus the secret session key necessary to use that ticket successfully
46 in an authentication exchange.
47
48 Principal:
49 A named client or server entity that participates in a network communication,
50 with one name that is considered canonical
51
52 Credential cache:
53 A credential cache (or ccache) holds Kerberos credentials while they
54 remain valid and, generally, while the user's session lasts, so that
55 authenticating to a service multiple times (e.g., connecting to a web or mail
56 server more than once) doesn't require contacting the KDC every time.
57*/
58class Kerberos {
59 public:
60 /**
61 Constructor.
62 */
63 Kerberos();
64 /**
65 Destructor.
66 */
67 ~Kerberos();
68 /**
69 Set user and password member variables.
70
71 @param user [in] user name
72 @param password [in] password
73 */
74 void set_user_and_password(const char *user, const char *password) {
75 assert(user);
76 assert(password);
77 m_user = user;
79 }
80 /**
81 1. This function authenticates with kerberos server.
82 2. If TGT destroy is false, this function stores the TGT in Kerberos cache
83 for subsequent usage.
84 3. If user credentials already exist in the cache, it doesn't attempt to get
85 it again.
86
87 @retval true Successfully able to obtain and store credentials.
88 @retval false Failed to obtain and store credentials.
89 */
91 /**
92 This function retrieves default principle from kerberos configuration and
93 parses the user name from it. If user name has not been provided in the
94 MySQL client, This method can be used to get the user name and use for
95 authentication.
96 @retval true Successfully able to get user name.
97 @retval false Failed to get user name.
98 */
99 bool get_default_principal_name(std::string &name);
100 /**
101 Check if the cache contains valid credentials
102
103 @retval true valid credentials exist
104 @retval false valid credentials not exist or an error ocurred
105 */
106 bool credentials_valid();
107 /**
108 Destroys existing credentials (remove them from the cache).
109 */
110 void destroy_credentials();
111 /**
112 This function gets LDAP host from krb5.conf file.
113 */
114 void get_ldap_host(std::string &host);
115
116 private:
117 /**
118 This function creates kerberos context, initializes credentials cache and
119 user principal.
120 @retval true All the required kerberos objects like context,
121 credentials cache and user principal are initialized correctly.
122 @retval false Required kerberos objects failed to initialized.
123 */
124 bool initialize();
125 /**
126 This function frees kerberos context, credentials, credentials cache and
127 user principal.
128 */
129 void cleanup();
130
131 /** is the object initialized */
133 /** user name */
134 std::string m_user;
135 /** user password */
136 std::string m_password;
137 /** LDAP host */
139 /** shall be the credentials destroyed on cleanup */
141 /** Kerberos context */
142 krb5_context m_context;
143 /** Kerberos cache */
145 /** Kerberos credentials */
146 krb5_creds m_credentials;
147 /** were the credentials created by the object */
149 /** interface to kerberos functions */
151
152 /**
153 Log a Kerberos error, the message is taken from the Kerberos based on the
154 error code.
155
156 @param error_code [in] Kerberos error code
157 */
158 void log(int error_code);
159 /**
160 This method gets kerberos profile settings from krb5.conf file.
161
162 @retval true success
163 @retval false failure
164
165 @details
166 Sample krb5.conf file format may be like this:
167
168 [realms]
169 MEM.LOCAL = {
170 kdc = VIKING67.MEM.LOCAL
171 admin_server = VIKING67.MEM.LOCAL
172 default_domain = MEM.LOCAL
173 }
174
175 # This portion is optional
176 [appdefaults]
177 mysql = {
178 ldap_server_host = ldap_host.oracle.com
179 ldap_destroy_tgt = true
180 }
181
182 kdc:
183 The name or address of a host running a KDC for that realm.
184 An optional port number, separated from the hostname by a colon, may
185 be included. If the name or address contains colons (for example, if it is
186 an IPv6 address), enclose it in square brackets to distinguish the colon
187 from a port separator.
188
189 For example:
190 kdchost.example.com:88
191 [2001:db8:3333:4444:5555:6666:7777:8888]:88
192
193 Details from:
194 https://web.mit.edu/kerberos/krb5-latest/doc/admin/conf_files/krb5_conf.html
195
196 Host information is used by LDAP SASL client API while initialization.
197 LDAP SASL API doesn't need port information and port is not used any where.
198 */
199 bool get_kerberos_config();
200 /**
201 Opens default Kerberos cache.
202
203 @retval true success
204 @retval false failure
205 */
206 bool open_default_cache();
207
208 /**
209 Closes default Kerberos cache.
210 */
211 void close_default_cache();
212};
213} // namespace auth_ldap_sasl_client
214#endif // AUTH_LDAP_KERBEROS_H_
Kerberos class is built around kerberos library.
Definition: auth_ldap_kerberos.h:58
krb5_context m_context
Kerberos context.
Definition: auth_ldap_kerberos.h:142
void get_ldap_host(std::string &host)
This function gets LDAP host from krb5.conf file.
Definition: auth_ldap_kerberos.cc:43
~Kerberos()
Destructor.
Definition: auth_ldap_kerberos.cc:41
void set_user_and_password(const char *user, const char *password)
Set user and password member variables.
Definition: auth_ldap_kerberos.h:74
void cleanup()
This function frees kerberos context, credentials, credentials cache and user principal.
Definition: auth_ldap_kerberos.cc:76
Krb5_interface krb5
interface to kerberos functions
Definition: auth_ldap_kerberos.h:150
bool get_kerberos_config()
This method gets kerberos profile settings from krb5.conf file.
Definition: auth_ldap_kerberos.cc:253
bool open_default_cache()
Opens default Kerberos cache.
Definition: auth_ldap_kerberos.cc:92
bool obtain_store_credentials()
Definition: auth_ldap_kerberos.cc:117
krb5_ccache m_krb_credentials_cache
Kerberos cache.
Definition: auth_ldap_kerberos.h:144
void destroy_credentials()
Destroys existing credentials (remove them from the cache).
Definition: auth_ldap_kerberos.cc:455
std::string m_user
user name
Definition: auth_ldap_kerberos.h:134
std::string m_ldap_server_host
LDAP host.
Definition: auth_ldap_kerberos.h:138
std::string m_password
user password
Definition: auth_ldap_kerberos.h:136
bool m_destroy_tgt
shall be the credentials destroyed on cleanup
Definition: auth_ldap_kerberos.h:140
Kerberos()
Constructor.
Definition: auth_ldap_kerberos.cc:32
void close_default_cache()
Closes default Kerberos cache.
Definition: auth_ldap_kerberos.cc:106
bool credentials_valid()
Check if the cache contains valid credentials.
Definition: auth_ldap_kerberos.cc:366
bool get_default_principal_name(std::string &name)
This function retrieves default principle from kerberos configuration and parses the user name from i...
Definition: auth_ldap_kerberos.cc:468
bool m_initialized
is the object initialized
Definition: auth_ldap_kerberos.h:132
void log(int error_code)
Log a Kerberos error, the message is taken from the Kerberos based on the error code.
Definition: auth_ldap_kerberos.cc:517
bool initialize()
This function creates kerberos context, initializes credentials cache and user principal.
Definition: auth_ldap_kerberos.cc:47
bool m_credentials_created
were the credentials created by the object
Definition: auth_ldap_kerberos.h:148
krb5_creds m_credentials
Kerberos credentials.
Definition: auth_ldap_kerberos.h:146
Class representing interface to KRB5 functions.
Definition: krb5_interface.h:138
static char * password
Definition: mysql_secure_installation.cc:58
char * user
Definition: mysqladmin.cc:66
const char * host
Definition: mysqladmin.cc:65
Definition: auth_ldap_kerberos.cc:30
case opt name
Definition: sslopt-case.h:29