| 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 | #include "rpc_args.h" |
| 30 | |
| 31 | #include <boost/algorithm/string.hpp> |
| 32 | #include <boost/asio/ip/address.hpp> |
| 33 | #include <functional> |
| 34 | #include "common/command_line.h" |
| 35 | #include "common/i18n.h" |
| 36 | #include "hex.h" |
| 37 | |
| 38 | namespace cryptonote |
| 39 | { |
| 40 | namespace |
| 41 | { |
| 42 | boost::optional<epee::net_utils::ssl_options_t> do_process_ssl(const boost::program_options::variables_map& vm, const rpc_args::descriptors& arg, const bool any_cert_option) |
| 43 | { |
| 44 | bool ssl_required = false; |
| 45 | epee::net_utils::ssl_options_t ssl_options = epee::net_utils::ssl_support_t::e_ssl_support_enabled; |
| 46 | if (any_cert_option && command_line::get_arg(vm, arg.rpc_ssl_allow_any_cert)) |
| 47 | ssl_options.verification = epee::net_utils::ssl_verification_t::none; |
| 48 | else |
| 49 | { |
| 50 | std::string ssl_ca_file = command_line::get_arg(vm, arg.rpc_ssl_ca_certificates); |
| 51 | const std::vector<std::string> ssl_allowed_fingerprints = command_line::get_arg(vm, arg.rpc_ssl_allowed_fingerprints); |
| 52 | |
| 53 | std::vector<std::vector<uint8_t>> allowed_fingerprints{ ssl_allowed_fingerprints.size() }; |
| 54 | std::transform(ssl_allowed_fingerprints.begin(), ssl_allowed_fingerprints.end(), allowed_fingerprints.begin(), epee::from_hex_locale::to_vector); |
| 55 | for (const auto &fpr: allowed_fingerprints) |
| 56 | { |
| 57 | if (fpr.size() != SSL_FINGERPRINT_SIZE) |
| 58 | { |
| 59 | MERROR("SHA-256 fingerprint should be " BOOST_PP_STRINGIZE(SSL_FINGERPRINT_SIZE) " bytes long."); |
| 60 | return boost::none; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (!allowed_fingerprints.empty() || !ssl_ca_file.empty()) |
| 65 | { |
| 66 | ssl_required = true; |
| 67 | ssl_options = epee::net_utils::ssl_options_t{ |
| 68 | std::move(allowed_fingerprints), std::move(ssl_ca_file) |
| 69 | }; |
| 70 | |
| 71 | if (command_line::get_arg(vm, arg.rpc_ssl_allow_chained)) |
| 72 | ssl_options.verification = epee::net_utils::ssl_verification_t::user_ca; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // user specified CA file or fingeprints implies enabled SSL by default |
| 77 | if (!ssl_required && !epee::net_utils::ssl_support_from_string(ssl_options.support, command_line::get_arg(vm, arg.rpc_ssl))) |
| 78 | { |
| 79 | MERROR("Invalid argument for " << std::string(arg.rpc_ssl.name)); |
| 80 | return boost::none; |
| 81 | } |
| 82 | |
| 83 | ssl_options.auth = epee::net_utils::ssl_authentication_t{ |
| 84 | command_line::get_arg(vm, arg.rpc_ssl_private_key), command_line::get_arg(vm, arg.rpc_ssl_certificate) |
| 85 | }; |
| 86 | |
| 87 | return {std::move(ssl_options)}; |
| 88 | } |
| 89 | } // anonymous |
| 90 | |
| 91 | rpc_args::descriptors::descriptors() |
| 92 | : rpc_bind_ip({"rpc-bind-ip", rpc_args::tr("Specify IP to bind RPC server"), "127.0.0.1"}) |
| 93 | , rpc_bind_ipv6_address({"rpc-bind-ipv6-address", rpc_args::tr("Specify IPv6 address to bind RPC server"), "::1"}) |
| 94 | , rpc_restricted_bind_ip({"rpc-restricted-bind-ip", rpc_args::tr("Specify IP to bind restricted RPC server"), "127.0.0.1"}) |
| 95 | , rpc_restricted_bind_ipv6_address({"rpc-restricted-bind-ipv6-address", rpc_args::tr("Specify IPv6 address to bind restricted RPC server"), "::1"}) |
| 96 | , rpc_use_ipv6({"rpc-use-ipv6", rpc_args::tr("Allow IPv6 for RPC"), false}) |
| 97 | , rpc_ignore_ipv4({"rpc-ignore-ipv4", rpc_args::tr("Ignore unsuccessful IPv4 bind for RPC"), false}) |
| 98 | , rpc_login({"rpc-login", rpc_args::tr("Specify username[:password] required for RPC server"), "", true}) |
| 99 | , confirm_external_bind({"confirm-external-bind", rpc_args::tr("Confirm rpc-bind-ip value is NOT a loopback (local) IP")}) |
| 100 | , rpc_access_control_origins({"rpc-access-control-origins", rpc_args::tr("Specify a comma separated list of origins to allow cross origin resource sharing"), ""}) |
| 101 | , rpc_ssl({"rpc-ssl", rpc_args::tr("Enable SSL on RPC connections: enabled|disabled|autodetect"), "autodetect"}) |
| 102 | , rpc_ssl_private_key({"rpc-ssl-private-key", rpc_args::tr("Path to a PEM format private key"), ""}) |
| 103 | , rpc_ssl_certificate({"rpc-ssl-certificate", rpc_args::tr("Path to a PEM format certificate"), ""}) |
| 104 | , rpc_ssl_ca_certificates({"rpc-ssl-ca-certificates", rpc_args::tr("Path to file containing concatenated PEM format certificate(s) to replace system CA(s)."), ""}) |
| 105 | , rpc_ssl_allowed_fingerprints({"rpc-ssl-allowed-fingerprints", rpc_args::tr("List of certificate fingerprints to allow")}) |
| 106 | , rpc_ssl_allow_chained({"rpc-ssl-allow-chained", rpc_args::tr("Allow user (via --rpc-ssl-certificates) chain certificates"), false}) |
| 107 | , rpc_ssl_allow_any_cert({"rpc-ssl-allow-any-cert", rpc_args::tr("Allow any peer certificate"), false}) |
| 108 | , disable_rpc_ban({"disable-rpc-ban", rpc_args::tr("Do not ban hosts on RPC errors"), false, false}) |
| 109 | {} |
| 110 | |
| 111 | const char* rpc_args::tr(const char* str) { return i18n_translate(str, "cryptonote::rpc_args"); } |
| 112 | |
| 113 | void rpc_args::init_options(boost::program_options::options_description& desc, const bool any_cert_option) |
| 114 | { |
| 115 | const descriptors arg{}; |
| 116 | command_line::add_arg(desc, arg.rpc_bind_ip); |
| 117 | command_line::add_arg(desc, arg.rpc_bind_ipv6_address); |
| 118 | command_line::add_arg(desc, arg.rpc_restricted_bind_ip); |
| 119 | command_line::add_arg(desc, arg.rpc_restricted_bind_ipv6_address); |
| 120 | command_line::add_arg(desc, arg.rpc_use_ipv6); |
| 121 | command_line::add_arg(desc, arg.rpc_ignore_ipv4); |
| 122 | command_line::add_arg(desc, arg.rpc_login); |
| 123 | command_line::add_arg(desc, arg.confirm_external_bind); |
| 124 | command_line::add_arg(desc, arg.rpc_access_control_origins); |
| 125 | command_line::add_arg(desc, arg.rpc_ssl); |
| 126 | command_line::add_arg(desc, arg.rpc_ssl_private_key); |
| 127 | command_line::add_arg(desc, arg.rpc_ssl_certificate); |
| 128 | command_line::add_arg(desc, arg.rpc_ssl_ca_certificates); |
| 129 | command_line::add_arg(desc, arg.rpc_ssl_allowed_fingerprints); |
| 130 | command_line::add_arg(desc, arg.rpc_ssl_allow_chained); |
| 131 | command_line::add_arg(desc, arg.disable_rpc_ban); |
| 132 | if (any_cert_option) |
| 133 | command_line::add_arg(desc, arg.rpc_ssl_allow_any_cert); |
| 134 | } |
| 135 | |
| 136 | boost::optional<rpc_args> rpc_args::process(const boost::program_options::variables_map& vm, const bool any_cert_option) |
| 137 | { |
| 138 | const descriptors arg{}; |
| 139 | rpc_args config{}; |
| 140 | |
| 141 | config.bind_ip = command_line::get_arg(vm, arg.rpc_bind_ip); |
| 142 | config.bind_ipv6_address = command_line::get_arg(vm, arg.rpc_bind_ipv6_address); |
| 143 | config.restricted_bind_ip = command_line::get_arg(vm, arg.rpc_restricted_bind_ip); |
| 144 | config.restricted_bind_ipv6_address = command_line::get_arg(vm, arg.rpc_restricted_bind_ipv6_address); |
| 145 | config.use_ipv6 = command_line::get_arg(vm, arg.rpc_use_ipv6); |
| 146 | config.require_ipv4 = !command_line::get_arg(vm, arg.rpc_ignore_ipv4); |
| 147 | config.disable_rpc_ban = command_line::get_arg(vm, arg.disable_rpc_ban); |
| 148 | if (!config.bind_ip.empty()) |
| 149 | { |
| 150 | // always parse IP here for error consistency |
| 151 | boost::system::error_code ec{}; |
| 152 | const auto parsed_ip = boost::asio::ip::make_address(config.bind_ip, ec); |
| 153 | if (ec) |
| 154 | { |
| 155 | LOG_ERROR(tr("Invalid IP address given for --") << arg.rpc_bind_ip.name); |
| 156 | return boost::none; |
| 157 | } |
| 158 | |
| 159 | if (!parsed_ip.is_loopback() && !command_line::get_arg(vm, arg.confirm_external_bind)) |
| 160 | { |
| 161 | LOG_ERROR( |
| 162 | "--" << arg.rpc_bind_ip.name << |
| 163 | tr(" permits inbound unencrypted external connections. Consider SSH tunnel or SSL proxy instead. Override with --") << |
| 164 | arg.confirm_external_bind.name |
| 165 | ); |
| 166 | return boost::none; |
| 167 | } |
| 168 | } |
| 169 | if (!config.bind_ipv6_address.empty()) |
| 170 | { |
| 171 | // allow square braces, but remove them here if present |
| 172 | if (config.bind_ipv6_address.find('[') != std::string::npos) |
| 173 | { |
| 174 | config.bind_ipv6_address = config.bind_ipv6_address.substr(1, config.bind_ipv6_address.size() - 2); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | // always parse IP here for error consistency |
| 179 | boost::system::error_code ec{}; |
| 180 | const auto parsed_ip = boost::asio::ip::make_address(config.bind_ipv6_address, ec); |
| 181 | if (ec) |
| 182 | { |
| 183 | LOG_ERROR(tr("Invalid IP address given for --") << arg.rpc_bind_ipv6_address.name); |
| 184 | return boost::none; |
| 185 | } |
| 186 | |
| 187 | if (!parsed_ip.is_loopback() && !command_line::get_arg(vm, arg.confirm_external_bind)) |
| 188 | { |
| 189 | LOG_ERROR( |
| 190 | "--" << arg.rpc_bind_ipv6_address.name << |
| 191 | tr(" permits inbound unencrypted external connections. Consider SSH tunnel or SSL proxy instead. Override with --") << |
| 192 | arg.confirm_external_bind.name |
| 193 | ); |
| 194 | return boost::none; |
| 195 | } |
| 196 | } |
| 197 | if (!config.restricted_bind_ip.empty()) |
| 198 | { |
| 199 | // always parse IP here for error consistency |
| 200 | boost::system::error_code ec{}; |
| 201 | boost::asio::ip::make_address(config.restricted_bind_ip, ec); |
| 202 | if (ec) |
| 203 | { |
| 204 | LOG_ERROR(tr("Invalid IP address given for --") << arg.rpc_restricted_bind_ip.name); |
| 205 | return boost::none; |
| 206 | } |
| 207 | } |
| 208 | if (!config.restricted_bind_ipv6_address.empty()) |
| 209 | { |
| 210 | // allow square braces, but remove them here if present |
| 211 | if (config.restricted_bind_ipv6_address.find('[') != std::string::npos) |
| 212 | { |
| 213 | config.restricted_bind_ipv6_address = config.restricted_bind_ipv6_address.substr(1, config.restricted_bind_ipv6_address.size() - 2); |
| 214 | } |
| 215 | |
| 216 | // always parse IP here for error consistency |
| 217 | boost::system::error_code ec{}; |
| 218 | boost::asio::ip::make_address(config.restricted_bind_ipv6_address, ec); |
| 219 | if (ec) |
| 220 | { |
| 221 | LOG_ERROR(tr("Invalid IP address given for --") << arg.rpc_restricted_bind_ipv6_address.name); |
| 222 | return boost::none; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | const char *env_rpc_login = nullptr; |
| 227 | const bool has_rpc_arg = command_line::has_arg(vm, arg.rpc_login); |
| 228 | const bool use_rpc_env = !has_rpc_arg && (env_rpc_login = getenv("RPC_LOGIN")) != nullptr && strlen(env_rpc_login) > 0; |
| 229 | boost::optional<tools::login> login{}; |
| 230 | if (has_rpc_arg || use_rpc_env) |
| 231 | { |
| 232 | config.login = tools::login::parse( |
| 233 | has_rpc_arg ? command_line::get_arg(vm, arg.rpc_login) : std::string(env_rpc_login), true, [](bool verify) { |
| 234 | return tools::password_container::prompt(verify, "RPC server password"); |
| 235 | }); |
| 236 | |
| 237 | if (!config.login) |
| 238 | return boost::none; |
| 239 | |
| 240 | if (config.login->username.empty()) |
| 241 | { |
| 242 | LOG_ERROR(tr("Username specified with --") << arg.rpc_login.name << tr(" cannot be empty")); |
| 243 | return boost::none; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | auto access_control_origins_input = command_line::get_arg(vm, arg.rpc_access_control_origins); |
| 248 | if (!access_control_origins_input.empty()) |
| 249 | { |
| 250 | std::vector<std::string> access_control_origins; |
| 251 | boost::split(access_control_origins, access_control_origins_input, boost::is_any_of(",")); |
| 252 | std::for_each(access_control_origins.begin(), access_control_origins.end(), std::bind(&boost::trim<std::string>, std::placeholders::_1, std::locale::classic())); |
| 253 | config.access_control_origins = std::move(access_control_origins); |
| 254 | } |
| 255 | |
| 256 | auto ssl_options = do_process_ssl(vm, arg, any_cert_option); |
| 257 | if (!ssl_options) |
| 258 | return boost::none; |
| 259 | config.ssl_options = std::move(*ssl_options); |
| 260 | |
| 261 | return {std::move(config)}; |
| 262 | } |
| 263 | |
| 264 | boost::optional<epee::net_utils::ssl_options_t> rpc_args::process_ssl(const boost::program_options::variables_map& vm, const bool any_cert_option) |
| 265 | { |
| 266 | const descriptors arg{}; |
| 267 | return do_process_ssl(vm, arg, any_cert_option); |
| 268 | } |
| 269 | } |