/root/bitcoin/src/common/config.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2023 The Bitcoin Core developers |
2 | | // Distributed under the MIT software license, see the accompanying |
3 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | | |
5 | | #include <common/args.h> |
6 | | |
7 | | #include <common/settings.h> |
8 | | #include <logging.h> |
9 | | #include <sync.h> |
10 | | #include <tinyformat.h> |
11 | | #include <univalue.h> |
12 | | #include <util/chaintype.h> |
13 | | #include <util/fs.h> |
14 | | #include <util/string.h> |
15 | | |
16 | | #include <algorithm> |
17 | | #include <cassert> |
18 | | #include <cstdlib> |
19 | | #include <filesystem> |
20 | | #include <fstream> |
21 | | #include <iostream> |
22 | | #include <list> |
23 | | #include <map> |
24 | | #include <memory> |
25 | | #include <optional> |
26 | | #include <string> |
27 | | #include <string_view> |
28 | | #include <utility> |
29 | | #include <vector> |
30 | | |
31 | | using util::TrimString; |
32 | | using util::TrimStringView; |
33 | | |
34 | | static bool GetConfigOptions(std::istream& stream, const std::string& filepath, std::string& error, std::vector<std::pair<std::string, std::string>>& options, std::list<SectionInfo>& sections) |
35 | 0 | { |
36 | 0 | std::string str, prefix; |
37 | 0 | std::string::size_type pos; |
38 | 0 | int linenr = 1; |
39 | 0 | while (std::getline(stream, str)) { Branch (39:12): [True: 0, False: 0]
|
40 | 0 | bool used_hash = false; |
41 | 0 | if ((pos = str.find('#')) != std::string::npos) { Branch (41:13): [True: 0, False: 0]
|
42 | 0 | str = str.substr(0, pos); |
43 | 0 | used_hash = true; |
44 | 0 | } |
45 | 0 | const static std::string pattern = " \t\r\n"; |
46 | 0 | str = TrimString(str, pattern); |
47 | 0 | if (!str.empty()) { Branch (47:13): [True: 0, False: 0]
|
48 | 0 | if (*str.begin() == '[' && *str.rbegin() == ']') { Branch (48:17): [True: 0, False: 0]
Branch (48:17): [True: 0, False: 0]
Branch (48:40): [True: 0, False: 0]
|
49 | 0 | const std::string section = str.substr(1, str.size() - 2); |
50 | 0 | sections.emplace_back(SectionInfo{section, filepath, linenr}); |
51 | 0 | prefix = section + '.'; |
52 | 0 | } else if (*str.begin() == '-') { Branch (52:24): [True: 0, False: 0]
|
53 | 0 | error = strprintf("parse error on line %i: %s, options in configuration file must be specified without leading -", linenr, str); |
54 | 0 | return false; |
55 | 0 | } else if ((pos = str.find('=')) != std::string::npos) { Branch (55:24): [True: 0, False: 0]
|
56 | 0 | std::string name = prefix + TrimString(std::string_view{str}.substr(0, pos), pattern); |
57 | 0 | std::string_view value = TrimStringView(std::string_view{str}.substr(pos + 1), pattern); |
58 | 0 | if (used_hash && name.find("rpcpassword") != std::string::npos) { Branch (58:21): [True: 0, False: 0]
Branch (58:34): [True: 0, False: 0]
|
59 | 0 | error = strprintf("parse error on line %i, using # in rpcpassword can be ambiguous and should be avoided", linenr); |
60 | 0 | return false; |
61 | 0 | } |
62 | 0 | options.emplace_back(name, value); |
63 | 0 | if ((pos = name.rfind('.')) != std::string::npos && prefix.length() <= pos) { Branch (63:21): [True: 0, False: 0]
Branch (63:69): [True: 0, False: 0]
|
64 | 0 | sections.emplace_back(SectionInfo{name.substr(0, pos), filepath, linenr}); |
65 | 0 | } |
66 | 0 | } else { |
67 | 0 | error = strprintf("parse error on line %i: %s", linenr, str); |
68 | 0 | if (str.size() >= 2 && str.substr(0, 2) == "no") { Branch (68:21): [True: 0, False: 0]
Branch (68:21): [True: 0, False: 0]
Branch (68:40): [True: 0, False: 0]
|
69 | 0 | error += strprintf(", if you intended to specify a negated option, use %s=1 instead", str); |
70 | 0 | } |
71 | 0 | return false; |
72 | 0 | } |
73 | 0 | } |
74 | 0 | ++linenr; |
75 | 0 | } |
76 | 0 | return true; |
77 | 0 | } |
78 | | |
79 | 0 | bool IsConfSupported(KeyInfo& key, std::string& error) { |
80 | 0 | if (key.name == "conf") { Branch (80:9): [True: 0, False: 0]
|
81 | 0 | error = "conf cannot be set in the configuration file; use includeconf= if you want to include additional config files"; |
82 | 0 | return false; |
83 | 0 | } |
84 | 0 | if (key.name == "reindex") { Branch (84:9): [True: 0, False: 0]
|
85 | | // reindex can be set in a config file but it is strongly discouraged as this will cause the node to reindex on |
86 | | // every restart. Allow the config but throw a warning |
87 | 0 | LogPrintf("Warning: reindex=1 is set in the configuration file, which will significantly slow down startup. Consider removing or commenting out this option for better performance, unless there is currently a condition which makes rebuilding the indexes necessary\n"); |
88 | 0 | return true; |
89 | 0 | } |
90 | 0 | return true; |
91 | 0 | } |
92 | | |
93 | | bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& filepath, std::string& error, bool ignore_invalid_keys) |
94 | 0 | { |
95 | 0 | LOCK(cs_args); |
96 | 0 | std::vector<std::pair<std::string, std::string>> options; |
97 | 0 | if (!GetConfigOptions(stream, filepath, error, options, m_config_sections)) { Branch (97:9): [True: 0, False: 0]
|
98 | 0 | return false; |
99 | 0 | } |
100 | 0 | for (const std::pair<std::string, std::string>& option : options) { Branch (100:60): [True: 0, False: 0]
|
101 | 0 | KeyInfo key = InterpretKey(option.first); |
102 | 0 | std::optional<unsigned int> flags = GetArgFlags('-' + key.name); |
103 | 0 | if (!IsConfSupported(key, error)) return false; Branch (103:13): [True: 0, False: 0]
|
104 | 0 | if (flags) { Branch (104:13): [True: 0, False: 0]
|
105 | 0 | std::optional<common::SettingsValue> value = InterpretValue(key, &option.second, *flags, error); |
106 | 0 | if (!value) { Branch (106:17): [True: 0, False: 0]
|
107 | 0 | return false; |
108 | 0 | } |
109 | 0 | m_settings.ro_config[key.section][key.name].push_back(*value); |
110 | 0 | } else { |
111 | 0 | if (ignore_invalid_keys) { Branch (111:17): [True: 0, False: 0]
|
112 | 0 | LogPrintf("Ignoring unknown configuration value %s\n", option.first); |
113 | 0 | } else { |
114 | 0 | error = strprintf("Invalid configuration value %s", option.first); |
115 | 0 | return false; |
116 | 0 | } |
117 | 0 | } |
118 | 0 | } |
119 | 0 | return true; |
120 | 0 | } |
121 | | |
122 | | bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys) |
123 | 0 | { |
124 | 0 | { |
125 | 0 | LOCK(cs_args); |
126 | 0 | m_settings.ro_config.clear(); |
127 | 0 | m_config_sections.clear(); |
128 | 0 | m_config_path = AbsPathForConfigVal(*this, GetPathArg("-conf", BITCOIN_CONF_FILENAME), /*net_specific=*/false); |
129 | 0 | } |
130 | |
|
131 | 0 | const auto conf_path{GetConfigFilePath()}; |
132 | 0 | std::ifstream stream; |
133 | 0 | if (!conf_path.empty()) { // path is empty when -noconf is specified Branch (133:9): [True: 0, False: 0]
|
134 | 0 | if (fs::is_directory(conf_path)) { Branch (134:13): [True: 0, False: 0]
|
135 | 0 | error = strprintf("Config file \"%s\" is a directory.", fs::PathToString(conf_path)); |
136 | 0 | return false; |
137 | 0 | } |
138 | 0 | stream = std::ifstream{conf_path}; |
139 | | // If the file is explicitly specified, it must be readable |
140 | 0 | if (IsArgSet("-conf") && !stream.good()) { Branch (140:13): [True: 0, False: 0]
Branch (140:13): [True: 0, False: 0]
Branch (140:34): [True: 0, False: 0]
|
141 | 0 | error = strprintf("specified config file \"%s\" could not be opened.", fs::PathToString(conf_path)); |
142 | 0 | return false; |
143 | 0 | } |
144 | 0 | } |
145 | | // ok to not have a config file |
146 | 0 | if (stream.good()) { Branch (146:9): [True: 0, False: 0]
|
147 | 0 | if (!ReadConfigStream(stream, fs::PathToString(conf_path), error, ignore_invalid_keys)) { Branch (147:13): [True: 0, False: 0]
|
148 | 0 | return false; |
149 | 0 | } |
150 | | // `-includeconf` cannot be included in the command line arguments except |
151 | | // as `-noincludeconf` (which indicates that no included conf file should be used). |
152 | 0 | bool use_conf_file{true}; |
153 | 0 | { |
154 | 0 | LOCK(cs_args); |
155 | 0 | if (auto* includes = common::FindKey(m_settings.command_line_options, "includeconf")) { Branch (155:23): [True: 0, False: 0]
|
156 | | // ParseParameters() fails if a non-negated -includeconf is passed on the command-line |
157 | 0 | assert(common::SettingsSpan(*includes).last_negated()); |
158 | 0 | use_conf_file = false; |
159 | 0 | } |
160 | 0 | } |
161 | 0 | if (use_conf_file) { Branch (161:13): [True: 0, False: 0]
|
162 | 0 | std::string chain_id = GetChainTypeString(); |
163 | 0 | std::vector<std::string> conf_file_names; |
164 | |
|
165 | 0 | auto add_includes = [&](const std::string& network, size_t skip = 0) { |
166 | 0 | size_t num_values = 0; |
167 | 0 | LOCK(cs_args); |
168 | 0 | if (auto* section = common::FindKey(m_settings.ro_config, network)) { Branch (168:27): [True: 0, False: 0]
|
169 | 0 | if (auto* values = common::FindKey(*section, "includeconf")) { Branch (169:31): [True: 0, False: 0]
|
170 | 0 | for (size_t i = std::max(skip, common::SettingsSpan(*values).negated()); i < values->size(); ++i) { Branch (170:98): [True: 0, False: 0]
|
171 | 0 | conf_file_names.push_back((*values)[i].get_str()); |
172 | 0 | } |
173 | 0 | num_values = values->size(); |
174 | 0 | } |
175 | 0 | } |
176 | 0 | return num_values; |
177 | 0 | }; |
178 | | |
179 | | // We haven't set m_network yet (that happens in SelectParams()), so manually check |
180 | | // for network.includeconf args. |
181 | 0 | const size_t chain_includes = add_includes(chain_id); |
182 | 0 | const size_t default_includes = add_includes({}); |
183 | |
|
184 | 0 | for (const std::string& conf_file_name : conf_file_names) { Branch (184:52): [True: 0, False: 0]
|
185 | 0 | const auto include_conf_path{AbsPathForConfigVal(*this, fs::PathFromString(conf_file_name), /*net_specific=*/false)}; |
186 | 0 | if (fs::is_directory(include_conf_path)) { Branch (186:21): [True: 0, False: 0]
|
187 | 0 | error = strprintf("Included config file \"%s\" is a directory.", fs::PathToString(include_conf_path)); |
188 | 0 | return false; |
189 | 0 | } |
190 | 0 | std::ifstream conf_file_stream{include_conf_path}; |
191 | 0 | if (conf_file_stream.good()) { Branch (191:21): [True: 0, False: 0]
|
192 | 0 | if (!ReadConfigStream(conf_file_stream, conf_file_name, error, ignore_invalid_keys)) { Branch (192:25): [True: 0, False: 0]
|
193 | 0 | return false; |
194 | 0 | } |
195 | 0 | LogPrintf("Included configuration file %s\n", conf_file_name); |
196 | 0 | } else { |
197 | 0 | error = "Failed to include configuration file " + conf_file_name; |
198 | 0 | return false; |
199 | 0 | } |
200 | 0 | } |
201 | | |
202 | | // Warn about recursive -includeconf |
203 | 0 | conf_file_names.clear(); |
204 | 0 | add_includes(chain_id, /* skip= */ chain_includes); |
205 | 0 | add_includes({}, /* skip= */ default_includes); |
206 | 0 | std::string chain_id_final = GetChainTypeString(); |
207 | 0 | if (chain_id_final != chain_id) { Branch (207:17): [True: 0, False: 0]
|
208 | | // Also warn about recursive includeconf for the chain that was specified in one of the includeconfs |
209 | 0 | add_includes(chain_id_final); |
210 | 0 | } |
211 | 0 | for (const std::string& conf_file_name : conf_file_names) { Branch (211:52): [True: 0, False: 0]
|
212 | 0 | tfm::format(std::cerr, "warning: -includeconf cannot be used from included files; ignoring -includeconf=%s\n", conf_file_name); |
213 | 0 | } |
214 | 0 | } |
215 | 0 | } |
216 | | |
217 | | // If datadir is changed in .conf file: |
218 | 0 | ClearPathCache(); |
219 | 0 | if (!CheckDataDirOption(*this)) { Branch (219:9): [True: 0, False: 0]
|
220 | 0 | error = strprintf("specified data directory \"%s\" does not exist.", GetArg("-datadir", "")); |
221 | 0 | return false; |
222 | 0 | } |
223 | 0 | return true; |
224 | 0 | } |
225 | | |
226 | | fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific) |
227 | 0 | { |
228 | 0 | if (path.is_absolute() || path.empty()) { Branch (228:9): [True: 0, False: 0]
Branch (228:31): [True: 0, False: 0]
|
229 | 0 | return path; |
230 | 0 | } |
231 | 0 | return fsbridge::AbsPathJoin(net_specific ? args.GetDataDirNet() : args.GetDataDirBase(), path); Branch (231:34): [True: 0, False: 0]
|
232 | 0 | } |