From 96adac9c5d3399a4890f54515a92472067771303 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 25 Dec 2018 10:41:06 +0100 Subject: Implement the config set subcommand This change implements the config set subcommand. The subcommand changes the configuration of a Nitrokey device. Its structure is more complex as it allows partial modifications: The user does not have to change all settings, but may choose to change only some. At the same time, the binding settings can be either set to a value or disabled. Therefore, we have the --{num,caps,scrol}lock options to set a value and the --no-{num,caps,scrol}lock options to disable the value. If none of the two is set, the setting is not changed. --- nitrocli/src/commands.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'nitrocli/src/commands.rs') diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index 8ce1076..9aef2de 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -128,7 +128,7 @@ where { let mut data = data; let mut retry = 3; - let mut error_msg: Option<&str> = None; + let mut error_msg = None; loop { let passphrase = match pinentry::inquire_passphrase(pin, error_msg) { Ok(passphrase) => passphrase, @@ -304,6 +304,28 @@ pub fn config_get() -> Result<()> { Ok(()) } +/// Write the Nitrokey configuration. +pub fn config_set( + numlock: args::ConfigOption, + capslock: args::ConfigOption, + scrollock: args::ConfigOption, + user_password: Option, +) -> Result<()> { + let device = authenticate_admin(get_device()?)?; + let config = device + .get_config() + .map_err(|err| get_error("Could not get configuration", &err))?; + let config = nitrokey::Config { + numlock: numlock.or(config.numlock), + capslock: capslock.or(config.capslock), + scrollock: scrollock.or(config.scrollock), + user_password: user_password.unwrap_or(config.user_password), + }; + device + .write_config(config) + .map_err(|err| get_error("Could not set configuration", &err)) +} + fn get_otp(slot: u8, algorithm: args::OtpAlgorithm, device: &T) -> Result { match algorithm { args::OtpAlgorithm::Hotp => device.get_hotp_code(slot), -- cgit v1.2.1