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