Philipp Marek
2016-Aug-29 11:03 UTC
[PATCH] Make "ssh" try different configuration filenames
To provide a bit more backwards-compatible (which is nice for eg. NFS- shared /home directories) try a few version-number based names. Eg., for "OpenSSH_7.3" the strings that are tried after "~/.ssh/config" are "_7.3", "_7", and "". --- ssh.c | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/ssh.c b/ssh.c index 03a23fb..25359fe 100644 --- a/ssh.c +++ b/ssh.c @@ -464,7 +464,8 @@ static void process_config_files(const char *host_arg, struct passwd *pw, int post_canon) { char buf[PATH_MAX]; - int r; + char *version_postfix; + int r, len; if (config != NULL) { if (strcasecmp(config, "none") != 0 && @@ -473,12 +474,34 @@ process_config_files(const char *host_arg, struct passwd *pw, int post_canon) fatal("Can't open user config file %.100s: " "%.100s", config, strerror(errno)); } else { - r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, - _PATH_SSH_USER_CONFFILE); - if (r > 0 && (size_t)r < sizeof(buf)) - (void)read_config_file(buf, pw, host, host_arg, - &options, SSHCONF_CHECKPERM | SSHCONF_USERCONF | - (post_canon ? SSHCONF_POSTCANON : 0)); + version_postfix = strchr(SSH_VERSION, '_'); + if (!version_postfix) + version_postfix = ""; + + /* Find the best fitting config file, + * Ie. try "_7.3", "_7", and "". */ + len = strlen(version_postfix); + while (1) { + r = snprintf(buf, sizeof buf, "%s/%s%.*s", pw->pw_dir, + _PATH_SSH_USER_CONFFILE, + len, version_postfix); + if (r > 0 && (size_t)r < sizeof(buf)) + if (read_config_file(buf, pw, host, host_arg, + &options, SSHCONF_CHECKPERM | SSHCONF_USERCONF | + (post_canon ? SSHCONF_POSTCANON : 0))) + break; + + /* Nothing to look at */ + if (!len) + break; + + /* Try a smaller fit; skip last digits, then non-digits. */ + len--; + while (len && isdigit(version_postfix[len-1])) + len--; + while (len && !isdigit(version_postfix[len-1])) + len--; + } /* Read systemwide configuration file after user config. */ (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, -- 2.9.3
Damien Miller
2016-Aug-29 13:29 UTC
[PATCH] Make "ssh" try different configuration filenames
On Mon, 29 Aug 2016, Philipp Marek wrote:> To provide a bit more backwards-compatible (which is nice for eg. NFS- > shared /home directories) try a few version-number based names.I'm not sure about this. We already have an IgnoreUnknown directive to skip keywords that aren't supported. Perhaps we could consider adding a percent_expand() to the include directive or a "localversion" clause to the "Match" keyword to get this capability in a more general form. E.g. Match localversion 7.* Include ~/.ssh/config/config-7x -d
Philipp Marek
2016-Aug-29 13:33 UTC
[PATCH] Make "ssh" try different configuration filenames
Hi Damien, thanks for the quick feedback!> > To provide a bit more backwards-compatible (which is nice for eg. NFS- > > shared /home directories) try a few version-number based names. > > I'm not sure about this. We already have an IgnoreUnknown directive to > skip keywords that aren't supported."Bad configuration option: IgnoreUnknown"> Perhaps we could consider adding a > percent_expand() to the include directive or a "localversion" clause to > the "Match" keyword to get this capability in a more general form. E.g. > > Match localversion 7.* > Include ~/.ssh/config/config-7xThat wouldn't help with old versions... My patch would allow a newer SSH to read "its" config file, which (among other things) "Include"s the files for older versions as well... Thanks for thinking about the merits!