From 9b73d152a4e154ac24491b98b3c5736aaa57bc0e Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 27 Dec 2018 17:13:38 +0100 Subject: Implement the pws set subcommand This patch adds the pws set subcommand that writes a PWS slot. --- nitrocli/src/args.rs | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'nitrocli/src/args.rs') diff --git a/nitrocli/src/args.rs b/nitrocli/src/args.rs index 1a54c43..9982d0f 100644 --- a/nitrocli/src/args.rs +++ b/nitrocli/src/args.rs @@ -327,12 +327,14 @@ impl str::FromStr for PinCommand { #[derive(Debug)] enum PwsCommand { Get, + Set, } impl PwsCommand { fn execute(&self, args: Vec) -> Result<()> { match *self { PwsCommand::Get => pws_get(args), + PwsCommand::Set => pws_set(args), } } } @@ -344,6 +346,7 @@ impl fmt::Display for PwsCommand { "{}", match *self { PwsCommand::Get => "get", + PwsCommand::Set => "set", } ) } @@ -355,6 +358,7 @@ impl str::FromStr for PwsCommand { fn from_str(s: &str) -> result::Result { match s { "get" => Ok(PwsCommand::Get), + "set" => Ok(PwsCommand::Set), _ => Err(()), } } @@ -796,7 +800,7 @@ fn pws(args: Vec) -> Result<()> { let _ = parser.refer(&mut subcommand).required().add_argument( "subcommand", argparse::Store, - "The subcommand to execute (get)", + "The subcommand to execute (get|set)", ); let _ = parser.refer(&mut subargs).add_argument( "arguments", @@ -851,6 +855,40 @@ fn pws_get(args: Vec) -> Result<()> { commands::pws_get(slot, name, login, password, quiet) } +/// Set a slot of the password safe on the Nitrokey. +fn pws_set(args: Vec) -> Result<()> { + let mut slot: u8 = 0; + let mut name = String::new(); + let mut login = String::new(); + let mut password = String::new(); + let mut parser = argparse::ArgumentParser::new(); + parser.set_description("Writes 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).required().add_argument( + "name", + argparse::Store, + "The name to store on the slot", + ); + let _ = parser.refer(&mut login).required().add_argument( + "login", + argparse::Store, + "The login to store on the slot", + ); + let _ = parser.refer(&mut password).required().add_argument( + "password", + argparse::Store, + "The password to store on the slot", + ); + parse(&parser, args)?; + drop(parser); + + commands::pws_set(slot, &name, &login, &password) +} + /// 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