mirror of
https://github.com/l0ng-ai/tty7.git
synced 2026-10-03 08:02:02 +00:00
fix(ssh): log in as this machine's user when a host leaves User blank
The form says a blank User is resolved at connect, and the host card shows $USER, but the empty string went to the server as the user name. sshd refused it as an invalid user and tty7 reported every key as rejected.
This commit is contained in:
@@ -263,6 +263,22 @@ fn try_parse_port(s: &str) -> Option<u16> {
|
||||
s.parse::<u16>().ok().filter(|&p| p != 0)
|
||||
}
|
||||
|
||||
/// The user a profile logs in as: its own, or — left blank, as the form's
|
||||
/// "resolved at connect" says — this machine's login name, the way `ssh`
|
||||
/// itself does. Sending the blank on was an empty user name the server
|
||||
/// refused, reported as every key being rejected.
|
||||
pub fn login_user(profile_user: &str) -> String {
|
||||
if !profile_user.trim().is_empty() {
|
||||
return profile_user.to_string();
|
||||
}
|
||||
["USER", "LOGNAME", "USERNAME"]
|
||||
.iter()
|
||||
.filter_map(|var| std::env::var(var).ok())
|
||||
.map(|name| name.trim().to_string())
|
||||
.find(|name| !name.is_empty())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn to_connect_string(profile: &SshProfile) -> String {
|
||||
let host = if profile.host.contains(':') {
|
||||
format!("[{}]", profile.host)
|
||||
@@ -366,6 +382,14 @@ fn default_identity_candidates_in(home: &str) -> Vec<String> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_blank_user_logs_in_as_this_machines_user() {
|
||||
assert_eq!(login_user("deploy"), "deploy");
|
||||
let here = login_user("");
|
||||
assert!(!here.is_empty(), "a blank user must resolve to someone");
|
||||
assert_eq!(login_user(" "), here);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn profiles_saved_before_the_switch_existed_default_to_integrated() {
|
||||
let profile: SshProfile =
|
||||
|
||||
@@ -429,7 +429,7 @@ fn build_spec_inner(
|
||||
NativeSshSpec {
|
||||
host: profile.host.clone(),
|
||||
port: profile.port,
|
||||
user: profile.user.clone(),
|
||||
user: crate::core::ssh_profile::login_user(&profile.user),
|
||||
auth_mode: map_auth_mode(profile.auth),
|
||||
identity_files,
|
||||
agent_forward: profile.agent_forward,
|
||||
|
||||
Reference in New Issue
Block a user