Skip to content

Commit f4a0d27

Browse files
committed
Merge #13716: bitcoin-cli: -stdinwalletpassphrase and non-echo stdin passwords
50c4afa add newline after -stdin* (Karl-Johan Alm) 7f11fba cli: add -stdinwalletpassphrase for (slightly more) secure CLI (Karl-Johan Alm) 0da503e add stdin helpers for password input support (Karl-Johan Alm) Pull request description: This PR * adds `-stdinwalletpassphrase` for use with `walletpasshprase(change)` * adds no-echo for passwords (`-stdinrpcpass` and above) It may not be ideal, but it's better than having to clear the screen whenever you unlock the wallet. ACKs for top commit: laanwj: code review ACK 50c4afa Tree-SHA512: 473db8a303ff360ffaa36ac81a2f82be2136fa82696df0bc4f33cb44033a3ae258b5aa5bbcc1f101f88ae9abe9598ed564ce52877ab139bd5d709833f5275ec6
2 parents fecc1be + 50c4afa commit f4a0d27

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,8 @@ endif
520520
libbitcoin_cli_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
521521
libbitcoin_cli_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
522522
libbitcoin_cli_a_SOURCES = \
523+
compat/stdin.h \
524+
compat/stdin.cpp \
523525
rpc/client.cpp \
524526
$(BITCOIN_CORE_H)
525527

src/bitcoin-cli.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <support/events.h>
2828

2929
#include <univalue.h>
30+
#include <compat/stdin.h>
3031

3132
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
3233

@@ -58,7 +59,8 @@ static void SetupCliArgs()
5859
gArgs.AddArg("-rpcwait", "Wait for RPC server to start", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
5960
gArgs.AddArg("-rpcwallet=<walletname>", "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/<walletname>", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
6061
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);
61-
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);
62+
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);
63+
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);
6264
}
6365

6466
/** libevent event log callback */
@@ -411,18 +413,47 @@ static int CommandLineRPC(int argc, char *argv[])
411413
}
412414
std::string rpcPass;
413415
if (gArgs.GetBoolArg("-stdinrpcpass", false)) {
416+
NO_STDIN_ECHO();
417+
if (!StdinReady()) {
418+
fputs("RPC password> ", stderr);
419+
fflush(stderr);
420+
}
414421
if (!std::getline(std::cin, rpcPass)) {
415422
throw std::runtime_error("-stdinrpcpass specified but failed to read from standard input");
416423
}
424+
if (StdinTerminal()) {
425+
fputc('\n', stdout);
426+
}
417427
gArgs.ForceSetArg("-rpcpassword", rpcPass);
418428
}
419429
std::vector<std::string> args = std::vector<std::string>(&argv[1], &argv[argc]);
430+
if (gArgs.GetBoolArg("-stdinwalletpassphrase", false)) {
431+
NO_STDIN_ECHO();
432+
std::string walletPass;
433+
if (args.size() < 1 || args[0].substr(0, 16) != "walletpassphrase") {
434+
throw std::runtime_error("-stdinwalletpassphrase is only applicable for walletpassphrase(change)");
435+
}
436+
if (!StdinReady()) {
437+
fputs("Wallet passphrase> ", stderr);
438+
fflush(stderr);
439+
}
440+
if (!std::getline(std::cin, walletPass)) {
441+
throw std::runtime_error("-stdinwalletpassphrase specified but failed to read from standard input");
442+
}
443+
if (StdinTerminal()) {
444+
fputc('\n', stdout);
445+
}
446+
args.insert(args.begin() + 1, walletPass);
447+
}
420448
if (gArgs.GetBoolArg("-stdin", false)) {
421449
// Read one arg per line from stdin and append
422450
std::string line;
423451
while (std::getline(std::cin, line)) {
424452
args.push_back(line);
425453
}
454+
if (StdinTerminal()) {
455+
fputc('\n', stdout);
456+
}
426457
}
427458
std::unique_ptr<BaseRequestHandler> rh;
428459
std::string method;

src/compat/stdin.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) 2018 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+
#if defined(HAVE_CONFIG_H)
6+
#include <config/bitcoin-config.h>
7+
#endif
8+
9+
#include <cstdio> // for fileno(), stdin
10+
11+
#ifdef WIN32
12+
#include <windows.h> // for SetStdinEcho()
13+
#include <io.h> // for isatty()
14+
#else
15+
#include <termios.h> // for SetStdinEcho()
16+
#include <unistd.h> // for SetStdinEcho(), isatty()
17+
#include <sys/poll.h> // for StdinReady()
18+
#endif
19+
20+
#include <compat/stdin.h>
21+
22+
// https://stackoverflow.com/questions/1413445/reading-a-password-from-stdcin
23+
void SetStdinEcho(bool enable)
24+
{
25+
#ifdef WIN32
26+
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
27+
DWORD mode;
28+
GetConsoleMode(hStdin, &mode);
29+
if (!enable) {
30+
mode &= ~ENABLE_ECHO_INPUT;
31+
} else {
32+
mode |= ENABLE_ECHO_INPUT;
33+
}
34+
SetConsoleMode(hStdin, mode);
35+
#else
36+
struct termios tty;
37+
tcgetattr(STDIN_FILENO, &tty);
38+
if (!enable) {
39+
tty.c_lflag &= ~ECHO;
40+
} else {
41+
tty.c_lflag |= ECHO;
42+
}
43+
(void)tcsetattr(STDIN_FILENO, TCSANOW, &tty);
44+
#endif
45+
}
46+
47+
bool StdinTerminal()
48+
{
49+
#ifdef WIN32
50+
return _isatty(_fileno(stdin));
51+
#else
52+
return isatty(fileno(stdin));
53+
#endif
54+
}
55+
56+
bool StdinReady()
57+
{
58+
if (!StdinTerminal()) {
59+
return true;
60+
}
61+
#ifdef WIN32
62+
return false;
63+
#else
64+
struct pollfd fds;
65+
fds.fd = 0; /* this is STDIN */
66+
fds.events = POLLIN;
67+
return poll(&fds, 1, 0) == 1;
68+
#endif
69+
}
70+
71+
NoechoInst::NoechoInst() { SetStdinEcho(false); }
72+
NoechoInst::~NoechoInst() { SetStdinEcho(true); }

src/compat/stdin.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2018 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+
#ifndef BITCOIN_COMPAT_STDIN_H
6+
#define BITCOIN_COMPAT_STDIN_H
7+
8+
struct NoechoInst {
9+
NoechoInst();
10+
~NoechoInst();
11+
};
12+
13+
#define NO_STDIN_ECHO() NoechoInst _no_echo
14+
15+
bool StdinTerminal();
16+
bool StdinReady();
17+
18+
#endif // BITCOIN_COMPAT_STDIN_H

0 commit comments

Comments
 (0)