handle ^D and ^C while password prompting

This commit is contained in:
Jethro Grassie 2018-02-01 15:32:30 -05:00
parent ed67e5c001
commit a4b50a6f51
No known key found for this signature in database
GPG key ID: DE8ED755616565BB
4 changed files with 24 additions and 6 deletions

View file

@ -42,12 +42,10 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef HAVE_READLINE
#include "readline_buffer.h"
#endif
#include "memwipe.h" #include "memwipe.h"
#define EOT 0x4
namespace namespace
{ {
#if defined(_WIN32) #if defined(_WIN32)
@ -134,7 +132,7 @@ namespace
while (aPass.size() < tools::password_container::max_password_size) while (aPass.size() < tools::password_container::max_password_size)
{ {
int ch = getch(); int ch = getch();
if (EOF == ch) if (EOF == ch || ch == EOT)
{ {
return false; return false;
} }
@ -229,13 +227,20 @@ namespace tools
m_password.clear(); m_password.clear();
} }
std::atomic<bool> password_container::is_prompting(false);
boost::optional<password_container> password_container::prompt(const bool verify, const char *message) boost::optional<password_container> password_container::prompt(const bool verify, const char *message)
{ {
is_prompting = true;
password_container pass1{}; password_container pass1{};
password_container pass2{}; password_container pass2{};
if (is_cin_tty() ? read_from_tty(verify, message, pass1.m_password, pass2.m_password) : read_from_file(pass1.m_password)) if (is_cin_tty() ? read_from_tty(verify, message, pass1.m_password, pass2.m_password) : read_from_file(pass1.m_password))
{
is_prompting = false;
return {std::move(pass1)}; return {std::move(pass1)};
}
is_prompting = false;
return boost::none; return boost::none;
} }

View file

@ -31,6 +31,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <atomic>
#include <boost/optional/optional.hpp> #include <boost/optional/optional.hpp>
#include "wipeable_string.h" #include "wipeable_string.h"
@ -49,6 +50,7 @@ namespace tools
//! \return A password from stdin TTY prompt or `std::cin` pipe. //! \return A password from stdin TTY prompt or `std::cin` pipe.
static boost::optional<password_container> prompt(bool verify, const char *mesage = "Password"); static boost::optional<password_container> prompt(bool verify, const char *mesage = "Password");
static std::atomic<bool> is_prompting;
password_container(const password_container&) = delete; password_container(const password_container&) = delete;
password_container(password_container&& rhs) = default; password_container(password_container&& rhs) = default;

View file

@ -153,8 +153,12 @@ namespace tools
} }
return r; return r;
#else #else
static struct sigaction sa;
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = posix_handler;
sa.sa_flags = 0;
/* Only blocks SIGINT, SIGTERM and SIGPIPE */ /* Only blocks SIGINT, SIGTERM and SIGPIPE */
signal(SIGINT, posix_handler); sigaction(SIGINT, &sa, NULL);
signal(SIGTERM, posix_handler); signal(SIGTERM, posix_handler);
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
m_handler = t; m_handler = t;

View file

@ -638,6 +638,8 @@ bool simple_wallet::change_password(const std::vector<std::string> &args)
// prompts for a new password, pass true to verify the password // prompts for a new password, pass true to verify the password
const auto pwd_container = default_password_prompter(true); const auto pwd_container = default_password_prompter(true);
if(!pwd_container)
return true;
try try
{ {
@ -6806,6 +6808,11 @@ int main(int argc, char* argv[])
else else
{ {
tools::signal_handler::install([&w](int type) { tools::signal_handler::install([&w](int type) {
if (tools::password_container::is_prompting.load())
{
// must be prompting for password so return and let the signal stop prompt
return;
}
#ifdef WIN32 #ifdef WIN32
if (type == CTRL_C_EVENT) if (type == CTRL_C_EVENT)
#else #else