MySQL  8.0.22
Source Code Documentation
common.h
Go to the documentation of this file.
1 /* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
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 
50 extern "C" int opt_auth_win_log_level;
51 unsigned int get_log_level(void);
52 void 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(DBUG_OFF)
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 
75 void error_log_vprint(error_log_level::type level, const char *fmt,
76  va_list args);
77 
78 template <error_log_level::type Level>
79 void 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 
86 typedef 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 DBUG_OFF
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 
108 inline 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_ASSERT
142 #ifndef DBUG_OFF
143 #define DBUG_ASSERT(X) assert(X)
144 #else
145 #define DBUG_ASSERT(X) \
146  do { \
147  } while (0)
148 #endif
149 
150 #undef DBUG_DUMP
151 #define DBUG_DUMP(A, B, C) \
152  do { \
153  } while (0)
154 
155 #endif
156 
157 /** Blob class *************************************************************/
158 
159 typedef unsigned char byte;
160 
161 /**
162  Class representing a region of memory (e.g., a string or binary buffer).
163 
164  @note This class does not allocate memory. It merely describes a region
165  of memory which must be allocated externally (if it is dynamic memory).
166 */
167 
168 class Blob {
169  byte *m_ptr; ///< Pointer to the first byte of the memory region.
170  size_t m_len; ///< Length of the memory region.
171 
172  public:
173  Blob() : m_ptr(NULL), m_len(0) {}
174 
175  Blob(const byte *ptr, const size_t len)
176  : m_ptr(const_cast<byte *>(ptr)), m_len(len) {}
177 
178  Blob(const char *str) : m_ptr((byte *)str) { m_len = strlen(str); }
179 
180  byte *ptr() const { return m_ptr; }
181 
182  size_t len() const { return m_len; }
183 
184  byte &operator[](unsigned pos) const {
185  static byte out_of_range = 0; // alas, no exceptions...
186  return pos < len() ? m_ptr[pos] : out_of_range;
187  }
188 
189  bool is_null() const { return m_ptr == NULL; }
190 
191  void trim(size_t l) { m_len = l; }
192 };
193 
194 /** Connection class *******************************************************/
195 
196 /**
197  Convenience wrapper around MYSQL_PLUGIN_VIO object providing basic
198  read/write operations.
199 */
200 
201 class Connection {
202  MYSQL_PLUGIN_VIO *m_vio; ///< Pointer to @c MYSQL_PLUGIN_VIO structure.
203 
204  /**
205  If non-zero, indicates that connection is broken. If this has happened
206  because of failed operation, stores non-zero error code from that failure.
207  */
208  int m_error;
209 
210  public:
212  int write(const Blob &);
213  Blob read();
214 
215  int error() const { return m_error; }
216 };
217 
218 /** Sid class **************************************************************/
219 
220 /**
221  Class for storing and manipulating Windows security identifiers (SIDs).
222 */
223 
224 class Sid {
225  TOKEN_USER *m_data; ///< Pointer to structure holding identifier's data.
226  SID_NAME_USE m_type; ///< Type of identified entity.
227 
228  public:
229  Sid(const wchar_t *);
230  Sid(HANDLE sec_token);
231  ~Sid();
232 
233  bool is_valid(void) const;
234 
235  bool is_group(void) const {
236  return m_type == SidTypeGroup || m_type == SidTypeWellKnownGroup ||
237  m_type == SidTypeAlias;
238  }
239 
240  bool is_user(void) const { return m_type == SidTypeUser; }
241 
242  bool operator==(const Sid &);
243 
244  operator PSID() const { return (PSID)m_data->User.Sid; }
245 
246 #ifndef DBUG_OFF
247 
248  private:
249  char *m_as_string; ///< Cached string representation of the SID.
250  public:
251  const char *as_string();
252 
253 #endif
254 };
255 
256 /** UPN class **************************************************************/
257 
258 /**
259  An object of this class obtains and stores User Principal Name of the
260  account under which current process is running.
261 */
262 
263 class UPN {
264  char *m_buf; ///< Pointer to UPN in utf8 representation.
265  size_t m_len; ///< Length of the name.
266 
267  public:
268  UPN();
269  ~UPN();
270 
271  bool is_valid() const { return m_len > 0; }
272 
273  const Blob as_blob() const {
274  return m_len ? Blob((byte *)m_buf, m_len) : Blob();
275  }
276 
277  const char *as_string() const { return (const char *)m_buf; }
278 };
279 
280 char *wchar_to_utf8(const wchar_t *, size_t *);
281 wchar_t *utf8_to_wchar(const char *, size_t *);
282 
283 #endif
Blob()
Definition: common.h:173
bool read(T *ap, const GV &gv, const char *key)
Definition: sdi_impl.h:339
size_t m_len
Length of the name.
Definition: common.h:265
size_t len() const
Definition: common.h:182
int m_error
If non-zero, indicates that connection is broken.
Definition: common.h:208
Blob(const byte *ptr, const size_t len)
Definition: common.h:175
Class representing a region of memory (e.g., a string or binary buffer).
Definition: common.h:168
UPN class.
Definition: common.h:263
char Error_message_buf[1024]
Definition: common.h:86
byte * m_ptr
Pointer to the first byte of the memory region.
Definition: common.h:169
Debugging and error reporting infrastructure.
Definition: common.h:46
bool operator==(const Uuid &a, const Uuid &b)
Definition: uuid.h:169
bool is_null() const
Definition: common.h:189
Blob(const char *str)
Definition: common.h:178
char * m_buf
Pointer to UPN in utf8 representation.
Definition: common.h:264
byte & operator[](unsigned pos) const
Definition: common.h:184
bool is_valid(const dd::Spatial_reference_system *srs, const Geometry *g, const char *func_name, bool *is_valid) noexcept
Decides if a geometry is valid.
Definition: is_valid.cc:94
type
Definition: common.h:47
char * m_as_string
Cached string representation of the SID.
Definition: common.h:249
void write(W *w, const T &t, const char *key, size_t key_sz)
Definition: sdi_impl.h:333
const char * get_last_error_message(Error_message_buf)
Error handling.
Definition: common.cc:465
void error_log_vprint(error_log_level::type level, const char *fmt, va_list args)
Definition: log_client.cc:30
byte * ptr() const
Definition: common.h:180
Definition: common.h:47
const Blob as_blob() const
Definition: common.h:273
Sid class.
Definition: common.h:224
char * pos
Definition: do_ctype.cc:76
const char * as_string() const
Definition: common.h:277
SID_NAME_USE m_type
Type of identified entity.
Definition: common.h:226
int opt_auth_win_log_level
Definition: common.h:50
void error_log_print(const char *fmt,...)
Definition: common.h:79
bool is_valid() const
Definition: common.h:271
#define HANDLE
Definition: violite.h:158
unsigned int get_log_level(void)
Definition: log_client.cc:60
#define NULL
Definition: types.h:54
TOKEN_USER * m_data
Pointer to structure holding identifier&#39;s data.
Definition: common.h:225
Definition: common.h:47
This file defines constants and data structures that are the same for both client- and server-side au...
char * wchar_to_utf8(const wchar_t *, size_t *)
Convert a wide-char string to utf8 representation.
Definition: common.cc:346
size_t m_len
Length of the memory region.
Definition: common.h:170
void debug_msg(const char *fmt,...)
Definition: common.h:108
wchar_t * utf8_to_wchar(const char *, size_t *)
Convert an utf8 string to a wide-char string.
Definition: common.cc:409
Provides plugin access to communication channel.
Definition: plugin_auth_common.h:140
bool is_user(void) const
Definition: common.h:240
int error() const
Definition: common.h:215
bool is_group(void) const
Definition: common.h:235
Connection class.
Definition: common.h:201
unsigned char byte
Blob class.
Definition: common.h:159
MYSQL_PLUGIN_VIO * m_vio
Pointer to MYSQL_PLUGIN_VIO structure.
Definition: common.h:202
Definition: common.h:47
void trim(size_t l)
Definition: common.h:191
void set_log_level(unsigned int)
Definition: log_client.cc:58