From b6f322a99f5c864868ad1450bfeb342e7dae3222 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 27 Dec 2018 16:56:23 +0100 Subject: Implement the pws get subcommand MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch implements the pws get subcommand that provides read access to a slot of the password safe. Per default, all available information – slot name, login and password – are printed. If one or more of the options --name, --login and --password are set, only the selected fields are printed. If --quiet is set, the field description is omitted such that the output can be easily parsed by other applications. --- nitrocli/src/args.rs | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'nitrocli/src/args.rs') diff --git a/nitrocli/src/args.rs b/nitrocli/src/args.rs index df7f56c..1a54c43 100644 --- a/nitrocli/src/args.rs +++ b/nitrocli/src/args.rs @@ -330,9 +330,9 @@ enum PwsCommand { } impl PwsCommand { - fn execute(&self, _args: Vec) -> Result<()> { + fn execute(&self, args: Vec) -> Result<()> { match *self { - PwsCommand::Get => Err(Error::Error("Not implemented".to_string())), + PwsCommand::Get => pws_get(args), } } } @@ -811,6 +811,46 @@ fn pws(args: Vec) -> Result<()> { subcommand.execute(subargs) } +/// Access a slot of the password safe on the Nitrokey. +fn pws_get(args: Vec) -> Result<()> { + let mut slot: u8 = 0; + let mut name = false; + let mut login = false; + let mut password = false; + let mut quiet = false; + let mut parser = argparse::ArgumentParser::new(); + parser.set_description("Reads a password safe slot"); + let _ = parser.refer(&mut slot).required().add_argument( + "slot", + argparse::Store, + "The PWS slot to read", + ); + let _ = parser.refer(&mut name).add_option( + &["-n", "--name"], + argparse::StoreTrue, + "Show the name stored on the slot", + ); + let _ = parser.refer(&mut login).add_option( + &["-l", "--login"], + argparse::StoreTrue, + "Show the login stored on the slot", + ); + let _ = parser.refer(&mut password).add_option( + &["-p", "--password"], + argparse::StoreTrue, + "Show the password stored on the slot", + ); + let _ = parser.refer(&mut quiet).add_option( + &["-q", "--quiet"], + argparse::StoreTrue, + "Print the stored data without description", + ); + parse(&parser, args)?; + drop(parser); + + commands::pws_get(slot, name, login, password, quiet) +} + /// Parse the command-line arguments and return the selected command and /// the remaining arguments for the command. fn parse_arguments(args: Vec) -> Result<(Command, Vec)> { -- cgit v1.2.1