1 #ifndef SQL_COMMON_INCLUDED
2 #define SQL_COMMON_INCLUDED
3 
4 /* Copyright (c) 2003, 2018, 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 #ifdef	__cplusplus
29 extern "C" {
30 #endif
31 
32 #include <mysql.h>
33 #include <hash.h>
34 
35 extern const char	*unknown_sqlstate;
36 extern const char	*cant_connect_sqlstate;
37 extern const char	*not_error_sqlstate;
38 
39 
40 /*
41   Free all memory allocated in MYSQL handle except the
42   current options.
43 */
44 void mysql_close_free(MYSQL *mysql);
45 
46 /*
47   Clear connection options stored in MYSQL handle and
48   free memory used by them.
49 */
50 void mysql_close_free_options(MYSQL *mysql);
51 
52 
53 /**
54   The structure is used to hold the state change information
55   received from the server. LIST functions are used for manipulation
56   of the members of the structure.
57 */
58 typedef struct st_session_track_info_node {
59   /** head_node->data is a LEX_STRING which contains the variable name. */
60   LIST *head_node;
61   LIST *current_node;
62 } STATE_INFO_NODE;
63 
64 /**
65   Store the change info received from the server in an array of linked lists
66   with STATE_INFO_NODE elements (one per state type).
67 */
68 typedef struct st_session_track_info {
69   /** Array of STATE_NODE_INFO elements (one per state type). */
70   struct st_session_track_info_node info_list[SESSION_TRACK_END + 1];
71 } STATE_INFO;
72 
73 /*
74   Access to MYSQL::extension member.
75 
76   Note: functions mysql_extension_{init,free}() are defined
77   in client.c.
78 */
79 
80 struct st_mysql_trace_info;
81 
82 typedef struct st_mysql_extension {
83   struct st_mysql_trace_info *trace_data;
84   struct st_session_track_info state_change;
85 } MYSQL_EXTENSION;
86 
87 /* "Constructor/destructor" for MYSQL extension structure. */
88 struct st_mysql_extension* mysql_extension_init(struct st_mysql*);
89 void mysql_extension_free(struct st_mysql_extension*);
90 
91 /*
92   Note: Allocated extension structure is freed in mysql_close_free()
93   called by mysql_close().
94 */
95 #define MYSQL_EXTENSION_PTR(H)                                    \
96 (                                                                 \
97  (struct st_mysql_extension*)                                     \
98  ( (H)->extension ?                                               \
99    (H)->extension : ((H)->extension= mysql_extension_init(H))     \
100  )                                                                \
101 )
102 
103 
104 struct st_mysql_options_extention {
105   char *plugin_dir;
106   char *default_auth;
107   char *ssl_crl;				/* PEM CRL file */
108   char *ssl_crlpath;				/* PEM directory of CRL-s? */
109   HASH connection_attributes;
110   char *server_public_key_path;
111   size_t connection_attributes_length;
112   my_bool enable_cleartext_plugin;
113   my_bool get_server_public_key;
114   char *tls_version; /* TLS version option */
115   long ssl_ctx_flags; /* SSL ctx options flag */
116   unsigned int ssl_mode;
117 };
118 
119 typedef struct st_mysql_methods
120 {
121   my_bool (*read_query_result)(MYSQL *mysql);
122   my_bool (*advanced_command)(MYSQL *mysql,
123 			      enum enum_server_command command,
124 			      const unsigned char *header,
125 			      size_t header_length,
126 			      const unsigned char *arg,
127 			      size_t arg_length,
128 			      my_bool skip_check,
129                               MYSQL_STMT *stmt);
130   MYSQL_DATA *(*read_rows)(MYSQL *mysql,MYSQL_FIELD *mysql_fields,
131 			   unsigned int fields);
132   MYSQL_RES * (*use_result)(MYSQL *mysql);
133   void (*fetch_lengths)(unsigned long *to,
134 			MYSQL_ROW column, unsigned int field_count);
135   void (*flush_use_result)(MYSQL *mysql, my_bool flush_all_results);
136   int (*read_change_user_result)(MYSQL *mysql);
137 #if !defined(MYSQL_SERVER) || defined(EMBEDDED_LIBRARY)
138   MYSQL_FIELD * (*list_fields)(MYSQL *mysql);
139   my_bool (*read_prepare_result)(MYSQL *mysql, MYSQL_STMT *stmt);
140   int (*stmt_execute)(MYSQL_STMT *stmt);
141   int (*read_binary_rows)(MYSQL_STMT *stmt);
142   int (*unbuffered_fetch)(MYSQL *mysql, char **row);
143   void (*free_embedded_thd)(MYSQL *mysql);
144   const char *(*read_statistics)(MYSQL *mysql);
145   my_bool (*next_result)(MYSQL *mysql);
146   int (*read_rows_from_cursor)(MYSQL_STMT *stmt);
147   void (*free_rows)(MYSQL_DATA *cur);
148 #endif
149 } MYSQL_METHODS;
150 
151 #define simple_command(mysql, command, arg, length, skip_check) \
152   ((mysql)->methods \
153     ? (*(mysql)->methods->advanced_command)(mysql, command, 0, \
154                                             0, arg, length, skip_check, NULL) \
155     : (set_mysql_error(mysql, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate), 1))
156 #define stmt_command(mysql, command, arg, length, stmt) \
157   ((mysql)->methods \
158     ? (*(mysql)->methods->advanced_command)(mysql, command, 0,  \
159                                            0, arg, length, 1, stmt) \
160     : (set_mysql_error(mysql, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate), 1))
161 
162 extern CHARSET_INFO *default_client_charset_info;
163 MYSQL_FIELD *unpack_fields(MYSQL *mysql, MYSQL_ROWS *data,MEM_ROOT *alloc,
164                            uint fields, my_bool default_value,
165                            uint server_capabilities);
166 MYSQL_FIELD * cli_read_metadata_ex(MYSQL *mysql, MEM_ROOT *alloc,
167                                    unsigned long field_count,
168                                    unsigned int fields);
169 MYSQL_FIELD * cli_read_metadata(MYSQL *mysql, unsigned long field_count,
170                                unsigned int fields);
171 void free_rows(MYSQL_DATA *cur);
172 void free_old_query(MYSQL *mysql);
173 void end_server(MYSQL *mysql);
174 my_bool mysql_reconnect(MYSQL *mysql);
175 void mysql_read_default_options(struct st_mysql_options *options,
176 				const char *filename,const char *group);
177 my_bool
178 cli_advanced_command(MYSQL *mysql, enum enum_server_command command,
179 		     const unsigned char *header, size_t header_length,
180 		     const unsigned char *arg, size_t arg_length,
181                      my_bool skip_check, MYSQL_STMT *stmt);
182 unsigned long cli_safe_read(MYSQL *mysql, my_bool *is_data_packet);
183 unsigned long cli_safe_read_with_ok(MYSQL *mysql, my_bool parse_ok,
184                                     my_bool *is_data_packet);
185 void net_clear_error(NET *net);
186 void set_stmt_errmsg(MYSQL_STMT *stmt, NET *net);
187 void set_stmt_error(MYSQL_STMT *stmt, int errcode, const char *sqlstate,
188                     const char *err);
189 void set_mysql_error(MYSQL *mysql, int errcode, const char *sqlstate);
190 void set_mysql_extended_error(MYSQL *mysql, int errcode, const char *sqlstate,
191                               const char *format, ...);
192 #ifdef EMBEDDED_LIBRARY
193 int embedded_ssl_check(MYSQL *mysql);
194 #endif
195 
196 /* client side of the pluggable authentication */
197 struct st_plugin_vio_info;
198 void mpvio_info(Vio *vio, struct st_plugin_vio_info *info);
199 int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
200                     const char *data_plugin, const char *db);
201 int mysql_client_plugin_init();
202 void mysql_client_plugin_deinit();
203 
204 struct st_mysql_client_plugin;
205 extern struct st_mysql_client_plugin *mysql_client_builtins[];
206 uchar * send_client_connect_attrs(MYSQL *mysql, uchar *buf);
207 extern my_bool libmysql_cleartext_plugin_enabled;
208 int is_file_or_dir_world_writable(const char *filepath);
209 void read_ok_ex(MYSQL *mysql, unsigned long len);
210 
211 #ifdef	__cplusplus
212 }
213 #endif
214 
215 #define protocol_41(A) ((A)->server_capabilities & CLIENT_PROTOCOL_41)
216 
217 #endif /* SQL_COMMON_INCLUDED */
218