| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers |
| 30 | |
| 31 | #include "common/command_line.h" |
| 32 | #include "common/scoped_message_writer.h" |
| 33 | #include "common/password.h" |
| 34 | #include "common/util.h" |
| 35 | #include "cryptonote_core/cryptonote_core.h" |
| 36 | #include "cryptonote_basic/miner.h" |
| 37 | #include "daemon/command_server.h" |
| 38 | #include "daemon/daemon.h" |
| 39 | #include "daemon/executor.h" |
| 40 | #include "daemonizer/daemonizer.h" |
| 41 | #include "misc_log_ex.h" |
| 42 | #include "net/parse.h" |
| 43 | #include "p2p/net_node.h" |
| 44 | #include "rpc/core_rpc_server.h" |
| 45 | #include "rpc/rpc_args.h" |
| 46 | #include "daemon/command_line_args.h" |
| 47 | #include "version.h" |
| 48 | |
| 49 | #ifdef STACK_TRACE |
| 50 | #include "common/stack_trace.h" |
| 51 | #endif // STACK_TRACE |
| 52 | |
| 53 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 54 | #define MONERO_DEFAULT_LOG_CATEGORY "daemon" |
| 55 | |
| 56 | namespace po = boost::program_options; |
| 57 | namespace bf = boost::filesystem; |
| 58 | |
| 59 | uint16_t parse_public_rpc_port(const po::variables_map &vm) |
| 60 | { |
| 61 | const auto &public_node_arg = daemon_args::arg_public_node; |
| 62 | const bool public_node = command_line::get_arg(vm, public_node_arg); |
| 63 | if (!public_node) |
| 64 | { |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | std::string rpc_port_str; |
| 69 | std::string rpc_bind_address = command_line::get_arg(vm, cryptonote::rpc_args::descriptors().rpc_bind_ip); |
| 70 | const auto &restricted_rpc_port = cryptonote::core_rpc_server::arg_rpc_restricted_bind_port; |
| 71 | if (!command_line::is_arg_defaulted(vm, restricted_rpc_port)) |
| 72 | { |
| 73 | rpc_port_str = command_line::get_arg(vm, restricted_rpc_port); |
| 74 | rpc_bind_address = command_line::get_arg(vm, cryptonote::rpc_args::descriptors().rpc_restricted_bind_ip); |
| 75 | } |
| 76 | else if (command_line::get_arg(vm, cryptonote::core_rpc_server::arg_restricted_rpc)) |
| 77 | { |
| 78 | rpc_port_str = command_line::get_arg(vm, cryptonote::core_rpc_server::arg_rpc_bind_port); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | throw std::runtime_error("restricted RPC mode is required"); |
| 83 | } |
| 84 | |
| 85 | uint16_t rpc_port; |
| 86 | if (!epee::string_tools::get_xtype_from_string(rpc_port, rpc_port_str)) |
| 87 | { |
| 88 | throw std::runtime_error("invalid RPC port " + rpc_port_str); |
| 89 | } |
| 90 | |
| 91 | const auto address = net::get_network_address(rpc_bind_address, rpc_port); |
| 92 | if (!address) { |
| 93 | throw std::runtime_error("failed to parse RPC bind address"); |
| 94 | } |
| 95 | if (address->get_zone() != epee::net_utils::zone::public_) |
| 96 | { |
| 97 | throw std::runtime_error(std::string(zone_to_string(address->get_zone())) |
| 98 | + " network zone is not supported, please check RPC server bind address"); |
| 99 | } |
| 100 | |
| 101 | if (address->is_loopback() || address->is_local()) |
| 102 | { |
| 103 | MLOG_RED(el::Level::Warning, "--" << public_node_arg.name |
| 104 | << " is enabled, but RPC server " << address->str() |
| 105 | << " may be unreachable from outside, please check RPC server bind address"); |
| 106 | } |
| 107 | |
| 108 | return rpc_port; |
| 109 | } |
| 110 | |
| 111 | #ifdef WIN32 |
| 112 | bool isFat32(const wchar_t* root_path) |
| 113 | { |
| 114 | std::vector<wchar_t> fs(MAX_PATH + 1); |
| 115 | if (!::GetVolumeInformationW(root_path, nullptr, 0, nullptr, 0, nullptr, &fs[0], MAX_PATH)) |
| 116 | { |
| 117 | MERROR("Failed to get '" << root_path << "' filesystem name. Error code: " << ::GetLastError()); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | return wcscmp(L"FAT32", &fs[0]) == 0; |
| 122 | } |
| 123 | #endif |
| 124 | |
| 125 | int main(int argc, char const * argv[]) |
| 126 | { |
| 127 | try { |
| 128 | |
| 129 | // TODO parse the debug options like set log level right here at start |
| 130 | |
| 131 | tools::on_startup(); |
| 132 | |
| 133 | epee::string_tools::set_module_name_and_folder(argv[0]); |
| 134 | |
| 135 | // Build argument description |
| 136 | po::options_description all_options("All"); |
| 137 | po::options_description hidden_options("Hidden"); |
| 138 | po::options_description visible_options("Options"); |
| 139 | po::options_description core_settings("Settings"); |
| 140 | po::positional_options_description positional_options; |
| 141 | { |
| 142 | // Misc Options |
| 143 | |
| 144 | command_line::add_arg(visible_options, command_line::arg_help); |
| 145 | command_line::add_arg(visible_options, command_line::arg_version); |
| 146 | command_line::add_arg(visible_options, daemon_args::arg_os_version); |
| 147 | command_line::add_arg(visible_options, daemon_args::arg_config_file); |
| 148 | |
| 149 | // Settings |
| 150 | command_line::add_arg(core_settings, daemon_args::arg_log_file); |
| 151 | command_line::add_arg(core_settings, daemon_args::arg_log_level); |
| 152 | command_line::add_arg(core_settings, daemon_args::arg_max_log_file_size); |
| 153 | command_line::add_arg(core_settings, daemon_args::arg_max_log_files); |
| 154 | command_line::add_arg(core_settings, daemon_args::arg_max_concurrency); |
| 155 | command_line::add_arg(core_settings, daemon_args::arg_proxy); |
| 156 | command_line::add_arg(core_settings, daemon_args::arg_proxy_allow_dns_leaks); |
| 157 | command_line::add_arg(core_settings, daemon_args::arg_public_node); |
| 158 | command_line::add_arg(core_settings, daemon_args::arg_zmq_rpc_bind_ip); |
| 159 | command_line::add_arg(core_settings, daemon_args::arg_zmq_rpc_bind_port); |
| 160 | command_line::add_arg(core_settings, daemon_args::arg_zmq_pub); |
| 161 | command_line::add_arg(core_settings, daemon_args::arg_confirm_zmq_rpc_external_bind); |
| 162 | command_line::add_arg(core_settings, daemon_args::arg_restricted_zmq_rpc); |
| 163 | command_line::add_arg(core_settings, daemon_args::arg_zmq_rpc_disabled); |
| 164 | |
| 165 | daemonizer::init_options(hidden_options, visible_options); |
| 166 | daemonize::t_executor::init_options(core_settings); |
| 167 | |
| 168 | // Hidden options |
| 169 | command_line::add_arg(hidden_options, daemon_args::arg_command); |
| 170 | |
| 171 | visible_options.add(core_settings); |
| 172 | all_options.add(visible_options); |
| 173 | all_options.add(hidden_options); |
| 174 | |
| 175 | // Positional |
| 176 | positional_options.add(daemon_args::arg_command.name, -1); // -1 for unlimited arguments |
| 177 | } |
| 178 | |
| 179 | // Do command line parsing |
| 180 | po::variables_map vm; |
| 181 | bool ok = command_line::handle_error_helper(visible_options, [&]() |
| 182 | { |
| 183 | boost::program_options::store( |
| 184 | boost::program_options::command_line_parser(argc, argv) |
| 185 | .options(all_options).positional(positional_options).run() |
| 186 | , vm |
| 187 | ); |
| 188 | |
| 189 | return true; |
| 190 | }); |
| 191 | if (!ok) return 1; |
| 192 | |
| 193 | if (command_line::get_arg(vm, command_line::arg_help)) |
| 194 | { |
| 195 | std::cout << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL << ENDL; |
| 196 | std::cout << "Usage: " + std::string{argv[0]} + " [options|settings] [daemon_command...]" << std::endl << std::endl; |
| 197 | std::cout << visible_options << std::endl; |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | // Monero Version |
| 202 | if (command_line::get_arg(vm, command_line::arg_version)) |
| 203 | { |
| 204 | std::cout << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL; |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | // OS |
| 209 | if (command_line::get_arg(vm, daemon_args::arg_os_version)) |
| 210 | { |
| 211 | std::cout << "OS: " << tools::get_os_version_string() << ENDL; |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | std::string config = command_line::get_arg(vm, daemon_args::arg_config_file); |
| 216 | boost::filesystem::path config_path(config); |
| 217 | boost::system::error_code ec; |
| 218 | if (bf::exists(config_path, ec)) |
| 219 | { |
| 220 | try |
| 221 | { |
| 222 | po::store(po::parse_config_file<char>(config_path.string<std::string>().c_str(), core_settings), vm); |
| 223 | } |
| 224 | catch (const po::unknown_option &e) |
| 225 | { |
| 226 | std::string unrecognized_option = e.get_option_name(); |
| 227 | if (all_options.find_nothrow(unrecognized_option, false)) |
| 228 | { |
| 229 | std::cerr << "Option '" << unrecognized_option << "' is not allowed in the config file, please use it as a command line flag." << std::endl; |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | std::cerr << "Unrecognized option '" << unrecognized_option << "' in config file." << std::endl; |
| 234 | } |
| 235 | return 1; |
| 236 | } |
| 237 | catch (const std::exception &e) |
| 238 | { |
| 239 | // log system isn't initialized yet |
| 240 | std::cerr << "Error parsing config file: " << e.what() << std::endl; |
| 241 | throw; |
| 242 | } |
| 243 | } |
| 244 | else if (!command_line::is_arg_defaulted(vm, daemon_args::arg_config_file)) |
| 245 | { |
| 246 | std::cerr << "Can't find config file " << config << std::endl; |
| 247 | return 1; |
| 248 | } |
| 249 | |
| 250 | try { cryptonote::core::get_network_type_from_args(vm); } |
| 251 | catch (const std::runtime_error&) |
| 252 | { |
| 253 | std::cerr << "Can't specify more than one of --tesnet and --stagenet and --regtest" << ENDL; |
| 254 | return 1; |
| 255 | } |
| 256 | |
| 257 | // data_dir |
| 258 | // default: e.g. ~/.bitmonero/ or ~/.bitmonero/testnet |
| 259 | // if data-dir argument given: |
| 260 | // absolute path |
| 261 | // relative path: relative to cwd |
| 262 | |
| 263 | // Create data dir if it doesn't exist |
| 264 | boost::filesystem::path data_dir = boost::filesystem::absolute( |
| 265 | command_line::get_arg(vm, cryptonote::arg_data_dir)); |
| 266 | |
| 267 | #ifdef WIN32 |
| 268 | if (isFat32(data_dir.root_path().c_str())) |
| 269 | { |
| 270 | MERROR("Data directory resides on FAT32 volume that has 4GiB file size limit, blockchain might get corrupted."); |
| 271 | } |
| 272 | #endif |
| 273 | |
| 274 | // FIXME: not sure on windows implementation default, needs further review |
| 275 | //bf::path relative_path_base = daemonizer::get_relative_path_base(vm); |
| 276 | bf::path relative_path_base = data_dir; |
| 277 | |
| 278 | po::notify(vm); |
| 279 | |
| 280 | // log_file_path |
| 281 | // default: <data_dir>/<CRYPTONOTE_NAME>.log |
| 282 | // if log-file argument given: |
| 283 | // absolute path |
| 284 | // relative path: relative to data_dir |
| 285 | bf::path log_file_path {data_dir / std::string(CRYPTONOTE_NAME ".log")}; |
| 286 | if (!command_line::is_arg_defaulted(vm, daemon_args::arg_log_file)) |
| 287 | log_file_path = command_line::get_arg(vm, daemon_args::arg_log_file); |
| 288 | if (!log_file_path.has_parent_path()) |
| 289 | log_file_path = bf::absolute(log_file_path, relative_path_base); |
| 290 | mlog_configure(log_file_path.string(), true, command_line::get_arg(vm, daemon_args::arg_max_log_file_size), command_line::get_arg(vm, daemon_args::arg_max_log_files)); |
| 291 | |
| 292 | // Set log level |
| 293 | if (!command_line::is_arg_defaulted(vm, daemon_args::arg_log_level)) |
| 294 | { |
| 295 | mlog_set_log(command_line::get_arg(vm, daemon_args::arg_log_level).c_str()); |
| 296 | } |
| 297 | |
| 298 | // after logs initialized |
| 299 | tools::create_directories_if_necessary(data_dir.string()); |
| 300 | |
| 301 | #ifdef STACK_TRACE |
| 302 | tools::set_stack_trace_log(log_file_path.filename().string()); |
| 303 | #endif // STACK_TRACE |
| 304 | |
| 305 | if (!command_line::is_arg_defaulted(vm, daemon_args::arg_max_concurrency)) |
| 306 | tools::set_max_concurrency(command_line::get_arg(vm, daemon_args::arg_max_concurrency)); |
| 307 | |
| 308 | // logging is now set up |
| 309 | MGINFO("Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")"); |
| 310 | |
| 311 | // If there are positional options, we're running a daemon command |
| 312 | { |
| 313 | auto command = command_line::get_arg(vm, daemon_args::arg_command); |
| 314 | |
| 315 | if (command.size()) |
| 316 | { |
| 317 | const cryptonote::rpc_args::descriptors arg{}; |
| 318 | auto rpc_ip_str = command_line::get_arg(vm, arg.rpc_bind_ip); |
| 319 | auto rpc_port_str = command_line::get_arg(vm, cryptonote::core_rpc_server::arg_rpc_bind_port); |
| 320 | |
| 321 | uint32_t rpc_ip; |
| 322 | uint16_t rpc_port; |
| 323 | if (!epee::string_tools::get_ip_int32_from_string(rpc_ip, rpc_ip_str)) |
| 324 | { |
| 325 | std::cerr << "Invalid IP: " << rpc_ip_str << std::endl; |
| 326 | return 1; |
| 327 | } |
| 328 | if (!epee::string_tools::get_xtype_from_string(rpc_port, rpc_port_str)) |
| 329 | { |
| 330 | std::cerr << "Invalid port: " << rpc_port_str << std::endl; |
| 331 | return 1; |
| 332 | } |
| 333 | |
| 334 | const char *env_rpc_login = nullptr; |
| 335 | const bool has_rpc_arg = command_line::has_arg(vm, arg.rpc_login); |
| 336 | const bool use_rpc_env = !has_rpc_arg && (env_rpc_login = getenv("RPC_LOGIN")) != nullptr && strlen(env_rpc_login) > 0; |
| 337 | boost::optional<tools::login> login{}; |
| 338 | if (has_rpc_arg || use_rpc_env) |
| 339 | { |
| 340 | login = tools::login::parse( |
| 341 | has_rpc_arg ? command_line::get_arg(vm, arg.rpc_login) : std::string(env_rpc_login), false, [](bool verify) { |
| 342 | PAUSE_READLINE(); |
| 343 | return tools::password_container::prompt(verify, "Daemon client password"); |
| 344 | } |
| 345 | ); |
| 346 | if (!login) |
| 347 | { |
| 348 | std::cerr << "Failed to obtain password" << std::endl; |
| 349 | return 1; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | auto ssl_options = cryptonote::rpc_args::process_ssl(vm, true); |
| 354 | if (!ssl_options) |
| 355 | return 1; |
| 356 | |
| 357 | daemonize::t_command_server rpc_commands{rpc_ip, rpc_port, std::move(login), std::move(*ssl_options)}; |
| 358 | if (rpc_commands.process_command_vec(command)) |
| 359 | { |
| 360 | return 0; |
| 361 | } |
| 362 | else |
| 363 | { |
| 364 | PAUSE_READLINE(); |
| 365 | std::cerr << "Unknown command: " << command.front() << std::endl; |
| 366 | return 1; |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | MINFO("Moving from main() into the daemonize now."); |
| 372 | |
| 373 | return daemonizer::daemonize(argc, argv, daemonize::t_executor{parse_public_rpc_port(vm)}, vm) ? 0 : 1; |
| 374 | } |
| 375 | catch (std::exception const & ex) |
| 376 | { |
| 377 | LOG_ERROR("Exception in main! " << ex.what()); |
| 378 | } |
| 379 | catch (...) |
| 380 | { |
| 381 | LOG_ERROR("Exception in main!"); |
| 382 | } |
| 383 | return 1; |
| 384 | } |