1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::error::Error;
#[cfg(target_family = "windows")]
pub use crate::windows::read_password;
#[cfg(target_family = "unix")]
pub use crate::unix::read_password;
#[derive(Debug)]
pub enum PromptError {
EnableFailed(std::io::Error),
IOError(std::io::Error),
}
impl std::fmt::Display for PromptError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
PromptError::EnableFailed(e) => write!(f, "Could not re-enable echo: {}", e),
PromptError::IOError(e) => e.fmt(f),
}
}
}
impl From<std::io::Error> for PromptError {
fn from(e: std::io::Error) -> PromptError {
PromptError::IOError(e)
}
}
impl Error for PromptError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
PromptError::EnableFailed(e) => Some(e),
PromptError::IOError(e) => Some(e),
}
}
}
#[cfg(target_family = "windows")]
mod windows {
use crate::PromptError;
use windows::Win32::Foundation::{BOOL, HANDLE};
use windows::Win32::System::Console::{
GetConsoleMode, GetStdHandle, SetConsoleMode, CONSOLE_MODE, ENABLE_ECHO_INPUT,
STD_INPUT_HANDLE,
};
fn set_stdin_echo(echo: bool, handle: HANDLE) -> Result<(), PromptError> {
let mut mode: u32 = 0;
unsafe {
let mode_ptr: *mut u32 = &mut mode;
if GetConsoleMode(handle, mode_ptr as *mut CONSOLE_MODE) == BOOL::from(false) {
return Err(PromptError::IOError(std::io::Error::last_os_error()));
}
}
let mut mode = CONSOLE_MODE::from(mode);
if !echo {
mode &= !ENABLE_ECHO_INPUT;
} else {
mode |= ENABLE_ECHO_INPUT;
}
unsafe {
if SetConsoleMode(handle, mode) == BOOL::from(false) {
let err = std::io::Error::last_os_error();
if echo {
return Err(PromptError::EnableFailed(err));
} else {
return Err(PromptError::IOError(err));
}
}
}
Ok(())
}
pub fn read_password() -> Result<String, PromptError> {
let mut pass = String::new();
let handle = unsafe { GetStdHandle(STD_INPUT_HANDLE) };
set_stdin_echo(false, handle)?;
let stdin = std::io::stdin();
match stdin.read_line(&mut pass) {
Ok(_) => {}
Err(e) => {
set_stdin_echo(true, handle)?;
return Err(PromptError::IOError(e));
}
};
pass = pass.trim().to_string();
set_stdin_echo(true, handle)?;
Ok(pass)
}
}
#[cfg(target_family = "unix")]
mod unix {
use libc::{tcgetattr, tcsetattr, termios, ECHO, STDIN_FILENO, TCSANOW};
use std::mem::MaybeUninit;
use crate::PromptError;
fn set_stdin_echo(echo: bool) -> Result<(), PromptError> {
let mut tty = MaybeUninit::<termios>::uninit();
unsafe {
if tcgetattr(STDIN_FILENO, tty.as_mut_ptr()) != 0 {
return Err(PromptError::IOError(std::io::Error::last_os_error()));
}
}
let mut tty = unsafe { tty.assume_init() };
if !echo {
tty.c_lflag &= !ECHO;
} else {
tty.c_lflag |= ECHO;
}
unsafe {
if tcsetattr(STDIN_FILENO, TCSANOW, &mut tty as *mut _) != 0 {
let err = std::io::Error::last_os_error();
if echo {
return Err(PromptError::EnableFailed(err));
} else {
return Err(PromptError::IOError(err));
}
}
}
Ok(())
}
pub fn read_password() -> Result<String, PromptError> {
let mut pass = String::new();
set_stdin_echo(false)?;
let stdin = std::io::stdin();
match stdin.read_line(&mut pass) {
Ok(_) => {}
Err(e) => {
set_stdin_echo(true)?;
return Err(PromptError::IOError(e));
}
};
set_stdin_echo(true)?;
pass = pass.trim().to_string();
Ok(pass)
}
}