| 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 | // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers |
| 30 | |
| 31 | #include "password.h" |
| 32 | |
| 33 | #include <iostream> |
| 34 | #include <stdio.h> |
| 35 | |
| 36 | #if defined(_WIN32) |
| 37 | #include <io.h> |
| 38 | #include <windows.h> |
| 39 | #else |
| 40 | #include <termios.h> |
| 41 | #include <unistd.h> |
| 42 | #endif |
| 43 | |
| 44 | #define EOT 0x4 |
| 45 | |
| 46 | namespace |
| 47 | { |
| 48 | #if defined(_WIN32) |
| 49 | bool is_cin_tty() noexcept |
| 50 | { |
| 51 | return 0 != _isatty(_fileno(stdin)); |
| 52 | } |
| 53 | |
| 54 | bool read_from_tty(epee::wipeable_string& pass, bool hide_input) |
| 55 | { |
| 56 | HANDLE h_cin = ::GetStdHandle(STD_INPUT_HANDLE); |
| 57 | |
| 58 | DWORD mode_old; |
| 59 | ::GetConsoleMode(h_cin, &mode_old); |
| 60 | DWORD mode_new = mode_old & ~((hide_input ? ENABLE_ECHO_INPUT : 0) | ENABLE_LINE_INPUT); |
| 61 | ::SetConsoleMode(h_cin, mode_new); |
| 62 | |
| 63 | bool r = true; |
| 64 | pass.reserve(tools::password_container::max_password_size); |
| 65 | std::vector<int> chlen; |
| 66 | chlen.reserve(tools::password_container::max_password_size); |
| 67 | while (pass.size() < tools::password_container::max_password_size) |
| 68 | { |
| 69 | DWORD read; |
| 70 | wchar_t ucs2_ch; |
| 71 | r = (TRUE == ::ReadConsoleW(h_cin, &ucs2_ch, 1, &read, NULL)); |
| 72 | r &= (1 == read); |
| 73 | |
| 74 | if (!r) |
| 75 | { |
| 76 | break; |
| 77 | } |
| 78 | else if (ucs2_ch == L'\r') |
| 79 | { |
| 80 | std::cout << std::endl; |
| 81 | break; |
| 82 | } |
| 83 | else if (ucs2_ch == L'\b') |
| 84 | { |
| 85 | if (!pass.empty()) |
| 86 | { |
| 87 | int len = chlen.back(); |
| 88 | chlen.pop_back(); |
| 89 | while(len-- > 0) |
| 90 | pass.pop_back(); |
| 91 | } |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | char utf8_ch[8] = {0}; |
| 96 | int len; |
| 97 | if((len = WideCharToMultiByte(CP_UTF8, 0, &ucs2_ch, 1, utf8_ch, sizeof(utf8_ch), NULL, NULL)) <= 0) |
| 98 | break; |
| 99 | |
| 100 | if(pass.size() + len >= tools::password_container::max_password_size) |
| 101 | break; |
| 102 | |
| 103 | chlen.push_back(len); |
| 104 | pass += utf8_ch; |
| 105 | } |
| 106 | |
| 107 | ::SetConsoleMode(h_cin, mode_old); |
| 108 | |
| 109 | return r; |
| 110 | } |
| 111 | |
| 112 | #else // end WIN32 |
| 113 | |
| 114 | bool is_cin_tty() noexcept |
| 115 | { |
| 116 | return 0 != isatty(fileno(stdin)); |
| 117 | } |
| 118 | |
| 119 | int getch(bool hide_input) noexcept |
| 120 | { |
| 121 | struct termios tty_old; |
| 122 | tcgetattr(STDIN_FILENO, &tty_old); |
| 123 | |
| 124 | struct termios tty_new; |
| 125 | tty_new = tty_old; |
| 126 | tty_new.c_lflag &= ~(ICANON | (hide_input ? ECHO : 0)); |
| 127 | tcsetattr(STDIN_FILENO, TCSANOW, &tty_new); |
| 128 | |
| 129 | int ch = getchar(); |
| 130 | |
| 131 | tcsetattr(STDIN_FILENO, TCSANOW, &tty_old); |
| 132 | |
| 133 | return ch; |
| 134 | } |
| 135 | |
| 136 | bool read_from_tty(epee::wipeable_string& aPass, bool hide_input) |
| 137 | { |
| 138 | static constexpr const char BACKSPACE = 127; |
| 139 | |
| 140 | aPass.reserve(tools::password_container::max_password_size); |
| 141 | while (aPass.size() < tools::password_container::max_password_size) |
| 142 | { |
| 143 | int ch = getch(hide_input); |
| 144 | if (EOF == ch || ch == EOT) |
| 145 | { |
| 146 | return false; |
| 147 | } |
| 148 | else if (ch == '\n' || ch == '\r') |
| 149 | { |
| 150 | std::cout << std::endl; |
| 151 | break; |
| 152 | } |
| 153 | else if (ch == BACKSPACE) |
| 154 | { |
| 155 | if (!aPass.empty()) |
| 156 | { |
| 157 | aPass.pop_back(); |
| 158 | if (!hide_input) |
| 159 | std::cout << "\b\b\b \b\b\b" << std::flush; |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | if (!hide_input) |
| 164 | std::cout << "\b\b \b\b" << std::flush; |
| 165 | } |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | aPass.push_back(ch); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | #endif // end !WIN32 |
| 177 | |
| 178 | bool read_from_tty(const bool verify, const char *message, bool hide_input, epee::wipeable_string& pass1, epee::wipeable_string& pass2) |
| 179 | { |
| 180 | while (true) |
| 181 | { |
| 182 | if (message) |
| 183 | std::cout << message <<": " << std::flush; |
| 184 | if (!read_from_tty(pass1, hide_input)) |
| 185 | return false; |
| 186 | if (verify) |
| 187 | { |
| 188 | std::cout << "Confirm password: " << std::flush; |
| 189 | if (!read_from_tty(pass2, hide_input)) |
| 190 | return false; |
| 191 | if(pass1!=pass2) |
| 192 | { |
| 193 | std::cout << "Passwords do not match! Please try again." << std::endl; |
| 194 | pass1.clear(); |
| 195 | pass2.clear(); |
| 196 | } |
| 197 | else //new password matches |
| 198 | return true; |
| 199 | } |
| 200 | else |
| 201 | return true; |
| 202 | //No need to verify password entered at this point in the code |
| 203 | } |
| 204 | |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | bool read_from_file(epee::wipeable_string& pass) |
| 209 | { |
| 210 | pass.reserve(tools::password_container::max_password_size); |
| 211 | for (size_t i = 0; i < tools::password_container::max_password_size; ++i) |
| 212 | { |
| 213 | char ch = static_cast<char>(std::cin.get()); |
| 214 | if (std::cin.eof() || ch == '\n' || ch == '\r') |
| 215 | { |
| 216 | break; |
| 217 | } |
| 218 | else if (std::cin.fail()) |
| 219 | { |
| 220 | return false; |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | pass.push_back(ch); |
| 225 | } |
| 226 | } |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | } // anonymous namespace |
| 231 | |
| 232 | namespace tools |
| 233 | { |
| 234 | // deleted via private member |
| 235 | password_container::password_container() noexcept : m_password() {} |
| 236 | password_container::password_container(std::string&& password) noexcept |
| 237 | : m_password(std::move(password)) |
| 238 | { |
| 239 | } |
| 240 | password_container::password_container(const epee::wipeable_string& password) noexcept |
| 241 | : m_password(password) |
| 242 | { |
| 243 | } |
| 244 | |
| 245 | password_container::~password_container() noexcept |
| 246 | { |
| 247 | m_password.clear(); |
| 248 | } |
| 249 | |
| 250 | std::atomic<bool> password_container::is_prompting(false); |
| 251 | |
| 252 | boost::optional<password_container> password_container::prompt(const bool verify, const char *message, bool hide_input) |
| 253 | { |
| 254 | is_prompting = true; |
| 255 | password_container pass1{}; |
| 256 | password_container pass2{}; |
| 257 | if (is_cin_tty() ? read_from_tty(verify, message, hide_input, pass1.m_password, pass2.m_password) : read_from_file(pass1.m_password)) |
| 258 | { |
| 259 | is_prompting = false; |
| 260 | return {std::move(pass1)}; |
| 261 | } |
| 262 | |
| 263 | is_prompting = false; |
| 264 | return boost::none; |
| 265 | } |
| 266 | |
| 267 | boost::optional<login> login::parse(std::string&& userpass, bool verify, const std::function<boost::optional<password_container>(bool)> &prompt) |
| 268 | { |
| 269 | login out{}; |
| 270 | |
| 271 | const auto loc = userpass.find(':'); |
| 272 | if (loc == std::string::npos) |
| 273 | { |
| 274 | auto result = prompt(verify); |
| 275 | if (!result) |
| 276 | return boost::none; |
| 277 | |
| 278 | out.password = std::move(*result); |
| 279 | } |
| 280 | else |
| 281 | { |
| 282 | out.password = password_container{userpass.substr(loc + 1)}; |
| 283 | } |
| 284 | |
| 285 | out.username = userpass.substr(0, loc); |
| 286 | password_container wipe{std::move(userpass)}; |
| 287 | return {std::move(out)}; |
| 288 | } |
| 289 | } |