1 #ifndef SQL_COMMON_INCLUDED
2 #define SQL_COMMON_INCLUDED
3 
4 /* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License, version 2.0,
8    as published by the Free Software Foundation.
9 
10    This program is also distributed with certain software (including
11    but not limited to OpenSSL) that is licensed under separate terms,
12    as designated in a particular file or component or in included license
13    documentation.  The authors of MySQL hereby grant you an additional
14    permission to link the program and your derivative works with the
15    separately licensed software that they have included with MySQL.
16 
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License, version 2.0, for more details.
21 
22    You should have received a copy of the GNU General Public License
23    along with this program; if not, write to the Free Software
24    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
25 
26 #define SQL_COMMON_INCLUDED
27 
28 /**
29   @file include/sql_common.h
30 */
31 
32 #include <mysql.h>
33 #include <stddef.h>
34 #include <sys/types.h>
35 
36 #include "errmsg.h"
37 #include "my_command.h"
38 #include "my_compiler.h"
39 #include "my_inttypes.h"
40 #include "my_list.h"
41 #include "mysql_com.h"
42 #ifdef MYSQL_SERVER
43 #include "mysql_com_server.h"
44 #endif
45 struct MEM_ROOT;
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 extern const char *unknown_sqlstate;
52 extern const char *cant_connect_sqlstate;
53 extern const char *not_error_sqlstate;
54 
55 /*
56   Free all memory allocated in MYSQL handle except the
57   current options.
58 */
59 void mysql_close_free(MYSQL *mysql);
60 
61 /*
62   Clear connection options stored in MYSQL handle and
63   free memory used by them.
64 */
65 void mysql_close_free_options(MYSQL *mysql);
66 
67 /**
68   The structure is used to hold the state change information
69   received from the server. LIST functions are used for manipulation
70   of the members of the structure.
71 */
72 struct STATE_INFO_NODE {
73   /** head_node->data is a LEX_STRING which contains the variable name. */
74   LIST *head_node;
75   LIST *current_node;
76 };
77 
78 /**
79   Store the change info received from the server in an array of linked lists
80   with STATE_INFO_NODE elements (one per state type).
81 */
82 struct STATE_INFO {
83   /** Array of STATE_NODE_INFO elements (one per state type). */
84   STATE_INFO_NODE info_list[SESSION_TRACK_END + 1];
85 };
86 
87 /*
88   Access to MYSQL::extension member.
89 
90   Note: functions mysql_extension_{init,free}() are defined
91   in client.c.
92 */
93 
94 struct st_mysql_trace_info;
95 
96 struct MYSQL_EXTENSION {
97   struct st_mysql_trace_info *trace_data;
98   STATE_INFO state_change;
99   /* Struct to track the state of asynchronous operations */
100   struct MYSQL_ASYNC *mysql_async_context;
101 #ifdef MYSQL_SERVER
102   // Used by replication to pass around compression context data.
103   NET_SERVER *server_extn;
104 #endif
105 };
106 
107 /* "Constructor/destructor" for MYSQL extension structure. */
108 MYSQL_EXTENSION *mysql_extension_init(MYSQL *);
109 void mysql_extension_free(MYSQL_EXTENSION *);
110 /*
111   Note: Allocated extension structure is freed in mysql_close_free()
112   called by mysql_close().
113 */
114 #define MYSQL_EXTENSION_PTR(H)              \
115   ((MYSQL_EXTENSION *)((H)->extension       \
116                            ? (H)->extension \
117                            : ((H)->extension = mysql_extension_init(H))))
118 
119 #define ASYNC_DATA(M) \
120   (NULL != (M) ? (MYSQL_EXTENSION_PTR(M)->mysql_async_context) : NULL)
121 #ifdef MYSQL_SERVER
mysql_extension_set_server_extn(MYSQL * mysql,NET_SERVER * extn)122 inline void mysql_extension_set_server_extn(MYSQL *mysql, NET_SERVER *extn) {
123   MYSQL_EXTENSION_PTR(mysql)->server_extn = extn;
124 }
125 #endif
126 
127 struct st_mysql_options_extention {
128   char *plugin_dir;
129   char *default_auth;
130   char *ssl_crl;     /* PEM CRL file */
131   char *ssl_crlpath; /* PEM directory of CRL-s? */
132   struct My_hash *connection_attributes;
133   char *server_public_key_path;
134   size_t connection_attributes_length;
135   bool enable_cleartext_plugin;
136   bool get_server_public_key; /* Former ssl_enforce */
137   char *tls_version;          /* TLS version option */
138   long ssl_ctx_flags;         /* SSL ctx options flag */
139   unsigned int ssl_mode;
140   unsigned int retry_count;
141   unsigned int ssl_fips_mode; /* SSL fips mode for enforced encryption.*/
142   char *tls_ciphersuites;
143   char *compression_algorithm;
144   unsigned int total_configured_compression_algorithms;
145   unsigned int zstd_compression_level;
146   bool connection_compressed;
147   char *load_data_dir;
148 };
149 
150 struct MYSQL_METHODS {
151   bool (*read_query_result)(MYSQL *mysql);
152   bool (*advanced_command)(MYSQL *mysql, enum enum_server_command command,
153                            const unsigned char *header, size_t header_length,
154                            const unsigned char *arg, size_t arg_length,
155                            bool skip_check, MYSQL_STMT *stmt);
156   MYSQL_DATA *(*read_rows)(MYSQL *mysql, MYSQL_FIELD *mysql_fields,
157                            unsigned int fields);
158   MYSQL_RES *(*use_result)(MYSQL *mysql);
159   void (*fetch_lengths)(unsigned long *to, MYSQL_ROW column,
160                         unsigned int field_count);
161   void (*flush_use_result)(MYSQL *mysql, bool flush_all_results);
162   int (*read_change_user_result)(MYSQL *mysql);
163 #if !defined(MYSQL_SERVER) || defined(XTRABACKUP)
164   MYSQL_FIELD *(*list_fields)(MYSQL *mysql);
165   bool (*read_prepare_result)(MYSQL *mysql, MYSQL_STMT *stmt);
166   int (*stmt_execute)(MYSQL_STMT *stmt);
167   int (*read_binary_rows)(MYSQL_STMT *stmt);
168   int (*unbuffered_fetch)(MYSQL *mysql, char **row);
169   const char *(*read_statistics)(MYSQL *mysql);
170   bool (*next_result)(MYSQL *mysql);
171   int (*read_rows_from_cursor)(MYSQL_STMT *stmt);
172   void (*free_rows)(MYSQL_DATA *cur);
173 #endif
174   enum net_async_status (*read_query_result_nonblocking)(MYSQL *mysql);
175   enum net_async_status (*advanced_command_nonblocking)(
176       MYSQL *mysql, enum enum_server_command command,
177       const unsigned char *header, unsigned long header_length,
178       const unsigned char *arg, unsigned long arg_length, bool skip_check,
179       MYSQL_STMT *stmt, bool *error);
180   enum net_async_status (*read_rows_nonblocking)(MYSQL *mysql,
181                                                  MYSQL_FIELD *mysql_fields,
182                                                  unsigned int fields,
183                                                  MYSQL_DATA **result);
184   enum net_async_status (*flush_use_result_nonblocking)(MYSQL *mysql,
185                                                         bool flush_all_results);
186   enum net_async_status (*next_result_nonblocking)(MYSQL *mysql);
187   enum net_async_status (*read_change_user_result_nonblocking)(MYSQL *mysql,
188                                                                ulong *res);
189 };
190 
191 #define simple_command(mysql, command, arg, length, skip_check)              \
192   ((mysql)->methods                                                          \
193        ? (*(mysql)->methods->advanced_command)(mysql, command, 0, 0, arg,    \
194                                                length, skip_check, NULL)     \
195        : (set_mysql_error(mysql, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate), \
196           1))
197 #define simple_command_nonblocking(mysql, command, arg, length, skip_check, \
198                                    error)                                   \
199   (*(mysql)->methods->advanced_command_nonblocking)(                        \
200       mysql, command, 0, 0, arg, length, skip_check, NULL, error)
201 
202 #define stmt_command(mysql, command, arg, length, stmt)                      \
203   ((mysql)->methods                                                          \
204        ? (*(mysql)->methods->advanced_command)(mysql, command, 0, 0, arg,    \
205                                                length, 1, stmt)              \
206        : (set_mysql_error(mysql, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate), \
207           1))
208 
209 extern CHARSET_INFO *default_client_charset_info;
210 MYSQL_FIELD *unpack_fields(MYSQL *mysql, MYSQL_ROWS *data, MEM_ROOT *alloc,
211                            uint fields, bool default_value,
212                            uint server_capabilities);
213 MYSQL_FIELD *cli_read_metadata_ex(MYSQL *mysql, MEM_ROOT *alloc,
214                                   unsigned long field_count,
215                                   unsigned int fields);
216 MYSQL_FIELD *cli_read_metadata(MYSQL *mysql, unsigned long field_count,
217                                unsigned int fields);
218 void free_rows(MYSQL_DATA *cur);
219 void free_old_query(MYSQL *mysql);
220 void end_server(MYSQL *mysql);
221 bool mysql_reconnect(MYSQL *mysql);
222 void mysql_read_default_options(struct st_mysql_options *options,
223                                 const char *filename, const char *group);
224 bool cli_advanced_command(MYSQL *mysql, enum enum_server_command command,
225                           const unsigned char *header, size_t header_length,
226                           const unsigned char *arg, size_t arg_length,
227                           bool skip_check, MYSQL_STMT *stmt);
228 unsigned long cli_safe_read(MYSQL *mysql, bool *is_data_packet);
229 enum net_async_status cli_safe_read_nonblocking(MYSQL *mysql,
230                                                 bool *is_data_packet,
231                                                 ulong *res);
232 unsigned long cli_safe_read_with_ok(MYSQL *mysql, bool parse_ok,
233                                     bool *is_data_packet);
234 void net_clear_error(NET *net);
235 void set_stmt_errmsg(MYSQL_STMT *stmt, NET *net);
236 void set_stmt_error(MYSQL_STMT *stmt, int errcode, const char *sqlstate,
237                     const char *err);
238 void set_mysql_error(MYSQL *mysql, int errcode, const char *sqlstate);
239 void set_mysql_extended_error(MYSQL *mysql, int errcode, const char *sqlstate,
240                               const char *format, ...)
241     MY_ATTRIBUTE((format(printf, 4, 5)));
242 
243 /* client side of the pluggable authentication */
244 struct MYSQL_PLUGIN_VIO_INFO;
245 
246 void mpvio_info(MYSQL_VIO vio, MYSQL_PLUGIN_VIO_INFO *info);
247 int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
248                     const char *data_plugin, const char *db);
249 int mysql_client_plugin_init();
250 void mysql_client_plugin_deinit();
251 
252 struct st_mysql_client_plugin;
253 
254 extern struct st_mysql_client_plugin *mysql_client_builtins[];
255 uchar *send_client_connect_attrs(MYSQL *mysql, uchar *buf);
256 extern bool libmysql_cleartext_plugin_enabled;
257 int is_file_or_dir_world_writable(const char *filepath);
258 void read_ok_ex(MYSQL *mysql, unsigned long len);
259 
260 #ifdef __cplusplus
261 }
262 #endif
263 
264 #define protocol_41(A) ((A)->server_capabilities & CLIENT_PROTOCOL_41)
265 
266 #endif /* SQL_COMMON_INCLUDED */
267