From d3deca60cd72eccd73486f61ecbc0b8806c63dcc Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 30 Jun 2021 12:08:28 -0700 Subject: [PATCH] ssh-config: allow multiple IdentityFile lines in ssh config The man page states: > For each parameter, the first obtained value will be used. but then later says: > It is possible to have multiple identity files specified in > configuration files; all these identities will be tried in sequence. > Multiple IdentityFile directives will add to the list of identities > tried (this behaviour differs from that of other configuration > directives). So that's what this commit does --- docs/changelog.md | 1 + wezterm-ssh/src/config.rs | 73 +++++++++++++++++++++++++++++++++------ 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 559040ae7..f8d400b8c 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -50,6 +50,7 @@ As features stabilize some brief notes about them will accumulate here. * Removed: `Parasio Dark` color scheme; it was a duplicate of the correctly named `Paraiso Dark` scheme. Thanks to [@adrian5](https://github.com/adrian5)! [#906](https://github.com/wez/wezterm/pull/906) * Fixed: key repeat on Wayland now respects the system specified key repeat rate, and doesn't "stick". [#669](https://github.com/wez/wezterm/issues/669) * Fixed: `force_reverse_video_cursor` wasn't correctly swapping the cursor colors in all cases. [#706](https://github.com/wez/wezterm/issues/706) +* Fixed: allow multuple `IdentityFile` lines in an ssh_config block to be considered ### 20210502-154244-3f7122cb diff --git a/wezterm-ssh/src/config.rs b/wezterm-ssh/src/config.rs index e3a0169ff..a525398ba 100644 --- a/wezterm-ssh/src/config.rs +++ b/wezterm-ssh/src/config.rs @@ -306,16 +306,26 @@ impl ParsedConfigFile { continue; } - if let Some(group) = groups.last_mut() { - group - .options - .entry(k.to_string()) - .or_insert_with(|| v.to_string()); - } else { + fn add_option(options: &mut ConfigMap, k: String, v: &str) { + // first option wins in ssh_config, except for identityfile + // which explicitly allows multiple entries to combine together + let is_identity_file = k == "identityfile"; options - .entry(k.to_string()) + .entry(k) + .and_modify(|e| { + if is_identity_file { + e.push(' '); + e.push_str(v); + } + }) .or_insert_with(|| v.to_string()); } + + if let Some(group) = groups.last_mut() { + add_option(&mut group.options, k, v); + } else { + add_option(options, k, v); + } } } } @@ -567,11 +577,18 @@ impl Config { *value = value.replace(t, v); } else if t == "%d" { if let Some(home) = self.resolve_home() { - if value.starts_with("~/") { - value.replace_range(0..1, &home); - } else { - *value = value.replace(t, &home); + let mut items = value + .split_whitespace() + .map(|s| s.to_string()) + .collect::>(); + for item in &mut items { + if item.starts_with("~/") { + item.replace_range(0..1, &home); + } else { + *item = item.replace(t, &home); + } } + *value = items.join(" "); } } } @@ -684,6 +701,40 @@ Config { ); } + #[test] + fn multiple_identityfile() { + let mut config = Config::new(); + + let mut fake_env = ConfigMap::new(); + fake_env.insert("HOME".to_string(), "/home/me".to_string()); + fake_env.insert("USER".to_string(), "me".to_string()); + config.assign_environment(fake_env); + + config.add_config_string( + r#" + Host foo + HostName 10.0.0.1 + User foo + IdentityFile "~/.ssh/id_pub.dsa" + IdentityFile "~/.ssh/id_pub.rsa" + "#, + ); + + let opts = config.for_host("foo"); + snapshot!( + opts, + r#" +{ + "hostname": "10.0.0.1", + "identityfile": "/home/me/.ssh/id_pub.dsa /home/me/.ssh/id_pub.rsa", + "port": "22", + "user": "foo", + "userknownhostsfile": "/home/me/.ssh/known_hosts /home/me/.ssh/known_hosts2", +} +"# + ); + } + #[test] fn sub_tilde() { let mut config = Config::new();