Skip to content

Commit

Permalink
fix: process multiple host entries (#331)
Browse files Browse the repository at this point in the history
PR #306 broke russh config parsing
for me.

My ssh config looks roughly like this:

```
Host *
    Common settings
Host wanted
   Host-specific settings
```

Before #306 common settings were
ignored, but after - common settings were applied, yet config parser
stops right on next `Host` section start, so settings I need were
ignored instead.

This PR brings behavior closer to what openssh itself does - for
requested host, it applies all the settings with maching glob, not only
the first one.
  • Loading branch information
CertainLach authored Aug 24, 2024
1 parent 4115b8f commit d5b303c
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions russh-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,20 @@ pub enum AddKeysToAgent {
}

pub fn parse(file: &str, host: &str) -> Result<Config, Error> {
let mut config: Option<Config> = None;
let mut config = Config::default(host);
let mut matches_current = false;
for line in file.lines() {
let tokens = line.trim().splitn(2, ' ').collect::<Vec<&str>>();
if tokens.len() == 2 {
let (key, value) = (tokens.first().unwrap_or(&""), tokens.get(1).unwrap_or(&""));
let lower = key.to_lowercase();
if let Some(ref mut config) = config {
if lower.as_str() == "host" {
matches_current = value
.split_whitespace()
.any(|x| check_host_against_glob_pattern(host, x));
}
if matches_current {
match lower.as_str() {
"host" => break,
"user" => {
config.user.clear();
config.user.push_str(value.trim_start());
Expand Down Expand Up @@ -166,22 +171,10 @@ pub fn parse(file: &str, host: &str) -> Result<Config, Error> {
debug!("{:?}", key);
}
}
} else if lower.as_str() == "host"
&& value
.split_whitespace()
.any(|x| check_host_against_glob_pattern(host, x))
{
let mut c = Config::default(host);
c.port = 22;
config = Some(c)
}
}
}
if let Some(config) = config {
Ok(config)
} else {
Err(Error::HostNotFound)
}
Ok(config)
}

fn check_host_against_glob_pattern(candidate: &str, glob_pattern: &str) -> bool {
Expand Down

0 comments on commit d5b303c

Please sign in to comment.