1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#![warn(missing_docs, rust_2018_compatibility, rust_2018_idioms, unused)]
//! 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<io::Error> for Error {
fn from(error: io::Error) -> Error {
Error::IoError(error)
}
}
#[derive(Debug)]
struct Options {
slot: u8,
file: Option<String>,
name: Option<String>,
}
fn parse_options() -> Result<Options, i32> {
let mut options = Options {
slot: 0,
file: None,
name: None,
};
let mut parser = argparse::ArgumentParser::new();
parser.set_description(
"Reads OTP configuration from a QR code and writes it to an OTP slot on a Nitrokey device.",
);
parser.refer(&mut options.slot).required().add_argument(
"slot",
argparse::Store,
"The slot to write the OTP data to",
);
parser.refer(&mut options.file).add_argument(
"file",
argparse::StoreOption,
"The file to read the QR code from",
);
parser.refer(&mut options.name).add_option(
&["-n", "--name"],
argparse::StoreOption,
"The name to store in the OTP slot",
);
parser.parse_args()?;
drop(parser);
Ok(options)
}
fn import_qr_code() -> Result<path::PathBuf, Error> {
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(options) => match run(options) {
Ok(()) => 0,
Err(err) => {
println!("{}", err);
1
}
},
Err(err) => err,
};
process::exit(status);
}
|