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
mod chomp;
use std::error;
use std::ffi::OsStr;
use std::fmt::{self, Display, Formatter};
use std::io::{self, Write};
use std::process::{Command, Stdio};
use std::string;
use Error::*;
use chomp::Chomp;
macro_rules! validate_path {
($path:expr) => {
if $path.trim().is_empty() {
return Err(InvalidInput);
}
};
}
#[derive(Debug)]
pub enum Error {
FromUtf8(string::FromUtf8Error),
Io(io::Error),
InvalidInput,
Pass(String),
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Io(error)
}
}
impl From<string::FromUtf8Error> for Error {
fn from(error: string::FromUtf8Error) -> Self {
FromUtf8(error)
}
}
impl Display for Error {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
let string =
match *self {
FromUtf8(ref error) => error.to_string(),
Io(ref error) => error.to_string(),
InvalidInput => "invalid input".to_string(),
Pass(ref error) => error.clone(),
};
write!(formatter, "{}", string)
}
}
impl error::Error for Error {
fn description(&self) -> &str {
match *self {
FromUtf8(ref error) => error.description(),
Io(ref error) => error.description(),
InvalidInput => "invalid input",
Pass(ref error) => error,
}
}
}
pub type Result<T> = std::result::Result<T, Error>;
pub struct PasswordStore;
impl PasswordStore {
pub fn get(path: &str) -> Result<String> {
validate_path!(path);
let mut password = exec_pass("", &[path], None)?;
password.chomp();
Ok(password)
}
pub fn get_usernames(path: &str) -> Result<Vec<String>> {
validate_path!(path);
let output = exec_pass("", &[path], None)?;
let mut usernames = vec![];
for line in output.lines().skip(1) {
usernames.push(line.chars().skip(4).collect());
}
Ok(usernames)
}
pub fn insert(path: &str, password: &str) -> Result<()> {
validate_path!(path);
exec_pass("insert", &["-m", path], Some(password))?;
Ok(())
}
pub fn remove(path: &str) -> Result<()> {
validate_path!(path);
exec_pass("rm", &["-f", path], None)?;
Ok(())
}
}
fn exec_pass<S: AsRef<OsStr>>(command: &str, args: &[S], input: Option<&str>) -> Result<String> {
let mut process = Command::new("pass");
if !command.trim().is_empty() {
process.arg(command);
}
let mut child = process.args(args)
.stderr(Stdio::piped())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()?;
if let (Some(stdin), Some(input)) = (child.stdin.as_mut(), input) {
write!(stdin, "{}\n", input)?;
}
let output = child.wait_with_output()?;
let mut stderr = String::from_utf8(output.stderr)?;
if !stderr.is_empty() {
stderr.chomp();
Err(Pass(stderr))
}
else {
Ok(String::from_utf8(output.stdout)?)
}
}