rass 0.2.0

A rust implementation for the standard *nix password manager: pass
Documentation
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
extern crate rasslib;
#[macro_use]
extern crate clap;
extern crate rpassword;
extern crate tempfile;

use std::io;
use std::io::prelude::*;
use std::env;
use std::path::PathBuf;
use std::process;

use clap::{App, Arg, ArgMatches, SubCommand};

use rasslib::store::PassStore;
use rasslib::vcs;

use tempfile::NamedTempFile;

static STORE_DIR_ENV_NAME: &'static str = "PASSWORD_STORE_DIR";

fn main() {
    let store = match env::var(STORE_DIR_ENV_NAME) {
        Ok(val) => {
            let p = PathBuf::from(val);
            PassStore::from(&p)
        },
        Err(_)  => PassStore::new(),
    };
    let store = match store {
        Ok(s) => s,
        Err(e) =>
        {
            println!("Error parsing store {}", e);
            return
        }
    };
    let vcs = vcs::GitWrapper::new(&store.get_location());

    let mut app = PassstoreApp {
        store: store,
    };

    let matches = get_matches();

    if matches.is_present("verbose") {
        app.store.set_verbose(true);
    }

    let ran_subcommand = match matches.subcommand() {
        ("edit", Some(matches)) =>   { app.edit(vcs, &matches); true }
        ("find", Some(matches)) =>   { app.find(&matches); true }
        ("insert", Some(matches)) => { app.insert(vcs, &matches); true }
        ("add", Some(matches)) =>    { app.insert(vcs, &matches); true } // alias for insert
        ("show", Some(matches)) =>   { app.show(&matches); true }
        ("ls", Some(matches)) =>     { app.list(&matches); true }
        ("git", Some(matches)) =>    { app.git_exec(vcs, &matches); true }
        ("rm", Some(matches)) =>     { app.remove(vcs, &matches); true }
        ("grep", Some(matches)) =>   { app.grep(&matches); true }
        _ => false
    };

    if !ran_subcommand {
        if  matches.is_present("PASS") {
            app.show(&matches);
        }
        else {
            app.list(&matches);
        }
    }
}

#[derive(Debug)]
struct PassstoreApp {
    store: PassStore,
}

impl PassstoreApp {
    fn git_exec<T: vcs::VersionControl>(&self, vcs: T, matches: &ArgMatches) {
        if !matches.is_present("PARAMS") {
            println!("Not git parameters found!");
            process::exit(-1);
        }

        let params: Vec<_> = matches.values_of("PARAMS").unwrap().collect();

        if let Ok(r) = vcs.cmd_dispatch(params) {
            process::exit(r.code().unwrap_or(-1))
        }
    }

    fn insert<T: vcs::VersionControl>(&mut self, vcs: T, matches: &ArgMatches) {
        let pass = matches.value_of("PASS").unwrap_or("");

        match self.store.get(pass) {
            Some(_) => {
                let q = format!("An entry already exists for {}.\
                                Overwrite it? [y/N] ", pass);
                match yes_no(q.as_ref(), YesNoAnswer::NO) {
                    YesNoAnswer::NO  => return,
                    YesNoAnswer::YES => (),
                }
            },
            None => (),
        };

        let multiline = matches.is_present("multiline");

        let stdin = io::stdin();
        let mut buffer = vec![];

        if multiline {
            println!("Enter contents for {} and press Ctrl+D when finsihed:\n", pass);
            match stdin.lock().read_to_end(&mut buffer) {
                Ok(..) => (),
                Err(err) => panic!("Something went wrong: {}", err)
            }
        } else {
            let pw = single_line_password(pass);
            buffer = pw.into_bytes();
            buffer.push('\n' as u8);
        }

        match self.store.insert(vcs, pass, buffer) {
            Ok(_) => (),
            Err(err) => panic!("{}", err)
        }
    }

    fn list(&self, matches: &ArgMatches) {
        let pass = matches.value_of("PASS").unwrap_or_default();

        let pass = if pass.ends_with("/") {
            &pass[0..pass.len()-1]
        } else {
            pass
        };

        if let Some(path) = self.store.get(pass) {
            self.store.print_tree(&path);
        } else {
            println!("Unable to find path for '{}'", pass);
        }
    }

    fn show(&self, matches: &ArgMatches) {
        let pass = matches.value_of("PASS").unwrap_or("");
        if let Some(entry) = self.store.get(pass) {
            if entry.is_leaf() {
                match self.store.read(&entry) {
                    Some(x) => print!("{}", x),
                    None => println!("Unable to read!"),
                }
            } else {
                self.store.print_tree(&entry);
            }
        } else {
            println!("Error: {} is not in the password store.", pass);
        }
    }

    fn find(&self, matches: &ArgMatches) {
        let query = matches.value_of("QUERY").unwrap();
        let print = matches.is_present("print");
        //let matches = match matches.is_present("name") {
            //true => self.store.find_by_name(query),
            //_    => self. store.find_by_location(query),
        //};
        let matches = self.store.find(query);

        if matches.len() == 1 {
            let e = &matches[0];
            println!("Only found: '{}'", e);
            if let Some(x) =  self.store.read(e) {
                println!("{}", x);
                return
            } else {
                println!("Unable to read!");
            }
        }

        for e in matches {
            if print {
                match self.store.read(&e) {
                    Some(x) => println!("{}:\n{}", e, x),
                    None => println!("Unable to read!"),
                }
            }
            else {
                println!("{}", e)
            }
        }
    }

    fn remove<T: vcs::VersionControl>(&mut self, vcs: T, matches: &ArgMatches) {
        let pass = matches.value_of("PASS").unwrap_or("");
        if let Some(entry) = self.store.get(pass) {
            if !matches.is_present("force") {
                let q = format!("Are you sure you would like to delete {}? [y/N]", pass);;
                match yes_no(q.as_ref(), YesNoAnswer::NO) {
                    YesNoAnswer::NO  => return,
                    YesNoAnswer::YES => (),
                }
                let _ = self.store.remove(vcs, &entry);
            }
        } else {
            println!("Error: {} is not in the password store.", pass);
        }
    }

    fn grep(&self, matches: &ArgMatches) {
        let params : Vec<&str>  = matches.values_of("PARAMS").unwrap().collect();
        if params.len() < 1 {
            println!("No search team specified");
            process::exit(-1);
        }

        let searcher = matches.value_of("SEACHER").unwrap_or("grep");
        if let Ok(out) = self.store.grep(&searcher, &params) {
            println!("{}", out);
        }
    }

    fn edit<T: vcs::VersionControl>(&mut self, vcs: T, matches: &ArgMatches) {
        let pass = matches.value_of("PASS").unwrap_or("");
        if let Some(entry) = self.store.get(pass) {
            if let Some(content) = self.store.read(&entry) {
                if let Some(content) = edit_in_tempfile(&content) {
                    match self.store.insert(vcs, pass, content) {
                        Ok(_) => (),
                        Err(err) => panic!("{}", err)
                    }
                }
            }
            else { println!("Error: Unable to read {}.", entry); }
            //let _ = self.store.remove(vcs, &entry);
        } else {
            println!("Error: {} is not in the password store.", pass);
        }
    }
}



fn get_matches<'a>() -> ArgMatches<'a> {
    App::new("rass")
        .author("Armin Widegreen, armin.widegreen@gmail.com")
        .version(crate_version!())
        .about("A manager for password-store, the *nix command line password manager")
        .arg(Arg::with_name("PASS")
             .help("pass-name which shall be shown, first try pass-name (full path),\
                   if nothing is found, I'll try just the pass name.")
             .required(false)
             .index(1)
             )
        .arg(Arg::with_name("verbose")
             .help("Print verbose information during execution.")
             .long("verbose")
             .short("v"))
        .subcommand(SubCommand::with_name("find")
                    .about("Query a pass store entry")
                    .arg(Arg::with_name("print")
                         .short("p")
                         .long("print")
                         .help("Immediately print all results"))
                    .arg(Arg::with_name("QUERY")
                         .help("Query string use for the find command")
                         .required(true)
                         .index(1))
                    .arg(Arg::with_name("name")
                         .short("n")
                         .long("name")
                         .help("use name instead of location for find")))
        .subcommand(SubCommand::with_name("show")
                    .about("Show, print a given entry. First try \
                            complete location within the store, afterwards, \
                            if nothing found, just go with the name!")
                    .arg(Arg::with_name("PASS")
                        .help("PASS which shall be shown, first try \
                               pass-name (full path), if nothing is found, I'll\
                               try just the pass name.")
                        .required(true)
                        .index(1)))
        .subcommand(SubCommand::with_name("insert")
                    .about("Inserts a new entry to the store.")
                    .arg(Arg::with_name("multiline")
                         .short("m")
                         .help("Use multiline inport for new entry."))
                    .arg(Arg::with_name("PASS")
                        .required(true)
                        .index(1)))
        .subcommand(SubCommand::with_name("add")
                    .about("Inserts a new entry to the store.")
                    .arg(Arg::with_name("multiline")
                         .short("m")
                         .help("Use multiline inport for new entry."))
                    .arg(Arg::with_name("PASS")
                        .required(true)
                        .index(1)))
        .subcommand(SubCommand::with_name("ls")
                    .about("List the whole store")
                    .arg(Arg::with_name("long")
                         .help("Print the full qualified location instead.")
                         .short("l"))
                    .arg(Arg::with_name("PASS")
                         .help("Print a sub-entry instead of full store.")
                         .default_value("")
                         .required(false)
                         .index(1)))
        .subcommand(SubCommand::with_name("edit")
                    .about("Edit a given entry.")
                    .arg(Arg::with_name("PASS")
                         .help("Entry which shall be edited, first try \
                                pass-name (full path), if nothing is found, I'll\
                                try just the pass name.")
                         .required(true)
                         .index(1)))
        .subcommand(SubCommand::with_name("rm")
                    .about("Remove entry from the store")
                    .arg(Arg::with_name("PASS")
                        .required(true)
                        .index(1))
                    //.arg(Arg::with_name("recursive")
                         //.help("Removes everything recursively.")
                         //.short("r"))
                    .arg(Arg::with_name("force")
                         .short("f")
                         .long("force")
                         .help("Forces to delete an entry, without interaction.")))
        .subcommand(SubCommand::with_name("git")
                    .about("Dispatch git command to execute within the store")
                    .arg(Arg::with_name("PARAMS")
                         .multiple(true)
                         .required(true)))
        .subcommand(SubCommand::with_name("grep")
                    .about("Greps for given search term in the password store. \
                          Relays the all parameter (except searcher) to to the \
                          command specified in SEACHER parameter, default \
                          'grep'. Therefore standard grep options apply.")
                    .arg(Arg::with_name("SEARCHER")
                         .possible_values(&["ag", "grep", "ack"])
                         .short("s")
                         .long("searcher")
                         .required(false)
                         .default_value("grep"))
                    .arg(Arg::with_name("PARAMS")
                         .multiple(true)
                         .required(true)))
        .get_matches()
}

fn single_line_password(pass: &str) -> String {
    let mut stdout = std::io::stdout();
    loop {
        print!("Enter password for {}: ", pass);
        stdout.flush().unwrap();
        let password = rpassword::read_password().unwrap();

        print!("Confirm password for {}: ", pass);
        stdout.flush().unwrap();
        let password_confirm = rpassword::read_password().unwrap();
        if password != password_confirm {
            println!("Error: the entered passwords do not match.");
        } else {
            return password.to_string();
        }
    }
}

#[derive(Debug)]
enum YesNoAnswer {
    YES,
    NO,
}

fn  yes_no(message: &str, default: YesNoAnswer) -> YesNoAnswer {
    let mut stdout = std::io::stdout();

    print!("{} ", message);
    stdout.flush().unwrap();

    let mut input = String::new();
    io::stdin().read_line(&mut input).unwrap();

    match input.trim() {
        "Yes" | "yes" | "Y" | "y" | "YeS" | "YES" => YesNoAnswer::YES,
        "No" | "NO" | "n" | "N"                   => YesNoAnswer::NO,
        _                                         => default,
    }
}

fn edit_in_tempfile(content: &str) -> Option<String> {
    let mut file = NamedTempFile::new().unwrap();
    let _ = write!(file, "{}\n", &content);

    match process::Command::new(env::var("EDITOR").unwrap_or("vim".to_string()))
        .arg(file.path().to_str().unwrap()).status() {
        Err(e) => {
            println!("Error occured: '{:?}'", e);
            return None
        },
        Ok(x) => if !x.success() {
            println!("Editor exit was failure!");
            return None
        }
    }

    let mut f = io::BufReader::new(file.try_clone().unwrap());
    let _ = f.seek(io::SeekFrom::Start(0));
    let mut result = String::new();

    match f.read_to_string(&mut result) {
        Ok(_) => Some(result),
        Err(_) => None,
    }
}