2019-03-05 14:05:34 -07:00
|
|
|
// Copyright (c) 2014-2019, The Monero Project
|
2014-07-23 07:03:52 -06:00
|
|
|
//
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
// permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
// conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
// of conditions and the following disclaimer in the documentation and/or other
|
|
|
|
// materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
|
|
|
// used to endorse or promote products derived from this software without specific
|
|
|
|
// prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
|
|
|
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
|
|
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2017-02-05 15:48:03 -07:00
|
|
|
#include "password.h"
|
2014-03-03 15:07:58 -07:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <io.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2018-02-01 13:32:30 -07:00
|
|
|
#define EOT 0x4
|
|
|
|
|
2016-12-17 16:07:15 -07:00
|
|
|
namespace
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
2016-12-17 16:07:15 -07:00
|
|
|
bool is_cin_tty() noexcept
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2016-12-17 16:07:15 -07:00
|
|
|
return 0 != _isatty(_fileno(stdin));
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
|
|
|
|
2018-07-06 17:03:15 -06:00
|
|
|
bool read_from_tty(epee::wipeable_string& pass, bool hide_input)
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
|
|
|
HANDLE h_cin = ::GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
|
|
|
|
DWORD mode_old;
|
|
|
|
::GetConsoleMode(h_cin, &mode_old);
|
2019-05-08 15:48:28 -06:00
|
|
|
DWORD mode_new = mode_old & ~((hide_input ? ENABLE_ECHO_INPUT : 0) | ENABLE_LINE_INPUT);
|
2014-03-03 15:07:58 -07:00
|
|
|
::SetConsoleMode(h_cin, mode_new);
|
|
|
|
|
|
|
|
bool r = true;
|
2016-12-17 16:07:15 -07:00
|
|
|
pass.reserve(tools::password_container::max_password_size);
|
2018-09-19 06:39:17 -06:00
|
|
|
std::vector<int> chlen;
|
|
|
|
chlen.reserve(tools::password_container::max_password_size);
|
2016-12-17 16:07:15 -07:00
|
|
|
while (pass.size() < tools::password_container::max_password_size)
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
|
|
|
DWORD read;
|
2018-09-19 06:39:17 -06:00
|
|
|
wchar_t ucs2_ch;
|
|
|
|
r = (TRUE == ::ReadConsoleW(h_cin, &ucs2_ch, 1, &read, NULL));
|
2014-03-03 15:07:58 -07:00
|
|
|
r &= (1 == read);
|
2018-09-19 06:39:17 -06:00
|
|
|
|
2014-03-03 15:07:58 -07:00
|
|
|
if (!r)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2018-10-10 06:15:20 -06:00
|
|
|
else if (ucs2_ch == L'\r')
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
|
|
|
std::cout << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
2018-09-19 06:39:17 -06:00
|
|
|
else if (ucs2_ch == L'\b')
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2016-07-31 18:48:53 -06:00
|
|
|
if (!pass.empty())
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2018-09-19 06:39:17 -06:00
|
|
|
int len = chlen.back();
|
|
|
|
chlen.pop_back();
|
|
|
|
while(len-- > 0)
|
|
|
|
pass.pop_back();
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
2018-09-19 06:39:17 -06:00
|
|
|
continue;
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
2018-09-19 06:39:17 -06:00
|
|
|
|
|
|
|
char utf8_ch[8] = {0};
|
|
|
|
int len;
|
|
|
|
if((len = WideCharToMultiByte(CP_UTF8, 0, &ucs2_ch, 1, utf8_ch, sizeof(utf8_ch), NULL, NULL)) <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(pass.size() + len >= tools::password_container::max_password_size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
chlen.push_back(len);
|
|
|
|
pass += utf8_ch;
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
::SetConsoleMode(h_cin, mode_old);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-12-17 16:07:15 -07:00
|
|
|
#else // end WIN32
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2016-12-17 16:07:15 -07:00
|
|
|
bool is_cin_tty() noexcept
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2016-12-17 16:07:15 -07:00
|
|
|
return 0 != isatty(fileno(stdin));
|
|
|
|
}
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2018-07-06 17:03:15 -06:00
|
|
|
int getch(bool hide_input) noexcept
|
2016-12-17 16:07:15 -07:00
|
|
|
{
|
|
|
|
struct termios tty_old;
|
|
|
|
tcgetattr(STDIN_FILENO, &tty_old);
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2016-12-17 16:07:15 -07:00
|
|
|
struct termios tty_new;
|
|
|
|
tty_new = tty_old;
|
2018-07-06 17:03:15 -06:00
|
|
|
tty_new.c_lflag &= ~(ICANON | (hide_input ? ECHO : 0));
|
2016-12-17 16:07:15 -07:00
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &tty_new);
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2016-12-17 16:07:15 -07:00
|
|
|
int ch = getchar();
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2016-12-17 16:07:15 -07:00
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &tty_old);
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2016-12-17 16:07:15 -07:00
|
|
|
return ch;
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
2016-12-17 16:07:15 -07:00
|
|
|
|
2018-07-06 17:03:15 -06:00
|
|
|
bool read_from_tty(epee::wipeable_string& aPass, bool hide_input)
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2016-12-17 16:07:15 -07:00
|
|
|
static constexpr const char BACKSPACE = 127;
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2016-12-17 16:07:15 -07:00
|
|
|
aPass.reserve(tools::password_container::max_password_size);
|
|
|
|
while (aPass.size() < tools::password_container::max_password_size)
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2018-07-06 17:03:15 -06:00
|
|
|
int ch = getch(hide_input);
|
2018-02-01 13:32:30 -07:00
|
|
|
if (EOF == ch || ch == EOT)
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (ch == '\n' || ch == '\r')
|
|
|
|
{
|
|
|
|
std::cout << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (ch == BACKSPACE)
|
|
|
|
{
|
2016-07-31 18:48:53 -06:00
|
|
|
if (!aPass.empty())
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2017-11-25 07:50:15 -07:00
|
|
|
aPass.pop_back();
|
2018-10-10 02:57:41 -06:00
|
|
|
if (!hide_input)
|
|
|
|
std::cout << "\b\b\b \b\b\b" << std::flush;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!hide_input)
|
|
|
|
std::cout << "\b\b \b\b" << std::flush;
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-31 18:48:53 -06:00
|
|
|
aPass.push_back(ch);
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-17 16:07:15 -07:00
|
|
|
#endif // end !WIN32
|
|
|
|
|
2018-07-06 17:03:15 -06:00
|
|
|
bool read_from_tty(const bool verify, const char *message, bool hide_input, epee::wipeable_string& pass1, epee::wipeable_string& pass2)
|
2016-12-17 16:07:15 -07:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (message)
|
2018-06-07 06:48:30 -06:00
|
|
|
std::cout << message <<": " << std::flush;
|
2018-07-06 17:03:15 -06:00
|
|
|
if (!read_from_tty(pass1, hide_input))
|
2016-12-17 16:07:15 -07:00
|
|
|
return false;
|
|
|
|
if (verify)
|
|
|
|
{
|
2017-11-28 09:15:50 -07:00
|
|
|
std::cout << "Confirm password: ";
|
2018-07-06 17:03:15 -06:00
|
|
|
if (!read_from_tty(pass2, hide_input))
|
2016-12-17 16:07:15 -07:00
|
|
|
return false;
|
|
|
|
if(pass1!=pass2)
|
|
|
|
{
|
|
|
|
std::cout << "Passwords do not match! Please try again." << std::endl;
|
2017-11-25 07:50:15 -07:00
|
|
|
pass1.clear();
|
|
|
|
pass2.clear();
|
2016-12-17 16:07:15 -07:00
|
|
|
}
|
|
|
|
else //new password matches
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
//No need to verify password entered at this point in the code
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-25 07:50:15 -07:00
|
|
|
bool read_from_file(epee::wipeable_string& pass)
|
2016-12-17 16:07:15 -07:00
|
|
|
{
|
|
|
|
pass.reserve(tools::password_container::max_password_size);
|
|
|
|
for (size_t i = 0; i < tools::password_container::max_password_size; ++i)
|
|
|
|
{
|
|
|
|
char ch = static_cast<char>(std::cin.get());
|
|
|
|
if (std::cin.eof() || ch == '\n' || ch == '\r')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (std::cin.fail())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pass.push_back(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace tools
|
|
|
|
{
|
|
|
|
// deleted via private member
|
|
|
|
password_container::password_container() noexcept : m_password() {}
|
|
|
|
password_container::password_container(std::string&& password) noexcept
|
|
|
|
: m_password(std::move(password))
|
|
|
|
{
|
|
|
|
}
|
2018-10-01 05:18:50 -06:00
|
|
|
password_container::password_container(const epee::wipeable_string& password) noexcept
|
|
|
|
: m_password(password)
|
|
|
|
{
|
|
|
|
}
|
2016-12-17 16:07:15 -07:00
|
|
|
|
|
|
|
password_container::~password_container() noexcept
|
|
|
|
{
|
2017-11-25 07:50:15 -07:00
|
|
|
m_password.clear();
|
2016-12-17 16:07:15 -07:00
|
|
|
}
|
|
|
|
|
2018-02-01 13:32:30 -07:00
|
|
|
std::atomic<bool> password_container::is_prompting(false);
|
|
|
|
|
2018-07-06 17:03:15 -06:00
|
|
|
boost::optional<password_container> password_container::prompt(const bool verify, const char *message, bool hide_input)
|
2016-12-17 16:07:15 -07:00
|
|
|
{
|
2018-02-01 13:32:30 -07:00
|
|
|
is_prompting = true;
|
2016-12-17 16:07:15 -07:00
|
|
|
password_container pass1{};
|
|
|
|
password_container pass2{};
|
2018-07-06 17:03:15 -06:00
|
|
|
if (is_cin_tty() ? read_from_tty(verify, message, hide_input, pass1.m_password, pass2.m_password) : read_from_file(pass1.m_password))
|
2018-02-01 13:32:30 -07:00
|
|
|
{
|
|
|
|
is_prompting = false;
|
2016-12-17 16:07:15 -07:00
|
|
|
return {std::move(pass1)};
|
2018-02-01 13:32:30 -07:00
|
|
|
}
|
2016-12-17 16:07:15 -07:00
|
|
|
|
2018-02-01 13:32:30 -07:00
|
|
|
is_prompting = false;
|
2016-12-17 16:07:15 -07:00
|
|
|
return boost::none;
|
|
|
|
}
|
2017-02-05 15:48:03 -07:00
|
|
|
|
2017-10-28 12:13:42 -06:00
|
|
|
boost::optional<login> login::parse(std::string&& userpass, bool verify, const std::function<boost::optional<password_container>(bool)> &prompt)
|
2017-02-05 15:48:03 -07:00
|
|
|
{
|
|
|
|
login out{};
|
|
|
|
|
2017-11-25 07:50:15 -07:00
|
|
|
const auto loc = userpass.find(':');
|
2017-02-05 15:48:03 -07:00
|
|
|
if (loc == std::string::npos)
|
|
|
|
{
|
2017-10-28 12:13:42 -06:00
|
|
|
auto result = prompt(verify);
|
2017-02-05 15:48:03 -07:00
|
|
|
if (!result)
|
|
|
|
return boost::none;
|
|
|
|
|
|
|
|
out.password = std::move(*result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-25 07:50:15 -07:00
|
|
|
out.password = password_container{userpass.substr(loc + 1)};
|
2017-02-05 15:48:03 -07:00
|
|
|
}
|
|
|
|
|
2017-11-25 07:50:15 -07:00
|
|
|
out.username = userpass.substr(0, loc);
|
|
|
|
password_container wipe{std::move(userpass)};
|
2017-02-05 15:48:03 -07:00
|
|
|
return {std::move(out)};
|
|
|
|
}
|
2016-12-17 16:07:15 -07:00
|
|
|
}
|