MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
common.h
1 /* Copyright (c) 2011, 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 as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
15 
16 #ifndef COMMON_H
17 #define COMMON_H
18 
19 #include <my_global.h>
20 #include <windows.h>
21 #include <sspi.h> // for CtxtHandle
22 #include <mysql/plugin_auth.h> // for MYSQL_PLUGIN_VIO
23 
25 #define MAX_SERVICE_NAME_LENGTH 1024
26 
27 
30 /*
31  Note: We use plugin local logging and error reporting mechanisms until
32  WL#2940 (plugin service: error reporting) is available.
33 */
34 
35 #undef INFO
36 #undef WARNING
37 #undef ERROR
38 
40 {
41  typedef enum {INFO, WARNING, ERROR} type;
42 };
43 
44 extern "C" int opt_auth_win_log_level;
45 unsigned int get_log_level(void);
46 void set_log_level(unsigned int);
47 
48 
49 /*
50  If DEBUG_ERROR_LOG is defined then error logging happens only
51  in debug-copiled code. Otherwise ERROR_LOG() expands to
52  error_log_print() even in production code.
53 
54  Note: Macro ERROR_LOG() can use printf-like format string like this:
55 
56  ERROR_LOG(Level, ("format string", args));
57 
58  The implementation should handle it correctly. Currently it is passed
59  to fprintf() (see error_log_vprint() function).
60 */
61 
62 #if defined(DEBUG_ERROR_LOG) && defined(DBUG_OFF)
63 #define ERROR_LOG(Level, Msg) do {} while (0)
64 #else
65 #define ERROR_LOG(Level, Msg) error_log_print< error_log_level::Level > Msg
66 #endif
67 
68 
69 void error_log_vprint(error_log_level::type level,
70  const char *fmt, va_list args);
71 
72 template <error_log_level::type Level>
73 void error_log_print(const char *fmt, ...)
74 {
75  va_list args;
76  va_start(args, fmt);
77  error_log_vprint(Level, fmt, args);
78  va_end(args);
79 }
80 
81 typedef char Error_message_buf[1024];
82 const char* get_last_error_message(Error_message_buf);
83 
84 
85 /*
86  Internal implementation of debug message printing which does not use
87  dbug library. This is invoked via macro:
88 
89  DBUG_PRINT_DO(Keyword, ("format string", args));
90 
91  This is supposed to be used as an implementation of DBUG_PRINT() macro,
92  unless the dbug library implementation is used or debug messages are disabled.
93 */
94 
95 #ifndef DBUG_OFF
96 
97 #define DBUG_PRINT_DO(Keyword, Msg) \
98  do { \
99  if (4 > get_log_level()) break; \
100  fprintf(stderr, "winauth: %s: ", Keyword); \
101  debug_msg Msg; \
102  } while (0)
103 
104 inline
105 void debug_msg(const char *fmt, ...)
106 {
107  va_list args;
108  va_start(args, fmt);
109  vfprintf(stderr, fmt, args);
110  fputc('\n', stderr);
111  fflush(stderr);
112  va_end(args);
113 }
114 
115 #else
116 #define DBUG_PRINT_DO(K, M) do {} while (0)
117 #endif
118 
119 
120 #ifndef WINAUTH_USE_DBUG_LIB
121 
122 #undef DBUG_PRINT
123 #define DBUG_PRINT(Keyword, Msg) DBUG_PRINT_DO(Keyword, Msg)
124 
125 /*
126  Redefine few more debug macros to make sure that no symbols from
127  dbug library are used.
128 */
129 
130 #undef DBUG_ENTER
131 #define DBUG_ENTER(X) do {} while (0)
132 
133 #undef DBUG_RETURN
134 #define DBUG_RETURN(X) return (X)
135 
136 #undef DBUG_ASSERT
137 #ifndef DBUG_OFF
138 #define DBUG_ASSERT(X) assert (X)
139 #else
140 #define DBUG_ASSERT(X) do {} while (0)
141 #endif
142 
143 #undef DBUG_DUMP
144 #define DBUG_DUMP(A,B,C) do {} while (0)
145 
146 #endif
147 
148 
151 typedef unsigned char byte;
152 
160 class Blob
161 {
162  byte *m_ptr;
163  size_t m_len;
164 
165 public:
166 
167  Blob(): m_ptr(NULL), m_len(0)
168  {}
169 
170  Blob(const byte *ptr, const size_t len)
171  : m_ptr(const_cast<byte*>(ptr)), m_len(len)
172  {}
173 
174  Blob(const char *str): m_ptr((byte*)str)
175  {
176  m_len= strlen(str);
177  }
178 
179  byte* ptr() const
180  {
181  return m_ptr;
182  }
183 
184  size_t len() const
185  {
186  return m_len;
187  }
188 
189  byte& operator[](unsigned pos) const
190  {
191  static byte out_of_range= 0; // alas, no exceptions...
192  return pos < len() ? m_ptr[pos] : out_of_range;
193  }
194 
195  bool is_null() const
196  {
197  return m_ptr == NULL;
198  }
199 
200  void trim(size_t l)
201  {
202  m_len= l;
203  }
204 };
205 
206 
215 {
216  MYSQL_PLUGIN_VIO *m_vio;
217 
222  int m_error;
223 
224 public:
225 
227  int write(const Blob&);
228  Blob read();
229 
230  int error() const
231  {
232  return m_error;
233  }
234 };
235 
236 
243 class Sid
244 {
245  TOKEN_USER *m_data;
246  SID_NAME_USE m_type;
247 
248 public:
249 
250  Sid(const wchar_t*);
251  Sid(HANDLE sec_token);
252  ~Sid();
253 
254  bool is_valid(void) const;
255 
256  bool is_group(void) const
257  {
258  return m_type == SidTypeGroup
259  || m_type == SidTypeWellKnownGroup
260  || m_type == SidTypeAlias;
261  }
262 
263  bool is_user(void) const
264  {
265  return m_type == SidTypeUser;
266  }
267 
268  bool operator==(const Sid&);
269 
270  operator PSID() const
271  {
272  return (PSID)m_data->User.Sid;
273  }
274 
275 #ifndef DBUG_OFF
276 
277 private:
278  char *m_as_string;
279 public:
280  const char* as_string();
281 
282 #endif
283 };
284 
285 
293 class UPN
294 {
295  char *m_buf;
296  size_t m_len;
297 
298 public:
299 
300  UPN();
301  ~UPN();
302 
303  bool is_valid() const
304  {
305  return m_len > 0;
306  }
307 
308  const Blob as_blob() const
309  {
310  return m_len ? Blob((byte*)m_buf, m_len) : Blob();
311  }
312 
313  const char* as_string() const
314  {
315  return (const char*)m_buf;
316  }
317 
318 };
319 
320 
321 char* wchar_to_utf8(const wchar_t*, size_t*);
322 wchar_t* utf8_to_wchar(const char*, size_t*);
323 
324 #endif