From cd6bcd3613c2a511ddf1f19f5739493de2c5a278 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Mon, 7 Jan 2019 20:23:02 +0000 Subject: Implement call to import to capture QR code --- Cargo.lock | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 70 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 168 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 3f41163..277ad6e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,12 +3,110 @@ name = "argparse" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "bitflags" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "fuchsia-zircon" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "fuchsia-zircon-sys" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.46" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "mktemp" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "uuid 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "nitrocli-otp-qr" version = "0.1.0" dependencies = [ "argparse 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "mktemp 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand" +version = "0.3.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc-serialize" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "uuid" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [metadata] "checksum argparse 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3f8ebf5827e4ac4fd5946560e6a99776ea73b596d80898f357007317a7141e47" +"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" +"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" +"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" +"checksum libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)" = "023a4cd09b2ff695f9734c1934145a315594b7986398496841c7031a5a1bbdbd" +"checksum mktemp 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "77001ceb9eed65439f3dc2a2543f9ba1417d912686bf224a7738d0966e6dcd69" +"checksum rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "15a732abf9d20f0ad8eeb6f909bf6868722d9a06e1e50802b6a70351f40b4eb1" +"checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd" +"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" +"checksum uuid 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "78c590b5bd79ed10aad8fb75f078a59d8db445af6c743e55c4a53227fc01c13f" +"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 8dec924..8315b8f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ license = "MIT" [dependencies] argparse = "0.2" +mktemp = "0.3" diff --git a/src/main.rs b/src/main.rs index 04140ed..b3fb3f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,39 @@ //! Reads OTP configuration from a QR code and writes it to an OTP slot on a Nitrokey device. +use std::fmt; +use std::fs; +use std::io; +use std::path; use std::process; +#[derive(Debug)] +enum Error { + IoError(io::Error), + Error(String), +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + Error::IoError(ref err) => write!(f, "IO error: {}", err), + Error::Error(ref string) => write!(f, "Error: {}", string), + } + } +} + +impl From<&str> for Error { + fn from(string: &str) -> Error { + Error::Error(string.to_string()) + } +} + +impl From for Error { + fn from(error: io::Error) -> Error { + Error::IoError(error) + } +} + #[derive(Debug)] struct Options { slot: u8, @@ -41,9 +72,46 @@ fn parse_options() -> Result { Ok(options) } +fn import_qr_code() -> Result { + let mut temp = mktemp::Temp::new_file()?; + let path = temp.to_path_buf(); + + let status = process::Command::new("import").arg(&path).status()?; + + if status.success() { + temp.release(); + Ok(path) + } else { + match status.code() { + Some(code) => Err(Error::Error(format!( + "import failed with error code {}", + code + ))), + None => Err(Error::from("import was terminated by a signal")), + } + } +} + +fn run(options: Options) -> Result<(), Error> { + let path = match options.file { + Some(ref file) => path::PathBuf::from(file), + None => import_qr_code()?, + }; + if options.file.is_none() { + fs::remove_file(&path)?; + } + Ok(()) +} + fn main() { let status = match parse_options() { - Ok(_) => 0, + Ok(options) => match run(options) { + Ok(()) => 0, + Err(err) => { + println!("{}", err); + 1 + } + }, Err(err) => err, }; process::exit(status); -- cgit v1.2.1