From 7f11fba2e3c7b1e00ffb98504cdcfc2ded9c09cf Mon Sep 17 00:00:00 2001 From: Karl-Johan Alm Date: Thu, 19 Jul 2018 23:48:55 +0900 Subject: cli: add -stdinwalletpassphrase for (slightly more) secure CLI --- src/bitcoin-cli.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index a6756fcce7..e1be5b7f92 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -27,6 +27,7 @@ #include #include +#include const std::function G_TRANSLATION_FUN = nullptr; @@ -58,7 +59,8 @@ static void SetupCliArgs() gArgs.AddArg("-rpcwait", "Wait for RPC server to start", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); gArgs.AddArg("-rpcwallet=", "Send RPC for non-default wallet on RPC server (needs to exactly match corresponding -wallet option passed to bitcoind). This changes the RPC endpoint used, e.g. http://127.0.0.1:8332/wallet/", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); gArgs.AddArg("-stdin", "Read extra arguments from standard input, one per line until EOF/Ctrl-D (recommended for sensitive information such as passphrases). When combined with -stdinrpcpass, the first line from standard input is used for the RPC password.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); - gArgs.AddArg("-stdinrpcpass", "Read RPC password from standard input as a single line. When combined with -stdin, the first line from standard input is used for the RPC password.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); + gArgs.AddArg("-stdinrpcpass", "Read RPC password from standard input as a single line. When combined with -stdin, the first line from standard input is used for the RPC password. When combined with -stdinwalletpassphrase, -stdinrpcpass consumes the first line, and -stdinwalletpassphrase consumes the second.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); + gArgs.AddArg("-stdinwalletpassphrase", "Read wallet passphrase from standard input as a single line. When combined with -stdin, the first line from standard input is used for the wallet passphrase.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); } /** libevent event log callback */ @@ -411,12 +413,32 @@ static int CommandLineRPC(int argc, char *argv[]) } std::string rpcPass; if (gArgs.GetBoolArg("-stdinrpcpass", false)) { + NO_STDIN_ECHO(); + if (!StdinReady()) { + fputs("RPC password> ", stderr); + fflush(stderr); + } if (!std::getline(std::cin, rpcPass)) { throw std::runtime_error("-stdinrpcpass specified but failed to read from standard input"); } gArgs.ForceSetArg("-rpcpassword", rpcPass); } std::vector args = std::vector(&argv[1], &argv[argc]); + if (gArgs.GetBoolArg("-stdinwalletpassphrase", false)) { + NO_STDIN_ECHO(); + std::string walletPass; + if (args.size() < 1 || args[0].substr(0, 16) != "walletpassphrase") { + throw std::runtime_error("-stdinwalletpassphrase is only applicable for walletpassphrase(change)"); + } + if (!StdinReady()) { + fputs("Wallet passphrase> ", stderr); + fflush(stderr); + } + if (!std::getline(std::cin, walletPass)) { + throw std::runtime_error("-stdinwalletpassphrase specified but failed to read from standard input"); + } + args.insert(args.begin() + 1, walletPass); + } if (gArgs.GetBoolArg("-stdin", false)) { // Read one arg per line from stdin and append std::string line; -- cgit v1.2.3