MySQL 9.1.0
Source Code Documentation
common.h
Go to the documentation of this file.
1/* Copyright (c) 2011, 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 COMMON_H
25#define COMMON_H
26
27#include <mysql/plugin_auth_common.h> // for MYSQL_PLUGIN_VIO
28#include <sspi.h> // for CtxtHandle
29#include <windows.h>
30
31#include "my_dbug.h"
32
33/// Maximum length of the target service name.
34#define MAX_SERVICE_NAME_LENGTH 1024
35
36/** Debugging and error reporting infrastructure ***************************/
37
38/*
39 Note: We use plugin local logging and error reporting mechanisms until
40 WL#2940 (plugin service: error reporting) is available.
41*/
42
43#undef INFO
44#undef WARNING
45#undef ERROR
46
48 typedef enum { INFO, WARNING, ERROR } type;
49};
50
52unsigned int get_log_level(void);
53void set_log_level(unsigned int);
54
55/*
56 If DEBUG_ERROR_LOG is defined then error logging happens only
57 in debug-copiled code. Otherwise ERROR_LOG() expands to
58 error_log_print() even in production code.
59
60 Note: Macro ERROR_LOG() can use printf-like format string like this:
61
62 ERROR_LOG(Level, ("format string", args));
63
64 The implementation should handle it correctly. Currently it is passed
65 to fprintf() (see error_log_vprint() function).
66*/
67
68#if defined(DEBUG_ERROR_LOG) && defined(NDEBUG)
69#define ERROR_LOG(Level, Msg) \
70 do { \
71 } while (0)
72#else
73#define ERROR_LOG(Level, Msg) error_log_print<error_log_level::Level> Msg
74#endif
75
76void error_log_vprint(error_log_level::type level, const char *fmt,
77 va_list args);
78
79template <error_log_level::type Level>
80void error_log_print(const char *fmt, ...) {
81 va_list args;
82 va_start(args, fmt);
83 error_log_vprint(Level, fmt, args);
84 va_end(args);
85}
86
87typedef char Error_message_buf[1024];
89
90/*
91 Internal implementation of debug message printing which does not use
92 dbug library. This is invoked via macro:
93
94 DBUG_PRINT_DO(Keyword, ("format string", args));
95
96 This is supposed to be used as an implementation of DBUG_PRINT() macro,
97 unless the dbug library implementation is used or debug messages are disabled.
98*/
99
100#ifndef NDEBUG
101
102#define DBUG_PRINT_DO(Keyword, Msg) \
103 do { \
104 if (4 > get_log_level()) break; \
105 fprintf(stderr, "winauth: %s: ", Keyword); \
106 debug_msg Msg; \
107 } while (0)
108
109inline void debug_msg(const char *fmt, ...) {
110 va_list args;
111 va_start(args, fmt);
112 vfprintf(stderr, fmt, args);
113 fputc('\n', stderr);
114 fflush(stderr);
115 va_end(args);
116}
117
118#else
119#define DBUG_PRINT_DO(K, M) \
120 do { \
121 } while (0)
122#endif
123
124#ifndef WINAUTH_USE_DBUG_LIB
125
126#undef DBUG_PRINT
127#define DBUG_PRINT(Keyword, Msg) DBUG_PRINT_DO(Keyword, Msg)
128
129/*
130 Redefine few more debug macros to make sure that no symbols from
131 dbug library are used.
132*/
133
134#undef DBUG_ENTER
135#define DBUG_ENTER(X) \
136 do { \
137 } while (0)
138
139#undef DBUG_RETURN
140#define DBUG_RETURN(X) return (X)
141
142#undef DBUG_DUMP
143#define DBUG_DUMP(A, B, C) \
144 do { \
145 } while (0)
146
147#endif
148
149/** Blob class *************************************************************/
150
151typedef unsigned char byte;
152
153/**
154 Class representing a region of memory (e.g., a string or binary buffer).
155
156 @note This class does not allocate memory. It merely describes a region
157 of memory which must be allocated externally (if it is dynamic memory).
158*/
159
160class Blob {
161 byte *m_ptr; ///< Pointer to the first byte of the memory region.
162 size_t m_len; ///< Length of the memory region.
163
164 public:
166
167 Blob(const byte *ptr, const size_t len)
168 : m_ptr(const_cast<byte *>(ptr)), m_len(len) {}
169
170 explicit Blob(const char *str)
171 : m_ptr(const_cast<byte *>(reinterpret_cast<const byte *>(str))) {
172 m_len = strlen(str);
173 }
174
175 byte *ptr() const { return m_ptr; }
176
177 size_t len() const { return m_len; }
178
179 byte &operator[](unsigned pos) const {
180 static byte out_of_range = 0; // alas, no exceptions...
181 return pos < len() ? m_ptr[pos] : out_of_range;
182 }
183
184 bool is_null() const { return m_ptr == nullptr; }
185
186 void trim(size_t l) { m_len = l; }
187};
188
189/** Connection class *******************************************************/
190
191/**
192 Convenience wrapper around MYSQL_PLUGIN_VIO object providing basic
193 read/write operations.
194*/
195
197 MYSQL_PLUGIN_VIO *m_vio; ///< Pointer to @c MYSQL_PLUGIN_VIO structure.
198
199 /**
200 If non-zero, indicates that connection is broken. If this has happened
201 because of failed operation, stores non-zero error code from that failure.
202 */
204
205 public:
206 explicit Connection(MYSQL_PLUGIN_VIO *vio);
207 int write(const Blob &);
208 Blob read();
209
210 int error() const { return m_error; }
211};
212
213/** Sid class **************************************************************/
214
215/**
216 Class for storing and manipulating Windows security identifiers (SIDs).
217*/
218
219class Sid {
220 TOKEN_USER *m_data; ///< Pointer to structure holding identifier's data.
221 SID_NAME_USE m_type; ///< Type of identified entity.
222
223 public:
224 explicit Sid(const wchar_t *);
225 explicit Sid(HANDLE sec_token);
226 ~Sid();
227
228 bool is_valid(void) const;
229
230 bool is_group(void) const {
231 return m_type == SidTypeGroup || m_type == SidTypeWellKnownGroup ||
232 m_type == SidTypeAlias;
233 }
234
235 bool is_user(void) const { return m_type == SidTypeUser; }
236
237 bool operator==(const Sid &) const;
238
239 operator PSID() const { return (PSID)m_data->User.Sid; }
240
241#ifndef NDEBUG
242
243 private:
244 char *m_as_string; ///< Cached string representation of the SID.
245 public:
246 const char *as_string();
247
248#endif
249};
250
251/** UPN class **************************************************************/
252
253/**
254 An object of this class obtains and stores User Principal Name of the
255 account under which current process is running.
256*/
257
258class UPN {
259 char *m_buf; ///< Pointer to UPN in utf8 representation.
260 size_t m_len; ///< Length of the name.
261
262 public:
263 UPN();
264 ~UPN();
265
266 bool is_valid() const { return m_len > 0; }
267
268 const Blob as_blob() const {
269 return m_len ? Blob((byte *)m_buf, m_len) : Blob();
270 }
271
272 const char *as_string() const { return (const char *)m_buf; }
273};
274
275char *wchar_to_utf8(const wchar_t *, size_t *);
276wchar_t *utf8_to_wchar(const char *, size_t *);
277
278#endif
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
Class representing a region of memory (e.g., a string or binary buffer).
Definition: common.h:160
byte & operator[](unsigned pos) const
Definition: common.h:179
void trim(size_t l)
Definition: common.h:186
Blob()
Definition: common.h:165
bool is_null() const
Definition: common.h:184
size_t m_len
Length of the memory region.
Definition: common.h:162
byte * ptr() const
Definition: common.h:175
size_t len() const
Definition: common.h:177
byte * m_ptr
Pointer to the first byte of the memory region.
Definition: common.h:161
Blob(const byte *ptr, const size_t len)
Definition: common.h:167
Blob(const char *str)
Definition: common.h:170
Connection class.
Definition: common.h:196
int m_error
If non-zero, indicates that connection is broken.
Definition: common.h:203
Blob read()
Read data from connection.
Definition: common.cc:100
int error() const
Definition: common.h:210
MYSQL_PLUGIN_VIO * m_vio
Pointer to MYSQL_PLUGIN_VIO structure.
Definition: common.h:197
Connection(MYSQL_PLUGIN_VIO *vio)
Connection class.
Definition: common.cc:65
int write(const Blob &)
Write data to the connection.
Definition: common.cc:80
Sid class.
Definition: common.h:219
SID_NAME_USE m_type
Type of identified entity.
Definition: common.h:221
~Sid()
Definition: common.cc:230
bool is_group(void) const
Definition: common.h:230
bool is_valid(void) const
Check if Sid object is valid.
Definition: common.cc:238
bool is_user(void) const
Definition: common.h:235
bool operator==(const Sid &) const
Definition: common.cc:276
Sid(const wchar_t *)
Sid class.
Definition: common.cc:126
char * m_as_string
Cached string representation of the SID.
Definition: common.h:244
TOKEN_USER * m_data
Pointer to structure holding identifier's data.
Definition: common.h:220
const char * as_string()
Produces string representation of the SID.
Definition: common.cc:253
UPN class.
Definition: common.h:258
char * m_buf
Pointer to UPN in utf8 representation.
Definition: common.h:259
bool is_valid() const
Definition: common.h:266
const Blob as_blob() const
Definition: common.h:268
~UPN()
Definition: common.cc:327
const char * as_string() const
Definition: common.h:272
UPN()
Generating User Principal Name.
Definition: common.cc:289
size_t m_len
Length of the name.
Definition: common.h:260
const char * get_last_error_message(Error_message_buf)
Error handling.
Definition: common.cc:467
void set_log_level(unsigned int)
Definition: log_client.cc:59
unsigned char byte
Blob class.
Definition: common.h:151
void debug_msg(const char *fmt,...)
Definition: common.h:109
char * wchar_to_utf8(const wchar_t *, size_t *)
Convert a wide-char string to utf8 representation.
Definition: common.cc:347
char Error_message_buf[1024]
Definition: common.h:87
void error_log_print(const char *fmt,...)
Definition: common.h:80
unsigned int get_log_level(void)
Definition: log_client.cc:61
int opt_auth_win_log_level
Definition: common.h:51
void error_log_vprint(error_log_level::type level, const char *fmt, va_list args)
Definition: log_client.cc:31
wchar_t * utf8_to_wchar(const char *, size_t *)
Convert an utf8 string to a wide-char string.
Definition: common.cc:411
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1105
borrowable::binary::Blob< true > Blob
Definition: classic_protocol_binary.h:327
This file defines constants and data structures that are the same for both client- and server-side au...
Provides plugin access to communication channel.
Definition: plugin_auth_common.h:147
Debugging and error reporting infrastructure.
Definition: common.h:47
type
Definition: common.h:48
@ INFO
Definition: common.h:48
@ WARNING
Definition: common.h:48
@ ERROR
Definition: common.h:48
#define HANDLE
Definition: violite.h:159