passlane 2.3.2

A password manager for the command line
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
use clap::Command;
use clipboard::ClipboardContext;
use clipboard::ClipboardProvider;
use crate::{crypto, keychain};
use crate::store;
use crate::ui;
use anyhow::{bail, Context};
use clap::ArgMatches;
use log::{debug, info};
use std::io;
use crate::vault::entities::{Credential};
use crate::vault::keepass_vault::KeepassVault;
use crate::vault::vault_trait::Vault;
use anyhow::Result;

pub trait Action {
    fn run(&self) -> anyhow::Result<()> {
        Ok(())
    }
}

pub trait UnlockingAction: Action {
    fn execute(&self)  {
        info!("Unlocking vault...");
        match self.unlock() {
            Ok(mut vault) => {
                match self.run_with_vault(&mut vault) {
                    Ok(_) => {
                        info!("Action completed successfully");
                    }
                    Err(e) => {
                        println!("Failed to run action: {}", e);
                    }
                }
            }
            Err(e) => {
                println!("Failed to unlock vault: {}", e);
            }
        }
    }

    fn run_with_vault(&self, _: &mut Box<dyn Vault>) -> anyhow::Result<()> {
        Ok(())
    }

    fn get_vault(&self, password: &str, filepath: &str, keyfile_path: Option<String>) -> anyhow::Result<Box<dyn Vault>> {
        // we could return some other Vault implementation here
        let vault = KeepassVault::new(password, filepath, keyfile_path);
        match vault {
            Ok(v) => Ok(Box::new(v)),
            Err(e) => {
                bail!("Incorrect password? {}", e);
            }
        }
    }
    fn unlock(&self) -> Result<Box<dyn Vault>> {
        let stored_password = keychain::get_master_password();
        let master_pwd = stored_password.unwrap_or_else(|_| ui::ask_master_password(None));
        let filepath = store::get_vault_path();
        let keyfile_path = store::get_keyfile_path();
        self.get_vault(&master_pwd, &filepath, keyfile_path)
    }
}

pub fn copy_to_clipboard(value: &String) {
    let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
    ctx.set_contents(String::from(value)).unwrap();
}

#[derive(PartialEq)]
pub enum ItemType {
    Credential,
    Payment,
    Note,
}

impl ItemType {
    pub fn new_from_args(matches: &ArgMatches) -> ItemType {
        if *matches
            .get_one::<bool>("payments")
            .expect("defaulted to false by clap") {
            ItemType::Payment
        } else if *matches.get_one("notes").expect("defaulted to false by clap") {
            ItemType::Note
        } else {
            ItemType::Credential
        }
    }
}

pub struct AddAction {
    pub generate: bool,
    pub clipboard: bool,
    pub item_type: ItemType,
}


impl AddAction {
    pub fn new(matches: &ArgMatches) -> AddAction {
        AddAction {
            generate: *matches
                .get_one::<bool>("generate")
                .expect("defaulted to false by clap"),
            clipboard: *matches
                .get_one::<bool>("clipboard")
                .expect("defaulted to false by clap"),
            item_type: ItemType::new_from_args(matches),
        }
    }
    fn password_from_clipboard(&self) -> anyhow::Result<String> {
        let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
        let value = ctx
            .get_contents()
            .expect("Unable to retrieve value from clipboard");
        if !crypto::validate_password(&value) {
            bail!("The text in clipboard is not a valid password");
        }
        Ok(value)
    }
    fn get_password(&self) -> anyhow::Result<String> {
        if self.generate {
            Ok(crypto::generate())
        } else if self.clipboard {
            self.password_from_clipboard()
        } else {
            Ok(ui::ask_password("Enter password to save: "))
        }
    }
    fn save(&self, creds: &Credential, vault: &mut Box<dyn Vault>) -> anyhow::Result<()> {
        vault.save_one_credential(creds.clone());
        println!("Saved.");
        Ok(())
    }
    fn add_credential(&self, vault: &mut Box<dyn Vault>) -> anyhow::Result<(), anyhow::Error> {
        let password = self.get_password().context(format!(
            "Failed to get password {}",
            if self.clipboard { "from clipboard" } else { "" }
        ))?;

        let creds = ui::ask_credentials(&password);
        self.save(&creds, vault)
            .context("failed to save")?;
        if !self.clipboard {
            copy_to_clipboard(&password);
            println!("Password - also copied to clipboard: {}", password);
        };
        Ok(())
    }
    fn add_payment(&self, vault: &mut Box<dyn Vault>) -> anyhow::Result<()> {
        let payment = ui::ask_payment_info();
        vault.save_payment(payment);
        Ok(())
    }
    fn add_note(&self, vault: &mut Box<dyn Vault>) -> anyhow::Result<()> {
        let note = ui::ask_note_info();
        vault.save_note(&note);
        println!("Note saved.");
        Ok(())
    }
}

impl Action for AddAction {}

impl UnlockingAction for AddAction {
    fn run_with_vault(&self, vault: &mut Box<dyn Vault>) -> anyhow::Result<()> {
        match self.item_type {
            ItemType::Credential => self.add_credential(vault)?,
            ItemType::Payment => self.add_payment(vault)?,
            ItemType::Note => self.add_note(vault)?
        };
        Ok(())
    }
}

pub struct ShowAction {
    pub grep: Option<String>,
    pub verbose: bool,
    pub item_type: ItemType,
}

impl ShowAction {
    pub fn new(matches: &ArgMatches) -> ShowAction {
        ShowAction {
            grep: matches.get_one::<String>("REGEXP").cloned(),
            verbose: *matches
                .get_one::<bool>("verbose")
                .expect("defaulted to false by clap"),
            item_type: ItemType::new_from_args(matches),
        }
    }
    fn show_credentials(&self, vault: &mut Box<dyn Vault>) -> anyhow::Result<()> {
        let grep = match &self.grep {
            Some(grep) => Some(String::from(grep)),
            None => panic!("-g <REGEXP> is required"),
        };
        let matches = find_credentials(&vault, grep).context("Failed to find matches. Invalid password? Try unlocking the vault with `passlane unlock`.")?;

        if matches.len() >= 1 {
            println!("Found {} matches:", matches.len());
            ui::show_credentials_table(&matches, self.verbose);
            if matches.len() == 1 {
                copy_to_clipboard(&matches[0].password);
                println!("Password copied to clipboard!", );
            } else {
                match ui::ask_index(
                    "To copy one of these passwords to clipboard, please enter a row number from the table above, or press q to exit:",
                    matches.len() as i16 - 1,
                ) {
                    Ok(index) => {
                        copy_to_clipboard(&matches[index].password);
                        println!("Password from index {} copied to clipboard!", index);
                    }
                    Err(message) => {
                        println!("{}", message);
                    }
                }
            }
        }
        Ok(())
    }

    fn show_payments(&self, vault: &mut Box<dyn Vault>) -> anyhow::Result<()> {
        debug!("showing payments");
        let matches = vault.find_payments();
        if matches.len() == 0 {
            println!("No payment cards found");
        } else {
            println!("Found {} payment cards:", matches.len());
            ui::show_payment_cards_table(&matches, self.verbose);

            if matches.len() == 1 {
                let response = ui::ask("Do you want to see the card details? (y/n)");
                if response == "y" {
                    ui::show_card(&matches[0]);
                    copy_to_clipboard(&matches[0].number);
                    println!("Card number copied to clipboard!", );
                }
            } else {
                match ui::ask_index(
                    "Enter a row number from the table above, or press q to exit:",
                    matches.len() as i16 - 1,
                ) {
                    Ok(index) => {
                        ui::show_card(&matches[index]);
                        copy_to_clipboard(&matches[index].number);
                        println!("Card number copied to clipboard!");
                    }
                    Err(message) => {
                        println!("{}", message);
                    }
                }
            }
        }
        Ok(())
    }

    fn show_notes(&self, vault: &mut Box<dyn Vault>) -> anyhow::Result<()> {
        debug!("showing notes");
        let matches = vault.find_notes();
        if matches.len() == 0 {
            println!("No notes found");
        } else {
            println!("Found {} notes:", matches.len());
            ui::show_notes_table(&matches, self.verbose);


            if matches.len() == 1 {
                let response = ui::ask("Do you want to see the full note? (y/n)");
                if response == "y" {
                    ui::show_note(&matches[0]);
                }
            } else {
                match ui::ask_index(
                    "Enter a row number from the table above, or press q to exit:",
                    matches.len() as i16 - 1,
                ) {
                    Ok(index) => {
                        ui::show_note(&matches[index]);
                    }
                    Err(message) => {
                        println!("{}", message);
                    }
                }
            }
        }
        Ok(())
    }
}

fn find_credentials(
    vault: &Box<dyn Vault>,
    grep: Option<String>,
) -> anyhow::Result<Vec<Credential>> {
    let matches = vault.grep(&grep);
    if matches.is_empty() {
        println!("No matches found");
    }
    Ok(matches)
}

impl Action for ShowAction {}

impl UnlockingAction for ShowAction {
    fn run_with_vault(&self, vault: &mut Box<dyn Vault>) -> anyhow::Result<()> {
        match self.item_type {
            ItemType::Credential => self.show_credentials(vault)?,
            ItemType::Payment => self.show_payments(vault)?,
            ItemType::Note => self.show_notes(vault)?
        };
        Ok(())
    }
}

pub struct ExportAction {
    pub file_path: String,
    pub item_type: ItemType,
}

impl ExportAction {
    pub fn new(matches: &ArgMatches) -> ExportAction {
        ExportAction {
            file_path: matches.get_one::<String>("file_path").expect("required").to_string(),
            item_type: ItemType::new_from_args(matches),
        }
    }
    pub fn export_csv(&self, vault: &mut Box<dyn Vault>) -> anyhow::Result<i64> {
        debug!("exporting to csv");
        if self.item_type == ItemType::Credential {
            let creds = find_credentials(&vault, None)?;
            store::write_credentials_to_csv(&self.file_path, &creds)
        } else if self.item_type == ItemType::Payment {
            let cards = vault.find_payments();
            store::write_payment_cards_to_csv(&self.file_path, &cards)
        } else if self.item_type == ItemType::Note {
            let notes = vault.find_notes();
            store::write_secure_notes_to_csv(&self.file_path, &notes)
        } else {
            Ok(0)
        }
    }
}


impl Action for ExportAction {}

impl UnlockingAction for ExportAction {
    fn run_with_vault(&self, vault: &mut Box<dyn Vault>) -> anyhow::Result<()> {
        match self.export_csv(vault) {
            Err(message) => println!("Failed to export: {}", message),
            Ok(count) => println!("Exported {} entries", count),
        }
        Ok(())
    }
}

pub struct DeleteAction {
    pub grep: Option<String>,
    pub item_type: ItemType,
}

impl DeleteAction {
    pub fn new(matches: &ArgMatches) -> DeleteAction {
        DeleteAction {
            grep: matches.get_one::<String>("REGEXP").cloned(),
            item_type: ItemType::new_from_args(matches),
        }
    }
}

fn delete_credentials(vault: &mut Box<dyn Vault>, grep: &str) -> anyhow::Result<()> {
    let matches = find_credentials(&vault, Some(String::from(grep))).context("Unable to get matches. Invalid password? Try unlocking again.")?;

    if matches.is_empty() {
        debug!("no matches found to delete");
        return Ok(());
    }
    if matches.len() == 1 {
        vault.delete_credentials(&matches.get(0).unwrap().uuid);
        println!("Deleted credential for service '{}'", matches[0].service);
    }
    if matches.len() > 1 {
        ui::show_credentials_table(&matches, false);
        match ui::ask_index(
            "To delete, please enter a row number from the table above, press a to delete all, or press q to abort:",
            matches.len() as i16 - 1,
        ) {
            Ok(index) => {
                if index == usize::MAX {
                    vault.delete_matching(grep);
                    println!("Deleted all {} matches!", matches.len());
                } else {
                    vault.delete_credentials(&matches[index].uuid);
                    println!("Deleted credentials of row {}!", index);
                }
            }
            Err(message) => {
                println!("{}", message);
            }
        }
    }
    Ok(())
}

fn delete_payment(vault: &mut Box<dyn Vault>) -> anyhow::Result<()> {
    let cards = vault.find_payments();
    if cards.is_empty() {
        println!("No payment cards found");
        return Ok(());
    }
    ui::show_payment_cards_table(&cards, false);
    if cards.len() == 1 {
        let response = ui::ask("Do you want to delete this card? (y/n)");
        if response == "y" {
            vault.delete_payment(&cards[0].id);
            println!("Deleted card named '{}'!", cards[0].name);
        }
        return Ok(());
    }
    match ui::ask_index(
        "To delete, please enter a row number from the table above, or press q to abort:",
        cards.len() as i16 - 1,
    ) {
        Ok(index) => {
            if index == usize::MAX {
                // ignore                   
            } else {
                vault.delete_payment(&cards[index].id);
                println!("Deleted card named '{}'!", cards[index].name);
            }
        }
        Err(message) => {
            println!("{}", message);
        }
    }
    Ok(())
}

fn delete_note(vault: &mut Box<dyn Vault>) -> anyhow::Result<()> {
    let notes = vault.find_notes();
    if notes.len() == 0 {
        println!("No notes found");
        return Ok(());
    }
    ui::show_notes_table(&notes, false);
    if notes.len() == 1 {
        let response = ui::ask("Do you want to delete this note? (y/n)");
        if response == "y" {
            vault.delete_note(&notes[0].id);
            println!("Deleted note with title '{}'!", notes[0].title);
        }
        return Ok(());
    }
    match ui::ask_index(
        "To delete, please enter a row number from the table above, or press q to abort:",
        notes.len() as i16 - 1,
    ) {
        Ok(index) => {
            if index == usize::MAX {
                // ignore                   
            } else {
                vault.delete_note(&notes[index].id);
                println!("Deleted note with title '{}'!", notes[index].title);
            }
        }
        Err(message) => {
            println!("{}", message);
        }
    }
    Ok(())
}


impl Action for DeleteAction {}

impl UnlockingAction for DeleteAction {
    fn run_with_vault(&self, vault: &mut Box<dyn Vault>) -> anyhow::Result<()> {
        match self.item_type {
            ItemType::Credential => {
                let grep = match &self.grep {
                    Some(grep) => grep,
                    None => panic!("-g <REGEXP> is required"),
                };
                delete_credentials(vault, grep)?;
            }
            ItemType::Payment => {
                delete_payment(vault)?;
            }
            ItemType::Note => {
                delete_note(vault)?;
            }
        };
        Ok(())
    }
}

pub struct ImportCsvAction {
    pub file_path: String,
}

impl ImportCsvAction {
    pub fn new(matches: &ArgMatches) -> ImportCsvAction {
        ImportCsvAction {
            file_path: matches.get_one::<String>("FILE_PATH").expect("required").to_string(),
        }
    }
}

fn push_from_csv(vault: &mut Box<dyn Vault>, file_path: &str) -> anyhow::Result<i64> {
    let input = store::read_from_csv(file_path)?;
    let creds = input.into_iter().map(|c| c.to_credential()).collect();

    vault.save_credentials(&creds);
    let num_imported = creds.len();
    Ok(num_imported.try_into().unwrap())
}


impl Action for ImportCsvAction {}

impl UnlockingAction for ImportCsvAction {
    fn run_with_vault(&self, vault: &mut Box<dyn Vault>) -> anyhow::Result<()> {
        match push_from_csv(vault, &self.file_path) {
            Err(message) => println!("Failed to import: {}", message),
            Ok(count) => println!("Imported {} entries", count),
        }
        Ok(())
    }
}

pub struct GeneratePasswordAction {}

impl Action for GeneratePasswordAction {
    fn run(&self) -> anyhow::Result<()> {
        let password = crypto::generate();
        copy_to_clipboard(&password);
        println!("Password - also copied to clipboard: {}", password);
        Ok(())
    }
}

pub struct LockAction {}

impl Action for LockAction {
    fn run(&self) -> anyhow::Result<()> {
        keychain::delete_master_password()?;
        Ok(())
    }
}

pub struct UnlockAction {}

impl Action for UnlockAction {}

impl UnlockingAction for UnlockAction {
    fn run_with_vault(&self, vault: &mut Box<dyn Vault>) -> anyhow::Result<()> {
        keychain::save_master_password(&vault.get_master_password())?;
        Ok(())
    }
}

pub struct PrintHelpAction {
    cli: Command,
}

impl PrintHelpAction {
    pub fn new(cli: Command) -> PrintHelpAction {
        PrintHelpAction {
            cli
        }
    }
}

impl Action for PrintHelpAction {
    fn run(&self) -> anyhow::Result<()> {
        let mut out = io::stdout();
        self.cli.clone().write_help(&mut out).context("Failed to display help!")?;
        Ok(())
    }
}