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
|
// main.rs
// *************************************************************************
// * Copyright (C) 2017 Daniel Mueller (deso@posteo.net) *
// * *
// * This program is free software: you can redistribute it and/or modify *
// * it under the terms of the GNU General Public License as published by *
// * the Free Software Foundation, either version 3 of the License, or *
// * (at your option) any later version. *
// * *
// * This program is distributed in the hope that it will be useful, *
// * but WITHOUT ANY WARRANTY; without even the implied warranty of *
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
// * GNU General Public License for more details. *
// * *
// * You should have received a copy of the GNU General Public License *
// * along with this program. If not, see <http://www.gnu.org/licenses/>. *
// *************************************************************************
#![deny(missing_docs)]
//! Nitrocli is a program providing a command line interface to certain
//! commands of the Nitrokey Storage device.
extern crate hid as libhid;
mod crc32;
mod error;
mod nitrokey;
mod pinentry;
use error::Error;
use std::process;
use std::result;
type Result<T> = result::Result<T, Error>;
type NitroFunc = Fn(&mut libhid::Handle) -> Result<()>;
/// Find and open the nitrokey device and execute a function on it.
fn nitrokey_do(function: &NitroFunc) -> Result<()> {
let hid = libhid::init()?;
// The Manager::find method is plain stupid as it still returns an
// iterable. Using it does not help in more concise error handling.
for device in hid.devices() {
if device.vendor_id() == nitrokey::VID && device.product_id() == nitrokey::PID {
return function(&mut device.open()?);
}
}
return Err(Error::Error("Nitrokey device not found".to_string()));
}
/// Open the encrypted volume on the nitrokey.
fn open() -> Result<()> {
return nitrokey_do(&|handle| {
let passphrase = pinentry::inquire_passphrase()?;
let payload = nitrokey::EnableEncryptedVolumeCommand::new(&passphrase);
let report = nitrokey::Report::from(payload);
handle.feature().send_to(0, report.as_ref())?;
return Ok(());
});
}
/// Close the previously opened encrypted volume.
fn close() -> Result<()> {
return nitrokey_do(&|handle| {
let payload = nitrokey::DisableEncryptedVolumeCommand::new();
let report = nitrokey::Report::from(payload);
handle.feature().send_to(0, report.as_ref())?;
return Ok(());
});
}
// A macro for generating a match of the different supported commands.
// Each supplied command is converted into a string and matched against.
macro_rules! commands {
( $str:expr, [ $( $command:expr), *] ) => {
match &*$str.to_string() {
$(
stringify!($command) => {
if let Err(err) = $command() {
println!("{}", err);
return 1
}
return 0
},
)*
x => {
println!("Invalid command: {}", x);
println!("Available commands: {}", stringify!( $($command)* ));
return 1
},
}
}
}
fn run() -> i32 {
let argv: Vec<String> = std::env::args().collect();
if argv.len() != 2 {
println!("Usage: {} <command>", argv[0]);
return 1;
}
commands!(&argv[1], [open, close]);
}
fn main() {
process::exit(run());
}
|