| 1 |
/* Copyright (c) 2021, 2026, 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 designed to work 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 either included with |
| 13 |
the program or referenced in the documentation. |
| 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 |
#include <memory> |
| 25 |
|
| 26 |
#include "my_rapidjson_size_t.h" |
| 27 |
|
| 28 |
#include <rapidjson/document.h> |
| 29 |
#include <rapidjson/stringbuffer.h> |
| 30 |
#include <rapidjson/writer.h> |
| 31 |
|
| 32 |
#include <components/keyrings/keyring_file/keyring_file.h> |
| 33 |
|
| 34 |
#include <components/keyrings/common/config/config_reader.h> /* Config_reader */ |
| 35 |
#include <include/mysql/components/component_implementation.h> |
| 36 |
|
| 37 |
using keyring_common::config::Config_reader; |
| 38 |
using keyring_file::g_config_pod; |
| 39 |
|
| 40 |
/** |
| 41 |
In order to locate a shared library, we need it to export at least |
| 42 |
one symbol. This way dlsym/GetProcAddress will be able to find it. |
| 43 |
*/ |
| 44 |
DLL_EXPORT int keyring_file_component_exported_symbol() { return 0; } |
| 45 |
|
| 46 |
namespace keyring_file { |
| 47 |
|
| 48 |
namespace config { |
| 49 |
|
| 50 |
char *g_component_path = nullptr; |
| 51 |
char *g_instance_path = nullptr; |
| 52 |
|
| 53 |
/* Component metadata */ |
| 54 |
static const char *s_component_metadata[][2] = { |
| 55 |
{"Component_name", "component_keyring_file"}, |
| 56 |
{"Author", "Oracle Corporation"}, |
| 57 |
{"License", "GPL"}, |
| 58 |
{"Implementation_name", "component_keyring_file"}, |
| 59 |
{"Version", "1.0"}}; |
| 60 |
|
| 61 |
/* Config file name */ |
| 62 |
const std::string config_file_name = "component_keyring_file.cnf"; |
| 63 |
|
| 64 |
/* Config names */ |
| 65 |
const std::string config_options[] = {"read_local_config", "path", "read_only"}; |
| 66 |
|
| 67 |
bool find_and_read_config_file(std::unique_ptr<Config_pod> &config_pod) { |
| 68 |
config_pod = std::make_unique<Config_pod>(); |
| 69 |
if (!config_pod) return true; |
| 70 |
/* Get shared library location */ |
| 71 |
std::string path(g_component_path); |
| 72 |
|
| 73 |
auto set_config_path = [](std::string &full_path) -> bool { |
| 74 |
if (full_path.length() == 0) return true; |
| 75 |
#ifdef _WIN32 |
| 76 |
full_path += "\\"; |
| 77 |
#else |
| 78 |
full_path += "/"; |
| 79 |
#endif |
| 80 |
full_path.append(config_file_name); |
| 81 |
return false; |
| 82 |
}; |
| 83 |
if (set_config_path(path) == true) return true; |
| 84 |
|
| 85 |
/* Read config file that's located at shared library location */ |
| 86 |
std::unique_ptr<Config_reader> config_reader(new (std::nothrow) |
| 87 |
Config_reader(path)); |
| 88 |
|
| 89 |
{ |
| 90 |
bool read_local_config = false; |
| 91 |
if (config_reader->get_element<bool>(config_options[0], |
| 92 |
read_local_config) == false) { |
| 93 |
if (read_local_config == true) { |
| 94 |
config_reader.reset(); |
| 95 |
/* |
| 96 |
Read config file from current working directory |
| 97 |
We assume that when control reaches here, binary has set |
| 98 |
current working directory appropriately. |
| 99 |
*/ |
| 100 |
std::string instance_path(g_instance_path); |
| 101 |
if (set_config_path(instance_path) == true) |
| 102 |
instance_path = config_file_name; |
| 103 |
config_reader = std::make_unique<Config_reader>(instance_path); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if (config_reader->get_element<std::string>( |
| 109 |
config_options[1], config_pod.get()->config_file_path_) || |
| 110 |
config_reader->get_element<bool>(config_options[2], |
| 111 |
config_pod.get()->read_only_)) { |
| 112 |
config_pod.reset(); |
| 113 |
return true; |
| 114 |
} |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
bool create_config( |
| 119 |
std::unique_ptr<std::vector<std::pair<std::string, std::string>>> |
| 120 |
&metadata) { |
| 121 |
metadata = |
| 122 |
std::make_unique<std::vector<std::pair<std::string, std::string>>>(); |
| 123 |
if (metadata.get() == nullptr) return true; |
| 124 |
keyring_file::config::Config_pod config_pod; |
| 125 |
bool global_config_available = false; |
| 126 |
if (g_config_pod != nullptr) { |
| 127 |
config_pod = *g_config_pod; |
| 128 |
global_config_available = true; |
| 129 |
} |
| 130 |
|
| 131 |
for (auto entry : keyring_file::config::s_component_metadata) { |
| 132 |
metadata.get()->push_back(std::make_pair(entry[0], entry[1])); |
| 133 |
} |
| 134 |
|
| 135 |
/* Status */ |
| 136 |
metadata.get()->push_back(std::make_pair( |
| 137 |
"Component_status", |
| 138 |
keyring_file::g_component_callbacks->keyring_initialized() ? "Active" |
| 139 |
: "Disabled")); |
| 140 |
|
| 141 |
/* Backend file */ |
| 142 |
metadata.get()->push_back(std::make_pair( |
| 143 |
"Data_file", ((global_config_available == true) |
| 144 |
? ((config_pod.config_file_path_.length() == 0) |
| 145 |
? "<NONE>" |
| 146 |
: config_pod.config_file_path_) |
| 147 |
: "<NOT APPLICABLE>"))); |
| 148 |
|
| 149 |
/* Read only flag */ |
| 150 |
metadata.get()->push_back(std::make_pair( |
| 151 |
"Read_only", ((global_config_available == true) |
| 152 |
? ((config_pod.read_only_ == true) ? "Yes" : "No") |
| 153 |
: "<NOT APPLICABLE>"))); |
| 154 |
|
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
} // namespace config |
| 159 |
} // namespace keyring_file |