| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use authenticator::{ |
| 6 |
authenticatorservice::AuthenticatorService, statecallback::StateCallback, Pin, StatusPinUv, |
| 7 |
StatusUpdate, |
| 8 |
}; |
| 9 |
use getopts::Options; |
| 10 |
use std::sync::mpsc::{channel, RecvError}; |
| 11 |
use std::{env, thread}; |
| 12 |
|
| 13 |
fn print_usage(program: &str, opts: Options) { |
| 14 |
let brief = format!("Usage: {program} [options]"); |
| 15 |
print!("{}", opts.usage(&brief)); |
| 16 |
} |
| 17 |
|
| 18 |
fn main() { |
| 19 |
env_logger::init(); |
| 20 |
|
| 21 |
let args: Vec<String> = env::args().collect(); |
| 22 |
let program = args[0].clone(); |
| 23 |
|
| 24 |
let mut opts = Options::new(); |
| 25 |
opts.optflag("h", "help", "print this help menu").optopt( |
| 26 |
"t", |
| 27 |
"timeout", |
| 28 |
"timeout in seconds", |
| 29 |
"SEC", |
| 30 |
); |
| 31 |
opts.optflag("h", "help", "print this help menu"); |
| 32 |
let matches = match opts.parse(&args[1..]) { |
| 33 |
Ok(m) => m, |
| 34 |
Err(f) => panic!("{}", f.to_string()), |
| 35 |
}; |
| 36 |
if matches.opt_present("help") { |
| 37 |
print_usage(&program, opts); |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
let mut manager = |
| 42 |
AuthenticatorService::new().expect("The auth service should initialize safely"); |
| 43 |
manager.add_u2f_usb_hid_platform_transports(); |
| 44 |
|
| 45 |
let timeout_ms = match matches.opt_get_default::<u64>("timeout", 25) { |
| 46 |
Ok(timeout_s) => { |
| 47 |
println!("Using {}s as the timeout", &timeout_s); |
| 48 |
timeout_s * 1_000 |
| 49 |
} |
| 50 |
Err(e) => { |
| 51 |
println!("{e}"); |
| 52 |
print_usage(&program, opts); |
| 53 |
return; |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
let new_pin = rpassword::prompt_password_stderr("Enter new PIN: ").expect("Failed to read PIN"); |
| 58 |
let repeat_new_pin = |
| 59 |
rpassword::prompt_password_stderr("Enter it again: ").expect("Failed to read PIN"); |
| 60 |
if new_pin != repeat_new_pin { |
| 61 |
println!("PINs did not match!"); |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
let (status_tx, status_rx) = channel::<StatusUpdate>(); |
| 66 |
thread::spawn(move || loop { |
| 67 |
match status_rx.recv() { |
| 68 |
Ok(StatusUpdate::InteractiveManagement(..)) => { |
| 69 |
panic!("STATUS: This can't happen when doing non-interactive usage"); |
| 70 |
} |
| 71 |
Ok(StatusUpdate::SelectDeviceNotice) => { |
| 72 |
println!("STATUS: Please select a device by touching one of them."); |
| 73 |
} |
| 74 |
Ok(StatusUpdate::PresenceRequired) => { |
| 75 |
println!("STATUS: waiting for user presence"); |
| 76 |
} |
| 77 |
Ok(StatusUpdate::PinUvError(StatusPinUv::PinRequired(sender))) => { |
| 78 |
let raw_pin = |
| 79 |
rpassword::prompt_password_stderr("Enter PIN: ").expect("Failed to read PIN"); |
| 80 |
sender.send(Pin::new(&raw_pin)).expect("Failed to send PIN"); |
| 81 |
continue; |
| 82 |
} |
| 83 |
Ok(StatusUpdate::PinUvError(StatusPinUv::InvalidPin(sender, attempts))) => { |
| 84 |
println!( |
| 85 |
"Wrong PIN! {}", |
| 86 |
attempts.map_or("Try again.".to_string(), |a| format!( |
| 87 |
"You have {a} attempts left." |
| 88 |
)) |
| 89 |
); |
| 90 |
let raw_pin = |
| 91 |
rpassword::prompt_password_stderr("Enter PIN: ").expect("Failed to read PIN"); |
| 92 |
sender.send(Pin::new(&raw_pin)).expect("Failed to send PIN"); |
| 93 |
continue; |
| 94 |
} |
| 95 |
Ok(StatusUpdate::PinUvError(StatusPinUv::PinAuthBlocked)) => { |
| 96 |
panic!("Too many failed attempts in one row. Your device has been temporarily blocked. Please unplug it and plug in again.") |
| 97 |
} |
| 98 |
Ok(StatusUpdate::PinUvError(StatusPinUv::PinBlocked)) => { |
| 99 |
panic!("Too many failed attempts. Your device has been blocked. Reset it.") |
| 100 |
} |
| 101 |
Ok(StatusUpdate::PinUvError(StatusPinUv::InvalidUv(attempts))) => { |
| 102 |
println!( |
| 103 |
"Wrong UV! {}", |
| 104 |
attempts.map_or("Try again.".to_string(), |a| format!( |
| 105 |
"You have {a} attempts left." |
| 106 |
)) |
| 107 |
); |
| 108 |
continue; |
| 109 |
} |
| 110 |
Ok(StatusUpdate::PinUvError(StatusPinUv::UvBlocked)) => { |
| 111 |
println!("Too many failed UV-attempts."); |
| 112 |
continue; |
| 113 |
} |
| 114 |
Ok(StatusUpdate::PinUvError(e)) => { |
| 115 |
panic!("Unexpected error: {:?}", e) |
| 116 |
} |
| 117 |
Ok(StatusUpdate::SelectResultNotice(_, _)) => { |
| 118 |
panic!("Unexpected select device notice") |
| 119 |
} |
| 120 |
Err(RecvError) => { |
| 121 |
println!("STATUS: end"); |
| 122 |
return; |
| 123 |
} |
| 124 |
} |
| 125 |
}); |
| 126 |
|
| 127 |
let (reset_tx, reset_rx) = channel(); |
| 128 |
let rs_tx = reset_tx; |
| 129 |
let callback = StateCallback::new(Box::new(move |rv| { |
| 130 |
let _ = rs_tx.send(rv); |
| 131 |
})); |
| 132 |
|
| 133 |
if let Err(e) = manager.set_pin(timeout_ms, Pin::new(&new_pin), status_tx, callback) { |
| 134 |
panic!("Couldn't call set_pin: {:?}", e); |
| 135 |
}; |
| 136 |
|
| 137 |
let reset_result = reset_rx |
| 138 |
.recv() |
| 139 |
.expect("Problem receiving, unable to continue"); |
| 140 |
match reset_result { |
| 141 |
Ok(()) => { |
| 142 |
println!("PIN successfully set!"); |
| 143 |
} |
| 144 |
Err(e) => panic!("Setting PIN failed: {:?}", e), |
| 145 |
}; |
| 146 |
} |