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