LCOV - code coverage report
Current view: top level - src/common - config.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 4 142 2.8 %
Date: 2023-11-12 01:39:15 Functions: 1 6 16.7 %
Branches: 2 274 0.7 %

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

Generated by: LCOV version 1.14