aboutsummaryrefslogtreecommitdiff
path: root/nitrocli/src/args.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2018-12-31 18:10:40 +0000
committerDaniel Mueller <deso@posteo.net>2019-01-01 17:14:51 -0800
commit8a59f307a2e0b9fa398ac200da44d8e5725150a7 (patch)
tree2db6b6bab9373a90ac54161342b5f60e149af002 /nitrocli/src/args.rs
parentabcaafee042c6f2036b822e6f1c6c2683a526d92 (diff)
downloadnitrocli-8a59f307a2e0b9fa398ac200da44d8e5725150a7.tar.gz
nitrocli-8a59f307a2e0b9fa398ac200da44d8e5725150a7.tar.bz2
Implement the pin command and rename clear to pin clear
We have functionality for changing the Nitrokey's user & admin PINs as well as for resetting the user PIN coming up. With the prospect of this new functionality arriving, it makes sense to introduce a new top-level command for the sole purpose of PIN management. This change introduces such a command, pin, and moves the existing clear command for clearing the PIN cache into it.
Diffstat (limited to 'nitrocli/src/args.rs')
-rw-r--r--nitrocli/src/args.rs88
1 files changed, 74 insertions, 14 deletions
diff --git a/nitrocli/src/args.rs b/nitrocli/src/args.rs
index b4733f6..edfd811 100644
--- a/nitrocli/src/args.rs
+++ b/nitrocli/src/args.rs
@@ -30,9 +30,9 @@ type Result<T> = result::Result<T, Error>;
/// A top-level command for nitrocli.
#[derive(Debug)]
pub enum Command {
- Clear,
Config,
Otp,
+ Pin,
Status,
Storage,
}
@@ -41,9 +41,9 @@ impl Command {
/// Execute this command with the given arguments.
pub fn execute(&self, args: Vec<String>) -> Result<()> {
match *self {
- Command::Clear => clear(args),
Command::Config => config(args),
Command::Otp => otp(args),
+ Command::Pin => pin(args),
Command::Status => status(args),
Command::Storage => storage(args),
}
@@ -56,9 +56,9 @@ impl fmt::Display for Command {
f,
"{}",
match *self {
- Command::Clear => "clear",
Command::Config => "config",
Command::Otp => "otp",
+ Command::Pin => "pin",
Command::Status => "status",
Command::Storage => "storage",
}
@@ -71,9 +71,9 @@ impl str::FromStr for Command {
fn from_str(s: &str) -> result::Result<Self, Self::Err> {
match s {
- "clear" => Ok(Command::Clear),
"config" => Ok(Command::Config),
"otp" => Ok(Command::Otp),
+ "pin" => Ok(Command::Pin),
"status" => Ok(Command::Status),
"storage" => Ok(Command::Storage),
_ => Err(()),
@@ -275,6 +275,42 @@ impl From<OtpMode> for nitrokey::OtpMode {
}
}
+#[derive(Debug)]
+enum PinCommand {
+ Clear,
+}
+
+impl PinCommand {
+ fn execute(&self, args: Vec<String>) -> Result<()> {
+ match *self {
+ PinCommand::Clear => pin_clear(args),
+ }
+ }
+}
+
+impl fmt::Display for PinCommand {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(
+ f,
+ "{}",
+ match *self {
+ PinCommand::Clear => "clear",
+ }
+ )
+ }
+}
+
+impl str::FromStr for PinCommand {
+ type Err = ();
+
+ fn from_str(s: &str) -> result::Result<Self, Self::Err> {
+ match s {
+ "clear" => Ok(PinCommand::Clear),
+ _ => Err(()),
+ }
+ }
+}
+
fn parse(parser: &argparse::ArgumentParser<'_>, args: Vec<String>) -> Result<()> {
if let Err(err) = parser.parse(args, &mut io::stdout(), &mut io::stderr()) {
Err(Error::ArgparseError(err))
@@ -387,15 +423,6 @@ fn storage_status(args: Vec<String>) -> Result<()> {
commands::storage_status()
}
-/// Clear the PIN as cached by various other commands.
-fn clear(args: Vec<String>) -> Result<()> {
- let mut parser = argparse::ArgumentParser::new();
- parser.set_description("Clears the cached passphrases");
- parse(&parser, args)?;
-
- commands::clear()
-}
-
/// Execute a config subcommand.
fn config(args: Vec<String>) -> Result<()> {
let mut subcommand = ConfigCommand::Get;
@@ -653,6 +680,39 @@ fn otp_status(args: Vec<String>) -> Result<()> {
commands::otp_status(all)
}
+/// Execute a PIN subcommand.
+fn pin(args: Vec<String>) -> Result<()> {
+ let mut subcommand = PinCommand::Clear;
+ let mut subargs = vec![];
+ let mut parser = argparse::ArgumentParser::new();
+ parser.set_description("Manages the Nitrokey PINs");
+ let _ = parser.refer(&mut subcommand).required().add_argument(
+ "subcommand",
+ argparse::Store,
+ "The subcommand to execute (clear)",
+ );
+ let _ = parser.refer(&mut subargs).add_argument(
+ "arguments",
+ argparse::List,
+ "The arguments for the subcommand",
+ );
+ parser.stop_on_first_argument(true);
+ parse(&parser, args)?;
+ drop(parser);
+
+ subargs.insert(0, format!("nitrocli pin {}", subcommand));
+ subcommand.execute(subargs)
+}
+
+/// Clear the PIN as cached by various other commands.
+fn pin_clear(args: Vec<String>) -> Result<()> {
+ let mut parser = argparse::ArgumentParser::new();
+ parser.set_description("Clears the cached PINs");
+ parse(&parser, args)?;
+
+ commands::pin_clear()
+}
+
/// Parse the command-line arguments and return the selected command and
/// the remaining arguments for the command.
fn parse_arguments(args: Vec<String>) -> Result<(Command, Vec<String>)> {
@@ -663,7 +723,7 @@ fn parse_arguments(args: Vec<String>) -> Result<(Command, Vec<String>)> {
let _ = parser.refer(&mut command).required().add_argument(
"command",
argparse::Store,
- "The command to execute (clear|config|otp|status|storage)",
+ "The command to execute (config|otp|pin|status|storage)",
);
let _ = parser.refer(&mut subargs).add_argument(
"arguments",