From 1818cce7f052816cef232ca34398042e9f544b8e Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 3 Aug 2019 11:35:44 -0700 Subject: [PATCH] factor out ssh connection code --- src/main.rs | 2 + src/server/client.rs | 196 +--------------------------------------- src/ssh.rs | 207 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 212 insertions(+), 193 deletions(-) create mode 100644 src/ssh.rs diff --git a/src/main.rs b/src/main.rs index 442a9c3b7..8c32143b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,8 @@ mod mux; mod opengl; mod ratelim; mod server; +mod ssh; + use crate::frontend::FrontEndSelection; use crate::mux::domain::{Domain, LocalDomain}; use crate::mux::Mux; diff --git a/src/server/client.rs b/src/server/client.rs index 80bee48cc..7f5ed15ad 100644 --- a/src/server/client.rs +++ b/src/server/client.rs @@ -9,12 +9,13 @@ use crate::server::domain::{ClientDomain, ClientDomainConfig}; use crate::server::pollable::*; use crate::server::tab::ClientTab; use crate::server::UnixStream; +use crate::ssh::ssh_connect; use crossbeam_channel::TryRecvError; use failure::{bail, err_msg, format_err, Fallible}; use filedescriptor::{pollfd, AsRawSocketDescriptor}; use log::info; use promise::{Future, Promise}; -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; use std::io::Write; use std::net::TcpStream; use std::path::Path; @@ -275,198 +276,7 @@ impl Reconnectable { } fn ssh_connect(&mut self, ssh_dom: SshDomain) -> Fallible<()> { - let mut sess = ssh2::Session::new()?; - - let tcp = TcpStream::connect(&ssh_dom.remote_address)?; - sess.set_tcp_stream(tcp); - sess.handshake()?; - - if let Ok(mut known_hosts) = sess.known_hosts() { - let varname = if cfg!(windows) { "USERPROFILE" } else { "HOME" }; - let var = std::env::var_os(varname).ok_or_else(|| { - failure::format_err!("environment variable {} is missing", varname) - })?; - let file = Path::new(&var).join(".ssh/known_hosts"); - if file.exists() { - known_hosts - .read_file(&file, ssh2::KnownHostFileKind::OpenSSH) - .map_err(|e| { - failure::format_err!("reading known_hosts file {}: {}", file.display(), e) - })?; - } - - let remote_host_name = ssh_dom.remote_address.split(':').next().ok_or_else(|| { - format_err!( - "expected remote_address to have the form 'host:port', but have {}", - ssh_dom.remote_address - ) - })?; - - let (key, key_type) = sess - .host_key() - .ok_or_else(|| failure::err_msg("failed to get ssh host key"))?; - - let fingerprint = sess - .host_key_hash(ssh2::HashType::Sha256) - .map(|fingerprint| { - format!( - "SHA256:{}", - base64::encode_config( - fingerprint, - base64::Config::new(base64::CharacterSet::Standard, false) - ) - ) - }) - .or_else(|| { - // Querying for the Sha256 can fail if for example we were linked - // against libssh < 1.9, so let's fall back to Sha1 in that case. - sess.host_key_hash(ssh2::HashType::Sha1).map(|fingerprint| { - let mut res = vec![]; - write!(&mut res, "SHA1").ok(); - for b in fingerprint { - write!(&mut res, ":{:02x}", *b).ok(); - } - String::from_utf8(res).unwrap() - }) - }) - .ok_or_else(|| failure::err_msg("failed to get host fingerprint"))?; - - use ssh2::CheckResult; - match known_hosts.check(&remote_host_name, key) { - CheckResult::Match => {} - CheckResult::NotFound => { - let allow = tinyfiledialogs::message_box_yes_no( - "wezterm", - &format!( - "SSH host {} is not yet trusted.\n\ - {:?} Fingerprint: {}.\n\ - Trust and continue connecting?", - ssh_dom.remote_address, key_type, fingerprint - ), - tinyfiledialogs::MessageBoxIcon::Question, - tinyfiledialogs::YesNo::No, - ); - - if tinyfiledialogs::YesNo::No == allow { - bail!("user declined to trust host"); - } - - known_hosts - .add( - remote_host_name, - key, - &ssh_dom.remote_address, - key_type.into(), - ) - .map_err(|e| { - failure::format_err!("adding known_hosts entry in memory: {}", e) - })?; - - known_hosts - .write_file(&file, ssh2::KnownHostFileKind::OpenSSH) - .map_err(|e| { - failure::format_err!( - "writing known_hosts file {}: {}", - file.display(), - e - ) - })?; - } - CheckResult::Mismatch => { - tinyfiledialogs::message_box_ok( - "wezterm", - &format!( - "host key mismatch for ssh server {}.\n\ - Got fingerprint {} instead of expected value from known_hosts\n\ - file {}.\n\ - Refusing to connect.", - ssh_dom.remote_address, - fingerprint, - file.display() - ), - tinyfiledialogs::MessageBoxIcon::Error, - ); - bail!("host mismatch, man in the middle attack?!"); - } - CheckResult::Failure => { - tinyfiledialogs::message_box_ok( - "wezterm", - "Failed to load and check known ssh hosts", - tinyfiledialogs::MessageBoxIcon::Error, - ); - bail!("failed to check the known hosts"); - } - } - } - - let methods: HashSet<&str> = sess.auth_methods(&ssh_dom.username)?.split(',').collect(); - - if !sess.authenticated() && methods.contains("publickey") { - if let Err(err) = sess.userauth_agent(&ssh_dom.username) { - log::info!("while attempting agent auth: {}", err); - } - } - - fn password_prompt(instructions: &str, prompt: &str, dom: &SshDomain) -> Option { - let text = format!( - "SSH Authentication for {} @ {}\n{}\n{}", - dom.username, dom.remote_address, instructions, prompt - ); - tinyfiledialogs::password_box("wezterm", &text) - } - - fn input_prompt(instructions: &str, prompt: &str, dom: &SshDomain) -> Option { - let text = format!( - "SSH Authentication for {} @ {}\n{}\n{}", - dom.username, dom.remote_address, instructions, prompt - ); - tinyfiledialogs::input_box("wezterm", &text, "") - } - - if !sess.authenticated() && methods.contains("keyboard-interactive") { - struct Prompt<'a> { - dom: &'a SshDomain, - } - - let mut prompt = Prompt { dom: &ssh_dom }; - impl<'a> ssh2::KeyboardInteractivePrompt for Prompt<'a> { - fn prompt<'b>( - &mut self, - _username: &str, - instructions: &str, - prompts: &[ssh2::Prompt<'b>], - ) -> Vec { - prompts - .iter() - .map(|p| { - let func = if p.echo { - input_prompt - } else { - password_prompt - }; - - func(instructions, &p.text, &self.dom).unwrap_or_else(String::new) - }) - .collect() - } - } - - if let Err(err) = sess.userauth_keyboard_interactive(&ssh_dom.username, &mut prompt) { - log::error!("while attempting keyboard-interactive auth: {}", err); - } - } - - if !sess.authenticated() && methods.contains("password") { - let pass = password_prompt("", "Password", &ssh_dom) - .ok_or_else(|| failure::err_msg("password entry was cancelled"))?; - if let Err(err) = sess.userauth_password(&ssh_dom.username, &pass) { - log::error!("while attempting password auth: {}", err); - } - } - - if !sess.authenticated() { - failure::bail!("unable to authenticate session"); - } + let sess = ssh_connect(&ssh_dom.username, &ssh_dom.remote_address)?; let mut chan = sess.channel_session()?; chan.exec("wezterm cli proxy")?; diff --git a/src/ssh.rs b/src/ssh.rs new file mode 100644 index 000000000..c9d5ddb4c --- /dev/null +++ b/src/ssh.rs @@ -0,0 +1,207 @@ +use failure::{bail, format_err, Fallible}; +use std::collections::HashSet; +use std::io::Write; +use std::net::TcpStream; +use std::path::Path; + +fn password_prompt( + instructions: &str, + prompt: &str, + username: &str, + remote_address: &str, +) -> Option { + let text = format!( + "SSH Authentication for {} @ {}\n{}\n{}", + username, remote_address, instructions, prompt + ); + tinyfiledialogs::password_box("wezterm", &text) +} + +fn input_prompt( + instructions: &str, + prompt: &str, + username: &str, + remote_address: &str, +) -> Option { + let text = format!( + "SSH Authentication for {} @ {}\n{}\n{}", + username, remote_address, instructions, prompt + ); + tinyfiledialogs::input_box("wezterm", &text, "") +} + +pub fn ssh_connect(remote_address: &str, username: &str) -> Fallible { + let mut sess = ssh2::Session::new()?; + + let tcp = TcpStream::connect(&remote_address)?; + sess.set_tcp_stream(tcp); + sess.handshake()?; + + if let Ok(mut known_hosts) = sess.known_hosts() { + let varname = if cfg!(windows) { "USERPROFILE" } else { "HOME" }; + let var = std::env::var_os(varname) + .ok_or_else(|| failure::format_err!("environment variable {} is missing", varname))?; + let file = Path::new(&var).join(".ssh/known_hosts"); + if file.exists() { + known_hosts + .read_file(&file, ssh2::KnownHostFileKind::OpenSSH) + .map_err(|e| { + failure::format_err!("reading known_hosts file {}: {}", file.display(), e) + })?; + } + + let remote_host_name = remote_address.split(':').next().ok_or_else(|| { + format_err!( + "expected remote_address to have the form 'host:port', but have {}", + remote_address + ) + })?; + + let (key, key_type) = sess + .host_key() + .ok_or_else(|| failure::err_msg("failed to get ssh host key"))?; + + let fingerprint = sess + .host_key_hash(ssh2::HashType::Sha256) + .map(|fingerprint| { + format!( + "SHA256:{}", + base64::encode_config( + fingerprint, + base64::Config::new(base64::CharacterSet::Standard, false) + ) + ) + }) + .or_else(|| { + // Querying for the Sha256 can fail if for example we were linked + // against libssh < 1.9, so let's fall back to Sha1 in that case. + sess.host_key_hash(ssh2::HashType::Sha1).map(|fingerprint| { + let mut res = vec![]; + write!(&mut res, "SHA1").ok(); + for b in fingerprint { + write!(&mut res, ":{:02x}", *b).ok(); + } + String::from_utf8(res).unwrap() + }) + }) + .ok_or_else(|| failure::err_msg("failed to get host fingerprint"))?; + + use ssh2::CheckResult; + match known_hosts.check(&remote_host_name, key) { + CheckResult::Match => {} + CheckResult::NotFound => { + let allow = tinyfiledialogs::message_box_yes_no( + "wezterm", + &format!( + "SSH host {} is not yet trusted.\n\ + {:?} Fingerprint: {}.\n\ + Trust and continue connecting?", + remote_address, key_type, fingerprint + ), + tinyfiledialogs::MessageBoxIcon::Question, + tinyfiledialogs::YesNo::No, + ); + + if tinyfiledialogs::YesNo::No == allow { + bail!("user declined to trust host"); + } + + known_hosts + .add(remote_host_name, key, &remote_address, key_type.into()) + .map_err(|e| { + failure::format_err!("adding known_hosts entry in memory: {}", e) + })?; + + known_hosts + .write_file(&file, ssh2::KnownHostFileKind::OpenSSH) + .map_err(|e| { + failure::format_err!("writing known_hosts file {}: {}", file.display(), e) + })?; + } + CheckResult::Mismatch => { + tinyfiledialogs::message_box_ok( + "wezterm", + &format!( + "host key mismatch for ssh server {}.\n\ + Got fingerprint {} instead of expected value from known_hosts\n\ + file {}.\n\ + Refusing to connect.", + remote_address, + fingerprint, + file.display() + ), + tinyfiledialogs::MessageBoxIcon::Error, + ); + bail!("host mismatch, man in the middle attack?!"); + } + CheckResult::Failure => { + tinyfiledialogs::message_box_ok( + "wezterm", + "Failed to load and check known ssh hosts", + tinyfiledialogs::MessageBoxIcon::Error, + ); + bail!("failed to check the known hosts"); + } + } + } + + let methods: HashSet<&str> = sess.auth_methods(&username)?.split(',').collect(); + + if !sess.authenticated() && methods.contains("publickey") { + if let Err(err) = sess.userauth_agent(&username) { + log::info!("while attempting agent auth: {}", err); + } + } + + if !sess.authenticated() && methods.contains("keyboard-interactive") { + struct Prompt<'a> { + username: &'a str, + remote_address: &'a str, + } + + let mut prompt = Prompt { + username, + remote_address, + }; + impl<'a> ssh2::KeyboardInteractivePrompt for Prompt<'a> { + fn prompt<'b>( + &mut self, + _username: &str, + instructions: &str, + prompts: &[ssh2::Prompt<'b>], + ) -> Vec { + prompts + .iter() + .map(|p| { + let func = if p.echo { + input_prompt + } else { + password_prompt + }; + + func(instructions, &p.text, &self.username, &self.remote_address) + .unwrap_or_else(String::new) + }) + .collect() + } + } + + if let Err(err) = sess.userauth_keyboard_interactive(&username, &mut prompt) { + log::error!("while attempting keyboard-interactive auth: {}", err); + } + } + + if !sess.authenticated() && methods.contains("password") { + let pass = password_prompt("", "Password", username, remote_address) + .ok_or_else(|| failure::err_msg("password entry was cancelled"))?; + if let Err(err) = sess.userauth_password(username, &pass) { + log::error!("while attempting password auth: {}", err); + } + } + + if !sess.authenticated() { + failure::bail!("unable to authenticate session"); + } + + Ok(sess) +}