Class OpenSshConfigFile
- java.lang.Object
-
- org.eclipse.jgit.internal.transport.ssh.OpenSshConfigFile
-
- All Implemented Interfaces:
SshConfigStore
public class OpenSshConfigFile extends Object implements SshConfigStore
Fairly complete configuration parser for the openssh ~/.ssh/config file.Both JSch 0.1.54 and Apache MINA sshd 2.1.0 have parsers for this, but both are buggy. Therefore we implement our own parser to read an openssh configuration file.
Limitations compared to the full openssh 7.5 parser:
- This parser does not handle Match or Include keywords.
- This parser does not do host name canonicalization.
Note that openssh's readconf.c is a validating parser; this parser does not validate entries.
This config does %-substitutions for the following tokens:
- %% - single %
- %C - short-hand for %l%h%p%r.
- %d - home directory path
- %h - remote host name
- %L - local host name without domain
- %l - FQDN of the local host
- %n - host name as specified in
lookup(String, int, String)
- %p - port number; if not given in
lookup(String, int, String)
replaced only if set in the config - %r - remote user name; if not given in
lookup(String, int, String)
replaced only if set in the config - %u - local user name
%i is not handled; Java has no concept of a "user ID". %T is always replaced by NONE.
- See Also:
- man ssh-config
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
OpenSshConfigFile.HostEntry
A host entry from the ssh config file.-
Nested classes/interfaces inherited from interface org.eclipse.jgit.transport.SshConfigStore
SshConfigStore.HostConfig
-
-
Field Summary
-
Fields inherited from interface org.eclipse.jgit.transport.SshConfigStore
EMPTY_CONFIG
-
-
Constructor Summary
Constructors Constructor Description OpenSshConfigFile(File home, File config, String localUserName)
Creates a newOpenSshConfigFile
that will read the config from fileconfig
use the given filehome
as "home" directory.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static boolean
flag(String value)
Converts a ssh config flag value (yes/true/on - no/false/off) into anboolean
.String
getLocalUserName()
Retrieves the local user name as given in the constructor.OpenSshConfigFile.HostEntry
lookup(String hostName, int port, String userName)
Locate the configuration for a specific host request.OpenSshConfigFile.HostEntry
lookupDefault(String hostName, int port, String userName)
Locate the configuration for a specific host request and if the configuration has no values forSshConstants.HOST_NAME
,SshConstants.PORT
,SshConstants.USER
, orSshConstants.CONNECTION_ATTEMPTS
, fill those values with defaults from the arguments: ssh config key value from argumentHostName
hostName
Port
port > 0 ? port : 22
User
userName
ConnectionAttempts
1
static int
positive(String value)
Converts a positive value into anint
.static int
timeSpec(String value)
Converts an OpenSSH time value into a number of seconds.String
toString()
protected String
validate(String key, String value)
Hook to perform validation on a single value, or to sanitize it.protected List<String>
validate(String key, List<String> value)
Hook to perform validation on values, or to sanitize them.
-
-
-
Constructor Detail
-
OpenSshConfigFile
public OpenSshConfigFile(@NonNull File home, @NonNull File config, @NonNull String localUserName)
Creates a newOpenSshConfigFile
that will read the config from fileconfig
use the given filehome
as "home" directory.- Parameters:
home
- user's home directory for the purpose of ~ replacementconfig
- file to load.localUserName
- user name of the current user on the local host OS
-
-
Method Detail
-
lookup
@NonNull public OpenSshConfigFile.HostEntry lookup(@NonNull String hostName, int port, String userName)
Locate the configuration for a specific host request.- Specified by:
lookup
in interfaceSshConfigStore
- Parameters:
hostName
- the name the user has supplied to the SSH tool. This may be a real host name, or it may just be a "Host" block in the configuration file.port
- the user supplied; <= 0 if noneuserName
- the user supplied, may benull
or empty if none given- Returns:
- the configuration for the requested name.
-
lookupDefault
@NonNull public OpenSshConfigFile.HostEntry lookupDefault(@NonNull String hostName, int port, String userName)
Description copied from interface:SshConfigStore
Locate the configuration for a specific host request and if the configuration has no values forSshConstants.HOST_NAME
,SshConstants.PORT
,SshConstants.USER
, orSshConstants.CONNECTION_ATTEMPTS
, fill those values with defaults from the arguments:ssh config key value from argument HostName
hostName
Port
port > 0 ? port : 22
User
userName
ConnectionAttempts
1
- Specified by:
lookupDefault
in interfaceSshConfigStore
- Parameters:
hostName
- host name to look upport
- port number; <= 0 if noneuserName
- the user name, may benull
or empty if none given- Returns:
- the configuration for the requested name.
-
validate
protected String validate(String key, String value)
Hook to perform validation on a single value, or to sanitize it. If this throws an (unchecked) exception, parsing of the file is abandoned.- Parameters:
key
- of the entryvalue
- as read from the config file- Returns:
- the validated and possibly sanitized value
-
validate
protected List<String> validate(String key, List<String> value)
Hook to perform validation on values, or to sanitize them. If this throws an (unchecked) exception, parsing of the file is abandoned.- Parameters:
key
- of the entryvalue
- list of arguments as read from the config file- Returns:
- a
List
of values, possibly empty and possibly containing empty elements, but not containingnull
-
positive
public static int positive(String value)
Converts a positive value into anint
.- Parameters:
value
- to convert- Returns:
- the value, or -1 if it wasn't a positive integral value
-
flag
public static boolean flag(String value)
Converts a ssh config flag value (yes/true/on - no/false/off) into anboolean
.- Parameters:
value
- to convert- Returns:
true
ifvalue
is "yes", "on", or "true";false
otherwise
-
timeSpec
public static int timeSpec(String value)
Converts an OpenSSH time value into a number of seconds. The format is defined by OpenSSH as a sequence of (positive) integers with suffixes for seconds, minutes, hours, days, and weeks.- Parameters:
value
- to convert- Returns:
- the parsed value as a number of seconds, or -1 if the value is not a valid OpenSSH time value
- See Also:
- OpenBSD man 5 sshd_config, section TIME FORMATS
-
getLocalUserName
public String getLocalUserName()
Retrieves the local user name as given in the constructor.- Returns:
- the user name
-
-