1 /*
2    Copyright (c) 2014, 2021, Oracle and/or its affiliates.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #ifndef MYSQL_CONNECTION_OPTIONS_INCLUDED
26 #define MYSQL_CONNECTION_OPTIONS_INCLUDED
27 
28 #include "client_priv.h"
29 #include <vector>
30 #include "composite_options_provider.h"
31 #include "abstract_program.h"
32 #include "i_connection_factory.h"
33 #include "nullable.h"
34 #include "base/mutex.h"
35 
36 namespace Mysql{
37 namespace Tools{
38 namespace Base{
39 namespace Options{
40 
41 /**
42   Options provider providing options to specify connection to MySQL server.
43  */
44 class Mysql_connection_options
45   : public Composite_options_provider, I_connection_factory
46 {
47 private:
48   /**
49     Options provider enclosing options related to SSL settings of connection
50     to MySQL server.
51    */
52   class Ssl_options : public Abstract_options_provider
53   {
54 public:
55     /**
56       Creates all options that will be provided.
57       Implementation of Abstract_options_provider virtual method.
58      */
59     void create_options();
60 
61     /**
62       Applies option values to MYSQL connection structure.
63      */
64     void apply_for_connection(MYSQL* connection);
65 
66 private:
67     Nullable<std::string> m_ssl_mode_string;
68     bool m_ssl;
69     bool m_ssl_verify_server_cert;
70 
71     void ca_option_callback(char *argument);
72     void mode_option_callback(char *argument);
73     void use_ssl_option_callback(char *argument);
74     void ssl_verify_server_cert_callback(char *argument);
75   };
76 
77 public:
78   /**
79     Constructs new MySQL server connection options provider. Calling this
80     function from multiple threads simultaneously is not thread safe.
81     @param program Pointer to main program class.
82    */
83   Mysql_connection_options(Abstract_program *program);
84 
85   /**
86     Creates all options that will be provided.
87     Implementation of Abstract_options_provider virtual method.
88    */
89   void create_options();
90 
91   /**
92     Provides new connection to MySQL database server based on option values.
93     Implementation of I_connection_factory interface.
94    */
95   MYSQL* create_connection();
96 
97   /**
98     Retrieves charset that will be used in new MySQL connections.. Can be NULL
99     if none was set explicitly.
100    */
101   CHARSET_INFO* get_current_charset() const;
102 
103   /**
104     Sets charset that will be used in new MySQL connections.
105    */
106   void set_current_charset(CHARSET_INFO* charset);
107 
108 private:
109   /**
110     Returns pointer to constant array containing specified string or NULL
111     value if string has length 0.
112    */
113   const char* get_null_or_string(Nullable<std::string>& maybeString);
114 
115   /**
116     Prints database connection error and exits program.
117    */
118   void db_error(MYSQL* connection, const char* when);
119 #ifdef _WIN32
120   void pipe_protocol_callback(char* not_used MY_ATTRIBUTE((unused)));
121 #endif
122   void protocol_callback(char* not_used MY_ATTRIBUTE((unused)));
123   void secure_auth_callback(char* argument MY_ATTRIBUTE((unused)));
124 
125   static bool mysql_inited;
126 
127   Ssl_options m_ssl_options_provider;
128   Abstract_program *m_program;
129   Nullable<std::string> m_protocol_string;
130   uint32 m_protocol;
131   Nullable<std::string> m_bind_addr;
132   Nullable<std::string> m_host;
133   uint32 m_mysql_port;
134   Nullable<std::string> m_mysql_unix_port;
135 #if defined (_WIN32) && !defined (EMBEDDED_LIBRARY)
136   Nullable<std::string> m_shared_memory_base_name;
137 #endif
138   Nullable<std::string> m_default_auth;
139   bool m_secure_auth;
140   Nullable<std::string> m_plugin_dir;
141   uint32 m_net_buffer_length;
142   uint32 m_max_allowed_packet;
143   bool m_compress;
144   Nullable<std::string> m_user;
145   Nullable<std::string> m_password;
146   Nullable<std::string> m_default_charset;
147   Nullable<std::string> m_server_public_key;
148   bool m_get_server_public_key;
149 };
150 
151 }
152 }
153 }
154 }
155 
156 #endif
157