1 /* Copyright (c) 2016, 2021, Oracle and/or its affiliates.
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 MYSQL_KEYRING_H
24 #define MYSQL_KEYRING_H
25
26 #include <my_global.h>
27 #include <mysql/plugin.h>
28 #include "keys_container.h"
29 #include "keys_iterator.h"
30 #include "keyring_memory.h"
31
32 using keyring::IKeys_container;
33 using keyring::Keys_iterator;
34 using keyring::IKeyring_io;
35 using keyring::ILogger;
36 using keyring::IKey;
37
38 namespace keyring
39 {
40 /* Always defined. */
41 extern PSI_memory_key key_memory_KEYRING;
42 extern PSI_rwlock_key key_LOCK_keyring;
43 }
44
45 extern mysql_rwlock_t LOCK_keyring;
46
47 extern boost::movelib::unique_ptr<IKeys_container> keys;
48 extern volatile my_bool is_keys_container_initialized;
49 extern boost::movelib::unique_ptr<ILogger> logger;
50 extern boost::movelib::unique_ptr<char[]> keyring_file_data;
51 extern my_bool keyring_open_mode;
52
53 #ifdef HAVE_PSI_INTERFACE
54 void keyring_init_psi_keys(void);
55 #endif //HAVE_PSI_INTERFACE
56
57 int init_keyring_locks();
58 my_bool create_keyring_dir_if_does_not_exist(const char *keyring_file_path);
59
60 void update_keyring_file_data(MYSQL_THD thd MY_ATTRIBUTE((unused)),
61 struct st_mysql_sys_var *var MY_ATTRIBUTE((unused)),
62 void *var_ptr MY_ATTRIBUTE((unused)),
63 const void *save_ptr);
64
65 my_bool mysql_key_fetch(boost::movelib::unique_ptr<IKey> key_to_fetch, char **key_type,
66 void **key, size_t *key_len);
67 my_bool mysql_key_store(boost::movelib::unique_ptr<IKey> key_to_store);
68 my_bool mysql_key_remove(boost::movelib::unique_ptr<IKey> key_to_remove);
69
70 my_bool mysql_keyring_iterator_init(Keys_iterator *);
71 void mysql_keyring_iterator_deinit(Keys_iterator *);
72 bool mysql_keyring_iterator_get_key(Keys_iterator *, char *key_id, char *user_id);
73
74 my_bool check_key_for_writing(IKey* key, std::string error_for);
75
76 void log_operation_error(const char *failed_operation, const char *plugin_name);
77
78 my_bool is_key_length_and_type_valid(const char *key_type, size_t key_len);
79
80 template <typename T>
mysql_key_fetch(const char * key_id,char ** key_type,const char * user_id,void ** key,size_t * key_len,const char * plugin_name)81 my_bool mysql_key_fetch(const char *key_id, char **key_type, const char *user_id,
82 void **key, size_t *key_len, const char *plugin_name)
83 {
84 try
85 {
86 boost::movelib::unique_ptr<IKey> key_to_fetch(new T(key_id, NULL, user_id, NULL, 0));
87 return mysql_key_fetch(::boost::move(key_to_fetch), key_type, key, key_len);
88 }
89 catch (...)
90 {
91 log_operation_error("fetch a key", plugin_name);
92 return TRUE;
93 }
94 }
95
96 template <typename T>
mysql_key_store(const char * key_id,const char * key_type,const char * user_id,const void * key,size_t key_len,const char * plugin_name)97 my_bool mysql_key_store(const char *key_id, const char *key_type, const char *user_id,
98 const void *key, size_t key_len, const char *plugin_name)
99 {
100 try
101 {
102 boost::movelib::unique_ptr<IKey> key_to_store(new T(key_id, key_type, user_id, key, key_len));
103 return mysql_key_store(::boost::move(key_to_store));
104 }
105 catch (...)
106 {
107 log_operation_error("store a key", plugin_name);
108 return TRUE;
109 }
110 }
111
112 template <typename T>
mysql_key_remove(const char * key_id,const char * user_id,const char * plugin_name)113 my_bool mysql_key_remove(const char *key_id, const char *user_id,
114 const char *plugin_name)
115 {
116 try
117 {
118 boost::movelib::unique_ptr<IKey> key_to_remove(new T(key_id, NULL, user_id, NULL, 0));
119 return mysql_key_remove(::boost::move(key_to_remove));
120 }
121 catch (...)
122 {
123 log_operation_error("remove a key", plugin_name);
124 return TRUE;
125 }
126 }
127
128 template <typename T>
mysql_key_iterator_init(Keys_iterator * key_iterator,const char * plugin_name)129 bool mysql_key_iterator_init(Keys_iterator *key_iterator, const char *plugin_name)
130 {
131 try
132 {
133 return mysql_keyring_iterator_init(key_iterator);
134 }
135 catch (...)
136 {
137 log_operation_error("iterator init", plugin_name);
138 return TRUE;
139 }
140 }
141
142 template <typename T>
mysql_key_iterator_deinit(Keys_iterator * key_iterator,const char * plugin_name)143 void mysql_key_iterator_deinit(Keys_iterator *key_iterator, const char *plugin_name)
144 {
145 try
146 {
147 mysql_keyring_iterator_deinit(key_iterator);
148 }
149 catch (...)
150 {
151 log_operation_error("iterator deinit", plugin_name);
152 }
153 }
154
155 template <typename T>
mysql_key_iterator_get_key(Keys_iterator * key_iterator,char * key_id,char * user_id,const char * plugin_name)156 bool mysql_key_iterator_get_key(Keys_iterator *key_iterator, char *key_id, char *user_id,
157 const char *plugin_name)
158 {
159 try
160 {
161 return mysql_keyring_iterator_get_key(key_iterator, key_id, user_id);
162 }
163 catch (...)
164 {
165 log_operation_error("iterator get_key", plugin_name);
166 return TRUE;
167 }
168 }
169
170 #endif //MYSQL_KEYRING_H
171